QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#640665 | #7248. Ivan Dorn | dongyc666 | RE | 3ms | 17528kb | C++14 | 2.0kb | 2024-10-14 15:08:03 | 2024-10-14 15:08:03 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int NR=5e5+10;
const int INF=1e9;
int n,m,tot,a[NR],lg[NR],ans[NR],pre[NR],nxt[NR],buc[NR];
struct interval{
int maxv,l,r;
interval operator +(const interval &T)const{
interval res;res.maxv=max(maxv,T.maxv);
res.l=min((res.maxv==maxv)?l:INF,(res.maxv==T.maxv)?T.l:INF);
res.r=max((res.maxv==maxv)?r:0,(res.maxv==T.maxv)?T.r:0);
return res;
}
}f[NR][20];
struct task{
int l,r,id;
bool operator <(const task &T)const{
if(r!=T.r)return r<T.r;
if(l!=T.l)return l>T.l;
return id<T.id;
}
}t[NR<<2];
void init(){
for(int i=1;i<=n;i++)
f[i][0]=interval{a[i],i,i},lg[i]=lg[i>>1]+1;
for(int i=1;i<=19;i++)
for(int j=1;j+(1<<i)<=n+1;j++)
f[j][i]=f[j][i-1]+f[j+(1<<(i-1))][i-1];
}
interval query(int l,int r){
int k=lg[r-l+1]-1;
return f[l][k]+f[r-(1<<k)+1][k];
}
int c[NR];
int lowbit(int x){
return x&(-x);
}
void modify(int x,int y){
while(x<=n){
c[x]=max(c[x],y);
x+=lowbit(x);
}
}
int ask(int x){
int res=0;
while(x){
res=max(res,c[x]);
x-=lowbit(x);
}
return res;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
init();
for(int i=1;i<=n;i++){
if(buc[a[i]]&&query(buc[a[i]],i).maxv==a[i]){
pre[i]=buc[a[i]];
while(pre[i]!=pre[pre[i]])pre[i]=pre[pre[i]];
}
else pre[i]=i;
buc[a[i]]=i;
}
memset(buc,0,sizeof(buc));
for(int i=n;i>=1;i--){
if(buc[a[i]]&&query(i,buc[a[i]]).maxv==a[i]){
nxt[i]=buc[a[i]];
while(nxt[i]!=nxt[nxt[i]])nxt[i]=nxt[nxt[i]];
}
else nxt[i]=i;
buc[a[i]]=i;
}
for(int i=1;i<=m;i++)cin>>t[i].l>>t[i].r,t[i].id=i;
tot=m;
for(int i=1;i<=n;i++){
if(pre[i]!=i)t[++tot]=task{pre[i],i,0};
if(nxt[i]!=i)t[++tot]=task{i,nxt[i],0};
}
sort(t+1,t+1+tot);
for(int i=1;i<=tot;i++)
if(t[i].id){
auto tmp=query(t[i].l,t[i].r);
ans[t[i].id]=max(tmp.r-tmp.l,ask(n+1-t[i].l));
}
else modify(n+1-t[i].l,t[i].r-t[i].l);
for(int i=1;i<=m;i++)cout<<ans[i]<<'\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 15784kb
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: 3ms
memory: 16400kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 16844kb
input:
2 1 1 2 1 2
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 2ms
memory: 16056kb
input:
2 1 1 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 14604kb
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: 3ms
memory: 17528kb
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: 16156kb
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: 2ms
memory: 16304kb
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: 16576kb
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: -100
Runtime Error
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