QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#290215#5075. Fenwick TreeWilliamHuWA 8ms31384kbC++201.3kb2023-12-24 16:12:182023-12-24 16:12:18

Judging History

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

  • [2023-12-24 16:12:18]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:31384kb
  • [2023-12-24 16:12:18]
  • 提交

answer

#include <bits/stdc++.h>
//#define int long long
using namespace std;
int n, T, a[1000010], f[1000010];
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(c != EOF and !isdigit(c))
	{
		if(c == '-')f = -1;
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}
int lowbit(int x){
	return x & (-x);
}
string s;
bool vis[1000010];
vector<int>l[1000010];
void dfs(int u, int fa)
{
	vis[u] = 1;
	int x = 0, x2 = 0;
	for(int i = 0;i < l[u].size();i ++)
	{
		int v = l[u][i];
		if(v == fa)continue;
		dfs(v, u);
		if(f[v] and a[v])x ++;
		f[u] += f[v];
	}
	if(a[u])
	{
		if(x == 0)f[u] ++;
	}
	else 
	{
		if(x == 1)f[u] ++;
	}
}
signed main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);
	T = read();
	while(T --)
	{
		n = read();
		int cnt = 0;
		cin>>s;
		for(int i = 1;i <= n;i ++)
		{
			a[i] = s[i - 1] - '0';
		}
		for(int i = 1;i <= n;i ++)
		{
			int x = i + lowbit(i);
			l[i].push_back(x);
			l[x].push_back(i);
		}
		for(int i = n;i >= 1;i --)
		{
			if(! vis[i])
			{
				dfs(i, -1);
				cnt += f[i];
			}
		}
		cout<<cnt<<endl;
		for(int i = 0;i <= n;i ++)
		{
			vis[i] = a[i] = f[i] = 0;
			l[i].clear();
		}
	}
	return 0;
}


详细

Test #1:

score: 0
Wrong Answer
time: 8ms
memory: 31384kb

input:

3
5
10110
5
00000
5
11111

output:

3
5064
3

result:

wrong answer 2nd numbers differ - expected: '0', found: '5064'