"Explanations" Luo Valley P2357 grave keeper

Portal

Portal1: Luogu

Description

There is a venerable on a desolate cemetery grave keeper, caretaker of the cemetery he had never stolen before, so people feel comfortable in his own tomb ancestors settled in his grave keeper can be optimistic this cemetery It is inevitable rather than accidental .....

Since the grave keeper .... understand feng shui. He cemetery is divided into major and minor tombstone tombstones, mainly tombstone only \ (1 \) months, the grave keeper he referred to as \ (1 \) number, while the secondary tombstones \ (n-1 \) one, the grave keeper of the number \ (2, 3 \ the n-DOTS \) , constitutes a (n \) \ tombstones in the cemetery.

And each tombstone has an initial value of feng shui, these values ​​determine the good or bad feng shui feng shui cemetery, the grave keeper often need to query these tombstones.

Good use of feng shui, the grave keeper, by Guards change it again and again, so that they own infinite life, no one knows how long he lived.

That day, you're lucky to visit him, he asked you and he witnessed the next few years of his victories, but he counted every time but feng shui values and needs you to help him calculate miscalculated he will want you life QAQ, feng shui is not immutable, unless special circumstances were known in the next \ (2147483647 \) years, there will be \ (n \) times of disaster, the grave keeper will have several operations:

  1. The \ ([l, r] \ ) Feng Shui all the values of this interval increases tombstone \ (K \) ;

  2. Feng Shui master will increase the value of the tombstone \ (k \) ;

  3. The Feng Shui tombstone reduced value of the main \ (K \) ;

  4. Statistics \ ([l, r] \ ) feng shui value of all the tombstones of this interval and;

  5. Lord tombstones feng shui value.

Said above, many people will tombs of their ancestors at home here, and the grave keeper lived many centuries, the number of tombstone will be more of you can not believe, the grave keeper kindly invite you to help him complete these operations, Otherwise your hotel the day of the explosion, the sky under the knife \ (\ dots \ dots \) in order to survive, or to help him.

Input

The first line, two positive integers \ (n, f \) represents a total of \ (n \) tombstones, and in the next \ (2147483647 \) years, there will be \ (f \) times end of the world;

The second line, \ (n-\) positive integer, represents the \ (I \) Feng Shui value of tombstones;

Next \ (f \) row each have a solution for the end of the world, such as the title, the same title tag.

Output

Output will be several rows of \ (4 \) and \ (5 \) questions to answer.

Sample Input

5 7
0 0 0 0 0
1 1 5 1
1 1 3 3
2 3
3 1
4 1 5
2 1
5

Sample Output

16
7

Hint

\ (20 \% \) data satisfies: \ (. 1 \ n-Le \ Le 100 \) ;

\ (50 \% \) data satisfies: \ (. 1 \ n-Le \ Le 6000 \) ;

\ (100 \% \) data satisfies: \ (. 1 \ n-Le, F \ 2 Le \ Times 10. 5 ^ {} \) ;

Solution

This problem Fenwick tree.

Use c1[]to maintain and, c2[]to maintain the plot.

The inquiry \ ([x, y] \ ) interval can query(y) - query(x - 1) + (x == 1) * 主墓碑的风水值be evaluated, which query(y) - query(x - 1)is similar to a prefix and, (x == 1) * 主墓碑的风水值represents \ ([x, y] \ ) interval does not contain a primary package gravestone, if coupled with the main comprising a single tombstone maintenance.

Wherein the main tombstone need to maintain separate.

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

typedef long long LL;
const int MAXN = 500005;
int n, m, last;
LL first_value, c1[MAXN], c2[MAXN];
inline int lowbit(int x) {//树状数组专属操作
    return x & -x;
}
inline void update(int x, LL val) {//更新
    for (LL i = x; i <= n; i += lowbit(i)) {
        c1[i] += val;
        c2[i] += val * x;
    }
}
inline LL query(int x) {//查询
    LL ret = 0;
    for (LL i = x; i; i -= lowbit(i))
        ret += (x + 1) * c1[i] - c2[i];
    return ret;
}
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        int x;
        scanf("%d", &x);
        update(i, x - last);
        last = x;
    }
    while (m--) {
        int opt, x, y;
        LL val;
        scanf("%d", &opt);
        if (opt == 1) {
            scanf("%d%d%lld", &x, &y, &val);
            update(x, val);
            update(y + 1, -val);
        } else
        if (opt == 2) {
            scanf("%lld", &val);
            first_value += val;
        } else
        if (opt == 3) {
            scanf("%lld", &val);
            first_value -= val;
        } else
        if (opt == 4) {
            scanf("%d%d", &x, &y);
            printf("%lld\n", query(y) - query(x - 1) + (x == 1) * first_value);
        } else printf("%lld\n", query(1) + first_value);//统一维护的加单独维护的
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/shenxiaohuang/p/11221149.html