Submission #953847

# Submission time Handle Problem Language Result Execution time Memory
953847 2024-03-26T18:00:32 Z dwuy Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 15444 KB
/**   - dwuy -

      />    フ
      |  _  _|
      /`ミ _x ノ
     /      |
    /   ヽ   ?
 / ̄|   | | |
 | ( ̄ヽ__ヽ_)_)
 \二つ

**/
#include <bits/stdc++.h>

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) int32_t(s.length())
#define MASK(k)(1LL<<(k))
#define TASK "test"

using namespace std;

typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;

const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 100005;

int n, m, q, bsz;
int c[MX], a[MX];
int answer[MX];
tpiii qr[MX];
vector<int> G[MX];

void nhap(){
    cin >> n >> m >> q;
    bsz = sqrt(m);
    for(int i=1; i<n; i++){
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for(int i=1; i<=m; i++) cin >> c[i];
    for(int i=1; i<=q; i++){
        int l, r;
        cin >> l >> r;
        qr[i] = {l, r, i};
    }
}

struct Node{
    int cnt, sum;

    Node(){
        this->cnt = 0;
        this->sum = 0;
    }
};

struct SMT{
    int n;
    vector<Node> tree;

    SMT(int n=0){
        this->n = n;
        this->tree.assign(n<<2|3, Node());
    }

    void build(int l, int r, int id){
        if(l == r){
            tree[id].sum = 1;
            tree[id].cnt = 0;
            return;
        }
        int mid = (l + r)>>1;
        int ID = id<<1;
        build(l, mid, ID);
        build(mid+1, r, ID|1);
        tree[id].cnt = 0;
        tree[id].sum = tree[ID].sum + tree[ID|1].sum;
    }

    void build(){
        build(1, n, 1);
    }

    void update(int l, int r, int id, const int &u, const int &v, const int &val){
        if(l>v || r<u) return;
        if(l>=u && r<=v){
            tree[id].cnt += val;
            tree[id].sum = tree[id].cnt? 0 : l!=r? tree[id<<1].sum + tree[id<<1|1].sum : 1;
            return;
        }
        int mid = (l + r)>>1;
        int ID = id<<1;
        update(l, mid, ID, u, v, val);
        update(mid+1, r, ID|1, u, v, val);
        tree[id].sum = tree[id].cnt? 0 : tree[ID].sum + tree[ID|1].sum;
    }

    void update(int l, int r, int val){
        update(1, n, 1, l, r, val);
    }

    int get(int l, int r, int id, const int &u, const int &v){
        if(l>v || r<u) return 0;
        if(l>=u && r<=v) return tree[id].sum;
        int mid = (l+r)>>1;
        if(tree[id].cnt) return 0;
        return get(l, mid, id<<1, u, v) + get(mid+1, r, id<<1|1, u, v);
    }

    int get(int l, int r){
        return get(1, n, 1, l, r);
    }
};

namespace HLD{
    int p[MX];
    int h[MX];
    int num[MX];
    int head[MX];
    int numC[MX];
    int heavy[MX];
    SMT smt(0);

    void dfs(int u){
        h[u] = h[p[u]] + 1;
        heavy[u] = -1;
        for(int v: G[u]) if(v!=p[u]){
            p[v] = u;
            dfs(v);
            numC[u] += numC[v];
            if(heavy[u] == -1 || numC[v] > numC[heavy[u]]) heavy[u] = v;
        }
    }

    int dfsID = 0;
    void dcp(int u, int head){
        num[u] = ++dfsID;
        HLD::head[u] = head;
        if(heavy[u] != -1) dcp(heavy[u], head);
        for(int v: G[u]) if(HLD::head[v] == 0){
            dcp(v, v);
        }
    }

    void build(){
        dfs(1);
        dcp(1, 1);
        smt = SMT(n);
    }

    int LCA(int u, int v){
        if(u==0 || v==0) return max(u, v);
        while(head[u] != head[v]){
            if(h[head[u]] < h[head[v]]) swap(u, v);
            u = p[head[u]];
        }
        return h[u] < h[v]? u : v;
    }

    int get(int u, int v){
        int res = 0;
        if(u == 0) u = v;
        if(v == 0) v = u;
        int puv = LCA(u, v);
        while(head[u] != head[v]){
            if(h[head[u]] < h[head[v]]) swap(u, v);
            res += smt.get(num[head[u]], num[u]);
            u = p[head[u]];
        }
        if(h[u] > h[v]) swap(u, v);
        res += smt.get(num[u], num[v]);
        return res;
    }

    void update(int u, int v, int val){
        if(u == 0) u = v;
        if(v == 0) v = u;
        int puv = LCA(u, v);
        while(head[u] != head[v]){
            if(h[head[u]] < h[head[v]]) swap(u, v);
            smt.update(num[head[u]], num[u], val);
            u = p[head[u]];
        }
        if(h[u] > h[v]) swap(u, v);
        smt.update(num[u], num[v], val);
    }
}

bool comp(const tpiii &a, const tpiii &b){
    int l = get<0>(a), r = get<1>(a);
    int u = get<0>(b), v = get<1>(b);
    return l/bsz != u/bsz? l < u : r < v;
}

void solve(){
    HLD::build();
    sort(qr+1, qr+1+q, comp);
    for(int i=1, j=0, st=0, p=0, ans=0; i<=q; i++){
        int l, r, id;
        tie(l, r, id) = qr[i];
        if(i==1 || l/bsz != get<0>(qr[i-1])/bsz){
            HLD::smt.build();
            j = st = bsz*(l/bsz + 1);
            ans = 0;
            p = 0;
        }
        while(j<=r){
            ans += HLD::get(c[j], p);            
            HLD::update(c[j], p, 1);
            p = HLD::LCA(p, c[j]);
            j++;
        }

        vector<int> tmp;
        tmp.push_back(p);
        int result = ans;
        for(int t=l; t<min(r+1, st); t++){
            result += HLD::get(c[t], tmp.back());
            HLD::update(c[t], tmp.back(), 1);
            tmp.push_back(HLD::LCA(c[t], tmp.back()));
        }
        tmp.pop_back();
        for(int t=min(r, st-1); t>=l; t--){
            HLD::update(c[t], tmp.back(), -1);
            tmp.pop_back();
        }
        answer[id] = result;
    }

    for(int i=1; i<=q; i++) cout << answer[i] << endl;
}

int32_t main(){
    fastIO;
    //file(TASK);

    nhap();
    solve();

    return 0;
}


Compilation message

tourism.cpp: In function 'int HLD::get(int, int)':
tourism.cpp:175:13: warning: unused variable 'puv' [-Wunused-variable]
  175 |         int puv = LCA(u, v);
      |             ^~~
tourism.cpp: In function 'void HLD::update(int, int, int)':
tourism.cpp:189:13: warning: unused variable 'puv' [-Wunused-variable]
  189 |         int puv = LCA(u, v);
      |             ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 3 ms 4568 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4440 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 4 ms 4444 KB Output is correct
10 Correct 3 ms 4444 KB Output is correct
11 Correct 3 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 2 ms 4580 KB Output is correct
15 Correct 2 ms 4440 KB Output is correct
16 Correct 3 ms 4444 KB Output is correct
17 Correct 2 ms 4636 KB Output is correct
18 Correct 4 ms 4444 KB Output is correct
19 Correct 3 ms 4640 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 3 ms 4444 KB Output is correct
23 Correct 4 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 3 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4568 KB Output is correct
29 Correct 2 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 3 ms 4568 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4440 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 4 ms 4444 KB Output is correct
10 Correct 3 ms 4444 KB Output is correct
11 Correct 3 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 2 ms 4580 KB Output is correct
15 Correct 2 ms 4440 KB Output is correct
16 Correct 3 ms 4444 KB Output is correct
17 Correct 2 ms 4636 KB Output is correct
18 Correct 4 ms 4444 KB Output is correct
19 Correct 3 ms 4640 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 3 ms 4444 KB Output is correct
23 Correct 4 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 3 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4568 KB Output is correct
29 Correct 2 ms 4444 KB Output is correct
30 Correct 39 ms 4704 KB Output is correct
31 Correct 54 ms 4576 KB Output is correct
32 Correct 62 ms 4696 KB Output is correct
33 Correct 81 ms 4700 KB Output is correct
34 Correct 56 ms 4700 KB Output is correct
35 Correct 36 ms 4816 KB Output is correct
36 Correct 39 ms 4700 KB Output is correct
37 Correct 39 ms 4696 KB Output is correct
38 Correct 30 ms 4700 KB Output is correct
39 Correct 33 ms 4696 KB Output is correct
40 Correct 19 ms 4920 KB Output is correct
41 Correct 11 ms 4956 KB Output is correct
42 Correct 10 ms 4952 KB Output is correct
43 Correct 25 ms 4700 KB Output is correct
44 Correct 65 ms 4856 KB Output is correct
45 Correct 59 ms 4824 KB Output is correct
46 Correct 41 ms 4700 KB Output is correct
47 Correct 28 ms 4700 KB Output is correct
48 Correct 31 ms 4696 KB Output is correct
49 Correct 32 ms 4836 KB Output is correct
50 Correct 29 ms 4696 KB Output is correct
51 Correct 30 ms 4700 KB Output is correct
52 Correct 29 ms 4700 KB Output is correct
53 Correct 30 ms 4700 KB Output is correct
54 Correct 32 ms 4696 KB Output is correct
55 Correct 30 ms 4576 KB Output is correct
56 Correct 4 ms 4440 KB Output is correct
57 Correct 3 ms 4560 KB Output is correct
58 Correct 3 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 4564 KB Output is correct
3 Correct 4 ms 4444 KB Output is correct
4 Execution timed out 5037 ms 15188 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 163 ms 10332 KB Output is correct
3 Correct 241 ms 10868 KB Output is correct
4 Correct 205 ms 11356 KB Output is correct
5 Correct 191 ms 14564 KB Output is correct
6 Correct 188 ms 14564 KB Output is correct
7 Correct 182 ms 14484 KB Output is correct
8 Correct 202 ms 14420 KB Output is correct
9 Correct 239 ms 14420 KB Output is correct
10 Correct 387 ms 14416 KB Output is correct
11 Correct 398 ms 14560 KB Output is correct
12 Correct 402 ms 14556 KB Output is correct
13 Correct 398 ms 14816 KB Output is correct
14 Correct 411 ms 14816 KB Output is correct
15 Correct 261 ms 15324 KB Output is correct
16 Correct 318 ms 14724 KB Output is correct
17 Correct 345 ms 14832 KB Output is correct
18 Correct 353 ms 14672 KB Output is correct
19 Correct 213 ms 14672 KB Output is correct
20 Correct 183 ms 14488 KB Output is correct
21 Correct 183 ms 14328 KB Output is correct
22 Correct 198 ms 14424 KB Output is correct
23 Correct 194 ms 14416 KB Output is correct
24 Correct 219 ms 14428 KB Output is correct
25 Correct 315 ms 14516 KB Output is correct
26 Correct 298 ms 14416 KB Output is correct
27 Correct 395 ms 14604 KB Output is correct
28 Correct 423 ms 14568 KB Output is correct
29 Correct 467 ms 14680 KB Output is correct
30 Correct 441 ms 14688 KB Output is correct
31 Correct 476 ms 14848 KB Output is correct
32 Correct 499 ms 14844 KB Output is correct
33 Correct 439 ms 15068 KB Output is correct
34 Correct 250 ms 15444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4452 KB Output is correct
3 Correct 4 ms 4444 KB Output is correct
4 Execution timed out 5009 ms 10752 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 3 ms 4568 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4440 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 4 ms 4444 KB Output is correct
10 Correct 3 ms 4444 KB Output is correct
11 Correct 3 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 2 ms 4580 KB Output is correct
15 Correct 2 ms 4440 KB Output is correct
16 Correct 3 ms 4444 KB Output is correct
17 Correct 2 ms 4636 KB Output is correct
18 Correct 4 ms 4444 KB Output is correct
19 Correct 3 ms 4640 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 3 ms 4444 KB Output is correct
23 Correct 4 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 3 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4568 KB Output is correct
29 Correct 2 ms 4444 KB Output is correct
30 Correct 39 ms 4704 KB Output is correct
31 Correct 54 ms 4576 KB Output is correct
32 Correct 62 ms 4696 KB Output is correct
33 Correct 81 ms 4700 KB Output is correct
34 Correct 56 ms 4700 KB Output is correct
35 Correct 36 ms 4816 KB Output is correct
36 Correct 39 ms 4700 KB Output is correct
37 Correct 39 ms 4696 KB Output is correct
38 Correct 30 ms 4700 KB Output is correct
39 Correct 33 ms 4696 KB Output is correct
40 Correct 19 ms 4920 KB Output is correct
41 Correct 11 ms 4956 KB Output is correct
42 Correct 10 ms 4952 KB Output is correct
43 Correct 25 ms 4700 KB Output is correct
44 Correct 65 ms 4856 KB Output is correct
45 Correct 59 ms 4824 KB Output is correct
46 Correct 41 ms 4700 KB Output is correct
47 Correct 28 ms 4700 KB Output is correct
48 Correct 31 ms 4696 KB Output is correct
49 Correct 32 ms 4836 KB Output is correct
50 Correct 29 ms 4696 KB Output is correct
51 Correct 30 ms 4700 KB Output is correct
52 Correct 29 ms 4700 KB Output is correct
53 Correct 30 ms 4700 KB Output is correct
54 Correct 32 ms 4696 KB Output is correct
55 Correct 30 ms 4576 KB Output is correct
56 Correct 4 ms 4440 KB Output is correct
57 Correct 3 ms 4560 KB Output is correct
58 Correct 3 ms 4700 KB Output is correct
59 Correct 1 ms 4444 KB Output is correct
60 Correct 2 ms 4564 KB Output is correct
61 Correct 4 ms 4444 KB Output is correct
62 Execution timed out 5037 ms 15188 KB Time limit exceeded
63 Halted 0 ms 0 KB -