Submission #769952

# Submission time Handle Problem Language Result Execution time Memory
769952 2023-06-30T14:40:32 Z bachhoangxuan Construction of Highway (JOI18_construction) C++17
100 / 100
318 ms 33948 KB
// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<pii,int>
#define mpp make_pair
#define fi first
#define se second
const int inf=1e18;
const int mod=1e9+7;
const int maxn=100005;
const int bl=650;
const int maxs=655;
const int maxm=200005;
const int maxq=500005;
const int maxl=20;
const int maxa=250000;
const int root=3;
int power(int a,int n){
    int res=1;
    while(n){
        if(n&1) res=res*a%mod;
        a=a*a%mod;n>>=1;
    }
    return res;
}
const int iroot=power(3,mod-2);
const int base=101;

namespace BIT{
    int n;
    vector<int> bit;
    void init(int _n=0){n=_n;bit.assign(n+1,0);}
    void update(int x,int val){
        for(int i=x;i<=n;i+=(i&(-i))) bit[i]+=val;
    }
    int query(int x){
        int res=0;
        for(int i=x;i>=1;i-=(i&(-i))) res+=bit[i];
        return res;
    }
}
int ans=0;
vector<pii> ss;
namespace Segtree{
    int n=0;
    vector<int> tree;
    void init(int _n=0){
        n=_n;
        tree.assign(4*n,0);
    }
    void pushdown(int id){
        if(tree[id]==0) return;
        tree[id<<1]=tree[id<<1|1]=tree[id];
    }
    void update(int l,int r,int id,int tl,int tr,int val){
        if(tr<l || r<tl) return;
        if(tl<=l && r<=tr && tree[id]){
            ans+=BIT::query(tree[id]-1)*(r-l+1);
            BIT::update(tree[id],r-l+1);
            ss.push_back({tree[id],r-l+1});
            tree[id]=val;
            return;
        }
        else if(l==r){tree[id]=val;return;}
        pushdown(id);
        int mid=(l+r)>>1;
        update(mid+1,r,id<<1|1,tl,tr,val);update(l,mid,id<<1,tl,tr,val);
        tree[id]=(tree[id<<1]==tree[id<<1|1])*tree[id<<1];
    }
};
void solve(){
    int n,m,q;cin >> n;
    vector<vector<int>> edge(n+1);
    vector<int> L(n+1),head(n+1,0),child(n+1,1),son(n+1,0),par(n+1,0),c(n+1);
    vector<pii> e(n);
    int pos=0;

    vector<int> com;
    for(int i=1;i<=n;i++) cin >> c[i],com.push_back(c[i]);
    sort(com.begin(),com.end());
    com.erase(unique(com.begin(),com.end()),com.end());
    for(int i=1;i<=n;i++) c[i]=lower_bound(com.begin(),com.end(),c[i])-com.begin()+1;

    BIT::init(n);Segtree::init(n);
    for(int i=1;i<n;i++){
        int u,v;cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
        e[i]={u,v};
    }
    function<void(int,int)> dfs = [&](int u,int p){
        par[u]=p;
        for(int v:edge[u]){
            if(v==p) continue;
            dfs(v,u);child[u]+=child[v];
            son[u]=(child[son[u]]<=child[v]?v:son[u]);
        }
    };
    dfs(1,0);
    function<void(int,int)> hld_dfs = [&](int u,int p){
        L[u]=++pos;head[u]+=(!head[u])*u;
        if(son[u]){
            head[son[u]]=head[u];
            hld_dfs(son[u],u);
        }
        for(int v:edge[u]){
            if(v==p || v==son[u]) continue;
            hld_dfs(v,u);
        }
    };
    hld_dfs(1,0);
    auto hld_update = [=](int u,int v,int val){
        while(head[u]!=head[v]){
            if(L[head[u]]<L[head[v]]) swap(u,v);
            Segtree::update(1,n,1,L[head[u]],L[u],val);
            u=par[head[u]];
        }
        Segtree::update(1,n,1,min(L[u],L[v]),max(L[u],L[v]),val);
    };
    hld_update(1,1,c[1]);
    for(int i=1;i<n;i++){
        int u=e[i].fi,v=e[i].se;
        if(par[v]!=u) swap(u,v);
        ans=0;ss.clear();hld_update(1,v,c[v]);
        for(auto &[x,cnt]:ss){
            //cout << '{' << x << ' ' << cnt << "} ";
            BIT::update(x,-cnt);
        }
        //cout << '\n';
        cout << ans << '\n';
    }
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int test=1;//cin >> test;
    while(test--) solve();
}

Compilation message

construction.cpp: In function 'void solve()':
construction.cpp:95:11: warning: unused variable 'm' [-Wunused-variable]
   95 |     int n,m,q;cin >> n;
      |           ^
construction.cpp:95:13: warning: unused variable 'q' [-Wunused-variable]
   95 |     int n,m,q;cin >> n;
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 456 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 456 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 324 KB Output is correct
45 Correct 2 ms 464 KB Output is correct
46 Correct 6 ms 1176 KB Output is correct
47 Correct 6 ms 1184 KB Output is correct
48 Correct 6 ms 1108 KB Output is correct
49 Correct 4 ms 1620 KB Output is correct
50 Correct 4 ms 1620 KB Output is correct
51 Correct 6 ms 1612 KB Output is correct
52 Correct 4 ms 1364 KB Output is correct
53 Correct 4 ms 1492 KB Output is correct
54 Correct 6 ms 1492 KB Output is correct
55 Correct 5 ms 1492 KB Output is correct
56 Correct 4 ms 1396 KB Output is correct
57 Correct 6 ms 1108 KB Output is correct
58 Correct 7 ms 1144 KB Output is correct
59 Correct 7 ms 1108 KB Output is correct
60 Correct 7 ms 1108 KB Output is correct
61 Correct 5 ms 1364 KB Output is correct
62 Correct 4 ms 1364 KB Output is correct
63 Correct 4 ms 1360 KB Output is correct
64 Correct 5 ms 1100 KB Output is correct
65 Correct 6 ms 1160 KB Output is correct
66 Correct 5 ms 1108 KB Output is correct
67 Correct 5 ms 1108 KB Output is correct
68 Correct 3 ms 1616 KB Output is correct
69 Correct 4 ms 1364 KB Output is correct
70 Correct 4 ms 1364 KB Output is correct
71 Correct 3 ms 1364 KB Output is correct
72 Correct 6 ms 1168 KB Output is correct
73 Correct 6 ms 1108 KB Output is correct
74 Correct 4 ms 1364 KB Output is correct
75 Correct 4 ms 1228 KB Output is correct
76 Correct 4 ms 1236 KB Output is correct
77 Correct 4 ms 1236 KB Output is correct
78 Correct 4 ms 1236 KB Output is correct
79 Correct 6 ms 1236 KB Output is correct
80 Correct 6 ms 1216 KB Output is correct
81 Correct 5 ms 1236 KB Output is correct
82 Correct 5 ms 1236 KB Output is correct
83 Correct 5 ms 1152 KB Output is correct
84 Correct 6 ms 1236 KB Output is correct
85 Correct 5 ms 1164 KB Output is correct
86 Correct 5 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 456 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 324 KB Output is correct
45 Correct 2 ms 464 KB Output is correct
46 Correct 6 ms 1176 KB Output is correct
47 Correct 6 ms 1184 KB Output is correct
48 Correct 6 ms 1108 KB Output is correct
49 Correct 4 ms 1620 KB Output is correct
50 Correct 4 ms 1620 KB Output is correct
51 Correct 6 ms 1612 KB Output is correct
52 Correct 4 ms 1364 KB Output is correct
53 Correct 4 ms 1492 KB Output is correct
54 Correct 6 ms 1492 KB Output is correct
55 Correct 5 ms 1492 KB Output is correct
56 Correct 4 ms 1396 KB Output is correct
57 Correct 6 ms 1108 KB Output is correct
58 Correct 7 ms 1144 KB Output is correct
59 Correct 7 ms 1108 KB Output is correct
60 Correct 7 ms 1108 KB Output is correct
61 Correct 5 ms 1364 KB Output is correct
62 Correct 4 ms 1364 KB Output is correct
63 Correct 4 ms 1360 KB Output is correct
64 Correct 5 ms 1100 KB Output is correct
65 Correct 6 ms 1160 KB Output is correct
66 Correct 5 ms 1108 KB Output is correct
67 Correct 5 ms 1108 KB Output is correct
68 Correct 3 ms 1616 KB Output is correct
69 Correct 4 ms 1364 KB Output is correct
70 Correct 4 ms 1364 KB Output is correct
71 Correct 3 ms 1364 KB Output is correct
72 Correct 6 ms 1168 KB Output is correct
73 Correct 6 ms 1108 KB Output is correct
74 Correct 4 ms 1364 KB Output is correct
75 Correct 4 ms 1228 KB Output is correct
76 Correct 4 ms 1236 KB Output is correct
77 Correct 4 ms 1236 KB Output is correct
78 Correct 4 ms 1236 KB Output is correct
79 Correct 6 ms 1236 KB Output is correct
80 Correct 6 ms 1216 KB Output is correct
81 Correct 5 ms 1236 KB Output is correct
82 Correct 5 ms 1236 KB Output is correct
83 Correct 5 ms 1152 KB Output is correct
84 Correct 6 ms 1236 KB Output is correct
85 Correct 5 ms 1164 KB Output is correct
86 Correct 5 ms 1108 KB Output is correct
87 Correct 15 ms 2508 KB Output is correct
88 Correct 50 ms 6860 KB Output is correct
89 Correct 218 ms 22152 KB Output is correct
90 Correct 229 ms 22104 KB Output is correct
91 Correct 213 ms 22136 KB Output is correct
92 Correct 114 ms 33848 KB Output is correct
93 Correct 116 ms 33856 KB Output is correct
94 Correct 127 ms 33948 KB Output is correct
95 Correct 122 ms 29644 KB Output is correct
96 Correct 118 ms 30036 KB Output is correct
97 Correct 118 ms 29984 KB Output is correct
98 Correct 154 ms 30024 KB Output is correct
99 Correct 125 ms 28572 KB Output is correct
100 Correct 268 ms 21720 KB Output is correct
101 Correct 301 ms 21992 KB Output is correct
102 Correct 310 ms 22068 KB Output is correct
103 Correct 318 ms 21984 KB Output is correct
104 Correct 160 ms 28576 KB Output is correct
105 Correct 147 ms 28576 KB Output is correct
106 Correct 146 ms 28612 KB Output is correct
107 Correct 187 ms 21252 KB Output is correct
108 Correct 208 ms 21444 KB Output is correct
109 Correct 230 ms 21728 KB Output is correct
110 Correct 124 ms 33308 KB Output is correct
111 Correct 115 ms 29604 KB Output is correct
112 Correct 109 ms 29388 KB Output is correct
113 Correct 115 ms 28016 KB Output is correct
114 Correct 296 ms 21736 KB Output is correct
115 Correct 303 ms 21388 KB Output is correct
116 Correct 182 ms 27972 KB Output is correct
117 Correct 148 ms 25116 KB Output is correct
118 Correct 165 ms 24204 KB Output is correct
119 Correct 173 ms 23552 KB Output is correct
120 Correct 145 ms 24692 KB Output is correct
121 Correct 132 ms 23752 KB Output is correct
122 Correct 166 ms 23260 KB Output is correct
123 Correct 183 ms 25156 KB Output is correct
124 Correct 171 ms 24064 KB Output is correct
125 Correct 206 ms 23736 KB Output is correct
126 Correct 196 ms 24792 KB Output is correct
127 Correct 170 ms 23824 KB Output is correct
128 Correct 202 ms 23252 KB Output is correct