Submission #636262

# Submission time Handle Problem Language Result Execution time Memory
636262 2022-08-28T16:34:35 Z jame0313 Tree Rotations (POI11_rot) C++17
63 / 100
264 ms 59840 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using ld = long double;
using pll = pair<ll, ll>;
// and so on
int _cnt = 0;
int arr[200001];
struct segtree{
	vector<int> tree[(1<<19)+1];
    int siz;
	void init(int N){
		for(siz=1;siz<N;siz<<=1);
        for(int i=0;i<N;i++){
            tree[siz+i] = {arr[i]};
        }
        for(int pos=siz-1;pos;pos--){
            tree[pos] = tree[pos*2];
            tree[pos].insert(tree[pos].end(),tree[pos*2+1].begin(),tree[pos*2+1].end());
            sort(tree[pos].begin(),tree[pos].end());
        }
	}
	int sol(int l, int r, int s, int e, int pos, int v){
		if(s<=l && r<=e) return upper_bound(tree[pos].begin(),tree[pos].end(),v) - tree[pos].begin();
		if(e<l || r<s) return 0;
		return sol(l,(l+r)/2,s,e,pos*2,v)+sol((l+r)/2+1,r,s,e,pos*2+1,v);
	}
	int sol(int s, int e, int v){
		return sol(0,siz-1,s,e,1,v);
	}
} tree;
int in[200001];
int out[200001];
vector<vector<int> > mp;
int id = -1;
int parse(int x){
    int c;
    cin>>c;
    in[x] = x;
    if(c==0){
        int l = parse(++id);
        int r = parse(++id);
        mp[x].push_back(l);
        mp[x].push_back(r);
    }
    else{
        arr[x] = c;
    }
    out[x] = id;
    return in[x];
}
ll sol(int x) {
    ll ret = 0;
    if (arr[x] == 0) {
        int a = mp[x][0];
        int b = mp[x][1];
        ret += sol(a) + sol(b);
        if(out[a] - in[a] < out[b] - in[b]) swap(a, b);
        ll acnt = 0, bcnt = 0;
        int zero_cnt = tree.sol(in[a],out[a],0);
        int siz = out[a] - in[a] + 1 - zero_cnt;
        for(int i=in[b];i<=out[b];i++){
            if(!arr[i]) continue;
            int cnt = tree.sol(in[a],out[a],arr[i]-1) - zero_cnt;
            //cout<<cnt<<endl;
            acnt += cnt;
            bcnt += siz - cnt;
        }
        ret += min(acnt, bcnt);
        return ret;
 
    } else {
        return 0;
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    mp.resize(2*N+1);
    int r = parse(++id);
    tree.init(id+1);
    cout<<sol(r);
    
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12628 KB Output is correct
2 Correct 7 ms 12628 KB Output is correct
3 Correct 10 ms 12628 KB Output is correct
4 Correct 8 ms 12644 KB Output is correct
5 Correct 7 ms 12628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12600 KB Output is correct
2 Correct 10 ms 12628 KB Output is correct
3 Correct 8 ms 12628 KB Output is correct
4 Correct 9 ms 12628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12796 KB Output is correct
2 Correct 9 ms 12924 KB Output is correct
3 Correct 9 ms 12820 KB Output is correct
4 Correct 8 ms 13012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 14576 KB Output is correct
2 Correct 16 ms 14036 KB Output is correct
3 Correct 18 ms 14836 KB Output is correct
4 Correct 15 ms 14928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 18948 KB Output is correct
2 Correct 42 ms 16880 KB Output is correct
3 Correct 125 ms 22988 KB Output is correct
4 Correct 34 ms 18764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 30592 KB Output is correct
2 Correct 137 ms 33940 KB Output is correct
3 Correct 176 ms 39132 KB Output is correct
4 Correct 151 ms 39880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 236 ms 59840 KB Output is correct
2 Correct 264 ms 52704 KB Output is correct
3 Correct 257 ms 46944 KB Output is correct
4 Correct 199 ms 45288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 56 ms 49268 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 60 ms 53824 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 75 ms 57408 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 56 ms 58164 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -