LightOJ 1112 Curious Robin Hood (单点更新+区间求和)
http://lightoj.com/volume_showproblem.php?problem=1112
题目大意:
1 i 将第i个数值输出,并将第i个值清0
2 i v 将第i个数值加v
3 i j 输出从i到j的数值和
简单的单点更新+区间求和,代码很简单的模板
但此题有一个神坑的地方当操作为1是输出第i个数值不是直接输出,而是通过查找输出(太坑了。。。)
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 100010
#define Lson root<<1, L, tree[root].Mid()
#define Rson root<<1|1, tree[root].Mid() + 1, R using namespace std; struct Tree
{
int L, R;
long long sum, e;
bool op;
int Mid()
{
return (R + L) / ;
}
} tree[N * ]; long long al[N]; void Build(int root, int L, int R)
{
tree[root].L = L, tree[root].R = R;
tree[root].op = false;
if(L == R)
{
tree[root].sum = al[L];
return ;
} Build(Lson);
Build(Rson); tree[root].sum = tree[root<<].sum + tree[root<<|].sum;
} void Insert(int root, int k, long long e)
{
tree[root].sum += e;
if(tree[root].L == tree[root].R)
return ;
if(k <= tree[root].Mid())
Insert(root<<, k, e);
else if(k > tree[root].Mid())
Insert(root<<|, k, e);
} long long Query(int root, int L, int R)
{
if(tree[root].L == L && tree[root].R == R)
return tree[root].sum;
if(R <= tree[root].Mid())
return Query(root<<, L, R);
else if(L > tree[root].Mid())
return Query(root<<|, L, R);
else
return Query(Lson) + Query(Rson);
} int main()
{
int m, n, a, p, b, i, t, x = ;
long long e;
char s[];
scanf("%d", &t);
while(t--)
{
x++;
scanf("%d%d", &m, &n);
for(i = ; i <= m ; i++)
scanf("%lld", &al[i]);
Build(, , m);
printf("Case %d:\n", x);
while(n--)
{
scanf("%d", &p);
if(p == )
{
scanf("%d", &a);
printf("%d\n", Query(, a + , a + ));//注意! ! ! 坑来了!!!
Insert(, a + , -Query(, a + , a + ));//此处一样
}
else if(p == )
{
scanf("%d%d", &a, &b);
Insert(, a + , b);
}
else if(p == )
{
scanf("%d%d", &a, &b);
printf("%lld\n", Query(, a + , b + ));
}
}
}
return ;
}
LightOJ 1112 Curious Robin Hood (单点更新+区间求和)的更多相关文章
- Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】
1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)
HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu1166(线段树单点更新&区间求和模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...
- poj 3321 单点更新 区间求和
Apple Tree Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java c ...
- hdu 1166 (单点更新+区间求和+裸题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...
随机推荐
- Qt之QHeaderView自定义排序(QSortFilterProxyModel)
简述 对以上节的排序,我们衍伸了两点: 把一个字符串前面的数据按照字符串比较,而后面的数据按照整形比较. 将整形显示为字符串,而排序依然正常呢. 为了分别描述,这里我们先解决问题1. 简述 效果 处理 ...
- Qt之国际化(系统文本-QMessageBox按钮、QLineEdit右键菜单等)
简介 使用Qt的时候,经常会遇到英文问题,例如:QMessageBox中的按钮.QLineEdit.QSpinBox.QScrollBar中的右键菜单等.通常情况下,我们软件都不会是纯英文的,那么如何 ...
- 用AngularJS开发下一代Web应用 系列入门基础教程
开篇介绍 AngularJS是什么东西?我觉得不用再描述了.可自行去充电一下.按照惯例,让我们先看看一个Hello World的开门简介吧. <!doctype html> <htm ...
- Using unique option prefix myisam-recover instead of myisam-recover-option
[转载]关于mysql error.log报"Using unique option prefix myisam-recover instead of myisam-recover-opti ...
- 新手学vim配置
我是新手啦,以前都没接触过Vim编辑器,所以感觉不怎么顺手,毕竟还没有用习惯.也没有什么基础,所以在配置的时候就一直在网上查资料....想要把vim编辑器配置成VS的话可以参考这个:http://ww ...
- factory工厂模式
工厂方法模式 工厂方法模式概述 工厂方法模式中抽象工厂类负责定义创建对象的接口,具体对象的创建工作由继承抽象工厂的具体类实现 简单理解: 与简单工厂模式类似,简单工厂模式是一个工厂,用户将条件为 ...
- BZOJ 1202 狡猾的商人
前缀和+带权并查集. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- Java 图片转换为字符图 CharMaps (整理)
/* * Java 图片转换成字符图 CharMaps (整理) * * 2016-1-2 深圳 南山平山村 曾剑锋 * * @(#)CharMaps.java 2014/1/16 * 1.这个一 ...
- ora-0000 normal跟/etc/hosts有关
当hosts文件配置错误时,用sqlplus登录后startup nomount,就会报错ORA-00000 [oracle11g@testdb2 dbs]$ sqlplus "/ as s ...
- 五个JS经典面试题
1:Scope作用范围 1: (function() { 2: var a = b = 5; 3: })(); 4: 5: console.log(b); 什么会被打印在控制台上? 回答 上面的代码会 ...