#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
vector<int> sortx;
int N,Q,x[100010],lookup[100010],root,sz;
struct range{
int s,e,v;
range(){}
range(int s_,int e_):s(s_),e(e_){}
}z[100010];
bool cmp(range a,range b){return (a.s/root)*root+a.e/root<(b.s/root)*root+b.e/root;}
struct ans{
int ret,v;
ans(){}
ans(int ret_,int v_):ret(ret_),v(v_){}
}A[100010];
bool cmp2(ans a,ans b){return a.v<b.v;}
int tree[400010];
void push(int x,int add){
tree[x]+=add;
for(int i=x/2;i>0;i>>=1){
tree[i]=max(tree[i*2],tree[i*2+1]);
}
}
main(){
int t;
scanf("%lld%lld",&N,&Q);
root=300;
sz=1;
while(sz<N)sz<<=1;
for(int i=1;i<=N;i++)scanf("%lld",&x[i]);
for(int i=0;i<Q;i++)scanf("%lld%lld",&z[i].s,&z[i].e),z[i].v=i;
for(int i=1;i<=N;i++)sortx.push_back(x[i]);
sort(sortx.begin(),sortx.end());
sortx.erase(unique(sortx.begin(),sortx.end()),sortx.end());
for(int i=1;i<=N;i++){
t=x[i];
x[i]=lower_bound(sortx.begin(),sortx.end(),x[i])-sortx.begin();
lookup[x[i]]=t;
}
sort(z,z+Q,cmp);
int L,R;
L=z[0].s;
R=z[0].s-1;
for(int i=0;i<Q;i++){
if(R<z[i].e){for(int j=R+1;j<=z[i].e;j++)push(x[j]+sz,lookup[x[j]]);R=z[i].e;}
else if(R>z[i].e){for(int j=R;j>z[i].e;j--)push(x[j]+sz,-lookup[x[j]]);R=z[i].e;}
if(L<z[i].s){for(int j=L;j<z[i].s;j++)push(x[j]+sz,-lookup[x[j]]);L=z[i].s;}
else if(L>z[i].s){for(int j=L-1;j>=z[i].s;j--)push(x[j]+sz,lookup[x[j]]);L=z[i].s;}
A[i]=ans(tree[1],z[i].v);
}
sort(A,A+Q,cmp2);
for(int i=0;i<Q;i++)printf("%lld ",A[i].ret);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
9812 KB |
Output is correct |
2 |
Correct |
0 ms |
9812 KB |
Output is correct |
3 |
Correct |
0 ms |
9812 KB |
Output is correct |
4 |
Correct |
0 ms |
9812 KB |
Output is correct |
5 |
Correct |
0 ms |
9812 KB |
Output is correct |
6 |
Correct |
1 ms |
9812 KB |
Output is correct |
7 |
Correct |
0 ms |
9812 KB |
Output is correct |
8 |
Correct |
0 ms |
9812 KB |
Output is correct |
9 |
Correct |
0 ms |
9812 KB |
Output is correct |
10 |
Correct |
0 ms |
9812 KB |
Output is correct |
11 |
Correct |
0 ms |
9812 KB |
Output is correct |
12 |
Correct |
0 ms |
9812 KB |
Output is correct |
13 |
Correct |
0 ms |
9812 KB |
Output is correct |
14 |
Correct |
0 ms |
9812 KB |
Output is correct |
15 |
Correct |
0 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9812 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
8 ms |
9812 KB |
Output is correct |
5 |
Correct |
26 ms |
9812 KB |
Output is correct |
6 |
Correct |
46 ms |
9812 KB |
Output is correct |
7 |
Correct |
46 ms |
9812 KB |
Output is correct |
8 |
Correct |
47 ms |
9812 KB |
Output is correct |
9 |
Correct |
47 ms |
9812 KB |
Output is correct |
10 |
Correct |
40 ms |
9812 KB |
Output is correct |
11 |
Correct |
47 ms |
9812 KB |
Output is correct |
12 |
Correct |
45 ms |
9812 KB |
Output is correct |
13 |
Correct |
45 ms |
9812 KB |
Output is correct |
14 |
Correct |
47 ms |
9812 KB |
Output is correct |
15 |
Correct |
48 ms |
9812 KB |
Output is correct |
16 |
Correct |
46 ms |
9812 KB |
Output is correct |
17 |
Correct |
48 ms |
9812 KB |
Output is correct |
18 |
Correct |
45 ms |
9812 KB |
Output is correct |
19 |
Correct |
45 ms |
9812 KB |
Output is correct |
20 |
Correct |
48 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
9812 KB |
Output is correct |
2 |
Correct |
0 ms |
9812 KB |
Output is correct |
3 |
Correct |
0 ms |
9812 KB |
Output is correct |
4 |
Correct |
0 ms |
9812 KB |
Output is correct |
5 |
Correct |
4 ms |
9812 KB |
Output is correct |
6 |
Correct |
0 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
4 ms |
9944 KB |
Output is correct |
9 |
Correct |
10 ms |
10200 KB |
Output is correct |
10 |
Correct |
7 ms |
10584 KB |
Output is correct |
11 |
Correct |
832 ms |
11352 KB |
Output is correct |
12 |
Correct |
39 ms |
11352 KB |
Output is correct |
13 |
Correct |
227 ms |
11352 KB |
Output is correct |
14 |
Correct |
525 ms |
11352 KB |
Output is correct |
15 |
Correct |
1322 ms |
11352 KB |
Output is correct |
16 |
Correct |
153 ms |
11352 KB |
Output is correct |
17 |
Correct |
84 ms |
11352 KB |
Output is correct |
18 |
Correct |
203 ms |
11352 KB |
Output is correct |
19 |
Correct |
187 ms |
11352 KB |
Output is correct |
20 |
Correct |
74 ms |
11352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
9944 KB |
Output is correct |
2 |
Correct |
260 ms |
10200 KB |
Output is correct |
3 |
Correct |
441 ms |
10200 KB |
Output is correct |
4 |
Correct |
727 ms |
10584 KB |
Output is correct |
5 |
Correct |
1007 ms |
10584 KB |
Output is correct |
6 |
Correct |
1211 ms |
10584 KB |
Output is correct |
7 |
Correct |
1573 ms |
11352 KB |
Output is correct |
8 |
Correct |
2185 ms |
11352 KB |
Output is correct |
9 |
Correct |
2786 ms |
11352 KB |
Output is correct |
10 |
Execution timed out |
4000 ms |
11352 KB |
Program timed out |
11 |
Halted |
0 ms |
0 KB |
- |