huhao @ 2018-07-23 09:57:29
RT,求查错
// luogu-judger-enable-o2
/**************************************************************
File name: 1600.cpp
Author: huhao
Email: [email protected]
Create time: 7/22/2018, 3:50:23 PM
**************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define fr(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define fd(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
int read()
{
int r=0,t=1,c=getchar();
while(c<'0'||c>'9')
{
t=c=='-'?-1:1;
c=getchar();
}
while(c>='0'&&c<='9')
{
r=(r<<3)+(r<<1)+(c^48);
c=getchar();
}
return r*t;
}
namespace run
{
#define N 1000010
#define D 20
int n,m,begin[N],next[N],to[N],f[N][D+10],e,h[N],l[N],r[N],cnt,q1[N],q2[N],ans[N];
void add(int u,int v)
{
e++;
next[e]=begin[u];
begin[u]=e;
to[e]=v;
}
#define fo(i,a) for(int i=begin[a];i;i=next[i])
void dfs(int u)
{
int v;
l[u]=++cnt;
fo(i,u)
if(!h[v=to[i]])
{
h[v]=h[u]+1;
f[v][0]=u;
dfs(v);
}
r[u]=cnt;
}
int lca(int u,int v)
{
if(h[u]<h[v])swap(u,v);
fd(i,D,0)if(h[f[u][i]]>=h[v])u=f[u][i];
fd(i,D,0)if(f[u][i]!=f[v][i]){u=f[u][i];v=f[v][i];}
return u==v?u:f[u][0];
}
struct tree
{
int w;
tree *l,*r;
tree()
{
w=0;
l=r=NULL;
}
}*r1[N],*r2[N];
tree *build(int l,int r)
{
tree *k=new tree;
if(l==r)return k;
int mid=(l+r)>>1;
k->l=build(l,mid);
k->r=build(mid+1,r);
return k;
}
tree *add(tree *k,int l,int r,int pos,int w)
{
tree *t=new tree;
t->w=k->w+w;
if(l==r)return t;
int mid=(l+r)>>1;
if(pos<=mid)
{
t->r=k->r;
t->l=add(k->l,l,mid,pos,w);
}
else
{
t->l=k->l;
t->r=add(k->r,mid+1,r,pos,w);
}
return t;
}
int query(tree *k,int l,int r,int pos)
{
if(l==r)return k->w;
int mid=(l+r)>>1;
if(pos<=mid)
return query(k->l,l,mid,pos);
else
return query(k->r,mid+1,r,pos);
}
struct player
{
int pos,opt,w;
player(int a=0,int b=0,int c=0){pos=a;opt=b;w=c;}
}p1[N],p2[N];
int cmp(player a,player b)
{
return a.pos<b.pos;
}
void print(tree *k,int l,int r)
{
if(k==NULL||k->w==0)return;
if(l==r)printf("%d %d\n",l,k->w);
int mid=(l+r)>>1;
print(k->l,l,mid);
print(k->r,mid+1,r);
}
#define ran -3*n,5*n
//seg tree range
int main()
{
n=read();
m=read();
fr(i,1,n-1)
{
int u=read(),v=read();
add(u,v);
}
h[1]=1;dfs(1);
fr(i,1,D)
fr(j,1,n)
f[j][i]=f[f[j][i-1]][i-1];
fr(i,1,n)
{
int a=read();
q1[i]=h[i]+a;
q2[i]=h[i]-a;
}
// fr(i,1,n)printf("%d%c",q1[i],i==n?'\n':' ');
// fr(i,1,n)printf("%d%c",q2[i],i==n?'\n':' ');
// fr(i,1,n)printf("%d%c",l[i],i==n?'\n':' ');
// fr(i,1,n)printf("%d%c",r[i],i==n?'\n':' ');
int M1=0,M2=0;
fr(i,1,m)
{
int u=read(),v=read();
int ll=lca(u,v);
p1[++M1]=player(l[f[ll][0]],-1,h[u]);
p1[++M1]=player(l[ll],-1,h[u]);
p1[++M1]=player(l[u],1,h[u]);
p2[++M2]=player(l[v],1,2*h[ll]-h[u]);
}
sort(p1+1,p1+M1+1,cmp);
// fr(i,1,M1)printf("%d %d %d\n",p1[i].pos,p1[i].w,p1[i].opt);
// putchar(10);
// fr(i,1,M2)printf("%d %d %d\n",p2[i].pos,p2[i].w,p2[i].opt);
sort(p2+1,p2+M2+1,cmp);
r1[0]=build(ran);
r2[0]=build(ran);
fr(i,1,M1)
r1[p1[i].pos]=add(r1[p1[i-1].pos],ran,p1[i].w,p1[i].opt);
fr(i,1,M2)
r2[p2[i].pos]=add(r2[p2[i-1].pos],ran,p2[i].w,p2[i].opt);
// fr(i,1,n){printf("%d\n",i);print(r1[i],ran);}
fr(i,1,n)
{
if(r1[i]==NULL)
r1[i]=r1[i-1];
if(r2[i]==NULL)
r2[i]=r2[i-1];
}
fr(i,1,n)
ans[i]=query(r1[r[i]],ran,q1[i])-query(r1[l[i]-1],ran,q1[i])
+query(r2[r[i]],ran,q2[i])-query(r2[l[i]-1],ran,q2[i]);
fr(i,1,n)
printf("%d%c",ans[i],i==n?'\n':' ');
return 0;
}
}
by Quank123Wip @ 2018-07-23 10:03:33
主席树大佬ORZ
by 勇敢的我 @ 2018-07-25 13:03:08
主席树大佬%%%
by waterlike @ 2019-10-10 01:32:38
主席树大佬%%%
by qwq2519 @ 2020-09-27 16:55:32
主席树大佬%%%