QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#468135 | #1163. Another Tree Queries Problem | zhao_daodao | TL | 0ms | 0kb | C++14 | 2.5kb | 2024-07-08 19:29:25 | 2024-07-08 19:29:26 |
answer
#include<bits/stdc++.h>
using namespace std;
namespace Read{
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
const int OUT=20;
static char outp[OUT];
int out;
template<typename T>inline void read(T& t){
int c=getchar(),f=1;t=0;
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c))t=t*10+c-48,c=getchar();t*=f;
}
template<typename T,typename ...Args>inline void read(T& t,Args&... args){
read(t),read(args...);
}
inline void write(short x){out=0; x<0&&(x=-x,putchar('-'));if(!x){ putchar(48); return; }while(x) outp[++out]=x%10+48,x/=10;while(out) putchar(outp[out--]);}
inline void write(signed x){out=0; x<0&&(x=-x,putchar('-'));if(!x){ putchar(48); return; }while(x) outp[++out]=x%10+48,x/=10;while(out) putchar(outp[out--]);}
inline void write(long long x){out=0; x<0&&(x=-x,putchar('-'));if(!x){ putchar(48); return; }while(x) outp[++out]=x%10+48,x/=10;while(out) putchar(outp[out--]);}
inline void write(char x){putchar(x);}
inline void write(const char* x){while(*x)putchar(*x++);}
template<typename T,typename ...Args>inline void write(T t,Args ...args){
write(t);write(args...);
}
}
using namespace Read;
const int MAXN=1e6+5;
int n,m,a[MAXN],b[MAXN];
int La[MAXN],Ra[MAXN],suma[MAXN];
int Lb[MAXN],Rb[MAXN],sumb[MAXN];
int cnta1,cnta3,cntb1,cntb3;
inline int Sa(int l,int r){return suma[r]-suma[l-1];}
inline int Sb(int l,int r){return sumb[r]-sumb[l-1];}
int ans;
inline void solve(){
cnta1=cnta3=cntb1=cntb3=ans=0;
read(n,m);
for(int i=1;i<=n;i++)read(a[i]);
for(int i=1;i<=m;i++)read(b[i]);
La[0]=0;for(int i=1;i<=n;i++)if(a[i]==1)La[++cnta1]=i;
Ra[0]=n+1;for(int i=n;i>=1;i--)if(a[i]==3)Ra[++cnta3]=i;
Lb[0]=0;for(int i=1;i<=m;i++)if(b[i]==1)Lb[++cntb1]=i;
Rb[0]=m+1;for(int i=m;i>=1;i--)if(b[i]==3)Rb[++cntb3]=i;
for(int i=1;i<=n;i++)suma[i]=suma[i-1]+(a[i]==2);
for(int i=1;i<=m;i++)sumb[i]=sumb[i-1]+(b[i]==2);
for(int i=0;i<=cnta1&&i<=cntb1;i++)
for(int j=0;j<=cnta3&&j<=cntb3;j++){
if(La[i]<Ra[j]&&Lb[i]<Rb[j]){
int now=i+j+min(Sa(La[i]+1,Ra[j]-1),Sb(Lb[i]+1,Rb[j]-1));
ans=max(ans,now);
}else break;
}
write(ans,'\n');
}
signed main(){
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int T;read(T);while(T--)solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
5 4 2 2 5 1 5 1 3 5 2 2 4 3 4 2 1 5 2 5 5 3 2