답안 #1082135

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082135 2024-08-30T18:01:07 Z VinhLuu Inside information (BOI21_servers) C++17
100 / 100
408 ms 76976 KB
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()

using namespace std;

typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 3e5 + 5;
const int oo = 2e9;

struct IT {
  int st[N << 2], lz[N << 2];
  void dolz(int i,int l,int r){
    if(!lz[i]) return;
    int x = lz[i], mid = (l + r) / 2; lz[i] = 0;
    st[i << 1] += x * (mid - l + 1), st[i << 1|1] += x * (r - mid);
    lz[i << 1] += x, lz[i << 1|1] += x;
  }

  void update(int i,int l,int r,int u,int v,int x){
    if(l > r || r < u || v < l) return;
    if(u <= l && r <= v){
      st[i] += x * (r - l + 1);
      lz[i] += x;
      return;
    }
    dolz(i, l, r);
    int mid = (l + r) / 2;
    update(i << 1, l, mid, u, v, x);
    update(i << 1|1, mid + 1, r, u, v, x);
    st[i] = st[i << 1] + st[i << 1|1];
  }

  int get(int i,int l,int r,int u,int v){
    if(l > r || r < u || v < l) return 0;
    if(u <= l && r <= v) return st[i];
    dolz(i, l, r);
    int mid = (l + r) / 2;
    return get(i << 1, l, mid, u, v) + get(i << 1|1, mid + 1, r, u, v);
  }
} f[2];

struct DSU{
  int par[N];

  void re(int n){
    for(int i = 1; i <= n; i ++) par[i] = i;
  }
  int fin(int u){
    return par[u] == u ? u : par[u] = fin(par[u]);
  }
  void dsu(int u,int v){
    u = fin(u);
    v = fin(v);
    if(u == v) return;
    par[u] = v;
  }
} g[2];

int n, q, T[N], X[N], Y[N], in[N], demin, en[N], head[N], pa[N], dp[N][22], d[N], be[N], h[N], key[2][N], tn[N];
vector<pii> p[N];

bool kt(int u,int v){
  return in[u] <= in[v] && in[v] <= en[u];
}

int LCA(int u,int v){
  if(kt(u, v)) return u;
  int lca = u;
  for(int i = 20; i >= 0; i --) if(kt(dp[u][i], v)) lca = dp[u][i]; else u = dp[u][i];
  return lca;
}

void dfs(int u,int v){

  d[u] = 1;
  for(auto [j, w] : p[u]) if(j != v){
    dfs(j, u);
    d[u] += d[j];
    pa[j] = u;
    h[j] = w;
  }

}

int scc = 1;

vector<int> vr[N], b[N], bit[N];
int sum[N];

void inc(int i,int pos,int val){
  sum[i] += val;
  pos++;
  for(; pos <= vr[i].size(); pos += pos & -pos) bit[i][pos] += val;
}

int co(int i,int pos){
  int ret = 0;
  pos++;
  for(; pos > 0; pos -= pos & -pos) ret += bit[i][pos];
  return ret;
}

void hld(int u,int v){
  in[u] = ++demin;
  if(!head[scc]) head[scc] = u;
  be[u] = scc;
  tn[demin] = u;
  if(!v){
      for(int i = 0; i <= 20; i ++) dp[u][i] = u;
  }else{
    dp[u][0] = v;
    for(int i = 1; i <= 20; i ++) dp[u][i] = dp[dp[u][i - 1]][i - 1];
  }

  int mx = 0, val = 0;
  for(auto [j, w] : p[u]) if(j != v && d[j] > d[mx]) val = w, mx = j;

  key[0][u] = mx, key[1][u] = val;

  if(mx) hld(mx, u);

  for(auto [j, w] : p[u]) if(j != v && j != mx){
    scc++;
    vr[u].pb(w);
    hld(j, u);
  }

  sort(all(vr[u]));
  for(auto j : vr[u]) bit[u].pb(0), b[u].pb(0);
  bit[u].pb(0);

  en[u] = demin;
}

void up(int u,int tmp){
  if(h[pa[u]] <= tmp){
    if(pa[u] == 1){
      g[0].dsu(u, pa[u]);
      g[1].dsu(u, pa[u]);
    }else{
      if(h[pa[u]] > h[u]) g[0].dsu(u, pa[u]);
      else g[1].dsu(u, pa[u]);
    }
  }
  for(auto [j, w] : p[u]) if(j != pa[u]){
    if(w > h[u] && w <= tmp) g[1].dsu(j, u);
    if(w < h[u] && w <= tmp) g[0].dsu(j, u);
  }

  int v = g[1].fin(u), val = f[0].get(1, 1, n, in[u], in[u]);
  if(v != 1 && key[1][pa[v]] != h[v]){
    if(key[1][pa[v]] < h[v]) f[1].update(1, 1, n, in[pa[v]], in[pa[v]], val);
    int dr = lower_bound(all(vr[pa[v]]), h[v]) - vr[pa[v]].begin();
    inc(pa[v], dr, val);
  }
  if(u == v) return;
  if(key[0][pa[u]] != u){
    if(key[1][pa[u]] < h[u]) f[1].update(1, 1, n, in[pa[u]], in[pa[u]], val);
    int dr = lower_bound(all(vr[pa[u]]), h[u]) - vr[pa[u]].begin();
    inc(pa[u], dr, val);
  }
  u = pa[u];
  while(true){
    if(be[u] == be[v]){
      f[0].update(1, 1, n, in[v], in[u], val);
      return;
    }
    int j = pa[head[be[u]]];
    if(h[head[be[u]]] > key[1][tn[in[j]]]) f[1].update(1, 1, n, in[j], in[j], val);
    int dr = lower_bound(all(vr[j]), h[head[be[u]]]) - vr[j].begin();
    inc(j, dr, val);
    f[0].update(1, 1, n, in[head[be[u]]], in[u], val);
    u = j;
  }
}

int lc(int u,int v){
  int kq = u;
  if(u == v) return kq;
  for(int i = 20; i >= 0; i --) if(v != dp[u][i] && kt(v, dp[u][i])){
    kq = dp[u][i];
    u = dp[u][i];
  }
  return kq;
}

int query(int u,int v,int tmp){
  int lca = LCA(u, v);
  int fu = g[0].fin(u), fv = g[1].fin(v);
  if(h[fu] <= tmp) fu = pa[fu];
  if(h[fv] <= tmp) fv = pa[fv];
  if(kt(fu, lca) && kt(fv, lca)){
    fu = lc(u, lca), fv = lc(v, lca);
    if(fu == lca || fv == lca || h[fu] < h[fv]) return 1;
  }
  return 0;
}

int solve(int u,int tmp){
  int ans = 1 + sum[u];
  if(h[key[0][u]] <= tmp && key[0][u]) ans += f[0].get(1, 1, n, in[key[0][u]], in[key[0][u]]);
  if(u == 1) return ans;

  int v = g[0].fin(u); if(h[v] <= tmp) v = pa[v];
  if(u == v) return ans;
  if(key[0][pa[u]] != u){
    int dr = upper_bound(all(vr[pa[u]]), h[u]) - vr[pa[u]].begin();
    ans += sum[pa[u]] - co(pa[u], dr - 1);
    ans++;
    if(h[key[0][pa[u]]] <= tmp && key[1][pa[u]] > h[u]) ans += f[0].get(1, 1, n, in[key[0][pa[u]]], in[key[0][pa[u]]]);

  }else{
    ans += f[1].get(1, 1, n, in[pa[u]], in[pa[u]]);
  }
  u = pa[u];
  while(true){
    if(be[u] == be[v]){
      if(u == v) return ans;
      ans += f[1].get(1, 1, n, in[v], in[u] - 1);
      return ans;
    }
    int j = pa[head[be[u]]];
//    cerr << j << " t\n";
    int k = head[be[u]];
    int pos = upper_bound(all(vr[j]), h[k]) - vr[j].begin();
    ans += sum[j] - co(j, pos - 1);
    ans += f[1].get(1, 1, n, in[k], in[u] - 1);
    ans++;
    if(h[key[0][j]] <= tmp && key[1][j] > h[k]) ans += f[0].get(1, 1, n, in[key[0][j]], in[key[0][j]]);
    u = j;
  }
  return ans;
}

signed main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #define task "v"
  if(fopen(task ".inp","r")){
    freopen(task ".inp","r",stdin);
    freopen(task ".out","w",stdout);
  }

  cin >> n >> q;
  q = n + q - 1;
  g[1].re(n), g[0].re(n);
  for(int i = 1; i <= q; i ++){
    char c; cin >> c;
    if(c == 'S'){
      cin >> X[i] >> Y[i];
      T[i] = 0,
      p[X[i]].pb({Y[i], i});
      p[Y[i]].pb({X[i], i});
    }else if(c == 'Q'){
      T[i] = 1;
      cin >> X[i] >> Y[i];
    }else{
      T[i] = 2;
      cin >> X[i];
    }
  }

  pa[1] = 1;
  h[1] = 0;
  dfs(1, 0);
  hld(1, 0);

  for(int i = 1; i <= n; i ++) f[0].update(1, 1, n, in[i], in[i], 1), f[1].update(1, 1, n, in[i], in[i], 1);

  for(int i = 1; i <= q; i ++){
    if(T[i] == 0){
      if(in[X[i]] < in[Y[i]]) swap(X[i], Y[i]);
      up(X[i], i);
    }else if(T[i] == 1){

      int x = X[i], y = Y[i];
      if(in[x] > in[y]) swap(x, y);
      if(x == y){
        cout << "yes\n";
        continue;
      }
      if(pa[y] == x){
        if(h[y] <= i) cout << "yes\n";
        else cout << "no\n";
        continue;
      }
      cout << (query(Y[i], X[i], i) == 1 ? "yes\n" : "no\n");
    }else {
      cout << solve(X[i], i) << "\n";
    }
  }
}

Compilation message

servers.cpp: In function 'void inc(int, int, int)':
servers.cpp:99:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |   for(; pos <= vr[i].size(); pos += pos & -pos) bit[i][pos] += val;
      |         ~~~~^~~~~~~~~~~~~~~
servers.cpp: In function 'void hld(int, int)':
servers.cpp:135:12: warning: unused variable 'j' [-Wunused-variable]
  135 |   for(auto j : vr[u]) bit[u].pb(0), b[u].pb(0);
      |            ^
servers.cpp: In function 'int main()':
servers.cpp:245:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  245 |     freopen(task ".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
servers.cpp:246:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  246 |     freopen(task ".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 31312 KB Output is correct
2 Correct 73 ms 32852 KB Output is correct
3 Correct 79 ms 32852 KB Output is correct
4 Correct 50 ms 33104 KB Output is correct
5 Correct 51 ms 33224 KB Output is correct
6 Correct 53 ms 32852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 31312 KB Output is correct
2 Correct 73 ms 32852 KB Output is correct
3 Correct 79 ms 32852 KB Output is correct
4 Correct 50 ms 33104 KB Output is correct
5 Correct 51 ms 33224 KB Output is correct
6 Correct 53 ms 32852 KB Output is correct
7 Correct 37 ms 31256 KB Output is correct
8 Correct 54 ms 32536 KB Output is correct
9 Correct 75 ms 32592 KB Output is correct
10 Correct 77 ms 32612 KB Output is correct
11 Correct 75 ms 32772 KB Output is correct
12 Correct 45 ms 32596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 31320 KB Output is correct
2 Correct 262 ms 64172 KB Output is correct
3 Correct 297 ms 64436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 31320 KB Output is correct
2 Correct 262 ms 64172 KB Output is correct
3 Correct 297 ms 64436 KB Output is correct
4 Correct 44 ms 31312 KB Output is correct
5 Correct 265 ms 64456 KB Output is correct
6 Correct 229 ms 63688 KB Output is correct
7 Correct 238 ms 63856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 31360 KB Output is correct
2 Correct 218 ms 76536 KB Output is correct
3 Correct 215 ms 76920 KB Output is correct
4 Correct 223 ms 76896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 31360 KB Output is correct
2 Correct 218 ms 76536 KB Output is correct
3 Correct 215 ms 76920 KB Output is correct
4 Correct 223 ms 76896 KB Output is correct
5 Correct 31 ms 31324 KB Output is correct
6 Correct 232 ms 76540 KB Output is correct
7 Correct 239 ms 76628 KB Output is correct
8 Correct 235 ms 76112 KB Output is correct
9 Correct 192 ms 76116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 31312 KB Output is correct
2 Correct 343 ms 66644 KB Output is correct
3 Correct 284 ms 67156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 31312 KB Output is correct
2 Correct 343 ms 66644 KB Output is correct
3 Correct 284 ms 67156 KB Output is correct
4 Correct 42 ms 31316 KB Output is correct
5 Correct 408 ms 66644 KB Output is correct
6 Correct 267 ms 66644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 31312 KB Output is correct
2 Correct 202 ms 76552 KB Output is correct
3 Correct 244 ms 76976 KB Output is correct
4 Correct 206 ms 76952 KB Output is correct
5 Correct 38 ms 31280 KB Output is correct
6 Correct 292 ms 66896 KB Output is correct
7 Correct 282 ms 66980 KB Output is correct
8 Correct 311 ms 66016 KB Output is correct
9 Correct 273 ms 66052 KB Output is correct
10 Correct 288 ms 72784 KB Output is correct
11 Correct 343 ms 71716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 31312 KB Output is correct
2 Correct 202 ms 76552 KB Output is correct
3 Correct 244 ms 76976 KB Output is correct
4 Correct 206 ms 76952 KB Output is correct
5 Correct 38 ms 31280 KB Output is correct
6 Correct 292 ms 66896 KB Output is correct
7 Correct 282 ms 66980 KB Output is correct
8 Correct 311 ms 66016 KB Output is correct
9 Correct 273 ms 66052 KB Output is correct
10 Correct 288 ms 72784 KB Output is correct
11 Correct 343 ms 71716 KB Output is correct
12 Correct 34 ms 31312 KB Output is correct
13 Correct 217 ms 76448 KB Output is correct
14 Correct 206 ms 76720 KB Output is correct
15 Correct 194 ms 76184 KB Output is correct
16 Correct 219 ms 76112 KB Output is correct
17 Correct 38 ms 31272 KB Output is correct
18 Correct 393 ms 66644 KB Output is correct
19 Correct 285 ms 66620 KB Output is correct
20 Correct 303 ms 65616 KB Output is correct
21 Correct 303 ms 65760 KB Output is correct
22 Correct 293 ms 70656 KB Output is correct
23 Correct 384 ms 73204 KB Output is correct
24 Correct 279 ms 73204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 31316 KB Output is correct
2 Correct 63 ms 32928 KB Output is correct
3 Correct 67 ms 32940 KB Output is correct
4 Correct 78 ms 32960 KB Output is correct
5 Correct 49 ms 33264 KB Output is correct
6 Correct 63 ms 32908 KB Output is correct
7 Correct 40 ms 31316 KB Output is correct
8 Correct 267 ms 64196 KB Output is correct
9 Correct 269 ms 64444 KB Output is correct
10 Correct 37 ms 31312 KB Output is correct
11 Correct 218 ms 76916 KB Output is correct
12 Correct 193 ms 76892 KB Output is correct
13 Correct 231 ms 76884 KB Output is correct
14 Correct 41 ms 31312 KB Output is correct
15 Correct 365 ms 66900 KB Output is correct
16 Correct 300 ms 67052 KB Output is correct
17 Correct 313 ms 66132 KB Output is correct
18 Correct 265 ms 65876 KB Output is correct
19 Correct 272 ms 72788 KB Output is correct
20 Correct 288 ms 71768 KB Output is correct
21 Correct 263 ms 65100 KB Output is correct
22 Correct 237 ms 64956 KB Output is correct
23 Correct 281 ms 65648 KB Output is correct
24 Correct 303 ms 65872 KB Output is correct
25 Correct 272 ms 67628 KB Output is correct
26 Correct 261 ms 65364 KB Output is correct
27 Correct 197 ms 65360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 31316 KB Output is correct
2 Correct 63 ms 32928 KB Output is correct
3 Correct 67 ms 32940 KB Output is correct
4 Correct 78 ms 32960 KB Output is correct
5 Correct 49 ms 33264 KB Output is correct
6 Correct 63 ms 32908 KB Output is correct
7 Correct 40 ms 31316 KB Output is correct
8 Correct 267 ms 64196 KB Output is correct
9 Correct 269 ms 64444 KB Output is correct
10 Correct 37 ms 31312 KB Output is correct
11 Correct 218 ms 76916 KB Output is correct
12 Correct 193 ms 76892 KB Output is correct
13 Correct 231 ms 76884 KB Output is correct
14 Correct 41 ms 31312 KB Output is correct
15 Correct 365 ms 66900 KB Output is correct
16 Correct 300 ms 67052 KB Output is correct
17 Correct 313 ms 66132 KB Output is correct
18 Correct 265 ms 65876 KB Output is correct
19 Correct 272 ms 72788 KB Output is correct
20 Correct 288 ms 71768 KB Output is correct
21 Correct 263 ms 65100 KB Output is correct
22 Correct 237 ms 64956 KB Output is correct
23 Correct 281 ms 65648 KB Output is correct
24 Correct 303 ms 65872 KB Output is correct
25 Correct 272 ms 67628 KB Output is correct
26 Correct 261 ms 65364 KB Output is correct
27 Correct 197 ms 65360 KB Output is correct
28 Correct 39 ms 31316 KB Output is correct
29 Correct 62 ms 32492 KB Output is correct
30 Correct 66 ms 32648 KB Output is correct
31 Correct 51 ms 32620 KB Output is correct
32 Correct 53 ms 33104 KB Output is correct
33 Correct 46 ms 32792 KB Output is correct
34 Correct 42 ms 31312 KB Output is correct
35 Correct 290 ms 64496 KB Output is correct
36 Correct 255 ms 63552 KB Output is correct
37 Correct 207 ms 63796 KB Output is correct
38 Correct 36 ms 31312 KB Output is correct
39 Correct 196 ms 76404 KB Output is correct
40 Correct 192 ms 76624 KB Output is correct
41 Correct 198 ms 76184 KB Output is correct
42 Correct 195 ms 76116 KB Output is correct
43 Correct 38 ms 31204 KB Output is correct
44 Correct 338 ms 66640 KB Output is correct
45 Correct 249 ms 66556 KB Output is correct
46 Correct 297 ms 65492 KB Output is correct
47 Correct 260 ms 65616 KB Output is correct
48 Correct 255 ms 70708 KB Output is correct
49 Correct 283 ms 73260 KB Output is correct
50 Correct 266 ms 73260 KB Output is correct
51 Correct 222 ms 64836 KB Output is correct
52 Correct 218 ms 64708 KB Output is correct
53 Correct 212 ms 64192 KB Output is correct
54 Correct 204 ms 64844 KB Output is correct
55 Correct 229 ms 64628 KB Output is correct
56 Correct 241 ms 65364 KB Output is correct
57 Correct 253 ms 66580 KB Output is correct
58 Correct 322 ms 64596 KB Output is correct
59 Correct 216 ms 65360 KB Output is correct