Submission #171776

# Submission time Handle Problem Language Result Execution time Memory
171776 2019-12-30T11:26:58 Z balbit Construction of Highway (JOI18_construction) C++14
100 / 100
858 ms 28648 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for (int i=(n-1); i>=0; --i)
#define REP1(i,n) FOR(i,1,n+1)
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif

const int iinf = 1<<29;
const ll inf = 1ll<<60;
const ll mod = 1e9+7;


void GG(){cout<<"-1\n"; exit(0);}

ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
    ll re=1;
    while (n>0){
        if (n&1) re = re*a %mo;
        a = a*a %mo;
        n>>=1;
    }
    return re;
}

ll inv (ll b, ll mo = mod){
    if (b==1) return b;
    return (mo-mo/b) * inv(mo%b) % mo;
}

const int maxn = 1e5+5;

struct BIT{
    vector<ll> s;
    int MX=0;
    ll QU(int e){
        ll re = 0; e ++;
        while (e>0) {
            re+=s[e]; e-=e&(-e);
        }return re;
    }
    void MO(int e, ll v){
        e++; while (e<MX){
            s[e]+=v; e+=e&(-e);
        }
    }
    BIT(int _mx){
        MX = _mx; s.resize(MX+1);
    }
};

int top[maxn]; // Lowest depth for this color
int rt[maxn]; // Rightmost child in its sub tree
int at1[maxn], at2[maxn]; // Position for each node in euler tour 1, euler tour 2
int fa[21][maxn], dep[maxn];

int it1 = 0, it2 = 0;
vector<int> g[maxn];
void dfs(int v, int p) {
    at1[v] = it1++;
    rt[v] = v;
    for (int u : g[v]) {
        if (u != p) {
            dep[u] = dep[v]+1; fa[0][u]= v;
            dfs(u,v);
            rt[v] = rt[u];
        }
    }
    at2[v] = it2++;
}

int C[maxn];
BIT b1 (maxn), b2(maxn);
vector<int> order;

int kth(int a, int k) {
    for (int j = 0; k!=0; j++,k/=2) {
        if (k&1) a = fa[j][a];
    }
    return a;
}


inline void upd(int a, int v) { // Change all ancestors of a (including a) by v
    b1.MO(0,v); b2.MO(0,v);
    b1.MO(at1[rt[a]]+1,-v);
    b2.MO(at2[a],-v);
}

inline int val(int a){
    return b1.QU(at1[a])-b2.QU(at2[a]);
}

BIT invbit(maxn+100);
ll inversions(vector<pii> &a){
    ll re = 0;
    for (pii ele : a) {
        re += invbit.QU(ele.f-1) * ele.s;
        invbit.MO(ele.f,ele.s);
    }
    for (pii ele : a) {
        invbit.MO(ele.f,-ele.s);
    }
    return re;
}

signed main(){
    IOS();
#ifdef BALBIT
freopen("input.in","r",stdin);
#endif // BALBIT
    int n; cin>>n;
    vector<int> tmpc(n);
    REP(i,n) cin>>tmpc[i];
    vector<int> tmpc2 = tmpc;
    sort(ALL(tmpc));
    REP(i,n) C[i] = lower_bound(ALL(tmpc), tmpc2[i]) - tmpc.begin();
    REP(i,n-1) {
        int a, b; cin>>a>>b; a--; b--; g[a].pb(b); order.pb(b);
    }
    dfs(0,-1);
    FOR(j,1,21) REP(i,n) {
        fa[j][i] = fa[j-1][fa[j-1][i]];
    }
    fill(top, top+n, 10000000); top[0] = 0; // 1 is already in place
    for (int B : order){
        bug(B);
        vector<pii> met; // C-values
        upd(B,B);
        int chg = B;
        int tocol = B;
        while (B != 0){
            int p = fa[0][B];
            int pval = val(p) - chg;
            bug(pval);
            int odep = dep[B];
            int addto = tocol-pval-chg;
            chg += addto;
            upd(p, addto);
            int totop = top[pval];
            top[pval] = dep[B];
            B = kth(B, dep[B]-totop);
            met.pb({C[pval],odep-dep[B]});
            bug(B);
        }
        top[tocol] = 0;

//#ifdef BALBIT
//        REP(i,n) {
//            bug(i+1, val(i));
//        }
//        for (pii item : met) {
//            cout<<item.f<<' '<<item.s<<endl;
//        }
//        cout<<"====="<<endl;
//#endif
        cout<<inversions(met)<<"\n";
    }

}




# Verdict Execution time Memory Grader output
1 Correct 6 ms 5244 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 7 ms 5240 KB Output is correct
6 Correct 7 ms 5240 KB Output is correct
7 Correct 7 ms 5240 KB Output is correct
8 Correct 7 ms 5240 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 6 ms 5244 KB Output is correct
11 Correct 7 ms 5240 KB Output is correct
12 Correct 7 ms 5240 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 7 ms 5288 KB Output is correct
15 Correct 8 ms 5240 KB Output is correct
16 Correct 8 ms 5240 KB Output is correct
17 Correct 8 ms 5264 KB Output is correct
18 Correct 8 ms 5216 KB Output is correct
19 Correct 7 ms 5240 KB Output is correct
20 Correct 7 ms 5248 KB Output is correct
21 Correct 7 ms 5180 KB Output is correct
22 Correct 7 ms 5240 KB Output is correct
23 Correct 7 ms 5180 KB Output is correct
24 Correct 7 ms 5240 KB Output is correct
25 Correct 7 ms 5212 KB Output is correct
26 Correct 7 ms 5240 KB Output is correct
27 Correct 7 ms 5228 KB Output is correct
28 Correct 7 ms 5240 KB Output is correct
29 Correct 7 ms 5212 KB Output is correct
30 Correct 8 ms 5240 KB Output is correct
31 Correct 8 ms 5244 KB Output is correct
32 Correct 7 ms 5244 KB Output is correct
33 Correct 7 ms 5240 KB Output is correct
34 Correct 7 ms 5240 KB Output is correct
35 Correct 7 ms 5240 KB Output is correct
36 Correct 7 ms 5240 KB Output is correct
37 Correct 7 ms 5320 KB Output is correct
38 Correct 7 ms 5244 KB Output is correct
39 Correct 7 ms 5212 KB Output is correct
40 Correct 7 ms 5188 KB Output is correct
41 Correct 7 ms 5200 KB Output is correct
42 Correct 7 ms 5240 KB Output is correct
43 Correct 7 ms 5212 KB Output is correct
44 Correct 7 ms 5264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5244 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 7 ms 5240 KB Output is correct
6 Correct 7 ms 5240 KB Output is correct
7 Correct 7 ms 5240 KB Output is correct
8 Correct 7 ms 5240 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 6 ms 5244 KB Output is correct
11 Correct 7 ms 5240 KB Output is correct
12 Correct 7 ms 5240 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 7 ms 5288 KB Output is correct
15 Correct 8 ms 5240 KB Output is correct
16 Correct 8 ms 5240 KB Output is correct
17 Correct 8 ms 5264 KB Output is correct
18 Correct 8 ms 5216 KB Output is correct
19 Correct 7 ms 5240 KB Output is correct
20 Correct 7 ms 5248 KB Output is correct
21 Correct 7 ms 5180 KB Output is correct
22 Correct 7 ms 5240 KB Output is correct
23 Correct 7 ms 5180 KB Output is correct
24 Correct 7 ms 5240 KB Output is correct
25 Correct 7 ms 5212 KB Output is correct
26 Correct 7 ms 5240 KB Output is correct
27 Correct 7 ms 5228 KB Output is correct
28 Correct 7 ms 5240 KB Output is correct
29 Correct 7 ms 5212 KB Output is correct
30 Correct 8 ms 5240 KB Output is correct
31 Correct 8 ms 5244 KB Output is correct
32 Correct 7 ms 5244 KB Output is correct
33 Correct 7 ms 5240 KB Output is correct
34 Correct 7 ms 5240 KB Output is correct
35 Correct 7 ms 5240 KB Output is correct
36 Correct 7 ms 5240 KB Output is correct
37 Correct 7 ms 5320 KB Output is correct
38 Correct 7 ms 5244 KB Output is correct
39 Correct 7 ms 5212 KB Output is correct
40 Correct 7 ms 5188 KB Output is correct
41 Correct 7 ms 5200 KB Output is correct
42 Correct 7 ms 5240 KB Output is correct
43 Correct 7 ms 5212 KB Output is correct
44 Correct 7 ms 5264 KB Output is correct
45 Correct 9 ms 5368 KB Output is correct
46 Correct 17 ms 5752 KB Output is correct
47 Correct 17 ms 5840 KB Output is correct
48 Correct 17 ms 5872 KB Output is correct
49 Correct 12 ms 6136 KB Output is correct
50 Correct 12 ms 6008 KB Output is correct
51 Correct 12 ms 6008 KB Output is correct
52 Correct 13 ms 5880 KB Output is correct
53 Correct 13 ms 6008 KB Output is correct
54 Correct 12 ms 5880 KB Output is correct
55 Correct 12 ms 6008 KB Output is correct
56 Correct 13 ms 5956 KB Output is correct
57 Correct 25 ms 5752 KB Output is correct
58 Correct 25 ms 5880 KB Output is correct
59 Correct 25 ms 5880 KB Output is correct
60 Correct 25 ms 5752 KB Output is correct
61 Correct 13 ms 5880 KB Output is correct
62 Correct 13 ms 5880 KB Output is correct
63 Correct 13 ms 5960 KB Output is correct
64 Correct 16 ms 5752 KB Output is correct
65 Correct 16 ms 5752 KB Output is correct
66 Correct 17 ms 5752 KB Output is correct
67 Correct 16 ms 5752 KB Output is correct
68 Correct 12 ms 6008 KB Output is correct
69 Correct 13 ms 5880 KB Output is correct
70 Correct 12 ms 5880 KB Output is correct
71 Correct 13 ms 5880 KB Output is correct
72 Correct 25 ms 5752 KB Output is correct
73 Correct 25 ms 5804 KB Output is correct
74 Correct 13 ms 5904 KB Output is correct
75 Correct 13 ms 5848 KB Output is correct
76 Correct 13 ms 5880 KB Output is correct
77 Correct 13 ms 5788 KB Output is correct
78 Correct 13 ms 5880 KB Output is correct
79 Correct 12 ms 5752 KB Output is correct
80 Correct 12 ms 5880 KB Output is correct
81 Correct 13 ms 5888 KB Output is correct
82 Correct 13 ms 5880 KB Output is correct
83 Correct 13 ms 5752 KB Output is correct
84 Correct 13 ms 5880 KB Output is correct
85 Correct 13 ms 5752 KB Output is correct
86 Correct 13 ms 5752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5244 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 7 ms 5240 KB Output is correct
6 Correct 7 ms 5240 KB Output is correct
7 Correct 7 ms 5240 KB Output is correct
8 Correct 7 ms 5240 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 6 ms 5244 KB Output is correct
11 Correct 7 ms 5240 KB Output is correct
12 Correct 7 ms 5240 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 7 ms 5288 KB Output is correct
15 Correct 8 ms 5240 KB Output is correct
16 Correct 8 ms 5240 KB Output is correct
17 Correct 8 ms 5264 KB Output is correct
18 Correct 8 ms 5216 KB Output is correct
19 Correct 7 ms 5240 KB Output is correct
20 Correct 7 ms 5248 KB Output is correct
21 Correct 7 ms 5180 KB Output is correct
22 Correct 7 ms 5240 KB Output is correct
23 Correct 7 ms 5180 KB Output is correct
24 Correct 7 ms 5240 KB Output is correct
25 Correct 7 ms 5212 KB Output is correct
26 Correct 7 ms 5240 KB Output is correct
27 Correct 7 ms 5228 KB Output is correct
28 Correct 7 ms 5240 KB Output is correct
29 Correct 7 ms 5212 KB Output is correct
30 Correct 8 ms 5240 KB Output is correct
31 Correct 8 ms 5244 KB Output is correct
32 Correct 7 ms 5244 KB Output is correct
33 Correct 7 ms 5240 KB Output is correct
34 Correct 7 ms 5240 KB Output is correct
35 Correct 7 ms 5240 KB Output is correct
36 Correct 7 ms 5240 KB Output is correct
37 Correct 7 ms 5320 KB Output is correct
38 Correct 7 ms 5244 KB Output is correct
39 Correct 7 ms 5212 KB Output is correct
40 Correct 7 ms 5188 KB Output is correct
41 Correct 7 ms 5200 KB Output is correct
42 Correct 7 ms 5240 KB Output is correct
43 Correct 7 ms 5212 KB Output is correct
44 Correct 7 ms 5264 KB Output is correct
45 Correct 9 ms 5368 KB Output is correct
46 Correct 17 ms 5752 KB Output is correct
47 Correct 17 ms 5840 KB Output is correct
48 Correct 17 ms 5872 KB Output is correct
49 Correct 12 ms 6136 KB Output is correct
50 Correct 12 ms 6008 KB Output is correct
51 Correct 12 ms 6008 KB Output is correct
52 Correct 13 ms 5880 KB Output is correct
53 Correct 13 ms 6008 KB Output is correct
54 Correct 12 ms 5880 KB Output is correct
55 Correct 12 ms 6008 KB Output is correct
56 Correct 13 ms 5956 KB Output is correct
57 Correct 25 ms 5752 KB Output is correct
58 Correct 25 ms 5880 KB Output is correct
59 Correct 25 ms 5880 KB Output is correct
60 Correct 25 ms 5752 KB Output is correct
61 Correct 13 ms 5880 KB Output is correct
62 Correct 13 ms 5880 KB Output is correct
63 Correct 13 ms 5960 KB Output is correct
64 Correct 16 ms 5752 KB Output is correct
65 Correct 16 ms 5752 KB Output is correct
66 Correct 17 ms 5752 KB Output is correct
67 Correct 16 ms 5752 KB Output is correct
68 Correct 12 ms 6008 KB Output is correct
69 Correct 13 ms 5880 KB Output is correct
70 Correct 12 ms 5880 KB Output is correct
71 Correct 13 ms 5880 KB Output is correct
72 Correct 25 ms 5752 KB Output is correct
73 Correct 25 ms 5804 KB Output is correct
74 Correct 13 ms 5904 KB Output is correct
75 Correct 13 ms 5848 KB Output is correct
76 Correct 13 ms 5880 KB Output is correct
77 Correct 13 ms 5788 KB Output is correct
78 Correct 13 ms 5880 KB Output is correct
79 Correct 12 ms 5752 KB Output is correct
80 Correct 12 ms 5880 KB Output is correct
81 Correct 13 ms 5888 KB Output is correct
82 Correct 13 ms 5880 KB Output is correct
83 Correct 13 ms 5752 KB Output is correct
84 Correct 13 ms 5880 KB Output is correct
85 Correct 13 ms 5752 KB Output is correct
86 Correct 13 ms 5752 KB Output is correct
87 Correct 36 ms 6772 KB Output is correct
88 Correct 108 ms 9860 KB Output is correct
89 Correct 444 ms 20792 KB Output is correct
90 Correct 441 ms 20904 KB Output is correct
91 Correct 442 ms 20860 KB Output is correct
92 Correct 192 ms 28584 KB Output is correct
93 Correct 206 ms 28552 KB Output is correct
94 Correct 204 ms 28648 KB Output is correct
95 Correct 218 ms 24152 KB Output is correct
96 Correct 227 ms 24560 KB Output is correct
97 Correct 228 ms 24728 KB Output is correct
98 Correct 233 ms 24656 KB Output is correct
99 Correct 229 ms 24068 KB Output is correct
100 Correct 756 ms 20988 KB Output is correct
101 Correct 858 ms 21392 KB Output is correct
102 Correct 817 ms 21300 KB Output is correct
103 Correct 838 ms 21336 KB Output is correct
104 Correct 271 ms 23920 KB Output is correct
105 Correct 272 ms 23920 KB Output is correct
106 Correct 283 ms 24060 KB Output is correct
107 Correct 405 ms 20068 KB Output is correct
108 Correct 415 ms 20252 KB Output is correct
109 Correct 455 ms 20756 KB Output is correct
110 Correct 184 ms 28084 KB Output is correct
111 Correct 209 ms 24176 KB Output is correct
112 Correct 205 ms 24088 KB Output is correct
113 Correct 195 ms 23356 KB Output is correct
114 Correct 778 ms 21104 KB Output is correct
115 Correct 754 ms 20848 KB Output is correct
116 Correct 274 ms 23408 KB Output is correct
117 Correct 239 ms 22112 KB Output is correct
118 Correct 211 ms 21724 KB Output is correct
119 Correct 257 ms 21388 KB Output is correct
120 Correct 267 ms 21616 KB Output is correct
121 Correct 417 ms 21232 KB Output is correct
122 Correct 218 ms 21096 KB Output is correct
123 Correct 273 ms 22208 KB Output is correct
124 Correct 266 ms 21616 KB Output is correct
125 Correct 277 ms 21360 KB Output is correct
126 Correct 262 ms 21904 KB Output is correct
127 Correct 250 ms 21232 KB Output is correct
128 Correct 257 ms 21104 KB Output is correct