QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#404640#7736. Red Black Treezhangtx123Compile Error//C++143.5kb2024-05-04 11:57:222024-05-04 11:57:23

Judging History

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

  • [2024-05-04 11:57:23]
  • 评测
  • [2024-05-04 11:57:22]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
 #define debug(...) fprintf(stdout, ##__VA_ARGS__)
 #define debag(...) fprintf(stderr, ##__VA_ARGS__)
#else
 #define debug(...) void(0)
 #define debag(...) void(0)
#endif
//#define int long long
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;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 200010
struct node {
	vector<int>h1, h2; 
	int c, sum; 
	void clear() {
		h1.clear(); h2.clear(); 
		c = sum = 0; 
	}
}a[N]; 
int dep[N], sum[N], son[N], h[N], ans[N]; 
int id[N], top, hi[N]; 
vector<int> G[N]; 
int n, m, i, j, k, T;
char str[N]; 

void dfs1(int x) {
	sum[x] = str[x] - '0'; 
	dep[x] = 1; son[x] = 0; 
	for(int y : G[x]) {
		dfs1(y); sum[x] += sum[y]; 
		dep[x] = max(dep[x], dep[y] + 1); 
		if(dep[y] < dep[son[x]]) son[x] = y; 
	}
//	debug("son[%d] = %d\n", x, son[x]); 
}

void tran(int i, int h[N], int len) {
	int k = 0; 
	for(int x : a[i].h1) {
		if(k == len) return ; 
		h[++k] += x; 
	}
	for(int x = 1; x <= a[i].c; ++x) {
		if(k == len) return ; 
		h[++k] += 0; 
	}
	for(int y = a[i].h2.size() - 1; y >= 0; --y) {
		int x = a[i].h2[y]; 
		if(k == len) return ; 
		h[++k] += x; 
	}
//	debug("??? %d == %d\n", k, len); 
//	assert(k == len); 
}

void itran(int i, int h[N], int len) {
	int k = 0, k2; a[i].clear(); 
	for(k = 1; k <= len && h[k] < 0; ++k) a[i].h1.pb(h[k]), a[i].sum += h[k]; 
	for(; k <= len && h[k] == 0; ++k) a[i].c++; 
	for(k2 = len; k2 >= k && h[k2] > 0; --k2) a[i].h2.pb(h[k2]); 
//	debug("??? %d + 1 == %d\n", k2, k); 
//	assert(k2 + 1 == k); 
}

void dfs2(int x) {
	int &i = id[x]; 
	if(!son[x]) {
		i = ++top; a[i].clear(); 
		if(str[x] == '0') a[i].h2.pb(1); 
		else a[i].h1.pb(-1), a[i].sum -= 1; 
		ans[x] = sum[x] + a[i].sum; 
//		debug("For %d is %d\n", x, a[i].sum); 
		return ; 
	}
	dfs2(son[x]); i = id[son[x]]; 
	for(int y : G[x]) if(y != son[x]) {
		dfs2(y); k = 0; j = id[y]; 
		int len = a[i].h1.size() + a[i].c + a[i].h2.size(); 
		for(k = 1; k <= len; ++k) h[k] = hi[k] = 0; 
//		debug("len is %d[for %d com(%d %d)]\n", len, x, son[x], y); 
		tran(i, h, len); tran(j, hi, len); 
//		debug("># "); for(k = 1; k <= len; ++k) debug("%d ", h[k]); debug("\n"); 
//		debug("># "); for(k = 1; k <= len; ++k) debug("%d ", hi[k]); debug("\n"); 
		for(k = 1; k <= len; ++k) h[k] += hi[k]; 
//		debug("# "); for(k = 1; k <= len; ++k) debug("%d ", h[k]); debug("\n"); 
		itran(i, h, len); //tran(i, h, len); 
//		for(k = 1; k <= len; ++k) h[k] = 0; 
//		debug("#check "); for(k = 1; k <= len; ++k) debug("%d ", h[k]); debug("\n"); 
	}
	if(str[x] == '0') a[i].h2.pb(1); 
	else a[i].h1.pb(-1), a[i].sum -= 1; 
	ans[x] = sum[x] + a[i].sum; 
//	debug("For %d is %d[%d]\n", x, a[i].sum, ans[x]); 	
}

signed main()
{
	#ifdef LOCAL
	  freopen("in.txt", "r", stdin);
	  freopen("out.txt", "w", stdout);
	#endif
//	srand(time(NULL));
	T = read();
	while(T--) {
		++cnt; 
		n = read(); scanf("%s", str + 1); top = 0; dep[0] = 1e9; 
		if(cnt == 325) {
			printf("%s ", str + 1); 
			for(i = 2; i <= n; ++i) k = read(), printf("%d ", k); 
			printf("\n"); 
		}
		for(i = 1; i <= n; ++i) G[i].clear(); 
		for(i = 2; i <= n; ++i) k = read(), G[k].pb(i); 
		dfs1(1); dfs2(1); 
		for(i = 1; i <= n; ++i) printf("%d ", ans[i]); 
		printf("\n"); 
	}

	return 0;
}

Details

answer.code: In function ‘int main()’:
answer.code:115:19: error: ‘cnt’ was not declared in this scope; did you mean ‘int’?
  115 |                 ++cnt;
      |                   ^~~
      |                   int
answer.code:116:34: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  116 |                 n = read(); scanf("%s", str + 1); top = 0; dep[0] = 1e9;
      |                             ~~~~~^~~~~~~~~~~~~~~