Hdu 4037 Development Value (mathematical formula for line segment tree maintenance), hdu4037

Source: Internet
Author: User
Tags ranges

Hdu 4037 Development Value (mathematical formula for line segment tree maintenance), hdu4037
Development ValueTime 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 + 1)/2, and then sums the value from x to y, sigma (D (I) * (y-I + 1)/2 ).
Split the sum into several convenient maintenance items: 1/2 * (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;}



Change of hdu1166 baseline pascal Line Segment tree

Line Segment tree
, Line segment tree Definition
Informatics competitions often encounter problems related to interval operations. When calculating the extreme values and sum of several segments, special data structures of line segments are used.
Line Segment tree binary tree T (AB ). parameter a and parameter B indicate the length of the interval [a, B] and its B-a are called the Interval Length. The line segment tree T (a, B) is also recursively defined:
If L> 1: [a, (a + B) div 2] T left son;
[(A + B) div 2, B] T right son
If L = l: T leaf node
Process object line segments or ranges. Set the range to [1 .. n] divide some [I, I + 1] Each unit area corresponds to a line segment tree leaf node. Each node uses variables to record the number of line segments that overwrite the node. Use a line segment tree to know that the endpoint of the specified interval can take values, set A [1 .. n] For any undefined closed range in the same endpoint set in a small large permutation area P = [xy]. save l ≤ I ≤ j ≤ n so that x = a [I] and y = a [j], Li I, j is called x, y number note: even if the real coordinate line segment tree has only integer meanings, the ranges [x, y] x and y are all integers. that is, the coordinate number of the vertex in the original interval.
Generally, the line segment Tree node type is defined as follows:
Type linetree = ^ node
Node = record
I, j: integer; {node indicates the vertex ID of the partition question I. j}
Count: integer; {Number of overwriting node zones}
Lchild, rchild: linetree; {two subnodes of a binary tree}
End;
Figure 6-22 typical line segment tree description interval endpoint has 10 values

Ii. Basic operations and Applications of line tree
The line segment tree has some important features. line Segment tree Balance Tree. the depth exceeds log2L. The second line segment tree divides any line segment in the area question into lines that exceed 2log2L. The line segment tree energy O (log2L) the following describes how to perform operations on a line segment tree when inserting, deleting, or searching a line segment within the specified time.
[Example 6-14] Ticket Sales System
[Problem description]
A certain train passes through city c. there are a total of s seats on the 1c train on the city no. the Railway Bureau stipulates that tickets sold can only be seated. that is, all passengers on the bus have a ticket sales system run by a computer. each ticket request contains three parameters. O and DN are used to represent the origin site D. N ticket count ticketing system accepts or accepts the ticket application. the ticket application is accepted only when there are N points N seats on the train in the OD section. Please write a program to implement automatic ticketing.
Input:
Input File: RAILWAY. IN, the first line contains three spaces to separate integers c, s, and R. its l ≤ c ≤ 600001 ≤ s ≤ 600001 ≤ R ≤ 60000c city number of s trains on the seat number R all ticket tickets application total number next R lines each ticket application. use three spaces to separate the integer OD and N to represent the start station of O. d. Visual site. N number of ticket stations, its l ≤ D ≤ cl ≤ O ≤ c. all ticket sales applications are provided from morning and evening based on the application time
Output:
Output file: RAlLWAY. OUT. There are a total of R rows. Each row outputs YES or NO, indicating that the current ticket sales application is accepted or accepted.
[Example input] RAIlWAY. IN
4 6 4
L 4 2
I 3 2
2 4 3
1 2 3
[Example output] RAILWAY. OUT
YES
YES
NO
NO
[Problem Analysis]
In this case, when the number of online statistics and search questions-interval insertion and search questions is relatively small, it is easy to simply simulate the scale of this question by 60,000. simple quasi-complexity O (n2) 60,000x60,000 = 3,600,000,000 The amount of computation cannot bear. Therefore, we must use special data structures and algorithms to reduce the time complexity.
Interval insertion and searching make it easy for us to think of line segments (intervals) of data structures. The time complexity of inserting and searching a line segment tree is O (log2n) only this topic is special. To search for segments, you must design a line segment tree with the function of searching for segments.
Let's design a line segment tree.
First. as a line segment tree. the number of times each record interval on the tree is overwritten is required. in this question, we need to record the two values of the period in each interval. except for the two values. we can also see that we need to record other information. We may as well, the shader line segment tree only records two values.
Maintenance Operation key record value for line segment tree maintenance refers to the insertion and search process. Maintenance complexity within the log2n level ...... the remaining full text>


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.