过了#1 #11 #13 其他全wa define int long long 了
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e4+5;
long long n,m,cnt,t,tr[N<<2],tag[N<<2],ans[N];
struct node{
long long x,y,z,opt;
}q[N],lq[N],rq[N];
void plu(int l,int r,int x,int y,int now,int k){
if(l>=x&&r<=y){
tr[now]+=(r-l+1)*k;
tag[now]+=k;
return ;
}
int mid=l+r>>1;
if(tag[now]){
tag[now*2]+=tag[now],tag[now*2+1]+=tag[now];
tr[now*2]+=(mid-l+1)*tag[now];
tr[now*2+1]+=(r-mid)*tag[now];
tag[now]=0;
}
if(x<=mid){
plu(l,mid,x,y,now*2,k);
}
if(y>mid){
plu(mid+1,r,x,y,now*2+1,k);
}
tr[now]=tr[now*2]+tr[now*2+1];
}
int get(int l,int r,int x,int y,int now){
if(l>=x&&r<=y){
return tr[now];
}
int mid=l+r>>1,sum=0;
if(tag[now]){
tag[now*2]+=tag[now],tag[now*2+1]+=tag[now];
tr[now*2]+=(mid-l+1)*tag[now];
tr[now*2+1]+=(r-mid)*tag[now];
tag[now]=0;
}
if(x<=mid){
sum+=get(l,mid,x,y,now*2);
}
if(y>mid){
sum+=get(mid+1,r,x,y,now*2+1);
}
return sum;
}
void solve(int l,int r,int st,int en){
if(st>en){
return ;
}
if(l==r){
for(int i=st;i<=en;i++){
ans[q[i].opt]=l;
}
return ;
}
int mid=l+r>>1,lt=0,rt=0;
for(int i=st;i<=en;i++){
if(q[i].opt==0){
if(q[i].z>mid){
plu(1,n,q[i].x,q[i].y,1,1);
rq[++rt]=q[i];
}
else{
lq[++lt]=q[i];
}
}
else{
int sum=get(1,n,q[i].x,q[i].y,1);
if(sum>=q[i].z){
rq[++rt]=q[i];
}
else{
lq[++lt]=q[i];
}
}
}
for(int i=st;i<=en;i++){
if(q[i].opt==0&&q[i].z>mid){
plu(1,n,q[i].x,q[i].y,1,-1);
}
}
for(int i=1;i<=lt;i++){
q[st+i-1]=lq[i];
}
for(int i=1;i<=rt;i++){
q[st+lt+i-1]=rq[i];
}
solve(l,mid,st,st+lt-1);
solve(mid+1,r,st+lt,en);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
while(m--){
int o,l,r,c;
cin>>o>>l>>r>>c;
if(o==1){
q[++t].opt=0,q[t].x=l,q[t].y=r,q[t].z=c;
}
else{
q[++t].opt=++cnt,q[t].x=l,q[t].y=r,q[t].z=c;
}
}
solve(-N,N,1,t);
for(int i=1;i<=cnt;i++){
cout<<ans[i]<<"\n";
}
return 0;
}