1. 程式人生 > >A Simple Problem with 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. 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
There is only one testcase.

The first line contains two numbers NN and QQ. 1≤N,Q≤1000001≤N,Q≤100000.

The second line contains NN numbers, the initial values of A1,A2,⋯,ANA1,A2,⋯,AN. −1000000000≤Ai≤1000000000−1000000000≤Ai≤1000000000.

Each of the next QQ lines represents an operation.

C a b c means adding cc to each of Aa,Aa+1,⋯,AbAa,Aa+1,⋯,Ab. −10000≤c≤10000−10000≤c≤10000.
Q a b means querying the sum of Aa,Aa+1,⋯,AbAa,Aa+1,⋯,Ab.
Output
You need to answer all QQ 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 3232-bit integers.

The data used in this problem is unofficial data prepared by standy. So any mistake here does not imply mistake in the offcial judge data.

思路:較簡單的區間更新和區間求和。

#include<stdio.h>
#include<string.h>
#define N 100009

struct node
{
    int l,r;
    long long p;
    long long xia;
} tree[N*3+10];

int a[N];
void build(int num,int l,int r)//建樹
{
    tree[num].l=l;
    tree[num].r=r;
    tree[num].xia=0;
    if(l==r)
    {
        tree[num].p=a[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(num<<1,l,mid);
    build(num<<1|1,mid+1,r);
    tree[num].p=tree[num<<1].p+tree[num<<1|1].p;
}
void cont(int num)//再次遇見以前要更新的區間時,將左右子區間更新
{
    if(tree[num].xia)
    {
        int l=tree[num].l,r=tree[num].r,mid=(l+r)/2;
        long long add=tree[num].xia;
        tree[num<<1].p+=(mid-l+1)*add;
        tree[num<<1|1].p+=(r-mid)*add;
        tree[num<<1].xia+=add;
        tree[num<<1|1].xia+=add;
        tree[num].xia=0;
    }
}
long long query(int num,int x,int y)//區間求和
{
    int l=tree[num].l,r=tree[num].r,mid=(l+r)/2;
    if(l>=x&&y>=r)
        return tree[num].p;
    cont(num);
    long long sum=0;
    if(mid>=x) sum+=query(num<<1,x,y);
    if(mid<y) sum+=query(num<<1|1,x,y);
    tree[num].p=tree[num<<1].p+tree[num<<1|1].p;
    return sum;
}
void update(int x,int y,long long z,int num)//區間更新
{
    int l=tree[num].l,r=tree[num].r,mid=(l+r)/2;
    if(x<=l&&r<=y)//現將需要更新的區間記錄下來,再次遇到時用void cont(int num)函式處理
    {
        tree[num].p+=(r-l+1)*z;
        tree[num].xia+=z;
        return ;
    }
    cont(num);//重要的一步
    if(mid>=x) update(x,y,z,num<<1);
    if(mid<y) update(x,y,z,num<<1|1);
    tree[num].p=tree[num<<1].p+tree[num<<1|1].p;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        build(1,1,n);
        char b[2];
        int x,y;
        long long z;
        while(m--)
        {
            scanf("%s",b);
            if(b[0]=='Q')
            {
                scanf("%d%d",&x,&y);
                printf("%lld\n",query(1,x,y));
            }
            else
            {
                scanf("%d%d%lld",&x,&y,&z);
                update(x,y,z,1);
            }
        }
    }
    return 0;
}

相關推薦

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

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

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大佬的傾情相

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

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

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

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,

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,

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

A Simple Problem with Integers線段區間更新 區間查詢】

POJ   3468 題意就不說了,之間看程式碼; 程式碼是對的,如果大佬覺得註釋有問題可以說,會仔細更改? #include<stdio.h> #include<string.h> #include<string> #include

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

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

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

A Simple Problem with Integers區間更新

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

A Simple Problem with Integerspoj 3468 (lazy標記)

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

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

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

線段專題 POJ3468 A Simple Problem with Integers

strong print style update else algo linker clas uil 題意:n個點。m個操作。兩種操作類型。C X Y K 表示區間[x,y]上每一個點值加k。Q X Y 求區間[x,y]的和 分析:線段樹區間求和,裸模板 註意