4.24:
打由乃打扑克然后……
我招谁惹谁了啊!
加个代码,求神帮调
#include<bits/stdc++.h>
using namespace std;
#define infile(x) freopen(x,"r",stdin)
#define outfile(x) freopen(x,"w",stdout)
#define errfile(x) freopen(x,"w",stderr)
using ll=long long;using ull=unsigned long long;
#ifdef linux
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#endif
namespace IO{
template<typename T>inline bool read(T &x){x=0;char s=gc();bool f=true;for(;(s<'0'||'9'<s);s=gc()) {if(s=='-') f=false;if(s==EOF)return false;}for(;'0'<=s&&s<='9';s=gc()) x=(x<<1)+(x<<3)+(s^48);if(!f) x=~x+1;return true;}
inline bool read(double &x){x=0.0;char s=gc();bool f=true;for(;(s<'0'||'9'<s);s=gc()) {if(s=='-') f=false;if(s==EOF)return false;}for(;'0'<=s&&s<='9';s=gc()) x=(x*10)+(s^48);if(s!='.'){return true;}double res=0.1;s=gc();for(;'0'<=s&&s<='9';res/=10,s=gc()) x+=(s^48)*res;x=f?x:-x;return true;}
inline bool read(string &str){string res;str.swap(res);char s=gc();for(;s==' '||s=='\n';s=gc());if(s==EOF) return false; for(;s!=' '&&s!='\n'&&s!=EOF;s=gc())str.push_back(s);return true;}
inline bool read_line(string &str){string res;str.swap(res);char s=gc();for(;s==' '||s=='\n';s=gc());if(s==EOF) return false;for(;s!='\n'&&s!=EOF;s=gc()){str.push_back(s);}return true;}
inline bool read_line(char *str){int len=0;char s=gc();for(;s==' '||s=='\n';s=gc());if(s==EOF) return false;for(;s!='\n'&&s!=EOF;s=gc()){str[len]=s;len++;}str[len]='\0';return true;}
inline bool read(char &s){char x=gc();for(;x==' '&&x=='\n';x=gc());if(x==EOF||x==' '||x=='\n')return false;s=x;return true;}
inline bool read(char *s){int len=0;char x=gc();for(;x==' '||x=='\n';x=gc());if(x==EOF)return false;for(;x!=' '&&x!='\n'&&x!=EOF;x=gc())s[len++]=x;s[len]='\0';return true;}
template<class T,class... Args> inline bool read(T &x,Args&... args){return (read(x)&&read(args...));}
template<class T>inline void write(T x){static T st[45];int top=0;if(x<0)x=~x+1,pc('-');do{st[top++]=x%10;}while(x/=10);while(top)pc(st[--top]^48);}
inline void write(char x){pc(x);}
inline void write(string s){for(register int i=0;s[i];++i) pc(s[i]);}
inline void write(char *s){int len=strlen(s);for(int i=0;i<len;++i) pc(s[i]);}
inline void write(const char *s){int len=strlen(s);for(int i=0;i<len;++i) pc(s[i]);}
template<class T,class... Args> inline void write(T x,Args... args){write(x);write(args...);}
}using namespace IO;
const int N=1e5+10;
int n,m,a[N],add[N],L[N],R[N],pos[N],len,minn=INT_MAX,maxx=-INT_MAX;
vector<int> num[N];
inline void update(int l,int r,int k){
int p=pos[l],q=pos[r];
if(p==q){
vector<int> ().swap(num[p]);
for(int i=l;i<=r;++i) a[i]+=k;
for(int i=L[p];i<=R[p];++i) num[p].emplace_back(a[i]);
stable_sort(num[p].begin(),num[p].end());
minn=min(minn,num[p].front()+add[p]);
maxx=max(maxx,num[p].back()+add[p]);
return;
}
vector<int> ().swap(num[p]);
for(int i=l;i<=R[p];++i) a[i]+=k;
for(int i=L[p];i<=R[p];++i) num[p].emplace_back(a[i]);
stable_sort(num[p].begin(),num[p].end());
minn=min(minn,num[p].front()+add[p]);
maxx=max(maxx,num[p].back()+add[p]);
vector<int> ().swap(num[q]);
for(int i=L[q];i<=r;++i) a[i]+=k;
for(int i=L[q];i<=R[q];++i) num[q].emplace_back(a[i]);
stable_sort(num[q].begin(),num[q].end());
minn=min(minn,num[q].front()+add[q]);
maxx=max(maxx,num[q].back()+add[q]);
for(int i=p+1;i<q;++i) add[i]+=k,minn=min(num[i].front()+add[i],minn),maxx=max(num[i].back()+add[i],maxx);
}
inline bool check(int mid,int l,int r,int k,int more){
if(r<l) return more<k;
int p=pos[l],q=pos[r],res=more;
for(int i=p;i<=q;++i){
if(num[i].back()+add[i]<=mid){res+=num[i].size();continue;}
if(num[i].front()+add[i]>mid) continue;
res+=upper_bound(num[i].begin(),num[i].end(),mid-add[i])-num[i].begin();
}
return res<k;
}
inline int query(int l,int r,int k){
if(k>r-l+1) return -1;
if(k==1){
int res=INT_MAX,p=pos[l],q=pos[r];
if(p==q){
for(int i=l,ad=add[p];i<=r;++i) res=min(res,a[i]+ad);
return res;
}
for(int i=l,ad=add[p];i<=R[p];++i) res=min(res,a[i]+ad);
for(int i=L[q],ad=add[q];i<=r;++i) res=min(res,a[i]+ad);
for(int i=p+1;i<q;++i) res=min(res,num[i].front()+add[i]);
return res;
}
else if(k==r-l+1){
int res=-INT_MAX,p=pos[l],q=pos[r];
if(p==q){
for(int i=l,ad=add[p];i<=r;++i) res=max(res,a[i]+ad);
return res;
}
for(int i=l,ad=add[p];i<=R[p];++i) res=max(res,a[i]+ad);
for(int i=L[q],ad=add[q];i<=r;++i) res=max(res,a[i]+ad);
for(int i=p+1;i<q;++i) res=max(res,num[i].back()+add[i]);
return res;
}
int left=minn,right=maxx,ans=0;
vector<int> san;
int p=pos[l],q=pos[r];
if(p==q) for(int i=l,ad=add[p];i<=r;++i) san.emplace_back(a[i]+ad);
else{
for(int i=l,ad=add[p];i<=R[p];++i) san.emplace_back(a[i]+ad);
for(int i=L[q],ad=add[q];i<=r;++i) san.emplace_back(a[i]+ad);
}
stable_sort(san.begin(),san.end());
l=R[p]+1,r=L[q]-1;
while(left<=right){
int mid=(left+right)>>1;
int more=upper_bound(san.begin(),san.end(),mid)-san.begin();
if(check(mid,l,r,k,more)) left=mid+1;
else ans=mid,right=mid-1;
}
return ans;
}
signed main(){
#ifndef ONLINE_JUDGE
infile("in.in");outfile("out.out");
#else
#endif
read(n,m);
for(int i=1;i<=n;++i) read(a[i]);
len=1000;int siz=n/len;
for(int i=1;i<=len;++i) L[i]=R[i-1]+1,R[i]=i*siz;
if(R[len]<n) len++,L[len]=R[len-1]+1,R[len]=n;
for(int i=1;i<=len;++i){
for(int j=L[i];j<=R[i];++j){
num[i].emplace_back(a[j]);
pos[j]=i;
}
stable_sort(num[i].begin(),num[i].end());
minn=min(minn,num[i].front());
maxx=max(maxx,num[i].back());
}
while(m--){
int op,l,r,k;read(op,l,r,k);
if(op==1) write(query(l,r,k),'\n');
else update(l,r,k);
}
}
标签:int,pos,mid,2024,read,num,日记,define
From: https://www.cnblogs.com/hzoi-Cu/p/18155736