hdu_5649 DZY Loves Sorting
来源:互联网 发布:单片机cy标志为 编辑:程序博客网 时间:2024/06/05 15:14
DZY Loves Sorting
http://acm.hdu.edu.cn/showproblem.php?pid=5649
题意:
有一个数列,它是1∼n这n个正整数的一个排列。 要求支持两种操作: 0 l r: 将a[l..r]原地升序排序。 1 l r: 将a[l..r]原地降序排序。 操作完后,他会给你指定一个位k,请你告诉他a[k]的值。详见样例: 1 6 3 1 6 2 5 3 4 0 1 4 1 3 6 0 2 4答案: 5
数据:
1≤t≤50,1≤n,m≤100000,1≤k≤n,1≤l≤r≤n,opt∈{0,1}. n、m的和不超过 150000.
思路:
- 二分+线段树区间更新
(mlog2n) . - 二分的是最终的结果a[k]的值,假设为res。
- 建树时候把小于 res 的置 0 ,大于等于的置 1 。那么每次排序就相当于把原来某区间的 01串 变成 00..0011..11 或者 11..1100..00 这样的形式,其实就相当于 线段树的区间修改。(这里需要注意修改区间 L与R 的大小,否则RE)
- 这样一顿操作后根据 第k个位置 的 0|1 情况二分下去。
- 还有
(mlogn) 的做法 , 待补。
代码:
#include <cstdio>#include <math.h>#include <iostream>#include <algorithm>#include <string.h>#include <queue>#include <set>#include <map>using namespace std;typedef long long LL;const int N = 400007;const int M = 100005;const int INF = 0x3f3f3f3f;const int MOD = 1000000007;int n,m;int a,b;int c;int ans;int pur;struct nd{ int val; int add;}tr[N<<2];struct poi{ int l,r; int op;}s[N];int t[N];void pushup(int k,int len){ tr[k].val=tr[k<<1].val+tr[k<<1|1].val;}void pushdown(int k,int len){ if (tr[k].add==-1) return; tr[k<<1].add=tr[k].add; tr[k<<1|1].add=tr[k].add; tr[k<<1].val=tr[k].add*(len-len/2); tr[k<<1|1].val=tr[k].add*(len/2); tr[k].add=-1;} void build(int l,int r,int k,int ep){ tr[k].add= -1; if (l==r) { tr[k].val= (t[l]>=ep? 1 : 0); return; } int mid=(l+r)>>1; build(l,mid,k<<1,ep); build(mid+1,r,k<<1|1,ep); pushup(k,r-l+1);}void update(int L,int R,int c,int l,int r,int k){ if (L<=l && r<=R) { tr[k].add=c; tr[k].val=(r-l+1)*c; return; } pushdown(k,r-l+1); int mid=(l+r)>>1; if (L<=mid ) update(L,R,c,l,mid,k<<1); if (R>mid) update(L,R,c,mid+1,r,k<<1|1); pushup(k,r-l+1);}int query(int L,int R,int l,int r,int k){ if (L<=l && r<=R) { return tr[k].val; } pushdown(k,r-l+1); int mid=(l+r)>>1; int ans=0; int len=r-l+1; if (L<=mid) ans+=query(L,R,l,mid,k<<1); if (R>mid) ans+=query(L,R,mid+1,r,k<<1|1); return ans;}int sat(int k){ build(1,n,1,k); for (int i=0;i<m;i++) { int a=s[i].l; int b=s[i].r; int c=s[i].op; int tmp=query(a,b,1,n,1); if (c==0) { if (a<=b-tmp) update(a,b-tmp,0,1,n,1); if (b-tmp+1<=b) update(b-tmp+1,b,1,1,n,1); } else { if (a<=a+tmp-1) update(a,a+tmp-1,1,1,n,1); if (a+tmp<=b) update(a+tmp,b,0,1,n,1); } } return query(pur,pur,1,n,1);}int main(){ int cc; int cas=1; scanf("%d",&cc); while (cc--) { scanf("%d%d",&n,&m); memset(tr,0,sizeof tr); for (int i=1;i<=n;i++) scanf("%d",&t[i]); for (int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); s[i].l=b; s[i].r=c; s[i].op=a; } scanf("%d",&pur); int l=0,r=n+1; int mid; int ans=-1; while (l<r) { mid=(l+r)>>1; if (sat(mid)) l=mid+1; else r=mid; } printf("%d\n", l-1); } return 0;}
阅读全文
0 0
- hdu_5649 DZY Loves Sorting
- BC DZY Loves Topological Sorting
- HDU 5195 DZY Loves Topological Sorting
- hdu 5195 DZY Loves Topological Sorting
- [线段树] hdu5195 DZY Loves Topological Sorting
- (拓扑排序)DZY Loves Topological Sorting
- HDU 5195 DZY Loves Topological Sorting
- HDU5649 DZY Loves Sorting 二分+线段树
- 线段树+二分 hdu5649 DZY Loves Sorting
- HDU 5195 DZY Loves Topological Sorting
- HDU 5195 DZY Loves Topological Sorting(线段树)
- BestCoder Round #35(DZY Loves Topological Sorting-堆+贪心)
- hdu 5195 DZY Loves Topological Sorting && BestCoder Round #35
- HDU 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
- hdu5195 DZY Loves Topological Sorting 线段树+拓扑排序
- Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】
- HDU 5195 DZY Loves Topological Sorting(优先队列)
- hdu 5649 DZY Loves Sorting(二分+线段树)
- struts2 no result defined for action xxxx and result input
- universal-image-loader源码解析
- android gps机制分析--定位数据HAL处理
- oracle 练习题
- OSGI入门
- hdu_5649 DZY Loves Sorting
- hdu1050 Moving Tables
- 一个能够快速把JSON数据格式化成漂亮网页的开源JavaScript库:angular-json-human
- thinkphp函数及常量
- windows7 C语言打印目前进程列表、删除一个进程、显示一个进程地址。
- UVA 1594 Ducci Sequence
- 【bzoj1050】[HAOI2006]旅行comf
- Jacobian矩阵和Hessian矩阵
- 图的建立 BFS DFS