답안 #1102572

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1102572 2024-10-18T10:47:07 Z epicci23 Inside information (BOI21_servers) C++17
100 / 100
249 ms 63424 KB
#include "bits/stdc++.h"
//#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;

const int N = 1e5 + 4e4 + 5;
const int LOG = 20;
const int INF = 1e9;

int maxi[N][LOG],mini[N][LOG];
int lift[N][LOG],depth[N],ans[N],vis[N],sub[N],comp=0;
vector<array<int,2>> v[N],tag[N];

void dfs(int c){
  for(int i=1;i<LOG;i++) lift[c][i]=lift[lift[c][i-1]][i-1];
  
  for(int i=1;i<LOG;i++){
  	int u = maxi[c][i-1];
  	int u2 = maxi[lift[c][i-1]][i-1];
  	if(u==-1 || u2==-1) continue;
  	if(u>maxi[lift[c][i-1]][0]) continue;
    maxi[c][i]=u2;
  }

  for(int i=1;i<LOG;i++){
  	int u = mini[c][i-1];
  	int u2 = mini[lift[c][i-1]][i-1];
  	if(u==-1 || u2==-1) continue;
  	if(u<mini[lift[c][i-1]][0]) continue;
    mini[c][i]=u2;
  }

  for(auto E:v[c]){
   int x=E[0],y=E[1];
   if(x==lift[c][0]) continue;
   depth[x]=depth[c]+1;
   lift[x][0]=c;
   maxi[x][0]=mini[x][0]=y;
   dfs(x);
  }
}

inline int lca(int a,int b){
  if(depth[a]<depth[b]) swap(a,b);
  for(int i=0;i<LOG;i++) if((depth[a]-depth[b])>>i&1) a=lift[a][i];
  if(a==b) return a;
  for(int i=LOG-1;i>=0;i--){
  	if(lift[a][i]!=lift[b][i]){
  	  a=lift[a][i];
  	  b=lift[b][i];
  	}
  }
  return lift[a][0];
}

inline int yukari(int a,int u){
  int le = depth[a]-depth[u],lol = 0;
  for(int i=LOG-1;i>=0;i--){
    if(!(le>>i&1)) continue;
    if(maxi[a][i]==-1) return -1;
    if(lol>maxi[a][0]) return -1;
    lol=maxi[a][i];
    a=lift[a][i];
  }
  return lol;
}

inline int asagi(int a,int u){
  int le = depth[a]-depth[u],lol = INF;
  for(int i=LOG-1;i>=0;i--){
    if(!(le>>i&1)) continue;
    if(mini[a][i]==-1) return -1;
    if(lol<mini[a][0]) return -1;
    lol=mini[a][i];
    a=lift[a][i];
  }
  return lol;
}

inline int check(int a,int b){
  if(a==b) return 0;
  int c=lca(a,b);
  int hm = yukari(a,c);
  int hm2 = asagi(b,c);
  if(hm==-1 || hm2==-1 || hm>hm2) return -1;
  int xdd = hm;
  if(b!=c) xdd=max(xdd,maxi[b][0]);
  return xdd;
}

void precalc(int c,int p){
  sub[c]=1;
  comp++;
  for(auto E:v[c]){
    int x = E[0];
    if(x==p || vis[x]) continue;
    precalc(x,c);
    sub[c]+=sub[x];
  }
}

int find_centroid(int c,int p){
  for(auto E:v[c]){
    int x = E[0];
    if(x==p || vis[x]) continue;
    if(sub[x]>comp/2) return find_centroid(x,c);
  }
  return c;
}

int fw[N];

inline void Upd(int c,int u){
  for(;c<N;c+=c&-c) fw[c]+=u;    
}

inline int Query(int c,int res=0){
  for(;c;c-=c&-c) res+=fw[c];
  return res;
}

void Add(int c,int p,int last,int val){
  Upd(last,val);
  for(auto E:v[c]){
    int x=E[0],u=E[1];
    if(vis[x] || x==p || last>u) continue;
    Add(x,c,u,val);
  }
}

void Get(int c,int p,int bruh,int azal){
  for(auto x:tag[c]){
    if(bruh>x[0]) continue;
    ans[x[1]]+=Query(x[0])+1;
  }
  for(auto E:v[c]){
    int x=E[0],u=E[1];
    if(vis[x] || x==p || azal<u) continue;
    Get(x,c,bruh,u);
  }
}

void centroid(int c,int p){
  comp=0;precalc(c,p);
  c=find_centroid(c,p);
  sort(all(v[c]),[&](array<int,2> a,array<int,2> b){
    return a[1]>b[1];
  });
  for(auto E:v[c]){
    int x = E[0], u = E[1];
    if(x==p || vis[x]) continue;
    Get(x,c,u,u);
    Add(x,c,u,1);
  }
  for(auto x:tag[c]) ans[x[1]]+=Query(x[0]);
  for(auto E:v[c]){
    int x = E[0], u = E[1];
    if(x==p || vis[x]) continue;
    Add(x,c,u,-1);
  }
  vis[c]=1;
  for(auto E:v[c]){
    int x = E[0], u = E[1];
    if(x==p || vis[x]) continue;
    centroid(x,c);
  }
}

void _(){
  memset(maxi,-1,sizeof(maxi));
  memset(mini,-1,sizeof(mini));

  int n,q;
  cin >> n >> q;
  
  vector<array<int,4>> cevapla;
  int cp=0,ed=0;

  for(int i=1;i<n+q;i++){
    char op;
    cin >> op;
    if(op=='S'){
     int a,b;
     cin >> a >> b;
     ++ed;
     v[a].push_back({b,ed});
     v[b].push_back({a,ed});
    }
    else if(op=='Q'){
      int a,b;
      cin >> a >> b;
      ++cp;
      cevapla.push_back({b,a,ed,cp});
    }
    else{
      int a;
      cin >> a;
      ++cp;
      tag[a].push_back({ed,cp});
    }
  }
  dfs(1);
  for(auto x:cevapla){
    int res = check(x[0],x[1]);
    if(res==-1 || res>x[2]) ans[x[3]]=-2;
    else ans[x[3]]=-1; 
  }
  centroid(1,0);
  for(int i=1;i<=cp;i++){
    if(ans[i]==-2) cout << "no\n";
    else if(ans[i]==-1) cout << "yes\n";
    else cout << ans[i] + 1 << '\n';
  }
}

int32_t main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tc=1;//cin >> tc;
  while(tc--) _();
  return 0;
}

Compilation message

servers.cpp: In function 'void centroid(int, int)':
servers.cpp:164:19: warning: unused variable 'u' [-Wunused-variable]
  164 |     int x = E[0], u = E[1];
      |                   ^
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 36048 KB Output is correct
2 Correct 40 ms 36816 KB Output is correct
3 Correct 36 ms 36820 KB Output is correct
4 Correct 44 ms 36824 KB Output is correct
5 Correct 41 ms 37052 KB Output is correct
6 Correct 33 ms 36760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 36048 KB Output is correct
2 Correct 40 ms 36816 KB Output is correct
3 Correct 36 ms 36820 KB Output is correct
4 Correct 44 ms 36824 KB Output is correct
5 Correct 41 ms 37052 KB Output is correct
6 Correct 33 ms 36760 KB Output is correct
7 Correct 29 ms 36048 KB Output is correct
8 Correct 34 ms 36228 KB Output is correct
9 Correct 29 ms 36148 KB Output is correct
10 Correct 38 ms 36288 KB Output is correct
11 Correct 35 ms 36552 KB Output is correct
12 Correct 31 ms 36240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 36048 KB Output is correct
2 Correct 128 ms 52492 KB Output is correct
3 Correct 111 ms 52300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 36048 KB Output is correct
2 Correct 128 ms 52492 KB Output is correct
3 Correct 111 ms 52300 KB Output is correct
4 Correct 28 ms 36048 KB Output is correct
5 Correct 107 ms 52404 KB Output is correct
6 Correct 77 ms 50492 KB Output is correct
7 Correct 88 ms 50472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 36048 KB Output is correct
2 Correct 174 ms 63164 KB Output is correct
3 Correct 184 ms 63160 KB Output is correct
4 Correct 176 ms 63172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 36048 KB Output is correct
2 Correct 174 ms 63164 KB Output is correct
3 Correct 184 ms 63160 KB Output is correct
4 Correct 176 ms 63172 KB Output is correct
5 Correct 26 ms 35860 KB Output is correct
6 Correct 190 ms 63180 KB Output is correct
7 Correct 174 ms 63384 KB Output is correct
8 Correct 170 ms 62796 KB Output is correct
9 Correct 174 ms 62820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 35916 KB Output is correct
2 Correct 127 ms 52732 KB Output is correct
3 Correct 125 ms 53248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 35916 KB Output is correct
2 Correct 127 ms 52732 KB Output is correct
3 Correct 125 ms 53248 KB Output is correct
4 Correct 30 ms 36048 KB Output is correct
5 Correct 130 ms 52800 KB Output is correct
6 Correct 130 ms 52932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 36048 KB Output is correct
2 Correct 169 ms 63168 KB Output is correct
3 Correct 180 ms 63164 KB Output is correct
4 Correct 169 ms 63152 KB Output is correct
5 Correct 29 ms 36048 KB Output is correct
6 Correct 125 ms 52924 KB Output is correct
7 Correct 142 ms 53192 KB Output is correct
8 Correct 156 ms 53824 KB Output is correct
9 Correct 145 ms 53764 KB Output is correct
10 Correct 183 ms 59068 KB Output is correct
11 Correct 207 ms 58076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 36048 KB Output is correct
2 Correct 169 ms 63168 KB Output is correct
3 Correct 180 ms 63164 KB Output is correct
4 Correct 169 ms 63152 KB Output is correct
5 Correct 29 ms 36048 KB Output is correct
6 Correct 125 ms 52924 KB Output is correct
7 Correct 142 ms 53192 KB Output is correct
8 Correct 156 ms 53824 KB Output is correct
9 Correct 145 ms 53764 KB Output is correct
10 Correct 183 ms 59068 KB Output is correct
11 Correct 207 ms 58076 KB Output is correct
12 Correct 26 ms 36048 KB Output is correct
13 Correct 193 ms 63260 KB Output is correct
14 Correct 179 ms 63424 KB Output is correct
15 Correct 174 ms 62952 KB Output is correct
16 Correct 185 ms 62716 KB Output is correct
17 Correct 29 ms 36048 KB Output is correct
18 Correct 140 ms 52932 KB Output is correct
19 Correct 148 ms 52968 KB Output is correct
20 Correct 151 ms 53440 KB Output is correct
21 Correct 146 ms 53696 KB Output is correct
22 Correct 201 ms 56884 KB Output is correct
23 Correct 249 ms 57604 KB Output is correct
24 Correct 221 ms 57836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 36048 KB Output is correct
2 Correct 39 ms 36816 KB Output is correct
3 Correct 36 ms 36752 KB Output is correct
4 Correct 51 ms 36820 KB Output is correct
5 Correct 41 ms 37020 KB Output is correct
6 Correct 35 ms 37092 KB Output is correct
7 Correct 27 ms 36048 KB Output is correct
8 Correct 111 ms 52404 KB Output is correct
9 Correct 115 ms 52404 KB Output is correct
10 Correct 25 ms 36048 KB Output is correct
11 Correct 171 ms 63164 KB Output is correct
12 Correct 166 ms 63336 KB Output is correct
13 Correct 166 ms 63164 KB Output is correct
14 Correct 27 ms 35988 KB Output is correct
15 Correct 119 ms 52748 KB Output is correct
16 Correct 135 ms 53180 KB Output is correct
17 Correct 138 ms 53756 KB Output is correct
18 Correct 125 ms 53692 KB Output is correct
19 Correct 160 ms 59068 KB Output is correct
20 Correct 181 ms 58196 KB Output is correct
21 Correct 99 ms 52928 KB Output is correct
22 Correct 99 ms 52924 KB Output is correct
23 Correct 108 ms 53180 KB Output is correct
24 Correct 110 ms 53440 KB Output is correct
25 Correct 154 ms 57020 KB Output is correct
26 Correct 134 ms 52708 KB Output is correct
27 Correct 146 ms 52924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 36048 KB Output is correct
2 Correct 39 ms 36816 KB Output is correct
3 Correct 36 ms 36752 KB Output is correct
4 Correct 51 ms 36820 KB Output is correct
5 Correct 41 ms 37020 KB Output is correct
6 Correct 35 ms 37092 KB Output is correct
7 Correct 27 ms 36048 KB Output is correct
8 Correct 111 ms 52404 KB Output is correct
9 Correct 115 ms 52404 KB Output is correct
10 Correct 25 ms 36048 KB Output is correct
11 Correct 171 ms 63164 KB Output is correct
12 Correct 166 ms 63336 KB Output is correct
13 Correct 166 ms 63164 KB Output is correct
14 Correct 27 ms 35988 KB Output is correct
15 Correct 119 ms 52748 KB Output is correct
16 Correct 135 ms 53180 KB Output is correct
17 Correct 138 ms 53756 KB Output is correct
18 Correct 125 ms 53692 KB Output is correct
19 Correct 160 ms 59068 KB Output is correct
20 Correct 181 ms 58196 KB Output is correct
21 Correct 99 ms 52928 KB Output is correct
22 Correct 99 ms 52924 KB Output is correct
23 Correct 108 ms 53180 KB Output is correct
24 Correct 110 ms 53440 KB Output is correct
25 Correct 154 ms 57020 KB Output is correct
26 Correct 134 ms 52708 KB Output is correct
27 Correct 146 ms 52924 KB Output is correct
28 Correct 31 ms 36048 KB Output is correct
29 Correct 36 ms 36032 KB Output is correct
30 Correct 31 ms 36152 KB Output is correct
31 Correct 38 ms 36288 KB Output is correct
32 Correct 36 ms 36600 KB Output is correct
33 Correct 31 ms 36288 KB Output is correct
34 Correct 28 ms 36048 KB Output is correct
35 Correct 105 ms 52472 KB Output is correct
36 Correct 77 ms 50484 KB Output is correct
37 Correct 93 ms 50624 KB Output is correct
38 Correct 27 ms 36048 KB Output is correct
39 Correct 192 ms 63072 KB Output is correct
40 Correct 171 ms 63404 KB Output is correct
41 Correct 185 ms 62932 KB Output is correct
42 Correct 179 ms 62804 KB Output is correct
43 Correct 30 ms 36060 KB Output is correct
44 Correct 139 ms 52928 KB Output is correct
45 Correct 146 ms 52928 KB Output is correct
46 Correct 149 ms 53432 KB Output is correct
47 Correct 160 ms 53824 KB Output is correct
48 Correct 181 ms 56780 KB Output is correct
49 Correct 231 ms 57792 KB Output is correct
50 Correct 213 ms 57720 KB Output is correct
51 Correct 93 ms 51584 KB Output is correct
52 Correct 87 ms 51540 KB Output is correct
53 Correct 82 ms 51088 KB Output is correct
54 Correct 85 ms 51508 KB Output is correct
55 Correct 84 ms 51284 KB Output is correct
56 Correct 113 ms 52452 KB Output is correct
57 Correct 141 ms 55476 KB Output is correct
58 Correct 165 ms 52420 KB Output is correct
59 Correct 139 ms 53180 KB Output is correct