【HDU - 5649】DZY Loves Sorting(线段树,区间更新区间查询,思维,01缩数变换,线段树分割)
题干:
DZY has a sequence a[1..n]a[1..n]. It is a permutation of integers 1∼n1∼n.
Now he wants to perform two types of operations:
0lr0lr: Sort a[l..r]a[l..r] in increasing order.
1lr1lr: Sort a[l..r]a[l..r] in decreasing order.
After doing all the operations, he will tell you a position kk, and ask you the value of a[k]a[k].
Input
First line contains tt, denoting the number of testcases.
tt testcases follow. For each testcase:
First line contains n,mn,m. mm is the number of operations.
Second line contains nn space-separated integers a[1],a[2],⋯,a[n]a[1],a[2],⋯,a[n], the initial sequence. We ensure that it is a permutation of 1∼n1∼n.
Then mm lines follow. In each line there are three integers opt,l,ropt,l,r to indicate an operation.
Last line contains kk.
(1≤t≤50,1≤n,m≤100000,1≤k≤n,1≤l≤r≤n,opt∈{0,1}1≤t≤50,1≤n,m≤100000,1≤k≤n,1≤l≤r≤n,opt∈{0,1}. Sum of nn in all testcases does not exceed 150000150000. Sum of mm in all testcases does not exceed 150000150000)
Output
For each testcase, output one line - the value of a[k]a[k] after performing all mmoperations.
Sample Input
1 6 3 1 6 2 5 3 4 0 1 4 1 3 6 0 2 4 3
Sample Output
5
Hint
1 6 2 5 3 4 -> [1 2 5 6] 3 4 -> 1 2 [6 5 4 3] -> 1 [2 5 6] 4 3. At last $a[3]=5$.
题目大意:
解题报告:
两个log的做法展现了二分答案的强大功能。首先二分枚举第 k 位的值x,然后将大于等于x的数都变为 1 ,小于x的数变为 0 ,这样这数字序列就变成了01序列,只有这两种性质。我们用线段树不难实现对 01 序列按要求进行排序,然后如果第 k 位为 1 说明x可以是ans但是太小了,要调整下界。就这样不断二分下来,得到的边界值就是第 k 位真实的值。这个做法是离线的,有两个log ,但代码好实现。
这题巧妙之处:第一在于只有一次查询,第二在于是n的全排列。(但是貌似不是全排列也可用类似的方法做。)
nlogn的神仙方法:https://www.cnblogs.com/Paulliant/p/10185235.html(线段树分割)
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,m;
struct Node {int op,l,r;Node(){}Node(int op,int l,int r):op(op),l(l),r(r){}
} nn[MAX];
int a[MAX],k;
struct TREE {int l,r;int val,laz;
} tree[MAX<<2];
int len(int cur) {return tree[cur].r - tree[cur].l + 1;
}
void pushup(int cur) {tree[cur].val = tree[cur*2].val + tree[cur*2+1].val;
}
void pushdown(int cur) {if(tree[cur].laz == -1) return ;//如果是0???则??? int tmp = tree[cur].laz;tree[cur].laz = -1;tree[cur*2].val = len(cur*2) * tmp;tree[cur*2+1].val = len(cur*2+1) * tmp;tree[cur*2].laz = tmp;tree[cur*2+1].laz = tmp;
}
void build(int l,int r,int cur,int key) {tree[cur].l = l;tree[cur].r = r;tree[cur].laz = -1;if(l == r) {tree[cur].val = a[l]>=key;return;}int m = (l+r)>>1;build(l,m,cur*2,key);build(m+1,r,cur*2+1,key);pushup(cur);
}
int query(int pl,int pr,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) {return tree[cur].val;}pushdown(cur);int res = 0;if(pl <= tree[cur*2].r) res += query(pl,pr,cur*2);if(pr >= tree[cur*2+1].l) res += query(pl,pr,cur*2+1);return res;
}
void update(int pl,int pr,int cur,int val) {if(pl <= tree[cur].l && pr >= tree[cur].r) {tree[cur].laz = val;tree[cur].val = val * len(cur);return;}pushdown(cur);if(pl <= tree[cur*2].r) update(pl,pr,cur*2,val);if(pr >= tree[cur*2+1].l) update(pl,pr,cur*2+1,val);pushup(cur);
}
bool ok(int x) {build(1,n,1,x);for(int i = 1; i<=m; i++) {int l=nn[i].l,r=nn[i].r,op=nn[i].op;int tmp = query(l,r,1);if(tmp == r-l+1 || tmp == 0) continue;if(op == 1) {update(l,l+tmp-1,1,1);update(l+tmp,r,1,0);}else {update(r-tmp+1,r,1,1);update(l,r-tmp,1,0);}} return query(k,k,1) == 1;
}
int main()
{int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);for(int i = 1; i<=n; i++) scanf("%d",a+i);for(int op,x,y,i = 1; i<=m; i++) {scanf("%d%d%d",&op,&x,&y);nn[i] = Node(op,x,y);}scanf("%d",&k);int l = 1,r = n,mid = (l+r)>>1,ans;while(l<=r) {mid = (l+r)>>1;if(ok(mid)) ans=mid,l = mid+1;else r = mid-1;}printf("%d\n",ans);} return 0 ;
}
【HDU - 5649】DZY Loves Sorting(线段树,区间更新区间查询,思维,01缩数变换,线段树分割)相关推荐
- 石油大 2019年第二阶段我要变强个人训练赛第十八场 Problem N 扶桑号战列舰(线段树+区间更新+区间查询)
链接:http://icpc.upc.edu.cn/problem.php?cid=1803&pid=13 题意:给出一个n,接下来一行给出n个数.才开始所有数为0,每次操作可以选一个区间[l ...
- poj3468 线段树区间更新+区间查询
题目链接: http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limi ...
- Codeforces 444C DZY Loves Colors 线段树区间更新
// Codeforces 444C DZY Loves Colors 线段树区间更新// 题目链接:// http://codeforces.com/problemset/problem/444/C ...
- hdu 5692 Snacks(dfs序+线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5692 解题思路:这道题是树节点的点权更新,而且涉及到子树,常用的思路是利用dfs序,用线段树来对区间进 ...
- hdu 3966(树链剖分+线段树区间更新)
传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...
- POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新
直接将这3题 放一起了 今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...
- hdu 1698(线段树区间更新)
解题思路:线段树区间更新水题. #include<iostream> #include<cstdio> #include<cstring> using namesp ...
- Just a Hook(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 In the game of DotA, Pudge's meat hook is actual ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- 牛客网 2018年全国多校算法寒假训练营练习比赛(第五场) H.Tree Recovery-完全版线段树(区间更新、区间求和)...
H.Tree Recovery 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 链接:https:/ ...
最新文章
- 计算机书籍-Exploring Cloud Computing免费电子书
- react textarea 空格为什么不换行_React 怎么实现预防XSS 攻击的
- ACM OJ反馈结果大全
- android控件使用大全,Android常见控件使用详解
- fx系列微型可编程控制器 通信_电气人,三菱Q系列和FX PLC系列之间的区别你都知道吗?...
- UE3 后期处理编辑器用户指南
- 依赖注入的三种方式_Spring IoC是如何进行依赖注入的
- 24秒篮球计时器mulisim12.0_奥尼尔力量有多恐怖?325磅体重把整个篮球架子拦腰折断...
- python用来自动修改pdf_python实现从pdf文件中提取文本,并自动翻译的方法
- 高通android7.0模块编译.ko
- 一秒学会,小白也能上手,最简单的装逼代码
- Python汉字转拼音-拼音转汉字的效率测评
- python爬取千图网_scrapy之千图网全站爬虫
- 复指数与高斯函数乘积的傅里叶变换_量子力学杂谈——格林函数
- SQL 语句,聚沙成塔
- 怎样更换手机壁纸?2022更换手机壁纸软件大全
- 电子信息(非全)考研分析——大连理工VS哈工程
- VLookup函数怎么用?详细解析
- 11-hive数据查询方式
- 计算机教室场地技术条,特殊教育学校功能室建设标准
热门文章
- Breadth-first Search(广度优先搜索)专题2
- [算法][二分查找][排除法]
- 2019昆明计算机会议,计算机 | ACSAC 2019等国际会议信息6条
- python中math库最大值_python-math库解析
- java 8 中文API
- centos 源码安装mysql5.6_CentOS 7下源码安装MySQL 5.6
- linux脚本ls输出到变量中,bash – 将命令输出的错误消息存储到shell变量中
- threejs向量夹角和夹角方向
- css划上去变长,Css3如何实现鼠标移上变长特效?(图文+视频)
- 一般通话记录能保存多少条_鸡蛋放冰箱,能保存多少天?正确保存方法是什么?...