# BZOJ4012:[HNOI2015]开店

## Input

L=min(a%A,b%A), R=max(a%A,b%A)。

L=min((a+ans)%A,(b+ans)%A), R=max((a+ans)%A,(b+ans)%A)。

## 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

1603
957
7161
9466
3232
5223
1879
1669
1282
0

## HINT

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

  1 #include<bits/stdc++.h>
2 #define int64 long long
3 using namespace std;
4 int vis[150005],c[150005],a[150005],n,q,b[300005][3],siz[150005],mx[150005],size,root,u;
5 int64 m,last,L,R;
6 struct Path
7 {
8     int p,s; int64 dis;
9     Path(int _p=0,int64 _d=0,int _s=0):p(_p),dis(_d),s(_s){}
10 };
11 vector<Path>path[150005];
12 struct data
13 {
14     int v; int64 dis;
15     data(int _v=0,int64 _d=0):v(_v),dis(_d){}
16     inline bool operator<(const data &p)const
17     {
18         return v==p.v?dis<p.dis:v<p.v;
19     }
20 };
21 vector<data>dis[150005][3];
22 void dfs2(int x,int y)
23 {
24     siz[x]=1;
25     for(int i=c[x];i;i=b[i][2])
26     if((y!=b[i][1])and(vis[b[i][1]]==0))
27     {
28         dfs2(b[i][1],x);
29         siz[x]=siz[x]+siz[b[i][1]];
30     }
31 }
32 void dfs(int x,int y)
33 {
34     siz[x]=1; mx[x]=0;
35     for(int i=c[x];i;i=b[i][2])
36     {
37         if((vis[b[i][1]]==0)and(b[i][1]!=y))
38         {
39             dfs(b[i][1],x);
40             siz[x]=siz[x]+siz[b[i][1]]; mx[x]=(mx[x],siz[b[i][1]]);
41         }
42     }
43     mx[x]=max(mx[x],size-siz[x]);
44     if(mx[x]<mx[root])root=x;
45 }
46 void dfs3(int x,int fa,int tar,int d,int now)
47 {
48     path[x].push_back(Path(tar, d, now));
49     dis[tar][now].push_back(data(a[x],d));
50     for(int i=c[x];i;i=b[i][2])
51     if((vis[b[i][1]]==0)and(b[i][1]!=fa))dfs3(b[i][1],x,tar,d+b[i][0],now);
52 }
53 void work(int x)
54 {
55     int now=0;
56     vis[x]=1; dfs2(x,0);
57     path[x].push_back(Path(x,0,3));
58     for(int i=c[x];i;i=b[i][2])
59     if(vis[b[i][1]]==0)dfs3(b[i][1],x,x,b[i][0],now++);
60     for(int i=c[x];i;i=b[i][2])
61     if(vis[b[i][1]]==0)
62     {
63         root=0; size=siz[b[i][1]];
64         dfs(b[i][1],0); work(root);
65     }
66 }
67 inline int64 qq2(vector <data> *v,int64 d,int s)
68 {
69     int64 res=0;
70     for(int i=0;i<3;i++)if(i!=s)
71     {
72         int t=lower_bound(v[i].begin(),v[i].end(),data(L,-1))-v[i].begin();
73         if(t)res-=d*t+v[i][t-1].dis;
74         t=lower_bound(v[i].begin(),v[i].end(),data(R+1,-1))-v[i].begin();
75         if(t)res+=d*t+v[i][t-1].dis;
76     }
77     return res;
78 }
79 inline int64 qq()
80 {
81     int64 ans=0;
82     int nn=path[u].size();
83     for(int i=0;i<path[u].size();i++)
84     {
85         if((L<=a[path[u][i].p])and(a[path[u][i].p]<=R))ans+=path[u][i].dis;
86         ans+=qq2(dis[path[u][i].p],path[u][i].dis,path[u][i].s);
87     }
88     return ans;
89 }
90 int main()
91 {
92     scanf("%d%d%lld",&n,&q,&m); int nn=0;
93     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
94     for(int i=1;i<=n-1;i++)
95     {
96         int j,k,l;
97         scanf("%d%d%d",&j,&k,&l);
98         b[++nn][0]=l; b[nn][1]=k; b[nn][2]=c[j]; c[j]=nn;
99         b[++nn][0]=l; b[nn][1]=j; b[nn][2]=c[k]; c[k]=nn;
100     }
101     mx[0]=n; size=n;
102     dfs(1,0);
103     work(root);
104     for(int i=1;i<=n;++i)
105     for(int j=0;j<3;++j)
106     {
107         sort(dis[i][j].begin(),dis[i][j].end());
108         for(int k=1;k<dis[i][j].size(); ++k)
109         dis[i][j][k].dis+=dis[i][j][k-1].dis;
110     }
111     for(int i=1;i<=q;i++)
112     {
113         scanf("%d%lld%lld",&u,&L,&R);
114         int64 t;
115         L=(L+last)%m; R=(R+last)%m;
116         if(L>R){ t=L; L=R; R=t; }
117         last=qq();
118         printf("%lld\n",last);
119     }
120 }
View Code
posted @ 2017-02-24 19:53  GhoStreach  阅读(119)  评论(0编辑  收藏  举报