# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
709895 | liaoli | 원숭이와 사과 나무 (IZhO12_apple) | C++17 | 799 ms | 262144 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define ff first
#define ss second
using namespace std;
typedef long long ftype;
const int MAX = 1e9+17;
struct Segtree {
vector<ftype> seg, d, e, lazy;
const ftype NEUTRAL = 0;
const ftype NEUTRAL_LAZY = -1;
int n;
Segtree(int n) {
this->n = n;
}
ftype apply_lazy(ftype a, ftype b, int len) {
if (b == NEUTRAL_LAZY) return a;
else return b * len;
}
void propagate(int pos, int ini, int fim) {
if (seg[pos] == 0) return;
if (ini == fim) {
return;
}
int m = (ini + fim) >> 1;
if (e[pos] == 0) e[pos] = create();
if (d[pos] == 0) d[pos] = create();
lazy[e[pos]] = apply_lazy(lazy[e[pos]], lazy[pos], 1);
lazy[d[pos]] = apply_lazy(lazy[d[pos]], lazy[pos], 1);
seg[e[pos]] = apply_lazy(seg[e[pos]], lazy[pos], m - ini + 1);
seg[d[pos]] = apply_lazy(seg[d[pos]], lazy[pos], fim - m);
lazy[pos] = NEUTRAL_LAZY;
}
ftype f(ftype a, ftype b) {
return a + b;
}
ftype create() {
seg.push_back(0);
e.push_back(0);
d.push_back(0);
lazy.push_back(-1);
return seg.size() - 1;
}
ftype query(int pos, int ini, int fim, int p, int q) {
propagate(pos, ini, fim);
if (q < ini || p > fim) return NEUTRAL;
if (pos == 0) return 0;
if (p <= ini && fim <= q) return seg[pos];
int m = (ini + fim) >> 1;
return f(query(e[pos], ini, m, p, q), query(d[pos], m + 1, fim, p, q));
}
void update(int pos, int ini, int fim, int p, int q, int val) {
propagate(pos, ini, fim);
if (ini > q || fim < p) {
return;
}
if (ini >= p && fim <= q) {
lazy[pos] = apply_lazy(lazy[pos], val, 1);
seg[pos] = apply_lazy(seg[pos], val, fim - ini + 1);
return;
}
int m = (ini + fim) >> 1;
if (e[pos] == 0) e[pos] = create();
update(e[pos], ini, m, p, q, val);
if (d[pos] == 0) d[pos] = create();
update(d[pos], m + 1, fim, p, q, val);
seg[pos] = f(seg[e[pos]], seg[d[pos]]);
}
ftype query(int p, int q) {
return query(1, 1, n, p, q);
}
void update(int p, int q, int val) {
update(1, 1, n, p, q, val);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int q; cin >> q;
Segtree seg = Segtree(MAX);
int c = 0;
seg.create();
seg.create();
while (q--) {
int op; cin >> op;
if (op == 1) {
int x, y; cin >> x >> y;
c = seg.query(x + c, y + c);
cout << c << '\n';
} else {
int x, y; cin >> x >> y;
seg.update(x + c, y + c, 1);
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |