Mu_leaf @ 2024-07-29 10:49:03
In:
8
5 6 7 6 2 3 4 3
1
0 1 4 7
我手模感觉是7,但我的代码和题解都输出6。
顺便帮蒟蒻调一下代码,谢谢dalao。
#include<bits/stdc++.h>
#define int long long
#define ls (tr[x].l)
#define rs (tr[x].r)
#define mid ((l+r)>>1)
using namespace std;
namespace IO
{
char buff[1<<21],*p1=buff,*p2=buff;
char getch(){return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;}
template<typename T>void read(T &x){char ch=getch();int fl=1;x=0;while(ch>'9'||ch<'0'){if(ch=='-')fl=-1;ch=getch();}while(ch<='9'&&ch>='0'){x=x*10+ch-48;ch=getch();}x*=fl;}
template<typename T>void read_s(T &x){char ch=getch();while(ch<'A'||ch>'Z')ch=getch();while(ch>='A'&&ch<='Z'){x+=ch;ch=getch();}}
template<typename T,typename ...Args>void read(T &x,Args& ...args){read(x);read(args...);}
char obuf[1<<21],*p3=obuf;
void putch(char ch) {if(p3-obuf<(1<<21))*p3++=ch;else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=ch;}
char ch[100];
template<typename T>void write(T x) {if(!x)return putch('0');if(x<0)putch('-'),x*=-1;int top=0;while(x)ch[++top]=x%10+48,x/=10;while(top)putch(ch[top]),top--;}
template<typename T,typename ...Args>void write(T x,Args ...args) {write(x);write(args...);}
void put(string s){for(int i=0;i<s.size();i++)putch(s[i]);}
void flush(){fwrite(obuf,p3-obuf,1,stdout);}
}
using namespace IO;
const int N=20005;
int cnt,root[N],a[N],n,m,p[N],q[5];
vector<int> Id[N];
struct SGT{
int l,r;
int sum,lc,rc;
}tr[N*100];
void pushup(int x){
tr[x].sum=tr[ls].sum+tr[rs].sum;
tr[x].lc =max(tr[ls].lc,tr[ls].sum+tr[rs].lc);
tr[x].rc =max(tr[rs].rc,tr[rs].sum+tr[ls].rc);
}
int change(int x,int l,int r,int k,int v){
++cnt;tr[cnt]=tr[x];
x=cnt;
if(l==r){
tr[x].sum=tr[x].lc=tr[x].rc=v;
return x;
}
if(k<=mid) ls=change(ls,l,mid,k,v);
else rs=change(rs,mid+1,r,k,v);
// cout << x << "\n";
pushup(x);
return x;
}
SGT query(int x,int l,int r,int lt,int rt){
if(lt>rt) return (SGT){0,0,0,0,0};
if(l>=lt && r<=rt) return tr[x];
SGT ans,pl,pr;
int f1=0,f2=0;
if(lt<=mid) pl=query(ls,l,mid,lt,rt),f1=1;
if(rt >mid) pr=query(rs,mid+1,r,lt,rt),f2=1;
if(f1 && f2){
// cout <<l << " " << r <<" " << pl.lc << "\n";
ans.lc=max(pl.lc,pl.sum+pr.lc);
ans.rc=max(pr.rc,pr.sum+pl.rc);
ans.sum=max(pl.sum,pr.sum);
}else if(f1) ans=pl;
else if(f2) ans=pr;
return ans;
}
#undef ls
#undef rs
#undef mid
signed main(){
read(n);
for(int i=1;i<=n;i++) read(a[i]),p[i]=a[i];
sort(p+1,p+n+1);
int cnt=unique(p+1,p+n+1)-p-1;
for(int i=1;i<=n;i++){
// cout << p[i] << " \n"[i==n];
a[i]=lower_bound(p+1,p+cnt+1,a[i])-p;
Id[a[i]].push_back(i);
}
for(int i=1;i<=n;i++) root[n+1]=change(root[n+1],1,n,i,-1);
// cout << 1 << "\n";
for(int i=n;i>=1;i--){
root[i]=root[i+1];
for(auto id:Id[i]) root[i]=change(root[i],1,n,id,1);
}
// cout << query(root[n],1,n,3,4).lc << "\n";
read(m);
int ans=0;
while(m--){
int a,b,c,d;
for(int i=0;i<4;i++) read(q[i]),q[i]=(q[i]+ans)%n;
sort(q,q+4);a=q[0],b=q[1],c=q[2],d=q[3];
a++,b++,c++,d++;
// cout << a << " " << b << " " << c << " " << d << "\n";
int l=1,r=n,res=-1;
while(l<=r){
int mid=l+r>>1;
SGT p1=query(root[mid],1,n,a,b);
SGT p2=query(root[mid],1,n,b+1,c-1);
SGT p3=query(root[mid],1,n,c,d);
// cout << p[mid] << " " << l << " " << r << " " <<p1.rc << " " << p2.sum << " " << p3.lc << "\n";
if(p1.rc+p2.sum+p3.lc>=0) res=mid,l=mid+1;
else r=mid-1;
}
write(ans=p[res]);
putch('\n');
// puts("---------FAQ--------");
}
flush();
}
by Mu_leaf @ 2024-07-29 10:49:32
代码是除了sub2的A了,其他在第一行就WA了
by Mu_leaf @ 2024-07-29 15:45:11
代码已过,但上面数据还是输出6,有没有dalao指点一二QAQ。
by DZDdzd @ 2024-07-30 16:50:17
我们考虑枚举各个区间
[ 0 , 4 ] 2,5,6,6,7 mid = 5
[ 0 , 5 ] 2,3,5,6,6,7 mid = 5
[ 0 , 6 ] 2,3,4,5,6,6,7 mid = 5
[ 0 , 7 ] 2,3,3,4,5,6,6,7 mid = 4
[ 1 , 4 ] 2,6,6,7 mid = 6
[ 1 , 5 ] 2,3,6,6,7 mid = 3
[ 1 , 6 ] 2,3,4,6,6,7 mid = 4
[ 1 , 7 ] 2,3,3,4,6,6,7 mid = 3
不难发现最大的是6,我们注意到每个区间的中位数总是经过排序的,而并非中间的数@Mu_leaf
by DZDdzd @ 2024-07-30 16:54:00
主席树好题
by Mu_leaf @ 2024-07-30 17:12:23
@DZDdzd thx.我唐完了。
by DZDdzd @ 2024-07-30 20:40:36
没事在梦熊北京营训练才是唐完了