Submission #986276

# Submission time Handle Problem Language Result Execution time Memory
986276 2024-05-20T08:15:54 Z AlperenT_ Tourism (JOI23_tourism) C++17
100 / 100
569 ms 47864 KB
#include <bits/stdc++.h>
 
#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second 
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define ld long double
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= b;i++)
#define per(i , a , b) for(int i=a;i >= b;i--)
using namespace std ;
const int maxn = 1e5 + 10  , N = 1e5 +1 , lg = 20 , maxq = 202   , inf = 1e9  , maxk = 2022  , mod =998244353;
int n , q , m ;
vector <pii> qr[maxn] ;
vector <int> G[maxn] ;
int sb[maxn] , st[maxn] , c[maxn] , en[maxn] , par[maxn] ;
int ms[maxn][lg+1] , me[maxn][lg+1] , dis[maxn] , h[maxn] ;

int lca(int v ,int u){
    if(st[v] <= st[u] && en[u] <= en[v])return v ;
    if(st[u] <= st[v] && en[v] <= en[u])return u ;
    if(dis[h[v]] > dis[h[u]])return lca(par[h[v]] , u) ;
    return lca(v , par[h[u]]) ;
}

int lc(int l ,int r){
    int x = 31 - __builtin_clz(r-l+1); 
    int v = (st[ms[l][x]] < st[ms[r-(1<<x)+1][x]] ? ms[l][x] : ms[r-(1<<x)+1][x]) ;
    int u = (en[me[l][x]] > en[me[r-(1<<x)+1][x]] ? me[l][x] : me[r-(1<<x)+1][x]) ;
    return lca(v , u) ;
}
void b1(int v , int p =0){
    sb[v]=1;
    for(int u : G[v]){
        if(u == p)continue ;
        par[u] = v ;
        dis[u] = dis[v]+1 ;
        b1(u , v) ;
        sb[v] += sb[u] ;
    }
}
int id =1 ; 
void b2(int v , int p =0){
    vector <pii> vec;
    st[v] = id ;id++;
    for(int u : G[v]){
        if(u == p)continue ;
        vec.pb({sb[u] , u});
    }
    sort(all(vec));
    reverse(all(vec));
    rep(i , 0 , sz(vec)-1){
        int u = vec[i].S; 
        if(i == 0){
            h[u] = h[v] ;
            b2(u , v);
        }else{
            h[u] = u ;
            b2(u , v);
        }
    }
    en[v] = id-1 ;
}

int fen[maxn] , ans[maxn] , f[maxn] ;
set <pii> s ;
void fupd(int x, int w){
    x++;
    while(x <= m+1){
        fen[x] += w;
        x += x&-x ;
    }
}
int fque(int x){
    x++;
    int ans = 0;
    while(x){
        ans += fen[x];
        x-=x&-x;
    }
    return ans; 
}
vector <pii> vec;

void find(int v){
    vec.pb({st[h[v]] , st[v]});
    if(h[v]==1)return ;
    find(par[h[v]]) ;
}
void add(int l ,int r){
    s.insert({l,r});
    fupd(f[l] , r-l+1);
}
void rem(int l , int r){
    s.erase({l,r}); 
    fupd(f[l] , -(r-l+1)) ;
}

void fix(int l , int r ,int x){
    auto a = s.lower_bound((pii){l+1, -1}) ; a-- ;
    if((*a).S >= r){
        int L = (*a).F , R = (*a).S ; 
        rem(L,R);
        if(r!=R){
            f[r+1] = f[L] ;
            add(r+1 ,R);
        }
        if(l!=L){
            add(L , l-1);
        
        }
        f[l] = x ;
        add(l,r);
        return ;
    }
    vector <pii>  o ;
    while(a!=s.end()){
        int R = (*a).S ;
        o.pb({(*a).F , R}) ;
        if(R >= r)break ;
        a++;
    }
    for(auto [l,r] : o)rem(l,r) ;
    if(o[0].F != l){
        add(o[0].F , l-1);
    }
    if(o.back().S != r){
        f[r+1] = f[o.back().F];
        add(r+1 , o.back().S) ; 
    }
    f[l] = x ;
    add(l ,r) ;
}

signed main(){
    ios_base::sync_with_stdio(false); cin.tie(0);   
    cin >> n >> m >> q ;
    rep(i , 1, n-1){
        int v, u ;
        cin >> v >> u ;
        G[v].pb(u);
        G[u].pb(v);
    }
    b1(1);
    h[1] =1 ;
    b2(1);
    rep(i ,1 , m){
        cin >> c[i] ;
        ms[i][0] = me[i][0] = c[i];
    }
    rep(j , 1 , lg){
        rep(i , 1, m){
            int f = (1<<(j-1)) ;
            if(i+f > m){
                me[i][j] = me[i][j-1] ;
                ms[i][j] = ms[i][j-1] ;
            }else{
                ms[i][j] = (st[ms[i][j-1]] < st[ms[i+f][j-1]] ? ms[i][j-1] : ms[i+f][j-1]) ;
                me[i][j] = (en[me[i][j-1]] > en[me[i+f][j-1]] ? me[i][j-1] : me[i+f][j-1]) ;
            }
        }
    }
    rep(i ,1 ,q){
        int l , r; 
        cin >> l >> r ;
        qr[r].pb({l , i});
        ans[i] -= dis[lc(l , r)] ;
    }
    f[1] = 0 ;
    add(1 ,n);
    rep(i , 1, m){
        vec.clear();
        find(c[i]) ;
        for(pii a : vec){
            fix(a.F ,a.S , i);
        }
        for(auto [l,j] : qr[i]){
            ans[j] += fque(i) - fque(l-1) ;
        }
    }
    rep(i ,1 ,q){
        cout << ans[i] << "\n"; 
    }

}
/*
8 8 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 6 4 3 5 2 4 7
3 5
4 6
6 8
1 4
2 3
6 8
5 5
2 8
1 2

*/
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 3 ms 10840 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 3 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10840 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 3 ms 11016 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 3 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 3 ms 10976 KB Output is correct
21 Correct 3 ms 10840 KB Output is correct
22 Correct 2 ms 10840 KB Output is correct
23 Correct 2 ms 10840 KB Output is correct
24 Correct 3 ms 10844 KB Output is correct
25 Correct 3 ms 10936 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 3 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 3 ms 10840 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 3 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10840 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 3 ms 11016 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 3 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 3 ms 10976 KB Output is correct
21 Correct 3 ms 10840 KB Output is correct
22 Correct 2 ms 10840 KB Output is correct
23 Correct 2 ms 10840 KB Output is correct
24 Correct 3 ms 10844 KB Output is correct
25 Correct 3 ms 10936 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 3 ms 10844 KB Output is correct
30 Correct 5 ms 11100 KB Output is correct
31 Correct 5 ms 11352 KB Output is correct
32 Correct 6 ms 11356 KB Output is correct
33 Correct 6 ms 11412 KB Output is correct
34 Correct 6 ms 11356 KB Output is correct
35 Correct 6 ms 11352 KB Output is correct
36 Correct 6 ms 11356 KB Output is correct
37 Correct 6 ms 11356 KB Output is correct
38 Correct 4 ms 11500 KB Output is correct
39 Correct 5 ms 11356 KB Output is correct
40 Correct 4 ms 11356 KB Output is correct
41 Correct 4 ms 11612 KB Output is correct
42 Correct 4 ms 11612 KB Output is correct
43 Correct 4 ms 11356 KB Output is correct
44 Correct 5 ms 11356 KB Output is correct
45 Correct 5 ms 11356 KB Output is correct
46 Correct 5 ms 11356 KB Output is correct
47 Correct 5 ms 11352 KB Output is correct
48 Correct 5 ms 11356 KB Output is correct
49 Correct 5 ms 11356 KB Output is correct
50 Correct 5 ms 11356 KB Output is correct
51 Correct 4 ms 11356 KB Output is correct
52 Correct 4 ms 11356 KB Output is correct
53 Correct 4 ms 11352 KB Output is correct
54 Correct 5 ms 11356 KB Output is correct
55 Correct 4 ms 11356 KB Output is correct
56 Correct 3 ms 10984 KB Output is correct
57 Correct 3 ms 11100 KB Output is correct
58 Correct 6 ms 11252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 3 ms 10844 KB Output is correct
4 Correct 97 ms 37676 KB Output is correct
5 Correct 83 ms 42716 KB Output is correct
6 Correct 92 ms 44864 KB Output is correct
7 Correct 133 ms 47576 KB Output is correct
8 Correct 128 ms 47624 KB Output is correct
9 Correct 132 ms 47864 KB Output is correct
10 Correct 129 ms 47604 KB Output is correct
11 Correct 128 ms 47700 KB Output is correct
12 Correct 98 ms 47504 KB Output is correct
13 Correct 98 ms 47440 KB Output is correct
14 Correct 97 ms 47444 KB Output is correct
15 Correct 50 ms 32596 KB Output is correct
16 Correct 119 ms 47308 KB Output is correct
17 Correct 68 ms 28656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 167 ms 28764 KB Output is correct
3 Correct 261 ms 29584 KB Output is correct
4 Correct 206 ms 30156 KB Output is correct
5 Correct 329 ms 33144 KB Output is correct
6 Correct 329 ms 33304 KB Output is correct
7 Correct 321 ms 33104 KB Output is correct
8 Correct 327 ms 33404 KB Output is correct
9 Correct 327 ms 33076 KB Output is correct
10 Correct 342 ms 33336 KB Output is correct
11 Correct 327 ms 33240 KB Output is correct
12 Correct 332 ms 33600 KB Output is correct
13 Correct 328 ms 33620 KB Output is correct
14 Correct 348 ms 34640 KB Output is correct
15 Correct 357 ms 37908 KB Output is correct
16 Correct 329 ms 33620 KB Output is correct
17 Correct 333 ms 33616 KB Output is correct
18 Correct 318 ms 33616 KB Output is correct
19 Correct 211 ms 30300 KB Output is correct
20 Correct 224 ms 30360 KB Output is correct
21 Correct 221 ms 30292 KB Output is correct
22 Correct 228 ms 30380 KB Output is correct
23 Correct 212 ms 30264 KB Output is correct
24 Correct 230 ms 30568 KB Output is correct
25 Correct 220 ms 30448 KB Output is correct
26 Correct 213 ms 30316 KB Output is correct
27 Correct 217 ms 30292 KB Output is correct
28 Correct 215 ms 30224 KB Output is correct
29 Correct 219 ms 30288 KB Output is correct
30 Correct 225 ms 30804 KB Output is correct
31 Correct 221 ms 30804 KB Output is correct
32 Correct 231 ms 31236 KB Output is correct
33 Correct 246 ms 32644 KB Output is correct
34 Correct 241 ms 35156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 3 ms 10844 KB Output is correct
4 Correct 440 ms 33260 KB Output is correct
5 Correct 466 ms 33620 KB Output is correct
6 Correct 535 ms 35932 KB Output is correct
7 Correct 560 ms 37456 KB Output is correct
8 Correct 559 ms 37520 KB Output is correct
9 Correct 541 ms 37524 KB Output is correct
10 Correct 547 ms 37508 KB Output is correct
11 Correct 569 ms 37556 KB Output is correct
12 Correct 560 ms 37472 KB Output is correct
13 Correct 566 ms 37404 KB Output is correct
14 Correct 76 ms 28752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 3 ms 10840 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 3 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10840 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 3 ms 11016 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 3 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 3 ms 10976 KB Output is correct
21 Correct 3 ms 10840 KB Output is correct
22 Correct 2 ms 10840 KB Output is correct
23 Correct 2 ms 10840 KB Output is correct
24 Correct 3 ms 10844 KB Output is correct
25 Correct 3 ms 10936 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 3 ms 10844 KB Output is correct
30 Correct 5 ms 11100 KB Output is correct
31 Correct 5 ms 11352 KB Output is correct
32 Correct 6 ms 11356 KB Output is correct
33 Correct 6 ms 11412 KB Output is correct
34 Correct 6 ms 11356 KB Output is correct
35 Correct 6 ms 11352 KB Output is correct
36 Correct 6 ms 11356 KB Output is correct
37 Correct 6 ms 11356 KB Output is correct
38 Correct 4 ms 11500 KB Output is correct
39 Correct 5 ms 11356 KB Output is correct
40 Correct 4 ms 11356 KB Output is correct
41 Correct 4 ms 11612 KB Output is correct
42 Correct 4 ms 11612 KB Output is correct
43 Correct 4 ms 11356 KB Output is correct
44 Correct 5 ms 11356 KB Output is correct
45 Correct 5 ms 11356 KB Output is correct
46 Correct 5 ms 11356 KB Output is correct
47 Correct 5 ms 11352 KB Output is correct
48 Correct 5 ms 11356 KB Output is correct
49 Correct 5 ms 11356 KB Output is correct
50 Correct 5 ms 11356 KB Output is correct
51 Correct 4 ms 11356 KB Output is correct
52 Correct 4 ms 11356 KB Output is correct
53 Correct 4 ms 11352 KB Output is correct
54 Correct 5 ms 11356 KB Output is correct
55 Correct 4 ms 11356 KB Output is correct
56 Correct 3 ms 10984 KB Output is correct
57 Correct 3 ms 11100 KB Output is correct
58 Correct 6 ms 11252 KB Output is correct
59 Correct 2 ms 10844 KB Output is correct
60 Correct 2 ms 10844 KB Output is correct
61 Correct 3 ms 10844 KB Output is correct
62 Correct 97 ms 37676 KB Output is correct
63 Correct 83 ms 42716 KB Output is correct
64 Correct 92 ms 44864 KB Output is correct
65 Correct 133 ms 47576 KB Output is correct
66 Correct 128 ms 47624 KB Output is correct
67 Correct 132 ms 47864 KB Output is correct
68 Correct 129 ms 47604 KB Output is correct
69 Correct 128 ms 47700 KB Output is correct
70 Correct 98 ms 47504 KB Output is correct
71 Correct 98 ms 47440 KB Output is correct
72 Correct 97 ms 47444 KB Output is correct
73 Correct 50 ms 32596 KB Output is correct
74 Correct 119 ms 47308 KB Output is correct
75 Correct 68 ms 28656 KB Output is correct
76 Correct 2 ms 10844 KB Output is correct
77 Correct 167 ms 28764 KB Output is correct
78 Correct 261 ms 29584 KB Output is correct
79 Correct 206 ms 30156 KB Output is correct
80 Correct 329 ms 33144 KB Output is correct
81 Correct 329 ms 33304 KB Output is correct
82 Correct 321 ms 33104 KB Output is correct
83 Correct 327 ms 33404 KB Output is correct
84 Correct 327 ms 33076 KB Output is correct
85 Correct 342 ms 33336 KB Output is correct
86 Correct 327 ms 33240 KB Output is correct
87 Correct 332 ms 33600 KB Output is correct
88 Correct 328 ms 33620 KB Output is correct
89 Correct 348 ms 34640 KB Output is correct
90 Correct 357 ms 37908 KB Output is correct
91 Correct 329 ms 33620 KB Output is correct
92 Correct 333 ms 33616 KB Output is correct
93 Correct 318 ms 33616 KB Output is correct
94 Correct 211 ms 30300 KB Output is correct
95 Correct 224 ms 30360 KB Output is correct
96 Correct 221 ms 30292 KB Output is correct
97 Correct 228 ms 30380 KB Output is correct
98 Correct 212 ms 30264 KB Output is correct
99 Correct 230 ms 30568 KB Output is correct
100 Correct 220 ms 30448 KB Output is correct
101 Correct 213 ms 30316 KB Output is correct
102 Correct 217 ms 30292 KB Output is correct
103 Correct 215 ms 30224 KB Output is correct
104 Correct 219 ms 30288 KB Output is correct
105 Correct 225 ms 30804 KB Output is correct
106 Correct 221 ms 30804 KB Output is correct
107 Correct 231 ms 31236 KB Output is correct
108 Correct 246 ms 32644 KB Output is correct
109 Correct 241 ms 35156 KB Output is correct
110 Correct 2 ms 10844 KB Output is correct
111 Correct 2 ms 10844 KB Output is correct
112 Correct 3 ms 10844 KB Output is correct
113 Correct 440 ms 33260 KB Output is correct
114 Correct 466 ms 33620 KB Output is correct
115 Correct 535 ms 35932 KB Output is correct
116 Correct 560 ms 37456 KB Output is correct
117 Correct 559 ms 37520 KB Output is correct
118 Correct 541 ms 37524 KB Output is correct
119 Correct 547 ms 37508 KB Output is correct
120 Correct 569 ms 37556 KB Output is correct
121 Correct 560 ms 37472 KB Output is correct
122 Correct 566 ms 37404 KB Output is correct
123 Correct 76 ms 28752 KB Output is correct
124 Correct 329 ms 36720 KB Output is correct
125 Correct 243 ms 34640 KB Output is correct
126 Correct 378 ms 37352 KB Output is correct
127 Correct 389 ms 37456 KB Output is correct
128 Correct 370 ms 37200 KB Output is correct
129 Correct 389 ms 37332 KB Output is correct
130 Correct 375 ms 37152 KB Output is correct
131 Correct 148 ms 45456 KB Output is correct
132 Correct 137 ms 47444 KB Output is correct
133 Correct 143 ms 40168 KB Output is correct
134 Correct 269 ms 34580 KB Output is correct
135 Correct 269 ms 34456 KB Output is correct
136 Correct 263 ms 34528 KB Output is correct
137 Correct 187 ms 38456 KB Output is correct
138 Correct 189 ms 38448 KB Output is correct
139 Correct 184 ms 38456 KB Output is correct
140 Correct 191 ms 38312 KB Output is correct
141 Correct 203 ms 38324 KB Output is correct
142 Correct 185 ms 38500 KB Output is correct
143 Correct 47 ms 17868 KB Output is correct
144 Correct 390 ms 36952 KB Output is correct