답안 #781945

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
781945 2023-07-13T14:02:46 Z peijar Tourism (JOI23_tourism) C++17
100 / 100
3038 ms 107288 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
  bool first = true;
  string res = "[";
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    first = false;
    res += to_string(x);
  }
  res += "]";
  return res;
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << to_string(H);
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

template <class T> class Fenwick {
public:
  int lim;
  vector<T> bit;

  Fenwick(int n) : lim(n + 1), bit(lim) {}

  void upd(int pos, T val) {
    for (pos++; pos < lim; pos += pos & -pos)
      bit[pos] += val;
  }

  T sum(int r) { // < r
    T ret = 0;
    for (; r; r -= r & -r)
      ret += bit[r];
    return ret;
  }

  T sum(int l, int r) { // [l, r)
    return sum(r) - sum(l);
  }
};

template <class T> struct RMQ {
  vector<vector<T>> jmp;
  RMQ(const vector<T> &V) : jmp(1, V) {
    for (int pw = 1, k = 1; pw * 2 <= (int)V.size(); pw *= 2, ++k) {
      jmp.emplace_back((int)V.size() - pw * 2 + 1);
      for (int j = 0; j < (int)jmp[k].size(); ++j)
        jmp[k][j] = max(jmp[k - 1][j], jmp[k - 1][j + pw]);
    }
  }
  T query(int a, int b) { // [a, b)
    assert(a < b);
    int dep = 31 - __builtin_clz(b - a);
    return max(jmp[dep][a], jmp[dep][b - (1 << dep)]);
  }
};

const int MAXN = 4e5 + 1;
const int MAXP = 20;

int iDeb[MAXN], iFin[MAXN], lazy[MAXN];
vector<int> adj[MAXN];
int sz[MAXN];
int depth[MAXN];
int par[MAXP][MAXN];
int heavyPar[MAXN];
int tin[MAXN];
int Time;

void push(int node) {
  if (iDeb[node] < iFin[node] and lazy[node] != -1) {
    lazy[2 * node] = lazy[2 * node + 1] = lazy[node];
    lazy[node] = -1;
  }
}

void build(int node, int l, int r, int defaultVal) {
  iDeb[node] = l, iFin[node] = r;
  lazy[node] = defaultVal;
  if (l == r)
    return;
  int m = (l + r) / 2;
  build(2 * node, l, m, defaultVal);
  build(2 * node + 1, m + 1, r, defaultVal);
}

void update(int node, int l, int r, int val) {
  push(node);
  if (l > iFin[node] or iDeb[node] > r)
    return;
  if (iDeb[node] >= l and iFin[node] <= r) {
    lazy[node] = val;
    push(node);
    return;
  }
  update(2 * node, l, r, val);
  update(2 * node + 1, l, r, val);
}

int query(int node, int pos) {
  push(node);
  if (pos > iFin[node] or pos < iDeb[node])
    return 0;
  if (iDeb[node] == iFin[node])
    return lazy[node];
  return query(2 * node, pos) + query(2 * node + 1, pos);
}

void dfsHLD(int u, int p) {
  sz[u] = 1;
  par[0][u] = p;
  for (int i = 0; i < MAXP - 1; ++i)
    par[i + 1][u] = par[i][par[i][u]];
  int largest = -1;
  for (int v : adj[u])
    if (v != p) {
      depth[v] = depth[u] + 1;
      dfsHLD(v, u);
      sz[u] += sz[v];
      if (largest == -1 or sz[largest] < sz[v])
        largest = v;
    }

  if (largest != -1)
    for (int i = 1; i < (int)adj[u].size(); ++i)
      if (adj[u][i] == largest) {
        swap(adj[u][i], adj[u][0]);
        break;
      }
}

void dfs(int u, int p) {
  tin[u] = Time++;
  for (int i = 0; i < (int)adj[u].size(); ++i) {
    int v = adj[u][i];
    if (v == p)
      continue;
    if (!i)
      heavyPar[v] = heavyPar[u];
    else
      heavyPar[v] = v;
    dfs(v, u);
  }
}

int getLca(int u, int v) {
  if (depth[u] < depth[v])
    swap(u, v);
  int d = depth[u] - depth[v];
  for (int p = 0; p < MAXP; ++p)
    if ((1 << p) & d)
      u = par[p][u];
  if (u == v)
    return u;
  for (int p = MAXP - 1; p >= 0; --p) {
    int uu = par[p][u], vv = par[p][v];
    if (uu != vv)
      u = uu, v = vv;
  }
  return par[0][u];
}

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int nbSommets, nbSites, nbRequetes;
  cin >> nbSommets >> nbSites >> nbRequetes;

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

  dfsHLD(0, 0);
  dfs(0, 0);

  vector<int> sites(nbSites);
  for (int &x : sites) {
    cin >> x;
    --x;
  }
  vector<pair<int, int>> inRMQ(nbSites);
  for (int i = 0; i < nbSites; ++i)
    inRMQ[i] = pair(tin[sites[i]], sites[i]);

  RMQ<pair<int, int>> maxSight(inRMQ);
  for (auto &[t, u] : inRMQ)
    t *= -1;
  RMQ<pair<int, int>> minSight(inRMQ);

  vector<int> solRequete(nbRequetes);
  vector<vector<pair<int, int>>> queriesAt(nbSites);
  for (int iRequete = 0; iRequete < nbRequetes; ++iRequete) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    queriesAt[l].emplace_back(r, iRequete);
  }
  for (int u = 0; u < nbSommets; ++u)
    dbg(u + 1, heavyPar[u] + 1);
  build(1, 0, nbSommets - 1, nbSites);

  Fenwick<int> withVal(nbSites + 1);
  withVal.upd(nbSites, nbSommets);
  for (int L = nbSites - 1; L >= 0; --L) {
    int u = sites[L];
    while (true) {
      int v = u;
      int val = query(1, tin[u]);
      for (int p = MAXP - 1; p >= 0; --p)
        if (val == query(1, tin[par[p][v]]))
          v = par[p][v];
      dbg(u + 1, v + 1, val);
      withVal.upd(val, -(depth[u] - depth[v] + 1));
      if (!v)
        break;
      u = par[0][v];
    }
    u = sites[L];
    while (true) {
      update(1, tin[heavyPar[u]], tin[u], L);
      dbg("UPDATE"s, 1 + heavyPar[u], 1 + u, L);
      withVal.upd(L, depth[u] - depth[heavyPar[u]] + 1);
      if (!heavyPar[u])
        break;
      u = par[0][heavyPar[u]];
    }

    for (auto [r, iRequete] : queriesAt[L]) {
      solRequete[iRequete] = withVal.sum(r + 1);
      int fst = minSight.query(L, r + 1).second;
      int lst = maxSight.query(L, r + 1).second;
      solRequete[iRequete] -= depth[getLca(fst, lst)];
    }
  }
  for (int x : solRequete)
    cout << x << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9868 KB Output is correct
2 Correct 5 ms 9940 KB Output is correct
3 Correct 5 ms 9864 KB Output is correct
4 Correct 6 ms 9940 KB Output is correct
5 Correct 6 ms 10068 KB Output is correct
6 Correct 7 ms 9964 KB Output is correct
7 Correct 7 ms 9988 KB Output is correct
8 Correct 6 ms 9996 KB Output is correct
9 Correct 7 ms 10068 KB Output is correct
10 Correct 7 ms 10128 KB Output is correct
11 Correct 7 ms 10116 KB Output is correct
12 Correct 6 ms 10068 KB Output is correct
13 Correct 7 ms 10124 KB Output is correct
14 Correct 7 ms 10092 KB Output is correct
15 Correct 8 ms 10124 KB Output is correct
16 Correct 8 ms 10140 KB Output is correct
17 Correct 7 ms 10116 KB Output is correct
18 Correct 8 ms 10112 KB Output is correct
19 Correct 9 ms 10068 KB Output is correct
20 Correct 8 ms 10068 KB Output is correct
21 Correct 6 ms 10068 KB Output is correct
22 Correct 7 ms 10068 KB Output is correct
23 Correct 7 ms 10056 KB Output is correct
24 Correct 6 ms 10068 KB Output is correct
25 Correct 6 ms 10068 KB Output is correct
26 Correct 8 ms 10124 KB Output is correct
27 Correct 6 ms 9960 KB Output is correct
28 Correct 6 ms 9992 KB Output is correct
29 Correct 9 ms 10024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9868 KB Output is correct
2 Correct 5 ms 9940 KB Output is correct
3 Correct 5 ms 9864 KB Output is correct
4 Correct 6 ms 9940 KB Output is correct
5 Correct 6 ms 10068 KB Output is correct
6 Correct 7 ms 9964 KB Output is correct
7 Correct 7 ms 9988 KB Output is correct
8 Correct 6 ms 9996 KB Output is correct
9 Correct 7 ms 10068 KB Output is correct
10 Correct 7 ms 10128 KB Output is correct
11 Correct 7 ms 10116 KB Output is correct
12 Correct 6 ms 10068 KB Output is correct
13 Correct 7 ms 10124 KB Output is correct
14 Correct 7 ms 10092 KB Output is correct
15 Correct 8 ms 10124 KB Output is correct
16 Correct 8 ms 10140 KB Output is correct
17 Correct 7 ms 10116 KB Output is correct
18 Correct 8 ms 10112 KB Output is correct
19 Correct 9 ms 10068 KB Output is correct
20 Correct 8 ms 10068 KB Output is correct
21 Correct 6 ms 10068 KB Output is correct
22 Correct 7 ms 10068 KB Output is correct
23 Correct 7 ms 10056 KB Output is correct
24 Correct 6 ms 10068 KB Output is correct
25 Correct 6 ms 10068 KB Output is correct
26 Correct 8 ms 10124 KB Output is correct
27 Correct 6 ms 9960 KB Output is correct
28 Correct 6 ms 9992 KB Output is correct
29 Correct 9 ms 10024 KB Output is correct
30 Correct 17 ms 10960 KB Output is correct
31 Correct 18 ms 10996 KB Output is correct
32 Correct 21 ms 11328 KB Output is correct
33 Correct 22 ms 11328 KB Output is correct
34 Correct 24 ms 11336 KB Output is correct
35 Correct 21 ms 11300 KB Output is correct
36 Correct 18 ms 11348 KB Output is correct
37 Correct 21 ms 11332 KB Output is correct
38 Correct 15 ms 11344 KB Output is correct
39 Correct 21 ms 11440 KB Output is correct
40 Correct 16 ms 11444 KB Output is correct
41 Correct 16 ms 11476 KB Output is correct
42 Correct 19 ms 11452 KB Output is correct
43 Correct 17 ms 11420 KB Output is correct
44 Correct 21 ms 11384 KB Output is correct
45 Correct 20 ms 11336 KB Output is correct
46 Correct 19 ms 11380 KB Output is correct
47 Correct 20 ms 11376 KB Output is correct
48 Correct 21 ms 11336 KB Output is correct
49 Correct 21 ms 11368 KB Output is correct
50 Correct 15 ms 11352 KB Output is correct
51 Correct 12 ms 11356 KB Output is correct
52 Correct 15 ms 11284 KB Output is correct
53 Correct 14 ms 11352 KB Output is correct
54 Correct 12 ms 11344 KB Output is correct
55 Correct 12 ms 11352 KB Output is correct
56 Correct 7 ms 10716 KB Output is correct
57 Correct 6 ms 10580 KB Output is correct
58 Correct 22 ms 11244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9812 KB Output is correct
2 Correct 6 ms 9992 KB Output is correct
3 Correct 7 ms 10648 KB Output is correct
4 Correct 847 ms 82556 KB Output is correct
5 Correct 646 ms 74128 KB Output is correct
6 Correct 804 ms 91896 KB Output is correct
7 Correct 1125 ms 107280 KB Output is correct
8 Correct 1056 ms 107288 KB Output is correct
9 Correct 1149 ms 107276 KB Output is correct
10 Correct 1106 ms 107280 KB Output is correct
11 Correct 1087 ms 107280 KB Output is correct
12 Correct 605 ms 107004 KB Output is correct
13 Correct 618 ms 106964 KB Output is correct
14 Correct 607 ms 107024 KB Output is correct
15 Correct 60 ms 49956 KB Output is correct
16 Correct 1097 ms 106948 KB Output is correct
17 Correct 98 ms 69500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9940 KB Output is correct
2 Correct 1045 ms 54644 KB Output is correct
3 Correct 1672 ms 75996 KB Output is correct
4 Correct 1390 ms 67832 KB Output is correct
5 Correct 2282 ms 95004 KB Output is correct
6 Correct 2195 ms 94996 KB Output is correct
7 Correct 2208 ms 95008 KB Output is correct
8 Correct 2185 ms 94980 KB Output is correct
9 Correct 2305 ms 95008 KB Output is correct
10 Correct 2040 ms 95024 KB Output is correct
11 Correct 2120 ms 95112 KB Output is correct
12 Correct 2163 ms 95156 KB Output is correct
13 Correct 2185 ms 95508 KB Output is correct
14 Correct 2197 ms 96516 KB Output is correct
15 Correct 2228 ms 100192 KB Output is correct
16 Correct 2031 ms 95516 KB Output is correct
17 Correct 2171 ms 95700 KB Output is correct
18 Correct 2029 ms 95520 KB Output is correct
19 Correct 2181 ms 94704 KB Output is correct
20 Correct 2179 ms 94560 KB Output is correct
21 Correct 2179 ms 94516 KB Output is correct
22 Correct 2315 ms 94560 KB Output is correct
23 Correct 2263 ms 94560 KB Output is correct
24 Correct 2271 ms 94544 KB Output is correct
25 Correct 2313 ms 94540 KB Output is correct
26 Correct 2286 ms 94592 KB Output is correct
27 Correct 2191 ms 94588 KB Output is correct
28 Correct 2215 ms 94572 KB Output is correct
29 Correct 2363 ms 94660 KB Output is correct
30 Correct 2481 ms 94840 KB Output is correct
31 Correct 2286 ms 95056 KB Output is correct
32 Correct 2330 ms 95568 KB Output is correct
33 Correct 2361 ms 97176 KB Output is correct
34 Correct 2063 ms 99828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 5 ms 9940 KB Output is correct
3 Correct 6 ms 10740 KB Output is correct
4 Correct 1672 ms 80348 KB Output is correct
5 Correct 1883 ms 82284 KB Output is correct
6 Correct 2242 ms 95980 KB Output is correct
7 Correct 2485 ms 100236 KB Output is correct
8 Correct 2346 ms 100208 KB Output is correct
9 Correct 2384 ms 100228 KB Output is correct
10 Correct 2412 ms 100228 KB Output is correct
11 Correct 2329 ms 100244 KB Output is correct
12 Correct 2343 ms 100248 KB Output is correct
13 Correct 2333 ms 100224 KB Output is correct
14 Correct 83 ms 69504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9868 KB Output is correct
2 Correct 5 ms 9940 KB Output is correct
3 Correct 5 ms 9864 KB Output is correct
4 Correct 6 ms 9940 KB Output is correct
5 Correct 6 ms 10068 KB Output is correct
6 Correct 7 ms 9964 KB Output is correct
7 Correct 7 ms 9988 KB Output is correct
8 Correct 6 ms 9996 KB Output is correct
9 Correct 7 ms 10068 KB Output is correct
10 Correct 7 ms 10128 KB Output is correct
11 Correct 7 ms 10116 KB Output is correct
12 Correct 6 ms 10068 KB Output is correct
13 Correct 7 ms 10124 KB Output is correct
14 Correct 7 ms 10092 KB Output is correct
15 Correct 8 ms 10124 KB Output is correct
16 Correct 8 ms 10140 KB Output is correct
17 Correct 7 ms 10116 KB Output is correct
18 Correct 8 ms 10112 KB Output is correct
19 Correct 9 ms 10068 KB Output is correct
20 Correct 8 ms 10068 KB Output is correct
21 Correct 6 ms 10068 KB Output is correct
22 Correct 7 ms 10068 KB Output is correct
23 Correct 7 ms 10056 KB Output is correct
24 Correct 6 ms 10068 KB Output is correct
25 Correct 6 ms 10068 KB Output is correct
26 Correct 8 ms 10124 KB Output is correct
27 Correct 6 ms 9960 KB Output is correct
28 Correct 6 ms 9992 KB Output is correct
29 Correct 9 ms 10024 KB Output is correct
30 Correct 17 ms 10960 KB Output is correct
31 Correct 18 ms 10996 KB Output is correct
32 Correct 21 ms 11328 KB Output is correct
33 Correct 22 ms 11328 KB Output is correct
34 Correct 24 ms 11336 KB Output is correct
35 Correct 21 ms 11300 KB Output is correct
36 Correct 18 ms 11348 KB Output is correct
37 Correct 21 ms 11332 KB Output is correct
38 Correct 15 ms 11344 KB Output is correct
39 Correct 21 ms 11440 KB Output is correct
40 Correct 16 ms 11444 KB Output is correct
41 Correct 16 ms 11476 KB Output is correct
42 Correct 19 ms 11452 KB Output is correct
43 Correct 17 ms 11420 KB Output is correct
44 Correct 21 ms 11384 KB Output is correct
45 Correct 20 ms 11336 KB Output is correct
46 Correct 19 ms 11380 KB Output is correct
47 Correct 20 ms 11376 KB Output is correct
48 Correct 21 ms 11336 KB Output is correct
49 Correct 21 ms 11368 KB Output is correct
50 Correct 15 ms 11352 KB Output is correct
51 Correct 12 ms 11356 KB Output is correct
52 Correct 15 ms 11284 KB Output is correct
53 Correct 14 ms 11352 KB Output is correct
54 Correct 12 ms 11344 KB Output is correct
55 Correct 12 ms 11352 KB Output is correct
56 Correct 7 ms 10716 KB Output is correct
57 Correct 6 ms 10580 KB Output is correct
58 Correct 22 ms 11244 KB Output is correct
59 Correct 8 ms 9812 KB Output is correct
60 Correct 6 ms 9992 KB Output is correct
61 Correct 7 ms 10648 KB Output is correct
62 Correct 847 ms 82556 KB Output is correct
63 Correct 646 ms 74128 KB Output is correct
64 Correct 804 ms 91896 KB Output is correct
65 Correct 1125 ms 107280 KB Output is correct
66 Correct 1056 ms 107288 KB Output is correct
67 Correct 1149 ms 107276 KB Output is correct
68 Correct 1106 ms 107280 KB Output is correct
69 Correct 1087 ms 107280 KB Output is correct
70 Correct 605 ms 107004 KB Output is correct
71 Correct 618 ms 106964 KB Output is correct
72 Correct 607 ms 107024 KB Output is correct
73 Correct 60 ms 49956 KB Output is correct
74 Correct 1097 ms 106948 KB Output is correct
75 Correct 98 ms 69500 KB Output is correct
76 Correct 6 ms 9940 KB Output is correct
77 Correct 1045 ms 54644 KB Output is correct
78 Correct 1672 ms 75996 KB Output is correct
79 Correct 1390 ms 67832 KB Output is correct
80 Correct 2282 ms 95004 KB Output is correct
81 Correct 2195 ms 94996 KB Output is correct
82 Correct 2208 ms 95008 KB Output is correct
83 Correct 2185 ms 94980 KB Output is correct
84 Correct 2305 ms 95008 KB Output is correct
85 Correct 2040 ms 95024 KB Output is correct
86 Correct 2120 ms 95112 KB Output is correct
87 Correct 2163 ms 95156 KB Output is correct
88 Correct 2185 ms 95508 KB Output is correct
89 Correct 2197 ms 96516 KB Output is correct
90 Correct 2228 ms 100192 KB Output is correct
91 Correct 2031 ms 95516 KB Output is correct
92 Correct 2171 ms 95700 KB Output is correct
93 Correct 2029 ms 95520 KB Output is correct
94 Correct 2181 ms 94704 KB Output is correct
95 Correct 2179 ms 94560 KB Output is correct
96 Correct 2179 ms 94516 KB Output is correct
97 Correct 2315 ms 94560 KB Output is correct
98 Correct 2263 ms 94560 KB Output is correct
99 Correct 2271 ms 94544 KB Output is correct
100 Correct 2313 ms 94540 KB Output is correct
101 Correct 2286 ms 94592 KB Output is correct
102 Correct 2191 ms 94588 KB Output is correct
103 Correct 2215 ms 94572 KB Output is correct
104 Correct 2363 ms 94660 KB Output is correct
105 Correct 2481 ms 94840 KB Output is correct
106 Correct 2286 ms 95056 KB Output is correct
107 Correct 2330 ms 95568 KB Output is correct
108 Correct 2361 ms 97176 KB Output is correct
109 Correct 2063 ms 99828 KB Output is correct
110 Correct 5 ms 9812 KB Output is correct
111 Correct 5 ms 9940 KB Output is correct
112 Correct 6 ms 10740 KB Output is correct
113 Correct 1672 ms 80348 KB Output is correct
114 Correct 1883 ms 82284 KB Output is correct
115 Correct 2242 ms 95980 KB Output is correct
116 Correct 2485 ms 100236 KB Output is correct
117 Correct 2346 ms 100208 KB Output is correct
118 Correct 2384 ms 100228 KB Output is correct
119 Correct 2412 ms 100228 KB Output is correct
120 Correct 2329 ms 100244 KB Output is correct
121 Correct 2343 ms 100248 KB Output is correct
122 Correct 2333 ms 100224 KB Output is correct
123 Correct 83 ms 69504 KB Output is correct
124 Correct 1789 ms 90296 KB Output is correct
125 Correct 1357 ms 75696 KB Output is correct
126 Correct 2196 ms 100064 KB Output is correct
127 Correct 2416 ms 100064 KB Output is correct
128 Correct 2360 ms 100068 KB Output is correct
129 Correct 2227 ms 100084 KB Output is correct
130 Correct 2217 ms 100184 KB Output is correct
131 Correct 1191 ms 106128 KB Output is correct
132 Correct 1231 ms 107176 KB Output is correct
133 Correct 1441 ms 103376 KB Output is correct
134 Correct 2732 ms 99764 KB Output is correct
135 Correct 3038 ms 99712 KB Output is correct
136 Correct 2630 ms 99620 KB Output is correct
137 Correct 805 ms 100256 KB Output is correct
138 Correct 782 ms 100252 KB Output is correct
139 Correct 807 ms 100256 KB Output is correct
140 Correct 775 ms 100240 KB Output is correct
141 Correct 788 ms 100240 KB Output is correct
142 Correct 803 ms 100288 KB Output is correct
143 Correct 97 ms 42724 KB Output is correct
144 Correct 2132 ms 99740 KB Output is correct