# [BZOJ 3653] 谈笑风生

## 题意

$$n,q\le3\times 10^5$$.

## 题解

### 参考代码

#include <bits/stdc++.h>

namespace rvalue{
const int MAXV=3e5+10;
const int MAXE=1e6+10;
typedef long long intEx;

struct Edge{
int from;
int to;
Edge* next;
};
Edge E[MAXE];
Edge* top=E;

struct Node{
int l;
int r;
intEx sum;
Node* lch;
Node* rch;
Node(int,int);
void Insert(int,int);
intEx Query(int,int);
};
Node* N[MAXV];

int n;
int q;
int deep[MAXV];
int size[MAXV];
intEx ans[MAXV];

void Insert(int,int);
void DFS(int,int,int);
Node* Merge(Node*,Node*);

int main(){
for(int i=1;i<n;i++){
Insert(a,b);
Insert(b,a);
}
DFS(1,0,1);
for(int i=0;i<q;i++){
intEx ans=N[root]->Query(deep[root]+1,std::min(deep[root]+k,n));
ans+=1ll*std::min(k,deep[root]-1)*(size[root]-1);
printf("%lld\n",ans);
}
return 0;
}

void DFS(int root,int prt,int deep){
rvalue::size[root]=1;
rvalue::deep[root]=deep;
N[root]=new Node(1,n);
if(i->to!=prt){
DFS(i->to,root,deep+1);
rvalue::size[root]+=rvalue::size[i->to];
N[root]=Merge(N[root],N[i->to]);
}
}
N[root]->Insert(rvalue::deep[root],size[root]-1);
}

Node::Node(int l,int r):l(l),r(r),sum(0),lch(NULL),rch(NULL){}

Node* Merge(Node* L,Node* R){
if(L==NULL)
return R;
if(R==NULL)
return L;
Node* cur=new Node(L->l,R->r);
cur->sum=L->sum+R->sum;
cur->lch=Merge(L->lch,R->lch);
cur->rch=Merge(L->rch,R->rch);
return cur;
}

intEx Node::Query(int l,int r){
if(l<=this->l&&this->r<=r)
return this->sum;
else{
int mid=(this->l+this->r)>>1;
intEx ans=0;
if(l<=mid&&this->lch!=NULL)
ans+=this->lch->Query(l,r);
if(mid+1<=r&&this->rch!=NULL)
ans+=this->rch->Query(l,r);
return ans;
}
}

void Node::Insert(int x,int d){
this->sum+=d;
if(this->l!=this->r){
int mid=(this->l+this->r)>>1;
if(x<=mid){
if(this->lch==NULL)
this->lch=new Node(this->l,mid);
this->lch->Insert(x,d);
}
else{
if(this->rch==NULL)
this->rch=new Node(mid+1,this->r);
this->rch->Insert(x,d);
}
}
}

inline void Insert(int from,int to){
top->from=from;
top->to=to;
}

int x=0;
register char ch=getchar();
while(!isdigit(ch))
ch=getchar();
while(isdigit(ch)){
x=x*10+ch-'0';
ch=getchar();
}
return x;
}
}

int main(){
freopen("laugh.in","r",stdin);
freopen("laugh.out","w",stdout);
rvalue::main();
return 0;
}



posted @ 2019-03-25 18:46  rvalue  阅读(207)  评论(0编辑  收藏  举报