BZOJ4012: [HNOI2015] Open Shop

Source: Internet
Author: User

Description

Wind See fragrance has a good friend called Eight clouds purple, they often together to see the moon from the poetry of the song Fu talked about

Philosophy. Recently, they brainwave to open a small shop in Gensokyo to make some money in business. This is certainly a good idea, but they also find themselves facing a problem where the store is open and what kind of crowd they are facing. It is amazing that the map of Gensokyo is a tree-shaped structure, with a total of n places in Gensokyo, numbered 1 to N, connected by the n-1 of the right side of the strip. Every place has a monster, where the monster age of the first place is x_i. Monsters are like quiet guys, so they don't want to be next to many monsters. So the degree of all vertices in this tree is less than or equal to 3. Monsters and people, the point of interest as the age of change will naturally change, such as our 18-year-old girl fragrance and eight cloud purple is more like lovely things. Fragrance through the study found that basically the interest of the monster is only related to age, so the fragrance is going to choose a place U (U is numbered), and then you open a face age between L and R (i.e. age greater than equal to l, less than equal to R) of the Monster Shop. It is also possible that you are far from these monsters, so the delicate fragrance would like to know all ages from L to R between the monster, to the distance between you and how much (Monster to u distance is the Devil's place to the edge of the path on the side of the sum of the right), the fragrance of this is called this open shop program convenience value. Fragrance they have not decided to open the store where, eight clouds purple is prepared a lot of solutions, so the fragrance wants to know, for each solution, the convenience value is how much. Input

The first row of three spaces separated by the number n, Q and A, indicating the size of the tree, the number of programs open shop and Demon

The age limit of the blame. The second row n a number separated by a space x_1, X_2 、...、 x_n,x_i represents the age of the first place monster, satisfies the 0<=x_i<a. (age can be 0, for example, the age of a newborn monster is 0.) Next n-1 line, each line of three spaces separated by a, B, C, indicating that the tree has a node between A and B has a right to C (1 <= C <= 1000) of the Edge, A and B is the vertex number. Next Q line, each line of three separated by a space of the number u, A, B. For each line of this Q line, L and R are calculated with A, B, a, and ask "what is the convenient value of the scheme for the Genie's age range [L,r] in the local U open shop?" For the 1th row, L and R are calculated as: L=min (a%a,b%a), R=max (a%a,b%a). For rows 2nd to Q, assuming that the convenience value of the previous line is ans, then the L and R for the forward calculation are: L=min ((A+ans)%A, (B+ans)%A), R=max ((A+ans)%A, (B+ans)%A). Output

For each scenario, the output line represents a convenient value.

Sample Input10 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 4Sample Output1603
957
7161
9466
3232
5223
1879
1669
1282
0HINT

Meet n<=150000,q<=200000. For all data, meet a<=10^9

Orz thy

Puzzle: http://blog.csdn.net/thy_asdf/article/details/50350793

Code

1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 #defineMAXN 1500057 #defineMaxnode 100000108 using namespacestd;9typedefLong LongInt64;Ten Charch; One BOOLOK; AInlinevoidReadint&x) { -      for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; -      for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); the     if(OK) x=-x; - } - intN,q,mod,u,a,b,c,l,r; - int64 ans; + structmonster{intAge,id;} LIST[MAXN]; -InlineBOOL operator< (ConstMonster &a,ConstMonster &b) {returna.age<b.age;} + inttot,now[maxn],son[maxn<<1],pre[maxn<<1],val[maxn<<1]; AInlinevoidPutintAintBintc) {pre[++tot]=now[a],now[a]=tot,son[tot]=b,val[tot]=C;} at intIDX,HSON[MAXN],TOP[MAXN],NUM[MAXN],FA[MAXN],DEP[MAXN],SIZ[MAXN],SIDE[MAXN],DIS[MAXN]; - Int64 SUM[MAXN],SUMDIS[MAXN]; -InlinevoidDFS1 (intu) { -     intans1=0, ans2=0, ans3=0; siz[u]=1; -      for(intP=now[u],v=son[p];p; p=pre[p],v=Son[p]) -         if(v!=Fa[u]) { infa[v]=u,dep[v]=dep[u]+1, DIS[V]=DIS[U]+VAL[P],DFS1 (v), siz[u]+=Siz[v]; -             if(Ans1<siz[v]) ans1=siz[v],ans2=v,ans3=p; to         } +hson[u]=ans2,side[u]=ans3; - } theInlinevoidDFS2 (intu) { *     intp=side[u],v=Hson[u]; $     if(v) top[v]=top[u],num[v]=++idx,sum[idx]=VAL[P],DFS2 (v);Panax Notoginseng      for(P=now[u],v=son[p];p; p=pre[p],v=Son[p]) -         if(V!=fa[u]&&v!=hson[u]) top[v]=v,num[v]=++idx,sum[idx]=VAL[P],DFS2 (v); the } + intROOT[MAXN]; A structchairman_tree{ the     structnode{ + Int64 Val; -         intson[2],tim; $ }node[maxnode]; $     inttot; -InlinevoidModifyint&p,intKintLintRintAintb) { -p=++tot,node[p]=Node[k]; the         if(l==a&&r==b) {node[p].tim++;return;} -node[p].val+=sum[b]-sum[a-1];Wuyi         intM= (l+r) >>1; the         if(b<=m) Modify (node[p].son[0],node[k].son[0],l,m,a,b); -         Else if(a<=m) Modify (node[p].son[0],node[k].son[0],L,M,A,M), modify (node[p].son[1],node[k].son[1],m+1, r,m+1, b); Wu         ElseModify (node[p].son[1],node[k].son[1],m+1, r,a,b); -     } AboutInline Int64 query (intPintLintRintAintb) { $Int64 res=1ll* (sum[b]-sum[a-1])*Node[p].tim; -         if(l==a&&r==b)returnres+Node[p].val; -         intM= (l+r) >>1; -         if(b<=m)returnRes+query (node[p].son[0],l,m,a,b); A         Else if(a<=m)returnRes+query (node[p].son[0],l,m,a,m) +query (node[p].son[1],m+1, r,m+1, b); +         Else returnRes+query (node[p].son[1],m+1, r,a,b); the     } - }t; $InlinevoidModifyint&rt,intu) { the     intf1=Top[u]; the      while(f1!=1) T.modify (Rt,rt,1, N-1, Num[f1],num[u]), u=fa[f1],f1=Top[u]; the     if(u!=1) T.modify (Rt,rt,1, N-1, num[hson[1]],num[u]); the } -Inline Int64 query (intRtintu) { in     intf1=Top[u]; theInt64 ans=0; the      while(f1!=1) Ans+=t.query (RT,1, N-1, Num[f1],num[u]), u=fa[f1],f1=Top[u]; About     if(u!=1) Ans+=t.query (RT,1, N-1, num[hson[1]],num[u]); the     returnans; the } the intMain () { + Read ( N), read (q), read (mod); -      for(intI=1; i<=n;i++) Read (list[i].age), list[i].id=i; theSort (list+1, list+n+1);Bayi      for(intI=1; i<n;i++Read ( a), read (b), read (c), put (a,b,c), put (b,a,c); theDFS1 (1), top[1]=1, DFS2 (1); the      for(intI=1; i<=n;i++) sum[i]+=sum[i-1]; -      for(intI=1; i<=n;i++) sumdis[i]=sumdis[i-1]+Dis[list[i].id]; -      for(intI=1; i<=n;i++) Modify (root[i]=root[i-1],list[i].id); the      while(q--){ the Read (U), read ( a), read (b); theL=min ((A+ans)%mod, (B+ans)%mod), R=max ((A+ans)%mod, (B+ans)%MoD); theA=upper_bound (list+1, list+n+1, (Monster) {L1,0})-list,b=lower_bound (list+1, list+n+1, (Monster) {r+1,0})-list-1; -ans=1ll* (b-a+1) *dis[u]+ (sumdis[b]-sumdis[a-1]) -2ll* (query (root[b],u)-query (root[a-1],u)); theprintf"%lld\n", ans); the     } the     return 0;94}

BZOJ4012: [HNOI2015] Open Shop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.