Trade Guilds of Erathiatime limit:2.0 second
Memory limit:64 mbthe continent of Antagarich was colonized slowly. Long ago It northern part is inhabited by the elves of Avlee. Later, the hot southern desert of Bracada is occupied by the White mages. At the same time, Necromancers settled in Deyja, a land to the north of Bracada and to the south-west of Avlee. Although white and dark mages didn ' t really as each of the other, each group had some artifacts so the other group would being H Appy to buy. As a result, the trading relationship between Bracada and Deyja grew stronger, and soon the mages built a very busy trade Route between these lands. Erathia was founded later, and at first it is stretched along this route. At that time Erathia ' s economy is based solely on trading, so new trading guilds appeared all the time. Each of the guilds is present in a few cities which were consecutively situated along the route. Caravans of each guild travelled between all pairs of cities of the that guild equally often. The state ' s Treasury is repleNished by fees collected from all the caravans moving along the trade route. There is a fee for each route segment connecting the neighboring cities, and this fee could change over time. For example, the fee could is decreased in the areas of frequent goblin attacks, or increased in the areas with high traff Ic. Loins, the Royal Treasurer, studies Erathia ' s economy and tries to predict the profit of trade guilds. He wants to know the amount of money paid in fees by each guild. He has a chronologically ordered list of documents that contains all the royal orders changing the fee and all the papers Establishing new guilds. This data should is used to calculate the average fee paid by a caravan of a given trade guild. Inputthe first line contains the number
Nof cities in Erathia and the number
mof documents collected by Loins (2≤
N≤105; 1≤
m≤105). The following
mLines describe the documents of the possible types:
- "Change a b D": the fee-travelling along each route segment between cities a and C10>bchanged by d gold coins (if D was positive, the fee increased; if D is negative , the fee decreased);
- "Establish a b": A new Guild which is present in all cities between a and b WA S established.
All numbers is integers; 1≤
a<
b≤
N; −10 000≤
D≤10 000. Cities is numbered in the order they is located along the Route:from Bracada to Deyja. The fee for travelling along a segment is never larger than ten gold coins, otherwise merchants would protest. Of course, the fee is always non-negative. Before the first royal order changing the fee, it is equal to zero for all route segments. Outputafter each document establishing the new guild, output in a single line the average amount of fee paid by a caravan of this guild. The absolute or relative error should not exceed 10−6. Sample
input |
Output |
4 5change 1 4 2change 1 2-1establish 1 2establish 2 4establish 1 4 |
1.000000002.666666672.83333333 |
Analysis: Set the inquiry interval for [L,r], the K-road number is k+1;
Then the contribution of the K-route is (K-L) * (r-k+1) *cost[k];
After expansion [-k*k+ (L+1+r) *k-l-l*r]*cost[k];
Line segment Tree single point maintenance good cost[k],k*cost[k],k*k*cost[k] can;
Attention burst int;
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineVI vector<int>#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)#definePII pair<int,int>#defineLson L, Mid, rt<<1#defineRson mid+1, R, rt<<1|1Const intmaxn=1e5+Ten;using namespacestd;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p;p=p*p;q>>=1;}returnF;}intn,m,k;Charop[Ten];ll ans[3];ll Gao (intp) { return(LL) p* (p+1)*(2*p+1)/6;}structnode{ll Sum,sum1,sum2,lazy;} T[MAXN<<2];voidPushup (intRT) {T[rt].sum= t[rt<<1].sum + t[rt<<1|1].sum; T[rt].sum1= t[rt<<1].SUM1 + t[rt<<1|1].sum1; T[rt].sum2= t[rt<<1].sum2 + t[rt<<1|1].sum2;}voidPushdown (intLintRintRT) { intMid = (L + R) >>1; ll T=T[rt].lazy; T[rt<<1].sum + = T * (mid-l +1); T[rt<<1|1].sum + = T * (R-mid); T[rt<<1].SUM1 + = T * (mid-l +1) * (mid + L)/2; T[rt<<1|1].SUM1 + = T * (r-mid) * (R + mid +1)/2; T[rt<<1].sum2 + = T * (Gao (mid)-gao (l1)); T[rt<<1|1].sum2 + = T * (Gao (R)-Gao (mid)); T[rt<<1].lazy + =T; T[rt<<1|1].lazy + =T; T[rt].lazy=0;}voidUpdate (intLintR, LL V,intLintRintRT) { if(L==l && r==R) {T[rt].lazy+=v; T[rt].sum+ = v * (r-l +1); T[rt].sum1+ = v * (r-l +1) * (R + L)/2; T[rt].sum2+ = v * (Gao (R)-gao (l1)); return ; } intMid = (L + R) >>1; if(T[rt].lazy) pushdown (L, R, RT); if(R <=mid) Update (L, R, V, Lson); Else if(L >mid) Update (L, R, V, Rson); Else{Update (L, Mid, V, Lson); Update (Mid+1, R, V, Rson); } pushup (RT);}voidQuery (intLintRintLintRintRT) { if(L==l && r==R) {ans[0]+=t[rt].sum; ans[1]+=T[rt].sum1; ans[2]+=t[rt].sum2; return; } intMid = (L + R) >>1; if(T[rt].lazy) pushdown (L, R, RT); if(R <=mid) Query (L, R, Lson); Else if(L >mid) Query (L, R, Rson); ElseQuery (L, Mid, Lson), query (mid +1, R, Rson);}intMain () {inti,j; scanf ("%d%d",&n,&m); while(m--) { intA,b,c; scanf ("%s", op); if(op[0]=='C') {scanf ("%d%d%d",&a,&b,&c); Update (A+1, B, (LL) C,1N1); } Else{scanf ("%d%d",&a,&b); ans[0]=ans[1]=ans[2]=0; Query (A+1B1N1); printf ("%.10f\n",(Double) (-ans[2]+ (a+b+1) *ans[1]-(A + (LL) a*b) *ans[0])/(B-A)/(b-a+1)*2); } } //System ("Pause"); return 0;}
ural1855 Trade Guilds of Erathia