# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
30115 | 2017-07-22T06:38:48 Z | 서규호(#1248) | Tree Rotations (POI11_rot) | C++14 | 0 ms | 1848 KB |
#include <bits/stdc++.h> #include <unistd.h> #define pii pair<int,int> #define pll pair<lld,lld> #define pb push_back #define next nextt #define left leftt #define right rightt #define lld long long #define Inf 1000000000 #define mod 1000000007 #define Linf 1000000000000000000LL #define get gett using namespace std; const int MX = 2000000; int N,rear; lld ans; int left[MX],right[MX]; set<int> S[MX]; void dfs(int x){ int in; scanf("%d",&in); if(in == 0){ rear++; left[x] = rear; dfs(rear); }else{ left[x] = in; } scanf("%d",&in); if(in == 0){ rear++; right[x] = rear; dfs(rear); }else{ right[x] = in; } } void calc(int x){ set<int> tmp; if(left[x] <= N){ S[x-N].insert(left[x]); }else{ calc(left[x]); swap(S[x-N],S[left[x]-N]); } if(right[x] <= N){ tmp.insert(right[x]); }else{ calc(right[x]); swap(tmp,S[right[x]-N]); } if(S[x-N].size() < tmp.size()) swap(S[x-N],tmp); int cnt = 0; lld sum = 0; auto it = S[x-N].begin(); for(auto &i : tmp){ while(1){ if(it != S[x-N].end() && (*it) < i){ cnt++; it++; }else break; } sum += cnt; } lld calctmp = (lld)S[x-N].size()*(lld)tmp.size(); ans += min(sum,calctmp-sum); for(auto &i : tmp) S[x-N].insert(i); } int main(){ //freopen("input.txt","r",stdin); int in; scanf("%d",&N); scanf("%d",&in); if(N == 1){ puts("0"); return 0; } rear = N+1; dfs(N+1); calc(N+1); printf("%lld\n",ans); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 0 ms | 1848 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
2 | Halted | 0 ms | 0 KB | - |