Submission #850572

# Submission time Handle Problem Language Result Execution time Memory
850572 2023-09-16T23:28:07 Z tvladm2009 Inside information (BOI21_servers) C++17
100 / 100
561 ms 140100 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 480000 + 777;
const int K = 20;
int n;
int k;
vector<pair<int, int>> g[N];
int tab[K][N];
int maxEdge[K][N];
bool inc[K][N];
bool desc[K][N];
int weight[N];
int dep[N];
vector<int> qs[N];
bool active[N];
int sz[N];
int aib[N];

struct T {
  char type;
  int a;
  int b;
  int d;
  int id;
};

vector<T> events;
int sol[2 * N];

int get(int a, int x) {
  for (int k = K - 1; k >= 0; k--) {
    if (x & (1 << k)) {
      a = tab[k][a];
    }
  }
  return a;
}

void build(int a, int p = 0) {
  tab[0][a] = p;
  maxEdge[0][a] = weight[a];
  for (int k = 1; k < K; k++) {
    tab[k][a] = tab[k - 1][tab[k - 1][a]];
    maxEdge[k][a] = max(maxEdge[k - 1][a], maxEdge[k - 1][tab[k - 1][a]]);
  }
  inc[0][a] = 1;
  inc[1][a] = (weight[a] < weight[p]);
  for (int k = 2; k < K; k++) {
    if (inc[k - 1][a] && inc[k - 1][tab[k - 1][a]] && weight[get(a, (1 << (k - 1)) - 1)] < weight[get(a, (1 << (k - 1)))] && weight[get(a, (1 << (k - 1)))] < weight[get(a, (1 << (k - 1)) + 1)]) {
      inc[k][a] = 1;
    }
  }
  desc[0][a] = 1;
  desc[1][a] = (weight[a] > weight[p]);
  for (int k = 2; k < K; k++) {
    if (desc[k - 1][a] && desc[k - 1][tab[k - 1][a]] && weight[get(a, (1 << (k - 1)) - 1)] > weight[get(a, (1 << (k - 1)))] && weight[get(a, (1 << (k - 1)))] > weight[get(a, (1 << (k - 1)) + 1)]) {
      desc[k][a] = 1;
    }
  }
  for (auto &b : g[a]) {
    if (b.first == p) continue;
    dep[b.first] = dep[a] + 1;
    weight[b.first] = b.second;
    build(b.first, a);
  }
}

int get_lca(int x, int y) {
  if (dep[x] < dep[y]) {
    swap(x, y);
  }
  for (int k = K - 1; k >= 0; k--) {
    if (dep[x] - (1 << k) >= dep[y]) {
      x = tab[k][x];
    }
  }
  if (x == y) {
    return x;
  }
  for (int k = K - 1; k >= 0; k--) {
    if (tab[k][x] != tab[k][y]) {
      x = tab[k][x];
      y = tab[k][y];
    }
  }
  return tab[0][x];
}

int under(int x, int y) { /// y is ancestor of x
  return get(x, dep[x] - dep[y] - 1);
}

bool isInc(int x, int y) { /// y is ancestor of x
  bool ok = 1;
  int xinit = x;
  for (int k = K - 1; k >= 0; k--) {
    if (dep[x] - (1 << k) >= dep[y]) {
      ok &= inc[k][x];
      x = tab[k][x];
      if (x == y) {
        break;
      }
      ok &= (weight[x] > weight[get(xinit, dep[xinit] - dep[x] - 1)]);
    }
  }
  return ok;
}

bool isDesc(int x, int y) { /// y is ancestor of x
  bool ok = 1;
  int xinit = x;
  for (int k = K - 1; k >= 0; k--) {
    if (dep[x] - (1 << k) >= dep[y]) {
      ok &= desc[k][x];
      x = tab[k][x];
      if (x == y) {
        break;
      }
      ok &= (weight[x] < weight[get(xinit, dep[xinit] - dep[x] - 1)]);
    }
  }
  return ok;
}

int get_max(int x, int y) {
  int z = get_lca(x, y);
  int ret = 0;
  for (int k = K - 1; k >= 0; k--) {
    if (dep[x] - (1 << k) >= dep[z]) {
      ret = max(ret, maxEdge[k][x]);
      x = tab[k][x];
    }
  }
  for (int k = K - 1; k >= 0; k--) {
    if (dep[y] - (1 << k) >= dep[z]) {
      ret = max(ret, maxEdge[k][y]);
      y = tab[k][y];
    }
  }
  return ret;
}

bool cmp(pair<int, int> a, pair<int, int> b) {
  return a.second > b.second;
}

void get_sizes(int a, int p = 0) {
  sz[a] = 1;
  for (auto &b : g[a]) {
    if (b.first == p) {
      continue;
    }
    if (active[b.first] == 0) {
      get_sizes(b.first, a);
      sz[a] += sz[b.first];
    }
  }
}

int get_centroid(int a, int limit, int p = 0) {
  for (auto &b : g[a]) {
    if (b.first == p) {
      continue;
    }
    if (active[b.first] == 0 && sz[b.first] >= limit) {
      return get_centroid(b.first, limit, a);
    }
  }
  return a;
}

void add(int i, int x) {
  while (i < N) {
    aib[i] += x;
    i += i & -i;
  }
}

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

vector<int> good;

void percolate(int a, int p, int last, int first, bool inc, bool dec) {
  if (dec == true) {
    for (auto &i : qs[a]) {
      sol[i] += get(i);
      if (first <= i) {
        sol[i]++;
      }
    }
  }
  if (inc == true) {
    good.push_back(last);
  }
  for (auto &b : g[a]) {
    if (b.first == p) {
      continue;
    }
    if (!active[b.first]) {
      bool inc2 = inc, dec2 = dec;
      if (b.second < last) {
        inc2 = 0;
      }
      if (b.second > last) {
        dec2 = 0;
      }
      percolate(b.first, a, b.second, first, inc2, dec2);
    }
  }
}

void decompose(int a, int p = 0) {
  get_sizes(a, a);
  a = get_centroid(a, (sz[a] + 1) / 2, a);
  active[a] = true;
  vector<int> not_active_kids;
  for (auto &b : g[a]) {
    if (!active[b.first]) {
      percolate(b.first, a, b.second, b.second, 1, 1);
      for (auto &i : good) {
        add(i, 1);
        not_active_kids.push_back(i);
      }
      good.clear();
    }
  }
  for (auto &i : qs[a]) {
    sol[i] += get(i);
  }
  for (auto &i : not_active_kids) {
    add(i, -1);
  }
  for (auto &b : g[a]) {
    if (b.first == p) {
      continue;
    }
    if (!active[b.first]) {
      decompose(b.first, a);
    }
  }
}

int main() {
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  // freopen ("input", "r", stdin);

  cin >> n >> k;
  for (int i = 1; i <= n + k - 1; i++) {
    T aux;
    cin >> aux.type;
    if (aux.type == 'S') {
      cin >> aux.a >> aux.b;
    } else if (aux.type == 'Q') {
      cin >> aux.a >> aux.d;
    } else if (aux.type == 'C') {
      cin >> aux.d;
    }
    aux.id = i;
    events.push_back(aux);
  }
  for (auto &e : events) {
    if (e.type == 'S') {
      g[e.a].push_back({e.b, e.id});
      g[e.b].push_back({e.a, e.id});
    }
  }
  dep[1] = 1;
  build(1);

  for (auto &e : events) {
    if (e.type == 'S' || e.type == 'C') {
      continue;
    }

    int x = e.a;
    int y = e.d;
    int z = get_lca(x, y);
    int mx = get_max(x, y);
    if (mx > e.id) {
      continue;
    }
    if (z == x) {
      sol[e.id] = isInc(y, x);
      continue;
    }
    if (z == y) {
      sol[e.id] = isDesc(x, y);
      continue;
    }
    if (isInc(y, z) && isDesc(x, z) && weight[under(y, z)] < weight[under(x, z)]) {
      sol[e.id] = 1;
    } else {
      sol[e.id] = 0;
    }
  }

  for (int i = 0; i < n + k - 1; i++) {
    if (events[i].type == 'C') {
      qs[events[i].d].push_back(events[i].id);
    }
  }
  for (int i = 1; i <= n; i++) {
    sort(g[i].begin(), g[i].end(), cmp);
  }
  decompose(1);

  for (auto &e : events) {
    if (e.type == 'Q') {
      cout << (sol[e.id] == 1 ? "yes" : "no") << "\n";
    } else if (e.type == 'C') {
      cout << sol[e.id] + 1 << "\n";
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 49 ms 115388 KB Output is correct
2 Correct 58 ms 115176 KB Output is correct
3 Correct 58 ms 115392 KB Output is correct
4 Correct 68 ms 115396 KB Output is correct
5 Correct 96 ms 115648 KB Output is correct
6 Correct 56 ms 115392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 115388 KB Output is correct
2 Correct 58 ms 115176 KB Output is correct
3 Correct 58 ms 115392 KB Output is correct
4 Correct 68 ms 115396 KB Output is correct
5 Correct 96 ms 115648 KB Output is correct
6 Correct 56 ms 115392 KB Output is correct
7 Correct 49 ms 115292 KB Output is correct
8 Correct 53 ms 116856 KB Output is correct
9 Correct 50 ms 116892 KB Output is correct
10 Correct 59 ms 116912 KB Output is correct
11 Correct 72 ms 117180 KB Output is correct
12 Correct 50 ms 116932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 115256 KB Output is correct
2 Correct 157 ms 123672 KB Output is correct
3 Correct 164 ms 123632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 115256 KB Output is correct
2 Correct 157 ms 123672 KB Output is correct
3 Correct 164 ms 123632 KB Output is correct
4 Correct 43 ms 115136 KB Output is correct
5 Correct 160 ms 123828 KB Output is correct
6 Correct 124 ms 124836 KB Output is correct
7 Correct 132 ms 124284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 115136 KB Output is correct
2 Correct 227 ms 133364 KB Output is correct
3 Correct 238 ms 133312 KB Output is correct
4 Correct 349 ms 135692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 115136 KB Output is correct
2 Correct 227 ms 133364 KB Output is correct
3 Correct 238 ms 133312 KB Output is correct
4 Correct 349 ms 135692 KB Output is correct
5 Correct 39 ms 115220 KB Output is correct
6 Correct 239 ms 134828 KB Output is correct
7 Correct 329 ms 137260 KB Output is correct
8 Correct 221 ms 135416 KB Output is correct
9 Correct 233 ms 135168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 115140 KB Output is correct
2 Correct 196 ms 123704 KB Output is correct
3 Correct 204 ms 123296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 115140 KB Output is correct
2 Correct 196 ms 123704 KB Output is correct
3 Correct 204 ms 123296 KB Output is correct
4 Correct 42 ms 115132 KB Output is correct
5 Correct 218 ms 128180 KB Output is correct
6 Correct 201 ms 127072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 115128 KB Output is correct
2 Correct 246 ms 133304 KB Output is correct
3 Correct 227 ms 133300 KB Output is correct
4 Correct 352 ms 135352 KB Output is correct
5 Correct 42 ms 115392 KB Output is correct
6 Correct 222 ms 123952 KB Output is correct
7 Correct 183 ms 123316 KB Output is correct
8 Correct 293 ms 123696 KB Output is correct
9 Correct 232 ms 123800 KB Output is correct
10 Correct 438 ms 131980 KB Output is correct
11 Correct 561 ms 130704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 115128 KB Output is correct
2 Correct 246 ms 133304 KB Output is correct
3 Correct 227 ms 133300 KB Output is correct
4 Correct 352 ms 135352 KB Output is correct
5 Correct 42 ms 115392 KB Output is correct
6 Correct 222 ms 123952 KB Output is correct
7 Correct 183 ms 123316 KB Output is correct
8 Correct 293 ms 123696 KB Output is correct
9 Correct 232 ms 123800 KB Output is correct
10 Correct 438 ms 131980 KB Output is correct
11 Correct 561 ms 130704 KB Output is correct
12 Correct 41 ms 115140 KB Output is correct
13 Correct 226 ms 134600 KB Output is correct
14 Correct 323 ms 136884 KB Output is correct
15 Correct 272 ms 135604 KB Output is correct
16 Correct 225 ms 135232 KB Output is correct
17 Correct 41 ms 115132 KB Output is correct
18 Correct 213 ms 128228 KB Output is correct
19 Correct 195 ms 127016 KB Output is correct
20 Correct 302 ms 128200 KB Output is correct
21 Correct 232 ms 127924 KB Output is correct
22 Correct 414 ms 133772 KB Output is correct
23 Correct 461 ms 135220 KB Output is correct
24 Correct 492 ms 134632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 115140 KB Output is correct
2 Correct 54 ms 115196 KB Output is correct
3 Correct 57 ms 115388 KB Output is correct
4 Correct 68 ms 115388 KB Output is correct
5 Correct 105 ms 115852 KB Output is correct
6 Correct 57 ms 115356 KB Output is correct
7 Correct 46 ms 115368 KB Output is correct
8 Correct 166 ms 123568 KB Output is correct
9 Correct 156 ms 123680 KB Output is correct
10 Correct 41 ms 115132 KB Output is correct
11 Correct 226 ms 133328 KB Output is correct
12 Correct 219 ms 133296 KB Output is correct
13 Correct 355 ms 135576 KB Output is correct
14 Correct 43 ms 115140 KB Output is correct
15 Correct 195 ms 124060 KB Output is correct
16 Correct 188 ms 123316 KB Output is correct
17 Correct 281 ms 123572 KB Output is correct
18 Correct 232 ms 123828 KB Output is correct
19 Correct 381 ms 131984 KB Output is correct
20 Correct 548 ms 130764 KB Output is correct
21 Correct 182 ms 123828 KB Output is correct
22 Correct 174 ms 123360 KB Output is correct
23 Correct 189 ms 123348 KB Output is correct
24 Correct 203 ms 123348 KB Output is correct
25 Correct 262 ms 127516 KB Output is correct
26 Correct 231 ms 122668 KB Output is correct
27 Correct 218 ms 122708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 115140 KB Output is correct
2 Correct 54 ms 115196 KB Output is correct
3 Correct 57 ms 115388 KB Output is correct
4 Correct 68 ms 115388 KB Output is correct
5 Correct 105 ms 115852 KB Output is correct
6 Correct 57 ms 115356 KB Output is correct
7 Correct 46 ms 115368 KB Output is correct
8 Correct 166 ms 123568 KB Output is correct
9 Correct 156 ms 123680 KB Output is correct
10 Correct 41 ms 115132 KB Output is correct
11 Correct 226 ms 133328 KB Output is correct
12 Correct 219 ms 133296 KB Output is correct
13 Correct 355 ms 135576 KB Output is correct
14 Correct 43 ms 115140 KB Output is correct
15 Correct 195 ms 124060 KB Output is correct
16 Correct 188 ms 123316 KB Output is correct
17 Correct 281 ms 123572 KB Output is correct
18 Correct 232 ms 123828 KB Output is correct
19 Correct 381 ms 131984 KB Output is correct
20 Correct 548 ms 130764 KB Output is correct
21 Correct 182 ms 123828 KB Output is correct
22 Correct 174 ms 123360 KB Output is correct
23 Correct 189 ms 123348 KB Output is correct
24 Correct 203 ms 123348 KB Output is correct
25 Correct 262 ms 127516 KB Output is correct
26 Correct 231 ms 122668 KB Output is correct
27 Correct 218 ms 122708 KB Output is correct
28 Correct 47 ms 115140 KB Output is correct
29 Correct 50 ms 116928 KB Output is correct
30 Correct 51 ms 116928 KB Output is correct
31 Correct 58 ms 116928 KB Output is correct
32 Correct 72 ms 117184 KB Output is correct
33 Correct 54 ms 116928 KB Output is correct
34 Correct 42 ms 116080 KB Output is correct
35 Correct 158 ms 126712 KB Output is correct
36 Correct 125 ms 126408 KB Output is correct
37 Correct 132 ms 126296 KB Output is correct
38 Correct 41 ms 116152 KB Output is correct
39 Correct 231 ms 137824 KB Output is correct
40 Correct 319 ms 140100 KB Output is correct
41 Correct 223 ms 137908 KB Output is correct
42 Correct 237 ms 138196 KB Output is correct
43 Correct 42 ms 116160 KB Output is correct
44 Correct 216 ms 128220 KB Output is correct
45 Correct 207 ms 127088 KB Output is correct
46 Correct 261 ms 128376 KB Output is correct
47 Correct 238 ms 128056 KB Output is correct
48 Correct 425 ms 133768 KB Output is correct
49 Correct 468 ms 135416 KB Output is correct
50 Correct 454 ms 134320 KB Output is correct
51 Correct 140 ms 127668 KB Output is correct
52 Correct 135 ms 127160 KB Output is correct
53 Correct 133 ms 126644 KB Output is correct
54 Correct 137 ms 127572 KB Output is correct
55 Correct 133 ms 127156 KB Output is correct
56 Correct 208 ms 126856 KB Output is correct
57 Correct 221 ms 130996 KB Output is correct
58 Correct 284 ms 127808 KB Output is correct
59 Correct 239 ms 126468 KB Output is correct