1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include<bits/stdc++.h>
using namespace std;
template<typename T>void read(T&x){x=0;int fl=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') fl=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}x*=fl;} template<typename T,typename...Args>inline void read(T&t,Args&...args){read(t);read(args...);}
typedef long long LL; typedef vector<int> vi; typedef pair<int, int> pii; #define mp(x, y) make_pair(x, y) #define pb(x) emplace_back(x)
const int N = 200005;
map<int, bool> isp;
inline int count(vector<int> &b, int l, int r){ auto ll = lower_bound(b.begin(), b.end(), l); auto rr = lower_bound(b.begin(), b.end(), r); return rr - ll + 1; }
inline int solve(vector<int> &a, vector<int> &b, int k){ int n = a.size(), m = b.size(); int res = 0; vector<int> sum(n+10), nega(n); for(int i = 0; i < n; i++) nega[i] = -a[i]; reverse(nega.begin(), nega.end()); for(int i = 0; i < n; i++) if(isp[k*a[i]]) sum[i]++; for(int i = n - 2; i >= 0; i--) sum[i] += sum[i+1]; for(int i = m - 1; i >= 0; i--){ auto ita = lower_bound(nega.begin(), nega.end(), -b[i]); int num = nega.end() - ita; res = max(res, count(b, b[i] - num + 1, b[i]) + sum[num]); } return res; }
int main(){ int T; for(read(T); T; T--){ isp.clear(); int n, m; read(n, m); vi a1, a2, b1, b2; for(int i = 1; i <= n; i++){ int a; read(a); if(a < 0) a1.pb(-a); else a2.pb(a); } for(int i = 1; i <= m; i++){ int b; read(b); isp[b] = true; if(b < 0) b1.pb(-b); else b2.pb(b); } reverse(a1.begin(), a1.end()); reverse(b1.begin(), b1.end()); printf("%d\n", solve(a1, b1, -1) + solve(a2, b2, 1)); } return 0; }
|