Розбір
Problem Author: | Illia Shevchenko |
Prepared by: | Maksym Shvedchenko Illia Shevchenko |
Editorial by: | Maksym Shvedchenko |
Let us iterate through the difference between and , which can range from to . We will shift the array by this difference. Now, the problem reduces to: given two arrays and , count the number of segments such that
We create an array such that . This simplifies the problem further to counting the number of segments such that
We construct a prefix sum array , where
This allows us to rewrite the sum over the segment as
Now, we iterate over the right boundary of the segment. The problem is reduced to counting the number of indices such that . This can be done in several ways, for example, by maintaining an array , where represents the count of prefix sums equal to .
Time complexity: or
Memory complexity:
#define int long long int log_ = 25; long long inf = 800000007ll; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> b(n); for (int i = 0; i < n; i++) cin >> b[i]; int ans = 0; for (int sd = -n; sd <= n; sd++) { vector<int> col(2 * n + 5); int p = n; col[p]++; for (int i = 0; i < n; i++) { int j = i + sd; if ((j < 0) or (j >= n)) continue; if (a[i] < b[j]) p++; if (a[i] > b[j]) p--; ans += col[p]; col[p]++; } } cout << ans; }