# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
709895 | liaoli | Monkey and Apple-trees (IZhO12_apple) | C++17 | 799 ms | 262144 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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... |