| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
这个题可以用树状数组吗?我觉得得这题用树状数组更好解呀,为什么我就T了呢?
望大牛指点!
#include <iostream>
using namespace std;
#define temp 100005
int cc[temp];
int n;
int lowbit(int x)
{
return x&(x^(x-1));
}
__int64 sum(int pos)
{
__int64 num=0;
while(pos>0)
{
num+=cc[pos];
pos-=lowbit(pos);
}
return num;
}
void update(int pos,int d)
{
while(pos<=n)
{
cc[pos]+=d;
pos+=lowbit(pos);
}
}
int main()
{
int i,j,k,q;
scanf("%d%d",&n,&q);
for(i=0;i<n;i++)
{
scanf("%d",&k);
update(i+1,k);
}
while(q--)
{
char ch;
int a,b,c;
getchar();
scanf("%c",&ch);
if(ch=='Q')
{
scanf("%d%d",&a,&b);
printf("%I64d\n",sum(b)-sum(a-1));
}
if(ch=='C')
{
scanf("%d%d%d",&a,&b,&c);
for(i=a;i<=b;i++)
update(i,c);
}
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator