Submission #1012951

#TimeUsernameProblemLanguageResultExecution timeMemory
1012951CookieCats or Dogs (JOI18_catdog)C++14
100 / 100
149 ms43260 KiB
#include "catdog.h" #include<bits/stdc++.h> #include<fstream> using namespace std; //#pragma GCC optimize("Ofast,unroll-loops") //#pragma GCC target("avx2,tune=native") //huhu #define sz(a) (int)a.size() #define ALL(v) v.begin(), v.end() #define ALLR(v) v.rbegin(), v.rend() #define ll long long #define pb push_back #define forr(i, a, b) for(int i = a; i < b; i++) #define dorr(i, a, b) for(int i = a; i >= b; i--) #define ld long double #define vt vector #include<fstream> #define fi first #define se second #define pll pair<ll, ll> #define pii pair<int, int> #define mpp make_pair #define ull unsigned long long const int mxn = 1e5 + 5, inf = 1e9 + 5; struct Node{ ll dp[2][2] = {}; Node(){ dp[0][1] = dp[1][0] = inf; } }; Node operator +(Node a, Node b){ Node res; for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ res.dp[i][j] = inf; } } for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ for(int k = 0; k < 2; k++){ for(int l = 0; l < 2; l++){ res.dp[i][j] = min(res.dp[i][j], a.dp[i][k] + b.dp[l][j] + (k != l)); } } } } return(res); } struct ST{ vt<Node>st; int sta, en; void init(int l, int r){ st.resize(4 * (r - l + 1) + 5); sta = l; en = r; } void upd(int nd, int l, int r, int id, ll dc, ll dd){ if(l == r){ st[nd].dp[0][0] += dc; st[nd].dp[1][1] += dd; return; } int mid = (l + r) >> 1; if(id <= mid)upd(nd << 1, l, mid, id, dc, dd); else upd(nd << 1 | 1, mid + 1, r, id, dc, dd); st[nd] = st[nd << 1] + st[nd << 1 | 1]; } void upd(int pos, ll dc, ll dd){ assert(pos >= sta && pos <= en); upd(1, sta, en, pos, dc, dd); } pll get(){ ll cat = min(st[1].dp[0][0], st[1].dp[0][1]), dog = min(st[1].dp[1][0], st[1].dp[1][1]); return(mpp(min(cat, dog + 1), min(dog, cat + 1))); } }; ST st[mxn + 1]; int n; int leaf[mxn + 1], head[mxn + 1], tin[mxn + 1], tt = 0, siz[mxn + 1], pa[mxn + 1]; vt<int>adj[mxn + 1]; int dp[mxn + 1][3], sign[mxn + 1]; void dfs(int s, int pre){ siz[s] = 1; pa[s] = pre; for(auto i: adj[s]){ if(i != pre){ dfs(i, s); siz[s] += siz[i]; } } } void hld(int s, int pre, int group){ head[s] = group; tin[s] = ++tt; leaf[group] = s; int mx = 0, son = -1; for(auto i: adj[s]){ if(i != pre && siz[i] > mx){ mx = siz[i]; son = i; } } if(son == -1)return; hld(son, s, group); for(auto i: adj[s]){ if(i != pre && i != son){ hld(i, s, i); } } } void initialize(int N, std::vector<int> A, std::vector<int> B) { n = N; for(int i = 0; i < n; i++){ int u = A[i], v = B[i]; adj[u].pb(v); adj[v].pb(u); } dfs(1, -1); hld(1, -1, 1); for(int i = 1; i <= n; i++){ if(leaf[i]){ st[i].init(tin[i], tin[leaf[i]]); } } } void upd(int node, ll dc, ll dd){ while(node != -1){ int par = head[node]; //cout << par << " "; //cout << dc << " " << dd << " "; pll pre = st[par].get(); st[par].upd(tin[node], dc, dd); pll nw = st[par].get(); dc = nw.fi - pre.fi; dd = nw.se - pre.se; //cout << dc << " " << dd << "\n"; node = pa[head[node]]; } } int cat(int v) { upd(v, 0, inf); sign[v] = 1; pll res = st[1].get(); return(min(res.fi, res.se)); } int dog(int v) { upd(v, inf, 0); sign[v] = 2; pll res = st[1].get(); return(min(res.fi, res.se)); } int neighbor(int v) { if(sign[v] == 1){ upd(v, 0, -inf); }else{ upd(v, -inf, 0); } sign[v] = 0; pll res = st[1].get(); return(min(res.fi, res.se)); } /* int readInt(){ int i; if(scanf("%d",&i)!=1){ fprintf(stderr,"Error while reading input\n"); exit(1); } return i; } int main(){ int N=readInt(); std::vector<int> A(N-1),B(N-1); for(int i=0;i<N-1;i++) { A[i]=readInt(); B[i]=readInt(); } int Q; assert(scanf("%d",&Q)==1); std::vector <int> T(Q),V(Q); for(int i=0;i<Q;i++) { T[i]=readInt(); V[i]=readInt(); } initialize(N,A,B); std::vector<int> res(Q); for(int j=0;j<Q;j++) { if(T[j]==1) res[j]=cat(V[j]); else if(T[j]==2) res[j]=dog(V[j]); else res[j]=neighbor(V[j]); } for(int j=0;j<Q;j++) printf("%d\n",res[j]); return 0; } */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...