热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

2018HDU多校联合第五场G题:GladYouGame(线段树优化解法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6356在《GladYouGame》中,Steve面临一个复杂的区间操作问题。该题可以通过线段树进行高效优化。具体来说,线段树能够快速处理区间更新和查询操作,从而大大提高了算法的效率。本文详细介绍了线段树的构建和维护方法,并给出了具体的代码实现,帮助读者更好地理解和应用这一数据结构。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6356            
Glad You Game 
Steve has an integer array aa of length nn (1-based). He assigned all the elements as zero at the beginning. After that, he made mm operations, each of which is to update an interval of aa with some value. You need to figure out ni=1(iai)⨁i=1n(i⋅ai) after all his operations are finished, where ⨁ means the bitwise exclusive-OR operator. 
In order to avoid huge input data, these operations are encrypted through some particular approach. 
There are three unsigned 32-bit integers X,YX,Y and ZZ which have initial values given by the input. A random number generator function is described as following, where ∧means the bitwise exclusive-OR operator, <<<< means the bitwise left shift operator and >>>> means the bitwise right shift operator. Note that function would change the values of X,YX,Y and ZZ after calling. 

1 #include
2 using namespace std;
3 typedef long long LL;
4 const int maxn&#61;1e5&#43;10;
5 int n,m,T;
6 unsigned int X,Y,Z;
7
8 struct Node{
9 int l,r,maxnum,minnum,tag;
10 } tree[maxn<<2];
11
12 unsigned int functions()
13 {
14 X&#61;X^(X<<11);
15 X&#61;X^(X>>4);
16 X&#61;X^(X<<5);
17 X&#61;X^(X>>14);
18 unsigned int w&#61;X^(Y^Z);
19 X&#61;Y; Y&#61;Z; Z&#61;w;
20 return Z;
21 }
22
23 void build(int pos,int l,int r)
24 {
25 tree[pos].l&#61;l,tree[pos].r&#61;r,tree[pos].tag&#61;-1;
26 if(l&#61;&#61;r)
27 {
28 tree[pos].maxnum&#61;0;
29 tree[pos].minnum&#61;0;
30 return ;
31 }
32 int mid&#61;(tree[pos].l&#43;tree[pos].r)>>1;
33 build(pos<<1,l,mid);
34 build(pos<<1|1,mid&#43;1,r);
35 tree[pos].maxnum&#61;max(tree[pos<<1].maxnum,tree[pos<<1|1].maxnum);
36 tree[pos].minnum&#61;min(tree[pos<<1].minnum,tree[pos<<1|1].minnum);
37 }
38
39 void pushup(int pos)
40 {
41 tree[pos].maxnum&#61;max(tree[pos<<1].maxnum,tree[pos<<1|1].maxnum);
42 tree[pos].minnum&#61;min(tree[pos<<1].minnum,tree[pos<<1|1].minnum);
43 }
44
45 void pushdown(int pos)
46 {
47 tree[pos<<1].maxnum&#61;max(tree[pos<<1].maxnum,tree[pos].tag);
48 tree[pos<<1|1].maxnum&#61;max(tree[pos<<1|1].maxnum,tree[pos].tag);
49 tree[pos<<1].minnum&#61;max(tree[pos<<1].minnum,tree[pos].tag);
50 tree[pos<<1|1].minnum&#61;max(tree[pos<<1|1].minnum,tree[pos].tag);
51 tree[pos<<1].tag&#61;max(tree[pos<<1].tag,tree[pos].tag);
52 tree[pos<<1|1].tag&#61;max(tree[pos<<1|1].tag,tree[pos].tag);
53 tree[pos].tag&#61;-1;
54 }
55
56 void update(int pos,int l,int r,int val)
57 {
58 if(tree[pos].l&#61;&#61;tree[pos].r)
59 {
60 tree[pos].maxnum&#61;max(tree[pos].maxnum,val);
61 tree[pos].minnum&#61;max(tree[pos].minnum,val);
62 return ;
63 }
64 if(tree[pos].l>&#61;l&&tree[pos].r<&#61;r)
65 {
66 if(tree[pos].maxnum<&#61;val)
67 {
68 tree[pos].maxnum&#61;tree[pos].minnum&#61;val;
69 tree[pos].tag&#61;max(tree[pos].tag,val);
70 return ;
71 }
72 }
73 if(tree[pos].minnum>&#61;val) return ;
74
75 if(tree[pos].tag!&#61;-1) pushdown(pos);
76 int mid&#61;(tree[pos].l&#43;tree[pos].r)>>1;
77 if(r<&#61;mid) update(pos<<1,l,r,val);
78 else if(l>&#61;mid&#43;1) update(pos<<1|1,l,r,val);
79 else update(pos<<1,l,mid,val),update(pos<<1|1,mid&#43;1,r,val);
80 pushup(pos);
81 }
82
83 int query(int pos,int k)
84 {
85 if(tree[pos].l&#61;&#61;tree[pos].r&&tree[pos].l&#61;&#61;k) return tree[pos].maxnum;
86 int mid&#61;(tree[pos].l&#43;tree[pos].r)>>1;
87 if(tree[pos].tag!&#61;-1) pushdown(pos);
88 if(k<&#61;mid) return query(pos<<1,k);
89 else return query(pos<<1|1,k);
90 }
91
92 int main()
93 {
94 ios::sync_with_stdio(false);
95 cin.tie(0);
96 cin>>T;
97 while(T--)
98 {
99 LL ans&#61;0;
100 cin>>n>>m>>X>>Y>>Z;
101 build(1,1,n);
102 for(int i&#61;1;i<&#61;m;i&#43;&#43;)
103 {
104 int l&#61;functions()%n&#43;1;
105 int r&#61;functions()%n&#43;1;
106 int v&#61;functions()%(1<<30);
107 if(l>r) swap(l,r);
108 update(1,l,r,v);
109 }
110 for(int i&#61;1;i<&#61;n;i&#43;&#43;) ans^&#61;1ll*i*query(1,i);
111 cout<endl;
112 }
113
114 return 0;
115 }

View Code

 

转:https://www.cnblogs.com/songorz/p/9482441.html



推荐阅读
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文介绍了在Windows环境下使用pydoc工具的方法,并详细解释了如何通过命令行和浏览器查看Python内置函数的文档。此外,还提供了关于raw_input和open函数的具体用法和功能说明。 ... [详细]
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 火星商店问题:线段树分治与持久化Trie树的应用
    本题涉及编号为1至n的火星商店,每个商店有一个永久商品价值v。操作包括每天在指定商店增加一个新商品,以及查询某段时间内某些商店中所有商品(含永久商品)与给定密码值的最大异或结果。通过线段树分治和持久化Trie树来高效解决此问题。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 360SRC安全应急响应:从漏洞提交到修复的全过程
    本文详细介绍了360SRC平台处理一起关键安全事件的过程,涵盖从漏洞提交、验证、排查到最终修复的各个环节。通过这一案例,展示了360在安全应急响应方面的专业能力和严谨态度。 ... [详细]
  • 本文探讨了如何在模运算下高效计算组合数C(n, m),并详细介绍了乘法逆元的应用。通过扩展欧几里得算法求解乘法逆元,从而实现除法取余的计算。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • This document outlines the recommended naming conventions for HTML attributes in Fast Components, focusing on readability and consistency with existing standards. ... [详细]
  • Splay Tree 区间操作优化
    本文详细介绍了使用Splay Tree进行区间操作的实现方法,包括插入、删除、修改、翻转和求和等操作。通过这些操作,可以高效地处理动态序列问题,并且代码实现具有一定的挑战性,有助于编程能力的提升。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文讨论了如何根据特定条件动态显示或隐藏文件上传控件中的默认文本(如“未选择文件”)。通过结合CSS和JavaScript,可以实现更灵活的用户界面。 ... [详细]
author-avatar
全哥-广州仔1
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有