G-Greater and Greater
题意:给定两个数组A和B,在A中找到一个子数组C使得
C
i
>
=
B
i
C_{i}>=B_{i}
Ci>=Bi,
∀
i
∈
[
1
,
m
]
\forall i \in[1,m]
∀i∈[1,m],这样的子数组存在几个
题解:
对A和B数组降序排列,并存下它们原本的位置id
设bitset<>res,对于每一个
B
i
B_{i}
Bi,res为1的位置
P
j
P_{j}
Pj表示
A
j
>
=
B
i
A_{j}>=B_{i}
Aj>=Bi,那么
P
j
−
B
i
.
i
d
+
1
P_{j}-B_{i}.id+1
Pj−Bi.id+1的位置就有可能作为子数组的起点,设setbit<>ans,初始化为1,当对于所有
B
i
B_{i}
Bi,该位置都可能作为子数组的起点的话,该位置对结果的贡献为1,ans对应的位置为1,只要有任何一个
B
i
B_{i}
Bi使得该位置不可以作为起点,该位置为0
代码中的注释可能会解决一些疑惑
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int, int>
#define pdd pair<db, db>
#define mem(a, b) memset(a, b, sizeof(a));
#define lowbit(x) (x & -x)
#define lrt nl, mid, rt << 1
#define rrt mid + 1, nr, rt << 1 | 1
template <typename T>
inline void read(T& t) {
t = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
t = t * 10 + ch - '0';
ch = getchar();
}
t *= f;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll Inf = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x7f7f7f7f;
const db eps = 1e-5;
const db Pi = acos(-1);
const int maxn = 15e4 + 10, maxm = 4e4 + 10;
pii an[maxn], bn[maxn];
bitset<maxn> ans, res;
int main(void) {
int n, m;
read(n), read(m);
for (int i = 1; i <= n; i++) {
int x;
read(x);
an[i] = {x, i};
}
for (int i = 1; i <= m; i++) {
int x;
read(x);
bn[i] = {x, i};
}
sort(an + 1, an + n + 1, greater<pii>());
sort(bn + 1, bn + m + 1, greater<pii>());
ans.set();
int p = 1;
for (int i = 1; i <= m; i++) {
/*an,bn数组均降序排序,若an[i]>=bn[j],则an[i]必然>=bn[j+1]
所以res可重复利用,大量降低复杂度
也正是这个原因,不能在储存an[i]的位置时直接移动*/
while (p <= n && an[p].first >= bn[i].first)
res.set(an[p++].second); /*该位置-bi.id+1的位置 可能作为起点
为避免对后续bi造成混乱,所以在接下来的按位与操作中右移*/
ans &= (res >> bn[i].second - 1);
}
printf("%d\n", ans.count());
return 0;
}