#include <stdio.h>
#define N 100000
#define Q 100000
#define B 316 /* B = floor(sqrt(N)) */
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int aa[N], aa_[N];
int ll[Q], rr[Q];
int compare_a(int i, int j) { return aa[i] - aa[j]; }
int compare_lr(int i, int j) {
return ll[i] / B != ll[j] / B ? ll[i] / B - ll[j] / B : rr[i] - rr[j];
}
int (*compare)(int, int);
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(ii[j], i_);
if (c == 0)
j++;
else if (c < 0) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
}
sort(ii, l, i);
l = k;
}
}
long long xx[N]; int iq[1 + N], pq[N], cnt;
int lt(int i, int j) { return xx[i] > xx[j]; }
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_update(int i, int x) {
xx[i] += x;
if (x > 0)
pq_up(i);
else
pq_dn(i);
}
int main() {
static int ii[N], hh[Q];
static long long ans[Q];
int n, n_, q, h, i, l, r;
scanf("%d%d", &n, &q);
for (i = 0; i < n; i++) {
scanf("%d", &aa[i]);
ii[i] = i;
}
compare = compare_a, sort(ii, 0, n);
for (i = 0, n_ = 0; i < n; i++)
aa_[ii[i]] = i + 1 == n || aa[ii[i + 1]] != aa[ii[i]] ? n_++ : n_;
for (h = 0; h < q; h++) {
scanf("%d%d", &ll[h], &rr[h]), ll[h]--, rr[h]--;
hh[h] = h;
}
compare = compare_lr, sort(hh, 0, q);
for (i = 0; i < n_; i++)
iq[pq[i] = ++cnt] = i;
l = 0, r = -1;
for (h = 0; h < q; h++) {
int h_ = hh[h];
while (l > ll[h_])
l--, pq_update(aa_[l], aa[l]);
while (r < rr[h_])
r++, pq_update(aa_[r], aa[r]);
while (l < ll[h_])
pq_update(aa_[l], -aa[l]), l++;
while (r > rr[h_])
pq_update(aa_[r], -aa[r]), r--;
ans[h_] = xx[iq[1]];
}
for (h = 0; h < q; h++)
printf("%lld\n", ans[h]);
return 0;
}
Compilation message
historic.c: In function 'main':
historic.c:83:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
historic.c:85:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
historic.c:92:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d%d", &ll[h], &rr[h]), ll[h]--, rr[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
288 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
280 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
288 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
280 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
284 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
5 ms |
332 KB |
Output is correct |
20 |
Correct |
9 ms |
424 KB |
Output is correct |
21 |
Correct |
13 ms |
556 KB |
Output is correct |
22 |
Correct |
11 ms |
512 KB |
Output is correct |
23 |
Correct |
9 ms |
544 KB |
Output is correct |
24 |
Correct |
11 ms |
460 KB |
Output is correct |
25 |
Correct |
17 ms |
572 KB |
Output is correct |
26 |
Correct |
16 ms |
592 KB |
Output is correct |
27 |
Correct |
16 ms |
592 KB |
Output is correct |
28 |
Correct |
16 ms |
588 KB |
Output is correct |
29 |
Correct |
16 ms |
592 KB |
Output is correct |
30 |
Correct |
16 ms |
596 KB |
Output is correct |
31 |
Correct |
9 ms |
588 KB |
Output is correct |
32 |
Correct |
9 ms |
588 KB |
Output is correct |
33 |
Correct |
15 ms |
548 KB |
Output is correct |
34 |
Correct |
21 ms |
620 KB |
Output is correct |
35 |
Correct |
21 ms |
548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
284 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
424 KB |
Output is correct |
8 |
Correct |
4 ms |
588 KB |
Output is correct |
9 |
Correct |
9 ms |
880 KB |
Output is correct |
10 |
Correct |
8 ms |
1228 KB |
Output is correct |
11 |
Correct |
59 ms |
5268 KB |
Output is correct |
12 |
Correct |
24 ms |
2200 KB |
Output is correct |
13 |
Correct |
45 ms |
3256 KB |
Output is correct |
14 |
Correct |
70 ms |
5012 KB |
Output is correct |
15 |
Correct |
110 ms |
7396 KB |
Output is correct |
16 |
Correct |
58 ms |
6036 KB |
Output is correct |
17 |
Correct |
28 ms |
2756 KB |
Output is correct |
18 |
Correct |
47 ms |
5184 KB |
Output is correct |
19 |
Correct |
68 ms |
7204 KB |
Output is correct |
20 |
Correct |
38 ms |
4608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
288 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
280 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
284 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
5 ms |
332 KB |
Output is correct |
20 |
Correct |
9 ms |
424 KB |
Output is correct |
21 |
Correct |
13 ms |
556 KB |
Output is correct |
22 |
Correct |
11 ms |
512 KB |
Output is correct |
23 |
Correct |
9 ms |
544 KB |
Output is correct |
24 |
Correct |
11 ms |
460 KB |
Output is correct |
25 |
Correct |
17 ms |
572 KB |
Output is correct |
26 |
Correct |
16 ms |
592 KB |
Output is correct |
27 |
Correct |
16 ms |
592 KB |
Output is correct |
28 |
Correct |
16 ms |
588 KB |
Output is correct |
29 |
Correct |
16 ms |
592 KB |
Output is correct |
30 |
Correct |
16 ms |
596 KB |
Output is correct |
31 |
Correct |
9 ms |
588 KB |
Output is correct |
32 |
Correct |
9 ms |
588 KB |
Output is correct |
33 |
Correct |
15 ms |
548 KB |
Output is correct |
34 |
Correct |
21 ms |
620 KB |
Output is correct |
35 |
Correct |
21 ms |
548 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
284 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
2 ms |
424 KB |
Output is correct |
43 |
Correct |
4 ms |
588 KB |
Output is correct |
44 |
Correct |
9 ms |
880 KB |
Output is correct |
45 |
Correct |
8 ms |
1228 KB |
Output is correct |
46 |
Correct |
59 ms |
5268 KB |
Output is correct |
47 |
Correct |
24 ms |
2200 KB |
Output is correct |
48 |
Correct |
45 ms |
3256 KB |
Output is correct |
49 |
Correct |
70 ms |
5012 KB |
Output is correct |
50 |
Correct |
110 ms |
7396 KB |
Output is correct |
51 |
Correct |
58 ms |
6036 KB |
Output is correct |
52 |
Correct |
28 ms |
2756 KB |
Output is correct |
53 |
Correct |
47 ms |
5184 KB |
Output is correct |
54 |
Correct |
68 ms |
7204 KB |
Output is correct |
55 |
Correct |
38 ms |
4608 KB |
Output is correct |
56 |
Correct |
24 ms |
768 KB |
Output is correct |
57 |
Correct |
56 ms |
1412 KB |
Output is correct |
58 |
Correct |
165 ms |
2108 KB |
Output is correct |
59 |
Correct |
303 ms |
2752 KB |
Output is correct |
60 |
Correct |
457 ms |
3448 KB |
Output is correct |
61 |
Correct |
392 ms |
4104 KB |
Output is correct |
62 |
Correct |
286 ms |
4796 KB |
Output is correct |
63 |
Correct |
323 ms |
5512 KB |
Output is correct |
64 |
Correct |
427 ms |
6196 KB |
Output is correct |
65 |
Correct |
397 ms |
6164 KB |
Output is correct |
66 |
Correct |
475 ms |
6308 KB |
Output is correct |
67 |
Correct |
479 ms |
6352 KB |
Output is correct |
68 |
Correct |
874 ms |
6552 KB |
Output is correct |
69 |
Correct |
1646 ms |
7116 KB |
Output is correct |
70 |
Correct |
2111 ms |
7440 KB |
Output is correct |
71 |
Correct |
1912 ms |
7236 KB |
Output is correct |
72 |
Correct |
1863 ms |
7236 KB |
Output is correct |
73 |
Correct |
1706 ms |
7112 KB |
Output is correct |
74 |
Correct |
1628 ms |
7060 KB |
Output is correct |
75 |
Correct |
1561 ms |
7024 KB |
Output is correct |
76 |
Correct |
1541 ms |
6984 KB |
Output is correct |
77 |
Correct |
1497 ms |
7096 KB |
Output is correct |
78 |
Correct |
1461 ms |
6940 KB |
Output is correct |
79 |
Correct |
1503 ms |
7044 KB |
Output is correct |
80 |
Correct |
415 ms |
6948 KB |
Output is correct |