(我愣是感觉事情没有这么简单。。。。)
于是乎先不颓数据结构了。。。。先把单调队列以及斜率优化dp给学会先,,,,
一般模拟赛出这种题我就只会爆搜了。。。。
然后就没啥好怕得了
二逼平衡树(二分打炸版)(6k的小清新代码小声bb)
#include<bits/stdc++.h>
#define MAXN 50005
typedef long long ll;
using namespace std;
ll n,m,b[MAXN];
int tot = 0,dex = 0;
struct node{
int ch[2],f,sz,cnt;
ll val;
}t[MAXN * 105];
struct Splay{
int op;
int up(int rt){
t[rt].sz = t[rt].cnt + t[t[rt].ch[0]].sz + t[t[rt].ch[1]].sz;
}
int get(int rt){
return (t[t[rt].f].ch[1] == rt);
}
int rote(int rt){
int ff = t[rt].f;
if(!ff)return 0;
int gg = t[ff].f;
int d = get(rt);
int cc = t[rt].ch[d ^ 1];
if(gg)t[gg].ch[get(ff)] = rt;
t[rt].f = gg;
t[ff].f = rt;
t[rt].ch[d ^ 1] = ff;
if(cc)t[cc].f = ff;
t[ff].ch[d] = cc;
up(ff) , up(rt);
}
int splay(int rt){
for(int f = t[rt].f ; (f = t[rt].f) ; rote(rt)){
if(get(f) == get(rt))rote(f);
else rote(rt);
}
op = rt;
}
int add(ll dx){
if(!op){
op = ++tot;
t[tot].cnt = t[tot].sz = 1;
t[tot].val = dx;t[tot].f = 0;
return 0;
}
int d , rt = op;
while(1){
if(t[rt].val == dx){
t[rt].cnt++;
splay(rt);
return 0;
}
d = (t[rt].val < dx);
if(!t[rt].ch[d]){
t[rt].ch[d] = ++tot;
t[tot].cnt = t[tot].sz = 1;
t[tot].val = dx;t[tot].f = rt;
splay(tot);
return 0;
}
rt = t[rt].ch[d];
}
}
int rank(ll dx){
int rt = op , d , zz = 0;
while(1){
if(!rt)return (-99999);
d = (t[rt].val <= dx);
if(d)zz = zz + t[rt].cnt + t[t[rt].ch[0]].sz;
if(!t[rt].ch[d])return zz;
rt = t[rt].ch[d];
}
}
int rank2(ll dx){
int rt = op , d , zz = 0;
while(1){
if(!rt)return (-99999);
d = (t[rt].val <= dx);
if(d)zz = zz + t[rt].cnt + t[t[rt].ch[0]].sz;
if(!t[rt].ch[d])return zz;
rt = t[rt].ch[d];
}
}
int found(ll dx){
int rt = op , d;
while(1){
if(t[rt].val == dx)return rt;
d = (t[rt].val < dx);
rt = t[rt].ch[d];
}
}
int pre(){
int rt = t[op].ch[0];
while(t[rt].ch[1])rt = t[rt].ch[1];
return rt;
}
int nxt(){
int rt = t[op].ch[1];
while(t[rt].ch[0])rt = t[rt].ch[0];
return rt;
}
int del(ll dx){
splay(found(dx));
if(!t[op].ch[0] && !t[op].ch[1]){op = 0;return 0;}
if(!t[op].ch[1]){
op = t[op].ch[0];
t[op].f = 0;
return 0;
}
if(!t[op].ch[0]){
op = t[op].ch[1];
t[op].f = 0;
return 0;
}
splay(pre());
t[op].ch[1] = t[t[op].ch[1]].ch[1];
t[t[op].ch[1]].f = op;
up(op);
}
int print(int rt){
if(t[rt].ch[0]){
cout<<t[rt].val<<" "<<t[t[rt].ch[0]].val<<endl;
print(t[rt].ch[0]);
}
if(t[rt].ch[1]){
cout<<t[rt].val<<" "<<t[t[rt].ch[1]].val<<endl;
print(t[rt].ch[1]);
}
}
};
struct st{
int lc,rc;
Splay T;
}a[MAXN * 5];
int build(int rt , int l , int r){
if(!rt)rt = ++dex;
for(int len = l ; len <= r ; len++)a[rt].T.add(b[len]);
/*cout<<" -------- "<<l<<" "<<r<<endl;
a[rt].T.print(a[rt].T.op);
cout<<endl;*/
if(l == r)return dex;
int mid = (l + r) >> 1;
a[rt].lc = build(a[rt].lc , l , mid);
a[rt].rc = build(a[rt].rc , mid + 1 , r);
return rt;
}
int que(int rt , int l , int r , ll x , ll y , ll dx){
if(!rt || l > y || r < x)return 0;
if(x <= l && r <= y)return a[rt].T.rank(dx);
int zz = 0 , mid = (l + r) >> 1;
zz = zz + que(a[rt].lc , l , mid , x , y , dx);
zz = zz + que(a[rt].rc , mid + 1 , r , x , y , dx);
return zz;
}
int que3(int rt , int l , int r , ll x , ll y , ll dx){
if(!rt || l > y || r < x)return 0;
if(x <= l && r <= y)return a[rt].T.rank2(dx);
int zz = 0 , mid = (l + r) >> 1;
zz = zz + que3(a[rt].lc , l , mid , x , y , dx);
zz = zz + que3(a[rt].rc , mid + 1 , r , x , y , dx);
return zz;
}
int change(int rt , int l , int r , ll x , ll y){
if(!rt || l > x || r < x)return 0;
if(l == r){
a[rt].T.del(b[x]);
a[rt].T.add(y);
return 0;
}
a[rt].T.del(b[x]);
a[rt].T.add(y);
int mid = (l + r) >> 1;
change(a[rt].lc , l , mid , x , y);
change(a[rt].rc , mid + 1 , r , x , y);
}
ll que1(int rt , int l , int r , ll x , ll y , ll dx){
if(!rt || l > y || r < x)return -2147483647;
ll zz = -2147483647;
if(x <= l && r <= y){
a[rt].T.add(dx);
zz = t[a[rt].T.pre()].val;
a[rt].T.del(dx);
if(!zz)return -2147483647;
return zz;
}
int mid = (l + r) >> 1;
zz = max(zz , que1(a[rt].lc , l , mid , x , y , dx));
zz = max(zz , que1(a[rt].rc , mid + 1 , r , x , y , dx));
return zz;
}
ll que2(int rt , int l , int r , ll x , ll y , ll dx){
if(!rt || l > y || r < x)return 2147483647;
ll zz = 2147483647;
if(x <= l && r <= y){
a[rt].T.add(dx);
zz = t[a[rt].T.nxt()].val;
a[rt].T.del(dx);
if(!zz)return 2147483647;
return zz;
}
int mid = (l + r) >> 1;
zz = min(zz , que2(a[rt].lc , l , mid , x , y , dx));
zz = min(zz , que2(a[rt].rc , mid + 1 , r , x , y , dx));
return zz;
}
int main(){
freopen("P3380_2 (1).in" , "r" , stdin);
freopen("out.txt" , "w" , stdout);
cin>>n>>m;
for(int i = 1 ; i <= n ; i++)cin>>b[i];
ll pp = build(0 , 1 , n);
ll tp,l,r,k;
ll x = 0 , y = 1e8 , mid , best , zz;
for(int i = 1 ; i <= m ; i++){
cin>>tp;
if(tp == 1){
cin>>l>>r>>k;
cout<<que(pp , 1 , n , l , r , k) + 1<<endl;
}
if(tp == 2){
cin>>l>>r>>k;
x = 0 , y = 1e8 , mid , best , zz;
while(x <= y){
mid = (x + y) >> 1;
zz = que(pp , 1 , n , l , r , mid);
if(zz >= k){
best = mid;
y = mid - 1;
}
else x = mid + 1;
}
cout<<best<<endl;
}
if(tp == 3){
cin>>l>>r;
change(pp , 1 , n , l , r);
b[l] = r;
}
if(tp == 4){
cin>>l>>r>>k;
cout<<que1(pp , 1 , n , l , r , k)<<endl;
}
if(tp == 5){
cin>>l>>r>>k;
cout<<que2(pp , 1 , n , l , r , k)<<endl;
}
}
}