POJ 3468 A Simple Problem with Integers (线段树区域更新)

A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 62431   Accepted: 19141
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

线段树的基础区域更新

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<stdlib.h>
  5 #include<algorithm>
  6 #define LL __int64
  7 using namespace std;
  8 const int MAXN=100000+10;
  9 const int INF=0x3f3f3f3f;
 10 int b[MAXN];
 11 struct node
 12 {
 13     int l,r;
 14     LL val,col;
 15     int mid()
 16     {
 17         return (l+r)/2;
 18     }
 19 }a[MAXN*4];
 20
 21 void btree(int step,int l,int r)
 22 {
 23     a[step].l=l;
 24     a[step].r=r;
 25     a[step].col=0;
 26     if(l==r)
 27     {
 28         a[step].val=b[l];
 29         //scanf("%d",&a[step].val); 这样输入会WA
 30         return ;
 31     }
 32     int mid=a[step].mid();
 33     btree(step*2,l,mid);
 34     btree(step*2+1,mid+1,r);
 35     a[step].val=a[step*2].val+a[step*2+1].val;
 36 }
 37
 38 void ptree(int step,int L,int R,LL num)
 39 {
 40     if(L==a[step].l&&a[step].r==R)//找到符合条件的区域就不更新了,记录下要要更新的值
 41     {
 42         a[step].col+=num;
 43         return ;
 44     }
 45
 46     a[step].val+=(R-L+1)*num;//如果不是符合条件的区域,对当前结点更新
 47
 48     int mid=a[step].mid();
 49     if(R<=mid)
 50         ptree(step*2,L,R,num);
 51     else if(L>mid)
 52         ptree(step*2+1,L,R,num);
 53     else
 54     {
 55         ptree(step*2,L,mid,num);
 56         ptree(step*2+1,mid+1,R,num);
 57     }
 58 }
 59
 60 LL qtree(int step,int L,int R)
 61 {
 62     if(L==a[step].l&&a[step].r==R)
 63     {
 64         return a[step].val+a[step].col*(R-L+1);
 65     }
 66
 67     a[step].val+=(a[step].r-a[step].l+1)*a[step].col;//对当前结点更新
 68
 69     int mid=a[step].mid();//对子结点更新
 70     ptree(step*2,a[step].l,mid,a[step].col);
 71     ptree(step*2+1,mid+1,a[step].r,a[step].col);
 72     a[step].col=0;
 73
 74     if(R<=mid)
 75         return qtree(step*2,L,R);
 76     else if(L>mid)
 77         return qtree(step*2+1,L,R);
 78     else
 79         return qtree(step*2,L,mid)+qtree(step*2+1,mid+1,R);
 80 }
 81 int main()
 82 {
 83     //freopen("in.txt","r",stdin);
 84     int n,kase;
 85     char ch;
 86     int star,en,num;
 87     while(scanf("%d %d",&n,&kase)!=EOF)
 88     {
 89         for(int i=1;i<=n;i++)
 90             scanf("%d",&b[i]);
 91         btree(1,1,n);
 92         while(kase--)
 93         {
 94             cin>>ch;
 95             if(ch==‘C‘)
 96             {
 97                 scanf("%d %d %d",&star,&en,&num);//num输入%I64d会RE
 98                 ptree(1,star,en,num);
 99             }
100             else
101             {
102                 scanf("%d %d",&star,&en);
103                 printf("%I64d\n",qtree(1,star,en));
104             }
105         }
106     }
107     return 0;
108 }

时间: 2024-10-11 17:32:17

POJ 3468 A Simple Problem with Integers (线段树区域更新)的相关文章

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

[POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

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

POJ 3468 A Simple Problem with Integers(线段树区间更新区间求和)

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

poj 3468 A Simple Problem with Integers 线段树区间更新

点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 63565   Accepted: 19546 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations.

(简单) POJ 3468 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. 题意

POJ 3468 A Simple Problem with Integers 线段树 区间更新 区间查询

题目链接: http://poj.org/problem?id=3468 题目描述: 一组数列, 可进行一段区间加上某一个数, 和区间查询 解题思路: 线段树, 之前的那道题是求总区间直接输出sum[1] 就可以了, 这次有了区间查询, 同理, 查询的时候Pushdown 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <map

POJ 3468 A Simple Problem with Integers(线段树 区间更新)

http://poj.org/problem?id=3468 题意 :对于一个序列有两种操作 1 查询 l到r 的和 2 对于l 到r上的每个数 加上 up 思路: 用单点更新必然超时 所以需要区间更新 (位运算时 注意 m-m>>1 与 m-(m>>1) 的区别) #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using names

poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

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

POJ 3468 A Simple Problem with Integers //线段树的成段更新

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