C - A Simple Problem with Integers - poj 3468(区间更新)

题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值。

分析:很明显我们不可能对区间的每个点都进行更新,不过我们可以使用一个op的开关,如果op等于0说明这个不需要更新,如果等于1就说明需要进行更新,这样只需要和插入的时候进行一下更新即可

***********************************************************************

注意:在向下更新的时候如果是更新子树有可能一个区间会更新多次,而子树区间更新的时候不能乘本区间的更新值,只能乘根节点的更新值,根节点的跟新值用完后归0;

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define maxn 100005
#define Lson root<<1,L,tree[root].Mid()
#define Rson root<<1|1,tree[root].Mid()+1,R

struct Tree//op等于0的时候子树不需要更新,op等于1需要更新
{
    int L, R, op;//需要向下更新的值为e
    long long sum, e;//因为区间和比较大,所以使用long long 保存
    int Mid(){return (L+R)/2;}
    int Len(){return (R-L+1);}
}tree[maxn*4];
long long val[maxn];

void Down(int root)//向下更新
{
    if(tree[root].op && tree[root].L != tree[root].R)
    {
        tree[root].op = false;
        tree[root<<1].op = tree[root<<1|1].op = true;
        tree[root<<1].e += tree[root].e;
        tree[root<<1|1].e += tree[root].e;

tree[root<<1].sum += tree[root<<1].Len() * tree[root].e;
        tree[root<<1|1].sum += tree[root<<1|1].Len() * tree[root].e;

tree[root].e = 0;
    }
}
void Build(int root, int L, int R)
{
    tree[root].L = L, tree[root].R = R;
    tree[root].op = false;

if(L == R)
    {
        tree[root].sum = val[L];
        return ;
    }

Build(Lson);
    Build(Rson);

tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
}
void Insert(int root, int L, int R, long long e)
{
    Down(root);

tree[root].sum += (R-L+1) * e;

if(tree[root].L == L && tree[root].R == R)
    {
        tree[root].e = e, tree[root].op = true;
        return ;
    }

if(R <= tree[root].Mid())
        Insert(root<<1, L, R, e);
    else if(L > tree[root].Mid())
        Insert(root<<1|1, L, R, e);
    else
    {
        Insert(Lson, e);
        Insert(Rson, e);
    }
}
long long Query(int root, int L, int R)
{
    Down(root);

if(tree[root].L == L && tree[root].R == R)
        return tree[root].sum;
    if(R <= tree[root].Mid())
        return Query(root<<1, L, R);
    else if(L > tree[root].Mid())
        return Query(root<<1|1, L, R);
    else
        return Query(Lson) + Query(Rson);
}

int main()
{
    int i, N, Q;

while(scanf("%d%d", &N, &Q) != EOF)
    {
        for(i=1; i<=N; i++)
            scanf("%I64d", &val[i]);
        Build(1, 1, N);

int a, b; char s[10];
        long long c;

while(Q--)
        {
            scanf("%s%d%d", s, &a, &b);

if(s[0] == ‘C‘)
            {
                scanf("%I64d", &c);
                Insert(1, a, b, c);
            }
            else
            {
                long long ans = Query(1, a, b);
                printf("%I64d\n", ans);
            }
        }
    }

return 0;
}

/*
5 2
1 2 3 4 5
C 1 5 5
Q 1 1
*/

时间: 2024-09-29 08:14:19

C - A Simple Problem with Integers - poj 3468(区间更新)的相关文章

A Simple Problem with Integers POJ - 3468 区间更新,区间查询板子

#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<list> #include<math.h> #include<vector> #include<stack>

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

C - A Simple Problem with Integers POJ 3468

C - A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3468 Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of ope

A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 69589   Accepted: 21437 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 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.

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

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 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(线段树 区间更新)

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 , 线段树+区间更新。

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. 题意