답안 #879837

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
879837 2023-11-28T08:09:05 Z 8pete8 Tree Rotations (POI11_rot) C++14
100 / 100
90 ms 24340 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
//#define p push
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
using namespace std;
const int mod=9901,mxn=4e5+5;
int tin[mxn+10],tout[mxn+10],l[mxn+10],r[mxn+10],pos[mxn+10],fwk[mxn+10];
ll sz[mxn+10],dp[mxn+10];
int cnt=0,n,node,t=0,sum;
void upd(int p,int val){for(int i=p;i<=n;i+=(i&-i))fwk[i]+=val;}
int qry(int p){
	sum=0;
	for(int i=p;i>0;i-=(i&-i))sum+=fwk[i];
	return sum;
}
int input(){
	int cur;cin>>cur;
	if(cur==0){
		cur=++cnt;
		l[cur]=input();
		r[cur]=input();
		tin[cur]=tin[l[cur]];
		tout[cur]=tout[r[cur]];
		sz[cur]=sz[l[cur]]+sz[r[cur]];
	}
	else{
		tin[cur]=tout[cur]=++t;
		pos[t]=cur;
		sz[cur]++;
	}
	return cur;
}
void solve(int cur,int keep){
	if(cur<=n){
		if(keep)upd(pos[tin[cur]],1);
		return;
	}
	if(sz[l[cur]]<sz[r[cur]])swap(l[cur],r[cur]);//left keep
	solve(r[cur],0);
	solve(l[cur],1);
	ll invcnt=0;
	for(int i=tin[r[cur]];i<=tout[r[cur]];i++)invcnt+=qry(pos[i]);
	dp[cur]=dp[l[cur]]+dp[r[cur]]+min(invcnt,(sz[l[cur]]*sz[r[cur]])-invcnt);
	if(keep)for(int i=tin[r[cur]];i<=tout[r[cur]];i++)upd(pos[i],1);
	else for(int i=tin[l[cur]];i<=tout[l[cur]];i++)upd(pos[i],-1);
}
int32_t main(){
    fastio
	cin>>n;
	cnt=n;
	input();
	solve(n+1,1);
	cout<<dp[n+1];
}

Compilation message

rot.cpp:30:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
   30 | #pragma GCC optimize ("03,unroll-lopps")
      |                                        ^
rot.cpp:36:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   36 | void upd(int p,int val){for(int i=p;i<=n;i+=(i&-i))fwk[i]+=val;}
      |                       ^
rot.cpp:37:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   37 | int qry(int p){
      |              ^
rot.cpp:42:11: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   42 | int input(){
      |           ^
rot.cpp:59:28: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   59 | void solve(int cur,int keep){
      |                            ^
rot.cpp:73:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   73 | int32_t main(){
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 12636 KB Output is correct
2 Correct 1 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 1 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 2 ms 12888 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13660 KB Output is correct
2 Correct 6 ms 14820 KB Output is correct
3 Correct 14 ms 12964 KB Output is correct
4 Correct 5 ms 15452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 11100 KB Output is correct
2 Correct 14 ms 16264 KB Output is correct
3 Correct 16 ms 15672 KB Output is correct
4 Correct 15 ms 13632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 19548 KB Output is correct
2 Correct 21 ms 16992 KB Output is correct
3 Correct 26 ms 16476 KB Output is correct
4 Correct 21 ms 14000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 14416 KB Output is correct
2 Correct 28 ms 16472 KB Output is correct
3 Correct 29 ms 19804 KB Output is correct
4 Correct 28 ms 20660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 17668 KB Output is correct
2 Correct 52 ms 17560 KB Output is correct
3 Correct 47 ms 18000 KB Output is correct
4 Correct 52 ms 18420 KB Output is correct
5 Correct 76 ms 15696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 15836 KB Output is correct
2 Correct 55 ms 24340 KB Output is correct
3 Correct 60 ms 20820 KB Output is correct
4 Correct 49 ms 24016 KB Output is correct
5 Correct 86 ms 15956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 15304 KB Output is correct
2 Correct 52 ms 18132 KB Output is correct
3 Correct 90 ms 16464 KB Output is correct
4 Correct 59 ms 17020 KB Output is correct
5 Correct 54 ms 23892 KB Output is correct
6 Correct 89 ms 15952 KB Output is correct