답안 #953952

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
953952 2024-03-27T01:32:52 Z dwuy Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 15464 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);
    }
 
    int update(int l, int r, int id, const int &u, const int &v, const int &val){
        if(l>v || r<u) return 0;
        if(l>=u && r<=v){
            int res = tree[id].cnt? 0 : l!=r? tree[id<<1].sum + tree[id<<1|1].sum : 1;
            tree[id].cnt += val;
            tree[id].sum = tree[id].cnt? 0 : l!=r? tree[id<<1].sum + tree[id<<1|1].sum : 1;
            return res;
        }
        int mid = (l + r)>>1;
        int ID = id<<1;
        int res = 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;
        return tree[id].cnt? 0 : res;
    }
 
    int update(int l, int r, int val){
        return update(1, n, 1, l, r, val);
    }
};
 
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 update(int u, int v, int val){
        if(u == 0) u = v;
        if(v == 0) v = u;
        int puv = LCA(u, v);
        int res = 0;
        while(head[u] != head[v]){
            if(h[head[u]] < h[head[v]]) swap(u, v);
            res += smt.update(num[head[u]], num[u], val);
            u = p[head[u]];
        }
        if(h[u] > h[v]) swap(u, v);
        res += smt.update(num[u], num[v], val);
        return res;
    }
}
 
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::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::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::update(int, int, int)':
tourism.cpp:163:13: warning: unused variable 'puv' [-Wunused-variable]
  163 |         int puv = LCA(u, v);
      |             ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 3 ms 4612 KB Output is correct
8 Correct 2 ms 4440 KB Output is correct
9 Correct 3 ms 4440 KB Output is correct
10 Correct 3 ms 4440 KB Output is correct
11 Correct 3 ms 4444 KB Output is correct
12 Correct 2 ms 4624 KB Output is correct
13 Correct 3 ms 4444 KB Output is correct
14 Correct 2 ms 4620 KB Output is correct
15 Correct 3 ms 4632 KB Output is correct
16 Correct 3 ms 4444 KB Output is correct
17 Correct 2 ms 4440 KB Output is correct
18 Correct 3 ms 4444 KB Output is correct
19 Correct 3 ms 4636 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 2 ms 4440 KB Output is correct
23 Correct 2 ms 4440 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 3 ms 4596 KB Output is correct
26 Correct 2 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 3 ms 4612 KB Output is correct
8 Correct 2 ms 4440 KB Output is correct
9 Correct 3 ms 4440 KB Output is correct
10 Correct 3 ms 4440 KB Output is correct
11 Correct 3 ms 4444 KB Output is correct
12 Correct 2 ms 4624 KB Output is correct
13 Correct 3 ms 4444 KB Output is correct
14 Correct 2 ms 4620 KB Output is correct
15 Correct 3 ms 4632 KB Output is correct
16 Correct 3 ms 4444 KB Output is correct
17 Correct 2 ms 4440 KB Output is correct
18 Correct 3 ms 4444 KB Output is correct
19 Correct 3 ms 4636 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 2 ms 4440 KB Output is correct
23 Correct 2 ms 4440 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 3 ms 4596 KB Output is correct
26 Correct 2 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4696 KB Output is correct
30 Correct 31 ms 4648 KB Output is correct
31 Correct 39 ms 4696 KB Output is correct
32 Correct 54 ms 4696 KB Output is correct
33 Correct 50 ms 4700 KB Output is correct
34 Correct 60 ms 4792 KB Output is correct
35 Correct 36 ms 4796 KB Output is correct
36 Correct 39 ms 4816 KB Output is correct
37 Correct 40 ms 4696 KB Output is correct
38 Correct 23 ms 4908 KB Output is correct
39 Correct 30 ms 4836 KB Output is correct
40 Correct 21 ms 4952 KB Output is correct
41 Correct 13 ms 4956 KB Output is correct
42 Correct 12 ms 4956 KB Output is correct
43 Correct 19 ms 4700 KB Output is correct
44 Correct 57 ms 4700 KB Output is correct
45 Correct 56 ms 4696 KB Output is correct
46 Correct 39 ms 4700 KB Output is correct
47 Correct 34 ms 4828 KB Output is correct
48 Correct 34 ms 4672 KB Output is correct
49 Correct 35 ms 4860 KB Output is correct
50 Correct 25 ms 4696 KB Output is correct
51 Correct 24 ms 4700 KB Output is correct
52 Correct 24 ms 4700 KB Output is correct
53 Correct 25 ms 4580 KB Output is correct
54 Correct 24 ms 4700 KB Output is correct
55 Correct 24 ms 4948 KB Output is correct
56 Correct 3 ms 4444 KB Output is correct
57 Correct 2 ms 4700 KB Output is correct
58 Correct 3 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Execution timed out 5103 ms 15196 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 162 ms 10336 KB Output is correct
3 Correct 271 ms 10868 KB Output is correct
4 Correct 198 ms 11344 KB Output is correct
5 Correct 177 ms 14416 KB Output is correct
6 Correct 166 ms 14484 KB Output is correct
7 Correct 159 ms 14416 KB Output is correct
8 Correct 185 ms 14576 KB Output is correct
9 Correct 204 ms 14480 KB Output is correct
10 Correct 368 ms 14476 KB Output is correct
11 Correct 381 ms 14432 KB Output is correct
12 Correct 406 ms 14560 KB Output is correct
13 Correct 420 ms 14672 KB Output is correct
14 Correct 406 ms 14816 KB Output is correct
15 Correct 249 ms 15464 KB Output is correct
16 Correct 303 ms 14824 KB Output is correct
17 Correct 299 ms 14672 KB Output is correct
18 Correct 292 ms 14816 KB Output is correct
19 Correct 162 ms 14728 KB Output is correct
20 Correct 164 ms 14420 KB Output is correct
21 Correct 177 ms 14392 KB Output is correct
22 Correct 174 ms 14416 KB Output is correct
23 Correct 161 ms 14404 KB Output is correct
24 Correct 197 ms 14480 KB Output is correct
25 Correct 263 ms 14424 KB Output is correct
26 Correct 273 ms 14488 KB Output is correct
27 Correct 388 ms 14660 KB Output is correct
28 Correct 430 ms 14560 KB Output is correct
29 Correct 444 ms 14420 KB Output is correct
30 Correct 459 ms 14672 KB Output is correct
31 Correct 464 ms 14848 KB Output is correct
32 Correct 471 ms 14928 KB Output is correct
33 Correct 427 ms 15072 KB Output is correct
34 Correct 244 ms 15408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Execution timed out 5033 ms 10780 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4444 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 3 ms 4612 KB Output is correct
8 Correct 2 ms 4440 KB Output is correct
9 Correct 3 ms 4440 KB Output is correct
10 Correct 3 ms 4440 KB Output is correct
11 Correct 3 ms 4444 KB Output is correct
12 Correct 2 ms 4624 KB Output is correct
13 Correct 3 ms 4444 KB Output is correct
14 Correct 2 ms 4620 KB Output is correct
15 Correct 3 ms 4632 KB Output is correct
16 Correct 3 ms 4444 KB Output is correct
17 Correct 2 ms 4440 KB Output is correct
18 Correct 3 ms 4444 KB Output is correct
19 Correct 3 ms 4636 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 2 ms 4440 KB Output is correct
23 Correct 2 ms 4440 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 3 ms 4596 KB Output is correct
26 Correct 2 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4696 KB Output is correct
30 Correct 31 ms 4648 KB Output is correct
31 Correct 39 ms 4696 KB Output is correct
32 Correct 54 ms 4696 KB Output is correct
33 Correct 50 ms 4700 KB Output is correct
34 Correct 60 ms 4792 KB Output is correct
35 Correct 36 ms 4796 KB Output is correct
36 Correct 39 ms 4816 KB Output is correct
37 Correct 40 ms 4696 KB Output is correct
38 Correct 23 ms 4908 KB Output is correct
39 Correct 30 ms 4836 KB Output is correct
40 Correct 21 ms 4952 KB Output is correct
41 Correct 13 ms 4956 KB Output is correct
42 Correct 12 ms 4956 KB Output is correct
43 Correct 19 ms 4700 KB Output is correct
44 Correct 57 ms 4700 KB Output is correct
45 Correct 56 ms 4696 KB Output is correct
46 Correct 39 ms 4700 KB Output is correct
47 Correct 34 ms 4828 KB Output is correct
48 Correct 34 ms 4672 KB Output is correct
49 Correct 35 ms 4860 KB Output is correct
50 Correct 25 ms 4696 KB Output is correct
51 Correct 24 ms 4700 KB Output is correct
52 Correct 24 ms 4700 KB Output is correct
53 Correct 25 ms 4580 KB Output is correct
54 Correct 24 ms 4700 KB Output is correct
55 Correct 24 ms 4948 KB Output is correct
56 Correct 3 ms 4444 KB Output is correct
57 Correct 2 ms 4700 KB Output is correct
58 Correct 3 ms 4700 KB Output is correct
59 Correct 1 ms 4440 KB Output is correct
60 Correct 1 ms 4444 KB Output is correct
61 Correct 3 ms 4444 KB Output is correct
62 Execution timed out 5103 ms 15196 KB Time limit exceeded
63 Halted 0 ms 0 KB -