QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#468353 | #1281. Longest Common Subsequence | Drimpossible | RE | 0ms | 17908kb | C++17 | 1.5kb | 2024-07-08 20:23:28 | 2024-07-08 20:23:28 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
// #define int long long
const int N=1e6+5;
int n,m;
int a[N],b[N];
int la[N],ra[N],sa[N];
int lb[N],rb[N],sb[N];
int lowbit(int x){return x&-x;}
struct BIT{
int c[N<<1];
void clear(){
for(int i=1;i<=max(n,m)*2+1;i++)
c[i]=0;
}
void add(int p,int x){
p+=max(n,m)+1;
while(p<=max(n,m)*2+1)
c[p]=max(c[p],x),p+=lowbit(p);
}
int ask(int p){
p+=max(n,m)+1;
int res=0;
while(p)
res=max(res,c[p]),p-=lowbit(p);
return res;
}
}T1,T2;
void Solve(){
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i],la[i]=n+1,ra[i]=0;
for(int i=1;i<=m;i++)cin>>b[i],lb[i]=m+1,rb[i]=0;
ra[0]=n+1,rb[0]=m+1;
int tmp=0;
for(int i=1;i<=n;i++){
if(a[i]==1)
tmp++,la[tmp]=i;
sa[i]=sa[i-1]+(a[i]==2);
}
tmp=0;
for(int i=n;i>=1;i--)
if(a[i]==3)
tmp++,ra[tmp]=i;
tmp=0;
for(int i=1;i<=m;i++){
if(b[i]==1)
tmp++,lb[tmp]=i;
sb[i]=sb[i-1]+(b[i]==2);
}
tmp=0;
for(int i=m;i>=1;i--)
if(b[i]==3)
tmp++,rb[tmp]=i;
T1.clear(),T2.clear();
int i=0,ans=0;
for(int j=n;~j;j--){
if(ra[j]==0||rb[j]==0)
continue;
while(i+1<=n&&la[i+1]<ra[j]&&lb[i+1]<rb[j]){
i++;
T1.add(sa[la[i]]-sb[lb[i]],i-sb[lb[i]]);
T2.add(sb[lb[i]]-sa[la[i]],i-sa[la[i]]);
}
int val1=j+sb[rb[j]-1]+T1.ask(sa[ra[j]-1]-sb[rb[j]-1]);
int val2=j+sa[ra[j]-1]+T2.ask(sb[rb[j]-1]-sa[ra[j]-1]);
ans=max({ans,val1,val2});
}
cout<<ans<<'\n';
}
signed main(){
int T;
cin>>T;
while(T--)Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17908kb
input:
3 3 3 1 2 3 1 2 3 3 3 1 1 1 1 1 2 3 3 1 3 2 1 2 3
output:
3 2 2
result:
ok 3 tokens
Test #2:
score: -100
Runtime Error
input:
139889 1 10 2 2 1 2 2 3 1 1 2 3 3 7 1 3 2 3 3 1 1 2 2 6 1 2 1 3 1 1 1 1 8 7 3 1 3 3 2 2 3 1 3 2 2 1 3 3 3 10 3 3 2 1 1 2 2 1 1 1 1 3 1 1 5 2 1 2 1 3 1 1 2 1 4 1 3 3 3 3 7 2 3 1 2 1 2 2 3 3 2 6 2 3 1 1 2 1 3 1 3 1 4 1 3 1 1 3 4 2 2 3 2 3 1 3 1 8 1 1 1 3 1 3 3 3 1 4 1 1 3 3 3 1 10 10 3 1 2 1 2 2 2 2 1...
output:
4 6 1 5 3 2 0 4 2 1 3 1 4 8 2 2 1 3 2 4 5 4 1 3 2 1 3 2 3 4 4 4 3 4 4 5 5 2 3 4 1 1 3 1 1 5 5 4 1 4 2 4 1 7 2 1 4 1 3 4 4 2 3 4 5 2 3 3 2 2 4 4 3 2 2 3 2 4 4 3 2 2 5 2 6 4 1 3 3 4 5 4 1 4 1 6 2 4 1 4 6 4 3 2 1 4 4 5 2 4 3 5 1 5 2 3 2 3 0 4 2 4 4 4 4 6 1 1 1 4 6 2 4 3 2 4 3 5 3 1 5 6 6 2 3 4 3 4 3 3 ...