Submission #236403

# Submission time Handle Problem Language Result Execution time Memory
236403 2020-06-02T03:17:56 Z LittleFlowers__ Construction of Highway (JOI18_construction) C++17
100 / 100
447 ms 20972 KB
#include <bits/stdc++.h>
using namespace std;
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0);
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1<<(i-1)))
#define gg exit(0);

const int N=100010,
          INF=1e9;

int n,it;
int uu[N],vv[N];
int a[N],sz[N],d[N],w[N],bit[N],st[N],ed[N],par[N],dep[N],id[N];
vector<int> val;
vector<int> ad[N];

vector<ii> mp[N];

void dfs1(int u){
    sz[u]=1;
    forv(v,ad[u]){
        dep[v]=dep[u]+1;
        par[v]=u;
        dfs1(v);
        sz[u]+=sz[v];
        if(sz[d[u]]<sz[v]) d[u]=v;
    }
}
void dfs2(int u){
    st[u]=++it;
    if(!w[u]) w[u]=u;
    if(d[u])  w[d[u]]=w[u], dfs2(d[u]);
    forv(v,ad[u]) if(v!=d[u]) dfs2(v);
    ed[u]=it;
}

vector<int> rev;

void upd(int i,int x){
    for(;i<=val.size();i+=i&-i){
        bit[i]+=x;
        rev.push_back(i);
    }
}
int que(int i){
    if(i>val.size()) return 0;
    int ret=0;
    for(;i;i-=i&-i) ret+=bit[i];
    return ret;
}

vector<ii> take(int u,int v){ /// take element beginning from v in set_u
    /// mp stores elements from leaf to root
    int sz=dep[v]-dep[w[u]]+1;
    vector<ii> inq;
    vector<ii>&val=mp[u];
    fordec(j,val.size()-1,0){
        if(sz<=val[j].se){
            inq.push_back({val[j].fi,sz});
            break;
        }
        sz-=val[j].se;
        inq.push_back(val[j]);
    }
    reverse(all(inq));
    return inq;
}

int mode;

void modify(int u,int v,int we){
    int sz=dep[v]-dep[w[u]]+1;
    vector<ii>&val=mp[u];
    for(;;){
        if(sz<=val.back().se){
            if(sz<val.back().se){
                val.back()={val.back().fi,val.back().se-sz};
            } else val.pop_back();
            break;
        }
        sz-=val.back().se;
        val.pop_back();
    }
    val.push_back({we,dep[v]-dep[w[u]]+1});

}

main(){
    #define task "vim"
    //freopen(task".inp","r",stdin);
    //freopen(task".out","w",stdout);

    forinc(i,1,n=in) val.push_back(a[i]=in);

    sort(all(val)), val.erase(unique(all(val)),val.end());
    forinc(i,1,n) a[i]=lower_bound(all(val),a[i])-val.begin()+1;

    forinc(i,2,n){
        uu[i]=in, vv[i]=in;
        ad[uu[i]].push_back(vv[i]);
    }

    dfs1(1);
    dfs2(1);


    //forinc(i,1,n) cout<<i<<" "<<d[i]<<"\n";

    forinc(i,1,n) if(!d[i]){
        mp[i].push_back({INF,dep[i]-dep[w[i]]+1});
        int j=i;
        for(;;){
            id[j]=i;
            if(j==w[i]) break;
            j=par[j];
        }
    }
    forinc(i,2,n){
        int p=uu[i], u=vv[i];

        vector<int> path;
        int j=p;
        int tot=0;
        while(j){
                if(0)
            if(i==5){
                cerr<<id[j]<<" "<<j<<"\n";
                forv(t,take(id[j],j))
                cerr<<t.fi<<" "<<t.se<<"\n";
                gg
            }
            forv(t,take(id[j],j)){
                //if(i==5)
                    //cerr<<id[j]<<" "<<j<<"\n";
                     //cerr<<t.fi<<" "<<t.se<<"\n";
                tot+=que(t.fi-1)*t.se;
                //if(i==5) cerr<<tot<<"\n";
                upd(t.fi,t.se);
            }
            j=par[w[j]];
        }
        //if(i==5) gg

        cout<<tot<<"\n";
        //cerr<<tot<<"\n";
        j=u;
        while(j){
            modify(id[j],j,a[u]);
            j=par[w[j]];
        }
        //if(i==4) gg

        forv(j,rev) bit[j]=0;
        rev.clear();
        if(i==3){
            //forv(j,mp[8]) cerr<<j.fi<<" "<<j.se<<"\n";
            //gg
        }
    }

}

Compilation message

construction.cpp: In function 'void upd(int, int)':
construction.cpp:54:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(;i<=val.size();i+=i&-i){
          ~^~~~~~~~~~~~
construction.cpp: In function 'int que(int)':
construction.cpp:60:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(i>val.size()) return 0;
        ~^~~~~~~~~~~
construction.cpp: At global scope:
construction.cpp:102:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 8 ms 4992 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 8 ms 5248 KB Output is correct
13 Correct 8 ms 5120 KB Output is correct
14 Correct 8 ms 5120 KB Output is correct
15 Correct 8 ms 5120 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 9 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 8 ms 5168 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 8 ms 5120 KB Output is correct
26 Correct 8 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 8 ms 5120 KB Output is correct
29 Correct 7 ms 5120 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 8 ms 5120 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 8 ms 5120 KB Output is correct
34 Correct 8 ms 5120 KB Output is correct
35 Correct 8 ms 5120 KB Output is correct
36 Correct 8 ms 5120 KB Output is correct
37 Correct 7 ms 5120 KB Output is correct
38 Correct 8 ms 5120 KB Output is correct
39 Correct 7 ms 5120 KB Output is correct
40 Correct 8 ms 5120 KB Output is correct
41 Correct 8 ms 5120 KB Output is correct
42 Correct 8 ms 5120 KB Output is correct
43 Correct 7 ms 5120 KB Output is correct
44 Correct 7 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 8 ms 4992 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 8 ms 5248 KB Output is correct
13 Correct 8 ms 5120 KB Output is correct
14 Correct 8 ms 5120 KB Output is correct
15 Correct 8 ms 5120 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 9 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 8 ms 5168 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 8 ms 5120 KB Output is correct
26 Correct 8 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 8 ms 5120 KB Output is correct
29 Correct 7 ms 5120 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 8 ms 5120 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 8 ms 5120 KB Output is correct
34 Correct 8 ms 5120 KB Output is correct
35 Correct 8 ms 5120 KB Output is correct
36 Correct 8 ms 5120 KB Output is correct
37 Correct 7 ms 5120 KB Output is correct
38 Correct 8 ms 5120 KB Output is correct
39 Correct 7 ms 5120 KB Output is correct
40 Correct 8 ms 5120 KB Output is correct
41 Correct 8 ms 5120 KB Output is correct
42 Correct 8 ms 5120 KB Output is correct
43 Correct 7 ms 5120 KB Output is correct
44 Correct 7 ms 5120 KB Output is correct
45 Correct 8 ms 5120 KB Output is correct
46 Correct 13 ms 5532 KB Output is correct
47 Correct 13 ms 5504 KB Output is correct
48 Correct 13 ms 5504 KB Output is correct
49 Correct 10 ms 5760 KB Output is correct
50 Correct 11 ms 5632 KB Output is correct
51 Correct 10 ms 5632 KB Output is correct
52 Correct 10 ms 5744 KB Output is correct
53 Correct 10 ms 5760 KB Output is correct
54 Correct 10 ms 5760 KB Output is correct
55 Correct 10 ms 5760 KB Output is correct
56 Correct 10 ms 5632 KB Output is correct
57 Correct 15 ms 5504 KB Output is correct
58 Correct 17 ms 5508 KB Output is correct
59 Correct 16 ms 5632 KB Output is correct
60 Correct 15 ms 5504 KB Output is correct
61 Correct 10 ms 5632 KB Output is correct
62 Correct 11 ms 5632 KB Output is correct
63 Correct 11 ms 5632 KB Output is correct
64 Correct 11 ms 5504 KB Output is correct
65 Correct 15 ms 5504 KB Output is correct
66 Correct 12 ms 5504 KB Output is correct
67 Correct 12 ms 5504 KB Output is correct
68 Correct 10 ms 5632 KB Output is correct
69 Correct 11 ms 5744 KB Output is correct
70 Correct 9 ms 5632 KB Output is correct
71 Correct 9 ms 5504 KB Output is correct
72 Correct 19 ms 5504 KB Output is correct
73 Correct 15 ms 5504 KB Output is correct
74 Correct 10 ms 5632 KB Output is correct
75 Correct 11 ms 5632 KB Output is correct
76 Correct 11 ms 5504 KB Output is correct
77 Correct 11 ms 5504 KB Output is correct
78 Correct 9 ms 5504 KB Output is correct
79 Correct 10 ms 5504 KB Output is correct
80 Correct 10 ms 5504 KB Output is correct
81 Correct 11 ms 5504 KB Output is correct
82 Correct 11 ms 5504 KB Output is correct
83 Correct 11 ms 5504 KB Output is correct
84 Correct 10 ms 5504 KB Output is correct
85 Correct 10 ms 5504 KB Output is correct
86 Correct 10 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 8 ms 4992 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 8 ms 5248 KB Output is correct
13 Correct 8 ms 5120 KB Output is correct
14 Correct 8 ms 5120 KB Output is correct
15 Correct 8 ms 5120 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 9 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 8 ms 5168 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 8 ms 5120 KB Output is correct
26 Correct 8 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 8 ms 5120 KB Output is correct
29 Correct 7 ms 5120 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 8 ms 5120 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 8 ms 5120 KB Output is correct
34 Correct 8 ms 5120 KB Output is correct
35 Correct 8 ms 5120 KB Output is correct
36 Correct 8 ms 5120 KB Output is correct
37 Correct 7 ms 5120 KB Output is correct
38 Correct 8 ms 5120 KB Output is correct
39 Correct 7 ms 5120 KB Output is correct
40 Correct 8 ms 5120 KB Output is correct
41 Correct 8 ms 5120 KB Output is correct
42 Correct 8 ms 5120 KB Output is correct
43 Correct 7 ms 5120 KB Output is correct
44 Correct 7 ms 5120 KB Output is correct
45 Correct 8 ms 5120 KB Output is correct
46 Correct 13 ms 5532 KB Output is correct
47 Correct 13 ms 5504 KB Output is correct
48 Correct 13 ms 5504 KB Output is correct
49 Correct 10 ms 5760 KB Output is correct
50 Correct 11 ms 5632 KB Output is correct
51 Correct 10 ms 5632 KB Output is correct
52 Correct 10 ms 5744 KB Output is correct
53 Correct 10 ms 5760 KB Output is correct
54 Correct 10 ms 5760 KB Output is correct
55 Correct 10 ms 5760 KB Output is correct
56 Correct 10 ms 5632 KB Output is correct
57 Correct 15 ms 5504 KB Output is correct
58 Correct 17 ms 5508 KB Output is correct
59 Correct 16 ms 5632 KB Output is correct
60 Correct 15 ms 5504 KB Output is correct
61 Correct 10 ms 5632 KB Output is correct
62 Correct 11 ms 5632 KB Output is correct
63 Correct 11 ms 5632 KB Output is correct
64 Correct 11 ms 5504 KB Output is correct
65 Correct 15 ms 5504 KB Output is correct
66 Correct 12 ms 5504 KB Output is correct
67 Correct 12 ms 5504 KB Output is correct
68 Correct 10 ms 5632 KB Output is correct
69 Correct 11 ms 5744 KB Output is correct
70 Correct 9 ms 5632 KB Output is correct
71 Correct 9 ms 5504 KB Output is correct
72 Correct 19 ms 5504 KB Output is correct
73 Correct 15 ms 5504 KB Output is correct
74 Correct 10 ms 5632 KB Output is correct
75 Correct 11 ms 5632 KB Output is correct
76 Correct 11 ms 5504 KB Output is correct
77 Correct 11 ms 5504 KB Output is correct
78 Correct 9 ms 5504 KB Output is correct
79 Correct 10 ms 5504 KB Output is correct
80 Correct 10 ms 5504 KB Output is correct
81 Correct 11 ms 5504 KB Output is correct
82 Correct 11 ms 5504 KB Output is correct
83 Correct 11 ms 5504 KB Output is correct
84 Correct 10 ms 5504 KB Output is correct
85 Correct 10 ms 5504 KB Output is correct
86 Correct 10 ms 5504 KB Output is correct
87 Correct 24 ms 6144 KB Output is correct
88 Correct 65 ms 8440 KB Output is correct
89 Correct 280 ms 16180 KB Output is correct
90 Correct 263 ms 16112 KB Output is correct
91 Correct 259 ms 15988 KB Output is correct
92 Correct 106 ms 20336 KB Output is correct
93 Correct 108 ms 20332 KB Output is correct
94 Correct 110 ms 20208 KB Output is correct
95 Correct 117 ms 20588 KB Output is correct
96 Correct 125 ms 20972 KB Output is correct
97 Correct 125 ms 20932 KB Output is correct
98 Correct 117 ms 20844 KB Output is correct
99 Correct 123 ms 18160 KB Output is correct
100 Correct 410 ms 16112 KB Output is correct
101 Correct 438 ms 16368 KB Output is correct
102 Correct 438 ms 16368 KB Output is correct
103 Correct 447 ms 16496 KB Output is correct
104 Correct 142 ms 18032 KB Output is correct
105 Correct 129 ms 18032 KB Output is correct
106 Correct 136 ms 18028 KB Output is correct
107 Correct 182 ms 14960 KB Output is correct
108 Correct 218 ms 14960 KB Output is correct
109 Correct 253 ms 15432 KB Output is correct
110 Correct 89 ms 19312 KB Output is correct
111 Correct 120 ms 20460 KB Output is correct
112 Correct 102 ms 18800 KB Output is correct
113 Correct 96 ms 17012 KB Output is correct
114 Correct 409 ms 16240 KB Output is correct
115 Correct 334 ms 15472 KB Output is correct
116 Correct 118 ms 17008 KB Output is correct
117 Correct 123 ms 16496 KB Output is correct
118 Correct 126 ms 16112 KB Output is correct
119 Correct 138 ms 15984 KB Output is correct
120 Correct 107 ms 15728 KB Output is correct
121 Correct 102 ms 15472 KB Output is correct
122 Correct 106 ms 15216 KB Output is correct
123 Correct 145 ms 16576 KB Output is correct
124 Correct 142 ms 16240 KB Output is correct
125 Correct 142 ms 15984 KB Output is correct
126 Correct 121 ms 15856 KB Output is correct
127 Correct 116 ms 15600 KB Output is correct
128 Correct 126 ms 15316 KB Output is correct