답안 #848200

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
848200 2023-09-11T14:53:31 Z nguyentunglam Tourism (JOI23_tourism) C++17
100 / 100
259 ms 37564 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
#define all(v) v.begin(), v.end()

using namespace std;

const int N = 1e5 + 10;

int sz[N], par[N], heavy[N], bit[N], b[N], st[N], root[N], ans[N], off[N];

int timer, n, m, q;

pair<int, int> lines[20][N];

vector<pair<int, int> > S[N], query[N];

int mn[N << 2], mx[N << 2];

vector<int> adj[N];

void dfs(int u) {
    sz[u] = 1;
    for(int &v : adj[u]) if (v != par[u]) {
        par[v] = u;
        dfs(v);
        sz[u] += sz[v];
        if (sz[heavy[u]] < sz[v]) heavy[u] = v;
    }
}

void hld(int u) {
    st[u] = ++timer;
    if (!root[u]) root[u] = u;

    if (heavy[u]) {
        root[heavy[u]] = root[u];
        hld(heavy[u]);
    }

    for(int &v : adj[u]) if (v != par[u] && v != heavy[u]) hld(v);
}

void add(int pos, int val) {
    while (pos <= m) {
        bit[pos] += val;
        pos += pos & -pos;
    }
}

void addrange(int l, int r, int val) {
    add(l, val); add(r + 1, -val);
}

int get(int pos) {
    int ret = 0;
    while (pos) {
        ret += bit[pos];
        pos -= pos & -pos;
    }
    return ret;
}

void build(int s, int l, int r) {
    if (l == r) {
        mn[s] = mx[s] = b[l];
        return;
    }
    int mid = l + r >> 1;
    build(s << 1, l, mid);
    build(s << 1 | 1, mid + 1, r);
    mn[s] = min(mn[s << 1], mn[s << 1 | 1]);
    mx[s] = max(mx[s << 1], mx[s << 1 | 1]);
}

pair<int, int> get(int s, int l, int r, int from, int to) {
    if (l > to || r < from) return make_pair(1e9, -1e9);
    if (from <= l && r <= to) return make_pair(mn[s], mx[s]);
    int mid = l + r >> 1;
    int mn1, mx1; tie(mn1, mx1) = get(s << 1, l, mid, from, to);
    int mn2, mx2; tie(mn2, mx2) = get(s << 1 | 1, mid + 1, r, from, to);
    return make_pair(min(mn1, mn2), max(mx1, mx2));
}

int main() {
    #define task ""

    cin.tie(0) -> sync_with_stdio(0);

    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }

    if (fopen (task".inp", "r"))  {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }

    cin >> n >> m >> q;

    for(int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs(1); hld(1);

    for(int i = 1; i <= m; i++) {
        int x; cin >> x;
        vector<pair<int, int> > tmp;

        while (x) {
            tmp.emplace_back(st[root[x]], st[x]);
            x = par[root[x]];
        }

        reverse(all(tmp));

        for(int j = 0; j < tmp.size(); j++) lines[j][i] = tmp[j];
    }


    for(int i = 1; i <= q; i++) {
        int l, r; cin >> l >> r;
        query[r].emplace_back(l, i);
    }

    for(int k = 17; k >= 0; k--) {
        for(int i = 1; i <= m; i++) {
            S[i].clear();
            bit[i] = 0;
            b[i] = lines[k][i].second;
        }

        int last = 0, prel = 0;

        build(1, 1, m);

        for(int i = 1; i <= m; i++) {
            int l, r; tie(l, r) = lines[k][i];

            if (l != prel) prel = l, last = i;

            if (l) {
                while (!S[l].empty() && S[l].back().first <= r) {
                    int tail, j; tie(tail, j) = S[l].back(); S[l].pop_back();
                    int pre = S[l].empty() ? 1 : S[l].back().second + 1;
                    addrange(pre, j, -(tail - l + 1));
                }

                int pre = S[l].empty() ? 1 : S[l].back().second + 1;
                addrange(pre, i, r - l + 1);
                S[l].emplace_back(r, i);
            }

            for(auto &[j, idx] : query[i]) if (!off[idx]) {
                if (last <= j && l) {
                    int tmp1, tmp2; tie(tmp1, tmp2) = get(1, 1, m, j, i);
                    ans[idx] += tmp2 - tmp1 + 1;
                    off[idx] = 1;
                }
                else {
                    ans[idx] += get(j);
                }
            }
        }
    }

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

Compilation message

tourism.cpp: In function 'void build(int, int, int)':
tourism.cpp:71:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   71 |     int mid = l + r >> 1;
      |               ~~^~~
tourism.cpp: In function 'std::pair<int, int> get(int, int, int, int, int)':
tourism.cpp:81:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   81 |     int mid = l + r >> 1;
      |               ~~^~~
tourism.cpp: In function 'int main()':
tourism.cpp:123:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |         for(int j = 0; j < tmp.size(); j++) lines[j][i] = tmp[j];
      |                        ~~^~~~~~~~~~~~
tourism.cpp:93:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:94:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:98:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:99:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15136 KB Output is correct
2 Correct 2 ms 13044 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 3 ms 16984 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 4 ms 16984 KB Output is correct
7 Correct 3 ms 16984 KB Output is correct
8 Correct 3 ms 16988 KB Output is correct
9 Correct 4 ms 16984 KB Output is correct
10 Correct 3 ms 16984 KB Output is correct
11 Correct 3 ms 16988 KB Output is correct
12 Correct 3 ms 16984 KB Output is correct
13 Correct 3 ms 16984 KB Output is correct
14 Correct 3 ms 16988 KB Output is correct
15 Correct 3 ms 14936 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 4 ms 16984 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 3 ms 14936 KB Output is correct
21 Correct 3 ms 14936 KB Output is correct
22 Correct 3 ms 14936 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14936 KB Output is correct
25 Correct 3 ms 14936 KB Output is correct
26 Correct 3 ms 14940 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 3 ms 16984 KB Output is correct
29 Correct 3 ms 16984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15136 KB Output is correct
2 Correct 2 ms 13044 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 3 ms 16984 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 4 ms 16984 KB Output is correct
7 Correct 3 ms 16984 KB Output is correct
8 Correct 3 ms 16988 KB Output is correct
9 Correct 4 ms 16984 KB Output is correct
10 Correct 3 ms 16984 KB Output is correct
11 Correct 3 ms 16988 KB Output is correct
12 Correct 3 ms 16984 KB Output is correct
13 Correct 3 ms 16984 KB Output is correct
14 Correct 3 ms 16988 KB Output is correct
15 Correct 3 ms 14936 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 4 ms 16984 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 3 ms 14936 KB Output is correct
21 Correct 3 ms 14936 KB Output is correct
22 Correct 3 ms 14936 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14936 KB Output is correct
25 Correct 3 ms 14936 KB Output is correct
26 Correct 3 ms 14940 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 3 ms 16984 KB Output is correct
29 Correct 3 ms 16984 KB Output is correct
30 Correct 5 ms 17240 KB Output is correct
31 Correct 6 ms 17240 KB Output is correct
32 Correct 6 ms 19288 KB Output is correct
33 Correct 6 ms 17240 KB Output is correct
34 Correct 7 ms 17240 KB Output is correct
35 Correct 5 ms 17240 KB Output is correct
36 Correct 5 ms 17276 KB Output is correct
37 Correct 5 ms 17240 KB Output is correct
38 Correct 5 ms 15192 KB Output is correct
39 Correct 5 ms 15192 KB Output is correct
40 Correct 5 ms 15260 KB Output is correct
41 Correct 4 ms 15192 KB Output is correct
42 Correct 4 ms 15192 KB Output is correct
43 Correct 6 ms 15192 KB Output is correct
44 Correct 7 ms 15740 KB Output is correct
45 Correct 6 ms 17240 KB Output is correct
46 Correct 5 ms 17240 KB Output is correct
47 Correct 5 ms 15192 KB Output is correct
48 Correct 5 ms 15192 KB Output is correct
49 Correct 5 ms 17244 KB Output is correct
50 Correct 6 ms 15192 KB Output is correct
51 Correct 5 ms 15192 KB Output is correct
52 Correct 5 ms 15192 KB Output is correct
53 Correct 6 ms 15192 KB Output is correct
54 Correct 6 ms 15196 KB Output is correct
55 Correct 6 ms 15192 KB Output is correct
56 Correct 4 ms 14936 KB Output is correct
57 Correct 3 ms 17244 KB Output is correct
58 Correct 5 ms 17240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 4 ms 14940 KB Output is correct
4 Correct 147 ms 23632 KB Output is correct
5 Correct 101 ms 25888 KB Output is correct
6 Correct 107 ms 27216 KB Output is correct
7 Correct 163 ms 29516 KB Output is correct
8 Correct 174 ms 29452 KB Output is correct
9 Correct 156 ms 29380 KB Output is correct
10 Correct 173 ms 29280 KB Output is correct
11 Correct 156 ms 29264 KB Output is correct
12 Correct 119 ms 28736 KB Output is correct
13 Correct 115 ms 28740 KB Output is correct
14 Correct 116 ms 28752 KB Output is correct
15 Correct 39 ms 25040 KB Output is correct
16 Correct 145 ms 29160 KB Output is correct
17 Correct 137 ms 19024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 58 ms 24992 KB Output is correct
3 Correct 89 ms 25680 KB Output is correct
4 Correct 85 ms 26944 KB Output is correct
5 Correct 113 ms 29776 KB Output is correct
6 Correct 133 ms 29888 KB Output is correct
7 Correct 110 ms 29776 KB Output is correct
8 Correct 106 ms 29792 KB Output is correct
9 Correct 122 ms 29792 KB Output is correct
10 Correct 115 ms 29776 KB Output is correct
11 Correct 109 ms 29868 KB Output is correct
12 Correct 113 ms 29912 KB Output is correct
13 Correct 123 ms 30316 KB Output is correct
14 Correct 141 ms 31100 KB Output is correct
15 Correct 162 ms 34564 KB Output is correct
16 Correct 115 ms 28496 KB Output is correct
17 Correct 124 ms 30180 KB Output is correct
18 Correct 118 ms 30288 KB Output is correct
19 Correct 85 ms 26448 KB Output is correct
20 Correct 87 ms 26448 KB Output is correct
21 Correct 89 ms 26452 KB Output is correct
22 Correct 92 ms 26448 KB Output is correct
23 Correct 86 ms 26416 KB Output is correct
24 Correct 85 ms 26560 KB Output is correct
25 Correct 90 ms 26448 KB Output is correct
26 Correct 86 ms 26448 KB Output is correct
27 Correct 87 ms 26452 KB Output is correct
28 Correct 87 ms 26592 KB Output is correct
29 Correct 92 ms 26704 KB Output is correct
30 Correct 93 ms 26908 KB Output is correct
31 Correct 93 ms 26964 KB Output is correct
32 Correct 102 ms 27472 KB Output is correct
33 Correct 127 ms 28756 KB Output is correct
34 Correct 140 ms 31608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 3 ms 14936 KB Output is correct
3 Correct 4 ms 14936 KB Output is correct
4 Correct 209 ms 31628 KB Output is correct
5 Correct 208 ms 34064 KB Output is correct
6 Correct 227 ms 36176 KB Output is correct
7 Correct 239 ms 37428 KB Output is correct
8 Correct 255 ms 37420 KB Output is correct
9 Correct 235 ms 37344 KB Output is correct
10 Correct 230 ms 37456 KB Output is correct
11 Correct 250 ms 37564 KB Output is correct
12 Correct 247 ms 37500 KB Output is correct
13 Correct 259 ms 37456 KB Output is correct
14 Correct 136 ms 20392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15136 KB Output is correct
2 Correct 2 ms 13044 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 3 ms 16984 KB Output is correct
5 Correct 3 ms 16988 KB Output is correct
6 Correct 4 ms 16984 KB Output is correct
7 Correct 3 ms 16984 KB Output is correct
8 Correct 3 ms 16988 KB Output is correct
9 Correct 4 ms 16984 KB Output is correct
10 Correct 3 ms 16984 KB Output is correct
11 Correct 3 ms 16988 KB Output is correct
12 Correct 3 ms 16984 KB Output is correct
13 Correct 3 ms 16984 KB Output is correct
14 Correct 3 ms 16988 KB Output is correct
15 Correct 3 ms 14936 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 4 ms 16984 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 3 ms 14936 KB Output is correct
21 Correct 3 ms 14936 KB Output is correct
22 Correct 3 ms 14936 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14936 KB Output is correct
25 Correct 3 ms 14936 KB Output is correct
26 Correct 3 ms 14940 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 3 ms 16984 KB Output is correct
29 Correct 3 ms 16984 KB Output is correct
30 Correct 5 ms 17240 KB Output is correct
31 Correct 6 ms 17240 KB Output is correct
32 Correct 6 ms 19288 KB Output is correct
33 Correct 6 ms 17240 KB Output is correct
34 Correct 7 ms 17240 KB Output is correct
35 Correct 5 ms 17240 KB Output is correct
36 Correct 5 ms 17276 KB Output is correct
37 Correct 5 ms 17240 KB Output is correct
38 Correct 5 ms 15192 KB Output is correct
39 Correct 5 ms 15192 KB Output is correct
40 Correct 5 ms 15260 KB Output is correct
41 Correct 4 ms 15192 KB Output is correct
42 Correct 4 ms 15192 KB Output is correct
43 Correct 6 ms 15192 KB Output is correct
44 Correct 7 ms 15740 KB Output is correct
45 Correct 6 ms 17240 KB Output is correct
46 Correct 5 ms 17240 KB Output is correct
47 Correct 5 ms 15192 KB Output is correct
48 Correct 5 ms 15192 KB Output is correct
49 Correct 5 ms 17244 KB Output is correct
50 Correct 6 ms 15192 KB Output is correct
51 Correct 5 ms 15192 KB Output is correct
52 Correct 5 ms 15192 KB Output is correct
53 Correct 6 ms 15192 KB Output is correct
54 Correct 6 ms 15196 KB Output is correct
55 Correct 6 ms 15192 KB Output is correct
56 Correct 4 ms 14936 KB Output is correct
57 Correct 3 ms 17244 KB Output is correct
58 Correct 5 ms 17240 KB Output is correct
59 Correct 2 ms 12888 KB Output is correct
60 Correct 3 ms 14940 KB Output is correct
61 Correct 4 ms 14940 KB Output is correct
62 Correct 147 ms 23632 KB Output is correct
63 Correct 101 ms 25888 KB Output is correct
64 Correct 107 ms 27216 KB Output is correct
65 Correct 163 ms 29516 KB Output is correct
66 Correct 174 ms 29452 KB Output is correct
67 Correct 156 ms 29380 KB Output is correct
68 Correct 173 ms 29280 KB Output is correct
69 Correct 156 ms 29264 KB Output is correct
70 Correct 119 ms 28736 KB Output is correct
71 Correct 115 ms 28740 KB Output is correct
72 Correct 116 ms 28752 KB Output is correct
73 Correct 39 ms 25040 KB Output is correct
74 Correct 145 ms 29160 KB Output is correct
75 Correct 137 ms 19024 KB Output is correct
76 Correct 3 ms 14936 KB Output is correct
77 Correct 58 ms 24992 KB Output is correct
78 Correct 89 ms 25680 KB Output is correct
79 Correct 85 ms 26944 KB Output is correct
80 Correct 113 ms 29776 KB Output is correct
81 Correct 133 ms 29888 KB Output is correct
82 Correct 110 ms 29776 KB Output is correct
83 Correct 106 ms 29792 KB Output is correct
84 Correct 122 ms 29792 KB Output is correct
85 Correct 115 ms 29776 KB Output is correct
86 Correct 109 ms 29868 KB Output is correct
87 Correct 113 ms 29912 KB Output is correct
88 Correct 123 ms 30316 KB Output is correct
89 Correct 141 ms 31100 KB Output is correct
90 Correct 162 ms 34564 KB Output is correct
91 Correct 115 ms 28496 KB Output is correct
92 Correct 124 ms 30180 KB Output is correct
93 Correct 118 ms 30288 KB Output is correct
94 Correct 85 ms 26448 KB Output is correct
95 Correct 87 ms 26448 KB Output is correct
96 Correct 89 ms 26452 KB Output is correct
97 Correct 92 ms 26448 KB Output is correct
98 Correct 86 ms 26416 KB Output is correct
99 Correct 85 ms 26560 KB Output is correct
100 Correct 90 ms 26448 KB Output is correct
101 Correct 86 ms 26448 KB Output is correct
102 Correct 87 ms 26452 KB Output is correct
103 Correct 87 ms 26592 KB Output is correct
104 Correct 92 ms 26704 KB Output is correct
105 Correct 93 ms 26908 KB Output is correct
106 Correct 93 ms 26964 KB Output is correct
107 Correct 102 ms 27472 KB Output is correct
108 Correct 127 ms 28756 KB Output is correct
109 Correct 140 ms 31608 KB Output is correct
110 Correct 2 ms 14936 KB Output is correct
111 Correct 3 ms 14936 KB Output is correct
112 Correct 4 ms 14936 KB Output is correct
113 Correct 209 ms 31628 KB Output is correct
114 Correct 208 ms 34064 KB Output is correct
115 Correct 227 ms 36176 KB Output is correct
116 Correct 239 ms 37428 KB Output is correct
117 Correct 255 ms 37420 KB Output is correct
118 Correct 235 ms 37344 KB Output is correct
119 Correct 230 ms 37456 KB Output is correct
120 Correct 250 ms 37564 KB Output is correct
121 Correct 247 ms 37500 KB Output is correct
122 Correct 259 ms 37456 KB Output is correct
123 Correct 136 ms 20392 KB Output is correct
124 Correct 212 ms 33304 KB Output is correct
125 Correct 141 ms 31056 KB Output is correct
126 Correct 232 ms 33640 KB Output is correct
127 Correct 208 ms 33620 KB Output is correct
128 Correct 226 ms 33584 KB Output is correct
129 Correct 222 ms 33580 KB Output is correct
130 Correct 218 ms 33584 KB Output is correct
131 Correct 171 ms 31316 KB Output is correct
132 Correct 178 ms 32080 KB Output is correct
133 Correct 172 ms 29152 KB Output is correct
134 Correct 184 ms 30288 KB Output is correct
135 Correct 187 ms 30288 KB Output is correct
136 Correct 188 ms 30288 KB Output is correct
137 Correct 176 ms 28036 KB Output is correct
138 Correct 184 ms 28104 KB Output is correct
139 Correct 182 ms 28296 KB Output is correct
140 Correct 167 ms 28356 KB Output is correct
141 Correct 176 ms 28396 KB Output is correct
142 Correct 167 ms 28216 KB Output is correct
143 Correct 47 ms 24520 KB Output is correct
144 Correct 186 ms 33360 KB Output is correct