QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#290209#5075. Fenwick TreeWilliamHuTL 6ms31576kbC++201.2kb2023-12-24 16:09:592023-12-24 16:09:59

Judging History

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

  • [2023-12-24 16:09:59]
  • 评测
  • 测评结果:TL
  • 用时:6ms
  • 内存:31576kb
  • [2023-12-24 16:09:59]
  • 提交

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()
{
	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;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 31576kb

input:

3
5
10110
5
00000
5
11111

output:

3
0
3

result:

ok 3 number(s): "3 0 3"

Test #2:

score: -100
Time Limit Exceeded

input:

100000
10
0000000000
10
0000000100
10
0000000000
10
1000000000
10
0000010000
10
0000000000
10
0000000000
10
0000000000
10
0100000000
10
0000000000
10
0000000001
10
0000001000
10
0000000000
10
0000000000
10
0000000001
10
0000100000
10
0010000000
10
0000000000
10
0010000000
10
0000000001
10
0000000000...

output:

0
1
0
2
2
0
0
0
2
0
1
2
0
0
1
2
2
0
2
1
0
0
2
2
2
0
2
2
2
0
2
2
0
0
2
2
0
0
2
0
2
2
0
0
0
0
0
0
0
2
2
2
2
0
1
0
2
2
0
2
2
0
2
2
0
1
0
2
0
0
2
2
0
0
0
1
2
0
2
0
0
0
0
2
2
0
0
0
0
0
0
2
0
2
2
0
2
2
2
0
0
0
0
0
0
0
1
0
0
0
2
0
2
0
0
0
2
0
2
0
0
2
0
0
0
1
0
0
1
2
0
0
2
0
2
0
0
2
0
2
0
0
0
2
0
0
2
2
1
0
...

result: