Submission #958751

# Submission time Handle Problem Language Result Execution time Memory
958751 2024-04-06T14:16:38 Z hotboy2703 Tourism (JOI23_tourism) C++14
100 / 100
1920 ms 226396 KB
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
#define MP make_pair
const ll MAXN = 1e5;
namespace BIT{
    ll a[MAXN + 100];
    void upd(ll i,ll val){
        for (;i <= MAXN;i += i & -i)a[i]+=val;
    }
    ll get(ll i){
        ll res = 0;
        for (;i > 0;i -= i & -i)res += a[i];
        return res;
    }
}
namespace hld{
    ll n;
    vector <ll> g[MAXN+100];
    ll pa[MAXN+100],sz[MAXN+100];
    ll in[MAXN+100],out[MAXN+100];
    ll depth[MAXN+100];
    ll nxt[MAXN+100];
    ll timeDFS;


    void dfs_pa(ll u = 1,ll p = 1){
        pa[u] = p;
        sz[u] = 1;
        depth[u] = depth[p] + 1;
        for (auto v:g[u]){
            if (v==p)continue;
            dfs_pa(v,u);
            sz[u] += sz[v];
        }
    }
    void dfs_hld(ll u = 1){
        in[u] = ++timeDFS;
        for (auto v:g[u]){
            nxt[v] = (v==g[u][0]?nxt[u]:v);
            dfs_hld(v);
        }
        out[u] = timeDFS;
    }


    void init(ll N){
        n = N;
        timeDFS = 0;
        for (ll i = 1;i <= n;i ++)g[i].clear();
        for (ll i = 1;i < n;i ++){
            ll u,v;
            cin>>u>>v;
            g[u].push_back(v);
            g[v].push_back(u);
        }

        dfs_pa();
        for (ll i = 1;i <= n;i ++)g[i].clear();
        for (ll i = 2;i <= n;i ++)g[pa[i]].push_back(i);

        nxt[1] = 1;
        dfs_hld();
//        for (ll i = 1;i <= n;i ++)cout<<pa[i]<<' ';
//        cout<<endl;
//        for (ll i = 1;i <= n;i ++)cout<<nxt[i]<<' ';
//        cout<<endl;
    }

    vector <pll> path(ll u,ll v){
        //pairs of ancestors and successors
        //to get range use in[first] and in[second]
        vector <pll> res;
        while (nxt[u] != nxt[v]){
//            cout<<"WOW "<<u<<' '<<v<<endl;
            if (depth[nxt[u]] > depth[nxt[v]])swap(u,v);
            res.push_back(MP(nxt[v],v));
            v = pa[nxt[v]];
        }
        if (depth[u] > depth[v])swap(u,v);
        res.push_back({u,v});
        return res;
    }
}

//const ll MAXN = 1e5;
ll n,m,q;
ll c[MAXN+100];
//ll range[MAXN+100];
struct range{
    ll l,r,w;
};
vector <range> all;
ll cnt[MAXN+100];
ll ans[MAXN+100];
int main(){
    ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
    cin>>n>>m>>q;
    hld::init(n);
    for (ll i = 1;i <= m;i ++){
        cin>>c[i];
    }
//    cout<<"OK"<<endl;
    vector <range> sus;
    for (ll i = 1;i + 1 <= m;i ++){
        vector <pll> tmp = hld::path(c[i],c[i+1]);
//        cout<<i<<endl;
        for (auto x:tmp){
//            cout<<x.fi<<' '<<x.se<<endl;
            sus.push_back({hld::in[x.fi],i,1});
            sus.push_back({hld::in[x.se]+1,i,-1});
        }
    }

    sort(sus.begin(),sus.end(),[=](range x, range y){return x.l < y.l;});
//    for (auto x:sus){
//        cout<<x.l<<' '<<x.se<<'\n';
//    }
    set <pll> s({MP(0,1),MP(m,1)});
    for (ll i = 1,ptr = 0;i <= n;i ++){
        vector <pll> all1;
        static bool vis[MAXN+100];
        while (ptr<sz(sus) && sus[ptr].l == i){
            if (!vis[sus[ptr].r]){
                all1.push_back(MP(sus[ptr].r,cnt[sus[ptr].r]));
                vis[sus[ptr].r] = 1;
            }
            cnt[sus[ptr].r] += sus[ptr].w;
            ptr++;
        }
        for (auto x:all1){
            if (vis[x.fi]){
                if (cnt[x.fi] != x.se){
                    if (cnt[x.fi] == 0){
                        auto mid = s.lower_bound(MP(x.fi,0));
                        auto l = prev(mid);
                        auto r = next(mid);
                        if ((*l).se-i!=0)all.push_back({(*l).fi + 1,(*mid).fi,(*l).se-i});
                        if ((*mid).se-i!=0)all.push_back({(*mid).fi + 1,(*r).fi,(*mid).se-i});
                        pll add = MP((*l).fi,i);
                        s.erase(l);
                        s.erase(mid);
                        s.insert(add);
//                        cout<<"WOW "<<endl;
                    }
                    else{
                        auto mid = s.insert(MP(x.fi,i)).fi;
                        auto l = prev(mid);
                        auto r = next(mid);
                        if ((*l).se-i!=0)all.push_back({(*l).fi + 1,(*r).fi,(*l).se-i});
                        pll add = MP((*l).fi,i);
                        s.erase(l);
                        s.insert(add);
                    }
                }
                vis[x.fi] = 0;
            }
        }
        if (i == n){
            for (auto it = s.begin();(*it).fi != m;it ++){
                all.push_back({(*it).fi+1,(*next(it)).fi,(*it).se-n-1});
            }
        }
//        for (auto x:s){
//            cout<<x.fi<<' ';
//        }
//        cout<<'\n';
    }
//    cout<<"OK"<<endl;
//    for (auto x:all)cout<<x.l<<' '<<x.r<<' '<<x.w<<'\n';
    sort(all.begin(),all.end(),[](range x,range y){return x.r > y.r;});
    vector <range> query;
    for (ll i = 1;i <= q;i ++){
        ll l,r;
        cin>>l>>r;
        query.push_back({l,r,i});
    }
    sort(query.begin(),query.end(),[](range x,range y){return x.r > y.r;});
    for (ll i = m,ptr1=0,ptr2=0;i >= 1;i --){
        while (ptr1 < sz(all) && all[ptr1].r == i){
            BIT::upd(all[ptr1].l,all[ptr1].w);
            ptr1++;
        }
        while (ptr2 < sz(query) && query[ptr2].r == i){
            ans[query[ptr2].w] = (query[ptr2].l == query[ptr2].r)?1-n:BIT::get(query[ptr2].l);
            ptr2++;
        }
    }
    for (ll i = 1;i <= q;i ++)cout<<(ans[i]+=n)<<'\n';
    cout<<'\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9816 KB Output is correct
4 Correct 3 ms 9820 KB Output is correct
5 Correct 3 ms 9820 KB Output is correct
6 Correct 3 ms 9820 KB Output is correct
7 Correct 3 ms 9844 KB Output is correct
8 Correct 3 ms 9816 KB Output is correct
9 Correct 3 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9820 KB Output is correct
12 Correct 3 ms 9820 KB Output is correct
13 Correct 3 ms 9924 KB Output is correct
14 Correct 3 ms 9820 KB Output is correct
15 Correct 3 ms 9820 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 3 ms 9816 KB Output is correct
19 Correct 3 ms 9816 KB Output is correct
20 Correct 2 ms 9816 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 3 ms 9912 KB Output is correct
23 Correct 3 ms 9820 KB Output is correct
24 Correct 3 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
26 Correct 3 ms 9820 KB Output is correct
27 Correct 2 ms 9716 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 3 ms 9908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9816 KB Output is correct
4 Correct 3 ms 9820 KB Output is correct
5 Correct 3 ms 9820 KB Output is correct
6 Correct 3 ms 9820 KB Output is correct
7 Correct 3 ms 9844 KB Output is correct
8 Correct 3 ms 9816 KB Output is correct
9 Correct 3 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9820 KB Output is correct
12 Correct 3 ms 9820 KB Output is correct
13 Correct 3 ms 9924 KB Output is correct
14 Correct 3 ms 9820 KB Output is correct
15 Correct 3 ms 9820 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 3 ms 9816 KB Output is correct
19 Correct 3 ms 9816 KB Output is correct
20 Correct 2 ms 9816 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 3 ms 9912 KB Output is correct
23 Correct 3 ms 9820 KB Output is correct
24 Correct 3 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
26 Correct 3 ms 9820 KB Output is correct
27 Correct 2 ms 9716 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 3 ms 9908 KB Output is correct
30 Correct 11 ms 11220 KB Output is correct
31 Correct 12 ms 11476 KB Output is correct
32 Correct 18 ms 12112 KB Output is correct
33 Correct 16 ms 11988 KB Output is correct
34 Correct 15 ms 11980 KB Output is correct
35 Correct 16 ms 12180 KB Output is correct
36 Correct 16 ms 12152 KB Output is correct
37 Correct 17 ms 12228 KB Output is correct
38 Correct 5 ms 10268 KB Output is correct
39 Correct 5 ms 10360 KB Output is correct
40 Correct 5 ms 10268 KB Output is correct
41 Correct 5 ms 10340 KB Output is correct
42 Correct 6 ms 10268 KB Output is correct
43 Correct 5 ms 10268 KB Output is correct
44 Correct 8 ms 10836 KB Output is correct
45 Correct 10 ms 10964 KB Output is correct
46 Correct 10 ms 10964 KB Output is correct
47 Correct 9 ms 10964 KB Output is correct
48 Correct 9 ms 10964 KB Output is correct
49 Correct 9 ms 11024 KB Output is correct
50 Correct 6 ms 10572 KB Output is correct
51 Correct 8 ms 10588 KB Output is correct
52 Correct 6 ms 10588 KB Output is correct
53 Correct 6 ms 10728 KB Output is correct
54 Correct 6 ms 10588 KB Output is correct
55 Correct 7 ms 10588 KB Output is correct
56 Correct 3 ms 10076 KB Output is correct
57 Correct 2 ms 9704 KB Output is correct
58 Correct 16 ms 12244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9552 KB Output is correct
2 Correct 2 ms 9816 KB Output is correct
3 Correct 3 ms 10076 KB Output is correct
4 Correct 166 ms 34748 KB Output is correct
5 Correct 116 ms 31936 KB Output is correct
6 Correct 143 ms 37432 KB Output is correct
7 Correct 181 ms 40252 KB Output is correct
8 Correct 171 ms 40664 KB Output is correct
9 Correct 181 ms 40720 KB Output is correct
10 Correct 173 ms 40292 KB Output is correct
11 Correct 170 ms 40628 KB Output is correct
12 Correct 123 ms 37920 KB Output is correct
13 Correct 123 ms 38400 KB Output is correct
14 Correct 127 ms 37744 KB Output is correct
15 Correct 41 ms 23248 KB Output is correct
16 Correct 167 ms 40372 KB Output is correct
17 Correct 93 ms 33916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 594 ms 102348 KB Output is correct
3 Correct 1000 ms 157076 KB Output is correct
4 Correct 832 ms 140948 KB Output is correct
5 Correct 1196 ms 171720 KB Output is correct
6 Correct 1284 ms 167920 KB Output is correct
7 Correct 1195 ms 177352 KB Output is correct
8 Correct 1205 ms 172308 KB Output is correct
9 Correct 1330 ms 178068 KB Output is correct
10 Correct 1232 ms 171668 KB Output is correct
11 Correct 1237 ms 170976 KB Output is correct
12 Correct 1322 ms 194328 KB Output is correct
13 Correct 1197 ms 172008 KB Output is correct
14 Correct 1186 ms 167652 KB Output is correct
15 Correct 1221 ms 186944 KB Output is correct
16 Correct 1248 ms 183864 KB Output is correct
17 Correct 1338 ms 193948 KB Output is correct
18 Correct 1223 ms 179764 KB Output is correct
19 Correct 1551 ms 204612 KB Output is correct
20 Correct 1533 ms 206940 KB Output is correct
21 Correct 1373 ms 174408 KB Output is correct
22 Correct 1349 ms 168712 KB Output is correct
23 Correct 1537 ms 206164 KB Output is correct
24 Correct 1725 ms 216092 KB Output is correct
25 Correct 1858 ms 223036 KB Output is correct
26 Correct 1468 ms 176152 KB Output is correct
27 Correct 1529 ms 206204 KB Output is correct
28 Correct 1674 ms 210860 KB Output is correct
29 Correct 1471 ms 174192 KB Output is correct
30 Correct 1470 ms 205420 KB Output is correct
31 Correct 1228 ms 161176 KB Output is correct
32 Correct 1421 ms 170988 KB Output is correct
33 Correct 1537 ms 202020 KB Output is correct
34 Correct 1800 ms 220204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 3 ms 10076 KB Output is correct
4 Correct 966 ms 150048 KB Output is correct
5 Correct 997 ms 150424 KB Output is correct
6 Correct 1108 ms 170552 KB Output is correct
7 Correct 1139 ms 174624 KB Output is correct
8 Correct 1134 ms 161940 KB Output is correct
9 Correct 1149 ms 174272 KB Output is correct
10 Correct 1164 ms 162288 KB Output is correct
11 Correct 1163 ms 162704 KB Output is correct
12 Correct 1159 ms 174584 KB Output is correct
13 Correct 1158 ms 174300 KB Output is correct
14 Correct 79 ms 31668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9816 KB Output is correct
4 Correct 3 ms 9820 KB Output is correct
5 Correct 3 ms 9820 KB Output is correct
6 Correct 3 ms 9820 KB Output is correct
7 Correct 3 ms 9844 KB Output is correct
8 Correct 3 ms 9816 KB Output is correct
9 Correct 3 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9820 KB Output is correct
12 Correct 3 ms 9820 KB Output is correct
13 Correct 3 ms 9924 KB Output is correct
14 Correct 3 ms 9820 KB Output is correct
15 Correct 3 ms 9820 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 3 ms 9816 KB Output is correct
19 Correct 3 ms 9816 KB Output is correct
20 Correct 2 ms 9816 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 3 ms 9912 KB Output is correct
23 Correct 3 ms 9820 KB Output is correct
24 Correct 3 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
26 Correct 3 ms 9820 KB Output is correct
27 Correct 2 ms 9716 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 3 ms 9908 KB Output is correct
30 Correct 11 ms 11220 KB Output is correct
31 Correct 12 ms 11476 KB Output is correct
32 Correct 18 ms 12112 KB Output is correct
33 Correct 16 ms 11988 KB Output is correct
34 Correct 15 ms 11980 KB Output is correct
35 Correct 16 ms 12180 KB Output is correct
36 Correct 16 ms 12152 KB Output is correct
37 Correct 17 ms 12228 KB Output is correct
38 Correct 5 ms 10268 KB Output is correct
39 Correct 5 ms 10360 KB Output is correct
40 Correct 5 ms 10268 KB Output is correct
41 Correct 5 ms 10340 KB Output is correct
42 Correct 6 ms 10268 KB Output is correct
43 Correct 5 ms 10268 KB Output is correct
44 Correct 8 ms 10836 KB Output is correct
45 Correct 10 ms 10964 KB Output is correct
46 Correct 10 ms 10964 KB Output is correct
47 Correct 9 ms 10964 KB Output is correct
48 Correct 9 ms 10964 KB Output is correct
49 Correct 9 ms 11024 KB Output is correct
50 Correct 6 ms 10572 KB Output is correct
51 Correct 8 ms 10588 KB Output is correct
52 Correct 6 ms 10588 KB Output is correct
53 Correct 6 ms 10728 KB Output is correct
54 Correct 6 ms 10588 KB Output is correct
55 Correct 7 ms 10588 KB Output is correct
56 Correct 3 ms 10076 KB Output is correct
57 Correct 2 ms 9704 KB Output is correct
58 Correct 16 ms 12244 KB Output is correct
59 Correct 2 ms 9552 KB Output is correct
60 Correct 2 ms 9816 KB Output is correct
61 Correct 3 ms 10076 KB Output is correct
62 Correct 166 ms 34748 KB Output is correct
63 Correct 116 ms 31936 KB Output is correct
64 Correct 143 ms 37432 KB Output is correct
65 Correct 181 ms 40252 KB Output is correct
66 Correct 171 ms 40664 KB Output is correct
67 Correct 181 ms 40720 KB Output is correct
68 Correct 173 ms 40292 KB Output is correct
69 Correct 170 ms 40628 KB Output is correct
70 Correct 123 ms 37920 KB Output is correct
71 Correct 123 ms 38400 KB Output is correct
72 Correct 127 ms 37744 KB Output is correct
73 Correct 41 ms 23248 KB Output is correct
74 Correct 167 ms 40372 KB Output is correct
75 Correct 93 ms 33916 KB Output is correct
76 Correct 2 ms 9564 KB Output is correct
77 Correct 594 ms 102348 KB Output is correct
78 Correct 1000 ms 157076 KB Output is correct
79 Correct 832 ms 140948 KB Output is correct
80 Correct 1196 ms 171720 KB Output is correct
81 Correct 1284 ms 167920 KB Output is correct
82 Correct 1195 ms 177352 KB Output is correct
83 Correct 1205 ms 172308 KB Output is correct
84 Correct 1330 ms 178068 KB Output is correct
85 Correct 1232 ms 171668 KB Output is correct
86 Correct 1237 ms 170976 KB Output is correct
87 Correct 1322 ms 194328 KB Output is correct
88 Correct 1197 ms 172008 KB Output is correct
89 Correct 1186 ms 167652 KB Output is correct
90 Correct 1221 ms 186944 KB Output is correct
91 Correct 1248 ms 183864 KB Output is correct
92 Correct 1338 ms 193948 KB Output is correct
93 Correct 1223 ms 179764 KB Output is correct
94 Correct 1551 ms 204612 KB Output is correct
95 Correct 1533 ms 206940 KB Output is correct
96 Correct 1373 ms 174408 KB Output is correct
97 Correct 1349 ms 168712 KB Output is correct
98 Correct 1537 ms 206164 KB Output is correct
99 Correct 1725 ms 216092 KB Output is correct
100 Correct 1858 ms 223036 KB Output is correct
101 Correct 1468 ms 176152 KB Output is correct
102 Correct 1529 ms 206204 KB Output is correct
103 Correct 1674 ms 210860 KB Output is correct
104 Correct 1471 ms 174192 KB Output is correct
105 Correct 1470 ms 205420 KB Output is correct
106 Correct 1228 ms 161176 KB Output is correct
107 Correct 1421 ms 170988 KB Output is correct
108 Correct 1537 ms 202020 KB Output is correct
109 Correct 1800 ms 220204 KB Output is correct
110 Correct 2 ms 9564 KB Output is correct
111 Correct 2 ms 9820 KB Output is correct
112 Correct 3 ms 10076 KB Output is correct
113 Correct 966 ms 150048 KB Output is correct
114 Correct 997 ms 150424 KB Output is correct
115 Correct 1108 ms 170552 KB Output is correct
116 Correct 1139 ms 174624 KB Output is correct
117 Correct 1134 ms 161940 KB Output is correct
118 Correct 1149 ms 174272 KB Output is correct
119 Correct 1164 ms 162288 KB Output is correct
120 Correct 1163 ms 162704 KB Output is correct
121 Correct 1159 ms 174584 KB Output is correct
122 Correct 1158 ms 174300 KB Output is correct
123 Correct 79 ms 31668 KB Output is correct
124 Correct 1012 ms 153500 KB Output is correct
125 Correct 797 ms 137612 KB Output is correct
126 Correct 1290 ms 170132 KB Output is correct
127 Correct 1222 ms 179864 KB Output is correct
128 Correct 1326 ms 169308 KB Output is correct
129 Correct 1206 ms 166580 KB Output is correct
130 Correct 1187 ms 176024 KB Output is correct
131 Correct 216 ms 40000 KB Output is correct
132 Correct 181 ms 39348 KB Output is correct
133 Correct 219 ms 48044 KB Output is correct
134 Correct 1569 ms 205400 KB Output is correct
135 Correct 1893 ms 226396 KB Output is correct
136 Correct 1920 ms 225508 KB Output is correct
137 Correct 240 ms 48792 KB Output is correct
138 Correct 242 ms 61016 KB Output is correct
139 Correct 240 ms 63200 KB Output is correct
140 Correct 246 ms 59752 KB Output is correct
141 Correct 275 ms 50896 KB Output is correct
142 Correct 240 ms 61688 KB Output is correct
143 Correct 48 ms 16432 KB Output is correct
144 Correct 1282 ms 169140 KB Output is correct