传送门

线段树简单题。

二分答案+线段树排序。

实际上就是二分答案mid" role="presentation" style="position: relative;">midmid,然后把比mid" role="presentation" style="position: relative;">midmid小的全部变成0" role="presentation" style="position: relative;">00,不比mid" role="presentation" style="position: relative;">midmid小的全部变成1" role="presentation" style="position: relative;">11,然后用线段树维护01" role="presentation" style="position: relative;">0101排序。

时间效率O(mlogn)" role="presentation" style="position: relative;">O(mlogn)O(mlogn)。

代码:

#include<bits/stdc++.h>
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
#define N 100005
using namespace std;
int n,m,a[N],b[N],l,r,pos;
struct Node{int l,r,cov,sum;}T[N<<2];
struct Query{int op,l,r;}q[N];
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans;
}
inline void pushup(int p){T[p].sum=T[lc].sum+T[rc].sum;}
inline void pushnow(int p,int v){T[p].cov=v,T[p].sum=(T[p].r-T[p].l+1)*v;}
inline void pushdown(int p){if(T[p].cov!=-1)pushnow(lc,T[p].cov),pushnow(rc,T[p].cov),T[p].cov=-1;}
inline void build(int p,int l,int r){
    T[p].l=l,T[p].r=r,T[p].cov=-1;
    if(l==r){T[p].sum=b[l];return;}
    build(lc,l,mid),build(rc,mid+1,r),pushup(p);
}
inline void update(int p,int ql,int qr,int v){
    if(ql>T[p].r||qr<T[p].l)return;
    if(ql<=T[p].l&&T[p].r<=qr)return pushnow(p,v);
    pushdown(p);
    if(qr<=mid)update(lc,ql,qr,v);
    else if(ql>mid)update(rc,ql,qr,v);
    else update(lc,ql,mid,v),update(rc,mid+1,qr,v);
    pushup(p);
}
inline int query(int p,int ql,int qr){
    if(ql>T[p].r||qr<T[p].l)return 0;
    if(ql<=T[p].l&&T[p].r<=qr)return T[p].sum;
    pushdown(p);
    if(qr<=mid)return query(lc,ql,qr);
    if(ql>mid)return query(rc,ql,qr);
    return query(lc,ql,mid)+query(rc,mid+1,qr);
}
inline bool check(int x){
    for(int i=1;i<=n;++i)b[i]=(a[i]>=x);
    build(1,1,n);
    for(int i=1;i<=m;++i){
        int num=query(1,q[i].l,q[i].r);
        if(!q[i].op)update(1,q[i].r-num+1,q[i].r,1),update(1,q[i].l,q[i].r-num,0);
        else update(1,q[i].l,q[i].l+num-1,1),update(1,q[i].l+num,q[i].r,0);
    }
    return query(1,pos,pos);
}
int main(){
    n=read(),m=read(),l=1,r=n;
    for(int i=1;i<=n;++i)a[i]=read();
    for(int i=1;i<=m;++i)q[i].op=read(),q[i].l=read(),q[i].r=read();
    pos=read();
    while(l<=r){
        int midd=l+r>>1;
        if(check(midd))l=midd+1;
        else r=midd-1;
    }
    if(check(r))printf("%d",r);
    else printf("%d",l);
    return 0;
}

2018.08.01 BZOJ4552: [Tjoi2016&Heoi2016]排序(二分+线段树)的更多相关文章

  1. [bzoj4552][Tjoi2016&Heoi2016]排序-二分+线段树

    Brief Description DZY有一个数列a[1..n],它是1∼n这n个正整数的一个排列. 现在他想支持两种操作: 0, l, r: 将a[l..r]原地升序排序. 1, l, r: 将a ...

  2. 【BZOJ4552】[Tjoi2016&Heoi2016]排序 二分+线段树

    [BZOJ4552][Tjoi2016&Heoi2016]排序 Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ...

  3. bzoj 4552: [Tjoi2016&Heoi2016]排序——二分+线段树

    Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ,需要你来帮助他.这个难题是这样子的:给出一个1到n的全排列,现在对这 ...

  4. [BZOJ4552][TJOI2016&&HEOI2016]排序(二分答案+线段树/线段树分裂与合并)

    解法一:二分答案+线段树 首先我们知道,对于一个01序列排序,用线段树维护的话可以做到单次排序复杂度仅为log级别. 这道题只有一个询问,所以离线没有意义,而一个询问让我们很自然的想到二分答案.先二分 ...

  5. bzoj千题计划128:bzoj4552: [Tjoi2016&Heoi2016]排序

    http://www.lydsy.com/JudgeOnline/problem.php?id=4552 二分答案 把>=mid 的数看做1,<mid 的数看做0 这样升序.降序排列相当于 ...

  6. bzoj4552: [Tjoi2016&Heoi2016]排序(二分+线段树)

    又是久违的1A哇... 好喵喵的题!二分a[p],把大于mid的数改为1,小于等于mid的数改为0,变成01串后就可以用线段树进行那一连串排序了,排序后如果p的位置上的数为0,说明答案比mid小,如果 ...

  7. BZOJ4552 [Tjoi2016&Heoi2016]排序 【二分 + 线段树】

    题目链接 BZOJ4552 题解 之前去雅礼培训做过一道题,\(O(nlogn)\)维护区间排序并能在线查询 可惜我至今不能get 但这道题有着\(O(nlog^2n)\)的离线算法 我们看到询问只有 ...

  8. BZOJ4552:[TJOI2016&HEOI2016]排序(线段树,二分)

    Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题,需要你来帮助他. 这个难题是这样子的:给出一个1到n的全排列,现在对这 ...

  9. BZOJ4552 Tjoi2016&Heoi2016排序 【二分+线段树】*

    Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题,需要你来帮助他.这个难题是这样子的:给出一个1到n的全排列,现在对这个 ...

随机推荐

  1. leetcode506

    public class Solution { public string[] FindRelativeRanks(int[] nums) { var list = nums.OrderByDesce ...

  2. jackson的小知识

  3. The type org.springframework.dao.support.DaoSupport cannot be resolved. It is indirectly referenced

    springmvc mybatis整合,遇到错误:The type org.springframework.dao.support.DaoSupport cannot be resolved. It ...

  4. Activity服务类-3 FormService服务类

    1.获取//通过流程定义ID获取表单字段集合StartFormData startFormData = formService.getStartFormData(processDefinitionId ...

  5. springmvc @valid

    JSR303是javaEE6中的一个子规范:Bean Validation.官方实现是HibernateValidatior.校验: 使用springmvc 的validate机制,需要引入valid ...

  6. AWK用法整理

    printf "1:2::3:::4::::5" | awk -F '[:]+' '{print $4}' [:]+ 表示以1个或多个 :(冒号)作为分隔符 ip  addr  | ...

  7. How to Pronounce UMBRELLA

    How to Pronounce UMBRELLA Share Tweet Share Tagged With: 3-Syllable When the weather is bad, you’ll ...

  8. 趣味编程:24点(Haskell版)

    24 game/Solve import Data.List import Data.Ratio import Control.Monad data Expr = Constant Rational ...

  9. 关于如何以编程的方式执行TestNG

    1.如果需要加入一个一个的类 public static void main(String args[]){ TestNG tng = new TestNG(); tng.SetTestClasses ...

  10. Python基础语法题库

    引言: 语法练习包括Python基础语法.数据类型.字符编码和简单文件操作等内容. 正文(参考答案附录在题目下方): 1.Python 里用来告知解释器跳过当前循环中的剩余语句,然后继续进行下一轮循环 ...