#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 1000000000;
int C, Q;
struct node{
node *left=nullptr, *right=nullptr;
int sum = 0, lazy = 0;
};
typedef node* pnode;
void push(pnode &tree, int vl, int vr){
if(!tree) tree = new node();
if(tree->lazy == 0) return;
tree->sum = (vr-vl+1);
if(vl != vr){
if(!tree->left) tree->left = new node();
if(!tree->right) tree->right = new node();
tree->left->lazy = 1;
tree->right->lazy = 1;
}
tree->lazy = 0;
}
int sum(pnode tree){
if(tree) return tree->sum;
return 0;
}
void update(pnode &tree, int l, int r, int vl, int vr){
if(!tree) tree = new node();
push(tree, vl, vr);
if(l > vr or vl > r) return;
if(l <= vl and r >= vr){
tree->lazy = 1;
push(tree, vl, vr);
return;
}
int mid = (vl + vr)>>1;
update(tree->left, l, r, vl, mid);
update(tree->right, l, r, mid+1, vr);
tree->sum = sum(tree->left) + sum(tree->right);
}
int get_sum(pnode &tree, int l, int r, int vl, int vr){
if(!tree){
tree = new node();
}
push(tree, vl, vr);
if(l > vr or vl > r) return 0;
if(l <= vl and r >= vr){
return tree->sum;
}
int mid = (vl + vr)>>1;
int s = (tree->left ? get_sum(tree->left,l, r, vl, mid) : 0);
int s1 = (tree->right ? get_sum(tree->right, l, r, mid+1, vr) : 0);
tree->sum = sum(tree->left) + sum(tree->right);
return s+s1;
}
// https://oj.uz/problem/view/IZhO12_apple
main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
pnode tree = new node();
cin >> Q;
while(Q--){
int type, l, r; cin >> type >> l >> r;
if(type == 1){
C = get_sum(tree, l+C, r+C, 1, N);
cout << C << '\n';
}else{
update(tree, l+C, r+C, 1, N);
}
}
return 0;
}
Compilation message
apple.cpp:62:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
62 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
17 ms |
5964 KB |
Output is correct |
5 |
Correct |
20 ms |
7124 KB |
Output is correct |
6 |
Correct |
20 ms |
6832 KB |
Output is correct |
7 |
Correct |
20 ms |
7092 KB |
Output is correct |
8 |
Correct |
154 ms |
52336 KB |
Output is correct |
9 |
Correct |
312 ms |
89120 KB |
Output is correct |
10 |
Correct |
323 ms |
99824 KB |
Output is correct |
11 |
Correct |
331 ms |
108196 KB |
Output is correct |
12 |
Correct |
336 ms |
111860 KB |
Output is correct |
13 |
Correct |
336 ms |
139032 KB |
Output is correct |
14 |
Correct |
330 ms |
140356 KB |
Output is correct |
15 |
Correct |
535 ms |
254612 KB |
Output is correct |
16 |
Correct |
527 ms |
256472 KB |
Output is correct |
17 |
Correct |
347 ms |
145388 KB |
Output is correct |
18 |
Correct |
333 ms |
145556 KB |
Output is correct |
19 |
Correct |
530 ms |
262144 KB |
Output is correct |
20 |
Correct |
544 ms |
262144 KB |
Output is correct |