BZOJ4012 [HNOI2015]开店

Description

 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到

人生哲学。最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱。这样的
想法当然非常好啦,但是她们也发现她们面临着一个问题,那就是店开在哪里,面
向什么样的人群。很神奇的是,幻想乡的地图是一个树形结构,幻想乡一共有 n
个地方,编号为 1 到 n,被 n-1 条带权的边连接起来。每个地方都住着一个妖怪,
其中第 i 个地方的妖怪年龄是 x_i。妖怪都是些比较喜欢安静的家伙,所以它们并
不希望和很多妖怪相邻。所以这个树所有顶点的度数都小于或等于 3。妖怪和人一
样,兴趣点随着年龄的变化自然就会变化,比如我们的 18 岁少女幽香和八云紫就
比较喜欢可爱的东西。幽香通过研究发现,基本上妖怪的兴趣只跟年龄有关,所以
幽香打算选择一个地方 u(u为编号),然后在 u开一家面向年龄在 L到R 之间(即
年龄大于等于 L、小于等于 R)的妖怪的店。也有可能 u这个地方离这些妖怪比较
远,于是幽香就想要知道所有年龄在 L 到 R 之间的妖怪,到点 u 的距离的和是多
少(妖怪到 u 的距离是该妖怪所在地方到 u 的路径上的边的权之和) ,幽香把这个
称为这个开店方案的方便值。幽香她们还没有决定要把店开在哪里,八云紫倒是准
备了很多方案,于是幽香想要知道,对于每个方案,方便值是多少呢。
 

Input

 第一行三个用空格分开的数 n、Q和A,表示树的大小、开店的方案个数和妖

怪的年龄上限。 
第二行n个用空格分开的数 x_1、x_2、…、x_n,x_i 表示第i 个地点妖怪的年
龄,满足0<=x_i<A。(年龄是可以为 0的,例如刚出生的妖怪的年龄为 0。) 
接下来 n-1 行,每行三个用空格分开的数 a、b、c,表示树上的顶点 a 和 b 之
间有一条权为c(1 <= c <= 1000)的边,a和b 是顶点编号。 
接下来Q行,每行三个用空格分开的数 u、 a、 b。对于这 Q行的每一行,用 a、
b、A计算出 L和R,表示询问“在地方 u开店,面向妖怪的年龄区间为[L,R]的方
案的方便值是多少”。对于其中第 1 行,L 和 R 的计算方法为:L=min(a%A,b%A), 
R=max(a%A,b%A)。对于第 2到第 Q行,假设前一行得到的方便值为 ans,那么当
前行的 L 和 R 计算方法为: L=min((a+ans)%A,(b+ans)%A), 
R=max((a+ans)%A,(b+ans)%A)。 
 

Output

对于每个方案,输出一行表示方便值。 

 

Sample Input

10 10 10
0 0 7 2 1 4 7 7 7 9
1 2 270
2 3 217
1 4 326
2 5 361
4 6 116
3 7 38
1 8 800
6 9 210
7 10 278
8 9 8
2 8 0
9 3 1
8 0 8
4 2 7
9 7 3
4 7 0
2 2 7
3 2 1
2 3 4

Sample Output

1603
957
7161
9466
3232
5223
1879
1669
1282
0

HINT

 满足 n<=150000,Q<=200000。对于所有数据,满足 A<=10^9

 写完莫名其妙地过了样例,交上去莫名其妙地WA了一发后改了改范围然后莫名其妙地A了。

考虑用动态树分治来做。对于以x为分治根的所有节点,记录它子树中所有点到它本身的颜色和距离,以及它子树中所有点到它分治父亲的颜色和距离。sort一下预处理前缀和,就可以在上面二分了,为了省事加了个虚拟节点颜色为-1。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define rep(s,t) for(int i=s;i<=t;i++)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
inline int read() {
    char ch=getchar();int x=0,f=1;
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
const int maxn=300010;
typedef long long LL;
int n,q,first[maxn],next[maxn],to[maxn],dis[maxn],e;
void AddEdge(int w,int v,int u) {
     to[++e]=v;dis[e]=w;next[e]=first[u];first[u]=e;
     to[++e]=u;dis[e]=w;next[e]=first[v];first[v]=e;     
}
int mn[maxn][20],dep[maxn],pos[maxn],cnt;
void dfs(int x,int fa) {
     mn[++cnt][0]=dep[x];pos[x]=cnt;
     ren if(to[i]!=fa) {
         dep[to[i]]=dep[x]+dis[i];
         dfs(to[i],x);    
         mn[++cnt][0]=dep[x];
     }
}
int Log[maxn];
void init() {
     Log[0]=-1;rep(1,cnt) Log[i]=Log[i>>1]+1;
     for(int j=1;(1<<j)<=cnt;j++)
        for(int i=1;i+(1<<j)-1<=cnt;i++)
           mn[i][j]=min(mn[i][j-1],mn[i+(1<<j-1)][j-1]);     
}
LL dist(int x,int y) {
    LL ans=dep[x]+dep[y];x=pos[x];y=pos[y];if(x>y) swap(x,y);
    int k=Log[y-x+1];
    return ans-2LL*min(mn[x][k],mn[y-(1<<k)+1][k]);    
}
int age[maxn],vis[maxn],f[maxn],s[maxn],size,root;
void getroot(int x,int fa) {
     s[x]=1;int maxs=0;
     ren if(!vis[to[i]]&&to[i]!=fa) getroot(to[i],x),s[x]+=s[to[i]],maxs=max(maxs,s[to[i]]);
     f[x]=max(maxs,size-s[x]);
     if(f[root]>f[x]) root=x;     
}
struct Arr {
     int c;LL val;
     bool operator < (const Arr& ths) const {return c<ths.c;}
     Arr operator - (const Arr& ths) const {Arr t;t.val=val-ths.val;t.c=c-ths.c;return t;}
}A[maxn*30];
int cur,L1[maxn],R1[maxn],L2[maxn],R2[maxn];
void dfs(int x,int fa,int dep) {
     A[++cur]=(Arr){age[x],dep};s[x]=1;
     ren if(to[i]!=fa&&!vis[to[i]]) dfs(to[i],x,dep+dis[i]),s[x]+=s[to[i]];
}
int fa[maxn];
void solve(int x,int F) {
     vis[x]=1;fa[x]=F;
     A[++cur]=(Arr){-1,0};L1[x]=cur;dfs(x,0,0);R1[x]=cur;
     sort(A+L1[x],A+R1[x]+1);
     rep(L1[x]+1,R1[x]) A[i].val+=A[i-1].val;
     ren if(!vis[to[i]]) {
         size=f[0]=s[to[i]];getroot(to[i],root=0);
         A[++cur]=(Arr){-1,0};L2[root]=cur;dfs(to[i],0,dis[i]);R2[root]=cur;
         sort(A+L2[root],A+R2[root]+1);
         rep(L2[root]+1,R2[root]) A[i].val+=A[i-1].val;
         solve(root,x);
     }
}
LL lastans,T;
Arr get(int x,int ql,int qr,int L,int R) {
    Arr t;
    t.c=qr;
    int pos1=upper_bound(A+L,A+R+1,t)-A-1;
    t.c=ql-1;
    int pos2=upper_bound(A+L,A+R+1,t)-A-1;
    t.c=pos1-pos2;t.val=A[pos1].val-A[pos2].val;
    return t;
}
LL query(int x,int l,int r) {
    LL ret=get(x,l,r,L1[x],R1[x]).val;
    for(int i=x;fa[i];i=fa[i]) {
        Arr t=get(fa[i],l,r,L1[fa[i]],R1[fa[i]])-get(i,l,r,L2[i],R2[i]); 
        ret+=t.val+t.c*dist(fa[i],x);     
    }
    return ret;
}
int main() {
    n=read();q=read();T=read();
    rep(1,n) age[i]=read();
    rep(2,n) AddEdge(read(),read(),read());
    dfs(1,0);init();
    size=f[0]=n;getroot(1,root=0);
    int t=root;solve(root,0);root=t;
    while(q--) {
        int u=read(),a=(read()+lastans)%T,b=(read()+lastans)%T;
        if(a>b) swap(a,b);
        printf("%lld
",lastans=query(u,a,b));
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/wzj-is-a-juruo/p/4635446.html