题目链接

/*
单点更新,用RMQ维护最大值,add对c[i]修改,或加,或减。
求[l,r]的和,用sum(r)-sum(l-1).即可。
*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn =500005;
int a[maxn];
int c[maxn];
char s[10];
int n,t;
void init()
{
memset(c,0,sizeof(c));
}
int lowbit(int x)
{
return x&(-x);
}
void add (int i,int v)
{
while(i<=n)
{
c[i]+=v;
i+=lowbit(i);
}
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
int main ()
{
int T;scanf("%d",&T);
int k=0;
while(T--)
{
init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
add(i,t);
}
int l,r;
printf("Case %d:\n",++k);
while(true)
{
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d%d",&l,&r);
printf("%d\n",sum(r)-sum(l-1));
}
else if(s[0]=='S')
{
scanf("%d%d",&l,&r);
add(l,-r);
}
else if(s[0]=='A')
{
scanf("%d%d",&l,&r);
add(l,r);
}
else
break;
}
}
return 0;
}
/*
线段树的话就不说了,线段树处理单点更新的问题很好处理。
*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn =500005;
struct node
{
int l;
int r;
int sum;
};
node f[maxn*3];
int a[maxn];
int n,T,k=0;
char s[10];
void build(int root,int l,int r)
{
f[root].l=l;
f[root].r=r;
if(l==r)
f[root].sum=a[l];
else
{
int mid=(l+r)>>1;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
f[root].sum=(f[root<<1].sum+f[root<<1|1].sum);
}
}
int query(int root,int l,int r)
{
if(f[root].l==l&&f[root].r==r)
return f[root].sum;
int mid=(f[root].l+f[root].r)>>1;
if(r<=mid)
return query(root<<1, l, r);
else if(l>=mid+1)
return query(root<<1|1, l, r);
else
return query(root<<1, l, mid)+query(root<<1|1, mid+1, r);
}
void update(int root,int r,int v)
{
if(f[root].r==f[root].l&&f[root].r==r)
{
f[root].sum+=v;
return ;
}
f[root].sum+=v;
int mid=(f[root].l+f[root].r)>>1;
if(r<=mid)
update(root<<1, r, v);
else
update(root<<1|1, r, v);
}
int main ()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,1,n);
int l,r;
printf("Case %d:\n",++k);
while(true)
{
scanf("%s",s);
if(s[0]=='E')
break;
else
{
scanf("%d%d",&l,&r);
if(s[0]=='Q')
printf("%d\n",query(1, l, r));
else if(s[0]=='A')
update(1, l, r);
else if(s[0]=='S')
update(1, l, -r);
}
}
}
return 0;
}

VJ16216/RMQ/线段树单点更新的更多相关文章

  1. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  2. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  3. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  4. HDU 1166 敌兵布阵(线段树单点更新,板子题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  6. HDU 1166 敌兵布阵(线段树单点更新,区间查询)

    描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

  7. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  8. HDUOJ----1166敌兵布阵(线段树单点更新)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

随机推荐

  1. vs2012常用快捷键总结

    ctrl+k+c  注释 ctrl+k+u 取消注释 ctrl+m+t 折叠代码 ctrl+t 取消折叠 ctrl+k+d 调节代码 ctrl+f 查找 ctrl+g  跳转行(一般调试的时候显示哪行 ...

  2. Union、Union All、Except、InterSect的区别

    UNION: 将多个「结果集 (result set)」的「行 (row)」合并,作为单个结果集返回,并移除重复的行.若有重复的行,只留下一个. UNION ALL: 将多个「结果集 (result ...

  3. 面试中有关C++的若干问题

    面试中有关C++的若干问题 By 晴天, 2014.5.16晚 什么是多态?简要说一下C++中的多态的概念. (1)定义:多态是指相同对象收到不同消息或者不同对象收到相同消息产生不同的行为. (2)C ...

  4. 利用yield关键字输出杨辉三角

    最近学习了下python,发现里面也有yield的用法,本来对C#里的yield不甚了解,但是通过学习python,对于C#的yield理解更深了!! 不多说了,小学生水平的表达能力伤不起.... 直 ...

  5. Chapter 16_0 面向对象编程

    Lua中的table就是一种对象. 1.table和对象一样拥有状态 2.和对象一样有一个独立的标识符(a self) 3.和对象一样,具有独立于创建者和创建地的生命周期. 对象有他们自己的操作,ta ...

  6. ControlTemple样式

    1.TextBox 样式 1.1 style <Window.Resources> <Style x:Key="aa" TargetType="{x:T ...

  7. 个人项目中的WCF使用

    今天闲着无事,给大家分享一下我的一个项目中WCF的使用.我这项目使用的是Silverlight,至于其他类型的使用方法也是一样的. 1.建立一个Silverlight带Web项目的解决方案. 2.在w ...

  8. java URL编程

    一.URL编程技术 URL是统一资源定位器的缩写,它是指向互联网“资源”的指针.URL表示了Internet上某个资源的地址.URL支持http,file,ftp等 多种协议.通过URL标识,可以直接 ...

  9. php+ajax的三级联动下拉菜单

    封装一个三级联动,就可以在任何页面进行引用了 先写个页面引用一下这个js <head> <meta http-equiv="Content-Type" conte ...

  10. HDU 1969 Pie

    二分答案+验证(这题精度卡的比较死) #include<stdio.h> #include<math.h> #define eps 1e-7 ; double a[ff]; d ...