Submission #1020623

# Submission time Handle Problem Language Result Execution time Memory
1020623 2024-07-12T07:48:30 Z cnn008 Tree Rotations (POI11_rot) C++17
0 / 100
17 ms 17776 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef N_N_C
#include "debug.h"
#else
#define cebug(...) "Arya"
#endif

#define ll long long

const int N=1e5+5;
const int mod=1e9+7;

int n,node=1,a[N],sz[N],ans[N],cur;
vector <int> g[N];
struct BIT{
	#define gb(x) (x&(-x))
   	int n;
   	vector <int> bit;
   	void init(int _n){
   		bit.resize(_n,0);
   		n=_n;
   	}
   	void update(int pos, int val){
   		int i=pos;
      	for (; i<=n; i+=gb(i)) bit[i]+=val;
   	}
   	int get(int pos){
    	int ans=0,i=pos;
      	for (; i>=1; i-=gb(i)) ans+=bit[i];
      	return ans;
   	}
}fw;
void input(){
	int x;
	cin>>x;
	a[node]=x;
	if(x) return;
	int nw=node;
	g[nw].push_back(++node);
	input();
	g[nw].push_back(++node);
	input();
}
void pre_dfs(int u){
	sz[u]=1;
	for(auto v:g[u]){
		pre_dfs(v);
		sz[u]+=sz[v];
	}
}
void add(int u){
	if(a[u]) cur+=fw.get(a[u]-1);
	for(auto v:g[u]) add(v);
}
void del(int u){
	if(a[u]) cur-=fw.get(a[u]-1);
	for(auto v:g[u]) del(v);
}
void upd(int u){
	if(a[u]) fw.update(a[u]+1,1);
	for(auto v:g[u]) upd(v);
}
void dfs(int u){
	if(a[u]){
		fw.update(a[u],1);
		return;
	}
	int big=0;
	cur=0;
	for(auto v:g[u]) if(sz[v]>sz[big]) big=v;
	for(auto v:g[u]){
		if(v!=big){
			dfs(v);
			del(v);
			ans[u]+=ans[v];
		}
	}
	assert(big);
	dfs(big);
	ans[u]+=ans[big];
	for(auto v:g[u]) if(v!=big) add(v);
	ans[u]+=cur;
}
void sol(){
	fw.init(N);
	cin>>n;
	input();
	pre_dfs(1);
	dfs(1);
	cout<<ans[1];
}
signed main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // freopen(".inp", "r", stdin);
    // freopen(".out", "w", stdout);
    int tt=1;
    //cin>>tt; 
    while(tt--){
    	sol();
    }
    cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 3420 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 3164 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 3160 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 3420 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 4440 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 10432 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 17 ms 17776 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 10844 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 17 ms 10772 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 12120 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 17 ms 11936 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -