当前位置:网站首页>[Luogu] p2709 little B's inquiry
[Luogu] p2709 little B's inquiry
2022-07-26 23:19:00 【Record algorithm solution】
Title address :
https://www.luogu.com.cn/problem/P2709
Title Description :
Small B There's a long one for n n n Integer sequence of a a a, The value range is [ 1 , k ] [1,k] [1,k]. He has m m m A asked , Each query is given an interval [ l , r ] [l,r] [l,r], seek : ∑ i = 1 k c i 2 \sum\limits_{i=1}^k c_i^2 i=1∑kci2 among c i c_i ci Representation number i i i stay [ l , r ] [l,r] [l,r] The number of occurrences in . Small B Please help him answer the question .
Input format :
The first row has three integers n , m , k n,m,k n,m,k.
The second line n n n It's an integer , It means small B Sequence .
Next m m m That's ok , Two integers per line l , r l,r l,r.
Output format :
Output m m m That's ok , One integer per row , The answer to a question .
Data range :
about 100 % 100\% 100% The data of , 1 ≤ n , m , k ≤ 5 × 1 0 4 1\le n,m,k \le 5\times 10^4 1≤n,m,k≤5×104.
Can use Mo team , Inquiries can be averaged O ( n ) O(\sqrt n) O(n) Transfer . Reference resources https://blog.csdn.net/qq_46105170/article/details/125827776. The code is as follows :
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 5e4 + 10;
int n, m, k, len;
int a[N], bel[N], cnt[N];
long res[N], ans;
struct Query {
int id, l, r;
} q[N];
void add(int x) {
ans -= cnt[x] * cnt[x];
cnt[x]++;
ans += cnt[x] * cnt[x];
}
void del(int x) {
ans -= cnt[x] * cnt[x];
cnt[x]--;
ans += cnt[x] * cnt[x];
}
int main() {
scanf("%d%d%d", &n, &m, &k);
len = sqrt(n);
for (int i = 1; i <= n; i++) {
bel[i] = i / len;
scanf("%d", &a[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q, q + m, [&](Query& q1, Query& q2) {
int l1 = q1.l, l2 = q2.l;
if (bel[l1] != bel[l2]) return bel[l1] < bel[l2];
int r1 = q1.r, r2 = q2.r;
if (bel[l1] & 1) return r1 < r2;
else return r1 > r2;
});
for (int k = 0, i = 0, j = 1; k < m; k++) {
int id = q[k].id, l = q[k].l, r = q[k].r;
while (i < r) add(a[++i]);
while (i > r) del(a[i--]);
while (j < l) del(a[j++]);
while (j > l) add(a[--j]);
res[id] = ans;
}
for (int i = 0; i < m; i++) printf("%ld\n", res[i]);
}
Preprocessing time complexity O ( m log m ) O(m\log m) O(mlogm), Time of each inquiry O ( n ) O(\sqrt n) O(n), Space O ( n + k ) O(n+k) O(n+k).
边栏推荐
- A13 processor has become the biggest highlight of iphone11 series: its performance is twice that of Kirin 980!
- C.Net timestamp and time conversion support time zone
- Basic select statement
- org.yaml.snakeyaml.scanner. ScannerException: mapping values are not allowed here in ‘reader‘, line
- Hcia-r & s self use notes (19) VLAN configuration and experiment, routing between VLANs
- 云原生微服务第一章之服务器环境说明
- Mate30 series release: how far can Huawei go in reconstructing images?
- TypeScript阶段学习
- Typescript stage learning
- Recruit | PostgreSQL database R & D engineers every week, with an annual salary of 60+, high salary for famous enterprises, and challenge yourself!
猜你喜欢
![[postgresql]postgresqlg use generate_ Series() function completes statistics](/img/62/893986eb97a61f4e9ef32abc8d2a90.png)
[postgresql]postgresqlg use generate_ Series() function completes statistics

Esmfold: a new breakthrough in protein structure prediction after alphafold2

An online accident, I suddenly realized the essence of asynchrony

正则表达式与绕过案例复现

The most classic Nature paper on Alzheimer's disease is suspected of fraud

json格式化小工具--pyqt5实例

Shardingsphere JDBC keyword problem

华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应
电脑开机后内存占用过高(50%以上)

Do you know the common core types of magnetic ring inductors?
随机推荐
DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主
Shardingsphere JDBC keyword problem
Easily implement seckill system with redis! (including code)
Xinding acquires Ziguang holdings! Wanye enterprise: comprehensive transformation of integrated circuits!
Page file system based on C language
黑马瑞吉外卖之新增员工
Database full stack Engineers (devdbops) have low down payment and high return, and pay after employment
比海豹便宜,造型炸裂空间大,20万左右真没对手?长安全新“王炸”这样选才划算
8-其他编程语言--记录
pgsql -&gt; flink cdc -&gt; flink -&gt; Mysql, if a PgSQL CDC
Do you know the common core types of magnetic ring inductors?
Promote the replacement of X86 with arm server chips, and Huawei and Feiteng carry the banner of localization!
Is test development development development?
Apifox -- a better API testing tool than postman
Signal debugging document developed by car
Day07 MySQL knowledge points re summary and multi table query
8 other programming languages -- Recording
HCIA-R&S自用笔记(21)STP技术背景、STP基础和数据包结构、STP选举规则及案例
json格式化小工具--pyqt5实例
Esmfold: a new breakthrough in protein structure prediction after alphafold2