답안 #1037849

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1037849 2024-07-29T09:09:31 Z 김은성(#10980) Tourism (JOI23_tourism) C++17
52 / 100
5000 ms 12956 KB
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[100009];
int par[100009], val[100009], ans[100009], depth[100009];
int tree[1<<18];
int a[100009], cur[100009], cnt = 0;
const int buc = 300;
struct Query{
    int idx, l, r;
    bool operator<(Query &q){
        if(l/buc == q.l/buc)
            return r < q.r;
        return l < q.l;
    }
};
void dfs(int v){
    //printf("v=%d\n", v);
    for(int i=0; i<graph[v].size(); i++){
        int u = graph[v][i];
        if(u != par[v]){
            par[u] = v;
            depth[u] = depth[v] + 1;
            dfs(u);
        }
    }
}
int lca(int u, int v){
    if(u==-1)
        return v;
    if(v==-1)
        return u;
   // printf("init u=%d v=%d du=%d dv=%d\n", u, v,depth[u], depth[v]);
    if(depth[u] > depth[v])
        swap(u, v);
   // printf("middle u=%d v=%d\n", u, v);
    while(depth[u] < depth[v])
        v = par[v];
   // printf("u=%d v=%d\n", u, v);
    while(u != v){
        u = par[u];
        v = par[v];
    }
    return v;
}
void settree(int v, int l, int r){
    if(l==r)
        tree[v] = a[l];
    else{
        int mid = (l+r)/2;
        settree(2*v, l, mid);
        settree(2*v+1, mid+1, r);
        tree[v] = lca(tree[2*v], tree[2*v+1]);
    }
}
int query(int v, int l, int r, int s, int e){
    if(s<=l && r<=e)
        return tree[v];
    if(e<l || r<s)
        return -1;
    int mid = (l+r)/2;
    return lca(query(2*v, l, mid, s, e), query(2*v+1, mid+1, r, s, e));
}
void update(int v, int val){
    while(v){
        if(val==1 && cur[v] == 0)
            cnt++;
        else if(val==-1 && cur[v]==1)
            cnt--;
        cur[v] += val;
        v = par[v];
    }
}
int main(){
    int n, m, q, i, u, v;
    scanf("%d %d %d", &n, &m, &q);
    for(i=0; i<n-1; i++){
        scanf("%d %d", &u, &v);
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    for(i=1; i<=m; i++){
        scanf("%d", &a[i]);
    }
    dfs(1);
    settree(1, 1, m);
    vector<Query> qu;
    for(i=0; i<q; i++){
        scanf("%d %d", &u, &v);
        qu.push_back({i, u, v});
        //printf("lca=%d\n", query(1, 1, m, u, v));
        ans[i] = -depth[query(1, 1, m, u, v)];
    }
    sort(qu.begin(), qu.end());
    int curl = 1, curr = 0;
    for(i=0; i<q; i++){
        int l = qu[i].l, r = qu[i].r;
        while(curr < r){
            update(a[++curr], 1);
        }
        while(curl > l){
            update(a[--curl], 1);
        }
        while(curr > r){
            update(a[curr--], -1);
        }
        while(curl < l){
            update(a[curl++], -1);
        }
        ans[qu[i].idx] += cnt;
    }
    for(i=0; i<q; i++)
        printf("%d\n", ans[i]);
    return 0;
}

Compilation message

tourism.cpp: In function 'void dfs(int)':
tourism.cpp:18:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for(int i=0; i<graph[v].size(); i++){
      |                  ~^~~~~~~~~~~~~~~~
tourism.cpp: In function 'int main()':
tourism.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     scanf("%d %d %d", &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         scanf("%d %d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~
tourism.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         scanf("%d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~
tourism.cpp:88:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |         scanf("%d %d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 1 ms 5980 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 2 ms 5980 KB Output is correct
7 Correct 1 ms 5980 KB Output is correct
8 Correct 1 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5980 KB Output is correct
11 Correct 2 ms 5980 KB Output is correct
12 Correct 1 ms 5980 KB Output is correct
13 Correct 1 ms 5980 KB Output is correct
14 Correct 1 ms 5980 KB Output is correct
15 Correct 5 ms 5980 KB Output is correct
16 Correct 6 ms 6012 KB Output is correct
17 Correct 7 ms 6016 KB Output is correct
18 Correct 4 ms 5980 KB Output is correct
19 Correct 7 ms 5976 KB Output is correct
20 Correct 4 ms 6008 KB Output is correct
21 Correct 2 ms 5980 KB Output is correct
22 Correct 2 ms 5976 KB Output is correct
23 Correct 2 ms 5980 KB Output is correct
24 Correct 1 ms 5980 KB Output is correct
25 Correct 1 ms 5980 KB Output is correct
26 Correct 1 ms 5980 KB Output is correct
27 Correct 1 ms 5976 KB Output is correct
28 Correct 1 ms 5980 KB Output is correct
29 Correct 1 ms 5980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 1 ms 5980 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 2 ms 5980 KB Output is correct
7 Correct 1 ms 5980 KB Output is correct
8 Correct 1 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5980 KB Output is correct
11 Correct 2 ms 5980 KB Output is correct
12 Correct 1 ms 5980 KB Output is correct
13 Correct 1 ms 5980 KB Output is correct
14 Correct 1 ms 5980 KB Output is correct
15 Correct 5 ms 5980 KB Output is correct
16 Correct 6 ms 6012 KB Output is correct
17 Correct 7 ms 6016 KB Output is correct
18 Correct 4 ms 5980 KB Output is correct
19 Correct 7 ms 5976 KB Output is correct
20 Correct 4 ms 6008 KB Output is correct
21 Correct 2 ms 5980 KB Output is correct
22 Correct 2 ms 5976 KB Output is correct
23 Correct 2 ms 5980 KB Output is correct
24 Correct 1 ms 5980 KB Output is correct
25 Correct 1 ms 5980 KB Output is correct
26 Correct 1 ms 5980 KB Output is correct
27 Correct 1 ms 5976 KB Output is correct
28 Correct 1 ms 5980 KB Output is correct
29 Correct 1 ms 5980 KB Output is correct
30 Correct 5 ms 5980 KB Output is correct
31 Correct 9 ms 5980 KB Output is correct
32 Correct 9 ms 6096 KB Output is correct
33 Correct 11 ms 5980 KB Output is correct
34 Correct 9 ms 5980 KB Output is correct
35 Correct 3 ms 5980 KB Output is correct
36 Correct 3 ms 5980 KB Output is correct
37 Correct 3 ms 5980 KB Output is correct
38 Correct 459 ms 6160 KB Output is correct
39 Correct 485 ms 5980 KB Output is correct
40 Correct 510 ms 5976 KB Output is correct
41 Correct 112 ms 6180 KB Output is correct
42 Correct 123 ms 6192 KB Output is correct
43 Correct 71 ms 6152 KB Output is correct
44 Correct 266 ms 5980 KB Output is correct
45 Correct 135 ms 5980 KB Output is correct
46 Correct 340 ms 5980 KB Output is correct
47 Correct 40 ms 5976 KB Output is correct
48 Correct 31 ms 5980 KB Output is correct
49 Correct 38 ms 5976 KB Output is correct
50 Correct 4 ms 5976 KB Output is correct
51 Correct 3 ms 5980 KB Output is correct
52 Correct 3 ms 5980 KB Output is correct
53 Correct 4 ms 5976 KB Output is correct
54 Correct 5 ms 5980 KB Output is correct
55 Correct 4 ms 5980 KB Output is correct
56 Correct 2 ms 5980 KB Output is correct
57 Correct 2 ms 6024 KB Output is correct
58 Correct 3 ms 5980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Execution timed out 5040 ms 11660 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 28 ms 7780 KB Output is correct
3 Correct 37 ms 8028 KB Output is correct
4 Correct 34 ms 8276 KB Output is correct
5 Correct 43 ms 9308 KB Output is correct
6 Correct 50 ms 9396 KB Output is correct
7 Correct 54 ms 9296 KB Output is correct
8 Correct 58 ms 9252 KB Output is correct
9 Correct 55 ms 9300 KB Output is correct
10 Correct 58 ms 9300 KB Output is correct
11 Correct 52 ms 9188 KB Output is correct
12 Correct 56 ms 9468 KB Output is correct
13 Correct 56 ms 9492 KB Output is correct
14 Correct 65 ms 9804 KB Output is correct
15 Correct 79 ms 10688 KB Output is correct
16 Correct 58 ms 9492 KB Output is correct
17 Correct 55 ms 9492 KB Output is correct
18 Correct 58 ms 9488 KB Output is correct
19 Correct 994 ms 9220 KB Output is correct
20 Correct 1284 ms 9376 KB Output is correct
21 Correct 1137 ms 9300 KB Output is correct
22 Correct 1339 ms 9296 KB Output is correct
23 Correct 1514 ms 9360 KB Output is correct
24 Correct 1316 ms 9196 KB Output is correct
25 Correct 1257 ms 9188 KB Output is correct
26 Correct 1907 ms 9224 KB Output is correct
27 Correct 1430 ms 9352 KB Output is correct
28 Correct 1271 ms 9296 KB Output is correct
29 Correct 1739 ms 9300 KB Output is correct
30 Correct 2230 ms 9556 KB Output is correct
31 Correct 1392 ms 9496 KB Output is correct
32 Correct 1649 ms 9756 KB Output is correct
33 Correct 1199 ms 10188 KB Output is correct
34 Correct 2271 ms 10928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 1528 ms 9636 KB Output is correct
5 Correct 1568 ms 10872 KB Output is correct
6 Correct 2105 ms 12104 KB Output is correct
7 Correct 2345 ms 12740 KB Output is correct
8 Correct 2417 ms 12884 KB Output is correct
9 Correct 2367 ms 12956 KB Output is correct
10 Correct 2317 ms 12884 KB Output is correct
11 Correct 2375 ms 12912 KB Output is correct
12 Correct 2396 ms 12756 KB Output is correct
13 Correct 2397 ms 12792 KB Output is correct
14 Correct 106 ms 8304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 1 ms 5980 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 2 ms 5980 KB Output is correct
7 Correct 1 ms 5980 KB Output is correct
8 Correct 1 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5980 KB Output is correct
11 Correct 2 ms 5980 KB Output is correct
12 Correct 1 ms 5980 KB Output is correct
13 Correct 1 ms 5980 KB Output is correct
14 Correct 1 ms 5980 KB Output is correct
15 Correct 5 ms 5980 KB Output is correct
16 Correct 6 ms 6012 KB Output is correct
17 Correct 7 ms 6016 KB Output is correct
18 Correct 4 ms 5980 KB Output is correct
19 Correct 7 ms 5976 KB Output is correct
20 Correct 4 ms 6008 KB Output is correct
21 Correct 2 ms 5980 KB Output is correct
22 Correct 2 ms 5976 KB Output is correct
23 Correct 2 ms 5980 KB Output is correct
24 Correct 1 ms 5980 KB Output is correct
25 Correct 1 ms 5980 KB Output is correct
26 Correct 1 ms 5980 KB Output is correct
27 Correct 1 ms 5976 KB Output is correct
28 Correct 1 ms 5980 KB Output is correct
29 Correct 1 ms 5980 KB Output is correct
30 Correct 5 ms 5980 KB Output is correct
31 Correct 9 ms 5980 KB Output is correct
32 Correct 9 ms 6096 KB Output is correct
33 Correct 11 ms 5980 KB Output is correct
34 Correct 9 ms 5980 KB Output is correct
35 Correct 3 ms 5980 KB Output is correct
36 Correct 3 ms 5980 KB Output is correct
37 Correct 3 ms 5980 KB Output is correct
38 Correct 459 ms 6160 KB Output is correct
39 Correct 485 ms 5980 KB Output is correct
40 Correct 510 ms 5976 KB Output is correct
41 Correct 112 ms 6180 KB Output is correct
42 Correct 123 ms 6192 KB Output is correct
43 Correct 71 ms 6152 KB Output is correct
44 Correct 266 ms 5980 KB Output is correct
45 Correct 135 ms 5980 KB Output is correct
46 Correct 340 ms 5980 KB Output is correct
47 Correct 40 ms 5976 KB Output is correct
48 Correct 31 ms 5980 KB Output is correct
49 Correct 38 ms 5976 KB Output is correct
50 Correct 4 ms 5976 KB Output is correct
51 Correct 3 ms 5980 KB Output is correct
52 Correct 3 ms 5980 KB Output is correct
53 Correct 4 ms 5976 KB Output is correct
54 Correct 5 ms 5980 KB Output is correct
55 Correct 4 ms 5980 KB Output is correct
56 Correct 2 ms 5980 KB Output is correct
57 Correct 2 ms 6024 KB Output is correct
58 Correct 3 ms 5980 KB Output is correct
59 Correct 1 ms 5980 KB Output is correct
60 Correct 1 ms 5980 KB Output is correct
61 Correct 2 ms 5980 KB Output is correct
62 Execution timed out 5040 ms 11660 KB Time limit exceeded
63 Halted 0 ms 0 KB -