poj_3468.A Simple Problem with Integers(线段树/分块)

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 86636   Accepted: 26898
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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.

线段树入门题,因为case数够多,需要用lazytag思想,同时注意不能用cin>>,会超时,同时宏定义也会有时间消耗,对于这道题差不多是100~200ms的时间消耗。不过懒,还是喜欢宏定义。。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define inf 0x3f3f3f3f
#define maxn 1000010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
#define lson l,m,i<<1
#define rson m+1,r,i<<1|1
#define N 100010
using namespace std;

LL sum[N<<2],add[N<<2];

struct node
{
    int l,r;
    int mid()
    {
        return l+r>>1;

    }
}tree[N<<2];

void PushUp(int i)
{
    sum[i]=sum[i<<1]+sum[i<<1|1];
}

void PushDown(int i,int m)
{
    if(add[i])
    {
        add[i<<1]+=add[i];
        add[i<<1|1]+=add[i];
        sum[i<<1]+=add[i]*(m-(m>>1));
        sum[i<<1|1]+=add[i]*(m>>1);
        add[i]=0;
    }
}
void update(int l,int r,int c,int i)
{
    if(tree[i].l==l&&tree[i].r==r)
    {
        add[i]+=c;
        sum[i]+=(LL)c*(r-l+1);
        return ;
    }
    if(tree[i].l==tree[i].r)return ;

    PushDown(i,tree[i].r-tree[i].l+1);
    int m=tree[i].mid();
    if(r<=m)update(l,r,c,i<<1);
    else if(l>m)update(l,r,c,i<<1|1);
    else
    {
        update(l,m,c,i<<1);
        update(m+1,r,c,i<<1|1);

    }
    PushUp(i);
}

LL query(int l,int r,int i)
{
    if(tree[i].l==l&&tree[i].r==r)
    {
        return sum[i];
    }
    PushDown(i,tree[i].r-tree[i].l+1);
    int m=tree[i].mid();
    LL res=0;
    if(r<=m)res+=query(l,r,i<<1);
    else if(l>m)res+=query(l,r,i<<1|1);
    else
    {
        res+=query(l,m,i<<1);
        res+=query(m+1,r,i<<1|1);

    }
    return res;
}
void build(int l,int r,int i)
{
    tree[i].l=l;
    tree[i].r=r;
    add[i]=0;
    if(l==r)
    {
        scanf("%lld",&sum[i]);
        return ;
    }
    int m=tree[i].mid();
    build(lson);
    build(rson);
    PushUp(i);
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int i;
        build(1,n,1);
        while(m--)
        {
            char s[2];
            //string s;
            //cin>>s;
            Ss(s);
            if(s[0]=='Q')
            {
                int a,b;
                Si(a);Si(b);
                Pl(query(a,b,1));
            }
            else if(s[0]=='C')
            {
                int a,b,c;
                Si(a);Si(b);Si(c);
                update(a,b,c,1);
            }
        }
    }
    return 0;
}

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define FOP freopen("data.txt","r",stdin)
#define FOP2 freopen("data1.txt","w",stdout)
#define inf_LL 4223372036854775807
#define inf 0x3f3f3f3f
#define maxn 100010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

int n, m, blo;
int bl[maxn];
LL v[maxn], atag[maxn], sum[maxn];

void add(int a, int b, LL c)
{
    int mi = min(b, bl[a]*blo);
    for(int i = a; i <= mi; i++) v[i] += c, sum[bl[i]] += c;
    if(bl[a] != bl[b])
        for(int i = (bl[b]-1)*blo+1; i <= b; i++) v[i] += c, sum[bl[i]] += c;
    for(int i = bl[a]+1; i <= bl[b]-1; i++) atag[i] += c, sum[i] += c*blo;
}

LL query(int a, int b)
{
    LL res = 0;
    int mi = min(b, bl[a]*blo);
    for(int i = a; i <= mi; i++) res += v[i]+atag[bl[i]];
    if(bl[a] != bl[b])
        for(int i = (bl[b]-1)*blo+1; i <= b; i++) res += v[i]+atag[bl[i]];
    for(int i = bl[a]+1; i <= bl[b]-1; i++) res += sum[i];
    return res;
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        memset(atag, 0, sizeof(atag));
        memset(sum, 0, sizeof(sum));

        blo = sqrt(1.0*n);
        for(int i = 1; i <= n; i++) bl[i] = (i-1)/blo + 1;

        for(int i = 1; i <= n; i++) scanf("%lld", &v[i]), sum[bl[i]] += v[i];

        char s[3];
        int a, b; LL c;
        while(m--)
        {
            scanf("%s%d%d", s, &a, &b);
            if(s[0] == 'C') scanf("%lld", &c), add(a, b, c);
            else if(s[0] == 'Q') printf("%lld\n", query(a, b));
        }
    }
    return 0;
}


你可能感兴趣的:(---线段树,poj,数据结构,分块)