Submission #636263

# Submission time Handle Problem Language Result Execution time Memory
636263 2022-08-28T16:35:16 Z jame0313 Tree Rotations (POI11_rot) C++17
54 / 100
207 ms 65536 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<<20)+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 14 ms 24916 KB Output is correct
2 Correct 17 ms 24916 KB Output is correct
3 Correct 14 ms 24916 KB Output is correct
4 Correct 13 ms 24936 KB Output is correct
5 Correct 13 ms 24876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24884 KB Output is correct
2 Correct 14 ms 24916 KB Output is correct
3 Correct 15 ms 24916 KB Output is correct
4 Correct 15 ms 24916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 25172 KB Output is correct
2 Correct 14 ms 25172 KB Output is correct
3 Correct 15 ms 25224 KB Output is correct
4 Correct 16 ms 25296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 26836 KB Output is correct
2 Correct 28 ms 26312 KB Output is correct
3 Correct 22 ms 27092 KB Output is correct
4 Correct 21 ms 27168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 31116 KB Output is correct
2 Correct 47 ms 29012 KB Output is correct
3 Correct 126 ms 35068 KB Output is correct
4 Correct 40 ms 30936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 207 ms 42480 KB Output is correct
2 Correct 143 ms 45896 KB Output is correct
3 Correct 182 ms 51024 KB Output is correct
4 Correct 192 ms 51708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 116 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 75 ms 65536 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 76 ms 65536 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 80 ms 65536 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 93 ms 65536 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -