Uvalive 4329--ping pong+ tree-like array

Source: Internet
Author: User

Topic Link: Click to enter
Start by trying to enumerate the two contestants, and then ask for the number of judges that meet the requirements, but the complexity of time will not be able to meet the requirements of the topic. It is assumed that each person can be enumerated as a referee, assuming that the first player is the referee and then the left 1–i-1 Lmin[i] The rank of individual is lower than he, then there is i-1-lmin[i] individual rank is not lower than he, set his right i+1–n Rmin[i] individual rank is lower than him, then have n-i-rmin[i] individual rank is not lower than him. The number of games that can be held when the first contestant is a referee is lmin[i] (N-i-rmin[i]) +rmin[i] (I-1-lmin[i]). So the problem now is to find out the rmin and lmin of each player.
Here we can assume that there is a rank array c[], where c[i]=x indicates that there is an X person in the position of the first place. And now we're asking how many people on the left side of the I are ranked lower than it; we can insert the rank of all the players on the left side of it into this array and query the value of c[1]+c[2]+...+c[a[i]-1]. So all we have to do is query and update all the players from left to right, which can be done using a line tree or a tree-like array. For each player on the right side of the situation can also be treated similarly.

The code is as follows:

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace STD;#define MAXN 100010#define LL Long Longinta[maxn],c[maxn*4],m;intLMIN[MAXN],RMIN[MAXN];intLowbit (intx) {returnx& (-X);}intSumintx) {intret=0; while(x>0) {ret+=c[x];    X-=lowbit (x); }returnRET;}voidAddintXintD) { while(x<=m)        {C[x]+=d;    X+=lowbit (x); }}intMain () {//freopen ("In.txt", "R", stdin);       intTscanf("%d", &t); while(t--) {intN m=0;scanf("%d", &n); for(intI=1; i<=n;i++) {scanf("%d", &a[i]);             M=max (M,a[i]); }memsetC0,sizeof(C));memset(Lmin,0,sizeof(Lmin));memset(Rmin,0,sizeof(rmin)); for(intI=1; i<=n;i++) {Lmin[i]=sum (a[i]-1); Add (A[i],1); }memsetC0,sizeof(C)); for(intj=n;j>=1; j--) {Rmin[j]=sum (a[j]-1); Add (A[j],1); } ll ans=0; for(intI=2; i<n;i++) {ans+= (LL) lmin[i]* (N-i-rmin[i]); ans+= (LL) rmin[i]* (i-1-lmin[i]); }printf("%lld\n", ans); }return 0;}

Uvalive 4329--ping pong+ tree-like array

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.