#include <bits/stdc++.h>
using namespace std;
typedef struct node* pnode;
struct node{
pnode l,r;
int key,prior,size,lazy;
node(int _key){
l = r = NULL;
key = _key;
prior = rand();
size = 1;
lazy = 0;
}
};
int sz(pnode t){
if(t == NULL) return 0;
return t->size;
}
void update(pnode t){
if(t) t->size = sz(t->l) + 1 + sz(t->r);
}
void propagate(pnode t){
if(t == NULL) return;
if(t->lazy == 0) return;
t->key += t->lazy;
if(t->l) t->l->lazy += t->lazy;
if(t->r) t->r->lazy += t->lazy;
t->lazy = 0;
}
void split(pnode t,int key,pnode &l,pnode &r){
propagate(t);
if(t == NULL){
l = r = NULL;
}
else if(key < t->key){
split(t->l,key,l,t->l);
r = t;
}
else{
split(t->r,key,t->r,r);
l = t;
}
update(t);
}
void merge(pnode &t,pnode l,pnode r){
propagate(l);
propagate(r);
if(l == NULL){
t = r;
}
else if(r == NULL){
t = l;
}
else if(l->prior > r->prior){
merge(l->r,l->r,r);
t = l;
}
else{
merge(r->l,l,r->l);
t = r;
}
update(t);
}
void insert(pnode &t,int key){
pnode aux = new node(key);
pnode L,R;
split(t,key,L,R);
merge(t,L,aux);
merge(t,t,R);
}
int kth(pnode t,pnode &l,pnode &r,int count){
propagate(t);
if(sz(t->l) + 1 == count){
r = t->r;
t->r = NULL;
l = t;
update(t);
return t->key;
}
if(count <= sz(t->l)){
int ans = kth(t->l,l,t->l,count);
r = t;
update(t);
return ans;
}
else{
int ans = kth(t->r,t->r,r,count - sz(t->l) - 1);
l = t;
update(t);
return ans;
}
}
void query_F(pnode &t,int ci,int hi){
pnode L = NULL,mid1 = NULL,mid2 = NULL,mid3 = NULL,R = NULL;
split(t,hi-1,L,R);
if(R){
//printf("C %d H %d Rs %d\n",ci,hi,sz(R));
ci = min(ci,sz(R));
int quebra = kth(R,mid1,R,ci);
if(mid1) mid1->lazy += 1;
split(R,quebra,mid3,R);
split(mid1,quebra,mid1,mid2);
merge(t,L,mid1);
merge(t,t,mid3);
merge(t,t,mid2);
merge(t,t,R);
}
else{
merge(t,L,R);
}
}
int query_C(pnode &t,int a,int b){
pnode L,mid,R;
split(t,a-1,L,R);
split(R,b,mid,R);
int ans = sz(mid);
merge(t,L,mid);
merge(t,t,R);
return ans;
}
int main(){
int N,M;
pnode raiz = NULL;
scanf("%d %d",&N,&M);
for(int i = 1;i<=N;i++){
int x;
scanf("%d",&x);
insert(raiz,x);
}
while(M--){
char op;
int a,b;
scanf(" %c %d %d",&op,&a,&b);
if(op == 'F'){
query_F(raiz,a,b);
}
else{
printf("%d\n",query_C(raiz,a,b));
}
}
return 0;
}
Compilation message
grow.cpp: In function 'int main()':
grow.cpp:140:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&M);
~~~~~^~~~~~~~~~~~~~~
grow.cpp:143:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&x);
~~~~~^~~~~~~~~
grow.cpp:150:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c %d %d",&op,&a,&b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
295 ms |
5880 KB |
Output is correct |
2 |
Correct |
446 ms |
7540 KB |
Output is correct |
3 |
Correct |
231 ms |
8908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8908 KB |
Output is correct |
2 |
Correct |
7 ms |
8908 KB |
Output is correct |
3 |
Correct |
9 ms |
8908 KB |
Output is correct |
4 |
Correct |
5 ms |
8908 KB |
Output is correct |
5 |
Correct |
110 ms |
8908 KB |
Output is correct |
6 |
Correct |
171 ms |
8908 KB |
Output is correct |
7 |
Correct |
17 ms |
8908 KB |
Output is correct |
8 |
Correct |
57 ms |
8908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
9324 KB |
Output is correct |
2 |
Correct |
144 ms |
10488 KB |
Output is correct |
3 |
Correct |
9 ms |
10488 KB |
Output is correct |
4 |
Correct |
97 ms |
10948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
12220 KB |
Output is correct |
2 |
Correct |
160 ms |
13196 KB |
Output is correct |
3 |
Correct |
17 ms |
13196 KB |
Output is correct |
4 |
Correct |
176 ms |
14380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
17840 KB |
Output is correct |
2 |
Correct |
369 ms |
19608 KB |
Output is correct |
3 |
Correct |
88 ms |
19608 KB |
Output is correct |
4 |
Correct |
205 ms |
21192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
300 ms |
22412 KB |
Output is correct |
2 |
Correct |
351 ms |
23904 KB |
Output is correct |
3 |
Correct |
214 ms |
24852 KB |
Output is correct |
4 |
Correct |
61 ms |
24852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
268 ms |
26836 KB |
Output is correct |
2 |
Correct |
281 ms |
28468 KB |
Output is correct |
3 |
Correct |
223 ms |
29588 KB |
Output is correct |
4 |
Correct |
67 ms |
29588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
458 ms |
31900 KB |
Output is correct |
2 |
Correct |
380 ms |
32896 KB |
Output is correct |
3 |
Correct |
221 ms |
34340 KB |
Output is correct |
4 |
Correct |
230 ms |
35068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
339 ms |
36276 KB |
Output is correct |
2 |
Correct |
381 ms |
37796 KB |
Output is correct |
3 |
Correct |
329 ms |
40248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
495 ms |
42524 KB |
Output is correct |