Submission #427428

#TimeUsernameProblemLanguageResultExecution timeMemory
427428arayiGame (IOI13_game)C++17
80 / 100
7297 ms256004 KiB
#include "game.h" #include <bits/stdc++.h> #define lli long long #define ad push_back using namespace std; const int N = 7e5 + 30; const int mod = 1e9; long long gcd2(long long X, long long Y) { while (X != Y && Y != 0) { X %= Y; swap(X, Y); } return X; } int n, m, ii; int l[N], r[N]; vector <int> e1; vector <lli> e2; vector<int> l1[N], r1[N]; vector<lli> t[N]; map <pair<int, int>, lli> mp; lli qry1(int l, int r, int nd, int nl = 0, int nr = m - 1, int i1 = 0) { if(l > nr || r < nl || i1 == mod) return 0; if(l == nl && r == nr) return t[nd][i1]; int mid = (nl + nr) / 2; return gcd2(qry1(l, min(mid, r), nd, nl, mid, l1[nd][i1]), qry1(max(mid + 1, l), r, nd, mid + 1, nr, r1[nd][i1])); } lli qry(int l, int r, int l1, int r1, int nl = 0, int nr = n - 1, int nd = 0) { if(l > nr || r < nl || nd == mod) return 0; if(l == nl && r == nr) return qry1(l1, r1, nd); int mid = (nl + nr) / 2; return gcd2(qry(l, min(mid, r), l1, r1, nl, mid, ::l[nd]), qry(max(mid + 1, l), r, l1, r1, mid + 1, nr, ::r[nd])); } void push1(int nd, int i) { if(i < 0) l1[nd][-i-1]=l1[nd].size(); else r1[nd][i-1]=l1[nd].size(); l1[nd].ad(mod); r1[nd].ad(mod); t[nd].ad(0); } void upd1(int l1, int r1, int R, int c, lli v, int nd, int l = 0, int r = m - 1, int i1 = 0, int par = 0) { if(l > c || r < c) return; if(i1 == mod) i1 = ::l1[nd].size(), push1(nd, par); if(l == r) { if(mp[make_pair(R, c)] == 0) { t[nd][i1] = gcd2(t[nd][i1], v); return; } t[nd][i1] = v; if(l1 < R) t[nd][i1] = gcd2(t[nd][i1], qry(l1, R - 1, l, l)); if(r1 > R) t[nd][i1] = gcd2(t[nd][i1], qry(R + 1, r1, l, l)); return; } int mid = (l + r) / 2; upd1(l1, r1, R, c, v, nd, l, mid, ::l1[nd][i1], -i1-1); upd1(l1, r1, R, c, v, nd, mid + 1, r, ::r1[nd][i1], i1+1); t[nd][i1] = 0; if(::l1[nd][i1]!= mod) t[nd][i1] = t[nd][::l1[nd][i1]]; if(::r1[nd][i1]!= mod) t[nd][i1] = gcd2(t[nd][i1], t[nd][::r1[nd][i1]]); } void push(int i) { if(i < 0) l[-i-1]=ii; else r[i-1]=ii; l[ii]=r[ii]=mod; l1[ii]=r1[ii]=e1; t[ii++]=e2; } void upd(int r1, int c, lli v, int l = 0, int r = n - 1, int nd = 0, int par = 0) { if(l > r1 || r < r1) return; if(nd == mod) nd = ii, push(par); upd1(l, r, r1, c, v, nd); if(l==r) return; int mid = (l + r) / 2; upd(r1, c, v, l, mid, ::l[nd], -nd-1); upd(r1, c, v, mid + 1, r, ::r[nd], nd+1); } void init(int R, int C) { n = R, m = C; ii++; e1.ad(mod), e2.ad(0); l[0] = r[0] = mod; l1[0] = r1[0] = e1; t[0] = e2; } void update(int P, int Q, long long K) { upd(P, Q, K); mp[make_pair(P, Q)]=K; } long long calculate(int P, int Q, int U, int V) { return qry(P, U, Q, V); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...