Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

必须要用lazy思想否则超时自己看着办!

Posted by chenxuan123456789 at 2012-09-15 20:43:27 on Problem 3468
#include <iostream> 
#include <cstdio> 
#include <cstdlib> 
#define M 100010 
using namespace std; 
__int64 map[M]; 
int m,n; 
__int64 ans; 
struct TREE 
{ 
    int l,r;//左节点,右节点 
    __int64 p,sum;//公共增量,区间和 
}T[M<<2]; 
void create(int u,int l,int r) 
{ 
    T[u].l=l;T[u].r=r;T[u].p=0; 
    if(l==r) 
    { 
        T[u].sum=map[l]; 
        return; 
    } 
    int mid=(T[u].l+T[u].r)>>1; 
    create(u<<1,l,mid); 
    create(u<<1|1,mid+1,r); 
    T[u].sum=T[u<<1].sum+T[u<<1|1].sum; 
} 
void updata(int u,int l,int r,__int64 c) 
{ 
    if(T[u].l==l&&T[u].r==r) 
    { 
        T[u].p+=c; 
        return; 
    } 
    T[u].sum+=(r-l+1)*c; 
    int mid=(T[u].l+T[u].r)>>1; 
    if(r<=mid) 
        updata(u<<1,l,r,c); 
    else if(l>=mid+1) 
        updata(u<<1|1,l,r,c); 
    else
    { 
        updata(u<<1,l,mid,c); 
        updata(u<<1|1,mid+1,r,c); 
    } 
} 
__int64 query(int u,int l,int r) 
{ 
    __int64 del=T[u].p; 
    if(T[u].l==l&&T[u].r==r) 
        return(T[u].sum+del*(r-l+1)); 
    else
    { 
        T[u<<1].p+=T[u].p; 
        T[u<<1|1].p+=T[u].p; 
        T[u].sum+=del*(T[u].r-T[u].l+1); 
        T[u].p=0; 
    } 
    int mid=(T[u].l+T[u].r)>>1; 
    if(r<=mid) 
         return(query(u<<1,l,r)); 
    else if(l>=mid+1) 
         return(query(u<<1|1,l,r)); 
    else
         return(query(u<<1,l,mid)+query(u<<1|1,mid+1,r)); 
} 
int main() 
{ 
    char a[7];int b,c;__int64 d; 
    while(scanf("%d%d",&n,&m)!=EOF) 
    { 
        for(int i=1;i<=n;i++) 
            scanf("%I64d",&map[i]); 
        create(1,1,n); 
        while(m--) 
        { 
            scanf("%s",a); 
            if(a[0]=='C') 
            { 
                scanf("%d%d%I64d",&b,&c,&d); 
                updata(1,b,c,d); 
            } 
            else
            { 
                scanf("%d%d",&b,&c); 
                ans=0; 
                ans=query(1,b,c); 
                printf("%I64d\n",ans); 
            } 
        } 
    } 
    system("pause"); 
    return 0; 
} 

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator