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

正解:主席树

解题报告:做完LCA之后再来做这题就很简单了

 

  1 #include <iostream>
  2 #include <iomanip>
  3 #include <cstdlib>
  4 #include <cstdio>
  5 #include <cmath>
  6 #include <cstring>
  7 #include <algorithm>
  8 #include <string>
  9 #define int long long
 10 #define RG register
 11 #define MIN(a,b) (a<b?a:b)
 12 #define MAX(a,b) (a>b?a:b)
 13 const int N = 500000;
 14 const int M = 12000000;
 15 
 16 using namespace std;
 17 
 18 int gi(){
 19     char ch=getchar();int x=0;
 20     while(ch<'0' || ch>'9') ch=getchar();
 21     while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
 22     return x;
 23 }
 24 
 25 struct date{
 26     int s,l,r,lazy;
 27 }tr[M];
 28 struct dota{
 29     int s,i;
 30     bool operator < (const dota a) const{
 31         return s<a.s;
 32     }
 33 }f[N];
 34 int cnt,nn[N][3],head[N],fa[N],dis[N],son[N],siz[N],ra[N];
 35 int top[N],id[N],w[N],c[N],h[N],rt[N],st[N][2],g[N];
 36 
 37 void dfs1(int xh,int fu){
 38     fa[xh]=fu,dis[xh]=dis[fu]+1;
 39     for (RG int i=head[xh]; i; i=nn[i][0]){
 40         if (nn[i][1]==fu) continue;
 41         w[nn[i][1]]=nn[i][2],dfs1(nn[i][1],xh);
 42         siz[xh]+=siz[nn[i][1]];
 43         if (siz[nn[i][1]]>siz[son[xh]]) son[xh]=nn[i][1];
 44     }
 45     ++siz[xh];
 46     return;
 47 }
 48 
 49 void dfs2(int xh,int tp){
 50     id[xh]=++cnt,top[xh]=tp,c[cnt]=c[cnt-1]+w[xh];
 51     if (son[xh]) dfs2(son[xh],tp);
 52     for (RG int i=head[xh]; i; i=nn[i][0]){
 53         if (nn[i][1]==son[xh] || nn[i][1]==fa[xh]) continue;
 54         dfs2(nn[i][1],nn[i][1]);
 55     }
 56     return;
 57 }
 58 
 59 void build(int &x,int l,int r,int ql,int qr){
 60     tr[++cnt]=tr[x],x=cnt;
 61     if (ql<=l && r<=qr) {++tr[x].lazy;tr[x].s+=(c[r]-c[l-1]);return;}
 62     int mid=(l+r)>>1;
 63     if (ql<=mid) build(tr[x].l,l,mid,ql,qr);
 64     if (qr>mid)  build(tr[x].r,mid+1,r,ql,qr);
 65     tr[x].s=tr[tr[x].l].s+tr[tr[x].r].s+(tr[x].lazy)*(c[r]-c[l-1]);
 66     return;
 67 }
 68 
 69 int query(int x,int y,int l,int r,int ql,int qr,int lla,int rla){
 70     if (ql<=l && r<=qr) return (c[r]-c[l-1])*(rla-lla)+tr[x].s-tr[y].s;
 71     int mid=(l+r)>>1,ans=0;
 72     if (ql<=mid) ans+=query(tr[x].l,tr[y].l,l,mid,ql,qr,lla+tr[y].lazy,rla+tr[x].lazy);
 73     if (qr>mid)  ans+=query(tr[x].r,tr[y].r,mid+1,r,ql,qr,lla+tr[y].lazy,rla+tr[x].lazy);
 74     return ans;
 75 }
 76 
 77 main(){
 78     freopen("shop.in","r",stdin);
 79     freopen("shop.out","w",stdout);
 80     RG int n=gi(),q=gi(),mo=gi();
 81     for (RG int i=1; i<=n; ++i) f[i]=(dota){gi(),i};
 82     for (RG int i=1; i< n; ++i){
 83         RG int a=gi(),b=gi(),c=gi();
 84         nn[++cnt][1]=a,nn[cnt][0]=head[b],head[b]=cnt,nn[cnt][2]=c;
 85         nn[++cnt][1]=b,nn[cnt][0]=head[a],head[a]=cnt,nn[cnt][2]=c;
 86     }
 87     cnt=0,dfs1(1,0),dfs2(1,1);
 88     sort(f+1,f+n+1);cnt=0;
 89     for (RG int i=1; i<=n; ++i){
 90         h[i]=f[i].s,rt[i]=rt[i-1];
 91         int tot=0,x=f[i].i;
 92         while(top[x]!=1) st[++tot][0]=id[top[x]],st[tot][1]=id[x],x=fa[top[x]];
 93         st[++tot][0]=1,st[tot][1]=id[x];
 94         for (RG int j=1; j<=tot; ++j)
 95             build(rt[i],1,n,st[j][0],st[j][1]);
 96     }
 97     for (RG int i=1; i<=n; ++i){
 98         int tot=0,x=f[i].i;g[i]=g[i-1],ra[f[i].i]=i;
 99         while(top[x]!=1) st[++tot][0]=id[top[x]],st[tot][1]=id[x],x=fa[top[x]];
100         st[++tot][0]=1,st[tot][1]=id[x];
101         for (RG int j=1; j<=tot; ++j) g[i]+=c[st[j][1]]-c[st[j][0]-1];
102     }
103     int jl=0;
104     for (RG int i=1; i<=q; ++i){
105         int u=gi(),a=gi(),b=gi(),c=u;
106         int la=(a+jl)%mo,lb=(b+jl)%mo;
107         a=MIN(la,lb),b=MAX(la,lb);
108         int l=lower_bound(h+1,h+n+1,a)-h;
109         int r=upper_bound(h+1,h+n+1,b)-h-1;
110         int tot=0,ans=0;
111         while(top[u]!=1) st[++tot][0]=id[top[u]],st[tot][1]=id[u],u=fa[top[u]];
112         st[++tot][0]=1,st[tot][1]=id[u];
113         for (RG int j=1; j<=tot; ++j)
114             ans+=query(rt[r],rt[l-1],1,n,st[j][0],st[j][1],0,0);
115         printf("%lld\n",jl=(g[r]-g[l-1]+(r-l+1)*(g[ra[c]]-g[ra[c]-1])-ans*2));
116     }
117     return 0;
118 }

 

posted @ 2017-03-04 21:55  Cjk_2001  阅读(156)  评论(0编辑  收藏  举报