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

TLE半天了,大牛们帮忙看看吧,已经用了树状数组了

Posted by userwlm at 2008-07-07 01:57:54 on Problem 1195
#include <cstdlib>
#include <iostream>

using namespace std;
int matrix[1024*1024+1];
int lowbit(int i)
{
  return i&(i^(i-1)); 
}


int main(int argc, char *argv[])
{
    int a,n;
    scanf("%d%d",&a,&n);
    while(scanf("%d",&a))
    {
       if(a==3)
          break;
       
       else if(a==1)
       {
         int a,b,c;
         scanf("%d%d%d",&a,&b,&c);
         int x=a*n+b+1;
         //change(n*n,a*n+b+1,c);
         //void change(int a , int x , int y)
         while(x<=n*n)
         {
           matrix[x]+= c;
           x=x+lowbit(x);
         }
       }
       else if(a==2)
       {
          int a,b,c,d,sum=0;
          scanf("%d%d%d%d",&a,&b,&c,&d);
          for(int i=a;i<=c;i++)
          {
              //sum+=getsum(b+i*n,d+i*n+1);
              //int getsum(int x,int y)
       
                int sumx=0,sumy=0;
                int x=b+i*n,y=d+i*n+1;
                while(x>0)
                {
                   sumx+=matrix[x];
                   x=x-lowbit(x);
                }
                while(y>0)
                {
                   sumy+=matrix[y];
                   y=y-lowbit(y);
                }
               sum+=sumy-sumx;
              
          }
          printf("%d\n",sum);
       }
         
     }
       
    system("PAUSE");
    return EXIT_SUCCESS;
}

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