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] ...
随机推荐
- 倒置字符串函数reverse
倒置字符串函数reverse:用于倒置字符串s中的各个字符的位置, 如原来字符串中如果初始值为123456,则通过reverse函数可将其倒置为654321, 程序如下: #include<st ...
- MongoDB Wiredtiger存储引擎实现原理——Copy on write的方式管理修改操作,Btree cache
转自:http://www.mongoing.com/archives/2540 传统数据库引擎的数据组织方式,一般存储引擎都是采用 btree 或者 lsm tree 来实现索引,但是索引的最小单位 ...
- Disruptor_学习_00_资源帖
一.官方 disruptor-github disruptor-api LMAX Disruptor 二.精选资料 Disruptor入门-官方文档翻译-方腾飞 Disruptor官方文档实现 Dis ...
- Django ImageField 上传图片并保存到数据库
转自:http://logic0.blog.163.com/blog/static/188928146201371235435974/ Form代码: class ImageUploadForm(fo ...
- LeetCode Max Consecutive Ones II
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...
- 推荐几本WinCE 6程序开发的书
因为学校期中考试和课程设计的原因,winCE6的项目开发耽误了一个多月的时间,现在学校没什么事情了,公司这边杂事也差不多办完了,可以专心的搞开发了,同时这也成了我的毕业设计,我不得不上心喽. 今天在卓 ...
- BZOJ3289:Mato的文件管理
浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- BZOJ3403:[USACO2009OPEN]Cow Line
浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- Azure VMSS ---- PowerShell创建自定义镜像的VMSS集群
前面一篇文章介绍了如何用PowerShell创建标准镜像的VMSS集群.http://www.cnblogs.com/hengwei/p/7391178.html 本文将介绍,如何用PowerShel ...
- Win8 安装.Net Framework3.5(2.0,3.0)组件二种方式
第一种: 通过命令+win8映像文件 找到系统盘cmd文件:C:\WINDOWS\system32\Cmd.exe 右键“以管理员身份运行”,然后弹出一个黑框框. 黑框框里面输入一下命令: dism. ...