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>
#include<string>
#include<stdio.h>

using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 10;
LL a[MAXN],st[MAXN << 2];
LL add[MAXN << 2];
void pushup(int o) {
    st[o] = st[o << 1] + st[o << 1 | 1];
}
void build(int o,int l,int r) {
    if (l == r) st[o] = a[l];
    else {
        int m = (l + r) >> 1;
        build(o << 1, l, m);
        build(o << 1 | 1, m + 1, r);
        pushup(o);
    }
}
void pushdown(int o,int l,int r) {
    if(add[o]) {
        add[o << 1] += add[o];
        add[o << 1 | 1] += add[o];
        int m = (l + r) >> 1;
        st[o << 1] += add[o] * (m - l + 1);
        st[o << 1 | 1] += add[o] * (r - m);
        add[o] = 0;
    }
}

LL query(int o,int l,int r,int ql,int qr) {
    if(ql <= l && r <= qr) return st[o];
    pushdown(o,l,r);
    int m = (l + r) >> 1;
    LL ans = 0;
    if(ql <= m) ans += query(o << 1, l, m, ql, qr);
    if(qr >= m + 1) ans += query(o << 1 | 1, m + 1, r, ql, qr);
    return ans;
}

void update(int o,int l,int r,int ql,int qr,LL addv) {
    if(ql <= l && r <= qr) {
        add[o] += addv;
        st[o] += addv * (r - l + 1);
        return;
    }
    pushdown(o,l,r);
    int m = (l + r) >> 1;
    if(ql <= m) update(o << 1,l,m,ql,qr,addv);
    if(qr >= m + 1) update(o << 1 | 1,m + 1,r,ql,qr,addv);
    pushup(o);
}

int main() {
    int n,q;
    scanf("%d %d",&n,&q);
    for (int i = 1; i <= n ; i++)
        scanf("%lld",&a[i]);
    build(1,1,n);
    char s[10];
    int x,y;
    LL add;
    while(q--) {
        scanf("%s",s);
        if(s[0] == ‘Q‘) {
            scanf("%d %d",&x,&y);
            LL ans = query(1,1,n,x,y);
            printf("%lld\n",ans);
        }
        else {
            scanf("%d %d %lld",&x,&y,&add);
            update(1,1,n,x,y,add);
        }
    }
}

原文地址:https://www.cnblogs.com/smallhester/p/11267225.html

时间: 2024-10-07 08:20:55

A Simple Problem with Integers POJ - 3468 区间更新,区间查询板子的相关文章

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

题目地址: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

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

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

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