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

WA了N次,问到底哪里错了(附代码)

Posted by wmyw961 at 2011-03-05 20:24:15 on Problem 3468
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long a[100001];

struct node
{
  long long sum,add;
}Tree[1100000];

int n,l,r,k;
long long dat;
char order;
void build(int x,int f,int t)
{
     if (f==t){
        Tree[x].sum=a[f];
        return;
     }
     int mid=(f+t)/2;
     build(x*2,f,mid);
     build(x*2+1,mid+1,t);
     Tree[x].sum = Tree[x*2].sum + Tree[x*2+1].sum;
}

long long sum(int x,int f,long long t)
{
    if (f>=l && t<=r)
    {
       long long tmp = Tree[x].sum + (t-f+1) * Tree[x].add;
       return tmp;
    }
       
    if (Tree[x].add!=0)
    {
        Tree[x].sum += (t-f+1) * Tree[x].add;
        Tree[x*2].add += Tree[x].add;
        Tree[x*2+1].add += Tree[x].add;
        Tree[x].add = 0;
    }
    
    long long ans1=0;
    int mid=(f+t)/2;
    if (l<=mid) ans1+=sum(x*2,f,(f+t)/2);
    if (r>=mid+1) ans1+=sum(x*2+1,(f+t)/2+1,t);
    return ans1;
}

void changed(int x,int f,int t)
{
     if (f>=l && t<=r)
     {
         Tree[x].add += dat;
         return;
     }
     int mid=(f+t)/2;
     if (l<=mid) changed(x*2,f,(f+t)/2);
     if (r>=mid+1) changed(x*2+1,(f+t)/2+1,t);
     
     Tree[x].sum = Tree[x*2].sum + Tree[x*2].add * ((f+t)/2 - f + 1);
     Tree[x].sum += Tree[x*2+1].sum + Tree[x*2+1].add * (t - (f+t)/2);
}
         
int main(){
    scanf("%d%d",&n,&k);
    for (int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    build(1,1,n);
    getchar();
    for (int i=1;i<=k;i++)
    {
          scanf("%c",&order);
          if (order=='Q')
          {
             scanf("%d%d",&l,&r);
             printf("%I64d\n",sum(1,1,n));
          }
          else
          {
              scanf("%d%d%I64d",&l,&r,&dat);
              changed(1,1,n);
          }
          getchar();
    }
    //system("pause");
}

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