Submission #919587

#TimeUsernameProblemLanguageResultExecution timeMemory
919587Khanhcsp2Game (IOI13_game)C++14
100 / 100
3342 ms66640 KiB
#include "game.h" #include<bits/stdc++.h> #define el '\n' #define fi first #define sc second #define pii pair<int, int> #define all(v) v.begin(), v.end() using namespace std; using ll=long long; int m, n; const int N = 5e6; ll gcd(ll a, ll b) { if (!b) return a; else return gcd(b, a % b); } ll ss[N]; int L[N], R[N], root[N], lazy[N]; int cur = 1; void down(int id, int l, int r) { int mid=(l+r)>>1; if(lazy[id]<=mid) { L[id] = ++cur; ss[L[id]] = ss[id]; lazy[L[id]] = lazy[id]; } else { R[id] = ++cur; ss[R[id]] = ss[id]; lazy[R[id]] = lazy[id]; } lazy[id] = 0; } void update1(int id, int l, int r, int pos, ll val) { if(l==r) { ss[id]=val; return; } if(lazy[id]) { if(lazy[id] == pos) ss[id] = val; else down(id, l, r); } int mid = (l+r)>>1; if(pos <= mid) { if(!L[id]) { L[id] = ++cur; ss[L[id]] = val; lazy[L[id]] = pos; } else update1(L[id], l, mid, pos, val); } else { if(!R[id]) { R[id] = ++cur; ss[R[id]] = val; lazy[R[id]] = pos; } else update1(R[id], mid + 1, r, pos, val); } ll s1 = 0, s2 = 0; if(L[id]) s1 = ss[L[id]]; if(R[id]) s2 = ss[R[id]]; ss[id] = gcd(s1, s2); } ll get1(int id, int l, int r, int a, int b) { if(a > r || l > b) return 0; if(a <= l && r <= b) return ss[id]; if(lazy[id]) { if(a <= lazy[id] && lazy[id] <= b) return ss[id]; else return 0; } int mid=(l+r)>>1; ll s1 = 0, s2 = 0; if(L[id]) s1 = get1(L[id], l, mid, a, b); if(R[id]) s2 = get1(R[id], mid + 1, r, a, b); return gcd(s1, s2); } void update2(int id, int l, int r, int i, int x, ll val) { if(l == r) { if(!root[id]) root[id] = ++cur; update1(root[id], 1, n, x, val); } else { int mid = (l + r) >> 1; if(i <= mid) { if(!L[id]) L[id] = ++cur; update2(L[id], l, mid, i, x, val); } else { if(!R[id]) R[id] = ++cur; update2(R[id], mid + 1, r, i, x, val); } ll s1 = 0, s2 = 0; if(L[id]) s1 = get1(root[L[id]], 1, n, x, x); if(R[id]) s2 = get1(root[R[id]], 1, n, x, x); ll s3 = gcd(s1, s2); if(!root[id]) root[id] = ++cur; update1(root[id], 1, n, x, s3); } } ll get2(int id, int l, int r, int a, int b, int u, int v) { if(a > r || l > b) return 0; else if(a <= l && r <= b) { if(root[id]) return get1(root[id], 1, n, u, v); else return 0; } int mid = (l + r) >> 1; ll s1 = 0, s2 = 0; if(L[id]) s1 = get2(L[id], l, mid, a, b, u, v); if(R[id]) s2 = get2(R[id], mid + 1, r, a, b, u, v); return gcd(s1, s2); } void init(int R, int C) { m=R; n=C; } void update(int P, int Q, ll K) { update2(1, 1, m, P+1, Q+1, K); } ll calculate(int P, int Q, int U, int V) { return get2(1, 1, m, P+1, U+1, Q+1, V+1); }
#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...