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

这个为什么会W呢···这代码哪儿错了

Posted by 11115456 at 2012-10-06 23:38:05 on Problem 3468
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const long long M = 500010;
long long a[M]={0};
long long n;
struct tree{
long long l,r,m;
long long sum,mark;
};
//typedef tree tree;
tree tr[M*4];

void build_tree(long long l,long long r,long long num)
{
    tr[num].l=l;
    tr[num].r=r;
    tr[num].m=(l+r)/2;
    tr[num].mark=0;
    if(l==r)
    {
        tr[num].sum=a[l];
        return ;
    }
    build_tree(l,tr[num].m,num<<1);
    build_tree(tr[num].m+1,r,(num<<1)+1);
    tr[num].sum = tr[num<<1].sum+tr[(num<<1)+1].sum;
}

void update_tree(long long l,long long r,long long  num,long long ad)
{
    if(tr[num].l==l&&tr[num].r==r)
    {
        tr[num].mark+=ad;
        return;
    }
    tr[num].sum +=(ad*(r-l+1));
    if(tr[num].m>=r)
    update_tree(l,r,num<<1,ad);
    else if(tr[num].m<l)
    update_tree(l,r,(num<<1)+1,ad);
    else
    {
        update_tree(l,tr[num].m,num<<1,ad);
        update_tree(tr[num].m+1,r,(num<<1)+1,ad);
    }
}

long long query_tree(long long l,long long r,long long num)
{
    if(l==tr[num].l&&r==tr[num].r)
    {
        return tr[num].sum+(l-r+1)*tr[num].mark;
    }
    if(tr[num].mark!=0)
    {
        tr[num].sum+=(tr[num].r-tr[num].l+1)*tr[num].mark;
        tr[num*2].mark=tr[num].mark;
        tr[num*2+1].mark=tr[num].mark;
        tr[num].mark=0;
    }
    if(tr[num].m>=r)
    return query_tree(l,r,num*2);
    else if(tr[num].m<l)
    return query_tree(l,r,num*2+1);
    else return query_tree(l,tr[num].m,num*2)+query_tree(tr[num].m+1,r,num*2+1);
}

int main()
{
    long long m;
    cin>>n>>m;
    long long  n1,n2,n3;
    long long ans;
    char c;
    for(long long i=1;i<=n;i++)
    cin>>a[i];
    build_tree(1,n,1);
    for(long long i=0;i<m;i++)
    {
        cin>>c;
        if(c=='Q')
       {
           cin>>n1>>n2;
           ans=query_tree(n1,n2,1);
           cout<<ans<<endl;
       }
       else if (c=='C')
       {
           cin>>n1>>n2>>n3;
           update_tree(n1,n2,1,n3);
       }
    }
}

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