Submission #30112

# Submission time Handle Problem Language Result Execution time Memory
30112 2017-07-22T06:31:26 Z 서규호(#1248) Tree Rotations (POI11_rot) C++14
45 / 100
623 ms 10856 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 = 100000;
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){
	if(1 <= x && x <= N){
		S[x].insert(x);
		return;
	}
	calc(left[x]);
	calc(right[x]);
	set<int> tmp;
	swap(S[x],S[left[x]]);
	swap(tmp,S[right[x]]);
	if(S[x].size() < tmp.size()) swap(S[x],tmp);
	int cnt = 0; lld sum = 0;
	auto it = S[x].begin();
	for(auto &i : tmp){
		while(1){
			if(it != S[x].end() && (*it) < i){
				cnt++;
				it++;
			}else break;
		}
		sum += cnt;
	}
	lld calctmp = (lld)S[x].size()*(lld)tmp.size();
	ans += min(sum,calctmp-sum);
	for(auto &i : tmp) S[x].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;
	dfs(0);
	calc(0);
	printf("%lld\n",ans);

	return 0;
}

Compilation message

rot.cpp: In function 'void dfs(int)':
rot.cpp:25:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&in);
                 ^
rot.cpp:33:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&in);
                 ^
rot.cpp: In function 'int main()':
rot.cpp:73:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&N);
                ^
rot.cpp:74:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&in);
                 ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7492 KB Output is correct
2 Correct 0 ms 7492 KB Output is correct
3 Correct 0 ms 7492 KB Output is correct
4 Correct 0 ms 7492 KB Output is correct
5 Correct 0 ms 7492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 7492 KB Output is correct
2 Correct 3 ms 7492 KB Output is correct
3 Correct 3 ms 7492 KB Output is correct
4 Correct 3 ms 7492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 7492 KB Output is correct
2 Correct 3 ms 7492 KB Output is correct
3 Correct 0 ms 7624 KB Output is correct
4 Correct 0 ms 7492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 8164 KB Output is correct
2 Correct 6 ms 7756 KB Output is correct
3 Correct 6 ms 8152 KB Output is correct
4 Correct 59 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 623 ms 9784 KB Output is correct
2 Correct 16 ms 8284 KB Output is correct
3 Correct 53 ms 9604 KB Output is correct
4 Correct 16 ms 9572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 7492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 10856 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 7492 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 7492 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 7492 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 7492 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -