poj3468 A Simple Problem with Integers(分块)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhanghaoxian1/article/details/82014870

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 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 Aa, Aa+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.

题意:区间加,区间求和。

分析:一个简单的分块题。

代码

#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#define N 200000
using namespace std;

struct arr
{
    int l,r;
    long long add,sum;
}t[N];
int a[N],pos[N],n,m,tot;

void change(int x, int y, int val)
{
    int p, q;
    p = pos[x];
    q = pos[y];
    if (p == q) 
    {
        for (int i = x; i <= y; i++) a[i] += val;
        t[p].sum += val * (y - x + 1);
    }
    else
    {
        for (int i = p + 1; i < q; i++) t[i].add += val;
        for (int i = x; i <= t[p].r; i++) a[i] += val;
        for (int i = t[q].l; i <= y; i++) a[i] += val;
        t[p].sum += val * (t[p].r - x + 1);
        t[q].sum += val * (y - t[q].l + 1);
    }
}

long long find(int x, int y)
{
    int p, q;
    p = pos[x];
    q = pos[y];
    long long s = 0;
    if (p == q)
    {
        for (int i = x; i <= y; i++) s += a[i];
        s += (y - x + 1) * t[p].add;
    }
    else 
    {
        for (int i = p + 1; i < q; i++) s += (t[i].r - t[i].l + 1) * t[i].add + t[i].sum;
        for (int i = x; i <= t[p].r; i++) s += a[i];
        for (int i = t[q].l; i <= y; i++) s += a[i];
        s += t[p].add * (t[p].r - x + 1);
        s += t[q].add * (y - t[q].l + 1);
    }
    return s;
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    tot = sqrt((double)n);
    for (int i = 1; i <= tot; i++)
    {
        t[i].l = (i - 1) * tot + 1;
        t[i].r = i * tot;
    }
    if (t[tot].r < n) t[++tot].l = t[tot - 1].r + 1, t[tot].r = n;
    for (int i = 1; i <= tot; i++)
        for (int j = t[i].l; j <= t[i].r; j++)
        {
            pos[j] = i;
            t[i].sum += a[j];
        }
    for (int i = 1; i <= m; i++)
    {
        char c[5];
        int x, y;
        scanf("%s%d%d", &c, &x, &y);
        if (c[0] == 'C')
        {
            int v;
            scanf("%d", &v);
            change(x, y, v);
        }
        else printf("%lld\n", find(x, y));
    }
}

猜你喜欢

转载自blog.csdn.net/zhanghaoxian1/article/details/82014870
今日推荐