Submission #538888

#TimeUsernameProblemLanguageResultExecution timeMemory
538888OlympiaSjeckanje (COCI21_sjeckanje)C++17
0 / 110
1 ms340 KiB
#include <vector> #include <algorithm> #include <iostream> #include <set> #include <cmath> #include <map> #include <random> #include <cassert> #include <ctime> #include <cstdlib> #include <limits.h> using namespace std; const int MOD = 1e9; class Node { public: pair<int64_t,int64_t> lval; pair<int64_t,int64_t> rval; int monotone = 1; int64_t tot[2][2]; }; Node merge (Node left, Node right) { if (left.monotone == -1) { return right; } if (right.monotone == -1) { return left; } Node ans; ans.tot[0][0] = ans.tot[1][1] = ans.tot[1][0] = ans.tot[0][1] = 0; ans.monotone = left.monotone + right.monotone; ans.lval = left.lval; ans.rval = right.rval; for (int x = 0; x <= 1; x++) { for (int y = 0; y <= 1; y++) { ans.tot[x][y] = left.tot[x][1] + right.tot[1][y]; } } if (left.rval.second <= right.lval.first) { for (int x = 0; x <= 1; x++) { for (int y = 0; y <= 1; y++) { ans.tot[x][y] -= (left.rval.second - left.rval.first); ans.tot[x][y] -= (right.lval.second - right.lval.first); ans.tot[x][y] += (right.rval.second - left.lval.first); } } ans.monotone--; } if (ans.monotone == 1) { ans.lval = ans.rval = {left.rval.first, right.rval.second}; ans.tot[0][1] = ans.tot[1][0] = ans.tot[0][0] = false; ans.tot[1][1] = ans.rval.second - ans.rval.first; return ans; } if (ans.monotone == 2) { ans.tot[0][0] = false; } return ans; } Node INF; struct segmentTree { vector<Node> vec; vector<int64_t> addLater; void push (int v) { addLater[2 * v + 1] += addLater[v]; vec[2 * v + 1].lval.first += addLater[v]; vec[2 * v + 1].lval.second += addLater[v]; vec[2 * v + 1].rval.first += addLater[v]; vec[2 * v + 1].rval.second += addLater[v]; addLater[2 * v] += addLater[v]; vec[2 * v].lval.first += addLater[v]; vec[2 * v].lval.second += addLater[v]; vec[2 * v].rval.first += addLater[v]; vec[2 * v].rval.second += addLater[v]; addLater[v] = 0; } void upd(int dum, int tl, int tr, int l, int r, int64_t val) { if (tr < l || tl > r) { return; } if (tl >= l && tr <= r) { addLater[dum] += val; vec[dum].lval.first += val; vec[dum].lval.second += val; vec[dum].rval.first += val; vec[dum].rval.second += val; return; } push(dum); int mid = (tl + tr) >> 1; upd(2 * dum, tl, mid, l, r, val); upd(2 * dum + 1, mid + 1, tr, l, r, val); vec[dum] = merge(vec[2 * dum], vec[2 * dum + 1]); } void upd(int l, int r, int val) { upd(1, 0, (int) vec.size() / 2 - 1, l, r, val); } Node get(int dum, int tl, int tr, int &l, int &r) { if (tl > r || tr < l) { return INF; } if (tl >= l && tr <= r) { return vec[dum]; } push(dum); int tm = (tl + tr) >> 1; return merge(get(dum * 2, tl, tm, l, r), get(dum * 2 + 1, tm + 1, tr, l, r)); } Node get(int l, int r) { return get(1, 0, (int) vec.size() / 2 - 1, l, r); } void resz(int n) { int sz = ((1 << (int) ceil(log2(n)))); vec.resize(sz * 2); addLater.resize(sz * 2); } }; int main() { //freopen("seating.in", "r", stdin); //freopen("haybales.out", "w", stdout); ios_base::sync_with_stdio(false); INF.monotone = -1; cin.tie(NULL); int N, D; cin >> N >> D; segmentTree st; st.resz(N); for (int i = 0; i < N; i++) { int x; cin >> x; st.upd(i, i, x); } cout << st.get(0, N - 1).tot[1][1] << '\n'; return 0; while (D--) { int l, r, x; cin >> l >> r >> x; l--, r--; st.upd(l, r, x); cout << st.get(0, N - 1).tot[1][1] << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...