P2824(线段树加二分)

原题传送门

这是一道十分巧妙的题目,虽然还有线段树分裂与合并的做法,奈何蒟蒻不会啊。所以在看了题解之后,终于想通了二分的做法

分析:

对于这道题目,它想让我们去维护一个序列,并且对于每组更新,都需要进行排序,但是光单次排序的复杂度就是 o ( l o g n ) o(logn) o(logn),如果这样执行m次,显然会t啊。(但是能骗分
所以我们需要换种思路,由于我们维护的是一个区间,线段树多半是逃不了了,但是线段树的复杂度已经是 o ( n ∗ l o g n ) o(n*logn) o(nlogn)了,我们不能在增加太多的运算。
那么怎么办?

我们真的需要把每个数的信息都“好好维护”吗?

根据题意,我们只需要输出在进行若干次对子区间升降序排序后第p个值,我们思考一下,如果只是要获得这个值,我们是否只需要知道这个值在整个区间的相对大小即可 (利用相对大小给区间打上标记)。但是我们又不知道这个值,我们又要怎么搞?这时我们容易想到二分答案,二分的复杂度非常优秀,但是其中又有何单调性?
思考一下,如果我们现在假定了一个值为答案,我们记为mid,那么我们把大于等于mid的值全部记为1,小于它的值记为0,如果当前更新后的即变成01序列后的答案(在进行若干操作后p位置的值),为0,那么它一定不符合,因为我们枚举的答案性质在上述过程中等价于1(当然并不是完全一样),并且,我们最后的答案一定小于mid,为什么?我们思考一下,如果在变换过后的序列中p位置的值是0,也就是说该点的值小于1性质,即当前值只能小于mid;而如果我们的p位置值是1,那么它就有可能为答案(因为它代表着当前位置上的值大于等于mid),我们就去缩小二分区间右端点r的值。

代码如下


//#include<bits/stdc++.h> ----万能头----
#include <iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cmath>
#include<queue>
#include<list>
#include<map>
#include<unordered_map>
#include<stack>
using namespace std;
const int p = 1e9+7;
const int N = 2e6+5;
const int maxn = 1e5+10;
const long long INF = 1e18;
#define REP(i,n) for(int i = 1; i <= n; ++i)
#define REPA(i,j,n) for(int i = j; i <= n; ++i)
#define RREP(i,n) for(int i = n; i >= 1; --i)
#define REPR(i,j,n) for(int i = n; i >= j; --i)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> PII;
ll n, m, t, k;
ll a[N];
ll pos;
struct tree{
    int l, r;
    ll lazy;
    ll cnt;
}tr[N<<2];
struct Query{
    int l, r;
    int is;
}query[N];
ll b[N];
void pushup(int p){
    tr[p].cnt = tr[p<<1].cnt + tr[p<<1|1].cnt;
}
void pushdown(int p){
    if(tr[p].lazy != -1){
        tr[p<<1].lazy = tr[p<<1|1].lazy = tr[p].lazy;
        if(tr[p].lazy == 1){
            tr[p<<1].cnt  = tr[p<<1].r + 1 - tr[p<<1].l;
            tr[p<<1|1].cnt = tr[p<<1|1].r + 1 - tr[p<<1|1].l;
        }
        else {
            tr[p<<1].cnt = tr[p<<1|1].cnt = 0;
        }
        tr[p].lazy = -1;
    }
}
void build(int l, int r, int p){
    tr[p].l = l, tr[p].r = r;
    tr[p].lazy = -1;
    if(l == r){
        tr[p].cnt = b[l];
        return;
    }
    int mid = tr[p].l + tr[p].r >> 1;
    build(l,mid,p<<1);
    build(mid+1,r,p<<1|1);
    pushup(p);
}
void update(int l,int r,int p,int val){
    if(l <= tr[p].l && tr[p].r <= r){
        tr[p].lazy = val;
        tr[p].cnt = val*(tr[p].r + 1 - tr[p].l);

        return;
    }
    pushdown(p);
    int mid = tr[p].l + tr[p].r >> 1;
    if(mid >= l)update(l,r,p<<1,val);
    if(mid < r)update(l,r,p<<1|1,val);
    pushup(p);
}
ll get(int l ,int r,int p,int val){
    if(l <= tr[p].l && tr[p].r <= r){
        return tr[p].cnt;
    }
    pushdown(p);
    int mid = tr[p].l + tr[p].r >> 1;
    ll res = 0;
    if(mid >= l) res += get(l,r,p<<1,val);
    if(mid < r) res += get(l,r,p<<1|1,val);
    return res;
}
bool get_point(int l,int r,int p){
    if(tr[p].l == tr[p].r){
        return tr[p].cnt;
    }
    pushdown(p);
    int mid = tr[p].l +  tr[p].r >> 1;
    if(mid >= l) return get_point(l,r,p<<1);
    else return get_point(l,r,p<<1|1);
}
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
bool ck(ll mid){
    for(int i = 1; i <= n; ++i){
        if(a[i] >= mid)b[i] = 1;
        else b[i] = 0;
    }
    build(1, n,1);
    for(int i = 1; i <= m; ++i){
        ll t_one = get(query[i].l,query[i].r,1,1);
        if(query[i].is == 0){
            if(t_one)update(query[i].r - t_one + 1,query[i].r,1,1);
            if(query[i].l<=query[i].r - t_one)update(query[i].l,query[i].r-t_one,1,0);
        }
        else {
            if(t_one)update(query[i].l,query[i].l+t_one-1,1,1);
            if(query[i].l + t_one <= query[i].r)update(query[i].l+t_one,query[i].r,1,0);
        }
    }
    if(get_point(pos,pos,1))return 1;
    else return 0;
}

void solve(){
    n = read(), m = read();
    ll mn = INF, mx = 0;
    for(int i = 1; i <= n; ++i){
        a[i] = read();
        mn = min(a[i],mn);
        mx = max(a[i],mx);
    }
    ll l = mn, r = mx;
    vector<PII> q;
    for(int i = 1; i <= m; ++i){
        query[i].is = read();
        query[i].l = read();
        query[i].r = read();
    }
    pos = read();
    if(mn == mx){
        cout<<mx<<'\n';
        return;
    }
    ll mid;
    while(l < r){
        mid = (l + r + 1) >> 1;
        if(ck(mid)){
            l = mid;
        }
        else{
            r = mid - 1;
        }
    }
    mid = l + r >> 1;
    cout<<mid<<"\n";
}


int main() {
    //ios::sync_with_stdio(0);
    //cin.tie(0);
    //cout.tie(0);

    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    //scanf("%lld",&t);
    //while(t--)
        solve();

    fclose(stdin);
    fclose(stdout);
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值