poj3468A Simple Problem with Integers区间和线段树

同上。。。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include<math.h>
using namespace std;
typedef long long LL;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
LL sum[444444];
LL color[444444];
void up(LL rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void down(LL rt, LL len)
{
    if (color[rt]){
        color[rt << 1] += color[rt];
        color[rt << 1 | 1] += color[rt];
        LL mid = len >> 1;
        sum[rt << 1] += color[rt] * (len - mid);
        sum[rt << 1 | 1] += color[rt] * mid;
        color[rt] = 0;
    }
}
void build(LL l, LL r, LL rt)
{
    color[rt] = 0;
    if (l == r){
        scanf("%I64d", &sum[rt]); return;
    }
    LL mid = (l + r) >> 1;
    build(lson);
    build(rson);
    up(rt);
}
void update(LL L, LL R, LL ad, LL l, LL r, LL rt)
{
    if (L <= l&&r <= R){
        sum[rt] += ad*(r - l + 1); color[rt] += ad; return;
    }
    down(rt, r - l + 1);
    LL mid = (l + r) >> 1;
    if (L <= mid) update(L, R, ad, lson);
    if (R>mid) update(L, R, ad, rson);
    up(rt);

}

LL ask(LL L, LL R, LL l, LL r, LL rt)
{
    if (L <= l&&r <= R) return sum[rt];
    LL mid = (l + r) >> 1;
    LL ans = 0;
    down(rt, r - l + 1);
    if (L <= mid) ans += ask(L, R, lson);
    if (R>mid) ans += ask(L, R, rson);
    return ans;
}
int main()
{
    LL n, m; LL a, b, c;
    char str[100];
    while (~scanf("%I64d%I64d", &n, &m)){
        build(1, n, 1);
        for (LL i = 0; i<m; i++){
            scanf("%s", str);
            scanf("%I64d%I64d", &a, &b);
            if (str[0] == ‘C‘){
                scanf("%I64d", &c);
                update(a, b, c, 1, n, 1);
            }
            else{
                printf("%I64d\n", ask(a, b, 1, n, 1));
            }
        }
    }
    return 0;
}

poj3468A Simple Problem with Integers区间和线段树,布布扣,bubuko.com

时间: 2024-12-29 12:11:05

poj3468A Simple Problem with Integers区间和线段树的相关文章

hdu 4267 A Simple Problem with Integers(树形结构-线段树)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3708    Accepted Submission(s): 1139 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

POJ3468-A Simple Problem with Integers(区间更新 + SegmentTree || SplayTree || BinaryIndexTree)

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

C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 1 #include<cstdio> 2 using namespace std; 3 const int maxn=1e5+6; 4 int n,a[maxn]; 5 struct Node{ 6 int l,r; 7 long long sum,lazy; 8 void update(long long x){//用于更新区间和 和懒标记 9 sum+=1ll*(r-l+1)*x; 10 lazy+=x; 11 } 12

A Simple Problem with Integers POJ - 3468 (线段树)

思路:线段树,区间更新,区间查找 1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<cmath> 5 #include<set> 6 #include<algorithm> 7 #include<cstdio> 8 #include<map> 9 #include<cstring> 10 #include<

A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int N = 100010; int n, m; int w[N]; struct Node { int l, r;

poj3468A Simple Problem with Integers(线段树的区域更新)

http://poj.org/problem?id=3468 真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊. 线段树的区域更新. 线段树功能:update:成段增减 query:区间求和 #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #d

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

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 60745   Accepted: 18522 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj3468A Simple Problem with Integers(线段树+成段更新)

题目链接: huangjing题意: 给n个数,然后有两种操作. [1]Q a b 询问a到b区间的和. [2]C a b c将区间a到b的值都增加c. 思路: 线段树成段更新的入门题目..学会使用lazy即可.还需要注意的是,lazy的时候更改是累加,而不是直接修改..有可能连续几次进行修改操作..注意这一点就好了... 题目: Language: Default A Simple Problem with Integers Time Limit: 5000MS   Memory Limit:

POJ-3468A 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 gi