hdu 1166(线段树或者树状数组)
来源:互联网 发布:js中class选择器 编辑:程序博客网 时间:2024/06/07 08:05
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1166
题意:
给出营地的总数和各个营地初始人数,通过对某个营地 add(增加人数)或sub(减少人数)操作
求第a个——第b个营地范围内的人数总和
可以采用线段树也可以是树状数组
树状数组:
#include <iostream>#include "stdio.h"#include "stdlib.h"#define maxn 50005using namespace std;int c[maxn]={0};int lowbit(int x){return x&(-x);}void update(int target,int change,int n){ while(target<=n) { c[target]+=change; target+=lowbit(target); }}int sum(int x){ int ans=0; while(x>0) { ans+=c[x]; x-=lowbit(x); } return ans;}int main(){ int t,deap=0; scanf("%d",&t); while(t--) { int n; memset(c,0,sizeof(c)); char temp[10]; scanf("%d",&n); for(int i=1;i<=n;i++) { int tmp; scanf("%d",&tmp); update(i,tmp,n); } printf("Case %d:\n",++deap); while(scanf("%s",temp)!=EOF) { int a,b; if(temp[0]=='E') break; else if(temp[0]=='Q') { scanf("%d%d",&a,&b); printf("%d\n",sum(b)-sum(a-1)); } else{ scanf("%d%d",&a,&b); if(temp[0]=='S') b=b*(-1); update(a,b,n); } } } return 0;}
线段树:
#include <iostream>#include "stdio.h"#include "stdlib.h"#define maxn 50005using namespace std;int sum[maxn<<2]={0};void build(int k,int l,int r){ if(l==r) { sum[k]=0; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r);}void update(int k,int left,int right,int pos,int val){ if(left==right) { sum[k]+=val; return; } int mid=(left+right)>>1; if(pos<=mid) update(k<<1,left,mid,pos,val); else update(k<<1|1,mid+1,right,pos,val); sum[k]=sum[k<<1|1]+sum[k<<1];}int query(int k,int left,int right,int ll,int rr){ if(ll<=left&&rr>=right) return sum[k]; int mid=(left+right)>>1; int sum1=0,sum2=0; if(ll<=mid) sum1=query(k<<1,left,mid,ll,rr); if(rr>mid) sum2=query(k<<1|1,mid+1,right,ll,rr); return sum1+sum2;}int main(){ int t,ans=0; scanf("%d",&t); while(t--) { int n; char temp[10]; scanf("%d",&n); build(1,1,n); for(int i=1;i<=n;i++) { int tmp; scanf("%d",&tmp); update(1,1,n,i,tmp); } printf("Case %d:\n",++ans); while(1) { scanf("%s",temp); if(temp[0]=='E') break; else if(temp[0]=='Q') { int a,b; scanf("%d%d",&a,&b); printf("%d\n",query(1,1,n,a,b)); } else{ int a,b; scanf("%d%d",&a,&b); if(temp[0]=='S') b=(-1)*b; update (1,1,n,a,b); } } } return 0;}
0 0
- hdu 1166(线段树或者树状数组)
- hdu 1166敌兵布阵(线段树或者树状数组)
- hdu 3333 Turing Tree 树状数组或者线段树
- HDU ACM 4046 Panda 线段树或者树状数组
- hdu 1166 线段树/树状数组
- hdu 1166 树状数组 & 线段树
- hdu 1166 树状数组 线段树
- hdu 1166_线段树&树状数组
- HDU 1166(线段树;树状数组)
- hdu 1166 树状数组 线段树入门
- hdu 1166 线段树,树状数组模板
- hdu 1166 树状数组 线段树
- HDU 1166 树状数组和线段树
- HDU 1164-排兵布阵典型的线段树单点查询或者一位树状数组
- [hdu]1166敌兵布阵-线段树&树状数组
- HDU 1166 敌兵布阵(树状数组,线段树)
- HDU 1166 敌兵布阵 线段树/树状数组入门题
- hdu 1166 敌兵布阵 线段树&树状数组
- 查找第一个只出现一次的字符
- Eclipse中用git解决冲突 避免每次重新拉代码
- apache 将html解析为php
- javascript object
- 独立显卡、核心显卡和集成显卡的区别
- hdu 1166(线段树或者树状数组)
- 归档与解归档
- Linux下Redis服务器安装配置
- Spring JdbcTemplate方法详解
- iOS UITableView表视图滚动隐藏UINavigationController导航栏
- 高级字符串用法
- 前端基础知识整理(四)
- 常用前端代码归纳
- BigDecimal类的加减乘除(解决double计算精度问题)