1. 程式人生 > >A Simple Problem with Integers(poj 3468) (lazy標記)

A Simple Problem with Integers(poj 3468) (lazy標記)

A Simple Problem with Integers

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 130621 Accepted: 40554
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 number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The 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 AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa

Aa+1, ... , Ab.

Output

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

Source

型別二:區間更新。

lazy策略:延遲標記(懶惰標記)簡單來說就是每次更新的時候不要更新到底,用延遲標記使得更新延遲到下次需要更新或者查詢的時候。

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+5;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
long long sum[N<<2],add[N<<2];
struct Node
{
    int l,r;
    int mid()
    {
        return (l+r)>>1;
    }
}tree[N<<2];
void PushUp(int rt)//通過當前節點rt把值遞歸向上更新到根節點
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt,int m)//通過當前節點rt遞歸向下去更新rt子節點的值
{
    if(add[rt])
    {
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];
        sum[rt<<1]+=add[rt]*(m-(m>>1));
        sum[rt<<1|1]+=add[rt]*(m>>1);
        add[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    add[rt]=0;
    if(l==r)
    {
        scanf("%lld",&sum[rt]);
        return;
    }
    int m=tree[rt].mid();
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(int c,int l,int r,int rt)
{
    if(tree[rt].l==l&&tree[rt].r==r)
    {
        add[rt]+=c;
        sum[rt]+=(long long)c*(r-l+1);
        return;
    }
    if(tree[rt].l==tree[rt].r) return;
    PushDown(rt,tree[rt].r-tree[rt].l+1);
    int m=tree[rt].mid();
    if(r<=m) update(c,l,r,rt<<1);
    else if(l>m) update(c,l,r,rt<<1|1);
    else
    {
        update(c,l,m,rt<<1);
        update(c,m+1,r,rt<<1|1);
    }
    PushUp(rt);
}
long long query(int l,int r,int rt)
{
    if(l==tree[rt].l&&r==tree[rt].r) return sum[rt];
    PushDown(rt,tree[rt].r-tree[rt].l+1);
    int m=tree[rt].mid();
    long long res=0;
    if(r<=m) res+=query(l,r,rt<<1);
    else if(l>m) res+=query(l,r,rt<<1|1);
    else
    {
        res+=query(l,m,rt<<1);
        res+=query(m+1,r,rt<<1|1);
    }
    return res;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        build(1,n,1);
        while(m--)
        {
            char ch[2];
            scanf("%s",ch);
            int a,b,c;
            if(ch[0]=='Q')
            {
                scanf("%d%d",&a,&b);
                printf("%lld\n",query(a,b,1));
            }
            else
            {
                scanf("%d%d%d",&a,&b,&c);
                update(c,a,b,1);
            }
        }
    }
    return 0;
}

相關推薦

A Simple Problem with Integerspoj 3468 (lazy標記)

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 130621 Accepted: 40554 Case

POJ 3468 A Simple Problem with Integers線段樹

truct fin clas define class 基本 open urn display 題目鏈接:A Simple Problem with Integers 題意:N個數字,M次操作;操作分為兩種,第一種將$[L,R]$區間內的每個數都加上C,第二種為求$[L,

bzoj3212: Pku3468 A Simple Problem with Integers線段樹

while printf scan font geo main post align sim 3212: Pku3468 A Simple Problem with Integers 題目:傳送門 題解:    感謝Rose_max大佬的傾情相

A Simple Problem with Integers區間更新

#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long l

POJ3468 A Simple Problem with Integers區間更新

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 106587 Accepted: 33254 Case Time Limit: 2

poj 3468 A Simple Problem with Integers原來是一道簡單的線段樹區間修改用來練練splay

long 兩個 可能 style push ios stream 區間 pan 題目鏈接:http://poj.org/problem?id=3468 題解:splay功能比線段樹強大當然代價就是有些操作比線段樹慢,這題用splay實現的比線段樹慢上一倍。線段樹用l

POJ-3468 A Simple Problem with Integers線段樹、段變化+段查詢、模板

sum .org miss numbers ... bsp wid scanf accepted A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Su

POJ 3468 A Simple Problem with Integers線段樹 單點更新+區間求和

names || log shu 更新 can pro struct sim 題目鏈接:http://poj.org/problem?id=3468 題意:單點更新,區間求和。 題解:裸題 1 //POJ 3468 A Simple Problem with

POJ 3468 A Simple Problem with Integers線段樹模板之區間增減更新 區間求和查詢

return string ali accept numbers other map nts contain A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 13107

POJ 3468 A Simple Problem with Integers線段樹區間更新區間查詢

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 Accepted: 28818 Case Time Limit: 20

Poj】-A Simple Problem with Integers線段樹,區間更新

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 100877 Accepted: 31450 Case Time Limit: 2

poj3511--A Simple Problem with Integers線段樹求和

poj pac style som can com onos roman miss A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K

poj3468 A Simple Problem with Integers 樹狀數組做法

align style cee arch puppet ret 定義 pre tab 題目傳送門 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K

BZOJ3212 Pku3468 A Simple Problem with Integers線段樹區間求和、區間加模板

題目大意: 你有N個整數,A1,A2,.,An。你需要處理兩種操作。一種操作是在給定的區間內向每個數字加上一個給定的數字。另一種是求給定區間內的數字之和。   題解: 線段樹的基本操作。 lazy標記。 附上程式碼: #include<cstdio> #includ

A Simple Problem with Integers線段樹區間更新

【題意】 給定長度為 NNN 的序列 AAA,有 QQQ次操作,操作分兩種 CabcC \ a \ b \ cCabc 表示將序列中 A[a,b]A[a,b]A[a,b]所有元素都加上一個數字 ccc QabQ \ a \ bQab 表示查詢序列 A[a,b]

A Simple Problem with Integers 線段樹應用型別三【區間增加 區間查詢】【模板基礎題】

題目連結:http://poj.org/problem?id=3468 參考部落格:https://blog.csdn.net/u013480600/article/details/22202711 Description You have N integers,

A Simple Problem with Integers 線段樹,區間求和+區間更新

A Simple Problem with Integers 連結:傳送門 You have NN integers, A1,A2,⋯,ANA1,A2,⋯,AN. You need to deal with two kinds of operations. O

poj 3468 A Simple Problem with Integers 【區間修改+區間查詢樹狀陣列

參考下面部落格的公式: 需要注意的是,輸入初始數列的時候要 add 兩次, L = R, add (L, x), add (R+1, -x) #include <iostream>

C - A Simple Problem with Integers POJ - 3468 線段樹模版區間查詢區間修改

線段樹模版 amp else 計算 更新 namespace scanf spa ger 參考qsc大佬的視頻 太強惹 先膜一下 視頻在b站 直接搜線段樹即可 1 #include<cstdio> 2 using namespace std; 3 con

poj 3468 A Simple Problem with Integers

arch fin uil range swe next char () limit A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total