#include<bits/stdc++.h>
#define MAXN 500007
using namespace std;
struct event{
int c;
long long k;
int type;
};
struct qr{
int kth,id;
};
const long long inf=1e17;
int n,m,q,type,l,r,ans[MAXN];
long long s,t,total;
long long sz[4*MAXN];
pair<long long,long long> lazy[4*MAXN];
int c[MAXN];
vector<qr> ask[MAXN];
vector<event> queries[MAXN];
pair<long long,long long> merge_lazy(pair<long long,long long> a,pair<long long,long long> b){
long long c = min(a.second, b.first);
a.second -= c;
b.first -= c;
a.first += b.first;
a.second += b.second;
return a;
}
void psh(int v){
sz[2*v]-=lazy[v].first;
sz[2*v]=max(0LL,sz[2*v]);
sz[2*v]+=lazy[v].second;
sz[2*v+1]-=lazy[v].first;
sz[2*v+1]=max(0LL,sz[2*v+1]);
sz[2*v+1]+=lazy[v].second;
lazy[2*v]=merge_lazy(lazy[2*v],lazy[v]);
lazy[2*v+1]=merge_lazy(lazy[2*v+1],lazy[v]);
lazy[v]={0,0};
}
void add(int v,int l,int r,int ll,int rr,long long val){
if(ll>rr)return;
psh(v);
if(l==ll and r==rr){
if(val<0){
sz[v]=max(0LL,sz[v]+val);
lazy[v].first+=-val;
}else{
sz[v]+=val;
lazy[v].second+=val;
}
}else{
int tt=(l+r)/2;
add(2*v,l,tt,ll,min(tt,rr),val);
add(2*v+1,tt+1,r,max(tt+1,ll),rr,val);
sz[v]=min(sz[2*v],sz[2*v+1]);
}
}
long long getsz(int v,int l,int r,int pos){
if(l==r)return sz[v];
psh(v);
int tt=(l+r)/2;
if(pos<=tt)return getsz(2*v,l,tt,pos);
else return getsz(2*v+1,tt+1,r,pos);
}
long long tree[4*MAXN],lz[4*MAXN];
void psh2(int v,long long l,long long r){
long long tt=(l+r)/2;
tree[2*v]+=(tt-l+1)*lz[v];
tree[2*v+1]+=(r-(tt+1)+1)*lz[v];
lz[2*v]+=lz[v];
lz[2*v+1]+=lz[v];
lz[v]=0;
}
void update(int v,long long l,long long r,int ll,int rr,long long val){
if(ll>rr)return;
if(l==ll and r==rr){
tree[v]+=val*(r-l+1);
lz[v]+=val;
}else{
psh2(v,l,r);
int tt=(l+r)/2;
update(2*v,l,tt,ll,min(tt,rr),val);
update(2*v+1,tt+1,r,max(tt+1,ll),rr,val);
tree[v]=tree[2*v]+tree[2*v+1];
}
}
long long getval(int v,int l,int r,int pos){
if(l==r)return tree[v];
psh2(v,l,r);
int tt=(l+r)/2;
if(pos<=tt)return getval(2*v,l,tt,pos);
else return getval(2*v+1,tt+1,r,pos);
}
int getpos(int v,int l,int r,long long sum){
if(l==r)return l;
psh2(v,l,r);
int tt=(l+r)/2;
if(tree[2*v]>=sum)return getpos(2*v,l,tt,sum);
return getpos(2*v+1,tt+1,r,sum-tree[2*v]);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>q;
for(int i=1;i<=q;i++){
cin>>type;
if(type==1){
cin>>l>>r>>s>>t;
c[i]=s;
queries[l].push_back({i,t,0});
queries[r+1].push_back({i,t,1});
add(1,1,n,l,r,t);
update(1,1,n,l,r,t);
}else if(type==2){
cin>>l>>r>>s;
add(1,1,n,l,r,-s);
}else{
cin>>s>>t;
if(getsz(1,1,n,s)<t){
ans[i]=-1;
}else{
if(getval(1,1,n,s)<getsz(1,1,n,s))cout<<1/0;
ask[s].push_back({getval(1,1,n,s)-getsz(1,1,n,s)+t,i});
}
}
}
for(int i=1;i<=max(n,q)*4;i++){
tree[i]=lz[i]=0;
}
for(int i=1;i<=n;i++){
for(int f=0;f<queries[i].size();f++){
if(queries[i][f].type==0){
update(1,1,q,queries[i][f].c,queries[i][f].c,queries[i][f].k);
total+=queries[i][f].k;
}else{
update(1,1,q,queries[i][f].c,queries[i][f].c,-queries[i][f].k);
total-=queries[i][f].k;
}
}
for(int f=0;f<ask[i].size();f++){
ans[ask[i][f].id]=c[getpos(1,1,q,ask[i][f].kth)];
}
}
for(int i=1;i<=q;i++){
if(ans[i]!=0){
if(ans[i]==-1)cout<<0<<"\n";
else cout<<ans[i]<<"\n";
}
}
return 0;
}
Compilation message
foodcourt.cpp: In function 'int main()':
foodcourt.cpp:162:58: warning: division by zero [-Wdiv-by-zero]
162 | if(getval(1,1,n,s)<getsz(1,1,n,s))cout<<1/0;
| ~^~
foodcourt.cpp:163:65: warning: narrowing conversion of '((getval(1, 1, n, ((int)s)) - getsz(1, 1, n, ((int)s))) + t)' from 'long long int' to 'int' [-Wnarrowing]
163 | ask[s].push_back({getval(1,1,n,s)-getsz(1,1,n,s)+t,i});
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
foodcourt.cpp:173:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
173 | for(int f=0;f<queries[i].size();f++){
| ~^~~~~~~~~~~~~~~~~~
foodcourt.cpp:183:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<qr>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
183 | for(int f=0;f<ask[i].size();f++){
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
10 ms |
29276 KB |
Output is correct |
3 |
Correct |
11 ms |
29276 KB |
Output is correct |
4 |
Correct |
10 ms |
29532 KB |
Output is correct |
5 |
Correct |
9 ms |
29276 KB |
Output is correct |
6 |
Correct |
11 ms |
29148 KB |
Output is correct |
7 |
Correct |
10 ms |
29528 KB |
Output is correct |
8 |
Correct |
10 ms |
29532 KB |
Output is correct |
9 |
Correct |
10 ms |
29276 KB |
Output is correct |
10 |
Correct |
11 ms |
29524 KB |
Output is correct |
11 |
Correct |
12 ms |
29524 KB |
Output is correct |
12 |
Correct |
11 ms |
29276 KB |
Output is correct |
13 |
Correct |
8 ms |
29276 KB |
Output is correct |
14 |
Correct |
9 ms |
29276 KB |
Output is correct |
15 |
Correct |
9 ms |
29276 KB |
Output is correct |
16 |
Correct |
11 ms |
29276 KB |
Output is correct |
17 |
Correct |
9 ms |
29276 KB |
Output is correct |
18 |
Correct |
10 ms |
29384 KB |
Output is correct |
19 |
Correct |
9 ms |
29492 KB |
Output is correct |
20 |
Correct |
11 ms |
29580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
10 ms |
29276 KB |
Output is correct |
3 |
Correct |
11 ms |
29276 KB |
Output is correct |
4 |
Correct |
10 ms |
29532 KB |
Output is correct |
5 |
Correct |
9 ms |
29276 KB |
Output is correct |
6 |
Correct |
11 ms |
29148 KB |
Output is correct |
7 |
Correct |
10 ms |
29528 KB |
Output is correct |
8 |
Correct |
10 ms |
29532 KB |
Output is correct |
9 |
Correct |
10 ms |
29276 KB |
Output is correct |
10 |
Correct |
11 ms |
29524 KB |
Output is correct |
11 |
Correct |
12 ms |
29524 KB |
Output is correct |
12 |
Correct |
11 ms |
29276 KB |
Output is correct |
13 |
Correct |
8 ms |
29276 KB |
Output is correct |
14 |
Correct |
9 ms |
29276 KB |
Output is correct |
15 |
Correct |
9 ms |
29276 KB |
Output is correct |
16 |
Correct |
11 ms |
29276 KB |
Output is correct |
17 |
Correct |
9 ms |
29276 KB |
Output is correct |
18 |
Correct |
10 ms |
29384 KB |
Output is correct |
19 |
Correct |
9 ms |
29492 KB |
Output is correct |
20 |
Correct |
11 ms |
29580 KB |
Output is correct |
21 |
Incorrect |
11 ms |
29276 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
40916 KB |
Output is correct |
2 |
Correct |
101 ms |
41816 KB |
Output is correct |
3 |
Correct |
76 ms |
41004 KB |
Output is correct |
4 |
Correct |
76 ms |
40784 KB |
Output is correct |
5 |
Correct |
101 ms |
41716 KB |
Output is correct |
6 |
Correct |
87 ms |
41556 KB |
Output is correct |
7 |
Correct |
28 ms |
34700 KB |
Output is correct |
8 |
Correct |
29 ms |
34692 KB |
Output is correct |
9 |
Correct |
91 ms |
41184 KB |
Output is correct |
10 |
Correct |
92 ms |
41152 KB |
Output is correct |
11 |
Correct |
87 ms |
41040 KB |
Output is correct |
12 |
Correct |
127 ms |
41008 KB |
Output is correct |
13 |
Correct |
83 ms |
41696 KB |
Output is correct |
14 |
Correct |
89 ms |
41888 KB |
Output is correct |
15 |
Correct |
100 ms |
43348 KB |
Output is correct |
16 |
Correct |
113 ms |
43348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
558 ms |
76836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
10 ms |
29276 KB |
Output is correct |
3 |
Correct |
11 ms |
29276 KB |
Output is correct |
4 |
Correct |
10 ms |
29532 KB |
Output is correct |
5 |
Correct |
9 ms |
29276 KB |
Output is correct |
6 |
Correct |
11 ms |
29148 KB |
Output is correct |
7 |
Correct |
10 ms |
29528 KB |
Output is correct |
8 |
Correct |
10 ms |
29532 KB |
Output is correct |
9 |
Correct |
10 ms |
29276 KB |
Output is correct |
10 |
Correct |
11 ms |
29524 KB |
Output is correct |
11 |
Correct |
12 ms |
29524 KB |
Output is correct |
12 |
Correct |
11 ms |
29276 KB |
Output is correct |
13 |
Correct |
8 ms |
29276 KB |
Output is correct |
14 |
Correct |
9 ms |
29276 KB |
Output is correct |
15 |
Correct |
9 ms |
29276 KB |
Output is correct |
16 |
Correct |
11 ms |
29276 KB |
Output is correct |
17 |
Correct |
9 ms |
29276 KB |
Output is correct |
18 |
Correct |
10 ms |
29384 KB |
Output is correct |
19 |
Correct |
9 ms |
29492 KB |
Output is correct |
20 |
Correct |
11 ms |
29580 KB |
Output is correct |
21 |
Correct |
104 ms |
40916 KB |
Output is correct |
22 |
Correct |
101 ms |
41816 KB |
Output is correct |
23 |
Correct |
76 ms |
41004 KB |
Output is correct |
24 |
Correct |
76 ms |
40784 KB |
Output is correct |
25 |
Correct |
101 ms |
41716 KB |
Output is correct |
26 |
Correct |
87 ms |
41556 KB |
Output is correct |
27 |
Correct |
28 ms |
34700 KB |
Output is correct |
28 |
Correct |
29 ms |
34692 KB |
Output is correct |
29 |
Correct |
91 ms |
41184 KB |
Output is correct |
30 |
Correct |
92 ms |
41152 KB |
Output is correct |
31 |
Correct |
87 ms |
41040 KB |
Output is correct |
32 |
Correct |
127 ms |
41008 KB |
Output is correct |
33 |
Correct |
83 ms |
41696 KB |
Output is correct |
34 |
Correct |
89 ms |
41888 KB |
Output is correct |
35 |
Correct |
100 ms |
43348 KB |
Output is correct |
36 |
Correct |
113 ms |
43348 KB |
Output is correct |
37 |
Correct |
101 ms |
41196 KB |
Output is correct |
38 |
Correct |
94 ms |
41536 KB |
Output is correct |
39 |
Correct |
26 ms |
34604 KB |
Output is correct |
40 |
Correct |
30 ms |
34608 KB |
Output is correct |
41 |
Correct |
113 ms |
41384 KB |
Output is correct |
42 |
Correct |
110 ms |
41552 KB |
Output is correct |
43 |
Correct |
101 ms |
41552 KB |
Output is correct |
44 |
Correct |
105 ms |
41488 KB |
Output is correct |
45 |
Correct |
101 ms |
41408 KB |
Output is correct |
46 |
Correct |
101 ms |
41496 KB |
Output is correct |
47 |
Correct |
58 ms |
38384 KB |
Output is correct |
48 |
Correct |
86 ms |
38760 KB |
Output is correct |
49 |
Correct |
80 ms |
41012 KB |
Output is correct |
50 |
Correct |
93 ms |
41180 KB |
Output is correct |
51 |
Correct |
110 ms |
41360 KB |
Output is correct |
52 |
Correct |
128 ms |
41348 KB |
Output is correct |
53 |
Correct |
84 ms |
42336 KB |
Output is correct |
54 |
Correct |
117 ms |
43072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
123 ms |
42068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
10 ms |
29276 KB |
Output is correct |
3 |
Correct |
11 ms |
29276 KB |
Output is correct |
4 |
Correct |
10 ms |
29532 KB |
Output is correct |
5 |
Correct |
9 ms |
29276 KB |
Output is correct |
6 |
Correct |
11 ms |
29148 KB |
Output is correct |
7 |
Correct |
10 ms |
29528 KB |
Output is correct |
8 |
Correct |
10 ms |
29532 KB |
Output is correct |
9 |
Correct |
10 ms |
29276 KB |
Output is correct |
10 |
Correct |
11 ms |
29524 KB |
Output is correct |
11 |
Correct |
12 ms |
29524 KB |
Output is correct |
12 |
Correct |
11 ms |
29276 KB |
Output is correct |
13 |
Correct |
8 ms |
29276 KB |
Output is correct |
14 |
Correct |
9 ms |
29276 KB |
Output is correct |
15 |
Correct |
9 ms |
29276 KB |
Output is correct |
16 |
Correct |
11 ms |
29276 KB |
Output is correct |
17 |
Correct |
9 ms |
29276 KB |
Output is correct |
18 |
Correct |
10 ms |
29384 KB |
Output is correct |
19 |
Correct |
9 ms |
29492 KB |
Output is correct |
20 |
Correct |
11 ms |
29580 KB |
Output is correct |
21 |
Incorrect |
11 ms |
29276 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
29276 KB |
Output is correct |
2 |
Correct |
10 ms |
29276 KB |
Output is correct |
3 |
Correct |
11 ms |
29276 KB |
Output is correct |
4 |
Correct |
10 ms |
29532 KB |
Output is correct |
5 |
Correct |
9 ms |
29276 KB |
Output is correct |
6 |
Correct |
11 ms |
29148 KB |
Output is correct |
7 |
Correct |
10 ms |
29528 KB |
Output is correct |
8 |
Correct |
10 ms |
29532 KB |
Output is correct |
9 |
Correct |
10 ms |
29276 KB |
Output is correct |
10 |
Correct |
11 ms |
29524 KB |
Output is correct |
11 |
Correct |
12 ms |
29524 KB |
Output is correct |
12 |
Correct |
11 ms |
29276 KB |
Output is correct |
13 |
Correct |
8 ms |
29276 KB |
Output is correct |
14 |
Correct |
9 ms |
29276 KB |
Output is correct |
15 |
Correct |
9 ms |
29276 KB |
Output is correct |
16 |
Correct |
11 ms |
29276 KB |
Output is correct |
17 |
Correct |
9 ms |
29276 KB |
Output is correct |
18 |
Correct |
10 ms |
29384 KB |
Output is correct |
19 |
Correct |
9 ms |
29492 KB |
Output is correct |
20 |
Correct |
11 ms |
29580 KB |
Output is correct |
21 |
Incorrect |
11 ms |
29276 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |