답안 #373539

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
373539 2021-03-05T03:23:35 Z i_am_noob Construction of Highway (JOI18_construction) C++14
100 / 100
1018 ms 43500 KB
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,avx512f")
#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;

#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
#define inf 1010000000
//#define inf 4000000000000000000
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {static int cnt=0;cerr << x << endl;cnt++;if(cnt>1000) exit(0);}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 826
#endif

inline char readchar(){
    const int maxn=1000000;
    static char buf[maxn],*p=buf,*q=buf;
    if(p==q&&(q=(p=buf)+fread(buf,1,maxn,stdin))==buf) return EOF;
    else return *p++;
}
inline int readint(){
    int c=readchar(),x=0,neg=0;
    while((c<'0'||c>'9')&&c!='-'&&c!=EOF) c=readchar();
    if(c=='-') neg=1,c=readchar();
    while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^'0'),c=readchar();
    return neg?-x:x;
}

const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod;
const int maxn=100005;
//i_am_noob
int n,a[maxn],e[maxn][2],fa[maxn],dep[maxn],siz[maxn],tour[maxn],cur,val[2*maxn][18],id[maxn],to[maxn],head[maxn],bit[maxn],tail[4*maxn],tag[4*maxn];
vector<int> vec,adj[maxn],child[maxn];
vector<pii> opers;

void dfs(int u, int par){
    fa[u]=par;
    dep[u]=par==-1?0:dep[par]+1;
    siz[u]=1;
    tour[u]=cur;
    val[cur++][0]=u;
    for(auto v: adj[u]) if(v!=par){
        dfs(v,u);
        val[cur++][0]=u;
        siz[u]+=siz[v];
    }
}

void dfs2(int u, int par, int h){
    to[u]=-1,head[u]=h,id[u]=cur++;
    if(par>=0) child[par].pb(u);
    for(auto v: adj[u]) if(v!=par) if(to[u]==-1||siz[v]>siz[to[u]]) to[u]=v;
    if(to[u]>=0) dfs2(to[u],u,h);
    for(auto v: adj[u]) if(v!=par&&v!=to[u]) dfs2(v,u,v);
}

bool cmp(int i, int j){return dep[i]<dep[j];}

int querylca(int u, int v){
    int l=tour[u],r=tour[v];
    if(l>r) swap(l,r);
    int x=__lg(r-l+1);
    return min(val[l][x],val[r-pow2(x)+1][x],cmp);
}

void modifybit(int p, int x){for(p++; p<maxn; p+=p&-p) bit[p]+=x;}

int querybit(int l, int r){
    int res=0;
    for(r++; r; r-=r&-r) res+=bit[r];
    for(; l; l-=l&-l) res-=bit[l];
    return res;
}

void push(int k, int l, int r){
    if(l!=r){
        if(tag[k]!=-1){
            tag[k<<1]=tag[k],tag[k<<1|1]=tag[k];
            tail[k<<1]=tag[k],tail[k<<1|1]=tag[k];
        }
    }
    tag[k]=-1;
}

void build(int k, int l, int r){
    if(l==r){
        tail[k]=0;
        return;
    }
    int mid=l+r>>1;
    build(k<<1,l,mid),build(k<<1|1,mid+1,r);
}

void modifyseg(int k, int l, int r, int ql, int qr, int x){
    if(l>qr||r<ql) return;
    if(ql<=l&&qr>=r){
        tail[k]=tag[k]=x;
        return;
    }
    int mid=l+r>>1;
    push(k,l,r);
    modifyseg(k<<1,l,mid,ql,qr,x),modifyseg(k<<1|1,mid+1,r,ql,qr,x);
}

int queryseg(int k, int l, int r, int p){
    if(l==r) return tail[k];
    int mid=l+r>>1;
    push(k,l,r);
    if(p<=mid) return queryseg(k<<1,l,mid,p);
    return queryseg(k<<1|1,mid+1,r,p);
}

void modify(int u, int x){
    int v;
    while(1){
        v=head[u];
        modifyseg(1,0,maxn-1,id[v],id[u],x);
        if(v==0) break;
        u=fa[v];
    }
}

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    #ifdef i_am_noob
    freopen("input1.txt","r",stdin);
    freopen("output1.txt","w",stdout);
    freopen("output2.txt","w",stderr);
    #endif
    n=readint();
    rep(n) a[i]=readint();
    rep(n) vec.pb(a[i]);
    sort(all(vec));
    vec.resize(unique(all(vec))-vec.begin());
    rep(n) a[i]=lower_bound(all(vec),a[i])-vec.begin();
    rep(n-1) e[i][0]=readint(),e[i][1]=readint();
    rep(n-1) e[i][0]--,e[i][1]--;
    rep(n-1) adj[e[i][0]].pb(e[i][1]),adj[e[i][1]].pb(e[i][0]);
    cur=0;
    dfs(0,-1);
    rep2(j,1,18) rep(2*n-pow2(j)) val[i][j]=min(val[i][j-1],val[i+pow2(j-1)][j-1],cmp);
    cur=0;
    dfs2(0,-1,0);
    rep(4*maxn) tag[i]=-1;
    build(1,0,maxn-1);
    rep(n-1){
        int u=e[i][0],v=e[i][1],x=0,y,z,t,tot=0;
        ll ans=0;
        opers.clear();
        bug(u,v);
        while(1){
            y=queryseg(1,0,maxn-1,id[x]);
            z=querylca(u,y);
            bug(x,y,z);
            ans+=1ll*(tot-querybit(0,a[y]))*(dep[z]-dep[x]+1);
            if(z==u) break;
            modifybit(a[y],dep[z]-dep[x]+1);
            opers.pb({a[y],-dep[z]+dep[x]-1});
            tot+=dep[z]-dep[x]+1;
            x=*--upper_bound(all(child[z]),u,[](int i, int j){return id[i]<id[j];});
        }
        for(auto& [p,q]: opers) modifybit(p,q);
        modify(v,v);
        rep1(j,n) bug(j,queryseg(1,0,maxn-1,id[j]));
        cout << ans << "\n";
    }
    return 0;
}

Compilation message

construction.cpp: In function 'void build(int, int, int)':
construction.cpp:106:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  106 |     int mid=l+r>>1;
      |             ~^~
construction.cpp: In function 'void modifyseg(int, int, int, int, int, int)':
construction.cpp:116:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  116 |     int mid=l+r>>1;
      |             ~^~
construction.cpp: In function 'int queryseg(int, int, int, int)':
construction.cpp:123:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  123 |     int mid=l+r>>1;
      |             ~^~
construction.cpp: In function 'int main()':
construction.cpp:27:18: warning: statement has no effect [-Wunused-value]
   27 | #define bug(...) 826
      |                  ^~~
construction.cpp:166:9: note: in expansion of macro 'bug'
  166 |         bug(u,v);
      |         ^~~
construction.cpp:27:18: warning: statement has no effect [-Wunused-value]
   27 | #define bug(...) 826
      |                  ^~~
construction.cpp:170:13: note: in expansion of macro 'bug'
  170 |             bug(x,y,z);
      |             ^~~
construction.cpp:178:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  178 |         for(auto& [p,q]: opers) modifybit(p,q);
      |                   ^
construction.cpp:27:18: warning: statement has no effect [-Wunused-value]
   27 | #define bug(...) 826
      |                  ^~~
construction.cpp:180:19: note: in expansion of macro 'bug'
  180 |         rep1(j,n) bug(j,queryseg(1,0,maxn-1,id[j]));
      |                   ^~~
construction.cpp:163:41: warning: unused variable 't' [-Wunused-variable]
  163 |         int u=e[i][0],v=e[i][1],x=0,y,z,t,tot=0;
      |                                         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 6 ms 7532 KB Output is correct
3 Correct 6 ms 7532 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 7 ms 7660 KB Output is correct
6 Correct 7 ms 7660 KB Output is correct
7 Correct 6 ms 7660 KB Output is correct
8 Correct 6 ms 7660 KB Output is correct
9 Correct 6 ms 7660 KB Output is correct
10 Correct 6 ms 7660 KB Output is correct
11 Correct 6 ms 7660 KB Output is correct
12 Correct 6 ms 7680 KB Output is correct
13 Correct 6 ms 7660 KB Output is correct
14 Correct 6 ms 7660 KB Output is correct
15 Correct 7 ms 7660 KB Output is correct
16 Correct 7 ms 7660 KB Output is correct
17 Correct 7 ms 7660 KB Output is correct
18 Correct 7 ms 7660 KB Output is correct
19 Correct 7 ms 7660 KB Output is correct
20 Correct 6 ms 7660 KB Output is correct
21 Correct 6 ms 7660 KB Output is correct
22 Correct 7 ms 7660 KB Output is correct
23 Correct 7 ms 7660 KB Output is correct
24 Correct 7 ms 7660 KB Output is correct
25 Correct 7 ms 7660 KB Output is correct
26 Correct 6 ms 7660 KB Output is correct
27 Correct 6 ms 7660 KB Output is correct
28 Correct 6 ms 7660 KB Output is correct
29 Correct 6 ms 7660 KB Output is correct
30 Correct 7 ms 7660 KB Output is correct
31 Correct 7 ms 7680 KB Output is correct
32 Correct 6 ms 7660 KB Output is correct
33 Correct 6 ms 7660 KB Output is correct
34 Correct 6 ms 7660 KB Output is correct
35 Correct 6 ms 7660 KB Output is correct
36 Correct 6 ms 7660 KB Output is correct
37 Correct 6 ms 7660 KB Output is correct
38 Correct 6 ms 7660 KB Output is correct
39 Correct 8 ms 7660 KB Output is correct
40 Correct 6 ms 7660 KB Output is correct
41 Correct 7 ms 7660 KB Output is correct
42 Correct 6 ms 7660 KB Output is correct
43 Correct 6 ms 7788 KB Output is correct
44 Correct 6 ms 7660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 6 ms 7532 KB Output is correct
3 Correct 6 ms 7532 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 7 ms 7660 KB Output is correct
6 Correct 7 ms 7660 KB Output is correct
7 Correct 6 ms 7660 KB Output is correct
8 Correct 6 ms 7660 KB Output is correct
9 Correct 6 ms 7660 KB Output is correct
10 Correct 6 ms 7660 KB Output is correct
11 Correct 6 ms 7660 KB Output is correct
12 Correct 6 ms 7680 KB Output is correct
13 Correct 6 ms 7660 KB Output is correct
14 Correct 6 ms 7660 KB Output is correct
15 Correct 7 ms 7660 KB Output is correct
16 Correct 7 ms 7660 KB Output is correct
17 Correct 7 ms 7660 KB Output is correct
18 Correct 7 ms 7660 KB Output is correct
19 Correct 7 ms 7660 KB Output is correct
20 Correct 6 ms 7660 KB Output is correct
21 Correct 6 ms 7660 KB Output is correct
22 Correct 7 ms 7660 KB Output is correct
23 Correct 7 ms 7660 KB Output is correct
24 Correct 7 ms 7660 KB Output is correct
25 Correct 7 ms 7660 KB Output is correct
26 Correct 6 ms 7660 KB Output is correct
27 Correct 6 ms 7660 KB Output is correct
28 Correct 6 ms 7660 KB Output is correct
29 Correct 6 ms 7660 KB Output is correct
30 Correct 7 ms 7660 KB Output is correct
31 Correct 7 ms 7680 KB Output is correct
32 Correct 6 ms 7660 KB Output is correct
33 Correct 6 ms 7660 KB Output is correct
34 Correct 6 ms 7660 KB Output is correct
35 Correct 6 ms 7660 KB Output is correct
36 Correct 6 ms 7660 KB Output is correct
37 Correct 6 ms 7660 KB Output is correct
38 Correct 6 ms 7660 KB Output is correct
39 Correct 8 ms 7660 KB Output is correct
40 Correct 6 ms 7660 KB Output is correct
41 Correct 7 ms 7660 KB Output is correct
42 Correct 6 ms 7660 KB Output is correct
43 Correct 6 ms 7788 KB Output is correct
44 Correct 6 ms 7660 KB Output is correct
45 Correct 7 ms 7788 KB Output is correct
46 Correct 14 ms 8556 KB Output is correct
47 Correct 15 ms 8556 KB Output is correct
48 Correct 15 ms 8556 KB Output is correct
49 Correct 10 ms 8940 KB Output is correct
50 Correct 10 ms 8940 KB Output is correct
51 Correct 10 ms 8940 KB Output is correct
52 Correct 10 ms 8684 KB Output is correct
53 Correct 11 ms 8812 KB Output is correct
54 Correct 10 ms 8812 KB Output is correct
55 Correct 10 ms 8812 KB Output is correct
56 Correct 12 ms 8812 KB Output is correct
57 Correct 20 ms 8556 KB Output is correct
58 Correct 20 ms 8556 KB Output is correct
59 Correct 21 ms 8576 KB Output is correct
60 Correct 20 ms 8556 KB Output is correct
61 Correct 11 ms 8812 KB Output is correct
62 Correct 11 ms 8812 KB Output is correct
63 Correct 11 ms 8812 KB Output is correct
64 Correct 16 ms 8556 KB Output is correct
65 Correct 14 ms 8556 KB Output is correct
66 Correct 14 ms 8556 KB Output is correct
67 Correct 14 ms 8556 KB Output is correct
68 Correct 9 ms 8940 KB Output is correct
69 Correct 10 ms 8684 KB Output is correct
70 Correct 10 ms 8684 KB Output is correct
71 Correct 10 ms 8684 KB Output is correct
72 Correct 19 ms 8556 KB Output is correct
73 Correct 20 ms 8556 KB Output is correct
74 Correct 11 ms 8684 KB Output is correct
75 Correct 11 ms 8684 KB Output is correct
76 Correct 11 ms 8684 KB Output is correct
77 Correct 11 ms 8684 KB Output is correct
78 Correct 10 ms 8556 KB Output is correct
79 Correct 10 ms 8556 KB Output is correct
80 Correct 10 ms 8556 KB Output is correct
81 Correct 11 ms 8684 KB Output is correct
82 Correct 14 ms 8684 KB Output is correct
83 Correct 11 ms 8684 KB Output is correct
84 Correct 11 ms 8556 KB Output is correct
85 Correct 11 ms 8556 KB Output is correct
86 Correct 11 ms 8556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 6 ms 7532 KB Output is correct
3 Correct 6 ms 7532 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 7 ms 7660 KB Output is correct
6 Correct 7 ms 7660 KB Output is correct
7 Correct 6 ms 7660 KB Output is correct
8 Correct 6 ms 7660 KB Output is correct
9 Correct 6 ms 7660 KB Output is correct
10 Correct 6 ms 7660 KB Output is correct
11 Correct 6 ms 7660 KB Output is correct
12 Correct 6 ms 7680 KB Output is correct
13 Correct 6 ms 7660 KB Output is correct
14 Correct 6 ms 7660 KB Output is correct
15 Correct 7 ms 7660 KB Output is correct
16 Correct 7 ms 7660 KB Output is correct
17 Correct 7 ms 7660 KB Output is correct
18 Correct 7 ms 7660 KB Output is correct
19 Correct 7 ms 7660 KB Output is correct
20 Correct 6 ms 7660 KB Output is correct
21 Correct 6 ms 7660 KB Output is correct
22 Correct 7 ms 7660 KB Output is correct
23 Correct 7 ms 7660 KB Output is correct
24 Correct 7 ms 7660 KB Output is correct
25 Correct 7 ms 7660 KB Output is correct
26 Correct 6 ms 7660 KB Output is correct
27 Correct 6 ms 7660 KB Output is correct
28 Correct 6 ms 7660 KB Output is correct
29 Correct 6 ms 7660 KB Output is correct
30 Correct 7 ms 7660 KB Output is correct
31 Correct 7 ms 7680 KB Output is correct
32 Correct 6 ms 7660 KB Output is correct
33 Correct 6 ms 7660 KB Output is correct
34 Correct 6 ms 7660 KB Output is correct
35 Correct 6 ms 7660 KB Output is correct
36 Correct 6 ms 7660 KB Output is correct
37 Correct 6 ms 7660 KB Output is correct
38 Correct 6 ms 7660 KB Output is correct
39 Correct 8 ms 7660 KB Output is correct
40 Correct 6 ms 7660 KB Output is correct
41 Correct 7 ms 7660 KB Output is correct
42 Correct 6 ms 7660 KB Output is correct
43 Correct 6 ms 7788 KB Output is correct
44 Correct 6 ms 7660 KB Output is correct
45 Correct 7 ms 7788 KB Output is correct
46 Correct 14 ms 8556 KB Output is correct
47 Correct 15 ms 8556 KB Output is correct
48 Correct 15 ms 8556 KB Output is correct
49 Correct 10 ms 8940 KB Output is correct
50 Correct 10 ms 8940 KB Output is correct
51 Correct 10 ms 8940 KB Output is correct
52 Correct 10 ms 8684 KB Output is correct
53 Correct 11 ms 8812 KB Output is correct
54 Correct 10 ms 8812 KB Output is correct
55 Correct 10 ms 8812 KB Output is correct
56 Correct 12 ms 8812 KB Output is correct
57 Correct 20 ms 8556 KB Output is correct
58 Correct 20 ms 8556 KB Output is correct
59 Correct 21 ms 8576 KB Output is correct
60 Correct 20 ms 8556 KB Output is correct
61 Correct 11 ms 8812 KB Output is correct
62 Correct 11 ms 8812 KB Output is correct
63 Correct 11 ms 8812 KB Output is correct
64 Correct 16 ms 8556 KB Output is correct
65 Correct 14 ms 8556 KB Output is correct
66 Correct 14 ms 8556 KB Output is correct
67 Correct 14 ms 8556 KB Output is correct
68 Correct 9 ms 8940 KB Output is correct
69 Correct 10 ms 8684 KB Output is correct
70 Correct 10 ms 8684 KB Output is correct
71 Correct 10 ms 8684 KB Output is correct
72 Correct 19 ms 8556 KB Output is correct
73 Correct 20 ms 8556 KB Output is correct
74 Correct 11 ms 8684 KB Output is correct
75 Correct 11 ms 8684 KB Output is correct
76 Correct 11 ms 8684 KB Output is correct
77 Correct 11 ms 8684 KB Output is correct
78 Correct 10 ms 8556 KB Output is correct
79 Correct 10 ms 8556 KB Output is correct
80 Correct 10 ms 8556 KB Output is correct
81 Correct 11 ms 8684 KB Output is correct
82 Correct 14 ms 8684 KB Output is correct
83 Correct 11 ms 8684 KB Output is correct
84 Correct 11 ms 8556 KB Output is correct
85 Correct 11 ms 8556 KB Output is correct
86 Correct 11 ms 8556 KB Output is correct
87 Correct 34 ms 10292 KB Output is correct
88 Correct 110 ms 15980 KB Output is correct
89 Correct 534 ms 34668 KB Output is correct
90 Correct 555 ms 34668 KB Output is correct
91 Correct 525 ms 34924 KB Output is correct
92 Correct 165 ms 43500 KB Output is correct
93 Correct 163 ms 43500 KB Output is correct
94 Correct 171 ms 43372 KB Output is correct
95 Correct 179 ms 38380 KB Output is correct
96 Correct 185 ms 39008 KB Output is correct
97 Correct 183 ms 39020 KB Output is correct
98 Correct 194 ms 39020 KB Output is correct
99 Correct 200 ms 38380 KB Output is correct
100 Correct 971 ms 34796 KB Output is correct
101 Correct 1004 ms 35300 KB Output is correct
102 Correct 1018 ms 35232 KB Output is correct
103 Correct 1014 ms 35180 KB Output is correct
104 Correct 252 ms 38380 KB Output is correct
105 Correct 261 ms 38380 KB Output is correct
106 Correct 237 ms 38380 KB Output is correct
107 Correct 488 ms 33516 KB Output is correct
108 Correct 494 ms 33644 KB Output is correct
109 Correct 545 ms 34156 KB Output is correct
110 Correct 154 ms 42988 KB Output is correct
111 Correct 195 ms 38400 KB Output is correct
112 Correct 165 ms 37996 KB Output is correct
113 Correct 174 ms 37384 KB Output is correct
114 Correct 933 ms 34796 KB Output is correct
115 Correct 935 ms 34156 KB Output is correct
116 Correct 216 ms 37356 KB Output is correct
117 Correct 193 ms 36076 KB Output is correct
118 Correct 195 ms 35436 KB Output is correct
119 Correct 199 ms 35052 KB Output is correct
120 Correct 191 ms 35436 KB Output is correct
121 Correct 182 ms 34796 KB Output is correct
122 Correct 190 ms 34412 KB Output is correct
123 Correct 258 ms 36204 KB Output is correct
124 Correct 259 ms 35764 KB Output is correct
125 Correct 271 ms 35180 KB Output is correct
126 Correct 242 ms 35564 KB Output is correct
127 Correct 262 ms 34796 KB Output is correct
128 Correct 246 ms 34540 KB Output is correct