Why does the Cow cross the Road III (tree-like array)

Source: Internet
Author: User

Why do the Cow cross the Road III time limit: 1 Sec memory limit: MB
Submit: $ 28 Resolution:
Submitted State [Discussion Version] Title Description

The layout of Farmer John ' s farm is quite peculiar, with a large circular road running around the perimeter of the main fi Eld on which he cows graze during the day. Every morning, the cows cross this road on their it towards the field, and every evening they all cross again as they lea ve the field and return to the barn.
As we know, cows is creatures of habit, and they each cross the road the same is every day. Each cow crosses to the field at a different point from where she crosses out of the field, and all of the These crossing PO INTs is distinct from each-other. Farmer John ownsNCows, conveniently identified with the integer IDs1...N, so there is precisely 2 n crossing points around the road. Farmer John records These crossing points concisely by scanning around the circle clockwise, writing down the ID of the CO W for each crossing point, ultimately forming a sequence with  2 N numbers in which each number appears exactly twice. He does not record which crossing points is entry points and which are exit points.

Looking at his map of crossing points, Farmer John was curious how many times various pairs of cows might cross paths Durin G The day. He calls a pair of cows(a, B)  a "crossing" pair if Cow  a ' s path from entry to exit must cross Cow  b ' s path from entry to exit. Farmer John count The total number of crossing pairs.

Enter the first line of input containsN(1≤ n≤ 50, , and the Next 2n lines describe the cow IDs for the sequence of entry and exit points around the field. Output Please print the total number of crossing pairs. Sample input
432441321
Sample output
3
"Test Instructions" in a circle, clockwise gives some numbers, each number appears two times, and then the same number of connected edges, ask how many pairs of edges intersect.
"Analysis" We can find the order of the data according to the topic, if the two number of lines intersect, then they two in one-dimensional connection, that is, if a number two times appear
The number of places in the middle of the number appeared once, then how many lines to intersect with him, then we directly for each number of statistics two locations between the occurrence of a single count on the line.
For such a large amount of data can not be solved by the N-side statistical problems, tree array is generally possible. For this question, from left to right sweep, when the first sweep to this number, from this position
Up lowbit in order +1, when the second sweep to this number, the first occurrence of the statistic position to the current position of the sum value, and then from the first occurrence of the position upward lowbit
1, in turn, to delete this edge.
#include <bits/stdc++.h>#defineINF 0x3f3f3f3f#defineMet (b) memset (a,b,sizeof a)#definePB Push_back#defineMP Make_pair#defineINF 0x3f3f3f3fusing namespaceStd;typedefLong Longll;Const intN = 1e5+ -;Const intMoD = 1e9+7;Const DoublePi= ACOs (-1.0); typedef pair<int,int>PII;intN,ans;intA[n],sum[n],vis[n];voidUpdintXintadd) {     for(inti=x;i<=2*n;i+=i& (-i)) {Sum[i]+=add; }}intQryintx) {    intret=0;  for(inti=x;i>=1;i-=i& (-i)) {ret+=Sum[i]; }    returnret;}intMain () {scanf ("%d",&N);  for(intI=1; i<=2*n;i++) {scanf ("%d",&A[i]); }     for(intI=1; i<=2*n;i++){        if(!Vis[a[i]]) {upd (i,1); Vis[a[i]]=i; }        Else {            intS=qry (I-1)-qry (Vis[a[i]); //printf ("i:%d ai:%d l:%d r:%d\n", I,a[i],qry (Vis[a[i]]), qry (i-1));ans+=s; UPD (Vis[a[i]),-1); }} printf ("%d\n", ans); return 0;}

Why does the Cow cross the Road III (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.