QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#812373 | #7248. Ivan Dorn | Aurore | WA | 3ms | 13900kb | C++23 | 2.9kb | 2024-12-13 14:50:43 | 2024-12-13 14:50:43 |
Judging History
answer
#include<bits/stdc++.h>
//#define int long long
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int buf[105];
inline void print(int x,char ch=' '){
if(x<0) putchar('-'),x=-x;
int tot=0;
do{
buf[++tot]=x%10;
x/=10;
}while(x);
for(int i=tot;i;i--) putchar(buf[i]+'0');
putchar(ch);
}
const int MAXN=5e5+5;
int n,m,B,a[MAXN],b[MAXN];
int st[MAXN][20],lg[MAXN];
int query(int l,int r){
int b=lg[r-l+1];
return max(st[l][b],st[r-(1<<b)+1][b]);
}
int ans[MAXN];
int f[MAXN],g[MAXN];
struct node{
int l,r,id;
node(int a=0,int b=0,int c=0){
l=a,r=b,id=c;
}
bool friend operator<(const node &a,const node &b){
return a.r<b.r;
}
};
vector<node> qry[MAXN];
#define pr(x,y) make_pair(x,y)
#define F first
#define S second
pair<int,int> pre[MAXN],suf[MAXN];
pair<int,int> tmp1[MAXN],tmp2[MAXN];
void solve(int mid){
sort(qry[mid].begin(),qry[mid].end());
int pos=mid,mx=0;
for(auto q:qry[mid]){
while(pos<=q.r){
if(suf[a[pos]].S&&query(suf[a[pos]].S,pos)<=a[pos]){
if(pre[a[pos]]==suf[a[pos]])
pre[a[pos]].S=suf[a[pos]].S=pos;
else
suf[a[pos]].S=pos;
}
else{
suf[a[pos]]=pr(pos,pos);
if(!pre[a[pos]].F) pre[a[pos]]=pr(pos,pos);
}
mx=max(mx,suf[a[pos]].S-suf[a[pos]].F);
pos++;
}
int cpy=mx;
for(int i=mid-1;i>=q.l;i--)
tmp1[a[i]]=pre[a[i]],tmp2[a[i]]=suf[a[i]];
for(int i=mid-1;i>=q.l;i--){
if(pre[a[i]].F&&query(i,pre[a[i]].F)<=a[i]){
if(pre[a[i]]==suf[a[i]])
pre[a[i]].F=suf[a[i]].F=i;
else
pre[a[i]].F=i;
}
else{
pre[a[i]]=pr(i,i);
if(!suf[a[i]].F) suf[a[i]]=pr(i,i);
}
mx=max(mx,pre[a[i]].S-pre[a[i]].F);
}
ans[q.id]=mx;
mx=cpy;
for(int i=mid-1;i>=q.l;i--)
pre[a[i]]=tmp1[a[i]],suf[a[i]]=tmp2[a[i]];
}
for(int i=1;i<=n;i++) pre[a[i]]=suf[a[i]]=pr(0,0);
}
signed main(){
n=read(),m=read();
for(int i=1;i<=n;i++) a[i]=b[i]=read();
sort(b+1,b+n+1);
for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+n+1,a[i])-b;
for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
for(int i=1;i<=n;i++) st[i][0]=a[i];
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
st[i][j]=max(st[i][j-1],st[i+(1<<j-1)][j-1]);
//B=sqrt(n);
B=n;
for(int i=1;i<=m;i++){
int l=read(),r=read();
if(r-l+1<=B){
for(int j=l;j<=r;j++){
if(f[a[j]]&&query(f[a[j]],j)<=a[j])
g[a[j]]+=j-f[a[j]];
f[a[j]]=j;
ans[i]=max(ans[i],g[a[j]]);
}
for(int j=l;j<=r;j++)
f[a[j]]=g[a[j]]=0;
}
else{
if((l-1)%B==0) qry[l].push_back(node(l,r,i));
else{
int id=((l-1)/B+1)*B+1;
qry[id].push_back(node(l,r,i));
}
}
}
for(int i=1;i<=n;i+=B)
solve(i);
for(int i=1;i<=m;i++) print(ans[i],'\n');
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 13888kb
input:
8 5 4 3 2 2 3 3 7 3 1 7 6 8 1 3 3 6 1 8
output:
4 0 0 1 4
result:
ok 5 number(s): "4 0 0 1 4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 13760kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 13820kb
input:
2 1 1 2 1 2
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 3ms
memory: 13900kb
input:
2 1 1 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 13820kb
input:
10 10 6 4 10 10 10 3 4 5 4 3 4 10 3 8 3 5 1 10 2 3 4 6 1 2 2 6 9 10 5 7
output:
1 2 2 2 0 1 0 2 0 0
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 2ms
memory: 9788kb
input:
10 10 4 10 1 4 1 1 4 1 4 10 8 10 1 7 5 9 5 9 3 7 9 10 4 10 5 10 2 3 2 8
output:
0 3 2 2 3 0 5 2 0 3
result:
ok 10 numbers
Test #7:
score: 0
Accepted
time: 0ms
memory: 13888kb
input:
10 10 5 6 6 6 6 5 6 6 6 5 1 10 2 9 3 9 3 10 5 10 3 7 2 4 1 1 1 3 3 9
output:
7 7 6 6 4 4 2 0 1 6
result:
ok 10 numbers
Test #8:
score: 0
Accepted
time: 3ms
memory: 13764kb
input:
10 10 8 8 8 8 8 8 8 8 8 8 3 10 6 7 3 4 3 10 2 8 6 8 9 10 9 10 2 3 1 2
output:
7 1 1 7 6 2 1 1 1 1
result:
ok 10 numbers
Test #9:
score: -100
Wrong Answer
time: 3ms
memory: 13888kb
input:
10 10 100 30 100 91 100 91 91 100 91 91 6 6 9 10 7 7 7 8 1 3 6 10 2 7 3 10 6 7 2 9
output:
0 1 0 0 2 2 2 5 1 5
result:
wrong answer 6th numbers differ - expected: '1', found: '2'