hdu1166 敌兵布阵,单点修改,区间求和。

【ATTENTION】MAXN要开成节点数的4倍,开得不够会提示TLE。

 #include<iostream>
#include<cstdio>
#include<cstring>
#define lson l,m,root<<1
#define rson m+1,r,root<<1|1
using namespace std;
const int MAXN=*+;
int n;
int sum[MAXN]; void pushUP(int root)
{
sum[root]=sum[root<<]+sum[root<<|];
} void build(int l,int r,int root)
{
if (l==r)
{
scanf("%d",&sum[root]);
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushUP(root);
} void update(int p,int delta,int l,int r,int root)
{
if (l==r)
{
sum[root]+=delta;
return;
}
int m=(l+r)>>;
if (p<=m) update(p,delta,lson);
if (p>m) update(p,delta,rson);
pushUP(root);
} int query(int L,int R,int l,int r,int root)
{
int result=;
if (l>=L && r<=R)
{
return sum[root];
}
int m=(l+r)>>;
if (L<=m) result+=query(L,R,lson);
if (R>m) result+=query(L,R,rson);
return result;
} int main()
{
int t;
scanf("%d",&t);
for (int kase=;kase<t;kase++)
{
cout<<"Case "<<kase+<<":"<<endl;
scanf("%d",&n);
build(,n,);
char s[];
while (scanf("%s",s))
{
if (s[]=='E') break;
int a,b;
scanf("%d%d",&a,&b);
if (s[]=='A') update(a,b,,n,);
if (s[]=='S') update(a,-b,,n,);
if (s[]=='Q') cout<<query(a,b,,n,)<<endl;
}
}
return ;
}

【线段树(单点修改,区间求和)】HDU1166 - 敌军布阵的更多相关文章

  1. Lightoj 1348 Aladdin and the Return Journey (树链剖分)(线段树单点修改区间求和)

    Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't wa ...

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

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

  3. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  4. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  5. hdu 1166线段树 单点更新 区间求和

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

  6. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

  7. hdu1394(枚举/树状数组/线段树单点更新&区间求和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...

  8. I Hate It HDU - 1754 线段树 单点修改+区间最值

    #include<iostream> #include<cstring> using namespace std; ; int m,n,p; struct node{ int ...

  9. HDU 1754 I Hate It(线段树单点替换+区间最值)

    I Hate It [题目链接]I Hate It [题目类型]线段树单点替换+区间最值 &题意: 本题目包含多组测试,请处理到文件结束. 在每个测试的第一行,有两个正整数 N 和 M ( 0 ...

  10. hdu1754(线段树单点替换&区间最值模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:中文题诶- 思路:线段树单点替换&区间最大值查询模板 代码: #include & ...

随机推荐

  1. ie8下a标签中的图片出现边框

    1.ie8下a标签中的图片出现边框 <a href="#"><img src="horse.jpg"></a> 效果如图所示 ...

  2. 报错注入遇到ERROR 1242 (21000): Subquery returns more than 1 row解决方案

    我的SQL语句是这样写的. mysql> select 1,2,3 and updatexml(1,concat(1,(select user from mysql.user),1),1);ER ...

  3. Linux-进程间通信(六): 记录锁

    1. 记录锁:记录锁的功能是,当一个进程正在读或者修改文件的某个部分的时候,它可以阻止其他进程修改同一文件区: 2. fcntl记录锁: #include <fcntl.h> int fc ...

  4. c语言简单实现telnet客户端

    c语言简单实现telnet客户端 http://blog.csdn.net/haiwenchen/article/details/69944118

  5. OC 07 类的扩展

    1.NSDate的使用 NSDate是Cocoa中⽤于处理⽇期和时间的基础类,封装了某⼀给定的时刻(含日期,时间,时区) 注意NSLog(@“%@”,nowDate);⽆论你是哪个时区的时间,打印时总 ...

  6. Java之CyclicBarrier使用

    http://blog.csdn.net/shihuacai/article/details/8856407 1.类说明: 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (commo ...

  7. WordPress主题设置插件,让你的站点有电脑、手机双主题

    我们建站的时候总是会优先考虑自适应的主题,但是与之对应,免费的自适应主题都调用国外公共资源,访问速度不太理想.加上wordpress未经优化之前,本身也没有极高的访问效率.所以大家可以下载这款“主题设 ...

  8. Go语言入门之变量声明

    1.使用var关键字声明变量,如果没有初始化,则变量默认为零值. var a string "hello world" 2.根据值自行判定变量类型 3.多变量声明 ,, 4.使用v ...

  9. Guid is not updated for cluster with specified cluster id; need to wait for hosts in this cluster to come up

    http://mail-archives.apache.org/mod_mbox/cloudstack-users/201306.mbox/%3c201306181058330006472@gmail ...

  10. initerrlog: 无法打开错误日志文件 'D:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Log 解决办法

    initerrlog: 无法打开错误日志文件 'D:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Log 1. 设置 ...