Interval Query and Interval Modification of Line Segment Tree 1 in P3372 [Template]

Keywords: C++

Topic Description

For example, if you know a sequence, you need to do the following two operations:

1. Add x to each number of an interval

2. Find the sum of every number in an interval

Input and output format

Input format:

The first row contains two integers, N and M, representing the number of digits in the sequence and the total number of operations, respectively.

The second row contains N integers separated by spaces, where the number i represents the initial value of item i of the sequence.

The next M line contains three or four integers per line, representing an operation, as follows:

Operation 1: Format: 1 x y k Meaning: Add K to each number in the interval [x,y]

Operation 2: Format: 2x y Meaning: Sum of each number in the output interval [x,y]

Output format:

The output contains several line integers, which are the results of all operations 2.

Input and Output Samples

Input sample #1:
5 5
1 5 4 2 3
2 2 4
1 2 3 2
2 3 4
1 1 5 1
2 1 4
Output sample #1:
11
8
20

Explain

Space-time constraints: 1000ms,128M

Data size:

For 30% of the data: N<=8, M<=10

For 70% of the data: N<=1000, M<=10000

For 100% data: N <= 100000, M <= 100000

(Data has been strengthened to ensure that it is within int64/long data range)

Sample description:

 

Here's a reminder

If you use 5.92 version of Dev-c++, run-time errors may occur with% lld input.

At this point, if you make sure that your program is 100% right, you can submit it it directly.

If you don't worry about your program, you can change% lld to% I64d (I is capitalized i) for debugging, so you won't make a mistake.

But remember

Write% lld!!!!!!!

Otherwise all WA s instead of RE s

Keep in mind

(ps: cena evaluation system is also% lld)

My code is basically made up of functions.

The writing is easy to understand.

You can refer to it.

Let's teach you another trick:

If you want large quantities of bar int to long long int

Make the # define statement

Then replace the function with lookup

Note that when looking up, it looks for int + spaces

Otherwise, your printf will become very beautiful (manual funny)

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define lglg long long int
 5 using namespace std;
 6 const lglg MAXN=200001;
 7 lglg n,m;
 8 lglg ans=0;
 9 struct node
10 {
11     lglg l,r,w,f;
12 }tree[MAXN*4];
13 inline void updata(lglg k)
14 {
15     tree[k].w=tree[k*2].w+tree[k*2+1].w;
16 }
17 inline void build(lglg k,lglg ll,lglg rr)
18 {
19     tree[k].l=ll;tree[k].r=rr;
20     if(tree[k].l==tree[k].r)
21     {
22         scanf("%lld",&tree[k].w);
23         return ;
24     }
25     lglg m=(ll+rr)/2;
26     build(k*2,ll,m);
27     build(k*2+1,m+1,rr);
28     updata(k);
29 }
30 inline void down(lglg k)
31 {
32     tree[k*2].f+=tree[k].f;
33     tree[k*2+1].f+=tree[k].f;
34     tree[k*2].w+=(tree[k*2].r-tree[k*2].l+1)*tree[k].f;
35     tree[k*2+1].w+=(tree[k*2+1].r-tree[k*2+1].l+1)*tree[k].f;
36     tree[k].f=0;
37 }
38 inline void interval_change(lglg k,lglg ll,lglg rr,lglg v)
39 {
40     if(tree[k].l>=ll&&tree[k].r<=rr)
41     {
42         tree[k].w+=(tree[k].r-tree[k].l+1)*v;
43         tree[k].f+=v;
44         return ;
45     }
46     if(tree[k].f)   down(k);
47     lglg m=(tree[k].l+tree[k].r)/2;
48     if(ll<=m)    interval_change(k*2,ll,rr,v);
49     if(rr>m)     interval_change(k*2+1,ll,rr,v);
50     updata(k);
51 }
52 inline void interval_ask(lglg k,lglg ll,lglg rr)
53 {
54     if(tree[k].l>=ll&&tree[k].r<=rr)
55     {
56         ans+=tree[k].w;
57         return ;
58     }
59     if(tree[k].f)    down(k);
60     lglg m=(tree[k].l+tree[k].r)/2;
61     if(ll<=m)
62         interval_ask(k*2,ll,rr);
63     if(rr>m)
64         interval_ask(k*2+1,ll,rr);
65 }
66 int main()
67 {
68     scanf("%lld",&n);
69     scanf("%lld",&m);
70     build(1,1,n);
71     while(m--)
72     {
73         lglg how;
74         scanf("%lld",&how);
75         if(how==1)//Interval increase 
76         {
77             lglg x,y,v;
78             scanf("%lld%lld%lld",&x,&y,&v);
79             interval_change(1,x,y,v);
80         }
81         else//Interval summation 
82         {
83             lglg x,y;
84             ans=0;
85             scanf("%lld%lld",&x,&y);
86             interval_ask(1,x,y);
87             printf("%lld\n",ans);
88         }
89     }
90     return 0;
91 }

 

Posted by pc-coholic on Tue, 02 Jul 2019 13:21:45 -0700