QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#812401 | #7248. Ivan Dorn | Aurore | WA | 4ms | 23672kb | C++23 | 3.2kb | 2024-12-13 15:01:57 | 2024-12-13 15:01:57 |
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 premx[MAXN],sufmx[MAXN];
int mp[MAXN];
void init(){
for(int i=1;i<=n;i++){
if(mp[a[i]])
premx[i]=query(mp[a[i]],i);
mp[a[i]]=i;
}
memset(mp,0,sizeof(mp));
for(int i=n;i;i--){
if(mp[a[i]])
sufmx[i]=query(i,mp[a[i]]);
else mp[a[i]]=i;
}
}
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&&premx[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&&sufmx[i]<=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);
}
mt19937 rd(time(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]);
init();
B=sqrt(n);
B+=rd()%B+1;
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]]&&premx[j]<=a[j])
g[a[j]]+=j-f[a[j]];
else g[a[j]]=0;
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: 4ms
memory: 16456kb
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: 2ms
memory: 19028kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 19568kb
input:
2 1 1 2 1 2
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 20840kb
input:
2 1 1 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 4ms
memory: 19012kb
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: 0ms
memory: 18160kb
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: 18988kb
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: 0ms
memory: 22092kb
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: 0
Accepted
time: 0ms
memory: 23672kb
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 1 2 5 1 5
result:
ok 10 numbers
Test #10:
score: 0
Accepted
time: 0ms
memory: 19472kb
input:
10 10 962186504 962186504 962186504 766659307 396032294 766659307 396032294 962186504 962186504 962186504 5 7 3 5 3 5 3 6 3 7 4 10 1 6 3 8 9 9 2 6
output:
0 0 0 2 2 2 2 5 0 2
result:
ok 10 numbers
Test #11:
score: -100
Wrong Answer
time: 0ms
memory: 18620kb
input:
1000 1000 10 9 4 2 7 7 5 6 8 5 2 3 5 1 10 5 7 5 2 1 9 3 9 3 10 1 10 4 9 10 1 4 2 6 2 7 1 10 2 7 2 1 4 4 10 3 2 7 4 1 10 8 3 3 3 7 7 4 9 10 8 8 2 6 2 9 8 7 10 6 6 9 5 9 4 1 4 9 2 7 1 1 7 5 5 6 8 9 8 9 2 6 7 4 5 2 6 6 9 5 4 3 6 6 9 4 1 5 8 1 7 1 1 4 1 3 5 10 1 8 3 4 7 1 10 3 7 7 8 8 9 1 9 3 7 2 9 6 1 ...
output:
104 827 728 149 190 375 111 224 357 245 274 120 259 284 39 475 637 126 633 9 634 186 481 426 111 396 117 304 356 241 322 17 13 39 123 182 3 29 31 559 73 377 324 428 53 499 668 808 367 384 47 269 358 38 402 295 393 46 338 506 17 94 198 420 530 30 66 111 380 0 457 75 337 250 750 209 239 319 94 603 114...
result:
wrong answer 20th numbers differ - expected: '33', found: '9'