【HDU1556】【POJ3468】区间更新

来源:互联网 发布:js filereader 编辑:程序博客网 时间:2024/05/20 05:03

HDU1556 Color the ball

题目链接:Color the ball

Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Description

N个气球排成一排,从左到右依次编号为1,2,3….N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽”牌
电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,
你能帮他算出每个气球被涂过几次颜色吗?

Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。

Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。

Sample Input
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0

Sample Output
1 1 1 3 2 1

Code

/*树状数组区间更新求点。区间左端点加1,右端点减1。这样求和得到的就是这个点的涂色次数。*/#include<stdio.h>#include<iostream>#include<string.h>using namespace std;const int MAXN=100010;int c[MAXN];int n;int lowbit(int x){    return x&(-x);}void add(int i,int val){    while(i<=n)    {        c[i]+=val;        i+=lowbit(i);    }}int sum(int i){    int s=0;    while(i>0)    {        s+=c[i];        i-=lowbit(i);    }    return s;}int main(){    int a,b;    while(scanf("%d",&n),n)    {        memset(c,0,sizeof(c));        for(int i=0;i<n;i++)        {            scanf("%d%d",&a,&b);            add(a,1);            add(b+1,-1);        }        for(int i=1;i<n;i++)          printf("%d ",sum(i));        printf("%d\n",sum(n));    }    return 0;}

POJ3468 A Simple Problem with Integers

题目链接: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

Solution

树状数组区间更新区间求和 ,参考 poj3468树状数组之区间更新+区间询问 看懂的。
引用过来:
更新的时候是区间更新 所以不能直接去一个个更新区间内的点,肯定会超时
对于每次更新C(a,b,d)表示区间[a,b]内的值增加d ,用ans[a]表示a~n区间元素增加的值,
所以对于C(a,b,d)有:ans[a]+=d,ans[b+1]-=d; 则每次询问的时候Q(a,b),
求a~b的和Sum=sum(a,b)+ans[a](b-a+1)+ans[a+1](b-a)…+ans[b] //sum(a,b)表示原数组中区间[a, b]的和
Sum=a+b+sum(ans[a+t](b-a-t+1)) = sum(a,b)+sum(ans[i](b-i+1)); a<=i<=b;
Sum=sum(a,b)+ (b+1)*sum(ans[i])-sum(ans[i]*i); //1~b所以(b+1)*sum(ans[i]),1~a-1则a*sum(ans[i])
所以可以用两个树状数组分别表示ans[i]的前缀和 和 ans[i]*i的前缀和 。
注意求ans[i]*i的前缀和会爆int。

Code

#include <cstdio>  #include <cstring>#include <iostream>#include <cstring>#include <algorithm>using namespace std;#define ll long long  const int N=1e5;int a[N+2],n;ll sum[N+2],c1[N+2],c2[N+2];int lowbit(int i){    return i&(-i);}void add(int i,int v,ll *c){    while(i<=n){        c[i]+=v;        i+=lowbit(i);    }}ll getsum(int i,ll *c){    ll ans=0;    while(i>0){        ans+=c[i];        i-=lowbit(i);    }    return ans;}int main(){    int q,aa,i,bb,v;    char c;    scanf("%d%d",&n,&q);    for(i=1;i<=n;++i){        scanf("%d",&a[i]);        sum[i]=sum[i-1]+a[i];    }    getchar();    for(i=0;i<q;++i){        scanf("%c",&c);        if(c=='C'){            scanf("%d%d%d",&aa,&bb,&v);            getchar();            add(aa,v,c1);            add(bb+1,-v,c1);            add(aa,v*aa,c2);            add(bb+1,-v*(bb+1),c2);        }        else if(c=='Q'){            scanf("%d%d",&aa,&bb);            getchar();            ll ans=sum[bb]-sum[aa-1]+(bb+1)*getsum(bb,c1)-aa*getsum(aa-1,c1)-getsum(bb,c2)+getsum(aa-1,c2);            printf("%I64d\n",ans);        }    }    return 0;}
0 0