Submission #794015

# Submission time Handle Problem Language Result Execution time Memory
794015 2023-07-26T08:44:56 Z vjudge1 Tourism (JOI23_tourism) C++17
35 / 100
5000 ms 252648 KB

// Author : حسن

#include <bits/stdc++.h>



using namespace std;

#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"


const int N = 2e5 + 9 , mod = 1e9 + 7;
ll c[N] = {} , d[N] = {} , a[N] = {}, b[N] = {}  , us[N] , us1[N] , st[20][N][2] , dp[N] = {};

vector<int>v[N];
set<pair<int,int>>st1[N / 2];
void dfs(int n,int p = 0){
    d[n] = d[p] + 1;
    for(auto to : v[n])
        if(to != p)
            dfs(to , n);
}

void dfs2(int n,int p = 0){
    for(auto to1 : v[n])
        if(to1 != p){
            dfs2(to1 , n);
            if(st1[to1].size() > st1[n].size())
                swap(st1[to1] , st1[n]);
            for(auto to : st1[to1]){
                auto it = st1[n].lower_bound({to.fi , 0});
                if(it != st1[n].end() && it->fi == to.fi){
                    c[to.fi] += max(to.se - d[n] - 1 ,0ll);
                    if(to.se != d[n] && it->se != d[n])
                        c[to.fi]++;
                    c[to.fi] += max(it->se - d[n] - 1 ,0ll);
                    st1[n].erase(it);
                    st1[n].insert({to.fi , d[n]});
                }else
                    st1[n].insert(to);
        }
}
}


ll ans;
void dfs1(int n,int p = 0){
    for(auto to : v[n])
        if(to != p)
            dfs1(to , n);
        ll s = 0;
     for(auto to  : v[n])
            if(b[to] != 0)
                if(to != p)
                s++, b[n] = b[to];
    if(s != 0){
        if(s == 1 && us[n] == 0){

        }else {
        ans++;
        for(auto to  : v[n])
            if(to != p && b[to] != 0){
            ans += d[b[to]] - d[n] - 1;
            }
        b[n] = n;
        }
    }else if(us[n] == 1){
        ans++;
        b[n] = n;
    }
}



pair<ll,ll> get(int x , int y){
    ll i = dp[y - x + 1];
    ll minimum = min(st[i][x][1], st[i][y - (1 << i) + 1][1]);
    ll maximum = max(st[i][x][0], st[i][y - (1 << i) + 1][0]);
    //cout<<i<<" "<<minimum<<" "<<maximum<<" ";
    return {maximum , minimum};
}

void get1(){
    ll x ,y , i;
x = 0;
    y = 1;
    dp[0] = 0;
    for(i = 1; i <= 2e5; i++){
        if(2 * y <= i)
            x++, y *= 2;
        dp[i] = x;
    }
    }

void solve(){
    ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
    cin>>n>>m>>q;
    for(i = 1; i < n; i++){
        cin>>l>>r;
        v[l].pb(r);
        v[r].pb(l);
        if(!(l == i && r == i + 1))
            s++;
    }
    for(i = 1; i <= m; i++)
        cin>>a[i];
    dfs(1);

    if(s && n <= 2000 && m <= 2000 && q <= 2000){
    while(q--){
        cin>>l>>r;
        for(i = 1; i <= n; i++){
            us[i] = b[i] = c[i] = 0;
        }
        for(i = l; i <= r; i++)
            us[a[i]] = 1;
        ans = 0;
        dfs1(1);
        cout<<ans<<"\n";
    }
    }else if(s == 0){
    get1();
    for(i = 1; i <= m; i++)
        st[0][i][0]  = st[0][i][1] = d[a[i]];
    for (int i = 1; i < 18; i++)
    for (int j = 1; j + (1 << i) <= (m + 2); j++){
        st[i][j][0] = max(st[i - 1][j][0], st[i - 1][j + (1 << (i - 1))][0]);
        st[i][j][1] = min(st[i - 1][j][1], st[i - 1][j + (1 << (i - 1))][1]);
    }
    while(q--){
        cin>>l>>r;
        cout<<get(l , r).fi - get(l , r).se + 1<<"\n";
    }
    }else {
        for(i = 1; i <= q; i++){
            cin>>l>>r;
            for(j = l; j <= r; j++){
                if(us[a[j]] != i){ us[a[j]] = i , c[i]++;}
                st1[a[j]].insert({i , d[a[j]]});
            }
        }
        dfs2( 1);
        for(i = 1; i <= q; i++)
            cout<<c[i]<<"\n";
    }
}

int main(){

     TL;
     /*
     #ifndef ONLINE_JUDGE
     freopen("input.txt", "r", stdin);
     freopen("output.txt", "w", stdout);
     #endif
     */
int t = 1;
//cin>>t;

while(t--)
     {
     solve();
     }

}
// Author : حسن

Compilation message

tourism.cpp: In function 'void solve()':
tourism.cpp:111:28: warning: unused variable 'z' [-Wunused-variable]
  111 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                            ^
tourism.cpp:111:40: warning: unused variable 'f' [-Wunused-variable]
  111 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                        ^
tourism.cpp:111:51: warning: unused variable 'k' [-Wunused-variable]
  111 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                                   ^
tourism.cpp:111:55: warning: unused variable 'x' [-Wunused-variable]
  111 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                                       ^
tourism.cpp:111:59: warning: unused variable 'y' [-Wunused-variable]
  111 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                                           ^
tourism.cpp:111:63: warning: unused variable 'mn' [-Wunused-variable]
  111 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                                               ^~
tourism.cpp:111:76: warning: unused variable 'mx' [-Wunused-variable]
  111 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                                                            ^~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 11260 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 11 ms 9692 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 5 ms 9732 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 6 ms 9684 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 6 ms 9720 KB Output is correct
18 Correct 6 ms 9684 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 6 ms 9732 KB Output is correct
21 Correct 7 ms 9728 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 6 ms 9684 KB Output is correct
24 Correct 7 ms 9684 KB Output is correct
25 Correct 6 ms 9732 KB Output is correct
26 Correct 6 ms 9732 KB Output is correct
27 Correct 6 ms 11388 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 6 ms 9768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 11260 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 11 ms 9692 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 5 ms 9732 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 6 ms 9684 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 6 ms 9720 KB Output is correct
18 Correct 6 ms 9684 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 6 ms 9732 KB Output is correct
21 Correct 7 ms 9728 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 6 ms 9684 KB Output is correct
24 Correct 7 ms 9684 KB Output is correct
25 Correct 6 ms 9732 KB Output is correct
26 Correct 6 ms 9732 KB Output is correct
27 Correct 6 ms 11388 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 6 ms 9768 KB Output is correct
30 Correct 50 ms 9888 KB Output is correct
31 Correct 58 ms 9876 KB Output is correct
32 Correct 91 ms 9948 KB Output is correct
33 Correct 86 ms 9928 KB Output is correct
34 Correct 92 ms 9940 KB Output is correct
35 Correct 55 ms 9940 KB Output is correct
36 Correct 66 ms 9948 KB Output is correct
37 Correct 57 ms 9948 KB Output is correct
38 Correct 69 ms 10016 KB Output is correct
39 Correct 62 ms 9940 KB Output is correct
40 Correct 65 ms 10036 KB Output is correct
41 Correct 69 ms 9940 KB Output is correct
42 Correct 70 ms 9988 KB Output is correct
43 Correct 70 ms 10012 KB Output is correct
44 Correct 72 ms 9980 KB Output is correct
45 Correct 65 ms 9940 KB Output is correct
46 Correct 65 ms 9980 KB Output is correct
47 Correct 64 ms 9984 KB Output is correct
48 Correct 63 ms 9944 KB Output is correct
49 Correct 63 ms 9940 KB Output is correct
50 Correct 65 ms 9960 KB Output is correct
51 Correct 64 ms 9944 KB Output is correct
52 Correct 64 ms 9940 KB Output is correct
53 Correct 63 ms 9956 KB Output is correct
54 Correct 63 ms 9940 KB Output is correct
55 Correct 66 ms 9940 KB Output is correct
56 Correct 6 ms 11732 KB Output is correct
57 Correct 45 ms 9900 KB Output is correct
58 Correct 44 ms 9940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 6 ms 11348 KB Output is correct
3 Correct 6 ms 11656 KB Output is correct
4 Correct 54 ms 39936 KB Output is correct
5 Correct 48 ms 32948 KB Output is correct
6 Correct 61 ms 40740 KB Output is correct
7 Correct 69 ms 47124 KB Output is correct
8 Correct 72 ms 47140 KB Output is correct
9 Correct 68 ms 47152 KB Output is correct
10 Correct 68 ms 47160 KB Output is correct
11 Correct 80 ms 47260 KB Output is correct
12 Correct 63 ms 46976 KB Output is correct
13 Correct 64 ms 47032 KB Output is correct
14 Correct 63 ms 46948 KB Output is correct
15 Correct 42 ms 20052 KB Output is correct
16 Correct 67 ms 46852 KB Output is correct
17 Correct 42 ms 38268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 68 ms 22420 KB Output is correct
3 Correct 95 ms 28360 KB Output is correct
4 Correct 82 ms 25356 KB Output is correct
5 Correct 57 ms 20008 KB Output is correct
6 Correct 75 ms 21656 KB Output is correct
7 Correct 90 ms 23680 KB Output is correct
8 Correct 88 ms 25640 KB Output is correct
9 Correct 102 ms 27700 KB Output is correct
10 Correct 110 ms 29640 KB Output is correct
11 Correct 127 ms 31916 KB Output is correct
12 Correct 136 ms 33948 KB Output is correct
13 Correct 139 ms 36148 KB Output is correct
14 Correct 155 ms 38768 KB Output is correct
15 Correct 186 ms 45000 KB Output is correct
16 Correct 114 ms 29708 KB Output is correct
17 Correct 112 ms 29980 KB Output is correct
18 Correct 115 ms 29648 KB Output is correct
19 Correct 57 ms 20156 KB Output is correct
20 Correct 76 ms 20952 KB Output is correct
21 Correct 68 ms 21520 KB Output is correct
22 Correct 70 ms 22008 KB Output is correct
23 Correct 72 ms 22320 KB Output is correct
24 Correct 79 ms 23096 KB Output is correct
25 Correct 92 ms 23744 KB Output is correct
26 Correct 91 ms 24776 KB Output is correct
27 Correct 95 ms 26100 KB Output is correct
28 Correct 107 ms 27288 KB Output is correct
29 Correct 112 ms 28468 KB Output is correct
30 Correct 137 ms 30424 KB Output is correct
31 Correct 128 ms 31724 KB Output is correct
32 Correct 139 ms 33460 KB Output is correct
33 Correct 152 ms 37124 KB Output is correct
34 Correct 156 ms 39128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 11348 KB Output is correct
3 Correct 6 ms 11732 KB Output is correct
4 Execution timed out 5035 ms 252648 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 11260 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 11 ms 9692 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 5 ms 9732 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 6 ms 9684 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 6 ms 9720 KB Output is correct
18 Correct 6 ms 9684 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 6 ms 9732 KB Output is correct
21 Correct 7 ms 9728 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 6 ms 9684 KB Output is correct
24 Correct 7 ms 9684 KB Output is correct
25 Correct 6 ms 9732 KB Output is correct
26 Correct 6 ms 9732 KB Output is correct
27 Correct 6 ms 11388 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 6 ms 9768 KB Output is correct
30 Correct 50 ms 9888 KB Output is correct
31 Correct 58 ms 9876 KB Output is correct
32 Correct 91 ms 9948 KB Output is correct
33 Correct 86 ms 9928 KB Output is correct
34 Correct 92 ms 9940 KB Output is correct
35 Correct 55 ms 9940 KB Output is correct
36 Correct 66 ms 9948 KB Output is correct
37 Correct 57 ms 9948 KB Output is correct
38 Correct 69 ms 10016 KB Output is correct
39 Correct 62 ms 9940 KB Output is correct
40 Correct 65 ms 10036 KB Output is correct
41 Correct 69 ms 9940 KB Output is correct
42 Correct 70 ms 9988 KB Output is correct
43 Correct 70 ms 10012 KB Output is correct
44 Correct 72 ms 9980 KB Output is correct
45 Correct 65 ms 9940 KB Output is correct
46 Correct 65 ms 9980 KB Output is correct
47 Correct 64 ms 9984 KB Output is correct
48 Correct 63 ms 9944 KB Output is correct
49 Correct 63 ms 9940 KB Output is correct
50 Correct 65 ms 9960 KB Output is correct
51 Correct 64 ms 9944 KB Output is correct
52 Correct 64 ms 9940 KB Output is correct
53 Correct 63 ms 9956 KB Output is correct
54 Correct 63 ms 9940 KB Output is correct
55 Correct 66 ms 9940 KB Output is correct
56 Correct 6 ms 11732 KB Output is correct
57 Correct 45 ms 9900 KB Output is correct
58 Correct 44 ms 9940 KB Output is correct
59 Correct 6 ms 11220 KB Output is correct
60 Correct 6 ms 11348 KB Output is correct
61 Correct 6 ms 11656 KB Output is correct
62 Correct 54 ms 39936 KB Output is correct
63 Correct 48 ms 32948 KB Output is correct
64 Correct 61 ms 40740 KB Output is correct
65 Correct 69 ms 47124 KB Output is correct
66 Correct 72 ms 47140 KB Output is correct
67 Correct 68 ms 47152 KB Output is correct
68 Correct 68 ms 47160 KB Output is correct
69 Correct 80 ms 47260 KB Output is correct
70 Correct 63 ms 46976 KB Output is correct
71 Correct 64 ms 47032 KB Output is correct
72 Correct 63 ms 46948 KB Output is correct
73 Correct 42 ms 20052 KB Output is correct
74 Correct 67 ms 46852 KB Output is correct
75 Correct 42 ms 38268 KB Output is correct
76 Correct 5 ms 9684 KB Output is correct
77 Correct 68 ms 22420 KB Output is correct
78 Correct 95 ms 28360 KB Output is correct
79 Correct 82 ms 25356 KB Output is correct
80 Correct 57 ms 20008 KB Output is correct
81 Correct 75 ms 21656 KB Output is correct
82 Correct 90 ms 23680 KB Output is correct
83 Correct 88 ms 25640 KB Output is correct
84 Correct 102 ms 27700 KB Output is correct
85 Correct 110 ms 29640 KB Output is correct
86 Correct 127 ms 31916 KB Output is correct
87 Correct 136 ms 33948 KB Output is correct
88 Correct 139 ms 36148 KB Output is correct
89 Correct 155 ms 38768 KB Output is correct
90 Correct 186 ms 45000 KB Output is correct
91 Correct 114 ms 29708 KB Output is correct
92 Correct 112 ms 29980 KB Output is correct
93 Correct 115 ms 29648 KB Output is correct
94 Correct 57 ms 20156 KB Output is correct
95 Correct 76 ms 20952 KB Output is correct
96 Correct 68 ms 21520 KB Output is correct
97 Correct 70 ms 22008 KB Output is correct
98 Correct 72 ms 22320 KB Output is correct
99 Correct 79 ms 23096 KB Output is correct
100 Correct 92 ms 23744 KB Output is correct
101 Correct 91 ms 24776 KB Output is correct
102 Correct 95 ms 26100 KB Output is correct
103 Correct 107 ms 27288 KB Output is correct
104 Correct 112 ms 28468 KB Output is correct
105 Correct 137 ms 30424 KB Output is correct
106 Correct 128 ms 31724 KB Output is correct
107 Correct 139 ms 33460 KB Output is correct
108 Correct 152 ms 37124 KB Output is correct
109 Correct 156 ms 39128 KB Output is correct
110 Correct 5 ms 9684 KB Output is correct
111 Correct 6 ms 11348 KB Output is correct
112 Correct 6 ms 11732 KB Output is correct
113 Execution timed out 5035 ms 252648 KB Time limit exceeded
114 Halted 0 ms 0 KB -