"BZOJ4769" super-zhen fish merge sort to seek reverse order

Source: Internet
Author: User

"BZOJ4769" super-Virgin Fish description Madagascar is a magical two-legged zhen fish, they put their wisdom on the feet-each of the fish's left foot and the right foot each have a number. One day, K-only fish mood cramps, lined up, from left to right, I only can be in the right foot to write Ai, left foot on I; the second year, this K-only fish with the number of the right foot as the first key word, the number of the left foot for the second keyword, from small to large row into a column. Then they decided to renumber, from left to right, I-only Jeong-so will write the number of left foot on the right foot, write I on the left foot, and the third year, they rearrange, renumber by the method of the second year ... N years later, for left-to-right and J-I<j, if the number of the right foot of the I-only-jeong fish is larger than the number on the right foot of the J-Jeong Fish, it is called a pair of "super-chaste fish". How many are there for "super-virgin fish"? Input a total of 3 lines, the first line a positive integer k, indicating that there is K-only jade fish. The second row is a K positive integer, and the number of I represents AI. The third line is a non-negative integer n, which represents the number of years. K≤2x10^6, ai≤10^9,n≤10^18output An integer that represents the logarithm of "super-chaste fish". Sample Input6
5 2 6 3 1 7
0Sample Output7

The problem: the previous brush of the water to re-test a pitch to the Tle ~

Easy to find, no matter how many years, the original sequence of the reverse logarithm is constant, so directly to reverse order.

But with the sort + Tree array will be the card constant, so to use the merge sort to find the reverse. It's supposed to be CDQ, right?

In Solve (L,R), the first solve (L,mid) and (Mid+1,r), so that both sides are lined up, you just need to count the left and right to produce the inverse logarithm, with two pointers on both sides sweep and statistical answer can be.

#include <cstdio> #include <cstring> #include <iostream>using namespace Std;typedef long ll; Inline char nc () {    static char buf[100000],*p1=buf,*p2=buf;    Return p1==p2&& (p2= (p1=buf) +fread (Buf,1,100000,stdin), P1==P2)? eof:*p1++;} inline Int Rd () {    char ch=nc (); int sum=0;    while (!) ( ch>= ' 0 ' &&ch<= ' 9 ')) Ch=nc ();    while (ch>= ' 0 ' &&ch<= ' 9 ') sum=sum*10+ch-48,ch=nc ();    return sum;} int n,v[2000010],p[2000010];ll ans;void Solve (int l,int r) {if (l==r) return; int mid= (L+R) >>1,i=l,h1=l,h2=mid+1; Solve (L,mid), Solve (mid+1,r); for (i=l;i<=r;i++) {if (h1<=mid&& (h2>r| | V[H1]<=V[H2]) ans+=h2-mid-1,p[i]=v[h1++];elsep[i]=v[h2++];} for (i=l;i<=r;i++) v[i]=p[i];} int main () {n=rd (); int i;for (i=1;i<=n;i++) v[i]=rd () Solve (1,n);p rintf ("%lld", ans); return 0;}

"BZOJ4769" super-zhen fish merge sort to seek reverse order

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.