|
| 1 | +#include <cstdio> |
| 2 | +#include <cstring> |
| 3 | +#include <cstdlib> |
| 4 | +#include <cmath> |
| 5 | +#include <ctime> |
| 6 | +#include <cctype> |
| 7 | + |
| 8 | +#include <algorithm> |
| 9 | +#include <random> |
| 10 | +#include <bitset> |
| 11 | +#include <queue> |
| 12 | +#include <functional> |
| 13 | +#include <set> |
| 14 | +#include <map> |
| 15 | +#include <vector> |
| 16 | +#include <chrono> |
| 17 | +#include <iostream> |
| 18 | +#include <limits> |
| 19 | +#include <numeric> |
| 20 | + |
| 21 | +#define LOG(FMT...) fprintf(stderr, FMT) |
| 22 | + |
| 23 | +using namespace std; |
| 24 | + |
| 25 | +typedef long long ll; |
| 26 | +typedef unsigned long long ull; |
| 27 | + |
| 28 | +// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); |
| 29 | + |
| 30 | +template <class T> |
| 31 | +istream& operator>>(istream& is, vector<T>& v) { |
| 32 | + for (T& x : v) |
| 33 | + is >> x; |
| 34 | + return is; |
| 35 | +} |
| 36 | + |
| 37 | +template <class T> |
| 38 | +ostream& operator<<(ostream& os, const vector<T>& v) { |
| 39 | + if (!v.empty()) { |
| 40 | + os << v.front(); |
| 41 | + for (int i = 1; i < v.size(); ++i) |
| 42 | + os << ' ' << v[i]; |
| 43 | + } |
| 44 | + return os; |
| 45 | +} |
| 46 | + |
| 47 | +const int N = 100010, P = 1000000007; |
| 48 | + |
| 49 | +int n, m, cnt; |
| 50 | +int x[N], y[N], dx[N]; |
| 51 | +pair<int, int> a[N]; |
| 52 | +int fw[N]; |
| 53 | + |
| 54 | +void add(int& x, int y) { |
| 55 | + if ((x += y) >= P) |
| 56 | + x -= P; |
| 57 | +} |
| 58 | + |
| 59 | +int norm(int x) { return x >= P ? (x - P) : x; } |
| 60 | + |
| 61 | +int lowBit(int k) { return k & -k; } |
| 62 | + |
| 63 | +void ch(int k, int x) { |
| 64 | + for (; k <= cnt; k += lowBit(k)) |
| 65 | + add(fw[k], x); |
| 66 | +} |
| 67 | + |
| 68 | +int qry(int k) { |
| 69 | + int ret = 0; |
| 70 | + while (k) { |
| 71 | + add(ret, fw[k]); |
| 72 | + k &= k - 1; |
| 73 | + } |
| 74 | + return ret; |
| 75 | +} |
| 76 | + |
| 77 | +int main() { |
| 78 | +#ifdef LBT |
| 79 | + freopen("test.in", "r", stdin); |
| 80 | + int nol_cl = clock(); |
| 81 | +#endif |
| 82 | + ios::sync_with_stdio(false); |
| 83 | + cin.tie(nullptr); |
| 84 | + |
| 85 | + cin >> n >> m; |
| 86 | + for (int i = 1; i <= n; ++i) |
| 87 | + cin >> x[i]; |
| 88 | + for (int i = 1; i <= m; ++i) |
| 89 | + cin >> y[i]; |
| 90 | + for (int i = 1; i <= n; ++i) { |
| 91 | + if (x[i] < y[1] || x[i] > y[m]) |
| 92 | + continue; |
| 93 | + int id = lower_bound(y + 1, y + m + 1, x[i]) - y; |
| 94 | + a[++cnt] = make_pair(x[i] - y[id - 1], y[id] - x[i]); |
| 95 | + dx[cnt] = a[cnt].second; |
| 96 | + } |
| 97 | + int ans = 1; |
| 98 | + sort(dx + 1, dx + cnt + 1); |
| 99 | + sort(a + 1, a + cnt + 1, [](const auto& lhs, const auto& rhs) { return lhs.first == rhs.first ? (lhs.second > rhs.second) : (lhs.first < rhs.first); }); |
| 100 | + for (int i = 1; i <= cnt; ++i) { |
| 101 | + if (a[i] == a[i - 1]) continue; |
| 102 | + int pos = lower_bound(dx + 1, dx + cnt + 1, a[i].second) - dx; |
| 103 | + int cur = norm(qry(pos - 1) + 1); |
| 104 | + add(ans, cur); |
| 105 | + ch(pos, cur); |
| 106 | + } |
| 107 | + cout << ans << '\n'; |
| 108 | + |
| 109 | +#ifdef LBT |
| 110 | + LOG("Time: %dms\n", int ((clock() |
| 111 | + -nol_cl) / (double)CLOCKS_PER_SEC * 1000)); |
| 112 | +#endif |
| 113 | + return 0; |
| 114 | +} |
0 commit comments