POJ3468(KB7-C 线段树)

A Simple Problem with Integers

Time Limit: 5000MS  Memory Limit: 131072K

Total Submissions: 108903   Accepted: 33919

Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

The sums may exceed the range of 32-bit integers.

Source

POJ Monthly--2007.11.25, Yang Yi

带lazy的线段树

 1 //2017-05-17
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #define ll long long
 7 #define mid ((st[id].l+st[id].r)>>1)
 8 #define lson (id<<1)
 9 #define rson ((id<<1)|1)
10
11 using namespace std;
12
13 const ll N = 100005;
14 ll arr[N];
15 struct Node{
16     ll l, r, sum, lazy;
17 }st[N<<2];
18
19 void build(int id, int l, int r)
20 {
21     st[id].l = l; st[id].r = r; st[id].lazy = 0;
22     if(l == r){
23         st[id].sum = arr[l];
24         return;
25     }
26     build(lson, l, mid);
27     build(rson, mid+1, r);
28     st[id].sum = st[lson].sum+st[rson].sum;
29 }
30
31 void push_down(int id)
32 {
33     if(st[id].lazy != 0){
34         st[lson].lazy += st[id].lazy;
35         st[rson].lazy += st[id].lazy;
36         st[id].sum += (st[id].r-st[id].l+1)*st[id].lazy;
37         st[id].lazy = 0;
38     }
39     return;
40 }
41
42 ll query(int id, int l, int r)
43 {
44     if(st[id].l == l && st[id].r == r)return st[id].sum+(r-l+1)*st[id].lazy;
45     push_down(id);
46     if(r <= mid)return query(lson, l, r);
47     else if(l > mid)return query(rson, l, r);
48     else return query(lson, l, mid)+query(rson, mid+1, r);
49 }
50
51 void update(int id, int l, int r, int w)
52 {
53     if(st[id].l == l && st[id].r == r){
54         st[id].lazy += w;
55         return;
56     }
57     st[id].sum += (r-l+1)*w;
58     if(r <= mid)update(lson, l, r, w);
59     else if(l > mid)update(rson, l, r, w);
60     else{
61         update(lson, l, mid, w);
62         update(rson, mid+1, r, w);
63     }
64 }
65
66 int main()
67 {
68     ll n, q;
69     while(scanf("%lld%lld", &n, &q)!=EOF){
70         for(int i = 1; i <= n; i++)
71           scanf("%lld", &arr[i]);
72         build(1, 1, n);
73         char op[5];
74         ll a, b, c;
75         while(q--){
76             scanf("%s", op);
77             if(op[0] == ‘Q‘){
78                 scanf("%lld%lld", &a, &b);
79                 printf("%lld\n", query(1, a, b));
80             }else if(op[0] == ‘C‘){
81                 scanf("%lld%lld%lld", &a, &b, &c);
82                 update(1, a, b, c);
83             }
84         }
85     }
86
87     return 0;
88 }
时间: 2024-10-25 12:51:11

POJ3468(KB7-C 线段树)的相关文章

A Simple Problem with Integers(POJ-3468)(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 71540   Accepted: 22049 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ3468 A Simple Problem with Integers 【线段树】+【成段更新】

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 57666   Accepted: 17546 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj3468 线段树 or splay

poj3468  裸线段树.因为在熟悉splay 所以就用splay交了一发...开始用的scanf()!==2 居然TLE了...然后我就当单组测试数据做的 然后就过了  囧TZ #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <cmath> using namespac

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj3468 线段树

http://poj.org/problem?id=3468 题目链接, 很经典的线段树的应用, 这里复习一下, 再写一遍, 代码如下: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100000 + 10; int N, Q; int a[maxn]; struct Segment{ int l, r; long long

poj3468(线段树)

题目连接:http://poj.org/problem?id=3468 线段树功能:update:成段增减 query:区间求和. 分析:需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define LL long long #

ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)

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. In

线段树专题 POJ3468 A Simple Problem with Integers

题意:n个点.m个操作.两种操作类型.C X Y K 表示区间[x,y]上每一个点值加k.Q X Y 求区间[x,y]的和 分析:线段树区间求和,裸模板 注意:结果会超int,要用long long 表示,假设是在hust上交结果要用%I64d.poj的话则用%lld 代码: #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algor

poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

转载请注明出处:http://blog.csdn.net/u012860063 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 th