Transmission Door
Test instructions: Given n number, you need to find out how many subscript two tuples (x, y), meet x < Y, a[x] >= y, A[y] >= x.
Idea: This problem is very interesting, first we have to maintain a good a[y] >= x, this value, that is, we have x this subscript store the largest ID (Id < y), so A[id] >= x, this we preprocessed the second condition, then how we count the second condition, then To use a tree-like array, we also have to determine that one thing is a[i] >= N, you can have a[i] = N to deal with, this will not affect the answer, but also convenient to use the tree-like array. We insert a[i] from the beginning, and then calculate the answer for the largest of those IDs, which is Getsum (n)-getsum (id-1), so that all the numbers are processed so that the summation can get ans. Consider correctness, assuming that 1-5, 2-4, a[1] = 5, a[5] = 1, a[2] = 4, a[4] = 3, then when processing to 2 o'clock, the cumulative value should be 2, and the corresponding two-tuple is (1, 4), (2, 4), because 1 must meet the a[1] & Gt;=4, and because a[4] is certain >= 1, if a[4] < 1, he will be behind the i-1, not I behind ....
AC Code
const int maxn = 2e5+5; int C[MAXN], n; void add (int x) {for (; x <= n; x + = x &-X)
c[x]++;
} int getsum (int x) {int res = 1;
for (; x; x-= x & x) {res + = c[x];
} return res;
} vector<int>ve[maxn];
int A[MAXN];
void Solve () {while (~SCANF ("%d", &n)) {Fill (c, 0);
for (int i = 1; I <= n; i + +) {scanf ("%d", A + i);
A[i] = min (A[i], n);
Ve[min (I-1, A[i])].PB (i);
} ll ans = 0;
for (int i = 1; I <= n; i + +) {Add (A[i]);
for (int j = 0; J < sz (Ve[i]); j + +) {int p = ve[i][j];
Ans + = Getsum (n)-getsum (p-1);
}} printf ("%lld\n", ans);
for (int i = 1; I <= n; i + +) ve[i].clear (); }
}