线段树(小写一发)
来源:互联网 发布:在线制作手机淘宝店招 编辑:程序博客网 时间:2024/05/17 22:09
题目信息:敌兵布阵
代码:
#include<stdio.h>#include<string.h>const int maxn =50000;int sum[maxn<<2];//void pushup(int rt)//{// sum[rt]=sum[rt<<1]+sum[(rt<<1)+1];////}//void pushup(int ret)//{// sum[ret]=sum[ret<<1]+sum[ret<<1+1];//}//void build(int l,int r,int ret)//{// if(l==r)// {// scanf("%d",sum[ret]);// return ;// }// int m=(l+r)/2;// build(l,m,ret<<1);// build(m+1,r,ret<<1+1);// sum[ret]=sum[ret<<1]+sum[ret<<1+1];//}void build(int l,int r,int ret){ if(l==r) { scanf("%d",&sum[ret]); return ; } int m=(l+r)>>1; build(l,m,ret<<1); build(m+1,r,(ret<<1)+1);// pushup(ret); sum[ret]=sum[ret<<1]+sum[(ret<<1)+1];}void update(int w,int e,int l,int r,int ret){ if(l==r) { sum[ret]+=e; return ; } int m=(l+r)>>1; if(w<=m) update(w,e,l,m,ret<<1); else update(w,e,m+1,r,(ret<<1)+1);// pushup(ret); sum[ret]=sum[ret<<1]+sum[(ret<<1)+1];}int query(int w,int e,int l,int r,int ret){ if(l>=w&&r<=e) { return sum[ret]; } int m=(l+r)>>1; int ret1=0; if(w<=m)ret1+=query( w, e, l, m,ret<<1); if(e>m) { ret1+=query(w,e,m+1,r,(ret<<1)+1); } return ret1;}int main(){ int n; scanf("%d",&n); int cas=1; while(n--) { printf("Case %d:\n",cas++); int q; scanf("%d",&q); memset(sum,0,sizeof(sum)); build(1,q,1); while(1) { char q2[10]; scanf("%s",q2); if(q2[0]=='E') break;// return 0; int w,e; scanf("%d%d",&w,&e); if(q2[0]=='Q') { printf("%d\n",query(w,e,1,q,1)); } if(q2[0]=='A') { update(w,e,1,q,1); } if(q2[0]=='S') { update(w,-e,1,q,1); } } } return 0;}
0 0
- 线段树(小写一发)
- HDU 3333 Turing tree 用了一发map容器加线段树
- [PKU暑课笔记] 趁机膜一发线段树和树状数组
- Trie树学习--数据结构一发
- 字典树入门爽一发
- 优先队列 使用 (补一发)
- DP(小写)
- 乱搞一发: 平面上最接近点对 (COGS 1159)
- 记录一发 逆序字符串(多组输入类型)
- linux内核API每天来一发(vfs)
- 水一发
- 抱怨一发
- 水一发
- 初识线段树(线段树总结)
- codevs1080线段树练习(线段树)
- poj2777-线段树应用(线段覆盖)
- 线段树(poj2528)
- 线段树(1)
- CListContrl得到headers的text属性
- 设计模式C++实现(18)——工厂方法模式
- android studio中使用GreenDao3.2.0
- RxBus
- CSS3—nth-child()和nth-of-type()
- 线段树(小写一发)
- 如何将confluence表格的第一行或者第一列固定
- 介绍安卓API<2>之设备兼容性
- 【NOIP模拟考一】 贪心 second 排座椅(seat.cpp)
- 10条PHP编程习惯助你找工作
- 使用POI读取excel文件
- IOS基础-NSArray、NSDictionary转JSON字符串
- [转]Memcached 与 Redis 实现的对比
- caffe 中的损失函数分析以及loss function