hdu 1166敌兵布阵(线段树入门题)
思路:这两天在学线段树,这个题直接手敲一下线段树就行了,都没有用上懒人标记。入门题
cin,cout会超时,记得加std::ios::sync_with_stdio(false);
#include<string>
#include<iostream> #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 using namespace std; const int maxn = 5e4 + ; int add[maxn << ];
int tree[maxn << ]; void PushDown(int rt)
{
if (add[rt]){
add[rt << ] += add[rt];
add[rt << | ] += add[rt];
tree[rt << ] += add[rt];
tree[rt << | ] += add[rt];
add[rt] = ;
}
}
void build(int l, int r, int rt)
{
if (l == r){
cin >> tree[rt];
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
tree[rt] = tree[rt << ] + tree[rt << | ];
}
void update(int x, int ans, int l, int r, int rt)
{
if (l == r){
tree[rt] += ans;
return;
}
int m = (l + r) >> ;
if (x <= m)update(x, ans, lson);
else update(x, ans, rson);
tree[rt] = tree[rt << ] + tree[rt << | ];
}
int query(int L, int R, int l, int r, int rt)
{
if (L <= l&&r <= R){
return tree[rt];
}
int m = (l + r) >> , ans = ;
if (L <= m)ans += query(L, R, lson);
if (R > m)ans += query(L, R, rson);
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
int t, ca = ;
cin >> t;
while (t--){ int n; cin >> n;
build(, n, );
string flag; int i, j;
cout << "Case " << ca++ << ":" << endl;
while (cin >> flag, flag != "End"){
cin >> i >> j;
if (flag == "Add"){
update(i, j, , n, );
}
else if (flag == "Sub"){
update(i, - * j, , n, );
}
else if (flag == "Query"){
cout << query(i, j, , n, ) << endl;
} } }
return ;
}
hdu 1166敌兵布阵(线段树入门题)的更多相关文章
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
- hdu 1166 敌兵布阵 线段树 点更新
// hdu 1166 敌兵布阵 线段树 点更新 // // 这道题裸的线段树的点更新,直接写就能够了 // // 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧, // 尽管十分简单 ...
- HDU 1166 敌兵布阵 线段树单点更新求和
题目链接 中文题,线段树入门题,单点更新求和,建一棵树就可以了. #include <iostream> #include <cstdio> #include <cmat ...
- HDU 1166 敌兵布阵(线段树单点更新,板子题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1166 敌兵布阵(线段树单点更新)
敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...
- HDU 1754 线段树 单点跟新 HDU 1166 敌兵布阵 线段树 区间求和
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 1166 敌兵布阵 <线段树 单点修改 区间查询>
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1166 敌兵布阵 (线段树、单点更新)
敌兵布阵Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- hdu 1166 敌兵布阵 线段树区间修改、查询、单点修改 板子题
题目链接:敌兵布阵 题目: C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视 ...
随机推荐
- JeePlus:代码生成器-生成示例(操作)
ylbtech-JeePlus:代码生成器-生成示例(操作) 1.返回顶部 1. 生成示例由以下部分组成 单表 主附表 树表 富文本 图片管理 自定义树组件 自定义Grid 多对多 左树右表 2. 2 ...
- CrystalQuartz实现Quartz的window服务的远程管理
1. 建一个空的ASP.NET WebSite,利用NuGet安装CrystalQuartz.Remote 包 我们可以看到,配置文件中多了如下节点: <crystalQuartz> &l ...
- springboot根据yml配置文件选择性加载bean
@Slf4j @Aspect @Component @ConditionalOnProperty(value = "localCache.apiCache", havingValu ...
- Glide和Picassio的比较
http://blog.csdn.net/fancylovejava/article/details/44747759 对象池: Glide原理的核心是为bitmap维护一个对象池.对象池的主要目的是 ...
- [笔试面试题] 3-C++关键字篇
C/C++关键字篇 语言是编程的基础,掌握基本的语言知识是编程的前提条件.关键字是组成语言的最基本单位,对关键字的理解,有助于编写高质量的代码. 1 static(静态)变量有什么作用? 在函数体 ...
- 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)
Description Given a positive integer N, your task is to calculate the sum of the positive integers l ...
- 全面学习ORACLE Scheduler特性(2)管理jobs
1.2 管理Jobs 1.2.1 启用Jobs 前面创建JOB时,由于未显式的指定ENABLED参数,因此即使指定了START_DATE,不过默认情况下JOB不会自动执行.对于这种情况,DBMS_ ...
- [转]mysql的查询、子查询及连接查询
转自:http://www.cnblogs.com/rollenholt/archive/2012/05/15/2502551.html 一.mysql查询的五种子句 where(条件 ...
- c# Queue实现生产者(Producer)消费者(Consumer)模式
我们在开发过程中经常会遇到需要从一个地方不断获取数据然后又需要交给另一个线程对数据进行二次加工的情况,这种场景适合使用生产者-消费者模式. Demo展示 //中间的容器 public static c ...
- Scala-基础-运算符
import junit.framework.TestCase /** * 运算符 */ class Demo3 extends TestCase { def test_+ { var x = 10; ...