题目链接   https://vjudge.net/problem/34215/origin

这个题就是线段树裸题,有两种操作,实现单点更新和区间和的查找即可,这里第一次学习使用树状数组完成。

二者相比,BIT无论从时间,空间还是代码量考虑都要优于ST,所以遇见这种求解区间和的操作考虑使用BIT。

 /*线段树 250 ms*/

 #include<bits/stdc++.h>
using namespace std;
#define M ((L+R)>>1)
#define lc (id<<1)
#define rc ((id<<1)|1)
int sumv[(<<)+];
void build(int L,int R,int id)
{
if(L==R){scanf("%d",&sumv[id]);return;}
build(L,M,lc);
build(M+,R,rc);
sumv[id]=sumv[lc]+sumv[rc];
}
void update(int L,int R,int id,int x,int y)
{
if(L==R) {sumv[id]=y;return;}
if(x<=M)update(L,M,lc,x,y);
else update(M+,R,rc,x,y);
sumv[id]=sumv[lc]+sumv[rc];
}
int ask(int L,int R,int id,int l,int r)
{
if(L>=l&&R<=r) {return sumv[id];}
if(r<=M) return ask(L,M,lc,l,r);
else if(l>M) return ask(M+,R,rc,l,r);
else return ask(L,M,lc,l,r)+ask(M+,R,rc,l,r);
}
int main()
{
int N,i,j,x,y,k=;
char s[];
while(cin>>N&&N){
build(,N,);
if(k>) puts("");
printf("Case %d:\n",++k);
while(scanf("%s",s)!=EOF){
if(!strcmp(s,"END")) break;
scanf("%d%d",&x,&y);
if(!strcmp(s,"M")) printf("%d\n",ask(,N,,x,y));
else update(,N,,x,y);
}
}
return ;
} /*树状数组 180ms */ #include<bits/stdc++.h>
using namespace std;
int sumv[+],n;
int a[+];
int lowbit(int x){return x&-x;}
int sum(int x)
{
int ret=;
while(x>){
ret+=sumv[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<=n){
sumv[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int k=;
while(scanf("%d",&n)!=EOF&&n){memset(sumv,,sizeof(sumv));
for(int i=;i<=n;++i){
int x;
scanf("%d",&x);
a[i]=x;
add(i,x);
}
char s[];
if(k>) puts("");
printf("Case %d:\n",++k);
while(scanf("%s",s)!=EOF){
if(!strcmp(s,"END")) break;
int x,y;
scanf("%d%d",&x,&y);
if(s[]=='M'){
printf("%d\n",sum(y)-sum(x-));
}
else{
add(x,y-a[x]);
a[x]=y;
}
}
}
return ;
}

最新文章

  1. low security dvwa--SQL Injection
  2. 获取&lt;img src=&quot;sdf.jpg&quot; Big=&quot;sf.jpg&quot;&gt;中的big的值
  3. [Sharepoint]备份 迁移 还原
  4. IPC_共享内存
  5. 环信_EaseUI 使用指南
  6. 使用alloctor模板来实现string类
  7. GPIO模拟串口注意是事项
  8. 总结一下.net framework适合装在哪些系统中
  9. 学习使用crosswalk
  10. Lichee(两) 在sun4i_crane该平台下编译
  11. 引用WCF地址报下载“https://xxx:8004/TerminalHandler.svc?disco”时出错问题
  12. PHP初入--添加内容到框框里并删除
  13. SpringMVC 控制器默认支持GET和POST两种方式
  14. IE8兼容border-radius.
  15. (NO.00001)iOS游戏SpeedBoy Lite成形记(一)
  16. (转)理解CPU steal time
  17. Springboot 之 多配置文件
  18. Codeforces Round #503 (by SIS, Div. 2)-C. Elections
  19. 2017ACM/ICPC广西邀请赛-重现赛
  20. windows下用nginx配置https服务器

热门文章

  1. 剑指offer-java
  2. python16_day20【Django_继续抽屉项目】
  3. centos7编译安装Python3.6(与2.7并存)
  4. 函数对象[条款18]---《C++必知必会》
  5. 了解Java应用中的开发攻击
  6. java对象在JVM堆中的数据结构
  7. Appium移动自动化
  8. JS答辩习题
  9. C#打开文件资源管理器
  10. Java-Minor GC、Major GC、Full GC