This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |