http://acm.hdu.edu.cn/showproblem.php?pid=1166
分析:
这里给出树状数组和线段树两种方法 不要介意两个不同的头文件
AC代码(树状数组):
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for (i=x;i<=y;i++)
#define DOWN(i,x,y) for (i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL longlong
#define N 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
int c[N];
int t,n;
void add(int k,int num){
W(k<=n){
c[k]+=num;
if(c[k]<0)
c[k]=0;
k+=lowbit(k);
}
}
int read(int k){
int sum=0;
W(k){
sum+=c[k];
k-=lowbit(k);
}
return sum;
}
int main (){
int i;
scanf ("%d",&t);
int count=1;
W(t--){
//freopen("data.txt","r",stdin);
MEM(c,0);
scanf("%d",&n);
UP(i,1,n){
int temp;
scanf ("%d",&temp);
add(i,temp);
}
printf ("Case %d:\n",count++);
char str[20];
int x,y;
W(scanf("%s",str)&&str[0]!='E'){
scanf ("%d%d",&x,&y);
if (str[0]=='A')
add(x,y);
else if(str[0]=='S')
add(x,-y);
else if(str[0]=='Q')
printf ("%d\n",read(y)-read(x-1));
}
}
return 0;
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define gcd(a,b) __gcd(a,b)
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const int MAX=1e6+5;
const double PI=acos(-1.0);
using namespace std;
LL a[MAX];
struct Tree{
int l,r;
LL val;
LL add; // 标记
}T[MAX];
void build(int rt,int l,int r){// 建树
T[rt].add=0;
T[rt].l=l;
T[rt].r=r;
if (l==r) {
T[rt].val=a[l];
return ;
}
int mid=(l+r)/2;
build(rt*2,l,mid);
build(rt*2+1,mid+1,r);
T[rt].val=T[rt*2].val+T[rt*2+1].val;
}
void pushDown(int rt,int l,int r){// 标记下推
if (T[rt].add!=0){
T[rt*2].add+=T[rt].add;
T[rt*2+1].add+=T[rt].add;
T[rt*2].val+=T[rt].add*l;
T[rt*2+1].val+=T[rt].add*r;
T[rt].add=0;
}
}
void update(int rt,int l,int r,int e){// 修改区间
if (T[rt].l>=l&&T[rt].r<=r){
T[rt].add+=e;
T[rt].val+=e*(T[rt].r-T[rt].l+1);
return ;
}
int mid=(T[rt].l+T[rt].r)/2;
pushDown(rt,mid-T[rt].l+1,T[rt].r-mid);
if (l<=mid) update(rt*2,l,r,e);
if (r>mid) update(rt*2+1,l,r,e);
T[rt].val=T[rt*2].val+T[rt*2+1].val;
}
LL Query (int rt,int Ql,int Qr){// 查询
if (T[rt].l>=Ql&&T[rt].r<=Qr) return T[rt].val;
int mid=(T[rt].l+T[rt].r)/2;
pushDown(rt,mid-T[rt].l+1,T[rt].r-mid);
LL ans=0;
if (Ql<=mid) ans+=Query(rt*2,Ql,Qr);
if (Qr>mid) ans+=Query(rt*2+1,Ql,Qr);
return ans;
}
int main (){
int cc=1;
int t;
//FIN;
scanf ("%d",&t);
while(t--){
printf ("Case %d:\n",cc++);
int n;
scanf ("%d",&n);
for (int i=1;i<=n;i++) scanf ("%lld",&a[i]);
build(1,1,n);
int m;
char st[20];
while (scanf ("%s",st)&&st[0]!='E'){
if (st[0]=='A'){
int s,v;
scanf ("%d%d",&s,&v);
update(1,s,s,v);
}
else if(st[0]=='S'){
int s,v;
scanf ("%d%d",&s,&v);
update(1,s,s,-v);
}
else{
int s,e;
scanf ("%d%d",&s,&e);
printf ("%lld\n",Query(1,s,e));
}
}
}
return 0;
}
289

被折叠的 条评论
为什么被折叠?



