刘张懿 @ 2020-11-30 19:58:13
样例都过了,对着题解改瞎了
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(register int i=a;i<=b;++i)
#define Rep(i,a,b) for(register int i=a;i>=b;--i)
inline int read()
{
bool f=0;int x=0;char ch;
do{ch=getchar();f|=(ch=='-');}while(!isdigit(ch));
do{x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}while(isdigit(ch));
return f?-x:x;
}
inline void write(int x)
{
if(x<0)x=-x,putchar('-');
if(x>9)write(x/10);putchar(x%10+'0');
}
inline void writesp(int x)
{
write(x);putchar(' ');
}
inline void writeln(int x)
{
write(x);puts("");
}
const int maxn=6e5+5;
int head[maxn],to[maxn<<1|1],nxt[maxn<<1|1],tot=0;
void addedge(int u,int v)
{
nxt[++tot]=head[u];
head[u]=tot;
to[tot]=v;
}
int dfn[maxn],dep[maxn],siz[maxn],cnt=0;
void dfs(int u,int fa)
{
dfn[u]=++cnt;
siz[u]=1;
dep[u]=dep[fa]+1;
for(register int i=head[u];i;i=nxt[i])
{
int v=to[i];
if(v==fa)continue;
dfs(v,u);
siz[v]=siz[u]+1;
}
}
int tree[maxn];
inline int lowbit(int x){return x&(-x);}
void add(int x,int k)
{
for(;x<=maxn;x+=lowbit(x))
{
tree[x]+=k;
}
}
int sum(int x)
{
int res=0;
for(;x;x-=lowbit(x))
{
res+=tree[x];
}
return res;
}
struct point
{
int dep;
int dfn;
int val;
bool operator <(const point &b)const
{
return dep<b.dep;
}
}a[maxn];
int res[maxn];
struct query
{
int x,y,z,id;
bool operator <(const query &b)const
{
return z<b.z;
}
}q[maxn<<1|1];
int main()
{
int n=read(),m=read();
rep(i,1,n-1)
{
int u=read(),v=read();
addedge(u,v);addedge(v,u);
}
dfs(1,0);
rep(i,1,n)
{
// --dep[i];
a[i]=(point){dep[i],dfn[i],siz[i]-1};
}
rep(i,1,m)
{
int p=read(),k=read();
res[i]+=min(dep[p],k)*(siz[p]-1);
q[(i<<1)-1]=(query){dfn[p],dfn[p]+siz[p]-1,dep[p],-i};
q[i<<1]=(query){dfn[p],dfn[p]+siz[p]-1,dep[p]+k,i};
}
sort(a+1,a+1+n);sort(q+1,q+1+(m<<1));
for(register int i=1,j=1;i<=(m<<1);++i)
{
while(j<=n&&a[j].dep<q[i].z)add(a[j].dfn,a[j].val),++j;
register int cur=abs(q[i].id);
if(q[i].id<0)res[q[i].id]-=sum(q[i].y)-sum(q[i].x);
else res[q[i].id]+=sum(q[i].y)-sum(q[i].x);
}
rep(i,1,m)
{
writeln(res[i]);
}
return 0;
}
by AuCloud @ 2020-11-30 20:00:59
I AK IOI!
by _maze @ 2020-11-30 21:11:07
好惨一张懿,发个求助贴唯一回复的还是个jc。。。