Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define fi first
- #define se second
- using namespace std;
- typedef long long i64;
- const int MAXN = 500010;
- int n;
- int ft[MAXN];
- void add(int idx, int val) {
- for (; idx <= n; idx += (idx & -idx)) {
- ft[idx] += val;
- }
- }
- int get(int idx) {
- int ans = 0;
- for (; idx > 0; idx -= (idx & -idx)) {
- ans += ft[idx];
- }
- return ans;
- }
- void add_range(int l, int r, int val) {
- l = max(1, l); r = min(r, n);
- add(l, val);
- add(r + 1, -val);
- }
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- cin >> n;
- vector<int> a(n + 1), b(n + 1);
- vector<pair<int, int>> ranges;
- for (int i = 1; i <= n; i++) {
- cin >> a[i];
- if (a[i])
- ranges.push_back({ max(1, i - a[i]), i });
- }
- sort(ranges.begin(), ranges.end());
- for (int i = 1; i <= n; i++) {
- cin >> b[i];
- add_range(i + 1, i + b[i], 1);
- }
- i64 ans = 0;
- int curr = 1;
- for (int i = 0; i < (int)ranges.size(); ++i) {
- while (curr <= n and curr < ranges[i].fi) {
- add_range(curr + 1, curr + b[curr], -1);
- ++curr;
- }
- ans += get(ranges[i].se);
- }
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment