分块+暴力。
对于每一块来说。如果更新不是一整块,我们直接暴力重构,如果是一整块更新的话,就给这一个块打上一个标记。
当询问的时候,我们二分答案,对于不是一整块的区间,暴力。
对于是一整块的区间,我们优先判断他和标记的大小。如果标记更大,那么由于这一道题更新的时候,区间只取min,故块中的所有数据不会因为更新而变的更大,因此,我们直接在块里面进行二分得答案。(如果这个块不是一整块更新,那么一定被重构过)。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
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;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 8e4 + 10;
int n, m, base;
int belong[N];
int lf[N], rt[N];
int a[N];
int mx[N];
vector<int>vec[N];
void build()
{
int sz = belong[n];
upd(i, 1, sz)
{
upd(j, lf[i], rt[i])
{
vec[i].push_back(a[j]);
}
sort(vec[i].begin(), vec[i].end());
mx[i] = vec[i].back();
}
}
void rebuild(int pos)
{
vec[pos].clear();
upd(i, lf[pos], rt[pos])
{
a[i] = min(a[i], mx[pos]);
vec[pos].push_back(a[i]);
}
sort(vec[pos].begin(), vec[pos].end());
mx[pos] = vec[pos].back();
}
void update(int l,int r,int val)
{
int x = belong[l]; int y = belong[r];
if (x == y)
{
upd(i, l, r)
{
a[i] = min(a[i],val);
}
rebuild(x);
return;
}
upd(i, l, rt[x])
{
a[i] = min(a[i], val);
}
rebuild(x);
upd(i, lf[y], r)
{
a[i] = min(a[i], val);
}
rebuild(y);
up(i, x + 1, y)
mx[i] = min(val, mx[i]);
}
int query(int l, int r, int val)
{
int x = belong[l]; int y = belong[r];
int res = 0;
if (x == y)
{
upd(i, l, r)
{
if (a[i] <= val)
res++;
}
return res;
}
upd(i, l, rt[x]) {
if (a[i] <= val)res++;
}
upd(i, lf[y], r) {
if (a[i] <= val)res++;
}
up(i,x+1,y)
{
if (mx[i] <= val) {
res += rt[i] - lf[i] + 1;
}
else
{
int temp = upper_bound(vec[i].begin(), vec[i].end(), val)-vec[i].begin();
res += temp;
}
}
return res;
}
int judge(int l,int r,int k)
{
int tl = 0, tr = 1e9 + 1;
int x = belong[l]; int y = belong[r];
rebuild(x);
if (x != y)rebuild(y);
while (tr - 1 > tl)
{
int mid = (tr + tl) >> 1;
if (query(l, r, mid) >= k)tr = mid;
else tl = mid;
}
return tr;
}
int main()
{
n = read(); m = read();
upd(i, 1, n)a[i] = read();
base = sqrt(n) + 1;
memset(lf, INF, sizeof(lf));
memset(rt, 0, sizeof(rt));
upd(i, 1, n)
{
int pos= i / base + 1;
belong[i] = pos;
lf[pos] = min(lf[pos], i);
rt[pos] = max(rt[pos], i);
}
build();
int templ, tempr, tempval;
int op;
while (m--)
{
op = read();
if (op == 1)
{
templ = read(); tempr = read(); tempval = read();
update(templ, tempr, tempval);
}
else
{
templ = read(); tempr = read(); tempval = read();
printf("%d\n", judge(templ, tempr, tempval));
}
}
return 0;
}