#include <bits/stdc++.h>
using namespace std;
const int N=400050;
const int S=447;
set<int> pos[N/S];
multiset<pair<int,int>> st[N/S];
int a[N];
int Push(int B, int x)
{
st[B].insert({x,0});
int p;tie(x,p)=*st[B].rbegin();
st[B].erase(--st[B].end());
if(p) pos[B].insert(-p);
return x;
}
void BuildBlock(int B, int l, int r)
{
pos[B].clear();
for(int i=l;i<=r;i++) st[B].insert({a[i],-i});
}
void BreakBlock(int B)
{
while(st[B].size())
{
int p=-1;
if(pos[B].size()) p=*pos[B].rbegin(),pos[B].erase(--pos[B].end());
while(st[B].size() && -st[B].rbegin()->second>p)
{
a[-st[B].rbegin()->second]=st[B].rbegin()->first;
st[B].erase(--st[B].end());
}
if(~p)
{
int x,y;tie(x,y)=*st[B].rbegin();y=-y;
st[B].erase(--st[B].end());
if(y) pos[B].insert(y);
a[p]=x;
}
}
}
int n,q;
int Push(int B, int l, int r, int x)
{
BreakBlock(B);
for(int i=l;i<=r;i++) if(a[i]>x) swap(a[i],x);
BuildBlock(B,B*S,min((B+1)*S,n)-1);
return x;
}
void BuildAll(){ for(int l=0;l<n;l+=S) BuildBlock(l/S,l,min(l+S,n)-1);}
int Query(int l, int r, int x)
{
int L=l/S,R=r/S;
if(L==R)
{
x=Push(L,l,r,x);
}
else
{
x=Push(L,l,L*S+S-1,x);
for(int i=L+1;i<R;i++) x=Push(i,x);
x=Push(R,R*S,r,x);
}
return x;
}
int main()
{
scanf("%i %i",&n,&q);
for(int i=0;i<n;i++) scanf("%i",&a[i]);
BuildAll();
while(q--)
{
int l,r,x;
scanf("%i %i %i",&l,&r,&x);
l--;r--;
if(l<=r) x=Query(l,r,x);
else x=Query(0,r,Query(l,n-1,x));
printf("%i\n",x);
}
return 0;
}
Compilation message
sushi.cpp: In function 'int main()':
sushi.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&q);
~~~~~^~~~~~~~~~~~~~~
sushi.cpp:68:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=0;i<n;i++) scanf("%i",&a[i]);
~~~~~^~~~~~~~~~~~
sushi.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i",&l,&r,&x);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
318 ms |
760 KB |
Output is correct |
2 |
Correct |
316 ms |
632 KB |
Output is correct |
3 |
Correct |
324 ms |
688 KB |
Output is correct |
4 |
Correct |
313 ms |
632 KB |
Output is correct |
5 |
Correct |
321 ms |
632 KB |
Output is correct |
6 |
Correct |
319 ms |
632 KB |
Output is correct |
7 |
Correct |
269 ms |
632 KB |
Output is correct |
8 |
Correct |
270 ms |
632 KB |
Output is correct |
9 |
Correct |
316 ms |
504 KB |
Output is correct |
10 |
Correct |
314 ms |
636 KB |
Output is correct |
11 |
Correct |
287 ms |
504 KB |
Output is correct |
12 |
Correct |
292 ms |
564 KB |
Output is correct |
13 |
Correct |
321 ms |
504 KB |
Output is correct |
14 |
Correct |
419 ms |
764 KB |
Output is correct |
15 |
Correct |
432 ms |
684 KB |
Output is correct |
16 |
Correct |
131 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
168 ms |
41968 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
318 ms |
760 KB |
Output is correct |
2 |
Correct |
316 ms |
632 KB |
Output is correct |
3 |
Correct |
324 ms |
688 KB |
Output is correct |
4 |
Correct |
313 ms |
632 KB |
Output is correct |
5 |
Correct |
321 ms |
632 KB |
Output is correct |
6 |
Correct |
319 ms |
632 KB |
Output is correct |
7 |
Correct |
269 ms |
632 KB |
Output is correct |
8 |
Correct |
270 ms |
632 KB |
Output is correct |
9 |
Correct |
316 ms |
504 KB |
Output is correct |
10 |
Correct |
314 ms |
636 KB |
Output is correct |
11 |
Correct |
287 ms |
504 KB |
Output is correct |
12 |
Correct |
292 ms |
564 KB |
Output is correct |
13 |
Correct |
321 ms |
504 KB |
Output is correct |
14 |
Correct |
419 ms |
764 KB |
Output is correct |
15 |
Correct |
432 ms |
684 KB |
Output is correct |
16 |
Correct |
131 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Runtime error |
168 ms |
41968 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
24 |
Halted |
0 ms |
0 KB |
- |