/**
* author : Lăng Trọng Đạt
* created: 08-11-2023
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int int64_t
#define mp make_pair
#define f first
#define s second
#define pb push_back
#define all(v) (v).begin(), (v).end()
using pii = pair<int, int>;
const int MAXN = 2e5 + 5;
struct Node {
int val, lazy;
int lc, rc; // left child, right child
int lb, rb; // left border, right border
} st[MAXN];
ostream& operator<<(ostream& os, Node& v) {
os << "[" << v.val << " " << v.lb << " " << v.rb << "]";
}
int q, type, l, r;
int id = 2;
void extend(int v) {
if (!st[v].lc) {
int mid = (st[v].lb + st[v].rb) / 2;
st[id].lb = st[v].lb;
st[id].rb = mid;
st[v].lc = id++;
st[id].lb = mid + 1;
st[id].rb = st[v].rb;
st[v].rc = id++;
}
}
void push(int v) {
if (st[v].lazy) {
if (st[v].lb < st[v].rb) {
extend(v);
st[st[v].lc].lazy = st[st[v].rc].lazy = 1;
}
st[v].val = st[v].rb - st[v].lb + 1;
// db(st[v])
st[v].lazy = 0;
}
}
void upd(int v = 1) {
push(v);
if (st[v].lb > r or l > st[v].rb) return;
else if (l <= st[v].lb && st[v].rb <= r) {
st[v].lazy = 1;
} else {
int mid = (st[v].lb + st[v].rb) / 2;
extend(v);
upd(st[v].lc); upd(st[v].rc);
push(st[v].lc); push(st[v].rc);
st[v].val = st[st[v].lc].val + st[st[v].rc].val;
}
}
int get(int v = 1) {
push(v);
if (st[v].lb > r or l > st[v].rb) return 0;
else if (l <= st[v].lb && st[v].rb <= r) {
// db(v, st[v])
return st[v].val;
} else {
int mid = (st[v].lb + st[v].rb) / 2;
extend(v);
return get(st[v].lc) + get(st[v].rc);
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("hi.inp", "r")) {
freopen("hi.inp", "r", stdin);
// freopen("hi.out", "w", stdout);
}
st[1].lb = 1; st[1].rb = 1e9;
cin >> q;
int c = 0;
while (cin >> type >> l >> r) {
// db(l, r)
l += c; r += c;
if (type == 2) {
upd();
} else {
int tmp = get();
cout << tmp << "\n";
c += tmp;
}
}
}
Compilation message
apple.cpp: In function 'std::ostream& operator<<(std::ostream&, Node&)':
apple.cpp:26:1: warning: no return statement in function returning non-void [-Wreturn-type]
26 | }
| ^
apple.cpp: In function 'void upd(int64_t)':
apple.cpp:59:13: warning: unused variable 'mid' [-Wunused-variable]
59 | int mid = (st[v].lb + st[v].rb) / 2;
| ^~~
apple.cpp: In function 'int64_t get(int64_t)':
apple.cpp:73:13: warning: unused variable 'mid' [-Wunused-variable]
73 | int mid = (st[v].lb + st[v].rb) / 2;
| ^~~
apple.cpp: In function 'int32_t main()':
apple.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen("hi.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
500 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |