#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct SegmentTree{
struct Node{
int sum, lazy;
int child[2];
Node(){
sum = 0,lazy = 0;
memset(child, -1, sizeof child);
}
};
int L, R;
vector<Node> a;
SegmentTree(int _l, int _r){
L = _l, R = _r;
a.push_back(Node());
}
void add_child(int &x){
x = a.size();
a.push_back(Node());
}
void down(int id, int l, int r){
if (a[id].lazy == false) return;
int mid = (l + r) >> 1;
for(int i = 0; i<=1; ++i) if (a[id].child[i] == -1) {
add_child(a[id].child[i]);
a[a[id].child[i]].lazy = 1;
}
a[a[id].child[0]].sum = (mid - l + 1);
a[a[id].child[1]].sum = (r - mid);
a[id].lazy = 0;
}
void update(int u, int v, int l, int r, int id){
if (u <= l && r <= v){
a[id].sum = (r - l + 1);
a[id].lazy = 1;
return;
}
down(id, l, r);
int mid = (l + r) >> 1;
if (u <= mid) {
if (a[id].child[0] == -1) add_child(a[id].child[0]);
update(u, v, l, mid, a[id].child[0]);
}
if (v > mid) {
if (a[id].child[1] == -1) add_child(a[id].child[1]);
update(u, v, mid + 1, r, a[id].child[1]);
}
a[id].sum = a[a[id].child[0]].sum + a[a[id].child[1]].sum;
}
void update(int u, int v){
update(u, v, L, R, 0);
}
int get(int u, int v, int l, int r, int id){
if (u <= l && r <= v) return a[id].sum;
down(id, l, r);
int mid = (l + r) >> 1;
int ans = 0;
if (a[id].child[0] != -1 && u <= mid) ans += get(u, v, l, mid, a[id].child[0]);
if (a[id].child[1] != -1 && v > mid) ans += get(u, v, mid + 1, r, a[id].child[1]);
return ans;
}
int get(int u, int v){
return get(u, v, L, R, 0);
}
};
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int C = 0;
int q; cin >> q;
SegmentTree st(0, 1e9 + 69);
while(q--){
int type, l, r; cin >> type >> l >> r;
l += C; r += C;
if (type == 2) st.update(l, r);
else {
C = st.get(l, r);
cout << C << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |