poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

A Simple Problem with Integers

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://poj.org/problem?id=3468

Description

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.
 

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4


Sample Output

4
55
9
15

HINT

题意

 区间加,区间查询和

题解:

 入门模板题,学了这么久的线段树,竟然打了20分钟,而且还debug了,果然还是太菜了。

忘记建树导致re,忘记return ans。虽然是细节错误,但是说明自己还是不够熟练。什么时候才可以一遍打完,不用编译直接交,不需看结果,帅气切题啊。

代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 #define N 200050
 6 #define ll long long
 7 int n,m; ll w[N];
 8 struct Tree{int l,r;ll j,sum;}tr[N<<2];
 9 template<typename T>void read(T&x)
10 {
11   int k=0;char c=getchar();
12   x=0;
13   while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
14   if (c==EOF)exit(0);
15   while(isdigit(c))x=x*10+c-'0',c=getchar();
16   x=k?-x:x;
17 }
18 void read_char(char &c)
19 {while(!isalpha(c=getchar())&&c!=EOF);}
20 void push_up(int x)
21 {
22   ll len=tr[x].r-tr[x].l+1;
23   tr[x].sum=len*tr[x].j;
24   if (len==1)return;
25   tr[x].sum+=tr[x<<1].sum+tr[x<<1|1].sum;
26 }
27 void push_down(int x)
28 {
29   Tree &a=tr[x<<1],&b=tr[x<<1|1];
30   a.j+=tr[x].j;
31   b.j+=tr[x].j;
32   push_up(x<<1);
33   push_up(x<<1|1);
34   tr[x].j=0;
35 }
36 void bt(int x,int l,int r)
37 {
38   tr[x].l=l; tr[x].r=r; tr[x].j=0;
39   if (l==r)
40     {
41       tr[x].j=w[l];
42       push_up(x);
43       return;
44     }
45   int mid=(l+r)>>1;
46   bt(x<<1,l,mid);
47   bt(x<<1|1,mid+1,r);
48   push_up(x);
49 }
50 void update(int x,int l,int r,ll tt)
51 {
52   if (l<=tr[x].l&&tr[x].r<=r)
53     {
54       tr[x].j+=tt;
55       push_up(x);
56       return;
57     }
58   int mid=(tr[x].l+tr[x].r)>>1;
59   if(l<=mid)update(x<<1,l,r,tt);
60   if (mid<r)update(x<<1|1,l,r,tt);
61   push_up(x);
62 }
63 ll query(int x,int l,int r)
64 {
65   if(l<=tr[x].l&&tr[x].r<=r)
66     return tr[x].sum;
67   ll ans=0,mid=(tr[x].l+tr[x].r)>>1;
68   push_down(x);
69   if (l<=mid)ans+=query(x<<1,l,r);
70   if (mid<r)ans+=query(x<<1|1,l,r);
71   push_up(x);
72   return ans;
73 }
74 int main()
75 {
76   #ifndef ONLINE_JUDGE
77   freopen("aa.in","r",stdin);
78   #endif
79   read(n); read(m);
80   for(int i=1;i<=n;i++)read(w[i]);
81   bt(1,1,n);
82   for(int i=1;i<=m;i++)
83     {
84       char id;
85       int x,y; ll tt;
86       read_char(id);read(x);read(y);
87       if (id=='C')read(tt),update(1,x,y,tt);
88       if (id=='Q')printf("%lld
",query(1,x,y));
89     }
90 }
View Code
原文地址:https://www.cnblogs.com/mmmqqdd/p/10758878.html