# BZOJ 4034: [HAOI2015]T2

Time Limit: 10 Sec
Memory Limit: 256 MB

Description

Input

Output

Sample Input

5 5

1 2 3 4 5

1 2

1 4

2 3

2 5

3 3

1 2 1

3 5

2 1 2

3 3
Sample Output

6

9

13

HINT

Source

#### Code####

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<vector>
using namespace std;
#define PA pair<int,int>
const int N=0,M=0;
{int s=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
return s*f;
}
int n,m;
int val[100005];
int lpos[100005],rpos[100005],up[100005],dfn,siz[100005],fa[100005];
long long sum[1<<18],tag[1<<18];
int be[100005],bn[200005],bv[200005],bw=1;
void put(int u,int v)
{bw++;bn[bw]=be[u];be[u]=bw;bv[bw]=v;}
int ss;
void dfs1(int x)
{
for(int i=be[x];i;i=bn[i])
if(bv[i]!=fa[x])
fa[bv[i]]=x,
dfs1(bv[i]),
siz[x]+=siz[bv[i]];
siz[x]++;
}
void dfs2(int x,int UP)
{
int ma=0;
for(int i=be[x];i;i=bn[i])
if(siz[bv[i]]>siz[ma]&&bv[i]!=fa[x])
ma=bv[i];
up[x]=UP;lpos[x]=rpos[x]=++dfn;
if(!ma)return;
dfs2(ma,UP);
rpos[x]=max(rpos[x],rpos[ma]);
for(int i=be[x];i;i=bn[i])
{int v=bv[i];if(v==ma||v==fa[x])continue;
dfs2(v,v);
rpos[x]=max(rpos[x],rpos[v]);
}
}
void down(int l,int r,int x)
{
if(l==r)return;
long long t=tag[x];tag[x]=0;int mid=l+r>>1;
tag[x<<1]+=t,sum[x<<1]+=t*(mid-l+1);
tag[x<<1|1]+=t,sum[x<<1|1]+=t*(r-mid);
}
void add(int x,int l,int r,int L,int R,long long s)
{
if(L>R)return;
if(tag[x])down(l,r,x);
if(l==L&&r==R)
{tag[x]+=s;sum[x]+=(r-l+1)*s;return;}
int mid=l+r>>1;
sum[x]=sum[x<<1]+sum[x<<1|1];
}
long long ask(int x,int l,int r,int L,int R)
{
if(L>R)return 0;
if(tag[x])down(l,r,x);
if(l==L&&r==R)
return sum[x];
int mid=l+r>>1;
}
{
long long ans=0;
for(;x;x=fa[up[x]])
return ans;
}
int main()
{
for(int i=1;i<=n;i++)
for(int i=1;i<n;i++)
put(s,f);
put(f,s);
}
dfs1(1);
dfs2(1,1);
for(int i=1;i<=n;i++)
while(m--)