Luo Gu U87561 magic moon cake

Luo Gu U87561 magic moon cake

Luo Gu Portal

Topic background

\ (9102 \) in the Mid-Autumn Festival is destined unlike in previous years ... because \ (9102 \) on the eve of the Mid-Autumn Festival, \ (Seaway \) was told this year's Mid-Autumn Festival moon cake to a new out - magic moon cake .

Title Description

Magic moon cake has a very strange effect - upgrade \ (IQ \) . This allows to obtain \ (Seaway \) just around the corner. \ (\ Seaway) Consideration is part of the brain is a long \ (N \) region, each has an initial intellectual intelligence point value. Magic moon cake can \ (Seaway \) thinking area from \ (x \) to \ (y \) intelligence value range are promoted \ (k \) points. However, the specific effect of each magic moon cake is not the same, that is to say, each moon cake scope and enhance the role of the points are not the same. Even so, \ (Seaway \) is very pleased with this moon cake. He bought a total of \ (M \) block magic moon cake, occasionally, when he bites, he wants to know their \ (IQ \) has been much improved. Then he queries \ (P \) times from his brain \ (x \) to \ (y \) intelligence and. But because he was too stupid (stupid if you do not need to eat the magic moon cake), so he did not know this and in the end is how much. Can you help him?

Input Format

Comprises a first line of input \ (3 \) integers: \ (N, M, P \) , meaning the subject as shown in FIG.

The second row includes a \ (N \) space-separated integers, wherein the first \ (I \) number indicates \ (Seaway \) of the brain \ (I \) an initial value of a point of intelligence.

The next \ (M + P \) rows, and each row contains a letter \ (2-3 \) integer, if the letters \ (C \) , followed by the \ (3 \) integers \ (X, the y-, k \) , on behalf \ (Seaway \) to eat a piece of moon cake, moon cake piece of his thinking from the area \ (x \) to \ (y \) intelligence section are improved \ (k \) points. If the letter is \ (Q \) , then followed by \ (2 \) integers \ (the X-, the y-\) , on behalf \ (Seaway \) wants to know how his brain from \ (x \) to \ (y \) and intelligence.

Output Format

Output comprising \ (P \) line, indicates \ (Seaway \) results for all query operations.

Tips and instructions

Data range and Conventions:

For \ (30 \% \) data, \ (. 1 \ Le N \ Le 10,1 \ Le M + P \ Le 10 \) .

For \ (60 \% \) data, \ (. 1 \ Le N \ Le 1000,1 \ Le M + P \ 10000 Le \) .

For \ (100 \% \) data, \ (. 1 \ Le N \ ^ 10 5,1 Le \ Le M + P \ Le. 5 ^ 10 \) .

All data assurance: \ (ANS \ Le maxlongint \) .

answer:

Segment tree template title, might answer my background would be some problems?

(I think it very clear ah ,,,)

Whispered one: not open longlong will die miserable. . .

Code:

#include<cstdio>
#include<iostream>
#define int long long
#define lson pos<<1
#define rson pos<<1|1
using namespace std;
const int maxn=1e5+1;
int n,m,p;
int a[maxn];
int tree[maxn<<2],lazy[maxn<<2];
void build(int pos,int l,int r)
{
    int mid=(l+r)>>1;
    if(l==r)
    {
        tree[pos]=a[l];
        return;
    }
    build(lson,l,mid);
    build(rson,mid+1,r);
    tree[pos]=tree[rson]+tree[lson];
}
void mark(int pos,int l,int r,int k)
{
    tree[pos]+=(r-l+1)*k;
    lazy[pos]+=k;
}
void pushdown(int pos,int l,int r)
{
    int mid=(l+r)>>1;
    mark(lson,l,mid,lazy[pos]);
    mark(rson,mid+1,r,lazy[pos]);
    lazy[pos]=0;
}
void update(int pos,int l,int r,int x,int y,int k)
{
    int mid=(l+r)>>1;
    if(x<=l && r<=y)
    {
        mark(pos,l,r,k);
        return;
    }   
    pushdown(pos,l,r);
    if(x<=mid)
        update(lson,l,mid,x,y,k);
    if(y>mid)
        update(rson,mid+1,r,x,y,k);
    tree[pos]=tree[lson]+tree[rson];
}
int query(int pos,int l,int r,int x,int y)
{
    int ret=0;
    int mid=(l+r)>>1;
    if(x<=l && r<=y)
        return tree[pos];
    pushdown(pos,l,r);
    if(x<=mid)
        ret+=query(lson,l,mid,x,y);
    if(y>mid)
        ret+=query(rson,mid+1,r,x,y);
    return ret;
}
signed main()
{
    scanf("%lld%lld%lld",&n,&m,&p);
    int q=m+p;
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    build(1,1,n);
    while(q--)
    {
        int x,y,k;
        char ch;
        cin>>ch;
        if(ch=='C')
        {
            scanf("%lld%lld%lld",&x,&y,&k);
            update(1,1,n,x,y,k);
        }
        else
        {
            scanf("%lld%lld",&x,&y);
            printf("%lld\n",query(1,1,n,x,y));
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11670930.html