#include<bits/stdc++.h>
using namespace std;
const int N = 1e9;
int Q;
struct node{
node *left , *right;
int sum = 0 , lz = -1;
node(int x = 0) : sum(x) , lz(0) , left(NULL) , right(NULL){}
} *root;
void push(node *&u , int l , int r){
if(u->lz == -1){
return;
}
if(u->left == NULL){
u->left = new node();
}
if(u->right == NULL){
u->right = new node();
}
int m = (l + r) / 2;
u->left->sum = (m - l + 1) * u->lz;
u->right->sum = (r - m) * u->lz;
u->left->lz = u->lz;
u->right->lz = u->lz;
u->lz = -1;
}
void upd(node *&u , int l , int r , int L , int R , int x){
if(l > r || r < L || l > R){
return;
}
if(u == NULL){
u = new node();
}
if(L <= l && r <= R){
u->sum = x * (r - l + 1);
u->lz = x;
return;
}
push(u , l , r);
int m = (l + r) / 2;
upd(u->left , l , m , L , R , x);
upd(u->right , m + 1 , r , L , R , x);
u->sum = 0;
if(u->left){
u->sum += u->left->sum;
}
if(u->right){
u->sum += u->right->sum;
}
}
int get(node *&u , int l , int r , int L , int R){
if(l > r || r < L || l > R){
return 0;
}
if(L <= l && r <= R){
return u->sum;
}
push(u , l , r);
int m = (l + r) / 2;
return get(u->left , l , m , L , R) + get(u->right , m + 1 , r , L , R);
}
int main(){
int C = 0;
cin >> Q;
while(Q--){
int D , X , Y;
cin >> D >> X >> Y;
X += C;
Y += C;
if(D == 1){
C = get(root , 1 , N , X , Y);
cout << C << " ";
}else{
upd(root , 1 , N , X , Y , 1);
}
}
}
Compilation message
apple.cpp: In constructor 'node::node(int)':
apple.cpp:7:18: warning: 'node::lz' will be initialized after [-Wreorder]
7 | int sum = 0 , lz = -1;
| ^~
apple.cpp:6:10: warning: 'node* node::left' [-Wreorder]
6 | node *left , *right;
| ^~~~
apple.cpp:8:4: warning: when initialized here [-Wreorder]
8 | node(int x = 0) : sum(x) , lz(0) , left(NULL) , right(NULL){}
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Runtime error |
0 ms |
348 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |