poj 3468 线段树成段更新

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

A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 58132   Accepted: 17704
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.

-----------------------------------------------------------------------------------------------------------------------

Life is so hard! many courage and not care the result!

成段更新不容易啊,看了两三天了,仍像云里雾里,不懂啊!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

#define Maxx 100010

int n,m;
int str[Maxx];
long long ans;

struct Node
{
    int left,right;
    long long add,sum;
}tree[Maxx*4];

void build(int l,int r,int rt)
{
    tree[rt].left=l;
    tree[rt].right=r;
    tree[rt].add=0;

    if(l == r)
    {
        tree[rt].sum=str[l];
        return ;
       }
    int mid=(l+r)/2;
    build(l,mid,2*rt);
    build(mid+1,r,2*rt+1);
    tree[rt].sum=tree[rt*2].sum+tree[rt*2+1].sum;
}

void update(int l,int r,int add,int rt)
{
    if(tree[rt].left>=l && tree[rt].right<=r)
    {
        tree[rt].sum+=(tree[rt].right-tree[rt].left+1)*add;
        tree[rt].add+=add;
        return ;
       }

    if(tree[rt].left>r || tree[rt].right<l)
    {
        return ;
       }

    if(tree[rt].add)
    {
        tree[2*rt].sum += (tree[2*rt].right-tree[2*rt].left+1)*tree[rt].add;
        tree[2*rt].add += tree[rt].add;
        tree[2*rt+1].sum += (tree[2*rt+1].right-tree[2*rt+1].left+1)*tree[rt].add;
        tree[2*rt+1].add += tree[rt].add;
        tree[rt].add = 0;
       }

    update(l,r,add,rt*2);
    update(l,r,add,rt*2+1);
    tree[rt].sum =tree[rt*2].sum+tree[rt*2+1].sum;
}

void query(int l,int r,int rt)
{
    if(tree[rt].left>r || tree[rt].right<l)
    {
            return ;
       }

    if(tree[rt].left>=l && tree[rt].right<=r)
    {
        ans+=tree[rt].sum;
        return ;
       }

    if(tree[rt].add)
    {
        tree[2*rt].sum += (tree[2*rt].right-tree[2*rt].left+1)*tree[rt].add;
        tree[2*rt].add += tree[rt].add;
        tree[2*rt+1].sum += (tree[2*rt+1].right-tree[2*rt+1].left+1)*tree[rt].add;
        tree[2*rt+1].add += tree[rt].add;
        tree[rt].add = 0;
       }

    query(l,r,rt*2);
    query(l,r,rt*2+1);
    tree[rt].sum=tree[rt*2].sum+tree[rt*2+1].sum;
}

int main()
{
    int i,j,k,t,a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&str[i]);
           }
        build(1,n,1);
        char st[2];
        for(i=1;i<=m;i++)
        {
                   scanf("%s",st);
                 if(st[0] == ‘Q‘)
                {
                    scanf("%d%d",&a,&b);
                    ans=0;
                    query(a,b,1);
                    printf("%lld\n",ans);
                   }
                else
                {
                    scanf("%d%d%d",&a,&b,&c);
                    update(a,b,c,1);
                   }
           }
       }
    return 0;
}

poj 3468 线段树成段更新

时间: 2024-10-12 09:06:13

poj 3468 线段树成段更新的相关文章

poj 3468 线段树 成段增减 区间求和

题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample Output 455915 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6

poj 3669 线段树成段更新+区间合并

添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define maxn 50005 int rsum[maxn<<2],lsum[maxn<<2],msum[maxn<<2];//msum[]维护区间1…N中的最大连续区间长度 int

poj 3468 A Simple Problem with Integers (线段树成段更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 77486   Accepted: 23862 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 【线段树-成段更新】

题目:poj 3468 A Simple Problem with Integers 题意:给出n个数,两种操作 1:l -- r 上的所有值加一个值val 2:求l---r 区间上的和 分析:线段树成段更新,成段求和 树中的每个点设两个变量sum 和 num ,分别保存区间 l--r 的和 和l---r 每个值要加的值 对于更新操作:对于要更新到的区间上面的区间,直接进行操作 加上 (r - l +1)* val .下面的区间标记num += val 对于求和操作,每次进行延迟更新,把num值

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K 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

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

题目传送门 1 /* 2 线段树-成段更新:裸题,成段增减,区间求和 3 注意:开long long:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 using namespace std; 11 12 #define lson l, mid, rt <<

poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)

题目链接 题意: 只有这两种操作 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. 分析:自己写的有点麻烦了,写的时候手残+脑残,改了好久. val+lazy*(r-l+1)表示和,如果lazy==0表示当前区间加的值不统一. 1 #include <iostream

POJ 2777 Count Color (线段树成段更新+二进制思维)

题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的颜色有几种. 很明显的线段树成段更新,但是查询却不好弄.经过提醒,发现颜色的种类最多不超过30种,所以我们用二进制的思维解决这个问题,颜色1可以用二进制的1表示,同理,颜色2用二进制的10表示,3用100,....假设有一个区间有颜色2和颜色3,那么区间的值为二进制的110(十进制为6).那我们就把

POJ 2528 Mayor&#39;s posters (hash+线段树成段更新)

题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的,但是必定是单位宽度的整数倍,且<=1QW.后贴的海报若与先贴的海报有交集,后贴的海报必定会全部或局部覆盖先贴的海报.现在给出每张海报所贴的位置(左端位置和右端位置),问张贴完N张海报后,还能看见多少张海报?(PS:看见一部分也算看到.) 思路:简单的成段更新,但是数据量是1千万,会MT,所以要区间压缩(离散化),保证覆盖的关系不变,离散化的时候有个易错的细节,poj数据水了,这个易错点引用h