A Simple Problem with Integers——区间查询、去区间修改模板题

题目链接

题意:

C a b c  【a,b】区间都加c

Q a b   查询【a,b】的区间和

题解:

区间修改+区间查询 模板题

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
struct node
{
    int l,r;
    ll sum,lazy;
    void update(ll x)
    {
        sum+=1ll*(r-l+1)*x;
        lazy+=x;
    }
}tree[maxn<<2];
int n,m,a[maxn];
void push_up(int x)
{
    tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum;
}
void push_down(int x)
{
    ll lazyval=tree[x].lazy;
    if(lazyval)
    {
        tree[x<<1].update(lazyval);
        tree[x<<1|1].update(lazyval);
        tree[x].lazy=0;
    }
}
void build(int x,int l,int r)
{
    tree[x].l=l,tree[x].r=r;
    tree[x].lazy=tree[x].sum=0;
    if(l==r)tree[x].sum=a[l];
    else
    {
        int mid=(l+r)>>1;
        build(x<<1,l,mid);
        build(x<<1|1,mid+1,r);
        push_up(x);
    }

}
void update(int x,int l,int r,ll val)
{
    int L=tree[x].l,R=tree[x].r;
    if(l<=L && R<=r)tree[x].update(val);
    else
    {
        push_down(x);
        int mid=(L+R)>>1;
        if(mid>=l)update(x<<1,l,r,val);
        if(mid<r)update(x<<1|1,l,r,val);
        push_up(x);
    }
}
ll query(int x,int l,int r)
{

    int L=tree[x].l,R=tree[x].r;
    if(l<=L && R<=r)return tree[x].sum;
    else
    {
        ll ans=0;
        push_down(x);
        int mid=(L+R)>>1;
        if(mid>=l)ans+=query(x<<1,l,r);
        if(mid<r)ans+=query(x<<1|1,l,r);
        push_up(x);
        return ans;
    }

}
int main()
{
    int n,m;;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    build(1,1,n);
    while(m--)
    {
        char op;
        cin>>op;
        if(op==‘C‘)
        {
            int a,b;
            ll c;
            scanf("%d%d%lld",&a,&b,&c);
            update(1,a,b,c);
        }
        else
        {
            int a,b;
            scanf("%d%d",&a,&b);
            printf("%lld\n",query(1,a,b));
        }
    }

    return 0;
}

原文地址:https://www.cnblogs.com/j666/p/11617391.html

时间: 2024-11-05 23:34:04

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

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(线段树区间更新区间求和)

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 线段树 区间更新 区间查询

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

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

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