HDU 4037 development value (mathematical formula of Line Segment tree maintenance)

Source: Internet
Author: User
Development Value Time Limit: 5000/3000 MS (Java/others) memory limit: 65768/65768 K (Java/Others)
Total submission (s): 609 accepted submission (s): 118


Problem descriptionstarcraft 2 (SC2) is a famous game. More and more people fall in love with this game.

As a crazy fan of SC2, ahua (flower fairy) Play it day and night. recently, he found that the most important part of being a top player of SC2 is economic development, which means you shocould get as much mine as possible by training SCVs (space construction vehicle) to collect mine. train a SCV at ith second costs CI units of mine. after training, this SCV can collect di units of mine each second. training a SCV needs one second of time.

Based on that, he composes a formula to evaluate the development in a time span from Xth second to yth second. assume at Xth second, ahua has no SCV and mine. he trains one SCV at each second during Xth second and yth second (the mount of mine can be negative, so that he always can train SCV ). each SCV will collect some amount of mines for ahua in each second after it was trained. at ith second ahua has Mi units of mine in total. the development value is defined as sum (MI) (x ≤ I ≤ y ). now he asks you to help him calculate the development value. to make it more interesting, ahua can apply following operations:

Cost x y z: the cost of training a SCV between Xth second to yth second will increase by Z units of mine. i. e. ci for x ≤ I ≤ y will increase by Z.

Collect x y z: Each SCV trained between Xth second and yth second can collect Z more mines every second after it has been trained. i. e. DI for x ≤ I ≤ y will increase by Z.

Query x y: output the development value between Xth second and yth second.
 
Inputfirst line of the input is a single integer T (T ≤ 10), indicates there are t test cases.
For each test case, the first line is an integer N (1 ≤ n ≤ 100000), means the maximum time you shoshould deal.

Following n lines, each contain two integers Ci and di (0 ≤ CI, di ≤ 100000), the cost and collect speed of SCV training in ith second initially as described above.

The next line is an integer Q (1 ≤ q ≤ 10000), the number of operations you shoshould deal. then Q lines followed, each line will be "cost x y z", "collect x y z" or "query x y ".
1 ≤ x ≤ y ≤ n, 0 ≤ z ≤100000
 
Outputfor each test case, first output "case K:" in a single line, k is the number of the test case, from 1 to T. then for each "q x y", you should output a single line contains the answer MOD 20110911.
Sample Input
151 32 33 12 23 35Query 1 3Cost 2 2 1Query 1 3Collect 1 1 5Query 1 3
 
Sample output
Case 1:2015
 
Sourcethe 36th ACM/ICPC Asia Regional Chengdu site -- online contest
Recommendlcy | we have carefully selected several similar problems for you: 4038 4036 4039 4032

Question:

There is a game. Mining Engineers are required. It takes C [I] to create a miner in the second I. Then, the miner will mine d [I] every second. Then ask. From X seconds to y seconds. Create a miner per second. (In x seconds, both the miner and miner are 0, but the miner can be negative ). Then a mi is defined. Indicates the total number of mines in second I. Then you need to output. Σ mi (x <= I <= Y ).


Ideas:

First, push the formula.

1. Consider the cost
Total cost of creating farmers in time J from Time X to time J
X C (X)
X + 1 C (x) + C (x + 1)
X + 2 C (x) + C (x + 1) + C (x + 2)
......
Y c (x) + C (x + 1) +... + C (y)
Sum the second column. Each column is C (I) * (Y-I + 1). Then sum the column from X to Y to obtain sigma (C (I) * (Y + 1-I ))
Divided into two items (Y + 1) * sigma (C (I)-sigma (C (I) * I)

2. Consider Mining
For the farmers created at I, the total number of mines collected at J is d (I) * (J-I ), the sum from X to J is the total number of mines collected by the farmers created before J moment until J moment, that is, SIGMA (d (I) * (J-I )) (sum of I from X to J), and then sum of J from X to Y is the answer. However, this form of summation is not suitable for line segment tree maintenance and does some deformation:
Moment J sigma (d (I) * (J-I ))
X d (x) * 0
X + 1 d (x) * 1 + d (x + 1) * 0
X + 2 d (x) * 2 + d (x + 1) * 1 + d (x + 2) * 0
......
Y d (x) * (Y-x) + d (x + 1) * (y-x-1) + ...... + d (Y-1) * 1 + d (y) * 0
Sums the second column. Each column is d (I) * (Y-I) * (Y-I + 1)/2, and then sums the value from X to Y, SIGMA (d (I) * (Y-I) * (Y-I + 1)/2 ).
Split the sum into several convenient maintenance items: 1/2 * (y * (Y + 1) sigma (d (I)-(2 * Y + 1) sigma (d (I) * I) + sigma (d (I) * I ^ 2 ))

Then the final answer is mining-cost.

Then you only need to use a line segment tree for maintenance.

SIGMA (CI), SIGMA (I * CI), SIGMA (DI), SIGMA (I * di), SIGMA (I * di ).

Then the query is completed based on the general update.

For the Division of two modulo.

(1) modulus multiplied by 2. All intermediate processes are directly modulo, and the final number is 2.
(2) perform the modulo operation directly. The final answer is ret. If RET is an even number, the answer is RET/2. If RET is an odd number, the answer is (Ret + mod)/2.

For details, see the code:

#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=100010;typedef long long ll;const ll mod=20110911*2;#define lson L,mid,ls#define rson mid+1,R,rsll sm[maxn],ss[maxn],sc[maxn<<2],sd[maxn<<2],siid[maxn<<2];ll sic[maxn<<2],sid[maxn<<2],ac[maxn<<2],ad[maxn<<2];ll asc,aic,asd,aid,aiid;void addc(int L,int R,int rt,ll d){    ac[rt]=(ac[rt]+d)%mod;    sc[rt]=(sc[rt]+(R-L+1)*d)%mod;    sic[rt]=(sic[rt]+(sm[R]-sm[L-1])*d)%mod;}void addd(int L,int R,int rt,ll d){    ad[rt]=(ad[rt]+d)%mod;    sd[rt]=(sd[rt]+(R-L+1)*d)%mod;    sid[rt]=(sid[rt]+(sm[R]-sm[L-1])*d)%mod;    siid[rt]=(siid[rt]+(ss[R]-ss[L-1])*d)%mod;}void PushDown(int L,int R,int rt){    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    if(ad[rt])        addd(lson,ad[rt]),addd(rson,ad[rt]),ad[rt]=0;    if(ac[rt])        addc(lson,ac[rt]),addc(rson,ac[rt]),ac[rt]=0;}void PushUp(int rt){    int ls=rt<<1,rs=ls|1;    sc[rt]=(sc[ls]+sc[rs])%mod;    sic[rt]=(sic[ls]+sic[rs])%mod;    sd[rt]=(sd[ls]+sd[rs])%mod;    sid[rt]=(sid[ls]+sid[rs])%mod;    siid[rt]=(siid[ls]+siid[rs])%mod;}void build(int L,int R,int rt){    ac[rt]=ad[rt]=0;    if(L==R)    {        scanf("%I64d%I64d",&sc[rt],&sd[rt]);        sic[rt]=(L*sc[rt])%mod;        sid[rt]=(L*sd[rt])%mod;        siid[rt]=(sid[rt]*L)%mod;        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    build(lson);    build(rson);    PushUp(rt);}void update(int L,int R,int rt,int l,int r,ll d,int op){    if(l<=L&&R<=r)    {        if(op)            addd(L,R,rt,d);        else            addc(L,R,rt,d);        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    PushDown(L,R,rt);    if(l<=mid)        update(lson,l,r,d,op);    if(r>mid)        update(rson,l,r,d,op);    PushUp(rt);    //printf("%d->%d sc")}void qu(int L,int R,int rt,int l,int r){    if(l<=L&&R<=r)    {        asc=(asc+sc[rt])%mod;        aic=(aic+sic[rt])%mod;        asd=(asd+sd[rt])%mod;        aid=(aid+sid[rt])%mod;        aiid=(aiid+siid[rt])%mod;        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    PushDown(L,R,rt);    if(l<=mid)        qu(lson,l,r);    if(r>mid)        qu(rson,l,r);    PushUp(rt);}int main(){    int i,t,n,q,x,y,z,cas=1;    char cmd[20];    for(i=1;i<maxn;i++)    {        sm[i]=(sm[i-1]+i)%mod;        ss[i]=(ss[i-1]+(ll)i*i)%mod;    }    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        build(1,n,1);        scanf("%d",&q);        printf("Case %d:\n",cas++);        while(q--)        {            asc=aic=asd=aid=aiid=0;            scanf("%s%d%d",cmd,&x,&y);            if(cmd[0]!='Q')                scanf("%d",&z);            if(cmd[2]=='s')                update(1,n,1,x,y,z,0);            else if(cmd[2]=='l')                update(1,n,1,x,y,z,1);            else            {                qu(1,n,1,x,y);                ll ans=((ll)y*(y+1)*asd-(2*y+1)*aid+aiid)%mod;                ans-=2*((y+1)*asc-aic);                ans%=mod;                ans=(ans+mod)%mod;                //printf("asc %I64d aic %I64d asd %I64d aid %I64d aiid %I64d\n",asc,aic,asd,aid,aiid);                printf("%I64d\n",ans/2);            }        }    }    return 0;}


HDU 4037 development value (mathematical formula of Line Segment tree maintenance)

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.