#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
int f[MAXN], ans, n, sum1all, sum2all;
int bit1[550];
void add1(int x, int v) {
sum1all += v;
for(;x<550;x+=x&-x) bit1[x] += v;
}
int sum1(int x) {
int s=0;
for(;x;x-=x&-x) s += bit1[x];
return s;
}
int bit2[550];
void add2(int x, int v) {
sum2all += v;
for(;x<550;x+=x&-x) bit2[x] += v;
}
int sum2(int x) {
int s=0;
for(;x;x-=x&-x) s += bit2[x];
return s;
}
int bit3[MAXN];
void add3(int x, int v) {
x++;
for(;x<MAXN;x+=x&-x) bit3[x] += v;
}
int sum3(int x) {
x++;
int s=0;
for(;x;x-=x&-x) s += bit3[x];
return s;
}
void edit(int i, int x) {
int cal1 = sum1(i), cal2 = sum2(i);
ans -= (cal1 - f[i]) * f[i] * i;
ans += (cal2 - f[i] * (i-1)) * f[i];
ans -= f[i] * (sum2all - cal2 + sum1all - cal1);
ans += f[i] * (i-1) * (sum1all - cal1);
add1(i, x-f[i]);
add2(i, (x-f[i]) * (i-1));
ans -= f[i] * (f[i] + 1) / 2;
f[i] = x;
ans += f[i] * (f[i] + 1) / 2;
cal1 += x-f[i];
cal2 += (x-f[i]) * (i-1);
ans += (cal1 - f[i]) * f[i] * i;
ans -= (cal2 - f[i] * (i-1)) * f[i];
ans += f[i] * (sum2all - cal2 + sum1all - cal1);
ans -= f[i] * (i-1) * (sum1all - cal1);
}
int block;
bool modui(pair<pair<int,int>,int> a, pair<pair<int,int>,int> b) {
if(a.first.first / block != b.first.first / block) return a.first.first / block < b.first.first / block;
return a.first.second < b.first.second;
}
void solve(int tc) {
int q;
cin >> n >> q;
int c = 300000;
block = ceil(sqrt(c));
int arr[n+1];
for(int i=1; i<=n; i++) cin >> arr[i];
pair<pair<int,int>,int> qry[q+1];
for(int i=1; i<=q; i++) {
cin >> qry[i].first.first >> qry[i].first.second;
qry[i].second = i;
}
sort(qry+1, qry+q+1, modui);
int res[q+1];
int l = qry[1].first.first, r = qry[1].first.first - 1;
int freq[c+1];
for(int i=1; i<=c; i++) freq[i] = 0;
add3(0, n);
for(int i=1; i<=q; i++) {
while(l > qry[i].first.first) {
l--;
add3(freq[arr[l]], -1);
freq[arr[l]]++;
add3(freq[arr[l]], 1);
int rank = sum3(freq[arr[l]] - 1) + 1;
int invrank = n - rank + 1;
rank = 549 - invrank;
int pos = (rank & 1 ? (rank + 1) / 2 : 549 - rank / 2);
edit(pos, freq[arr[l]]);
}
while(r > qry[i].first.second) {
add3(freq[arr[r]], -1);
freq[arr[r]]--;
add3(freq[arr[r]], 1);
int rank = sum3(freq[arr[r]]);
int invrank = n - rank + 1;
rank = 549 - invrank;
int pos = (rank & 1 ? (rank + 1) / 2 : 549 - rank / 2);
edit(pos, freq[arr[r]]);
r--;
}
while(r < qry[i].first.second) {
r++;
add3(freq[arr[r]], -1);
freq[arr[r]]++;
add3(freq[arr[r]], 1);
int rank = sum3(freq[arr[r]] - 1) + 1;
int invrank = n - rank + 1;
rank = 549 - invrank;
int pos = (rank & 1 ? (rank + 1) / 2 : 549 - rank / 2);
edit(pos, freq[arr[r]]);
}
while(l < qry[i].first.first) {
add3(freq[arr[l]], -1);
freq[arr[l]]--;
add3(freq[arr[l]], 1);
int rank = sum3(freq[arr[l]]);
int invrank = n - rank + 1;
rank = 549 - invrank;
int pos = (rank & 1 ? (rank + 1) / 2 : 549 - rank / 2);
edit(pos, freq[arr[l]]);
l++;
}
res[qry[i].second] = ans;
}
for(int i=1; i<=q; i++) cout << res[i] << "\n";
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Correct |
17 ms |
3708 KB |
Output is correct |
5 |
Correct |
37 ms |
4832 KB |
Output is correct |
6 |
Correct |
45 ms |
5552 KB |
Output is correct |
7 |
Correct |
41 ms |
6268 KB |
Output is correct |
8 |
Correct |
42 ms |
5364 KB |
Output is correct |
9 |
Correct |
46 ms |
5460 KB |
Output is correct |
10 |
Correct |
44 ms |
5832 KB |
Output is correct |
11 |
Correct |
47 ms |
5580 KB |
Output is correct |
12 |
Correct |
44 ms |
5372 KB |
Output is correct |
13 |
Correct |
45 ms |
5356 KB |
Output is correct |
14 |
Correct |
55 ms |
5576 KB |
Output is correct |
15 |
Correct |
45 ms |
5492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Correct |
17 ms |
3708 KB |
Output is correct |
5 |
Correct |
37 ms |
4832 KB |
Output is correct |
6 |
Correct |
45 ms |
5552 KB |
Output is correct |
7 |
Correct |
41 ms |
6268 KB |
Output is correct |
8 |
Correct |
42 ms |
5364 KB |
Output is correct |
9 |
Correct |
46 ms |
5460 KB |
Output is correct |
10 |
Correct |
44 ms |
5832 KB |
Output is correct |
11 |
Correct |
47 ms |
5580 KB |
Output is correct |
12 |
Correct |
44 ms |
5372 KB |
Output is correct |
13 |
Correct |
45 ms |
5356 KB |
Output is correct |
14 |
Correct |
55 ms |
5576 KB |
Output is correct |
15 |
Correct |
45 ms |
5492 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
16 ms |
3588 KB |
Output is correct |
20 |
Correct |
37 ms |
4448 KB |
Output is correct |
21 |
Correct |
66 ms |
5472 KB |
Output is correct |
22 |
Correct |
48 ms |
5328 KB |
Output is correct |
23 |
Correct |
49 ms |
5368 KB |
Output is correct |
24 |
Correct |
46 ms |
5256 KB |
Output is correct |
25 |
Correct |
51 ms |
5284 KB |
Output is correct |
26 |
Correct |
49 ms |
5288 KB |
Output is correct |
27 |
Correct |
47 ms |
5368 KB |
Output is correct |
28 |
Correct |
54 ms |
5540 KB |
Output is correct |
29 |
Correct |
45 ms |
5380 KB |
Output is correct |
30 |
Correct |
52 ms |
5348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Correct |
17 ms |
3708 KB |
Output is correct |
5 |
Correct |
37 ms |
4832 KB |
Output is correct |
6 |
Correct |
45 ms |
5552 KB |
Output is correct |
7 |
Correct |
41 ms |
6268 KB |
Output is correct |
8 |
Correct |
42 ms |
5364 KB |
Output is correct |
9 |
Correct |
46 ms |
5460 KB |
Output is correct |
10 |
Correct |
44 ms |
5832 KB |
Output is correct |
11 |
Correct |
47 ms |
5580 KB |
Output is correct |
12 |
Correct |
44 ms |
5372 KB |
Output is correct |
13 |
Correct |
45 ms |
5356 KB |
Output is correct |
14 |
Correct |
55 ms |
5576 KB |
Output is correct |
15 |
Correct |
45 ms |
5492 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
16 ms |
3588 KB |
Output is correct |
20 |
Correct |
37 ms |
4448 KB |
Output is correct |
21 |
Correct |
66 ms |
5472 KB |
Output is correct |
22 |
Correct |
48 ms |
5328 KB |
Output is correct |
23 |
Correct |
49 ms |
5368 KB |
Output is correct |
24 |
Correct |
46 ms |
5256 KB |
Output is correct |
25 |
Correct |
51 ms |
5284 KB |
Output is correct |
26 |
Correct |
49 ms |
5288 KB |
Output is correct |
27 |
Correct |
47 ms |
5368 KB |
Output is correct |
28 |
Correct |
54 ms |
5540 KB |
Output is correct |
29 |
Correct |
45 ms |
5380 KB |
Output is correct |
30 |
Correct |
52 ms |
5348 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
3 ms |
2772 KB |
Output is correct |
35 |
Correct |
4 ms |
2772 KB |
Output is correct |
36 |
Execution timed out |
7026 ms |
2772 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2632 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
3 ms |
2772 KB |
Output is correct |
14 |
Correct |
17 ms |
3708 KB |
Output is correct |
15 |
Correct |
37 ms |
4832 KB |
Output is correct |
16 |
Correct |
45 ms |
5552 KB |
Output is correct |
17 |
Correct |
41 ms |
6268 KB |
Output is correct |
18 |
Correct |
42 ms |
5364 KB |
Output is correct |
19 |
Correct |
46 ms |
5460 KB |
Output is correct |
20 |
Correct |
44 ms |
5832 KB |
Output is correct |
21 |
Correct |
47 ms |
5580 KB |
Output is correct |
22 |
Correct |
44 ms |
5372 KB |
Output is correct |
23 |
Correct |
45 ms |
5356 KB |
Output is correct |
24 |
Correct |
55 ms |
5576 KB |
Output is correct |
25 |
Correct |
45 ms |
5492 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Correct |
16 ms |
3588 KB |
Output is correct |
30 |
Correct |
37 ms |
4448 KB |
Output is correct |
31 |
Correct |
66 ms |
5472 KB |
Output is correct |
32 |
Correct |
48 ms |
5328 KB |
Output is correct |
33 |
Correct |
49 ms |
5368 KB |
Output is correct |
34 |
Correct |
46 ms |
5256 KB |
Output is correct |
35 |
Correct |
51 ms |
5284 KB |
Output is correct |
36 |
Correct |
49 ms |
5288 KB |
Output is correct |
37 |
Correct |
47 ms |
5368 KB |
Output is correct |
38 |
Correct |
54 ms |
5540 KB |
Output is correct |
39 |
Correct |
45 ms |
5380 KB |
Output is correct |
40 |
Correct |
52 ms |
5348 KB |
Output is correct |
41 |
Correct |
2 ms |
2644 KB |
Output is correct |
42 |
Correct |
2 ms |
2644 KB |
Output is correct |
43 |
Correct |
2 ms |
2644 KB |
Output is correct |
44 |
Correct |
3 ms |
2772 KB |
Output is correct |
45 |
Correct |
4 ms |
2772 KB |
Output is correct |
46 |
Execution timed out |
7026 ms |
2772 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2632 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
3 ms |
2772 KB |
Output is correct |
14 |
Correct |
17 ms |
3708 KB |
Output is correct |
15 |
Correct |
37 ms |
4832 KB |
Output is correct |
16 |
Correct |
45 ms |
5552 KB |
Output is correct |
17 |
Correct |
41 ms |
6268 KB |
Output is correct |
18 |
Correct |
42 ms |
5364 KB |
Output is correct |
19 |
Correct |
46 ms |
5460 KB |
Output is correct |
20 |
Correct |
44 ms |
5832 KB |
Output is correct |
21 |
Correct |
47 ms |
5580 KB |
Output is correct |
22 |
Correct |
44 ms |
5372 KB |
Output is correct |
23 |
Correct |
45 ms |
5356 KB |
Output is correct |
24 |
Correct |
55 ms |
5576 KB |
Output is correct |
25 |
Correct |
45 ms |
5492 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Correct |
16 ms |
3588 KB |
Output is correct |
30 |
Correct |
37 ms |
4448 KB |
Output is correct |
31 |
Correct |
66 ms |
5472 KB |
Output is correct |
32 |
Correct |
48 ms |
5328 KB |
Output is correct |
33 |
Correct |
49 ms |
5368 KB |
Output is correct |
34 |
Correct |
46 ms |
5256 KB |
Output is correct |
35 |
Correct |
51 ms |
5284 KB |
Output is correct |
36 |
Correct |
49 ms |
5288 KB |
Output is correct |
37 |
Correct |
47 ms |
5368 KB |
Output is correct |
38 |
Correct |
54 ms |
5540 KB |
Output is correct |
39 |
Correct |
45 ms |
5380 KB |
Output is correct |
40 |
Correct |
52 ms |
5348 KB |
Output is correct |
41 |
Correct |
2 ms |
2644 KB |
Output is correct |
42 |
Correct |
2 ms |
2644 KB |
Output is correct |
43 |
Correct |
2 ms |
2644 KB |
Output is correct |
44 |
Correct |
3 ms |
2772 KB |
Output is correct |
45 |
Correct |
4 ms |
2772 KB |
Output is correct |
46 |
Execution timed out |
7026 ms |
2772 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |