Submission #1102113

# Submission time Handle Problem Language Result Execution time Memory
1102113 2024-10-17T13:31:33 Z epicci23 Inside information (BOI21_servers) C++17
50 / 100
255 ms 54464 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 + 2e4 + 5;
const int LOG = 18;
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[1]) 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;
      cevapla.push_back({b,a,ed,++cp});
    }
    else{
      int a;
      cin >> a;
      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<=q;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];
      |                   ^
# Verdict Execution time Memory Grader output
1 Correct 40 ms 26672 KB Output is correct
2 Correct 45 ms 27376 KB Output is correct
3 Correct 46 ms 27744 KB Output is correct
4 Correct 51 ms 27560 KB Output is correct
5 Correct 51 ms 27912 KB Output is correct
6 Correct 45 ms 27536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 26672 KB Output is correct
2 Correct 45 ms 27376 KB Output is correct
3 Correct 46 ms 27744 KB Output is correct
4 Correct 51 ms 27560 KB Output is correct
5 Correct 51 ms 27912 KB Output is correct
6 Correct 45 ms 27536 KB Output is correct
7 Incorrect 41 ms 26572 KB Extra information in the output file
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 40 ms 26828 KB Output is correct
2 Correct 133 ms 43500 KB Output is correct
3 Correct 126 ms 43444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 26828 KB Output is correct
2 Correct 133 ms 43500 KB Output is correct
3 Correct 126 ms 43444 KB Output is correct
4 Incorrect 35 ms 26572 KB Extra information in the output file
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 36 ms 26664 KB Output is correct
2 Correct 175 ms 54436 KB Output is correct
3 Correct 179 ms 54460 KB Output is correct
4 Correct 174 ms 54252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 26664 KB Output is correct
2 Correct 175 ms 54436 KB Output is correct
3 Correct 179 ms 54460 KB Output is correct
4 Correct 174 ms 54252 KB Output is correct
5 Incorrect 33 ms 26572 KB Extra information in the output file
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 36 ms 26616 KB Output is correct
2 Correct 127 ms 43976 KB Output is correct
3 Correct 136 ms 44476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 26616 KB Output is correct
2 Correct 127 ms 43976 KB Output is correct
3 Correct 136 ms 44476 KB Output is correct
4 Incorrect 42 ms 26564 KB Extra information in the output file
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 33 ms 26672 KB Output is correct
2 Correct 180 ms 54460 KB Output is correct
3 Correct 205 ms 54460 KB Output is correct
4 Correct 185 ms 54204 KB Output is correct
5 Correct 42 ms 26564 KB Output is correct
6 Correct 152 ms 43868 KB Output is correct
7 Correct 153 ms 44332 KB Output is correct
8 Correct 216 ms 44864 KB Output is correct
9 Correct 185 ms 44912 KB Output is correct
10 Correct 234 ms 50108 KB Output is correct
11 Correct 247 ms 49340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 26672 KB Output is correct
2 Correct 180 ms 54460 KB Output is correct
3 Correct 205 ms 54460 KB Output is correct
4 Correct 185 ms 54204 KB Output is correct
5 Correct 42 ms 26564 KB Output is correct
6 Correct 152 ms 43868 KB Output is correct
7 Correct 153 ms 44332 KB Output is correct
8 Correct 216 ms 44864 KB Output is correct
9 Correct 185 ms 44912 KB Output is correct
10 Correct 234 ms 50108 KB Output is correct
11 Correct 247 ms 49340 KB Output is correct
12 Incorrect 35 ms 26636 KB Extra information in the output file
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 42 ms 26564 KB Output is correct
2 Correct 60 ms 27600 KB Output is correct
3 Correct 45 ms 27584 KB Output is correct
4 Correct 53 ms 27600 KB Output is correct
5 Correct 50 ms 28056 KB Output is correct
6 Correct 45 ms 27620 KB Output is correct
7 Correct 37 ms 26728 KB Output is correct
8 Correct 125 ms 43444 KB Output is correct
9 Correct 141 ms 43588 KB Output is correct
10 Correct 35 ms 26644 KB Output is correct
11 Correct 203 ms 54464 KB Output is correct
12 Correct 189 ms 54464 KB Output is correct
13 Correct 212 ms 54204 KB Output is correct
14 Correct 40 ms 26668 KB Output is correct
15 Correct 149 ms 43964 KB Output is correct
16 Correct 144 ms 44344 KB Output is correct
17 Correct 208 ms 44752 KB Output is correct
18 Correct 199 ms 44848 KB Output is correct
19 Correct 221 ms 50116 KB Output is correct
20 Correct 255 ms 49164 KB Output is correct
21 Correct 137 ms 43964 KB Output is correct
22 Correct 123 ms 44220 KB Output is correct
23 Correct 139 ms 44380 KB Output is correct
24 Correct 150 ms 44476 KB Output is correct
25 Correct 204 ms 48228 KB Output is correct
26 Correct 169 ms 43964 KB Output is correct
27 Correct 159 ms 43964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 26564 KB Output is correct
2 Correct 60 ms 27600 KB Output is correct
3 Correct 45 ms 27584 KB Output is correct
4 Correct 53 ms 27600 KB Output is correct
5 Correct 50 ms 28056 KB Output is correct
6 Correct 45 ms 27620 KB Output is correct
7 Correct 37 ms 26728 KB Output is correct
8 Correct 125 ms 43444 KB Output is correct
9 Correct 141 ms 43588 KB Output is correct
10 Correct 35 ms 26644 KB Output is correct
11 Correct 203 ms 54464 KB Output is correct
12 Correct 189 ms 54464 KB Output is correct
13 Correct 212 ms 54204 KB Output is correct
14 Correct 40 ms 26668 KB Output is correct
15 Correct 149 ms 43964 KB Output is correct
16 Correct 144 ms 44344 KB Output is correct
17 Correct 208 ms 44752 KB Output is correct
18 Correct 199 ms 44848 KB Output is correct
19 Correct 221 ms 50116 KB Output is correct
20 Correct 255 ms 49164 KB Output is correct
21 Correct 137 ms 43964 KB Output is correct
22 Correct 123 ms 44220 KB Output is correct
23 Correct 139 ms 44380 KB Output is correct
24 Correct 150 ms 44476 KB Output is correct
25 Correct 204 ms 48228 KB Output is correct
26 Correct 169 ms 43964 KB Output is correct
27 Correct 159 ms 43964 KB Output is correct
28 Incorrect 37 ms 26480 KB Extra information in the output file
29 Halted 0 ms 0 KB -