Submission #931386

# Submission time Handle Problem Language Result Execution time Memory
931386 2024-02-21T17:24:27 Z alexander707070 Construction of Highway (JOI18_construction) C++14
100 / 100
775 ms 33220 KB
#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;

const int inf=1e9+7;

int n,c[MAXN],a[MAXN],b[MAXN],sz[MAXN],heavy[MAXN],parent[MAXN],cnt;
int pos[MAXN],head[MAXN],tt,lazy[4*MAXN],dep[MAXN];
pair<int,int> tree[4*MAXN],curr;

vector<int> v[MAXN],s;
vector< pair<int,int> > w;
map<int,int> mp;

void dfs(int x,int p,int d){

    parent[x]=p; dep[x]=d;
    sz[x]=1;

    for(int i=0;i<v[x].size();i++){
        if(v[x][i]==p)continue;
        dfs(v[x][i],x,d+1);
        if(sz[v[x][i]]>sz[heavy[x]])heavy[x]=v[x][i];

        sz[x]+=sz[v[x][i]];
    }
}

void decompose(int x,int p,int h){
    tt++; pos[x]=tt; head[x]=h;

    if(heavy[x]!=0){
        decompose(heavy[x],x,h);
    }

    for(int i=0;i<v[x].size();i++){
        if(v[x][i]==p or v[x][i]==heavy[x])continue;
        decompose(v[x][i],x,v[x][i]);
    }
}

void psh(int v){
    if(lazy[v]==0)return;

    tree[2*v].first=tree[2*v].second=lazy[v];
    tree[2*v+1].first=tree[2*v+1].second=lazy[v];

    lazy[2*v]=lazy[2*v+1]=lazy[v];

    lazy[v]=0;
}

pair<int,int> combine(pair<int,int> fr,pair<int,int> sc){
    return {min(fr.first,sc.first),max(fr.second,sc.second)};
}

void update(int v,int l,int r,int ll,int rr,int val){
    if(ll>rr)return;
    if(l==ll and r==rr){
        tree[v].first=tree[v].second=val;
        lazy[v]=val;
    }else{
        psh(v);
        int tt=(l+r)/2;
        update(2*v,l,tt,ll,min(tt,rr),val);
        update(2*v+1,tt+1,r,max(tt+1,ll),rr,val);

        tree[v]=combine(tree[2*v],tree[2*v+1]);
    }
}

pair<int,int> info(int v,int l,int r,int ll,int rr){
    if(ll>rr)return{inf,0};

    if(l==ll and r==rr){
        return tree[v];
    }else{
        psh(v);
        int tt=(l+r)/2;
        return combine( info(2*v,l,tt,ll,min(tt,rr)) , info(2*v+1,tt+1,r,max(tt+1,ll),rr) );
    }
}

void upgrade(int x,int val){
    while(x!=0){
        update(1,1,n,pos[head[x]],pos[x],val);
        x=parent[head[x]];
    }
}

void add_segments(int l,int r){

    while(r>=l){
        int ll=l-1,rr=r,mid;
        while(ll+1<rr){
            mid=(ll+rr)/2;

            curr=info(1,1,n,mid,r);
            if(curr.first==curr.second){
                rr=mid;
            }else{
                ll=mid;
            }
        }

        curr=info(1,1,n,rr,r);
        w.push_back({curr.first,r-rr+1});
        r=rr-1;
    }
}

int fenwick[MAXN];

void add(int pos,int val){
    while(pos<=cnt){
        fenwick[pos]+=val;
        pos+=(pos & (-pos));
    }
}

int getsum(int pos){
    int res=0;
    while(pos>=1){
        res+=fenwick[pos];
        pos-=(pos & (-pos));
    }
    return res;
}

long long query(int x){
    long long ans=0;

    w.clear();

    while(x!=0){
        add_segments(pos[head[x]],pos[x]);
        x=parent[head[x]];
    }

    for(int i=0;i<w.size();i++){
        ans+=(long long) getsum(w[i].first-1)*w[i].second;
        add(w[i].first,w[i].second);
    }

    for(int i=0;i<w.size();i++){
        add(w[i].first,-w[i].second);
    }

    return ans;
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>c[i];
        s.push_back(c[i]);
    }

    sort(s.begin(),s.end());
    for(int i=0;i<s.size();i++){
        if(i==0 or s[i]!=s[i-1])cnt++;
        mp[s[i]]=cnt;
    }

    for(int i=1;i<=n;i++)c[i]=mp[c[i]];

    for(int i=1;i<=n-1;i++){
        cin>>a[i]>>b[i];
        v[a[i]].push_back(b[i]);
        v[b[i]].push_back(a[i]);
    }

    dfs(1,0,1);
    decompose(1,0,1);

    for(int i=1;i<=n;i++){
        update(1,1,n,pos[i],pos[i],c[i]);
    }

    for(int i=1;i<=n-1;i++){
        cout<<query(a[i])<<"\n";
        upgrade(b[i],c[b[i]]);
    }

    return 0;
}

Compilation message

construction.cpp: In function 'void dfs(int, int, int)':
construction.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
construction.cpp: In function 'void decompose(int, int, int)':
construction.cpp:36:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
construction.cpp: In function 'long long int query(int)':
construction.cpp:140:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  140 |     for(int i=0;i<w.size();i++){
      |                 ~^~~~~~~~~
construction.cpp:145:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |     for(int i=0;i<w.size();i++){
      |                 ~^~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:165:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |     for(int i=0;i<s.size();i++){
      |                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 12772 KB Output is correct
6 Correct 3 ms 12632 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 3 ms 12636 KB Output is correct
11 Correct 4 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12632 KB Output is correct
14 Correct 3 ms 12636 KB Output is correct
15 Correct 3 ms 12728 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 4 ms 12636 KB Output is correct
18 Correct 3 ms 12636 KB Output is correct
19 Correct 4 ms 12636 KB Output is correct
20 Correct 3 ms 12636 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12636 KB Output is correct
23 Correct 3 ms 12636 KB Output is correct
24 Correct 3 ms 12632 KB Output is correct
25 Correct 3 ms 12636 KB Output is correct
26 Correct 3 ms 12632 KB Output is correct
27 Correct 3 ms 12636 KB Output is correct
28 Correct 3 ms 12636 KB Output is correct
29 Correct 3 ms 12636 KB Output is correct
30 Correct 3 ms 12636 KB Output is correct
31 Correct 3 ms 12636 KB Output is correct
32 Correct 3 ms 12636 KB Output is correct
33 Correct 4 ms 12636 KB Output is correct
34 Correct 3 ms 12636 KB Output is correct
35 Correct 3 ms 12632 KB Output is correct
36 Correct 3 ms 12636 KB Output is correct
37 Correct 3 ms 12636 KB Output is correct
38 Correct 3 ms 12636 KB Output is correct
39 Correct 3 ms 12636 KB Output is correct
40 Correct 4 ms 12636 KB Output is correct
41 Correct 3 ms 12632 KB Output is correct
42 Correct 3 ms 12636 KB Output is correct
43 Correct 3 ms 12636 KB Output is correct
44 Correct 4 ms 12632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 12772 KB Output is correct
6 Correct 3 ms 12632 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 3 ms 12636 KB Output is correct
11 Correct 4 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12632 KB Output is correct
14 Correct 3 ms 12636 KB Output is correct
15 Correct 3 ms 12728 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 4 ms 12636 KB Output is correct
18 Correct 3 ms 12636 KB Output is correct
19 Correct 4 ms 12636 KB Output is correct
20 Correct 3 ms 12636 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12636 KB Output is correct
23 Correct 3 ms 12636 KB Output is correct
24 Correct 3 ms 12632 KB Output is correct
25 Correct 3 ms 12636 KB Output is correct
26 Correct 3 ms 12632 KB Output is correct
27 Correct 3 ms 12636 KB Output is correct
28 Correct 3 ms 12636 KB Output is correct
29 Correct 3 ms 12636 KB Output is correct
30 Correct 3 ms 12636 KB Output is correct
31 Correct 3 ms 12636 KB Output is correct
32 Correct 3 ms 12636 KB Output is correct
33 Correct 4 ms 12636 KB Output is correct
34 Correct 3 ms 12636 KB Output is correct
35 Correct 3 ms 12632 KB Output is correct
36 Correct 3 ms 12636 KB Output is correct
37 Correct 3 ms 12636 KB Output is correct
38 Correct 3 ms 12636 KB Output is correct
39 Correct 3 ms 12636 KB Output is correct
40 Correct 4 ms 12636 KB Output is correct
41 Correct 3 ms 12632 KB Output is correct
42 Correct 3 ms 12636 KB Output is correct
43 Correct 3 ms 12636 KB Output is correct
44 Correct 4 ms 12632 KB Output is correct
45 Correct 4 ms 12888 KB Output is correct
46 Correct 14 ms 13148 KB Output is correct
47 Correct 12 ms 13344 KB Output is correct
48 Correct 12 ms 13148 KB Output is correct
49 Correct 15 ms 13404 KB Output is correct
50 Correct 13 ms 13400 KB Output is correct
51 Correct 13 ms 13404 KB Output is correct
52 Correct 14 ms 13404 KB Output is correct
53 Correct 14 ms 13404 KB Output is correct
54 Correct 15 ms 13404 KB Output is correct
55 Correct 14 ms 13400 KB Output is correct
56 Correct 14 ms 13148 KB Output is correct
57 Correct 16 ms 13148 KB Output is correct
58 Correct 16 ms 13148 KB Output is correct
59 Correct 16 ms 13148 KB Output is correct
60 Correct 16 ms 13148 KB Output is correct
61 Correct 17 ms 13148 KB Output is correct
62 Correct 17 ms 13148 KB Output is correct
63 Correct 17 ms 13148 KB Output is correct
64 Correct 12 ms 12888 KB Output is correct
65 Correct 15 ms 13024 KB Output is correct
66 Correct 11 ms 12892 KB Output is correct
67 Correct 11 ms 12892 KB Output is correct
68 Correct 12 ms 13144 KB Output is correct
69 Correct 15 ms 13404 KB Output is correct
70 Correct 18 ms 13148 KB Output is correct
71 Correct 15 ms 13192 KB Output is correct
72 Correct 16 ms 13148 KB Output is correct
73 Correct 15 ms 12892 KB Output is correct
74 Correct 15 ms 13148 KB Output is correct
75 Correct 14 ms 13148 KB Output is correct
76 Correct 14 ms 13272 KB Output is correct
77 Correct 14 ms 13144 KB Output is correct
78 Correct 14 ms 12892 KB Output is correct
79 Correct 13 ms 13068 KB Output is correct
80 Correct 13 ms 13056 KB Output is correct
81 Correct 15 ms 13148 KB Output is correct
82 Correct 16 ms 13244 KB Output is correct
83 Correct 14 ms 13148 KB Output is correct
84 Correct 14 ms 12888 KB Output is correct
85 Correct 15 ms 12892 KB Output is correct
86 Correct 13 ms 12888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 12772 KB Output is correct
6 Correct 3 ms 12632 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 3 ms 12636 KB Output is correct
11 Correct 4 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12632 KB Output is correct
14 Correct 3 ms 12636 KB Output is correct
15 Correct 3 ms 12728 KB Output is correct
16 Correct 3 ms 12636 KB Output is correct
17 Correct 4 ms 12636 KB Output is correct
18 Correct 3 ms 12636 KB Output is correct
19 Correct 4 ms 12636 KB Output is correct
20 Correct 3 ms 12636 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12636 KB Output is correct
23 Correct 3 ms 12636 KB Output is correct
24 Correct 3 ms 12632 KB Output is correct
25 Correct 3 ms 12636 KB Output is correct
26 Correct 3 ms 12632 KB Output is correct
27 Correct 3 ms 12636 KB Output is correct
28 Correct 3 ms 12636 KB Output is correct
29 Correct 3 ms 12636 KB Output is correct
30 Correct 3 ms 12636 KB Output is correct
31 Correct 3 ms 12636 KB Output is correct
32 Correct 3 ms 12636 KB Output is correct
33 Correct 4 ms 12636 KB Output is correct
34 Correct 3 ms 12636 KB Output is correct
35 Correct 3 ms 12632 KB Output is correct
36 Correct 3 ms 12636 KB Output is correct
37 Correct 3 ms 12636 KB Output is correct
38 Correct 3 ms 12636 KB Output is correct
39 Correct 3 ms 12636 KB Output is correct
40 Correct 4 ms 12636 KB Output is correct
41 Correct 3 ms 12632 KB Output is correct
42 Correct 3 ms 12636 KB Output is correct
43 Correct 3 ms 12636 KB Output is correct
44 Correct 4 ms 12632 KB Output is correct
45 Correct 4 ms 12888 KB Output is correct
46 Correct 14 ms 13148 KB Output is correct
47 Correct 12 ms 13344 KB Output is correct
48 Correct 12 ms 13148 KB Output is correct
49 Correct 15 ms 13404 KB Output is correct
50 Correct 13 ms 13400 KB Output is correct
51 Correct 13 ms 13404 KB Output is correct
52 Correct 14 ms 13404 KB Output is correct
53 Correct 14 ms 13404 KB Output is correct
54 Correct 15 ms 13404 KB Output is correct
55 Correct 14 ms 13400 KB Output is correct
56 Correct 14 ms 13148 KB Output is correct
57 Correct 16 ms 13148 KB Output is correct
58 Correct 16 ms 13148 KB Output is correct
59 Correct 16 ms 13148 KB Output is correct
60 Correct 16 ms 13148 KB Output is correct
61 Correct 17 ms 13148 KB Output is correct
62 Correct 17 ms 13148 KB Output is correct
63 Correct 17 ms 13148 KB Output is correct
64 Correct 12 ms 12888 KB Output is correct
65 Correct 15 ms 13024 KB Output is correct
66 Correct 11 ms 12892 KB Output is correct
67 Correct 11 ms 12892 KB Output is correct
68 Correct 12 ms 13144 KB Output is correct
69 Correct 15 ms 13404 KB Output is correct
70 Correct 18 ms 13148 KB Output is correct
71 Correct 15 ms 13192 KB Output is correct
72 Correct 16 ms 13148 KB Output is correct
73 Correct 15 ms 12892 KB Output is correct
74 Correct 15 ms 13148 KB Output is correct
75 Correct 14 ms 13148 KB Output is correct
76 Correct 14 ms 13272 KB Output is correct
77 Correct 14 ms 13144 KB Output is correct
78 Correct 14 ms 12892 KB Output is correct
79 Correct 13 ms 13068 KB Output is correct
80 Correct 13 ms 13056 KB Output is correct
81 Correct 15 ms 13148 KB Output is correct
82 Correct 16 ms 13244 KB Output is correct
83 Correct 14 ms 13148 KB Output is correct
84 Correct 14 ms 12888 KB Output is correct
85 Correct 15 ms 12892 KB Output is correct
86 Correct 13 ms 12888 KB Output is correct
87 Correct 31 ms 13916 KB Output is correct
88 Correct 103 ms 15964 KB Output is correct
89 Correct 450 ms 25516 KB Output is correct
90 Correct 427 ms 25520 KB Output is correct
91 Correct 427 ms 25408 KB Output is correct
92 Correct 497 ms 33220 KB Output is correct
93 Correct 482 ms 33008 KB Output is correct
94 Correct 489 ms 33124 KB Output is correct
95 Correct 593 ms 30016 KB Output is correct
96 Correct 648 ms 29904 KB Output is correct
97 Correct 614 ms 31952 KB Output is correct
98 Correct 604 ms 32084 KB Output is correct
99 Correct 638 ms 31460 KB Output is correct
100 Correct 703 ms 27392 KB Output is correct
101 Correct 716 ms 27564 KB Output is correct
102 Correct 733 ms 27800 KB Output is correct
103 Correct 775 ms 27532 KB Output is correct
104 Correct 725 ms 31272 KB Output is correct
105 Correct 695 ms 31340 KB Output is correct
106 Correct 726 ms 31424 KB Output is correct
107 Correct 353 ms 21944 KB Output is correct
108 Correct 412 ms 21968 KB Output is correct
109 Correct 475 ms 23860 KB Output is correct
110 Correct 461 ms 29800 KB Output is correct
111 Correct 626 ms 31548 KB Output is correct
112 Correct 600 ms 26620 KB Output is correct
113 Correct 622 ms 26236 KB Output is correct
114 Correct 720 ms 27344 KB Output is correct
115 Correct 689 ms 21892 KB Output is correct
116 Correct 742 ms 25952 KB Output is correct
117 Correct 630 ms 28868 KB Output is correct
118 Correct 592 ms 28500 KB Output is correct
119 Correct 552 ms 27884 KB Output is correct
120 Correct 556 ms 23760 KB Output is correct
121 Correct 529 ms 23436 KB Output is correct
122 Correct 512 ms 23568 KB Output is correct
123 Correct 651 ms 28884 KB Output is correct
124 Correct 644 ms 28360 KB Output is correct
125 Correct 614 ms 27868 KB Output is correct
126 Correct 622 ms 24140 KB Output is correct
127 Correct 594 ms 23840 KB Output is correct
128 Correct 575 ms 23156 KB Output is correct