답안 #739051

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
739051 2023-05-09T19:50:18 Z tch1cherin Inside information (BOI21_servers) C++17
87.5 / 100
3500 ms 108344 KB
#include <bits/stdc++.h>
using namespace std;

struct fenwick {
  int size;
  vector<int> fenw, changes;

  fenwick() {}

  fenwick(int _size) : size(_size), fenw(_size + 1) {}

  void add(int i, int v) {
    for (i++; i <= size; i += i & -i) {
      fenw[i] += v;
      changes.push_back(i);
    }
  }

  int prefix_sum(int r) {
    int ans = 0;
    for (; r > 0; r -= r & -r) {
      ans += fenw[r];
    }
    return ans;
  }

  int suffix_sum(int l) {
    return prefix_sum(size) - prefix_sum(l);  
  }

  void clear() {
    for (int i : changes) {
      fenw[i] = 0;
    }
    changes.clear();
  } 
};

const int N = 120000;
vector<pair<int, int>> graph[N]; // (v, t)
vector<tuple<int, int, int, int>> Share[N]; // (a, b, t, id)
vector<tuple<int, int, int, int>> Query[N]; // (a, b, t, id) 
vector<tuple<int, int, int>> Count[N]; // (u, t, id)
int sz[N], answer[2 * N], type[N], last[N];
bool used[N];
fenwick bit;
set<int> s;
map<int, int> edge;

void sizes(int u, int p) {
  sz[u] = 1;
  for (auto [v, t] : graph[u]) {
    if (v != p && !used[v]) {
      sizes(v, u);
      sz[u] += sz[v];
    }
  }
}

int centroid(int u, int p, int n) {
  for (auto [v, t] : graph[u]) {
    if (v != p && !used[v] && sz[v] > n / 2) {
      return centroid(v, u, n);
    }
  }
  return u;
}

void DFS(int u, int p, vector<int>& comp) {
  comp.push_back(u);
  for (auto [v, t] : graph[u]) {
    if (v != p && !used[v]) {
      DFS(v, u, comp);
    }
  }
}

void explore(int u, int p, int time) {
  s.insert(u);
  if (type[u] == 0 || type[u] == 1) {
    bit.add(edge[u], 1); 
  }
  for (auto [v, t] : graph[u]) {
    if (v != p && !used[v] && t <= time) {
      if (type[u] == 0) {
        type[v] = (last[u] == -1 ? 0 : (last[u] < t ? 1 : 2)); 
      } else if (type[u] == 1) {
        type[v] = (last[u] < t ? 1 : 3);
      } else if (type[u] == 2) {
        type[v] = (last[u] > t ? 2 : 3);
      } else {
        type[v] = 3;
      }
      last[v] = t;
      explore(v, u, time);
    }
  }
}

void find_centroids(int u, int p) {
  map<int, int> first, to;
  edge[u] = -1;
  for (auto [v, t] : graph[u]) {
    if (v != p && !used[v]) {
      sizes(v, u);
      to[v] = centroid(v, u, sz[v]);
    }
  }
  for (auto [v, t] : graph[u]) {
    if (v != p && !used[v]) {
      vector<int> comp;
      DFS(v, u, comp);
      for (int x : comp) {
        first[x] = v;
        edge[x] = t; 
      }
    }
  }
  for (auto &[k, v] : first) {
    v = to[v];
  }
  first[u] = INT_MIN;
  vector<int> D;
  for (auto [a, b, t, id] : Share[u]) {
    D.push_back(t);
  }
  for (auto [a, b, t, id] : Query[u]) {
    D.push_back(t);
  }
  for (auto [a, t, id] : Count[u]) {
    D.push_back(t);
  }
  sort(D.begin(), D.end());
  D.resize(unique(D.begin(), D.end()) - D.begin());
  int share_pos = 0, query_pos = 0, count_pos = 0;
  s.insert(u);
  type[u] = 0;
  last[u] = -1;
  for (int t : D) {
    while (share_pos < (int)Share[u].size() && get<2>(Share[u][share_pos]) <= t) {
      auto [a, b, _t, id] = Share[u][share_pos];
      if (first[a] == first[b]) {
        Share[first[a]].emplace_back(a, b, _t, id);  
      } 
      if (s.count(b)) {
        swap(a, b);
      }
      if (s.count(a)) { 
        if (type[a] == 0) {
          type[b] = (last[a] == -1 ? 0 : (last[a] < t ? 1 : 2)); 
        } else if (type[a] == 1) {
          type[b] = (last[a] < t ? 1 : 3);
        } else if (type[a] == 2) {
          type[b] = (last[a] > t ? 2 : 3);
        } else {
          type[b] = 3;
        }
        last[b] = t;
        explore(b, a, t);
      } 
      share_pos++;
    }
    while (query_pos < (int)Query[u].size() && get<2>(Query[u][query_pos]) <= t) {
      auto [a, b, _t, id] = Query[u][query_pos];
      if (a == b) {
        answer[id] = INT_MAX;
      } else if (first[a] == first[b]) {
        Query[first[a]].emplace_back(a, b, _t, id);
      } else {
        bool good = true;
        good &= s.count(a) && s.count(b);
        good &= type[a] <= 1;
        good &= type[b] == 0 || type[b] == 2;
        good &= edge[a] == -1 || edge[b] == -1 || edge[a] > edge[b];
        answer[id] = good ? INT_MAX : INT_MIN;
      }
      query_pos++;
    }
    while (count_pos < (int)Count[u].size() && get<1>(Count[u][count_pos]) <= t) {
      auto [a, _t, id] = Count[u][count_pos];
      if (a != u) {
        Count[first[a]].emplace_back(a, _t, id);
      }
      if (s.count(a)) {
        if (type[a] == 0 || type[a] == 2) {
          answer[id] += bit.suffix_sum(edge[a] + 1) + 1; 
        }
      }
      count_pos++;  
    }
  }
  s.clear();
  bit.clear();
  edge.clear();
  used[u] = true;
  for (auto [v, t] : graph[u]) {
    if (v != p && !used[v]) {
      sizes(v, u);
      find_centroids(centroid(v, u, sz[v]), u);
    }
  }
}

void solve() {
  memset(used, 0, sizeof used);
  memset(answer, 0, sizeof answer);
  int n, k;
  cin >> n >> k;
  bit = fenwick(n);
  int t = 0;
  vector<tuple<int, int, int, int>> share_queries, query_queries;
  vector<tuple<int, int, int>> count_queries;
  for (int i = 0; i < n + k - 1; i++) {
    char type;
    cin >> type;
    if (type == 'S') {
      int a, b;
      cin >> a >> b;
      a--, b--;
      t++;
      graph[a].emplace_back(b, t);
      graph[b].emplace_back(a, t);
      share_queries.emplace_back(a, b, t, i);
    } else if (type == 'Q') {
      int a, d;
      cin >> a >> d;
      a--, d--;
      query_queries.emplace_back(a, d, t, i - t);
    } else {
      int d;
      cin >> d;
      d--;
      count_queries.emplace_back(d, t, i - t);
    }
  }
  sizes(0, -1);
  int c = centroid(0, -1, n);
  Share[c] = share_queries;
  Query[c] = query_queries;
  Count[c] = count_queries;
  find_centroids(c, -1);
  for (int i = 0; i < k; i++) {
    if (answer[i] == INT_MIN) {
      cout << "no\n";
    } else if (answer[i] == INT_MAX) {
      cout << "yes\n";
    } else {
      cout << answer[i] << "\n"; 
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 17928 KB Output is correct
2 Correct 152 ms 20772 KB Output is correct
3 Correct 200 ms 23524 KB Output is correct
4 Correct 179 ms 22152 KB Output is correct
5 Correct 192 ms 23056 KB Output is correct
6 Correct 134 ms 20092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 17928 KB Output is correct
2 Correct 152 ms 20772 KB Output is correct
3 Correct 200 ms 23524 KB Output is correct
4 Correct 179 ms 22152 KB Output is correct
5 Correct 192 ms 23056 KB Output is correct
6 Correct 134 ms 20092 KB Output is correct
7 Correct 47 ms 19308 KB Output is correct
8 Correct 154 ms 25304 KB Output is correct
9 Correct 133 ms 23252 KB Output is correct
10 Correct 202 ms 28444 KB Output is correct
11 Correct 207 ms 29192 KB Output is correct
12 Correct 90 ms 20116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 17804 KB Output is correct
2 Correct 690 ms 57528 KB Output is correct
3 Correct 738 ms 57668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 17804 KB Output is correct
2 Correct 690 ms 57528 KB Output is correct
3 Correct 738 ms 57668 KB Output is correct
4 Correct 62 ms 18628 KB Output is correct
5 Correct 721 ms 57648 KB Output is correct
6 Correct 464 ms 55648 KB Output is correct
7 Correct 536 ms 57056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 19788 KB Output is correct
2 Correct 2475 ms 107972 KB Output is correct
3 Correct 2680 ms 108076 KB Output is correct
4 Correct 2198 ms 90292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 19788 KB Output is correct
2 Correct 2475 ms 107972 KB Output is correct
3 Correct 2680 ms 108076 KB Output is correct
4 Correct 2198 ms 90292 KB Output is correct
5 Correct 54 ms 20108 KB Output is correct
6 Correct 2779 ms 108080 KB Output is correct
7 Correct 2269 ms 99272 KB Output is correct
8 Correct 2699 ms 107360 KB Output is correct
9 Correct 2719 ms 107184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 18820 KB Output is correct
2 Correct 2243 ms 80000 KB Output is correct
3 Correct 3001 ms 89736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 18820 KB Output is correct
2 Correct 2243 ms 80000 KB Output is correct
3 Correct 3001 ms 89736 KB Output is correct
4 Correct 49 ms 19524 KB Output is correct
5 Correct 2489 ms 89220 KB Output is correct
6 Correct 2855 ms 92796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 19764 KB Output is correct
2 Correct 2537 ms 107960 KB Output is correct
3 Correct 2410 ms 108080 KB Output is correct
4 Correct 1912 ms 90356 KB Output is correct
5 Correct 46 ms 19720 KB Output is correct
6 Correct 2118 ms 80104 KB Output is correct
7 Correct 2627 ms 89640 KB Output is correct
8 Correct 2603 ms 69852 KB Output is correct
9 Correct 2694 ms 82540 KB Output is correct
10 Correct 3034 ms 93068 KB Output is correct
11 Correct 3375 ms 83648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 19764 KB Output is correct
2 Correct 2537 ms 107960 KB Output is correct
3 Correct 2410 ms 108080 KB Output is correct
4 Correct 1912 ms 90356 KB Output is correct
5 Correct 46 ms 19720 KB Output is correct
6 Correct 2118 ms 80104 KB Output is correct
7 Correct 2627 ms 89640 KB Output is correct
8 Correct 2603 ms 69852 KB Output is correct
9 Correct 2694 ms 82540 KB Output is correct
10 Correct 3034 ms 93068 KB Output is correct
11 Correct 3375 ms 83648 KB Output is correct
12 Correct 52 ms 20040 KB Output is correct
13 Correct 2636 ms 108344 KB Output is correct
14 Correct 2105 ms 99332 KB Output is correct
15 Correct 2381 ms 107488 KB Output is correct
16 Correct 2496 ms 107208 KB Output is correct
17 Correct 48 ms 19536 KB Output is correct
18 Correct 2288 ms 89276 KB Output is correct
19 Correct 2653 ms 92648 KB Output is correct
20 Correct 2777 ms 78660 KB Output is correct
21 Correct 2601 ms 85644 KB Output is correct
22 Correct 3188 ms 100092 KB Output is correct
23 Execution timed out 3568 ms 98672 KB Time limit exceeded
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 17856 KB Output is correct
2 Correct 153 ms 20804 KB Output is correct
3 Correct 171 ms 23496 KB Output is correct
4 Correct 180 ms 22216 KB Output is correct
5 Correct 194 ms 23004 KB Output is correct
6 Correct 135 ms 20100 KB Output is correct
7 Correct 46 ms 18664 KB Output is correct
8 Correct 708 ms 57580 KB Output is correct
9 Correct 690 ms 57536 KB Output is correct
10 Correct 60 ms 19872 KB Output is correct
11 Correct 2349 ms 107948 KB Output is correct
12 Correct 2464 ms 108076 KB Output is correct
13 Correct 1985 ms 90444 KB Output is correct
14 Correct 54 ms 19708 KB Output is correct
15 Correct 2131 ms 80068 KB Output is correct
16 Correct 2606 ms 89632 KB Output is correct
17 Correct 2571 ms 69792 KB Output is correct
18 Correct 2620 ms 82532 KB Output is correct
19 Correct 3020 ms 92980 KB Output is correct
20 Correct 3121 ms 83492 KB Output is correct
21 Correct 1010 ms 57236 KB Output is correct
22 Correct 998 ms 56740 KB Output is correct
23 Correct 1915 ms 64960 KB Output is correct
24 Correct 2090 ms 64536 KB Output is correct
25 Correct 2428 ms 80548 KB Output is correct
26 Correct 1887 ms 71912 KB Output is correct
27 Correct 1763 ms 71212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 17856 KB Output is correct
2 Correct 153 ms 20804 KB Output is correct
3 Correct 171 ms 23496 KB Output is correct
4 Correct 180 ms 22216 KB Output is correct
5 Correct 194 ms 23004 KB Output is correct
6 Correct 135 ms 20100 KB Output is correct
7 Correct 46 ms 18664 KB Output is correct
8 Correct 708 ms 57580 KB Output is correct
9 Correct 690 ms 57536 KB Output is correct
10 Correct 60 ms 19872 KB Output is correct
11 Correct 2349 ms 107948 KB Output is correct
12 Correct 2464 ms 108076 KB Output is correct
13 Correct 1985 ms 90444 KB Output is correct
14 Correct 54 ms 19708 KB Output is correct
15 Correct 2131 ms 80068 KB Output is correct
16 Correct 2606 ms 89632 KB Output is correct
17 Correct 2571 ms 69792 KB Output is correct
18 Correct 2620 ms 82532 KB Output is correct
19 Correct 3020 ms 92980 KB Output is correct
20 Correct 3121 ms 83492 KB Output is correct
21 Correct 1010 ms 57236 KB Output is correct
22 Correct 998 ms 56740 KB Output is correct
23 Correct 1915 ms 64960 KB Output is correct
24 Correct 2090 ms 64536 KB Output is correct
25 Correct 2428 ms 80548 KB Output is correct
26 Correct 1887 ms 71912 KB Output is correct
27 Correct 1763 ms 71212 KB Output is correct
28 Correct 47 ms 19408 KB Output is correct
29 Correct 154 ms 25256 KB Output is correct
30 Correct 127 ms 23272 KB Output is correct
31 Correct 192 ms 28476 KB Output is correct
32 Correct 194 ms 29188 KB Output is correct
33 Correct 86 ms 20176 KB Output is correct
34 Correct 47 ms 18532 KB Output is correct
35 Correct 658 ms 57460 KB Output is correct
36 Correct 433 ms 55740 KB Output is correct
37 Correct 470 ms 57044 KB Output is correct
38 Correct 47 ms 20044 KB Output is correct
39 Correct 2310 ms 108132 KB Output is correct
40 Correct 2000 ms 99444 KB Output is correct
41 Correct 2336 ms 107448 KB Output is correct
42 Correct 2388 ms 107216 KB Output is correct
43 Correct 47 ms 19520 KB Output is correct
44 Correct 2115 ms 89280 KB Output is correct
45 Correct 2555 ms 92748 KB Output is correct
46 Correct 2676 ms 78512 KB Output is correct
47 Correct 2533 ms 85520 KB Output is correct
48 Correct 3174 ms 99840 KB Output is correct
49 Correct 3435 ms 98452 KB Output is correct
50 Correct 3289 ms 96048 KB Output is correct
51 Correct 718 ms 58684 KB Output is correct
52 Correct 494 ms 57784 KB Output is correct
53 Correct 478 ms 57400 KB Output is correct
54 Correct 466 ms 56216 KB Output is correct
55 Correct 679 ms 56072 KB Output is correct
56 Correct 1881 ms 66324 KB Output is correct
57 Correct 1933 ms 82000 KB Output is correct
58 Correct 2382 ms 75872 KB Output is correct
59 Correct 1851 ms 71324 KB Output is correct