A Simple Problem with Integers poj3468(线段树区间更新,区间查询)

原创 2018年04月17日 20:57:50
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 128832 Accepted: 39978
Case Time Limit: 2000MS
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

题意:线段树模版题,区间更新和区间查询。

线段树的原理,就是,将[1,n]分解成若干特定的子区间(数量不超过4*n),然后,将每个区间[L,R]都分解为
少量特定的子区间,通过对这些少量子区间的修改或者统计,来实现快速对[L,R]的修改或者统计。

#include<map>
#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define LL long long
#define inf 0x3f3f3f
#define root  1,n,1
#define lson  l,mid,rt<<1
#define rson  mid+1,r,rt<<1|1
using namespace std;
const int maxn=100115;
LL sum[maxn<<2];
LL lazy[maxn<<2];
int n,m;
void PushUp(int rt)//向上更新求和
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt,int len)//用lazy数组标记,之后向下更新
{
    if(lazy[rt])
    {
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        sum[rt<<1]+=(len-(len>>1))*lazy[rt];
        sum[rt<<1|1]+=(len>>1)*lazy[rt];
        lazy[rt]=0;
    }
}
void Build(int l,int r,int rt)//建树
{
    lazy[rt]=0;
    if(l==r){
      scanf("%I64d",&sum[rt]);
        return ;
    }
    int mid=l+r>>1;
    Build(lson);//建左儿子
    Build(rson);//右儿子
    PushUp(rt);//更新
}
void Update(int ll,int rr,int c,int l,int r,int rt)//区间更新
{
    if(ll<=l&&rr>=r)//找到相应区间直接返回sum
    {
        sum[rt]+=(LL)(r-l+1)*c;
        lazy[rt]+=c;
        return ;
    }
    PushDown(rt,r-l+1);//向下更新sum
    int mid=l+r>>1;
    if(ll<=mid)  Update(ll,rr,c,lson);//在左儿子中找区间
    if(rr>mid)   Update(ll,rr,c,rson);//在右儿子中找
    PushUp(rt);//向上更新sum;
}
LL  Query(int ll,int rr,int l,int r,int rt)//区间查询
{
    if(ll<=l&&rr>=r)
        return sum[rt];
    PushDown(rt,r-l+1);//把延迟的标记更新到对应的位置
    int mid=l+r>>1;
    LL cnt=0;
    if(ll<=mid) cnt+=Query(ll,rr,lson);
    if(rr>mid)  cnt+=Query(ll,rr,rson);
    return cnt;//返回区间的和
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
       Build(root);
       for(int i=0;i<m;i++){
        char s[3];int a,b,c;
        scanf("%s",s);
        if(s[0]=='Q'){scanf("%d%d",&a,&b);cout<<Query(a,b,root)<<endl;}
        else {scanf("%d%d%d",&a,&b,&c);Update(a,b,c,root);}
       }
    }
}





版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lpeaceminusone/article/details/79980598

A Simple Problem with Integers----线段树

C - A Simple Problem with Integers Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:...
  • bless924295
  • bless924295
  • 2016-06-02 18:55:20
  • 561

A Simple Problem with Integers(线段树之区间点的成段更新)

萌萌哒的传送门 线段树的另一个知识点,区间点的成段更新,此处需要学习一下一个新的知识点,延迟标记. 延迟标记是用来优化线段树的更新和查找的时间复杂度的,总的来说,就是对于当前找到的 这一...
  • ZSGG_ACM
  • ZSGG_ACM
  • 2015-02-12 21:43:43
  • 839

线段树--区间更新-区间查询HDU 1698

题意: 给钩子的价值更新,给出区间,查找区间价值 思路:加lazy标记,不必更新到每个 #include #include #include #include #define lson i #...
  • qq_33951440
  • qq_33951440
  • 2016-08-10 18:48:59
  • 314

poj3468 A Simple Problem with Integers 线段树区间更新,区间求和

传送门:poj3468 A Simple Problem with Integers题目大意输入:第一行输入两个数N和Q。N代表有多少个元素,Q代表进行多少次操作 第二行数输入n个数 表示对应的初始...
  • woshinannan741
  • woshinannan741
  • 2016-07-09 10:11:25
  • 207

poj-3468 A Simple Problem with Integers-线段树-整段区间增加求和

题目地址:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS  ...
  • ly59782
  • ly59782
  • 2016-08-11 17:48:23
  • 256

POJ 3468 A Simple Problem with Integers(段更新的区间求和&Lazy思想&线段树)

题目链接:[kuangbin带你飞]专题七 线段树 C - A Simple Problem with Integers题意 给定n个数及m个操作。 操作分两种: 1. C a b c...
  • to_be_better
  • to_be_better
  • 2016-02-23 22:03:32
  • 1111

线段树模版—A Simple Problem with Integers

D - A Simple Problem with Integers Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:...
  • llzhh
  • llzhh
  • 2016-04-29 00:03:30
  • 227

poj 3468 A Simple Problem with Integers(splay tree模板题)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Subm...
  • zuihoudebingwen
  • zuihoudebingwen
  • 2012-10-03 15:13:41
  • 2395

poj3468树状数组之区间更新+区间询问

Language: Default A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 1...
  • xingyeyongheng
  • xingyeyongheng
  • 2014-03-11 19:10:34
  • 2920

POJ3468 - A Simple Problem with Integers (线段树 区间更新)

POJ3468 - 解题报告 (线段树 区间更新)poj3468 - A Simple Problem with IntegersPOJ3468 - 解题报告 线段树 区间更新 思路 面对的问题 解决...
  • dcf_xyz
  • dcf_xyz
  • 2015-08-30 01:27:31
  • 503
收藏助手
不良信息举报
您举报文章:A Simple Problem with Integers poj3468(线段树区间更新,区间查询)
举报原因:
原因补充:

(最多只允许输入30个字)