Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an innate talent, gaining it through study and practice, or receiving it from another being, often a god, spirit, or demon of some sort. Some wizards are depicted 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 ?

 
Input
The first line is an integer T represent the number of test cases.
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)
 
Output
For each 0 type query, output the corresponding answer.
 
Sample Input
1
1 1
1
0 1 1
 
Sample Output
1
 
Source

// 以前没专门学习过线段树,所有是参考别人代码写出了的(表示最初并没有看懂题意 OoO!)

题意:有n个数,两个操作,0操作,输出l到r ,所有奇偶交替的子序列中,值的最大和。 1操作是把a位置的数改成b。

用oo代表偶始偶终:oo可以由oj  oo合成,oo jo 合成。其他与此类似

#include <iostream>
#include <cstdio>
#include <cstring>
typedef long long ll;
using namespace std;
const int n= 100010;
const ll INF = 1000000000000000000; struct pnode
{
int l,r;
ll jj,jo,oj,oo;
} node[n<<2];
ll a[n]; ll max(ll x, ll y)
{
if(x < y)
return y;
return x;
} void work(int i)
{
int lc = i << 1;
int rc = lc | 1; node[i].jj = max(node[lc].jj,node[rc].jj);
node[i].jj = max(node[i].jj,node[lc].jj+node[rc].oj);
node[i].jj = max(node[i].jj,node[lc].jo+node[rc].jj); node[i].oj = max(node[lc].oj,node[rc].oj);
node[i].oj = max(node[i].oj,node[lc].oo+node[rc].jj);
node[i].oj = max(node[i].oj,node[lc].oj+node[rc].oj); node[i].jo = max(node[lc].jo,node[rc].jo);
node[i].jo = max(node[i].jo,node[lc].jj+node[rc].oo);
node[i].jo = max(node[i].jo,node[lc].jo+node[rc].jo); node[i].oo = max(node[lc].oo,node[rc].oo);
node[i].oo = max(node[i].oo,node[lc].oj+node[rc].oo);
node[i].oo = max(node[i].oo,node[lc].oo+node[rc].jo);
}
void build(int i,int l,int r)
{
node[i].l = l;
node[i].r = r; if(l == r)
{
if(l % 2)
{
node[i].jj = a[l];
node[i].jo = node[i].oj = node[i].oo = -INF;
}
else
{
node[i].oo = a[l];
node[i].jo = node[i].oj = node[i].jj = -INF;
}
return;
} build(i << 1,l ,(l+r)/2);
build(i << 1 | 1, (l+r)/2 + 1,r); work(i);
} void update(int i,int pos,int val)
{
if(node[i].l == pos && node[i].r == pos)
{
if(pos % 2)
{
node[i].jj = val;
}
else
{
node[i].oo = val;
}
return;
} int mid = (node[i].l + node[i].r)/2;
if(pos <= mid)
update(i << 1,pos, val);
else
update(i << 1 | 1,pos, val); work(i);
} pnode query(int i,int l,int r)
{
if(node[i].l == l && node[i].r == r) return node[i]; int lc = i << 1;
int rc = lc+1;
int mid = ( node[i].l + node[i].r )/2; if(r <= mid )
return query(lc,l,r);
else if(l > mid)
return query(rc,l,r);
else
{
pnode ln = query( lc, l, mid ), rn = query( rc, mid + 1, r ), res;
res.jj = max( ln.jj, rn.jj );
res.jj = max( res.jj, ln.jj + rn.oj );
res.jj = max( res.jj, ln.jo + rn.jj );
res.jo = max( ln.jo, rn.jo );
res.jo = max( res.jo, ln.jj + rn.oo );
res.jo = max( res.jo, ln.jo + rn.jo );
res.oj = max( ln.oj, rn.oj );
res.oj = max( res.oj, ln.oj + rn.oj );
res.oj = max( res.oj, ln.oo + rn.jj );
res.oo = max( ln.oo, rn.oo );
res.oo = max( res.oo, ln.oo + rn.jo );
res.oo = max( res.oo, ln.oj + rn.oo );
return res;
}
} int main ()
{ int t,m,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&k); for(int i = 1; i <= m; i++)
scanf("%I64d",&a[i]); build(1,1,m); while(k--)
{
int op;
scanf("%d", &op);
if ( op == 0 ) {
int l, r;
scanf("%d%d", &l, &r);
pnode nn = query( 1, l, r );
ll ans = nn.jj;
ans = max( ans, nn.jo );
ans = max( ans, nn.oj );
ans = max( ans, nn.oo );
printf("%I64d\n", ans); }
else if ( op == 1 ) {
int pos, val;
scanf("%d%d", &pos, &val);
update( 1, pos, val );
}
}
}
return 0;
}

  

2015 多校联赛 ——HDU5316(线段树)的更多相关文章

  1. 2015 多校联赛 ——HDU5372(树状数组)

    Sample Input 3 0 0 0 3 0 1 5 0 1 0 0 1 1 0 1 0 0   Sample Output Case #1: 0 0 0 Case #2: 0 1 0 2 有0, ...

  2. 2015 多校联赛 ——HDU5299(树删边)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  3. HDU 4614 Vases and Flowers (2013多校2 1004 线段树)

    Vases and Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others ...

  4. HDU 4614 Vases and Flowers (2013多校第二场线段树)

    题意摘自:http://blog.csdn.net/kdqzzxxcc/article/details/9474169 ORZZ 题意:给你N个花瓶,编号是0 到 N - 1 ,初始状态花瓶是空的,每 ...

  5. HDU6602 Longest Subarray hdu多校第二场 线段树

    HDU6602 Longest Subarray 线段树 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6602 题意: 给你一段区间,让你求最长的区间使 ...

  6. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  7. 2015 多校联赛 ——HDU5303(贪心)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  8. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  9. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

随机推荐

  1. 201621123040《Java程序设计》第3周学习总结

    1.本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词,如类.对象.封装等 面向对象的思想 对象 类 1.2 用思维导图或者Onenote或其他工具将这些关键词组织起来. 掌握的还不够深 ...

  2. Linux下ftp和ssh详解

    学习了几天Linux下ftp和ssh的搭建和使用,故记录一下.学习ftp和ssh的主要目的是为了连接远程主机,并且进行文件传输.废话不多说,直接开讲! ftp服务器 1. 环境搭建 本人的系统是Arc ...

  3. Python 3.* print 出现SyntaxError: invalid syntax

    很简单,不知道为啥,据说是3.0以后的print都改为了print(); 例如 a=1 print a 上边出错 输入 a=1 print(a) 就正确了

  4. mysql命令行大全

      1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  5. 在360、UC等浏览器,img不加载原因

    问题:图片在360浏览器不被加载,在UC浏览器强制不显示. 前言不多说,直接上图. 360浏览器显示情况: UC浏览器显示情况: 由以上两张截图可以看到,在360浏览器,banner图片处根本没有加载 ...

  6. VMware虚拟机误删除vmdk文件后如何恢复?

    故障描述: Dell R710系列服务器(用于VMware虚拟主机),Dell MD 3200系列存储(用于存放虚拟机文件),VMware ESXi 5.5版本,因意外断电,导致某台虚拟机不能正常启动 ...

  7. GitChat招募IT类写作作者

    GitChat是一个移动端的IT知识.技术分享平台,于2017.10和CSDN合并,成为其旗下独立品牌. 我们正在寻求有互联网基因的人来一起分享IT人员的关切,诚挚邀请您来做一次分享(让IT类文章变现 ...

  8. STM32常见问题

    一.STM32 下不了程序 提示: (1).JLink Info: CPU halted Erase Done. Programming Failed! 解决办法: 用isp把芯片全部擦除再试试,如果 ...

  9. Linq 生成运算符 Empty,Range,Repeat

    var c1 = Enumerable.Empty<string>();//c1.Count=0 , );//{9527,9528,9529,......9536} , );//{9527 ...

  10. 4-51单片机WIFI学习(开发板51单片机自动冷启动下载原理)

    上一篇链接 http://www.cnblogs.com/yangfengwu/p/8743936.html 这一篇说一下自己板子的51单片机自动冷启动下载原理,我挥舞着键盘和鼠标,发誓要把世界写个明 ...