#include <stdio.h>
#include <map>
#include <algorithm>
#define SQRT 320
using namespace std;
typedef long long ll;
struct query{
int l, r, ti;
ll ans;
}T[111111];
struct seg_tree{
ll tree[1<<18];
int base;
void init(int x) {base=1; while(base<x) base<<=1;}
void update(int x, ll y) {
x += base - 1;
while(x) {
tree[x]=y;
y=max(tree[x/2*2], tree[x/2*2+1]);
if(y == tree[x/2]) return;
x>>=1;
}
}
ll RMQ(int s, int e) {
s += base - 1, e += base - 1;
ll res=0;
while(s<e) {
if(s&1) res=max(res, tree[s++]);
if(!(e&1)) res=max(res, tree[e--]);
s>>=1; e>>=1;
}
if(s == e) res = max(res, tree[e]);
return res;
}
};
seg_tree s;
bool cmp1(const query &x, const query &y){return x.l/SQRT == y.l/SQRT ? x.r < y.r : x.l < y.l;}
bool cmp2(const query &x, const query &y){return x.ti < y.ti;}
int N, Q, A[111111], cnt[111111];
ll B[111111];
map<int, int> m;
int main() {
scanf("%d %d", &N, &Q);
s.init(N);
for(int i=1; i<=N; i++) {
scanf("%d", &A[i]);
if(m[A[i]]) B[i]=(ll)A[i], A[i]=m[A[i]];
else m[A[i]]=i, B[i]=(ll)A[i], A[i]=i;
}
for(int i=0; i<Q; i++) scanf("%d %d", &T[i].l, &T[i].r), T[i].ti=i;
sort(T, T+Q, cmp1);
int l=1, r=0;
for(int i=0; i<Q; i++) {
if(r < T[i].r) {
while(r != T[i].r) {
cnt[A[++r]]++;
s.update(A[r], B[r]*cnt[A[r]]);
}
}
else {
while(r != T[i].r) {
cnt[A[r]]--;
s.update(A[r], B[r]*cnt[A[r]]);
r--;
}
}
if(l > T[i].l) {
while(l != T[i].l) {
cnt[A[--l]]++;
s.update(A[l], B[l]*cnt[A[l]]);
}
}
else {
while(l != T[i].l) {
cnt[A[l]]--;
s.update(A[l], B[l]*cnt[A[l]]);
l++;
}
}
T[i].ans = s.RMQ(1, N);
}
sort(T, T+Q, cmp2);
for(int i=0; i<Q; i++) printf("%lld\n", T[i].ans);
}
Compilation message
historic.cpp: In function 'int main()':
historic.cpp:43:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &Q);
^
historic.cpp:46:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i]);
^
historic.cpp:50:71: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=0; i<Q; i++) scanf("%d %d", &T[i].l, &T[i].r), T[i].ti=i;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
7568 KB |
Output is correct |
2 |
Correct |
0 ms |
7568 KB |
Output is correct |
3 |
Correct |
0 ms |
7568 KB |
Output is correct |
4 |
Correct |
0 ms |
7568 KB |
Output is correct |
5 |
Correct |
0 ms |
7568 KB |
Output is correct |
6 |
Correct |
0 ms |
7568 KB |
Output is correct |
7 |
Correct |
0 ms |
7568 KB |
Output is correct |
8 |
Correct |
0 ms |
7568 KB |
Output is correct |
9 |
Correct |
0 ms |
7568 KB |
Output is correct |
10 |
Correct |
0 ms |
7568 KB |
Output is correct |
11 |
Correct |
0 ms |
7568 KB |
Output is correct |
12 |
Correct |
0 ms |
7568 KB |
Output is correct |
13 |
Correct |
0 ms |
7568 KB |
Output is correct |
14 |
Correct |
0 ms |
7568 KB |
Output is correct |
15 |
Correct |
0 ms |
7568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
7568 KB |
Output is correct |
2 |
Correct |
0 ms |
7568 KB |
Output is correct |
3 |
Correct |
0 ms |
7568 KB |
Output is correct |
4 |
Correct |
3 ms |
7568 KB |
Output is correct |
5 |
Correct |
9 ms |
7568 KB |
Output is correct |
6 |
Correct |
16 ms |
7568 KB |
Output is correct |
7 |
Correct |
16 ms |
7568 KB |
Output is correct |
8 |
Correct |
13 ms |
7568 KB |
Output is correct |
9 |
Correct |
13 ms |
7568 KB |
Output is correct |
10 |
Correct |
16 ms |
7700 KB |
Output is correct |
11 |
Correct |
16 ms |
7700 KB |
Output is correct |
12 |
Correct |
16 ms |
7700 KB |
Output is correct |
13 |
Correct |
16 ms |
7700 KB |
Output is correct |
14 |
Correct |
16 ms |
7568 KB |
Output is correct |
15 |
Correct |
16 ms |
7568 KB |
Output is correct |
16 |
Correct |
16 ms |
7568 KB |
Output is correct |
17 |
Correct |
13 ms |
7568 KB |
Output is correct |
18 |
Correct |
13 ms |
7568 KB |
Output is correct |
19 |
Correct |
16 ms |
7700 KB |
Output is correct |
20 |
Correct |
16 ms |
7700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
7568 KB |
Output is correct |
2 |
Correct |
0 ms |
7568 KB |
Output is correct |
3 |
Correct |
0 ms |
7568 KB |
Output is correct |
4 |
Correct |
0 ms |
7568 KB |
Output is correct |
5 |
Correct |
0 ms |
7568 KB |
Output is correct |
6 |
Correct |
0 ms |
7568 KB |
Output is correct |
7 |
Correct |
3 ms |
7700 KB |
Output is correct |
8 |
Correct |
6 ms |
7700 KB |
Output is correct |
9 |
Correct |
13 ms |
7964 KB |
Output is correct |
10 |
Correct |
9 ms |
7568 KB |
Output is correct |
11 |
Correct |
66 ms |
7568 KB |
Output is correct |
12 |
Correct |
29 ms |
7568 KB |
Output is correct |
13 |
Correct |
59 ms |
7832 KB |
Output is correct |
14 |
Correct |
109 ms |
9416 KB |
Output is correct |
15 |
Correct |
146 ms |
10208 KB |
Output is correct |
16 |
Correct |
109 ms |
12320 KB |
Output is correct |
17 |
Correct |
36 ms |
7568 KB |
Output is correct |
18 |
Correct |
59 ms |
7568 KB |
Output is correct |
19 |
Correct |
103 ms |
12320 KB |
Output is correct |
20 |
Correct |
69 ms |
12320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
7568 KB |
Output is correct |
2 |
Correct |
99 ms |
7568 KB |
Output is correct |
3 |
Correct |
206 ms |
7832 KB |
Output is correct |
4 |
Correct |
349 ms |
8096 KB |
Output is correct |
5 |
Correct |
509 ms |
8360 KB |
Output is correct |
6 |
Correct |
633 ms |
7832 KB |
Output is correct |
7 |
Correct |
666 ms |
7568 KB |
Output is correct |
8 |
Correct |
869 ms |
7568 KB |
Output is correct |
9 |
Correct |
1286 ms |
7568 KB |
Output is correct |
10 |
Correct |
2896 ms |
7568 KB |
Output is correct |
11 |
Correct |
1533 ms |
7568 KB |
Output is correct |
12 |
Correct |
1249 ms |
7568 KB |
Output is correct |
13 |
Correct |
1589 ms |
7832 KB |
Output is correct |
14 |
Correct |
1879 ms |
9020 KB |
Output is correct |
15 |
Correct |
1709 ms |
10208 KB |
Output is correct |
16 |
Correct |
1783 ms |
9680 KB |
Output is correct |
17 |
Correct |
1819 ms |
9416 KB |
Output is correct |
18 |
Correct |
1819 ms |
9284 KB |
Output is correct |
19 |
Correct |
1829 ms |
9152 KB |
Output is correct |
20 |
Correct |
1879 ms |
9020 KB |
Output is correct |
21 |
Correct |
1846 ms |
8888 KB |
Output is correct |
22 |
Correct |
1833 ms |
8888 KB |
Output is correct |
23 |
Correct |
1876 ms |
8756 KB |
Output is correct |
24 |
Correct |
1823 ms |
8756 KB |
Output is correct |
25 |
Correct |
2943 ms |
7568 KB |
Output is correct |