제출 #709895

#제출 시각아이디문제언어결과실행 시간메모리
709895liaoli원숭이와 사과 나무 (IZhO12_apple)C++17
100 / 100
799 ms262144 KiB
#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 timeMemoryGrader output
Fetching results...