Ultraviolet A 1428-Ping pong (tree array)

Source: Internet
Author: User

Ultraviolet A 1428-Ping pong (tree array)

Link to the question: Ultraviolet A 1428-Ping pong

There are n table tennis fans living in a street and they often organize competitions. Each person has a different ability value. Each game requires three people. The referee must live between two players and the ability value must also be between players, ask how many matches can be held at most.

Solution: Pre-processing bi and ci are represented in 1 ~ In I, the energy value is smaller than that in I ~ The energy value in n is smaller than that in I. You can use a tree array to maintain the processing process.

    #include using namespace std;#define lowbit(x) ((x)&(-x))const int maxn = 1e5;typedef long long ll;int n, s[maxn+5];int N, a[maxn+5];ll b[maxn+5];void add (int x, int v) {    while (x <= n) {        s[x] += v;        x += lowbit(x);    }}int sum (int x) {    int ret = 0;    while (x > 0) {        ret += s[x];        x -= lowbit(x);    }    return ret;}int main () {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%d", &N);        n = 0;        memset(s, 0, sizeof(s));        for (int i = 1; i <= N; i++) {            scanf("%d", &a[i]);            n = max(a[i], n);        }        for (int i = 1; i <= N; i++) {            b[i] = sum(a[i]-1);            //printf("%lld ", b[i]);            add(a[i], 1);        }        //printf("\n");        ll ans = 0;        memset(s, 0, sizeof(s));        for (int i = N; i > 0; i--) {            ll d = sum(a[i]-1);            add(a[i], 1);            ans += (b[i] * (N - i - d)) + d * (i - 1 - b[i]);        }        printf("%lld\n", ans);    }    return 0;}

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.