QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#198189#3307. Query on a Tree 17phenomenaRE 0ms0kbC++114.5kb2023-10-03 08:59:412023-10-03 08:59:41

Judging History

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

  • [2023-10-03 08:59:41]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-10-03 08:59:41]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using i64=long long;
#define L(i,l,r) for(int i=(l);i<=(r);++i)
#define R(i,r,l) for(int i=(r);i>=(l);--i)
namespace nqio {
	const int mxbf=1<<20;
	char ib[mxbf],*p1=ib,*p2=ib,c;
	bool s;
	struct q {
		void r(char&x) {
//			return x=getchar(),void();
			x=p1==p2&&(p2=(p1=ib)+fread(ib,1,mxbf,stdin),p1==p2)?EOF:*p1++;
		} q&operator>>(char&x) {
			for(r(x); x<=32; r(x));
			return *this;
		} q&operator>>(char*x) {
			for(r(*x); *x<=32; r(*x));
			while(*x>32)r(*++x);
			*x=0;
			return *this;
		} q&operator>>(string&x) {
			for(r(c); c<=32; r(c));
			x=c,r(c);
			while(c>32)x+=c,r(c);
			return *this;
		} template<typename t>q&operator>>(t&x) {
			for(r(c),s=0; !isdigit(c); r(c))s|=c==45;
			for(x=0; isdigit(c); r(c))x=x*10+(c^48);
			if(s)x=-x;
			return *this;
		}
	} qi;
	char ob[mxbf],*pp=ob,*pd=ob+mxbf,stk[64],*tp=stk;
	struct p {
		void f() {
			fwrite(ob,1,pp-ob,stdout),pp=ob;
		}~p() {
			f();
		} void w(char x) {
			if(*pp++=x,pp==pd)f();
		} p&operator<<(char x) {
			w(x);
			return *this;
		} p&operator<<(char*x) {
			while(*x)w(*x++);
			return *this;
		} p&operator<<(const char*x) {
			while(*x)w(*x++);
			return *this;
		} p&operator<<(string x) {
			for(char v:x)w(v);
			return *this;
		} template<typename t>p&operator<<(t x) {
			if(!x)return w(48),*this;
			if(x<0)w(45),x=-x;
			while(x)*tp++=48|x%10,x/=10;
			while(tp!=stk)w(*--tp);
			return *this;
		}
	} qo;
}
using nqio::qi;
using nqio::qo;

namespace yihlaushih {
	bool _114;

	int n;
	const int maxn=3e5+5;
	const int maxm=maxn<<1;
	int et,lst[maxn],nxt[maxm],ed[maxm];
	inline void addedge(int x,int y) {
		ed[++et]=y,nxt[et]=lst[x],lst[x]=et;
		return ;
	}
	int fa[maxn][20],sz[maxn],hvy[maxn],dep[maxn];
	void dfs(int u) {
		assert(dep[u]>=0);
		assert(__lg(dep[u])==(int)log2(dep[u]));
		L(i,1,(int)log2(dep[u])) {
			fa[u][i]=fa[fa[u][i-1]][i-1];
		}
		sz[u]=1;
		for(int i=lst[u]; i; i=nxt[i]) {
			int v=ed[i];
			if(v!=fa[u][0]) {
				dep[v]=dep[u]+1,fa[v][0]=u,dfs(v);
				sz[u]+=sz[v];
				if(sz[hvy[u]]<sz[v])hvy[u]=v;
			}
		}
		return ;
	}
	int cntnid,dfn[maxn],low[maxn],bc[maxn],top[maxn];
	void chain(int u, int TOP) {
		dfn[u]=++cntnid,bc[cntnid]=u;
		top[u]=TOP;
		if(hvy[u])chain(hvy[u],TOP);
		for(int i=lst[u]; i; i=nxt[i]) {
			int v=ed[i];
			if(v!=fa[u][0]&&v!=hvy[u]) {
				chain(v,v);
			}
		}
		low[u]=cntnid;
		return ;
	}
#define ls (p<<1)
#define rs (ls|1)
	const int maxt=maxn<<3;
	i64 sum[maxt],lzy[maxt];
#define pushup(p) (sum[p]=sum[ls]+sum[rs])
#define pushdown(p) (sum[ls]+=(mid-l+1)*lzy[p],lzy[ls]+=lzy[p],sum[rs]+=(r-mid)*lzy[p],lzy[rs]+=lzy[p],lzy[p]=0)
	void modify(int p,int l,int r,int ql,int qr,int w) {
		if(ql<=l&&r<=qr) {
			sum[p]+=1ll*w*(r-l+1);
			lzy[p]+=w;
			return ;
		}
		int mid=l+r>>1;
		if(lzy[p])pushdown(p);
		if(ql<=mid)modify(ls,l,mid,ql,qr,w);
		if(mid<qr)modify(rs,mid+1,r,ql,qr,w);
		pushup(p);
		return ;
	}
	i64 query(int p, int l, int r, int ql, int qr) {
		if(ql<=l&&r<=qr) {
			return sum[p];
		}
		int mid=l+r>>1;
		i64 ret=0;
		if(lzy[p])pushdown(p);
		if(ql<=mid)ret=query(ls,l,mid,ql,qr);
		if(mid<qr)ret+=query(rs,mid+1,r,ql,qr);
		return ret;
	}
	int quermid(int p, int l, int r, i64 val) {
		if(l==r)return bc[l];
		int mid=l+r>>1;
		if(lzy[p])pushdown(p);
		if(sum[ls]>=val)return quermid(ls,l,mid,val);
		return quermid(rs,mid+1,r,val-sum[ls]);
	}
#undef ls
#undef rs
	bool _514;
	int _main(void) {
		fprintf(stderr,"floor memory:%.6lf MB\n",(&_114-&_514)/1024./1024.);
//		freopen("centroid.in","r",stdin);
//		freopen("centroid.out","w",stdout);
		qi>>n;
		L(i,1,n-1) {
			int u,v;
			qi>>u>>v;
			addedge(u,v);
			addedge(v,u);
		}
		dfs(1),chain(1,1);
		int q;
		qi>>q;
		while(q--) {
			int op;
			qi>>op;
			if(op==1) {
				int u,w=1;
				qi>>u;
				modify(1,1,n,dfn[u],low[u],w);
			} else {
				int x,y,w=1;
				qi>>x>>y;
				while(top[x]!=top[y]) {
					if(dep[top[x]]<dep[top[y]])swap(x,y);
					modify(1,1,n,dfn[top[x]],dfn[x],w);
					x=fa[top[x]][0];
				}
				if(dep[x]<dep[y])swap(x,y);
				modify(1,1,n,dfn[y],dfn[x],w);
			}
			int u=quermid(1,1,n,sum[1]+1>>1);
			for(int i=__lg(n); i>=0; --i) {
				if(fa[u][i]&&(query(1,1,n,dfn[fa[u][i]],low[fa[u][i]])<<1)<=sum[1]) {
					u=fa[u][i];
				}
			}
			if(fa[u][0]&&(query(1,1,n,dfn[u],low[u])<<1)<=sum[1]) {
				u=fa[u][0];
			}
			qo<<u<<'\n';
		}
		return 0;
	}
}
int main() {
	return yihlaushih::_main(),0;
}

詳細信息

Test #1:

score: 0
Runtime Error

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:


result: