QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#89098 | #5249. Bandits | zhouhuanyi | WA | 620ms | 25896kb | C++11 | 3.5kb | 2023-03-18 22:48:38 | 2023-03-18 22:48:41 |
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=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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 620ms
memory: 25896kb
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:
-18 -18 -45 -120 -120 -29 -120 -120 -119 -136 -122 -157 -229 -264 -192 -216 -56 -167 -169 -292 -336 -294 -201 -257 -294 -351 -192 -307 -110 -324 -210 -213 -210 -120 -354 -96 -183 -372 -181 -260 -390 -215 -471 -249 -136 -136 -548 -502 -258 -364 -636 -149 -365 -798 -923 -334 -139 -758 -138 -152 -785 -...
result:
wrong answer 1st lines differ - expected: '0', found: '-18'