CDOJ1324-卿学姐与公主 【线段树点更新】
http://acm.uestc.edu.cn/#/problem/show/1324
卿学姐与公主
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
某日,百无聊赖的卿学姐打开了某11区的某魔幻游戏
在这个魔幻的游戏里,生活着一个美丽的公主,但现在公主被关押在了魔王的城堡中。
英勇的卿学姐拔出利刃冲向了拯救公主的道路。
走过了荒野,翻越了高山,跨过了大洋,卿学姐来到了魔王的第一道城关。
在这个城关面前的是魔王的精锐部队,这些士兵成一字排开。
卿学姐的武器每次只能攻击一个士兵,并造成一定伤害,卿学姐想知道某时刻从L
到R
这个区间内,从开始到现在累计受伤最严重的士兵受到的伤害。
最开始每个士兵的受到的伤害都是0
Input
第一行两个整数N,Q表示总共有N个士兵编号从1到N,和Q个操作。
接下来Q行,每行三个整数,首先输入一个t,如果t是1,那么输入p,x,表示卿学姐攻击了p这个位置的士兵,并造成了x的伤害。如果t是2,那么输入L,R,表示卿学姐想知道现在[L,R]闭区间内,受伤最严重的士兵受到的伤害。
1≤N≤100000
1≤Q≤100000
1≤p≤N
1≤x≤100000
1≤L≤R≤N1≤L≤R≤
Output
对于每个询问,回答相应的值
Sample input and output
Sample Input | Sample Output |
---|---|
5 4 |
0 |
思路:线段树 点更新
代码:
#include <fstream>
#include <iostream>
using namespace std; #define MAX 100005
#define ll long long int class SegmentTree
{
private:
int n;
struct Node
{
int left, right, mid;
ll maxval;
Node (): maxval()
{}
}*tree;
public:
SegmentTree (int n): n(n)
{
tree = new Node[n<<];
}
~SegmentTree ()
{
delete []tree;
}
void Build ();
void Update (int id, int subid, int val);
ll Search (int id, int left, int right);
};
void SegmentTree::Build () //非递归,由于叶子只能出现在树的最后两层,故而可以以tree[i].left < n作为循环条件进行非递归操作。需要说明的是,这里把最后一层的多余内存也初始化了,不过不影响全局。
{
int t1;
tree[].left = ;
tree[].right = n;
tree[].mid = (n + ) >> ;
for (int i = ; tree[i].left < n; i++)
{
if (tree[i].left < tree[i].right)
{
t1 = i << ;
tree[t1].left = tree[i].left;
tree[t1].right = tree[i].mid;
tree[t1].mid = (tree[t1].left + tree[t1].right) >> ;
t1++;
tree[t1].left = tree[i].mid + ;
tree[t1].right = tree[i].right;
tree[t1].mid = (tree[t1].left + tree[t1].right) >> ;
}
}
}
void SegmentTree::Update (int id, int subid, int val)
{
if (tree[id].left == tree[id].right)
{
tree[id].maxval += val;
}
else if (tree[id].mid >= subid)
{
Update(id << , subid, val);
tree[id].maxval = max(tree[id << ].maxval, tree[id].maxval);
}
else if (tree[id].mid < subid)
{
Update((id << )|, subid, val);
tree[id].maxval = max(tree[id].maxval, tree[(id << )|].maxval);
}
}
ll SegmentTree::Search (int id, int left, int right)
{
if (tree[id].left == left && tree[id].right == right)
{
return tree[id].maxval;
}
else if (tree[id].mid >= right)
{
return Search(id << , left, right);
}
else if (tree[id].mid < left)
{
return Search((id << )|, left, right);
}
else
{
return max(Search(id << , left, tree[id].mid), Search((id << )|, tree[id].mid + , right));
}
} int main ()
{
//freopen("D:\\input.in","r",stdin);
int n, q, t, p, x;
scanf ("%d %d", &n, &q);
SegmentTree lv(n);
lv.Build();
for (int i = ; i < q; i++)
{
scanf ("%d %d %d", &t, &p, &x);
if (t == )
{
lv.Update(, p, x);
}
else
{
printf("%lld\n", lv.Search(, p, x));
}
}
return ;
}
代码2:递归
#include <fstream>
#include <iostream>
using namespace std; #define MAX 100005
#define ll long long int class SegmentTree
{
private:
int n;
struct Node
{
int left, right, mid;
ll maxval;
Node (): maxval()
{}
}*tree;
public:
SegmentTree (int n): n(n)
{
tree = new Node[n<<];
}
~SegmentTree ()
{
delete []tree;
}
void Build (int id, int left, int right);
void Update (int id, int subid, int val);
ll Search (int id, int left, int right);
};
void SegmentTree::Build (int id, int left, int right) //递归
{
tree[id].left = left;
tree[id].right = right;
tree[id].mid = (left + right) >> ;
if (tree[id].mid == right)
{
return;
}
Build(id << , left, tree[id].mid);
Build((id << )|, tree[id].mid + , right);
}
void SegmentTree::Update (int id, int subid, int val)
{
if (tree[id].left == tree[id].right)
{
tree[id].maxval += val;
}
else if (tree[id].mid >= subid)
{
Update(id << , subid, val);
tree[id].maxval = max(tree[id << ].maxval, tree[id].maxval);
}
else if (tree[id].mid < subid)
{
Update((id << )|, subid, val);
tree[id].maxval = max(tree[id].maxval, tree[(id << )|].maxval);
}
}
ll SegmentTree::Search (int id, int left, int right)
{
if (tree[id].left == left && tree[id].right == right)
{
return tree[id].maxval;
}
else if (tree[id].mid >= right)
{
return Search(id << , left, right);
}
else if (tree[id].mid < left)
{
return Search((id << )|, left, right);
}
else
{
return max(Search(id << , left, tree[id].mid), Search((id << )|, tree[id].mid + , right));
}
} int main ()
{
//freopen("D:\\input.in","r",stdin);
int n, q, t, p, x;
scanf ("%d %d", &n, &q);
SegmentTree lv(n);
lv.Build(, , n);
for (int i = ; i < q; i++)
{
scanf ("%d %d %d", &t, &p, &x);
if (t == )
{
lv.Update(, p, x);
}
else
{
printf("%lld\n", lv.Search(, p, x));
}
}
return ;
}
CDOJ1324-卿学姐与公主 【线段树点更新】的更多相关文章
- A - 卿学姐与公主(线段树+单点更新+区间极值)
A - 卿学姐与公主 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- cdoj 1324 卿学姐与公主 线段树裸题
卿学姐与公主 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- cdoj1324卿学姐与公主
地址:http://acm.uestc.edu.cn/#/problem/show/1324 卿学姐与公主 Time Limit: 2000/1000MS (Java/Others) Memo ...
- CDOJ 1324 卿学姐与公主(分块)
CDOJ 1324 卿学姐与公主(分块) 传送门: UESTC Online Judgehttp://acm.uestc.edu.cn/#/problem/show/1324 某日,百无聊赖的卿学姐打 ...
- UESTC - 1324 卿学姐与公主
题目链接 某日,百无聊赖的卿学姐打开了某11区的某魔幻游戏 在这个魔幻的游戏里,生活着一个美丽的公主,但现在公主被关押在了魔王的城堡中. 英勇的卿学姐拔出利刃冲向了拯救公主的道路. 走过了荒野,翻越了 ...
- 卿学姐与公主 UESTC - 1324 分块模板题
题意:http://acm.uestc.edu.cn/#/problem/show/1324 中文题,自己看喽. 题解:分块模板,update时顺便更新块属性.ask时先判掉belong[l]==be ...
- UESTC 1324:卿学姐与公主(分块)
http://acm.uestc.edu.cn/#/problem/show/1324 题意:…… 思路:卿学姐的学习分块例题. 分块是在线处理区间问题的类暴力算法,复杂度O(n*sqrt(n)),把 ...
- B - 卿学姐与基本法 (离散化+成段更新+区间求和)
卿学姐与基本法 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit S ...
- CDOJ 1324 卿学姐与公主 分块
题目地址 分块模板 #include<cstdio> #include<algorithm> #include<math.h> using namespace st ...
- UESTC 1324 卿学姐与公主 分块板子
#include<iostream> #include<cmath> using namespace std; ; //表示当前数在哪一块里面 int belong[maxn] ...
随机推荐
- js执行环境、作用域
js执行环境.作用域 执行环境:是javascript中的一个重要的概念,<javascript高级程序设计第三版>的定义是:执行环境定义了变量或函数有权访问的其他数据,决定了他们各自的行 ...
- 51nod 1243 二分+贪心
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1243 1243 排船的问题 题目来源: Codility 基准时间限制: ...
- count(*) 和count(1) 有区别吗
create table test1 (a varchar2(2),b varchar2(2)); insert into test1 values ('b','c'); insert into te ...
- NAVICAT PREMIUM 初识
1 问题运行SQL取数语句出错?. 答案:因为运行SQL文件时没有这个表.新的数据库里面 解决方法: 获取需要建立的二维表创建语句 新建查询 输入语句,点击运行即可 解释注释语句: 主体为五个字段的主 ...
- 07-THREE.JS 各种形状的几何图形
<!DOCTYPE html> <html> <head> <title>Example 02.04 - Geometries</title> ...
- ionic2——学习指引-学习资源汇总
Ionic2 官网............................官网的文档非常好,超级全,一定要细心看中文文档.....................比较简单 Angular 2 官网.. ...
- 畅通工程再续 (kruskal算法的延续)
个人心得:这题其实跟上一题没什么区别,自己想办法把坐标啥的都给转换为对应的图形模样就好了 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实 ...
- [BZOJ1797][AHOI2009]最小割Mincut
bzoj luogu sol 一条边出现在最小割集中的必要条件和充分条件. 先跑出任意一个最小割,然后在残余网络上跑出\(scc\). 一条边\((u,v)\)在最小割集中的必要条件:\(bel[u] ...
- Codeforces 808D. Array Division
题目大意 给定你一个长为\(n\)的序列,问能否在最多一次取出某一元素然后插入到某一点后可以将整个序列分成两段使得其两段的元素之和相同. \(n \leq 10^5\) 题解 发现插入操作实际上是让某 ...
- boost_1.61.0编译安装
1.下载源码boost_1_61_0.zip 2.进入目录 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shor ...