#include <bits/stdc++.h>
#define int long long
using namespace std;
struct node {
int sum;
int lazy;
node* left;
node* right;
node() {
sum = 0;
lazy = 0;
left = nullptr;
right = nullptr;
}
node* new_node(node* p) {
if(p != nullptr)
return p;
else
return new node;
}
void down(int len) {
if(lazy != -1) {
left = new_node(left);
right = new_node(right);
left->sum = (len - len / 2) * lazy;
right->sum = (len / 2) * lazy;
left->lazy = right->lazy = lazy;
lazy = -1;
}
}
void update(int u, int v, int l, int r) {
if(u <= l && r <= v) {
sum = r-l+1;
lazy = 1;
}
else if(u <= r && l <= v) {
down(r-l+1);
int m = l + r >> 1;
left->update(u, v, l, m);
right->update(u, v, m+1, r);
sum = left->sum + right->sum;
}
}
int query(int u, int v, int l, int r) {
if(u <= l && r <= v)
return sum;
else if(u <= r && l <= v) {
down(r-l+1);
int m = l + r >> 1;
int s1 = left->query(u, v, l, m);
int s2 = right->query(u, v, m+1, r);
return s1 + s2;
}
return 0;
}
void update(int u, int v) {
update(u, v, INT_MIN, INT_MAX);
}
int query(int u, int v) {
return query(u, v, INT_MIN, INT_MAX);
}
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
#endif
int q; cin >> q;
int c = 0;
node root;
while(q--) {
int id; cin >> id;
if(id == 2) {
int l, r;
cin >> l >> r;
root.update(l + c, r + c);
}
else {
int l, r;
cin >> l >> r;
int ans = root.query(l + c, r + c);
c += ans;
cout << ans << "\n";
}
}
}
Compilation message
apple.cpp: In member function 'void node::update(long long int, long long int, long long int, long long int)':
apple.cpp:39:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int m = l + r >> 1;
| ~~^~~
apple.cpp: In member function 'long long int node::query(long long int, long long int, long long int, long long int)':
apple.cpp:50:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int m = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |