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] ...
随机推荐
- WPF TextBox 只能输入数字键
<Grid> <TextBox Name="textBox1" PreviewTextInput="textBox1_PreviewT ...
- 51nod 1272 思维/线段树
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 1272 最大距离 题目来源: Codility 基准时间限制:1 ...
- Disruptor_学习_00_资源帖
一.官方 disruptor-github disruptor-api LMAX Disruptor 二.精选资料 Disruptor入门-官方文档翻译-方腾飞 Disruptor官方文档实现 Dis ...
- C#文件操作常用相关类(Directory类、File类、Path类)
1.文件操作常用相关类 1)File //操作文件,静态类,对文件整体操作.拷贝.删除.剪切等 2)Directory //操作目录(文件夹),静态类 3)DirectoryInfo //文件夹的一个 ...
- 2018.7.26 学会说NO,拒绝道德绑架。
一.领导交给你一项不属于你工作范围的工作,是否需要拒绝,你可以考虑以下问题: 1.这是与我核心能力相关的工作吗?是,接受:否,进入下一条: 2.它能帮助我拓展我核心能力的边界,或是我感兴趣的吗?是,接 ...
- signal 信号具体含义解释~
) SIGHUP 本信号在用户终端连接(正常或非正常)结束时发出,通常是在终端的控 制进程结束时, 通知同一session内的各个作业,这时它们与控制终端不再关联. ) SIGINT 程序终止(int ...
- Win 7升级记
微软要抛弃它的XP了,我也应该提前把家里的PC升级成Win7,省得将来麻烦事多. 其实升级它也很简单,这全要归功于网络上的能人.我首先在网络上下载好一个操作系统DEEP_Ghost_Win7_Sp1_ ...
- CodeForces - 156C:Cipher (不错的DP)
Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But t ...
- Mybatis_generator自动化生成代码
1.Run as 2.ok
- Webpack之“多页面开发”最佳实战
前言:相信之前看过这篇文章,前端构建工具之“Webpack”的朋友,对于Webpack有了一定的了解.那么今天就跟大家分享下:如何利用webpack,来进行多页面项目实战开发. 一.项目初始化安装 1 ...