답안 #831308

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
831308 2023-08-20T04:29:16 Z Cookie Tourism (JOI23_tourism) C++14
100 / 100
879 ms 22628 KB
#include<bits/stdc++.h>
#include<fstream>
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
//ifstream fin("FEEDING.INP");
//ofstream fout("FEEDING.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const int base = 74;
const int mxn = 1e5 + 5;
int n, m, q;
vt<int>adj[mxn + 5];
int sz[mxn + 1], pa[mxn + 1], head[mxn + 1], id[mxn + 1], tin = 0, c[mxn + 1], neg = -69420, inf = 1e9;
int ans[mxn + 1];
struct BIT{
    int bit[mxn + 1];
    void upd(int p, int v){
        while(p <= m){
            bit[p] += v; p += p & (-p);
        }
    }
    int get(int p){
        int ans = 0;
        while(p){
            ans += bit[p]; p -= p & (-p);
        }
        return(ans);
    }
};

void dfs(int s, int pre){
    sz[s] = 1;
    for(auto i: adj[s]){
        if(i != pre){
            pa[i] = s;
            dfs(i, s); sz[s] += sz[i];
        }
    }
}
void hld(int s, int pre, int group){
    head[s] = group; id[s] = ++tin;
    int mx = 0, id = -1;
    for(auto i: adj[s]){
        if(i != pre && sz[i] > mx){
            mx = sz[i]; id = i;
        }
    }
    if(!mx)return;
    hld(id, s, group); 
    for(auto i: adj[s]){
        if(i != pre && i != id){
            hld(i, s, i);
        }
    }
}
 map<int, int>pos;
vt<pii>hld_path(int u, int v){
    vt<pii>path;
    while(head[u] != head[v]){
        if(id[head[u]] < id[head[v]])swap(u, v);
       
        path.pb(make_pair(id[head[u]], id[u]));
        
        u = pa[head[u]];
    }
    
    if(id[u] < id[v]){
        path.pb(make_pair(id[u], id[v]));
    }else{
        path.pb(make_pair(id[v], id[u]));
    }
    return(path);
}
vt<pii>queries[mxn + 1];
BIT bit;
signed main()
{
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m >> q;
    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        adj[u].pb(v); adj[v].pb(u);
    }
    for(int i = 1; i <= m; i++){
        cin >> c[i];
    }
    for(int i = 0; i < q; i++){
        int l, r; cin >> l >> r;
        queries[l].pb(make_pair(r, i));
        if(l == r)ans[i]++;
    }
    dfs(1, -1);
    hld(1, -1, 1);
    
    pos[-1] = neg; pos[0] = inf; pos[n + 2] = neg;
    for(int i = m - 1; i >= 1; i--){
        
            vt<pii>path = hld_path(c[i], c[i + 1]);
            
            for(int j = 0; j < sz(path); j++){
                auto [l, r] = path[j];
                
                for(int tmp: {l, r + 1}){
                    if(pos.count(tmp))continue;
                    auto it = prev(pos.lower_bound(tmp));
                
                    pos[tmp] = (*it).second;
                    
                }
                
                while(1){
                    auto it = pos.lower_bound(l);
                    int po = (*it).first, val = (*it).second;
                    if(po == r + 1)break;
                    auto it2 = next(it);
                    if(val != inf){
                        int po2 = (*it2).first;
                        bit.upd(val, -(po2 - po));
                    }
                    pos.erase(it);
                    
                }
                
                pos[l] = i;
                bit.upd(i, r - l + 1);
            }
        
        
        for(auto [r, id]: queries[i]){
            ans[id] += bit.get(r - 1);
        }
    }
    for(int i = 0; i < q; i++){
        cout << ans[i] << "\n";
    }
    return(0);
}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:112:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  112 |                 auto [l, r] = path[j];
      |                      ^
tourism.cpp:140:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  140 |         for(auto [r, id]: queries[i]){
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 5112 KB Output is correct
4 Correct 3 ms 5028 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 4 ms 5036 KB Output is correct
8 Correct 4 ms 5028 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 5 ms 5028 KB Output is correct
12 Correct 3 ms 5036 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5036 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5036 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5036 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5032 KB Output is correct
27 Correct 2 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 5036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 5112 KB Output is correct
4 Correct 3 ms 5028 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 4 ms 5036 KB Output is correct
8 Correct 4 ms 5028 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 5 ms 5028 KB Output is correct
12 Correct 3 ms 5036 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5036 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5036 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5036 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5032 KB Output is correct
27 Correct 2 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 5036 KB Output is correct
30 Correct 7 ms 5204 KB Output is correct
31 Correct 8 ms 5220 KB Output is correct
32 Correct 9 ms 5304 KB Output is correct
33 Correct 10 ms 5232 KB Output is correct
34 Correct 10 ms 5300 KB Output is correct
35 Correct 9 ms 5252 KB Output is correct
36 Correct 9 ms 5292 KB Output is correct
37 Correct 8 ms 5204 KB Output is correct
38 Correct 5 ms 5296 KB Output is correct
39 Correct 6 ms 5296 KB Output is correct
40 Correct 4 ms 5232 KB Output is correct
41 Correct 6 ms 5332 KB Output is correct
42 Correct 5 ms 5296 KB Output is correct
43 Correct 4 ms 5296 KB Output is correct
44 Correct 5 ms 5204 KB Output is correct
45 Correct 5 ms 5204 KB Output is correct
46 Correct 5 ms 5204 KB Output is correct
47 Correct 5 ms 5232 KB Output is correct
48 Correct 6 ms 5236 KB Output is correct
49 Correct 6 ms 5204 KB Output is correct
50 Correct 5 ms 5332 KB Output is correct
51 Correct 5 ms 5332 KB Output is correct
52 Correct 5 ms 5332 KB Output is correct
53 Correct 7 ms 5296 KB Output is correct
54 Correct 6 ms 5320 KB Output is correct
55 Correct 6 ms 5292 KB Output is correct
56 Correct 4 ms 5040 KB Output is correct
57 Correct 3 ms 5160 KB Output is correct
58 Correct 10 ms 5312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 88 ms 14064 KB Output is correct
5 Correct 72 ms 18128 KB Output is correct
6 Correct 94 ms 19540 KB Output is correct
7 Correct 112 ms 22496 KB Output is correct
8 Correct 127 ms 22560 KB Output is correct
9 Correct 111 ms 22492 KB Output is correct
10 Correct 112 ms 22628 KB Output is correct
11 Correct 96 ms 22588 KB Output is correct
12 Correct 103 ms 21952 KB Output is correct
13 Correct 125 ms 21920 KB Output is correct
14 Correct 101 ms 21844 KB Output is correct
15 Correct 38 ms 18996 KB Output is correct
16 Correct 113 ms 22296 KB Output is correct
17 Correct 53 ms 9660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 278 ms 10800 KB Output is correct
3 Correct 433 ms 11932 KB Output is correct
4 Correct 346 ms 12076 KB Output is correct
5 Correct 555 ms 15448 KB Output is correct
6 Correct 580 ms 15424 KB Output is correct
7 Correct 534 ms 15492 KB Output is correct
8 Correct 540 ms 15460 KB Output is correct
9 Correct 544 ms 15400 KB Output is correct
10 Correct 529 ms 15400 KB Output is correct
11 Correct 572 ms 15504 KB Output is correct
12 Correct 533 ms 15544 KB Output is correct
13 Correct 540 ms 15964 KB Output is correct
14 Correct 535 ms 16916 KB Output is correct
15 Correct 573 ms 20344 KB Output is correct
16 Correct 506 ms 15928 KB Output is correct
17 Correct 569 ms 15992 KB Output is correct
18 Correct 507 ms 15888 KB Output is correct
19 Correct 301 ms 12556 KB Output is correct
20 Correct 294 ms 12512 KB Output is correct
21 Correct 330 ms 12592 KB Output is correct
22 Correct 301 ms 12540 KB Output is correct
23 Correct 291 ms 12536 KB Output is correct
24 Correct 306 ms 12464 KB Output is correct
25 Correct 290 ms 12412 KB Output is correct
26 Correct 297 ms 12680 KB Output is correct
27 Correct 301 ms 12476 KB Output is correct
28 Correct 295 ms 12548 KB Output is correct
29 Correct 305 ms 12616 KB Output is correct
30 Correct 343 ms 12800 KB Output is correct
31 Correct 313 ms 13000 KB Output is correct
32 Correct 303 ms 13500 KB Output is correct
33 Correct 322 ms 14964 KB Output is correct
34 Correct 312 ms 17424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 5080 KB Output is correct
4 Correct 690 ms 15396 KB Output is correct
5 Correct 705 ms 15548 KB Output is correct
6 Correct 815 ms 18460 KB Output is correct
7 Correct 850 ms 19824 KB Output is correct
8 Correct 876 ms 19860 KB Output is correct
9 Correct 878 ms 19796 KB Output is correct
10 Correct 879 ms 19888 KB Output is correct
11 Correct 875 ms 19828 KB Output is correct
12 Correct 855 ms 19812 KB Output is correct
13 Correct 863 ms 19844 KB Output is correct
14 Correct 45 ms 9612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 5112 KB Output is correct
4 Correct 3 ms 5028 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 4 ms 5036 KB Output is correct
8 Correct 4 ms 5028 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 5 ms 5028 KB Output is correct
12 Correct 3 ms 5036 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5036 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5036 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5036 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 5032 KB Output is correct
27 Correct 2 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 5036 KB Output is correct
30 Correct 7 ms 5204 KB Output is correct
31 Correct 8 ms 5220 KB Output is correct
32 Correct 9 ms 5304 KB Output is correct
33 Correct 10 ms 5232 KB Output is correct
34 Correct 10 ms 5300 KB Output is correct
35 Correct 9 ms 5252 KB Output is correct
36 Correct 9 ms 5292 KB Output is correct
37 Correct 8 ms 5204 KB Output is correct
38 Correct 5 ms 5296 KB Output is correct
39 Correct 6 ms 5296 KB Output is correct
40 Correct 4 ms 5232 KB Output is correct
41 Correct 6 ms 5332 KB Output is correct
42 Correct 5 ms 5296 KB Output is correct
43 Correct 4 ms 5296 KB Output is correct
44 Correct 5 ms 5204 KB Output is correct
45 Correct 5 ms 5204 KB Output is correct
46 Correct 5 ms 5204 KB Output is correct
47 Correct 5 ms 5232 KB Output is correct
48 Correct 6 ms 5236 KB Output is correct
49 Correct 6 ms 5204 KB Output is correct
50 Correct 5 ms 5332 KB Output is correct
51 Correct 5 ms 5332 KB Output is correct
52 Correct 5 ms 5332 KB Output is correct
53 Correct 7 ms 5296 KB Output is correct
54 Correct 6 ms 5320 KB Output is correct
55 Correct 6 ms 5292 KB Output is correct
56 Correct 4 ms 5040 KB Output is correct
57 Correct 3 ms 5160 KB Output is correct
58 Correct 10 ms 5312 KB Output is correct
59 Correct 2 ms 4948 KB Output is correct
60 Correct 2 ms 4948 KB Output is correct
61 Correct 3 ms 5076 KB Output is correct
62 Correct 88 ms 14064 KB Output is correct
63 Correct 72 ms 18128 KB Output is correct
64 Correct 94 ms 19540 KB Output is correct
65 Correct 112 ms 22496 KB Output is correct
66 Correct 127 ms 22560 KB Output is correct
67 Correct 111 ms 22492 KB Output is correct
68 Correct 112 ms 22628 KB Output is correct
69 Correct 96 ms 22588 KB Output is correct
70 Correct 103 ms 21952 KB Output is correct
71 Correct 125 ms 21920 KB Output is correct
72 Correct 101 ms 21844 KB Output is correct
73 Correct 38 ms 18996 KB Output is correct
74 Correct 113 ms 22296 KB Output is correct
75 Correct 53 ms 9660 KB Output is correct
76 Correct 2 ms 4948 KB Output is correct
77 Correct 278 ms 10800 KB Output is correct
78 Correct 433 ms 11932 KB Output is correct
79 Correct 346 ms 12076 KB Output is correct
80 Correct 555 ms 15448 KB Output is correct
81 Correct 580 ms 15424 KB Output is correct
82 Correct 534 ms 15492 KB Output is correct
83 Correct 540 ms 15460 KB Output is correct
84 Correct 544 ms 15400 KB Output is correct
85 Correct 529 ms 15400 KB Output is correct
86 Correct 572 ms 15504 KB Output is correct
87 Correct 533 ms 15544 KB Output is correct
88 Correct 540 ms 15964 KB Output is correct
89 Correct 535 ms 16916 KB Output is correct
90 Correct 573 ms 20344 KB Output is correct
91 Correct 506 ms 15928 KB Output is correct
92 Correct 569 ms 15992 KB Output is correct
93 Correct 507 ms 15888 KB Output is correct
94 Correct 301 ms 12556 KB Output is correct
95 Correct 294 ms 12512 KB Output is correct
96 Correct 330 ms 12592 KB Output is correct
97 Correct 301 ms 12540 KB Output is correct
98 Correct 291 ms 12536 KB Output is correct
99 Correct 306 ms 12464 KB Output is correct
100 Correct 290 ms 12412 KB Output is correct
101 Correct 297 ms 12680 KB Output is correct
102 Correct 301 ms 12476 KB Output is correct
103 Correct 295 ms 12548 KB Output is correct
104 Correct 305 ms 12616 KB Output is correct
105 Correct 343 ms 12800 KB Output is correct
106 Correct 313 ms 13000 KB Output is correct
107 Correct 303 ms 13500 KB Output is correct
108 Correct 322 ms 14964 KB Output is correct
109 Correct 312 ms 17424 KB Output is correct
110 Correct 3 ms 4948 KB Output is correct
111 Correct 2 ms 4948 KB Output is correct
112 Correct 3 ms 5080 KB Output is correct
113 Correct 690 ms 15396 KB Output is correct
114 Correct 705 ms 15548 KB Output is correct
115 Correct 815 ms 18460 KB Output is correct
116 Correct 850 ms 19824 KB Output is correct
117 Correct 876 ms 19860 KB Output is correct
118 Correct 878 ms 19796 KB Output is correct
119 Correct 879 ms 19888 KB Output is correct
120 Correct 875 ms 19828 KB Output is correct
121 Correct 855 ms 19812 KB Output is correct
122 Correct 863 ms 19844 KB Output is correct
123 Correct 45 ms 9612 KB Output is correct
124 Correct 519 ms 19024 KB Output is correct
125 Correct 370 ms 16748 KB Output is correct
126 Correct 586 ms 19664 KB Output is correct
127 Correct 582 ms 19596 KB Output is correct
128 Correct 610 ms 19604 KB Output is correct
129 Correct 621 ms 19592 KB Output is correct
130 Correct 614 ms 19576 KB Output is correct
131 Correct 144 ms 21524 KB Output is correct
132 Correct 124 ms 22408 KB Output is correct
133 Correct 116 ms 19428 KB Output is correct
134 Correct 351 ms 16696 KB Output is correct
135 Correct 359 ms 16640 KB Output is correct
136 Correct 348 ms 16588 KB Output is correct
137 Correct 224 ms 20736 KB Output is correct
138 Correct 240 ms 20700 KB Output is correct
139 Correct 258 ms 20696 KB Output is correct
140 Correct 246 ms 20712 KB Output is correct
141 Correct 242 ms 20700 KB Output is correct
142 Correct 255 ms 20648 KB Output is correct
143 Correct 60 ms 12764 KB Output is correct
144 Correct 581 ms 19272 KB Output is correct