# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
30114 | 2017-07-22T06:34:51 Z | 서규호(#1248) | Tree Rotations (POI11_rot) | C++14 | 1000 ms | 65536 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 = 1000000; 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
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 13 ms | 56712 KB | Output is correct |
2 | Correct | 13 ms | 56712 KB | Output is correct |
3 | Correct | 6 ms | 56712 KB | Output is correct |
4 | Correct | 9 ms | 56712 KB | Output is correct |
5 | Correct | 13 ms | 56712 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 56712 KB | Output is correct |
2 | Correct | 16 ms | 56712 KB | Output is correct |
3 | Correct | 16 ms | 56712 KB | Output is correct |
4 | Correct | 19 ms | 56712 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 9 ms | 56712 KB | Output is correct |
2 | Correct | 23 ms | 56712 KB | Output is correct |
3 | Correct | 19 ms | 56844 KB | Output is correct |
4 | Correct | 13 ms | 56712 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 43 ms | 57384 KB | Output is correct |
2 | Correct | 26 ms | 56976 KB | Output is correct |
3 | Correct | 19 ms | 57368 KB | Output is correct |
4 | Correct | 66 ms | 57488 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 606 ms | 59000 KB | Output is correct |
2 | Correct | 33 ms | 57504 KB | Output is correct |
3 | Correct | 66 ms | 58824 KB | Output is correct |
4 | Correct | 23 ms | 58792 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 119 ms | 60012 KB | Output is correct |
2 | Execution timed out | 1000 ms | 61464 KB | Execution timed out |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Memory limit exceeded | 36 ms | 65536 KB | Memory limit exceeded |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Execution timed out | 1000 ms | 62916 KB | Execution timed out |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Memory limit exceeded | 236 ms | 65536 KB | Memory limit exceeded |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Execution timed out | 1000 ms | 60660 KB | Execution timed out |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Execution timed out | 1000 ms | 60852 KB | Execution timed out |
2 | Halted | 0 ms | 0 KB | - |