제출 #863437

#제출 시각아이디문제언어결과실행 시간메모리
863437BolatuluUntitled (POI11_rot)C++17
0 / 100
1067 ms31412 KiB
// Bolatulu #include <bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; typedef double db; #define int long long #define kanagattandirilmagandiktarinizdan ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define pb push_back #define F first #define S second #define md (tl+tr)/2 #define TL v+v,tl,mid #define TR v+v+1,mid+1,tr #pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3") using namespace std; int binpow(int a,int n,int M) { if (n==0) return 1; if (n%2!=0) return (a * binpow(a,n-1,M))%M; int z=binpow(a,n/2,M); return (z*z)%M; } const ll INF = 1e18; const int N = 1e6+7; const int M = 1e9+7; const ll HZ = 1e5; const int MAX = INT_MAX; const int MIN = INT_MIN; const db pi = 3.141592653; const int P=31; int sz[N],n,a[N],l[N],r[N],tame=1,ans,t[N]; vector <int> d, s; void upd(int v,int tl,int tr,int pos,bool add) { if (tl==tr) { if (add) t[v]++; else t[v]=0; return; } if (pos<=md) upd(v+v,tl,md,pos,add); else upd(v+v+1,md+1,tr,pos,add); t[v]=t[v+v]+t[v+v+1]; } int get(int v,int tl,int tr,int l,int r) { if (tl>=l and tr<=r) return t[v]; if (tl>r or tr<l) return 0; return get(v+v,tl,md,l,r)+get(v+v+1,md+1,tr,l,r); } void read(int v) { int z; cin >> z; sz[v] = 1; if (z) { a[v] = z; return; } l[v] = ++tame; read(l[v]); sz[v] += sz[l[v]]; r[v] = ++tame; read(r[v]); sz[v] += sz[r[v]]; } void add(int v) { if (!l[v]) { d.push_back(a[v]); return; } add(l[v]), add(r[v]); } void add1(int v) { if (!l[v]) { upd(1,1,n,a[v],1); s.push_back(a[v]); return; } add1(l[v]), add1(r[v]); } void gett(int v,bool cl=0,bool dir=0) { if (!l[v]) { if (!cl) { if (!dir) d.push_back(a[v]); else { upd(1,1,n,a[v],1); s.push_back(a[v]); } } return; } if (sz[l[v]]>sz[r[v]]) swap(l[v],r[v]); gett(l[v],1,0), gett(r[v],0,1); add(l[v]); int sum1=0,sum2=0; for (auto now : d) sum1+=get(1,1,n,1,now-1), sum2+=get(1,1,n,now+1,n); ans+=min(sum1,sum2); if (cl) { if (!dir) d.clear(); else { for (auto now : s) upd(1,1,n,now,0); s.clear(); } } else { if (!dir) add(r[v]); else add1(l[v]); } } void solve() { cin >> n; read(1); gett(1); cout << ans; } signed main() { // freopen("lca.in", "r", stdin); // freopen("lca.out", "w", stdout); kanagattandirilmagandiktarinizdan int test = 1, count = 1; // cin >> test; while (test--) { // cout << "Case " << count << ":\n"; solve(); if (test) { cout << '\n'; } count++; } return 0; }
#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...
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...