QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#327700#3307. Query on a Tree 17WRuperDCompile Error//C++143.6kb2024-02-15 12:21:022024-02-15 12:21:02

Judging History

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

  • [2024-02-15 12:21:02]
  • 评测
  • [2024-02-15 12:21:02]
  • 提交

answer

// Problem: I. Query On A Tree 17
// URL: https://codeforces.com/gym/102759/problem/I
// Writer: WRuperD
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mininf = 1e9 + 7;
#define int long long
#define pb emplace_back
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');}
#define put() putchar(' ')
#define endl puts("")
const int MAX = 2e5 + 10;
int n;
vector <int> g[MAX];

int fa[MAX][30];
int siz[MAX], son[MAX];

void dfs(int u, int father){
	siz[u] = 1;
	fa[u][0] = father; 
	for(int v : g[u]){
		if(v == father)	continue;
		dfs(v, u);
		siz[u] += siz[v];
		if(siz[v] > siz[son[u]])	son[u] = v;
	}
}

int dfn[MAX], top[MAX], dfx[MAX], clk;
int dep[MAX];

void dfs2(int u, int topu){
	dep[u] = dep[fa[u][0]] + 1;
	top[u] = topu, dfn[u] = ++clk, dfx[clk] = u;
	if(son[u])	dfs2(son[u], topu);
	for(int v : g[u]){
		if(v == fa[u][0] or v == son[u])	continue;
		dfs2(v, v);
	}
}

int s[MAX << 1], tag[MAX << 1];

void pushup(int x){
	s[x] = s[x << 1] + s[x << 1 | 1];
}

void pushdown(int x, int l, int r){
	int mid = (l + r) >> 1;
	s[x << 1] += tag[x] * (mid - l + 1), s[x << 1 | 1] += (r - mid) * tag[x];
	tag[x << 1] += tag[x], tag[x << 1 | 1] += tag[x];
	tag[x] = 0;+
	return ;
}

void build(int l, int r, int x){
	tag[x] = 0;
	if(l == r){
		s[x] = 1;
		return ;
	}
	int mid = (l + r) >> 1;
	build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
	pushup(x);
}

void add(int l, int r, int dl, int dr, int x){
	if(dl <= l and r <= dr){
		s[x] += r - l + 1;
		tag[x]++;
		return ;
	}
	pushdown(x, l, r);
	int mid = (l + r) >> 1;
	if(dl <= mid)	add(l, mid, dl, dr, x << 1);
	if(dr > mid)	add(mid + 1, r, dl, dr, x << 1 | 1);
	pushup(x);
}

int query(int l, int r, int dl, int dr, int x){
	if(dl <= l and r <= dr)	return s[x];
	pushdown(x, l, r);
	int mid = (l + r) >> 1, ans = 0;
	if(dl <= mid)	ans += query(l, mid, dl, dr, x << 1);
	if(dr > mid)	ans += query(mid + 1, r, dl, dr, x << 1 | 1);
	pushup(x);
	return ans;
}

int search(int l, int r, int val, int x){
	if(l == r)	return dfx[l];
	int mid = (l + r) >> 1;
	if(s[x << 1] < val)	return search(mid + 1, r, val - s[x << 1], x << 1 | 1);
	return search(l, mid, val, x << 1);	
}


void Add(int u, int v){
	while(top[u] != top[v]){
		if(dep[top[u]] < dep[top[v]])	swap(u, v);
		add(1, n, dfn[top[u]], dfn[u], 1);
		u = fa[top[u]][0];
	}
	if(dfn[u] > dfn[v])	swap(u, v);
	add(1, n, dfn[u], dfn[v], 1);
}

void solve(){
	n = read();
	for(int i = 1; i < n; i++){
		int u = read(), v = read();
		g[u].pb(v), g[v].pb(u);
	}
	dfs(1, 1), dfs2(1, 1);
	// build(1, n, 1);
	for(int i = 1; i <= 29; i++){
		for(int j = 1; j <= n; j++){
			fa[j][i] = fa[fa[j][i - 1]][i - 1];
		}
	}
	int q = read();
	for(int i = 1; i <= q; i++){
		int op = read();
		if(op == 1){
			int x = read();
			add(1, n, dfn[x], dfn[x] + siz[x] - 1, 1);
		}else{
			int u = read(), v = read();
			Add(u, v);
		}
		// write(s[1]), endl;
		int x = search(1, n, (s[1]) / 2 + 1, 1);
		for(int i = 29; i >= 0; i--){
			while (x != fa[x][i] and query(1, n, dfn[fa[x][i]], dfn[fa[x][i]] + siz[fa[x][i]] - 1, 1) <= s[1] / 2){
				x = fa[x][i];
			}
		}
		// write(x), endl;
		if(query(1, n, dfn[x], dfn[x] + siz[x] - 1, 1) <= s[1] / 2){
			x = fa[x][0];
		}
		write(x), endl;
	}
} 

signed main(){
	int t = 1;
	while(t--)	solve();
	return 0;
}

Details

answer.code: In function ‘void pushdown(long long int, long long int, long long int)’:
answer.code:59:9: error: expected primary-expression before ‘return’
   59 |         return ;
      |         ^~~~~~