QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#89099 | #5249. Bandits | zhouhuanyi | WA | 618ms | 27124kb | C++11 | 3.5kb | 2023-03-18 22:51:36 | 2023-03-18 22:51:37 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#define N 100000
#define inf 1e9
using namespace std;
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
struct reads
{
int num,data,cl;
bool operator < (const reads &t)const
{
return data>t.data;
}
};
vector<reads>E[N+1];
vector<reads>p[N+1];
vector<int>v[N+1];
reads st[N+1],st2[N+1];
int n,q,length,length2,rt,smz,c[N+1],sz[N+1],szt[N+1],maxp[N+1],ans[N+1];
long long depth[N+1];
char cs[N+1];
bool used[N+1],vis[N+1];
void add(int x,int y,int z,int w)
{
E[x].push_back((reads){y,z,w}),E[y].push_back((reads){x,z,w});
return;
}
void get_rt(int x)
{
vis[x]=sz[x]=1,maxp[x]=0;
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i].num]&&!vis[E[x][i].num])
get_rt(E[x][i].num),sz[x]+=sz[E[x][i].num],maxp[x]=max(maxp[x],sz[E[x][i].num]);
maxp[x]=max(maxp[x],smz-sz[x]);
if (maxp[x]<maxp[rt]) rt=x;
vis[x]=0;
return;
}
void dfs2(int x)
{
vis[x]=szt[x]=1;
for (int i=0;i<p[x].size();++i)
if (p[x][i].data>=depth[x])
st[++length]=(reads){p[x][i].num,p[x][i].data-depth[x]};
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i].num]&&!vis[E[x][i].num])
{
depth[E[x][i].num]=depth[x]+E[x][i].data;
for (int j=0;j<v[E[x][i].cl].size();++j)
if (depth[E[x][i].num]<=(int)(inf))
st2[++length2]=(reads){v[E[x][i].cl][j],depth[E[x][i].num]};
dfs2(E[x][i].num),szt[x]+=szt[E[x][i].num];
}
vis[x]=0;
return;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int d)
{
for (;x<=n;x+=lowbit(x)) c[x]+=d;
return;
}
int query(int x)
{
int res=0;
for (;x>=1;x-=lowbit(x)) res+=c[x];
return res;
}
void solve(int x)
{
int ps=0;
length=length2=0,depth[x]=0,dfs2(x),sort(st+1,st+length+1),sort(st2+1,st2+length2+1);
for (int i=1;i<=length2;++i)
{
while (ps+1<=length&&st[ps+1].data>=st2[i].data) ++ps,add(st[ps].num,1);
ans[st2[i].num]+=query(st2[i].num);
}
for (int i=1;i<=ps;++i) add(st[i].num,-1);
used[x]=1;
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i].num])
{
length=length2=ps=0,dfs2(E[x][i].num),sort(st+1,st+length+1),sort(st2+1,st2+length2+1);
for (int j=1;j<=length2;++j)
{
while (ps+1<=length&&st[ps+1].data>=st2[j].data) ++ps,add(st[ps].num,1);
ans[st2[j].num]-=query(st2[j].num);
}
for (int j=1;j<=ps;++j) add(st[j].num,-1);
length2=ps=0;
for (int j=0;j<v[E[x][i].cl].size();++j)
if (depth[v[E[x][i].cl][j]]<=(int)(inf))
st2[++length2]=(reads){v[E[x][i].cl][j],0};
for (int j=1;j<=length2;++j)
{
while (ps+1<=length&&st[ps+1].data>=st2[j].data) ++ps,add(st[ps].num,1);
ans[st2[j].num]+=query(st2[j].num);
}
for (int j=1;j<=ps;++j) add(st[j].num,-1);
}
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i].num])
rt=0,smz=szt[E[x][i].num],get_rt(E[x][i].num),solve(rt);
return;
}
int main()
{
int x,y,z,k;
n=read(),maxp[0]=inf;
for (int i=1;i<=n-1;++i) x=read(),y=read(),z=read(),add(x,y,z,i);
q=read();
for (int i=1;i<=q;++i)
{
cin>>cs[i];
if (cs[i]=='+') x=read(),k=read(),p[x].push_back((reads){i,k});
else x=read(),v[x].push_back(i);
}
smz=n,get_rt(1),solve(rt);
for (int i=1;i<=q;++i)
if (cs[i]=='?')
printf("%d\n",ans[i]);
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 618ms
memory: 27124kb
input:
100000 2670 75097 4080 87477 75802 1712 51835 36626 2883 19412 25923 5852 23976 19312 2520 82536 19514 2492 27160 66601 4483 99087 15088 3504 47050 58820 2964 37063 5696 9901 7717 1496 4891 79136 5448 4340 22575 81285 9289 96280 3803 9877 41980 32139 2855 44236 64938 3298 5983 99947 9666 95856 62545...
output:
0 0 0 2 2 5 2 2 3 4 4 7 8 9 11 10 14 12 12 10 11 10 10 9 10 11 11 9 15 11 14 13 14 16 11 17 15 13 15 14 14 20 15 20 22 22 20 17 23 23 24 29 24 26 30 31 36 28 37 39 35 34 45 39 46 45 43 46 42 49 44 50 43 47 52 50 49 57 51 56 61 58 68 66 69 69 61 63 67 63 72 74 78 72 73 78 77 73 85 76 86 82 85 76 82 8...
result:
wrong answer 144th lines differ - expected: '139', found: '140'