CF EDU E Question Tufurama "thinking + Tree Array"

Source: Internet
Author: User
Tags min

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 (); }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.