Submission #958750

# Submission time Handle Problem Language Result Execution time Memory
958750 2024-04-06T14:14:06 Z hotboy2703 Tourism (JOI23_tourism) C++14
100 / 100
2292 ms 343508 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);
                        all.push_back({(*l).fi + 1,(*mid).fi,(*l).se-i});
                        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);
                        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 9560 KB Output is correct
2 Correct 2 ms 9560 KB Output is correct
3 Correct 2 ms 9564 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 9952 KB Output is correct
7 Correct 3 ms 10072 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 3 ms 10188 KB Output is correct
10 Correct 3 ms 10076 KB Output is correct
11 Correct 4 ms 10076 KB Output is correct
12 Correct 3 ms 10076 KB Output is correct
13 Correct 3 ms 10076 KB Output is correct
14 Correct 3 ms 10076 KB Output is correct
15 Correct 2 ms 9816 KB Output is correct
16 Correct 3 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 3 ms 9820 KB Output is correct
19 Correct 4 ms 9948 KB Output is correct
20 Correct 3 ms 9820 KB Output is correct
21 Correct 3 ms 9820 KB Output is correct
22 Correct 3 ms 9820 KB Output is correct
23 Correct 3 ms 9820 KB Output is correct
24 Correct 3 ms 9820 KB Output is correct
25 Correct 3 ms 9820 KB Output is correct
26 Correct 3 ms 10076 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 4 ms 10076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9560 KB Output is correct
3 Correct 2 ms 9564 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 9952 KB Output is correct
7 Correct 3 ms 10072 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 3 ms 10188 KB Output is correct
10 Correct 3 ms 10076 KB Output is correct
11 Correct 4 ms 10076 KB Output is correct
12 Correct 3 ms 10076 KB Output is correct
13 Correct 3 ms 10076 KB Output is correct
14 Correct 3 ms 10076 KB Output is correct
15 Correct 2 ms 9816 KB Output is correct
16 Correct 3 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 3 ms 9820 KB Output is correct
19 Correct 4 ms 9948 KB Output is correct
20 Correct 3 ms 9820 KB Output is correct
21 Correct 3 ms 9820 KB Output is correct
22 Correct 3 ms 9820 KB Output is correct
23 Correct 3 ms 9820 KB Output is correct
24 Correct 3 ms 9820 KB Output is correct
25 Correct 3 ms 9820 KB Output is correct
26 Correct 3 ms 10076 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 4 ms 10076 KB Output is correct
30 Correct 13 ms 12180 KB Output is correct
31 Correct 14 ms 12232 KB Output is correct
32 Correct 18 ms 13004 KB Output is correct
33 Correct 19 ms 13012 KB Output is correct
34 Correct 17 ms 13004 KB Output is correct
35 Correct 18 ms 13008 KB Output is correct
36 Correct 19 ms 13008 KB Output is correct
37 Correct 21 ms 13264 KB Output is correct
38 Correct 6 ms 10504 KB Output is correct
39 Correct 5 ms 10500 KB Output is correct
40 Correct 5 ms 10484 KB Output is correct
41 Correct 5 ms 10268 KB Output is correct
42 Correct 5 ms 10268 KB Output is correct
43 Correct 5 ms 10508 KB Output is correct
44 Correct 9 ms 11028 KB Output is correct
45 Correct 11 ms 11472 KB Output is correct
46 Correct 11 ms 11452 KB Output is correct
47 Correct 10 ms 11476 KB Output is correct
48 Correct 11 ms 11220 KB Output is correct
49 Correct 10 ms 11220 KB Output is correct
50 Correct 7 ms 11156 KB Output is correct
51 Correct 7 ms 11060 KB Output is correct
52 Correct 7 ms 11128 KB Output is correct
53 Correct 7 ms 11160 KB Output is correct
54 Correct 7 ms 11060 KB Output is correct
55 Correct 7 ms 11152 KB Output is correct
56 Correct 3 ms 10072 KB Output is correct
57 Correct 3 ms 9820 KB Output is correct
58 Correct 18 ms 13088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9648 KB Output is correct
3 Correct 3 ms 10076 KB Output is correct
4 Correct 159 ms 42404 KB Output is correct
5 Correct 106 ms 33728 KB Output is correct
6 Correct 139 ms 39268 KB Output is correct
7 Correct 178 ms 45848 KB Output is correct
8 Correct 176 ms 46216 KB Output is correct
9 Correct 183 ms 46524 KB Output is correct
10 Correct 180 ms 48260 KB Output is correct
11 Correct 176 ms 46524 KB Output is correct
12 Correct 126 ms 41712 KB Output is correct
13 Correct 128 ms 43216 KB Output is correct
14 Correct 126 ms 41668 KB Output is correct
15 Correct 40 ms 24752 KB Output is correct
16 Correct 172 ms 45304 KB Output is correct
17 Correct 85 ms 34828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 771 ms 152556 KB Output is correct
3 Correct 1236 ms 183096 KB Output is correct
4 Correct 988 ms 169912 KB Output is correct
5 Correct 1512 ms 296676 KB Output is correct
6 Correct 1479 ms 297776 KB Output is correct
7 Correct 1503 ms 298076 KB Output is correct
8 Correct 1480 ms 294988 KB Output is correct
9 Correct 1626 ms 302212 KB Output is correct
10 Correct 1559 ms 300024 KB Output is correct
11 Correct 1488 ms 299256 KB Output is correct
12 Correct 1628 ms 304528 KB Output is correct
13 Correct 1504 ms 299288 KB Output is correct
14 Correct 1480 ms 294960 KB Output is correct
15 Correct 1509 ms 295464 KB Output is correct
16 Correct 1520 ms 343508 KB Output is correct
17 Correct 1626 ms 305064 KB Output is correct
18 Correct 1477 ms 297708 KB Output is correct
19 Correct 1809 ms 303936 KB Output is correct
20 Correct 1862 ms 307920 KB Output is correct
21 Correct 1679 ms 299416 KB Output is correct
22 Correct 1656 ms 299236 KB Output is correct
23 Correct 1849 ms 307384 KB Output is correct
24 Correct 2051 ms 315500 KB Output is correct
25 Correct 2240 ms 323908 KB Output is correct
26 Correct 1757 ms 302672 KB Output is correct
27 Correct 1829 ms 305864 KB Output is correct
28 Correct 2056 ms 312556 KB Output is correct
29 Correct 1759 ms 302932 KB Output is correct
30 Correct 1778 ms 304436 KB Output is correct
31 Correct 1470 ms 288220 KB Output is correct
32 Correct 1724 ms 300080 KB Output is correct
33 Correct 1805 ms 303824 KB Output is correct
34 Correct 2168 ms 320900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9816 KB Output is correct
3 Correct 3 ms 10076 KB Output is correct
4 Correct 1134 ms 172576 KB Output is correct
5 Correct 1185 ms 178224 KB Output is correct
6 Correct 1335 ms 283436 KB Output is correct
7 Correct 1366 ms 335420 KB Output is correct
8 Correct 1375 ms 334920 KB Output is correct
9 Correct 1362 ms 288364 KB Output is correct
10 Correct 1369 ms 286288 KB Output is correct
11 Correct 1374 ms 287308 KB Output is correct
12 Correct 1361 ms 286992 KB Output is correct
13 Correct 1362 ms 288432 KB Output is correct
14 Correct 85 ms 36288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9560 KB Output is correct
3 Correct 2 ms 9564 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 9952 KB Output is correct
7 Correct 3 ms 10072 KB Output is correct
8 Correct 3 ms 9820 KB Output is correct
9 Correct 3 ms 10188 KB Output is correct
10 Correct 3 ms 10076 KB Output is correct
11 Correct 4 ms 10076 KB Output is correct
12 Correct 3 ms 10076 KB Output is correct
13 Correct 3 ms 10076 KB Output is correct
14 Correct 3 ms 10076 KB Output is correct
15 Correct 2 ms 9816 KB Output is correct
16 Correct 3 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 3 ms 9820 KB Output is correct
19 Correct 4 ms 9948 KB Output is correct
20 Correct 3 ms 9820 KB Output is correct
21 Correct 3 ms 9820 KB Output is correct
22 Correct 3 ms 9820 KB Output is correct
23 Correct 3 ms 9820 KB Output is correct
24 Correct 3 ms 9820 KB Output is correct
25 Correct 3 ms 9820 KB Output is correct
26 Correct 3 ms 10076 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 4 ms 10076 KB Output is correct
30 Correct 13 ms 12180 KB Output is correct
31 Correct 14 ms 12232 KB Output is correct
32 Correct 18 ms 13004 KB Output is correct
33 Correct 19 ms 13012 KB Output is correct
34 Correct 17 ms 13004 KB Output is correct
35 Correct 18 ms 13008 KB Output is correct
36 Correct 19 ms 13008 KB Output is correct
37 Correct 21 ms 13264 KB Output is correct
38 Correct 6 ms 10504 KB Output is correct
39 Correct 5 ms 10500 KB Output is correct
40 Correct 5 ms 10484 KB Output is correct
41 Correct 5 ms 10268 KB Output is correct
42 Correct 5 ms 10268 KB Output is correct
43 Correct 5 ms 10508 KB Output is correct
44 Correct 9 ms 11028 KB Output is correct
45 Correct 11 ms 11472 KB Output is correct
46 Correct 11 ms 11452 KB Output is correct
47 Correct 10 ms 11476 KB Output is correct
48 Correct 11 ms 11220 KB Output is correct
49 Correct 10 ms 11220 KB Output is correct
50 Correct 7 ms 11156 KB Output is correct
51 Correct 7 ms 11060 KB Output is correct
52 Correct 7 ms 11128 KB Output is correct
53 Correct 7 ms 11160 KB Output is correct
54 Correct 7 ms 11060 KB Output is correct
55 Correct 7 ms 11152 KB Output is correct
56 Correct 3 ms 10072 KB Output is correct
57 Correct 3 ms 9820 KB Output is correct
58 Correct 18 ms 13088 KB Output is correct
59 Correct 2 ms 9560 KB Output is correct
60 Correct 2 ms 9648 KB Output is correct
61 Correct 3 ms 10076 KB Output is correct
62 Correct 159 ms 42404 KB Output is correct
63 Correct 106 ms 33728 KB Output is correct
64 Correct 139 ms 39268 KB Output is correct
65 Correct 178 ms 45848 KB Output is correct
66 Correct 176 ms 46216 KB Output is correct
67 Correct 183 ms 46524 KB Output is correct
68 Correct 180 ms 48260 KB Output is correct
69 Correct 176 ms 46524 KB Output is correct
70 Correct 126 ms 41712 KB Output is correct
71 Correct 128 ms 43216 KB Output is correct
72 Correct 126 ms 41668 KB Output is correct
73 Correct 40 ms 24752 KB Output is correct
74 Correct 172 ms 45304 KB Output is correct
75 Correct 85 ms 34828 KB Output is correct
76 Correct 2 ms 9564 KB Output is correct
77 Correct 771 ms 152556 KB Output is correct
78 Correct 1236 ms 183096 KB Output is correct
79 Correct 988 ms 169912 KB Output is correct
80 Correct 1512 ms 296676 KB Output is correct
81 Correct 1479 ms 297776 KB Output is correct
82 Correct 1503 ms 298076 KB Output is correct
83 Correct 1480 ms 294988 KB Output is correct
84 Correct 1626 ms 302212 KB Output is correct
85 Correct 1559 ms 300024 KB Output is correct
86 Correct 1488 ms 299256 KB Output is correct
87 Correct 1628 ms 304528 KB Output is correct
88 Correct 1504 ms 299288 KB Output is correct
89 Correct 1480 ms 294960 KB Output is correct
90 Correct 1509 ms 295464 KB Output is correct
91 Correct 1520 ms 343508 KB Output is correct
92 Correct 1626 ms 305064 KB Output is correct
93 Correct 1477 ms 297708 KB Output is correct
94 Correct 1809 ms 303936 KB Output is correct
95 Correct 1862 ms 307920 KB Output is correct
96 Correct 1679 ms 299416 KB Output is correct
97 Correct 1656 ms 299236 KB Output is correct
98 Correct 1849 ms 307384 KB Output is correct
99 Correct 2051 ms 315500 KB Output is correct
100 Correct 2240 ms 323908 KB Output is correct
101 Correct 1757 ms 302672 KB Output is correct
102 Correct 1829 ms 305864 KB Output is correct
103 Correct 2056 ms 312556 KB Output is correct
104 Correct 1759 ms 302932 KB Output is correct
105 Correct 1778 ms 304436 KB Output is correct
106 Correct 1470 ms 288220 KB Output is correct
107 Correct 1724 ms 300080 KB Output is correct
108 Correct 1805 ms 303824 KB Output is correct
109 Correct 2168 ms 320900 KB Output is correct
110 Correct 2 ms 9560 KB Output is correct
111 Correct 2 ms 9816 KB Output is correct
112 Correct 3 ms 10076 KB Output is correct
113 Correct 1134 ms 172576 KB Output is correct
114 Correct 1185 ms 178224 KB Output is correct
115 Correct 1335 ms 283436 KB Output is correct
116 Correct 1366 ms 335420 KB Output is correct
117 Correct 1375 ms 334920 KB Output is correct
118 Correct 1362 ms 288364 KB Output is correct
119 Correct 1369 ms 286288 KB Output is correct
120 Correct 1374 ms 287308 KB Output is correct
121 Correct 1361 ms 286992 KB Output is correct
122 Correct 1362 ms 288432 KB Output is correct
123 Correct 85 ms 36288 KB Output is correct
124 Correct 1213 ms 182188 KB Output is correct
125 Correct 964 ms 166656 KB Output is correct
126 Correct 1583 ms 297644 KB Output is correct
127 Correct 1515 ms 292976 KB Output is correct
128 Correct 1569 ms 297524 KB Output is correct
129 Correct 1463 ms 295600 KB Output is correct
130 Correct 1430 ms 293540 KB Output is correct
131 Correct 208 ms 47236 KB Output is correct
132 Correct 191 ms 48332 KB Output is correct
133 Correct 235 ms 54800 KB Output is correct
134 Correct 1855 ms 306772 KB Output is correct
135 Correct 2292 ms 325452 KB Output is correct
136 Correct 2289 ms 327380 KB Output is correct
137 Correct 288 ms 87116 KB Output is correct
138 Correct 276 ms 74400 KB Output is correct
139 Correct 282 ms 85308 KB Output is correct
140 Correct 282 ms 84380 KB Output is correct
141 Correct 290 ms 74932 KB Output is correct
142 Correct 274 ms 85960 KB Output is correct
143 Correct 49 ms 18124 KB Output is correct
144 Correct 1564 ms 298596 KB Output is correct