PKU-3468 simple problem with integer 线段树 LL坑点(欢迎讨论)

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.InputThe first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"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.OutputYou 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.

——————————————————————正义的分割线————————————————————————————

嘛,仍然是到线段树题目,但是这道题目一个不同的点是需要对段进行增删操作,这个和之前count color的替换不大一样,具体哪里不一样么,就是这样一来查询的时候需要进行pushdown操作,因为在程序路过一个节点的时候,它可能只是要这个节点的部分信息而已,这时候就变成了底层信息需要从底层节点获取,但是增删信息存在上层节点上,而且多层的结构也使携带信息成为了一个增加代码复杂度降低可读性的东西,所以既然增加一个pushdown又能过的前提下我们当然没有必要设计一个一路携带信息下来的访问程序。

不过这里有一个值得疑问的东西,我之前由于一开始的设计习惯增加了lazy标签来标注是否有增量,然后增量存在lazyn这个数据里面,判断是否需要pushdown用的是lazy标签,但是这在后来证明是错的,判断用lazyn标签就对了。

我们来列举一下lazy和lazyn的关系:

若lazy为0,那么lazyn必为0,两者的判断无影响

若lazy为1,那么lazyn会是一个int,但是我在里面特判了一下,如果lazyn为0,那么lazy也为0,所以lazy和lazyn应该是等价的……好吧我不得不承认这其中应该存在一个非常严重的逻辑漏洞,但是确实是没有找到一组错误样例来证明的。

p.s:如果评论里有人能给出错误样例我会很感激QAQ

————————————————————————————————————————————————————————

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<climits>
#include<map>
#include<stack>
#define file_in freopen("input.txt","r",stdin)
#define MAX 100000
#define maxn 5005
using namespace std;
#define LL long long
#define FF(x,y) for(int i=x;i<y;i++)
struct node
{
    LL lef;
    LL rig;
    LL mid;
    LL data;
    LL lazyn;
    LL lazy;
}tree[3 * MAX];
vector<LL>sto;
LL createtree(LL left, LL right, LL id)
{
    tree[id].lef = left;
    tree[id].rig = right;
    tree[id].lazy = 0;
    tree[id].lazyn = 0;
    if (left == right)
    {
        tree[id].data = sto[left];
        return tree[id].data;
    }
    LL mid = (left + right) >> 1;
    tree[id].mid = mid;
    tree[id].data = createtree(left, mid, id * 2) + createtree(mid + 1, right, id * 2 + 1);
    return tree[id].data;
}
void pushdown(LL id)
{
    if (tree[id].lazyn)
    {
        tree[2 * id].data += (tree[2 * id].rig - tree[2 * id].lef + 1)*tree[id].lazyn;
        tree[2 * id + 1].data += (tree[2 * id + 1].rig - tree[2 * id + 1].lef + 1)*tree[id].lazyn;
        tree[2 * id].lazyn += tree[id].lazyn;
        tree[2 * id + 1].lazyn += tree[id].lazyn;
        tree[id].lazy = 0;
        tree[id].lazyn = 0;
    }
}
void update(LL left, LL right, LL value, LL id)
{
    if (tree[id].lef >= left&&tree[id].rig <= right)
    {
        tree[id].data += (tree[id].rig - tree[id].lef + 1)*value;
        tree[id].lazy = 1;
        tree[id].lazyn += value;
        if (tree[id].lazyn == 0)tree[id].lazy = 0;
        //cout << tree[id].lef << " " << tree[id].rig << " " << tree[id].data << "|";
        return;
    }
    pushdown(id);
    if (left > tree[id].mid)
    {
        update(left, right, value, 2 * id + 1);
    }
    else if (tree[id].mid >= right)
    {
        update(left, right, value, 2 * id);
    }
    else
    {
        update(left, right, value, 2 * id);
        update(left, right, value, 2 * id + 1);
    }
    tree[id].data = tree[id * 2].data + tree[id * 2 + 1].data;

}
LL query(LL lef, LL rig, LL id)
{
    if (tree[id].lef >= lef&&tree[id].rig <= rig)
    {
        //cout << tree[id].lef << " " << tree[id].rig << " " << tree[id].data << "|";
        return tree[id].data;
    }
    pushdown(id);
    if (lef > tree[id].mid)
    {
        return query(lef, rig, 2 * id + 1);
    }
    else if (tree[id].mid >= rig)
    {
        return query(lef, rig, 2 * id);
    }
    else
    {
        return query(lef, rig, 2 * id + 1) + query(lef, rig, 2 * id);
    }
}

int main()
{
    LL num, op;
    while (scanf("%lld %lld", &num, &op) != EOF)
    {
        sto.resize(num + 1);
        FF(1, num + 1)
        {
            scanf("%lld", &sto[i]);
        }
        createtree(1, num, 1);
        FF(0, op)
        {
            char T;
            getchar();
            scanf("%c", &T);
            if (T == ‘Q‘)
            {
                LL a, b;
                scanf("%lld %lld", &a, &b);
                printf("%lld\n", query(a, b, 1));
            }
            else
            {
                LL a, b, v;
                scanf("%lld %lld %lld", &a, &b, &v);
                /*    for (int i = a; i <= b; i++)
                {
                sto[i] += v;
                }
                for (int i = 1; i <= num; i++)
                printf("%lld ", sto[i]);
                puts("");*/
                update(a, b, v, 1);
                //puts("");
            }
        }
    }

}
/*10 5
1 2 3 4 5 6 7 8 9 10
Q 2 4
C 3 6 3
Q 2 4
C 3 6 -3
Q 2 4
*/

时间: 2024-10-27 16:19:42

PKU-3468 simple problem with integer 线段树 LL坑点(欢迎讨论)的相关文章

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

HDU4267 A Simple Problem with Integers 线段树/树状数组

HDU4267 A Simple Problem with Integers  线段树/树状数组 2012长春网络赛A题 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. T

POJ3468_A Simple Problem with Integers(线段树/成段更新)

解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio> #define LL long long #define int_now int l,int r,int root using namespace std; LL sum[500000],lazy[500000]; void push_up(int root,int l,int r) { sum[ro

POJ3468__A Simple Problem with Integers (线段树)

本文出自blog.csdn.net/svitter --我大C++的指针岂是尔等能够简单领悟! 题意 给N个节点,标号A1~An,然后有Q个操作,操作分为Q i j,查询i,j间的区间和.C i j k,i到j个数字,每个数字增加k,并且输出. 输入输出分析 给N,Q,然后跟操作.注意判断Q,C使用scanf("%s"). 测试数据: 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 Samp

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: 62431   Accepted: 19141 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 题目描述: 一组数列, 可进行一段区间加上某一个数, 和区间查询 解题思路: 线段树, 之前的那道题是求总区间直接输出sum[1] 就可以了, 这次有了区间查询, 同理, 查询的时候Pushdown 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <map

[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