#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
struct Node {
int lz = 0;
long long val = 0;
Node* left = NULL, *right = NULL;
} *root;
int q;
void read() {
cin >> q;
}
void update(Node* &head, int l, int r, int u, int v, int lz) {
if (u > r || v < l) return;
if (head == NULL) head = new Node();
if (u <= l && r <= v) {
// cout << "UPD: " << l << ' ' << r << ' ' << r - l + 1 << endl;
head->lz = 1;
head->val = r - l + 1;
return;
}
// cerr << "RUN: " << l << ' ' << r << ' ' << u << ' ' << v << endl;
int mid = (l + r) >> 1;
lz |= head->lz;
if (v <= mid) update(head->left, l, mid, u, v, lz);
else if (u > mid) update(head->right, mid + 1, r, u, v, lz);
else {
update(head->left, l, mid, u, v, lz);
update(head->right, mid + 1, r, u, v, lz);
}
head->val = (head->left != NULL ? head->left->val : 0) +
(head->right != NULL ? head->right->val : 0);
if (lz) {
head->val = r - l + 1;
head->lz = 1;
}
// cout << l << ' ' << r << ' ' << head->val << endl;
}
long long getSum(Node* head, int l, int r, int u, int v, int lz) {
if (head == NULL) {
if (lz) return r - l + 1;
return 0;
}
if (u > r || v < l) return 0;
// cerr << l << ' ' << r << ' ' << u << ' ' << v << endl;
if (u <= l && r <= v) {
// cout << l << ' ' << r << ' ' << head->val << endl;
return head->val;
}
int mid = (l + r) >> 1;
if (v <= mid) return getSum(head->left, l, mid, u, v, head->lz | lz);
else if (u > mid) return getSum(head->right, mid + 1, r, u, v, head->lz | lz);
else {
return getSum(head->left, l, mid, u, v, head->lz | lz) + getSum(head->right, mid + 1, r, u, v, head->lz | lz);
}
}
void solve() {
root = new Node();
long long C = 0;
while(q--) {
long long type, l, r; cin >> type >> l >> r;
l += C; r += C;
if (type == 2) update(root, 1, (int)1e9, l, r, root->lz);
else {
C = getSum(root, 1, (int)1e9, l, r, 0);
cout << C << endl;
}
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
Compilation message
apple.cpp: In function 'int main()':
apple.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
apple.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |