Submission #219602

# Submission time Handle Problem Language Result Execution time Memory
219602 2020-04-05T16:55:32 Z thebes Construction of Highway (JOI18_construction) C++14
100 / 100
634 ms 150700 KB
#include <bits/stdc++.h>
#define DEBUG 1
using namespace std;

namespace output{
    void __(short x){cout<<x;}
    void __(unsigned x){cout<<x;}
    void __(int x){cout<<x;}
    void __(long long x){cout<<x;}
    void __(unsigned long long x){cout<<x;}
    void __(double x){cout<<x;}
    void __(long double x){cout<<x;}
    void __(char x){cout<<x;}
    void __(const char*x){cout<<x;}
    void __(const string&x){cout<<x;}
    void __(bool x){cout<<(x?"true":"false");}
    template<class S,class T>
    void __(const pair<S,T>&x){__(DEBUG?"(":""),__(x.first),__(DEBUG?", ":" "),__(x.second),__(DEBUG?")":"");}
    template<class T>
    void __(const vector<T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
    template<class T>
    void __(const set<T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
    template<class T>
    void __(const multiset<T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
    template<class S,class T>
    void __(const map<S,T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
    void pr(){cout<<"\n";}
    template<class S,class... T>
    void pr(const S&a,const T&...b){__(a);if(sizeof...(b))__(' ');pr(b...);}
}

using namespace output;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,char> pic;
typedef pair<double,double> pdd;
typedef pair<ld,ld> pld;
typedef vector<int> vi;
typedef vector<ll> vl;

#define pb push_back
#define fox(i,x,y) for(int i=(x);i<=(y);i++)
#define foxr(i,x,y) for(int i=(y);i>=(x);i--)

struct Treap{
    struct nd{
        int p, sz, sm, k; nd *l, *r;
        nd(int key,int size):p(rand()),sz(size),sm(size),k(key),l(0),r(0){}
    }*rt=NULL,*l,*r,*t;
    void clear(){
        rt=l=r=t=NULL;
    }
    inline int sz(nd *n){return n?n->sz:0;}
    inline int sm(nd *n){return n?n->sm:0;}
    inline void upd(nd *&n){
        if(!n) return;
        n->sm=sm(n->l)+sm(n->r)+sz(n);
    }
    void split(nd *n,nd *&l,nd *&r,int k){
        if(!n) l=r=NULL;
        else if(n->k<=k) split(n->r,n->r,r,k),l=n;
        else split(n->l,l,n->l,k),r=n;
        upd(n);
    }
    void mrg(nd *&n,nd *l,nd *r){
        if(!l||!r) n=l?l:r;
        else if(l->p>r->p) mrg(l->r,l->r,r),n=l;
        else mrg(r->l,l,r->l),n=r;
        upd(n);
    }
    ll insert(int key,int size){
        split(rt,l,r,key);
        split(l,l,t,key-1);
        ll ret = 1LL*size*sm(l);
        if(!t) t=new nd(key,size);
        else t->sz+=size,t->sm+=size;
        mrg(l,l,t);
        mrg(rt,l,r);
        return ret;
    }
}treap;

const int MN = 1e5+5;
ll N, c[MN], i, x, y, id[MN], sz[MN], len[MN], par[MN], lnk[MN], nxt, stid, pos[MN];
struct idk{ll st, ed, val;};
stack<idk> hld[MN];
vl adj[MN]; pll ed[MN], big[MN];

void dfs(ll n){
    sz[n] = 1;
    big[n]={-1,-1};
    for(auto v : adj[n]){
        par[v] = n;
        dfs(v);
        sz[n] += sz[v];
        if(sz[v]>big[n].first)
            big[n]={sz[v],v};
    }
    if(big[n].second!=-1) len[n]=1+len[big[n].second];
}

void dfs2(ll n){
    if(!lnk[n]){
        lnk[n]=n; pos[n]=++nxt; nxt+=len[n];
        id[n]=++stid; 
    }
    else{
        pos[n]=pos[par[n]]+1; 
        id[n]=id[par[n]];
    }
    for(auto v : adj[n]){
        if(v==big[n].second) lnk[v] = lnk[n];
        dfs2(v);
    }
}

int main(){
    srand(time(0));
    for(scanf("%lld",&N),i=1;i<=N;i++)
        scanf("%lld",&c[i]);
    for(i=1;i<N;i++){
        scanf("%lld%lld",&x,&y);
        adj[x].pb(y);
        ed[i]={x,y};
    }
    dfs(1); dfs2(1);
    hld[id[1]].push({pos[1],pos[1],c[1]});
    for(i=1;i<N;i++){
        treap.clear();
        ll ans = 0, cur = ed[i].second, rep, st, tmp, col=c[ed[i].second];
        vector<pll> blk;
        while(cur){
            rep = lnk[cur]; blk.clear();
            st = id[rep];
            while(hld[st].size()){
                if(hld[st].top().ed<=pos[cur]){
                    blk.pb({hld[st].top().ed-hld[st].top().st+1,hld[st].top().val});
                    hld[st].pop();
                }
                else{
                    blk.pb({pos[cur]-hld[st].top().st+1,hld[st].top().val});
                    tmp=hld[st].top().ed; hld[st].pop();
                    hld[st].push({pos[cur]+1,tmp,blk.back().second});
                    break;
                }
            }
            hld[st].push({pos[rep],pos[cur],col});
            reverse(blk.begin(),blk.end());
            for(auto v : blk){
                ans += treap.insert(v.second,v.first);
            }
            cur = par[rep];
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:122:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(scanf("%lld",&N),i=1;i<=N;i++)
         ~~~~~~~~~~~~~~~~^~~~
construction.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",&c[i]);
         ~~~~~^~~~~~~~~~~~~~
construction.cpp:125:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld%lld",&x,&y);
         ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 52 ms 68480 KB Output is correct
2 Correct 54 ms 68472 KB Output is correct
3 Correct 51 ms 68472 KB Output is correct
4 Correct 54 ms 68608 KB Output is correct
5 Correct 52 ms 68608 KB Output is correct
6 Correct 52 ms 68600 KB Output is correct
7 Correct 51 ms 68600 KB Output is correct
8 Correct 52 ms 68600 KB Output is correct
9 Correct 51 ms 68600 KB Output is correct
10 Correct 51 ms 68556 KB Output is correct
11 Correct 54 ms 68604 KB Output is correct
12 Correct 52 ms 68600 KB Output is correct
13 Correct 52 ms 68600 KB Output is correct
14 Correct 53 ms 68728 KB Output is correct
15 Correct 54 ms 68728 KB Output is correct
16 Correct 53 ms 68728 KB Output is correct
17 Correct 54 ms 68856 KB Output is correct
18 Correct 52 ms 68736 KB Output is correct
19 Correct 53 ms 68600 KB Output is correct
20 Correct 53 ms 68600 KB Output is correct
21 Correct 52 ms 68600 KB Output is correct
22 Correct 53 ms 68600 KB Output is correct
23 Correct 52 ms 68600 KB Output is correct
24 Correct 53 ms 68600 KB Output is correct
25 Correct 51 ms 68600 KB Output is correct
26 Correct 55 ms 68608 KB Output is correct
27 Correct 53 ms 68600 KB Output is correct
28 Correct 54 ms 68600 KB Output is correct
29 Correct 52 ms 68600 KB Output is correct
30 Correct 54 ms 68728 KB Output is correct
31 Correct 52 ms 68600 KB Output is correct
32 Correct 52 ms 68600 KB Output is correct
33 Correct 53 ms 68600 KB Output is correct
34 Correct 52 ms 68600 KB Output is correct
35 Correct 53 ms 68608 KB Output is correct
36 Correct 52 ms 68600 KB Output is correct
37 Correct 52 ms 68600 KB Output is correct
38 Correct 56 ms 68728 KB Output is correct
39 Correct 53 ms 68600 KB Output is correct
40 Correct 52 ms 68600 KB Output is correct
41 Correct 54 ms 68608 KB Output is correct
42 Correct 51 ms 68600 KB Output is correct
43 Correct 54 ms 68600 KB Output is correct
44 Correct 55 ms 68600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 68480 KB Output is correct
2 Correct 54 ms 68472 KB Output is correct
3 Correct 51 ms 68472 KB Output is correct
4 Correct 54 ms 68608 KB Output is correct
5 Correct 52 ms 68608 KB Output is correct
6 Correct 52 ms 68600 KB Output is correct
7 Correct 51 ms 68600 KB Output is correct
8 Correct 52 ms 68600 KB Output is correct
9 Correct 51 ms 68600 KB Output is correct
10 Correct 51 ms 68556 KB Output is correct
11 Correct 54 ms 68604 KB Output is correct
12 Correct 52 ms 68600 KB Output is correct
13 Correct 52 ms 68600 KB Output is correct
14 Correct 53 ms 68728 KB Output is correct
15 Correct 54 ms 68728 KB Output is correct
16 Correct 53 ms 68728 KB Output is correct
17 Correct 54 ms 68856 KB Output is correct
18 Correct 52 ms 68736 KB Output is correct
19 Correct 53 ms 68600 KB Output is correct
20 Correct 53 ms 68600 KB Output is correct
21 Correct 52 ms 68600 KB Output is correct
22 Correct 53 ms 68600 KB Output is correct
23 Correct 52 ms 68600 KB Output is correct
24 Correct 53 ms 68600 KB Output is correct
25 Correct 51 ms 68600 KB Output is correct
26 Correct 55 ms 68608 KB Output is correct
27 Correct 53 ms 68600 KB Output is correct
28 Correct 54 ms 68600 KB Output is correct
29 Correct 52 ms 68600 KB Output is correct
30 Correct 54 ms 68728 KB Output is correct
31 Correct 52 ms 68600 KB Output is correct
32 Correct 52 ms 68600 KB Output is correct
33 Correct 53 ms 68600 KB Output is correct
34 Correct 52 ms 68600 KB Output is correct
35 Correct 53 ms 68608 KB Output is correct
36 Correct 52 ms 68600 KB Output is correct
37 Correct 52 ms 68600 KB Output is correct
38 Correct 56 ms 68728 KB Output is correct
39 Correct 53 ms 68600 KB Output is correct
40 Correct 52 ms 68600 KB Output is correct
41 Correct 54 ms 68608 KB Output is correct
42 Correct 51 ms 68600 KB Output is correct
43 Correct 54 ms 68600 KB Output is correct
44 Correct 55 ms 68600 KB Output is correct
45 Correct 51 ms 68728 KB Output is correct
46 Correct 59 ms 70008 KB Output is correct
47 Correct 58 ms 70008 KB Output is correct
48 Correct 60 ms 70008 KB Output is correct
49 Correct 69 ms 69368 KB Output is correct
50 Correct 55 ms 69372 KB Output is correct
51 Correct 54 ms 69368 KB Output is correct
52 Correct 54 ms 69368 KB Output is correct
53 Correct 54 ms 69368 KB Output is correct
54 Correct 57 ms 69368 KB Output is correct
55 Correct 64 ms 69368 KB Output is correct
56 Correct 54 ms 69368 KB Output is correct
57 Correct 59 ms 70776 KB Output is correct
58 Correct 63 ms 70904 KB Output is correct
59 Correct 61 ms 70908 KB Output is correct
60 Correct 62 ms 70904 KB Output is correct
61 Correct 56 ms 69368 KB Output is correct
62 Correct 64 ms 69344 KB Output is correct
63 Correct 54 ms 69368 KB Output is correct
64 Correct 54 ms 69368 KB Output is correct
65 Correct 57 ms 69752 KB Output is correct
66 Correct 56 ms 69880 KB Output is correct
67 Correct 57 ms 70008 KB Output is correct
68 Correct 53 ms 69376 KB Output is correct
69 Correct 53 ms 69368 KB Output is correct
70 Correct 54 ms 69368 KB Output is correct
71 Correct 54 ms 69368 KB Output is correct
72 Correct 59 ms 70904 KB Output is correct
73 Correct 61 ms 70648 KB Output is correct
74 Correct 52 ms 69368 KB Output is correct
75 Correct 60 ms 69368 KB Output is correct
76 Correct 53 ms 69368 KB Output is correct
77 Correct 55 ms 69376 KB Output is correct
78 Correct 53 ms 69368 KB Output is correct
79 Correct 53 ms 69372 KB Output is correct
80 Correct 53 ms 69376 KB Output is correct
81 Correct 60 ms 69496 KB Output is correct
82 Correct 54 ms 69488 KB Output is correct
83 Correct 54 ms 69496 KB Output is correct
84 Correct 55 ms 69368 KB Output is correct
85 Correct 54 ms 69372 KB Output is correct
86 Correct 53 ms 69368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 68480 KB Output is correct
2 Correct 54 ms 68472 KB Output is correct
3 Correct 51 ms 68472 KB Output is correct
4 Correct 54 ms 68608 KB Output is correct
5 Correct 52 ms 68608 KB Output is correct
6 Correct 52 ms 68600 KB Output is correct
7 Correct 51 ms 68600 KB Output is correct
8 Correct 52 ms 68600 KB Output is correct
9 Correct 51 ms 68600 KB Output is correct
10 Correct 51 ms 68556 KB Output is correct
11 Correct 54 ms 68604 KB Output is correct
12 Correct 52 ms 68600 KB Output is correct
13 Correct 52 ms 68600 KB Output is correct
14 Correct 53 ms 68728 KB Output is correct
15 Correct 54 ms 68728 KB Output is correct
16 Correct 53 ms 68728 KB Output is correct
17 Correct 54 ms 68856 KB Output is correct
18 Correct 52 ms 68736 KB Output is correct
19 Correct 53 ms 68600 KB Output is correct
20 Correct 53 ms 68600 KB Output is correct
21 Correct 52 ms 68600 KB Output is correct
22 Correct 53 ms 68600 KB Output is correct
23 Correct 52 ms 68600 KB Output is correct
24 Correct 53 ms 68600 KB Output is correct
25 Correct 51 ms 68600 KB Output is correct
26 Correct 55 ms 68608 KB Output is correct
27 Correct 53 ms 68600 KB Output is correct
28 Correct 54 ms 68600 KB Output is correct
29 Correct 52 ms 68600 KB Output is correct
30 Correct 54 ms 68728 KB Output is correct
31 Correct 52 ms 68600 KB Output is correct
32 Correct 52 ms 68600 KB Output is correct
33 Correct 53 ms 68600 KB Output is correct
34 Correct 52 ms 68600 KB Output is correct
35 Correct 53 ms 68608 KB Output is correct
36 Correct 52 ms 68600 KB Output is correct
37 Correct 52 ms 68600 KB Output is correct
38 Correct 56 ms 68728 KB Output is correct
39 Correct 53 ms 68600 KB Output is correct
40 Correct 52 ms 68600 KB Output is correct
41 Correct 54 ms 68608 KB Output is correct
42 Correct 51 ms 68600 KB Output is correct
43 Correct 54 ms 68600 KB Output is correct
44 Correct 55 ms 68600 KB Output is correct
45 Correct 51 ms 68728 KB Output is correct
46 Correct 59 ms 70008 KB Output is correct
47 Correct 58 ms 70008 KB Output is correct
48 Correct 60 ms 70008 KB Output is correct
49 Correct 69 ms 69368 KB Output is correct
50 Correct 55 ms 69372 KB Output is correct
51 Correct 54 ms 69368 KB Output is correct
52 Correct 54 ms 69368 KB Output is correct
53 Correct 54 ms 69368 KB Output is correct
54 Correct 57 ms 69368 KB Output is correct
55 Correct 64 ms 69368 KB Output is correct
56 Correct 54 ms 69368 KB Output is correct
57 Correct 59 ms 70776 KB Output is correct
58 Correct 63 ms 70904 KB Output is correct
59 Correct 61 ms 70908 KB Output is correct
60 Correct 62 ms 70904 KB Output is correct
61 Correct 56 ms 69368 KB Output is correct
62 Correct 64 ms 69344 KB Output is correct
63 Correct 54 ms 69368 KB Output is correct
64 Correct 54 ms 69368 KB Output is correct
65 Correct 57 ms 69752 KB Output is correct
66 Correct 56 ms 69880 KB Output is correct
67 Correct 57 ms 70008 KB Output is correct
68 Correct 53 ms 69376 KB Output is correct
69 Correct 53 ms 69368 KB Output is correct
70 Correct 54 ms 69368 KB Output is correct
71 Correct 54 ms 69368 KB Output is correct
72 Correct 59 ms 70904 KB Output is correct
73 Correct 61 ms 70648 KB Output is correct
74 Correct 52 ms 69368 KB Output is correct
75 Correct 60 ms 69368 KB Output is correct
76 Correct 53 ms 69368 KB Output is correct
77 Correct 55 ms 69376 KB Output is correct
78 Correct 53 ms 69368 KB Output is correct
79 Correct 53 ms 69372 KB Output is correct
80 Correct 53 ms 69376 KB Output is correct
81 Correct 60 ms 69496 KB Output is correct
82 Correct 54 ms 69488 KB Output is correct
83 Correct 54 ms 69496 KB Output is correct
84 Correct 55 ms 69368 KB Output is correct
85 Correct 54 ms 69372 KB Output is correct
86 Correct 53 ms 69368 KB Output is correct
87 Correct 80 ms 72696 KB Output is correct
88 Correct 123 ms 81844 KB Output is correct
89 Correct 380 ms 117624 KB Output is correct
90 Correct 390 ms 116856 KB Output is correct
91 Correct 374 ms 115704 KB Output is correct
92 Correct 157 ms 91872 KB Output is correct
93 Correct 155 ms 91896 KB Output is correct
94 Correct 156 ms 91896 KB Output is correct
95 Correct 168 ms 90864 KB Output is correct
96 Correct 191 ms 91252 KB Output is correct
97 Correct 191 ms 91244 KB Output is correct
98 Correct 193 ms 91120 KB Output is correct
99 Correct 160 ms 90488 KB Output is correct
100 Correct 510 ms 150264 KB Output is correct
101 Correct 618 ms 150648 KB Output is correct
102 Correct 630 ms 150700 KB Output is correct
103 Correct 634 ms 150648 KB Output is correct
104 Correct 186 ms 90488 KB Output is correct
105 Correct 189 ms 90488 KB Output is correct
106 Correct 189 ms 90488 KB Output is correct
107 Correct 279 ms 89868 KB Output is correct
108 Correct 383 ms 115376 KB Output is correct
109 Correct 428 ms 116612 KB Output is correct
110 Correct 155 ms 91256 KB Output is correct
111 Correct 171 ms 90864 KB Output is correct
112 Correct 165 ms 89716 KB Output is correct
113 Correct 161 ms 90104 KB Output is correct
114 Correct 509 ms 150264 KB Output is correct
115 Correct 633 ms 148472 KB Output is correct
116 Correct 181 ms 89848 KB Output is correct
117 Correct 178 ms 91512 KB Output is correct
118 Correct 173 ms 91768 KB Output is correct
119 Correct 176 ms 91896 KB Output is correct
120 Correct 183 ms 91000 KB Output is correct
121 Correct 165 ms 91384 KB Output is correct
122 Correct 173 ms 91516 KB Output is correct
123 Correct 209 ms 92024 KB Output is correct
124 Correct 207 ms 92792 KB Output is correct
125 Correct 206 ms 93304 KB Output is correct
126 Correct 208 ms 91896 KB Output is correct
127 Correct 198 ms 92408 KB Output is correct
128 Correct 220 ms 92872 KB Output is correct