QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#290216 | #5075. Fenwick Tree | WilliamHu | TL | 0ms | 31104kb | C++20 | 1.3kb | 2023-12-24 16:13:24 | 2023-12-24 16:13:25 |
Judging History
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);
cin>>T;
while(T --)
{
cin>>n;
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: 100
Accepted
time: 0ms
memory: 31104kb
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 ...