2015 多校联赛 ——HDU5316(线段树)
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
0 1 1
// 以前没专门学习过线段树,所有是参考别人代码写出了的(表示最初并没有看懂题意 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(线段树)的更多相关文章
- 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, ...
- 2015 多校联赛 ——HDU5299(树删边)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- 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 ...
- HDU 4614 Vases and Flowers (2013多校第二场线段树)
题意摘自:http://blog.csdn.net/kdqzzxxcc/article/details/9474169 ORZZ 题意:给你N个花瓶,编号是0 到 N - 1 ,初始状态花瓶是空的,每 ...
- HDU6602 Longest Subarray hdu多校第二场 线段树
HDU6602 Longest Subarray 线段树 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6602 题意: 给你一段区间,让你求最长的区间使 ...
- 2015 多校联赛 ——HDU5325(DFS)
Crazy Bobo Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Tota ...
- 2015 多校联赛 ——HDU5303(贪心)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- 2015 多校联赛 ——HDU5334(构造)
Virtual Participation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 2015 多校联赛 ——HDU5302(构造)
Connect the Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
随机推荐
- 项目Beta冲刺Day7
项目进展 李明皇 今天解决的进度 部分数据传递和使用逻辑测试 林翔 今天解决的进度 服务器端查看个人发布的action,修改已发布消息状态的action,仍在尝试使用第三方云存储功能保存图片 孙敏铭 ...
- 算法第四版学习笔记之优先队列--Priority Queues
软件:DrJava 参考书:算法(第四版) 章节:2.4优先队列(以下截图是算法配套视频所讲内容截图) 1:API 与初级实现 2:堆得定义 3:堆排序 4:事件驱动的仿真 优先队列最重要的操作就是删 ...
- 利用java反射读写csv中的数据
前一段有个需求需要将从数据库读取到的信息保存到csv文件中,在实现该需求的时候发现资料比较少,经过收集反射和csv相关资料,最终得到了如下程序. 1.在使用java反射读取csv文件数据时,先通 ...
- ExtJs6级联combo的实现
父类获取子类进行操作 { xtype: 'combo', store: Common.Dic.getDicData("IMAGE_BIG_TYPE") , multiSelect: ...
- JAVA_SE基础——26.[深入解析]局部变量与成员变量的区别
黑马程序员入学blog ... 如果这章节很难懂的话应该返回去先看 JAVA_SE基础--10.变量的作用域 定义的位置上区别: 1. 成员变量是定义在方法之外,类之内的. 2. 局部变量是定义在方 ...
- JAVA_SE基础——2.环境变量的配置&测试JDK
哈喽,利用晚上的空余时间再写篇心的~~~ 谢谢大家 前一篇文章 JAVA_SE基础--JDK&JRE下载及安装http://blog.csdn.net/thescript_j/article ...
- 遍历JSON
第一种: each,不做详细说明,太常用了 第二种:我用来遍历单个组,实现前端界面绑定 for(var item in person){ alert("person中"+item+ ...
- C# 使用 GDI+ 给图片添加文字,并使文字自适应矩形区域
需求 需求是要做一个编辑文字的页面.用户在网页端写文字,文字区域是个矩形框,用户可以通过下方的拖动条调节文字大小. 如下图: 提交数据的时候前端传文字区域的左上角和右下角定位给后台.因为前端的字体大小 ...
- Mego(07) - 关系配置
这个是本框架的重要功能,该关系就是指对象中的复杂对象或集合属性,该关系与EF中的关系是有区别的.EF中强调关系的成对出现,这是由于数据库关系的思想决定的.然而Mego更接近与对象化逻辑,我们只关心当前 ...
- 第5章 子网划分和CIDR
第5章 子网划分和CIDR 划分网络 根据A类.B类或C类网络ID来识别网段具有一些局限性,主要是在网络级别之下不能对地址空间进行任何逻辑细分 如果一个IP是一个A类网络.数据报到达网关,然后传输到9 ...