hdu 5316 Magician 线段树
链接:http://acm.hdu.edu.cn/showproblem.php?
pid=5316
Magician
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 190 Accepted Submission(s): 54
as having a special gift which sets them apart from the vast majority of characters in fantasy worlds who are unable to learn magic.
Magicians, sorcerers, wizards, magi, and practitioners of magic by other titles have appeared in myths, folktales, and literature throughout recorded history, with fantasy works drawing from this background.
In medieval chivalric romance, the wizard often appears as a wise old man and acts as a mentor, with Merlin from the King Arthur stories representing a prime example. Other magicians can appear as villains, hostile to the hero.

Mr. Zstu is a magician, he has many elves like dobby, each of which has a magic power (maybe negative). One day, Mr. Zstu want to test his ability of doing some magic. He made the elves stand in a straight line, from position 1 to position n, and he used two
kinds of magic, Change magic and Query Magic, the first is to change an elf’s power, the second is get the maximum sum of beautiful subsequence of a given interval. A beautiful subsequence is a subsequence that all the adjacent pairs of elves in the sequence
have a different parity of position. Can you do the same thing as Mr. Zstu ?
Each of the test case begins with two integers n, m represent the number of elves and the number of time that Mr. Zstu used his magic.
(n,m <= 100000)
The next line has n integers represent elves’ magic power, magic power is between -1000000000 and 1000000000.
Followed m lines, each line has three integers like
type a b describe a magic.
If type equals 0, you should output the maximum sum of beautiful subsequence of interval [a,b].(1 <= a <= b <= n)
If type equals 1, you should change the magic power of the elf at position a to b.(1 <= a <= n, 1 <= b <= 1e9)
1
1 1
1
0 1 1
1
题意:有n个数。两个操作,0操作,输出l到r 。全部奇偶交替 的子序列中。值的最大和。
(自序列是能够不用连续的)。 1操作是把a位置的数改成b。
做法:维护区间内的
jiou, 这个区间以奇数位開始,偶数位结束的 全部子序列中的最大和。
ouji,jiji。ouou 三个数同理。
#include <cstdio>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std; #define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define LL __int64
#define inf -100000000000000000 const int maxn = 111111;
struct point
{
__int64 jiji,ouou,jiou,ouji;
}; point pp[maxn<<2];
int jiou[maxn<<2]; point big(point a,point b)
{
point c;
c.jiji=max(a.jiji,b.jiji);
c.jiji=max(c.jiji,a.jiji+b.ouji);
c.jiji=max(c.jiji,a.jiou+b.jiji);
c.jiji=max(c.jiji,inf); c.jiou=max(a.jiou,b.jiou);
c.jiou=max(c.jiou,a.jiji+b.ouou);
c.jiou=max(c.jiou,a.jiou+b.jiou);
c.jiou=max(c.jiou,inf); c.ouji=max(a.ouji,b.ouji);
c.ouji=max(c.ouji,a.ouji+b.ouji);
c.ouji=max(c.ouji,a.ouou+b.jiji);
c.ouji=max(c.ouji,inf); c.ouou=max(a.ouou,b.ouou);
c.ouou=max(c.ouou,a.ouji+b.ouou);
c.ouou=max(c.ouou,a.ouou+b.jiou);
c.ouou=max(c.ouou,inf);
return c;
} void PushUp(int rt) {
pp[rt]=big(pp[rt<<1],pp[rt<<1|1]);
} int kk=1;
void build(int l,int r,int rt) {
if (l == r) {
if(kk&1)
{
jiou[rt]=1;
scanf("%I64d",&pp[rt].jiji);
pp[rt].ouou=inf;
pp[rt].ouji=inf;
pp[rt].jiou=inf;
}
else
{
jiou[rt]=0;
scanf("%I64d",&pp[rt].ouou);
pp[rt].jiou=pp[rt].jiji=pp[rt].ouji=inf;
}
kk++;
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int c,int l,int r,int rt) {
if (l==r&&l==L) {
if(jiou[rt]==1)
pp[rt].jiji=c;
if(jiou[rt]==0)
pp[rt].ouou=c;
return ;
}
int m = (l + r) >> 1;
if (L <= m) update(L , c , lson);
else
update(L , c , rson);
PushUp(rt);
}
point query(int L,int R,int l,int r,int rt) {
if (L <= l && r <= R) {
return pp[rt];
} int m = (l + r) >> 1; if(L<=m&&m<R) return big( query(L , R , lson),query(L , R , rson));
if(L<=m) return query(L , R , lson);
if(m<R) return query(L , R , rson); }
int main() {
int t;
int n,q;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
kk=1;
if(n)
build(1 , n , 1);
while(q--)
{
int op;
int a,b;
scanf("%d",&op);
scanf("%d%d",&a,&b);
if(n==0)
{
if(op==0)
puts("0");
continue;
} if(op==0)
{
if(a>b)
swap(a,b);
point ppp=query(a,b,1,n,1);
printf("%I64d\n",max(ppp.jiji,max(ppp.jiou,max(ppp.ouji,ppp.ouou))));
}
else
update(a , b , 1 , n , 1);
}
}
return 0;
}
hdu 5316 Magician 线段树的更多相关文章
- hdu 5316 Magician 线段树维护最大值
题目链接:Magician 题意: 给你一个长度为n的序列v,你需要对这个序列进行m次操作,操作一共有两种,输入格式为 type a b 1.如果type==0,你就需要输出[a,b]区间内的美丽序列 ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 5316——Magician——————【线段树区间合并区间最值】
Magician Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 5316 Magician (线段树,单值修改,微变形)
题意:给一个初始序列A[1,n],第j个数字代表精灵j的power值,有两种操作:(1)查询区间[L,R] (2)修改某个精灵的power值. 但,查询的是区间[L,R]中一个美丽子序列sub[l,r ...
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- HDU 3308 LCIS (线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...
- HDU 2795 Billboard (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告; 然后给n个1*wi的广告,要求把广告贴 ...
随机推荐
- oracle列转行 WM_CONCAT LISTAGG
开发给个SQL说给某个条件时报ORA-22922 代码段: SELECT 袋号, SUM(实际重量) AS 实际重量, SUM(材积重量) AS 材积重量, COUNT(运单号) AS 件数, TO_ ...
- 性能学习之六---socket接口测试
socket协议较底层,所以是一个万能协议.socket发的是数据包,所以较难看懂. 下面我们来讲解socket接口测试. 大致思路为:新建sever端和client端---建立连接---发送数据 一 ...
- [LOJ#515]「LibreOJ β Round #2」贪心只能过样例
[LOJ#515]「LibreOJ β Round #2」贪心只能过样例 试题描述 一共有 \(n\) 个数,第 \(i\) 个数 \(x_i\) 可以取 \([a_i , b_i]\) 中任意值. ...
- hdu 1558 线段相交+并查集路径压缩
Segment set Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU1936 [贪心+KMP] 点的区间覆盖
每一行对话分别取匹配所有的表情 这样是一个n**2的匹配,可以用KMP 找出每行对话中的每个表情的左右端点 这样相当于就是问用最少多少个点 可以覆盖所有的区间(每个区间中放一个点表示覆盖) 贪心 按右 ...
- APUE 学习笔记(三) 文件和目录
1. 文件类型 文件类型信息包含在 struct stat 里的 st_mode 成员 (1)普通文件,unix内核并不区分文本文件和二进制文件 (2)目录文件,这种文件包含了其他文件的名字以及指向这 ...
- 学习javascript设计模式之单例模式
1.单例模式的核心是确保只有一个实例,并提供全局访问. 2.惰性单例 指的是在需要的时候才创建对象实例. 如在页面中创建唯一div 普通做法 var createDiv = (function(){ ...
- iframe平铺到浏览器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Servlet 2.4 规范之第六篇:响应
响应对象封装了服务端返回给客户端的所有信息.在HTTP协议中,这些信息通过HTTP头和消息体传送. SRV.5.1 缓冲 出于效率考量,servlet容器可以缓冲输出数据,但这并非强制要求.常见 ...
- (41)C#异步编程
VS2010是经常阻塞UI线程的应用程序之一.例如用vs2010打开一个包含数百个项目的解决方案,可以要等待很长时间(感觉像卡死),自从vs2012情况得到了改善,项目在后台进行了异步加载. 一.同步 ...