#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define svec(x) sort(x.begin(), x.end())
#define press(x) x.erase(unique(x.begin(), x.end()), x.end())
#define lb(x, v) lower_bound(x.begin(), x.end(), v)
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
vector<pair<pii, int> > cmp;
pii qu[100010];
int n, q, arr[100010], cha[100010], cnt[100010], sz;
vector<int> id;
LL ans[100010];
inline int readChar();
template<class T=int> inline T readInt();
template<class T> inline void writeInt(T x, char end=0);
inline void writeChar(int x);
inline void writeWord(const char *s);
static const int buf_size=4096;
inline int getChar(){
static char buf[buf_size];
static int len=0, pos=0;
if(pos==len)pos=0, len=fread(buf, 1, buf_size, stdin);
if(pos==len)return -1;
return buf[pos++];
}
inline int readChar(){
int c=getChar();
while(c<=32)c=getChar();
return c;
}
template <class T>
inline T readInt(){
int s=1, c=readChar();
T x=0;
if(c=='-')s=-1, c=getChar();
while('0'<= c&&c<='9')x=x*10+c-'0', c=getChar();
return s==1?x:-x;
}
LL tree[400010];
void upd(int point, int s, int e, int num, LL val){
if(s==e){
tree[point]=val;
return;
}
if(num<=(s+e)/2)upd(point<<1, s, s+e>>1, num, val);
else upd(point<<1|1, (s+e>>1)+1, e, num, val);
tree[point]=max(tree[point<<1], tree[point<<1|1]);
}
inline void ins(int num){
num=arr[num];
cnt[num]++;
upd(1, 1, sz, num, (LL)cha[num]*cnt[num]);
}
inline void er(int num){
num=arr[num];
cnt[num]--;
upd(1, 1, sz, num, (LL)cha[num]*cnt[num]);
}
int main(){
n=readInt();
q=readInt();
int sq=sqrt(n);
id.resize(n);
for(int i=1; i<=n; i++){
arr[i]=readInt();
id[i-1]=arr[i];
}
svec(id); press(id);
for(int i=1; i<=n; i++){
int tmp=lb(id, arr[i])-id.begin()+1;
cha[tmp]=arr[i];
arr[i]=tmp;
}
sz=id.size();
cmp.resize(q);
for(int i=1; i<=q; i++){
qu[i].F=readInt();
qu[i].S=readInt();
cmp[i-1]=mp(mp(qu[i].F/sq, qu[i].S), i);
}
svec(cmp);
int l=1, r=0;
for(auto i:cmp){
while(qu[i.S].F<l)ins(--l);
while(qu[i.S].S>r)ins(++r);
while(qu[i.S].F>l)er(l++);
while(qu[i.S].S<r)er(r--);
ans[i.S]=tree[1];
}
for(int i=1; i<=q; i++)printf("%lld\n", ans[i]);
}
Compilation message
historic.cpp: In function 'void upd(int, int, int, int, LL)':
historic.cpp:56:39: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
if(num<=(s+e)/2)upd(point<<1, s, s+e>>1, num, val);
~^~
historic.cpp:57:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
else upd(point<<1|1, (s+e>>1)+1, e, num, val);
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
13 ms |
512 KB |
Output is correct |
21 |
Correct |
23 ms |
640 KB |
Output is correct |
22 |
Correct |
26 ms |
640 KB |
Output is correct |
23 |
Correct |
24 ms |
640 KB |
Output is correct |
24 |
Correct |
17 ms |
632 KB |
Output is correct |
25 |
Correct |
37 ms |
640 KB |
Output is correct |
26 |
Correct |
37 ms |
640 KB |
Output is correct |
27 |
Correct |
40 ms |
640 KB |
Output is correct |
28 |
Correct |
42 ms |
640 KB |
Output is correct |
29 |
Correct |
33 ms |
640 KB |
Output is correct |
30 |
Correct |
35 ms |
640 KB |
Output is correct |
31 |
Correct |
20 ms |
640 KB |
Output is correct |
32 |
Correct |
8 ms |
640 KB |
Output is correct |
33 |
Correct |
34 ms |
640 KB |
Output is correct |
34 |
Correct |
38 ms |
640 KB |
Output is correct |
35 |
Correct |
39 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
12 ms |
768 KB |
Output is correct |
10 |
Correct |
4 ms |
896 KB |
Output is correct |
11 |
Correct |
38 ms |
4244 KB |
Output is correct |
12 |
Correct |
29 ms |
1280 KB |
Output is correct |
13 |
Correct |
49 ms |
2296 KB |
Output is correct |
14 |
Correct |
67 ms |
4600 KB |
Output is correct |
15 |
Correct |
81 ms |
6520 KB |
Output is correct |
16 |
Correct |
62 ms |
6008 KB |
Output is correct |
17 |
Correct |
24 ms |
2168 KB |
Output is correct |
18 |
Correct |
36 ms |
4088 KB |
Output is correct |
19 |
Correct |
44 ms |
7160 KB |
Output is correct |
20 |
Correct |
27 ms |
4864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
13 ms |
512 KB |
Output is correct |
21 |
Correct |
23 ms |
640 KB |
Output is correct |
22 |
Correct |
26 ms |
640 KB |
Output is correct |
23 |
Correct |
24 ms |
640 KB |
Output is correct |
24 |
Correct |
17 ms |
632 KB |
Output is correct |
25 |
Correct |
37 ms |
640 KB |
Output is correct |
26 |
Correct |
37 ms |
640 KB |
Output is correct |
27 |
Correct |
40 ms |
640 KB |
Output is correct |
28 |
Correct |
42 ms |
640 KB |
Output is correct |
29 |
Correct |
33 ms |
640 KB |
Output is correct |
30 |
Correct |
35 ms |
640 KB |
Output is correct |
31 |
Correct |
20 ms |
640 KB |
Output is correct |
32 |
Correct |
8 ms |
640 KB |
Output is correct |
33 |
Correct |
34 ms |
640 KB |
Output is correct |
34 |
Correct |
38 ms |
640 KB |
Output is correct |
35 |
Correct |
39 ms |
760 KB |
Output is correct |
36 |
Correct |
0 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
512 KB |
Output is correct |
42 |
Correct |
3 ms |
512 KB |
Output is correct |
43 |
Correct |
5 ms |
640 KB |
Output is correct |
44 |
Correct |
12 ms |
768 KB |
Output is correct |
45 |
Correct |
4 ms |
896 KB |
Output is correct |
46 |
Correct |
38 ms |
4244 KB |
Output is correct |
47 |
Correct |
29 ms |
1280 KB |
Output is correct |
48 |
Correct |
49 ms |
2296 KB |
Output is correct |
49 |
Correct |
67 ms |
4600 KB |
Output is correct |
50 |
Correct |
81 ms |
6520 KB |
Output is correct |
51 |
Correct |
62 ms |
6008 KB |
Output is correct |
52 |
Correct |
24 ms |
2168 KB |
Output is correct |
53 |
Correct |
36 ms |
4088 KB |
Output is correct |
54 |
Correct |
44 ms |
7160 KB |
Output is correct |
55 |
Correct |
27 ms |
4864 KB |
Output is correct |
56 |
Correct |
57 ms |
888 KB |
Output is correct |
57 |
Correct |
253 ms |
1400 KB |
Output is correct |
58 |
Correct |
638 ms |
1912 KB |
Output is correct |
59 |
Correct |
1101 ms |
2808 KB |
Output is correct |
60 |
Correct |
1590 ms |
3320 KB |
Output is correct |
61 |
Correct |
1823 ms |
3576 KB |
Output is correct |
62 |
Correct |
1599 ms |
3928 KB |
Output is correct |
63 |
Correct |
1134 ms |
4504 KB |
Output is correct |
64 |
Correct |
706 ms |
4984 KB |
Output is correct |
65 |
Correct |
216 ms |
5240 KB |
Output is correct |
66 |
Correct |
1035 ms |
5240 KB |
Output is correct |
67 |
Correct |
2445 ms |
5204 KB |
Output is correct |
68 |
Execution timed out |
4027 ms |
7256 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |