QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#327215#3307. Query on a Tree 1711d10xyWA 2ms7036kbC++142.1kb2024-02-14 20:37:022024-02-14 20:37:02

Judging History

你现在查看的是最新测评结果

  • [2024-02-14 20:37:02]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7036kb
  • [2024-02-14 20:37:02]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using i64=long long;
int n,m,fa[100010][25],dep[100010],top[100010],dfn[100010],raw[100010],sz[100010],tot;
basic_string<int>G[100010];
i64 tag[100010<<2],sum[100010<<2];
auto app=[](int u,int len,i64 w){sum[u]+=w*len,tag[u]+=w;};
auto pushdown=[](int u,int len){
   app(u<<1,len+1>>1,tag[u]),
   app(u<<1|1,len>>1,tag[u]),tag[u]=0;
};
void mdf(int u,int l,int r,int L,int R){
   if(L<=l&&r<=R)return app(u,r-l+1,1);
   int mid=l+r>>1;
   pushdown(u,r-l+1);
   if(L<=mid)mdf(u<<1,l,mid,L,R);
   if(R>mid)mdf(u<<1|1,mid+1,r,L,R);
   sum[u]=sum[u<<1]+sum[u<<1|1];
}
i64 qry(int u,int l,int r,int L,int R){
   if(L<=l&&r<=R)return sum[u];
   int mid=l+r>>1;
   pushdown(u,r-l+1);
   return(L<=mid?qry(u<<1,l,mid,L,R):0)+(R>mid?qry(u<<1|1,mid+1,r,L,R):0);
}
inline i64 qry(int u){return qry(1,1,n,dfn[u],dfn[u]+sz[u]-1);}
void dfs1(int u){
   sz[u]=1,dep[u]=dep[fa[u][0]]+1;
   if(fa[u][0])G[u].erase(find(begin(G[u]),end(G[u]),fa[u][0]));
   for(int i=1;fa[u][i]=fa[fa[u][i-1]][i-1];i++);
   for(int&v:G[u]){
      fa[v][0]=u,dfs1(v),sz[u]+=sz[v];
      if(sz[v]>sz[G[u][0]])swap(v,G[u][0]);
   }
}
void dfs2(int u,int t){
   top[u]=t,dfn[u]=++tot,raw[tot]=u;
   for(int v:G[u])dfs2(v,v==G[u][0]?t:v);
}
i64 findv(int u,int l,int r,i64 v){//找到第一个>v的
   if(l==r)return raw[l];
   int mid=l+r>>1;
   pushdown(u,r-l+1);
   return sum[u<<1]<=v?findv(u<<1|1,mid+1,r,v-sum[u<<1]):findv(u<<1,l,mid,v);
}
int main(){
   cin>>n;
   for(int i=1,u,v;i<n;i++)scanf("%d%d",&u,&v),G[u]+=v,G[v]+=u;
   dfs1(1),dfs2(1,1);
   cin>>m;
   for(int op,u,v;m--;){
      scanf("%d%d",&op,&u);
      if(op==1)mdf(1,1,n,dfn[u],dfn[u]+sz[u]-1);
      else{
         scanf("%d",&v);
         for(;top[u]!=top[v];u=fa[top[u]][0]){
            if(dep[top[u]]<dep[top[v]])swap(u,v);
            mdf(1,1,n,dfn[top[u]],dfn[u]);
         }
      }
      i64 h=sum[1]/2;
      u=findv(1,1,n,h);
      for(int i=20;i>=0;i--)(v=fa[u][i])&&qry(v)<=h&&(u=v);
      printf("%d\n",qry(u)>h?u:(u==1?1:fa[u][0]));
   }
   return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 7036kb

input:

7
1 6
1 7
7 3
3 2
7 5
5 4
4
1 2
1 4
1 6
2 6 7

output:

2
7
7
1

result:

ok 4 lines

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 6952kb

input:

15
1 2
1 3
2 4
3 7
6 2
3 8
4 9
2 5
5 11
7 13
10 4
11 15
12 5
14 7
11
2 6 11
1 6
2 10 9
1 9
2 2 6
1 4
2 7 6
1 2
2 8 13
1 10
2 8 15

output:

6
6
6
2
6
4
2
2
2
2
2

result:

wrong answer 1st lines differ - expected: '2', found: '6'