#include <bits/stdc++.h>
using namespace std;
struct node {
int sum = 0;
int l = -1;
int r = -1;
int tl,tr;
bool has = false;
};
const int MAX_N = 1e6*4;
const int MAX_V = 1e9;
int cnt = 0;
node segt[4*MAX_N+1];
void upd(int idx, int tl, int tr){
if (segt[idx].tl > tr || segt[idx].tr < tl){
return;
}
if (segt[idx].tl >= tl && segt[idx].tr <= tr){
segt[idx].has = true;
return;
}
int mid = (segt[idx].tl + segt[idx].tr)/2;
if (segt[idx].l == -1){
segt[idx].l = cnt;
cnt ++;
segt[segt[idx].l].tl = segt[idx].tl;
segt[segt[idx].l].tr = mid;
}
if (segt[idx].r == -1){
segt[idx].r = cnt;
cnt ++;
segt[segt[idx].r].tl = mid+1;
segt[segt[idx].r].tr = segt[idx].tr;
}
upd(segt[idx].l,tl,tr);
upd(segt[idx].r,tl,tr);
int cl = segt[idx].l;
int cr = segt[idx].r;
segt[idx].sum = 0;
if (segt[cl].has){
segt[idx].sum += segt[cl].tr - segt[cl].tl + 1;
} else {
segt[idx].sum += segt[cl].sum;
}
if (segt[cr].has){
segt[idx].sum += segt[cr].tr - segt[cr].tl+1;
} else {
segt[idx].sum += segt[cr].sum;
}
return;
}
int query(int idx, int tl, int tr, bool chas){
if (segt[idx].tl > tr || segt[idx].tr < tl){
return 0;
}
if (segt[idx].has){
chas = true;
}
if (segt[idx].tl >= tl && segt[idx].tr <= tr){
// cout << idx << ": " << segt[idx].tl << ' ' << segt[idx].tr <<" " << segt[idx].sum << " asd " << tl << ", " << tr << " " << (int) chas << '\n';
if (chas){
return segt[idx].tr - segt[idx].tl + 1;
}
return segt[idx].sum;
}
int res = 0;
if (segt[idx].l != -1){
res += query(segt[idx].l,tl,tr,chas);
}
if (segt[idx].r != -1){
res += query(segt[idx].r,tl,tr,chas);
}
return res;
}
int main()
{
int m;
cin >> m;
int d,x,y;
segt[0].tl = 1;
segt[0].tr = MAX_V;
cnt ++;
int c = 0;
int res;
for (int i =0; i < m; i++){
cin >> d >> x >> y;
if (d == 1){
// cout << c << ": ";
res = query(0,x+c,y+c,false);
c += res;
cout << res << '\n';
} else {
upd(0,x+c,y+c);
}
}
//cout << MAX_V << " " << MAX_N << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
112 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |