Rokua P3660 [usaco17feb]why did the Cow cross the Road III G (tree-shaped array)

Source: Internet
Author: User

Topic background

Given a sequence of 2N in length, 1~n is now over 2 times, I first occurrence position as AI, second time as BI, to satisfy the logarithm of AI<AJ<BI<BJ

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 ownsNN cows, conveniently identified with the integer IDs1 \ldots N1 ... n, so there is Precisely 2N 2n 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 2n< Span class= "katex-html" > 2n 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 (b) (a,b) a "crossing" pair if cow aa ' s path from entry to exit must cross cow b< C20>b ' s path from entry to exit. Farmer John Count The total number of crossing pairs.

Input/output format

Input format:

The first line of input containsNn  (1 \leq N \leq 50,0001≤n≤< Span class= "Mord mathrm" >50,000), and the Next 2N< Span class= "katex-html" > 2n lines describe the cow IDs for the sequence of entry and exit Points around the field.

Output format:

Please print the total number of crossing pairs.

Input and Output Sample input example # #:
432441321
Sample # # of output:
3

Solution: This problem with a tree-like array can be done, the difficulty is general. The two positions of all the numbers are preprocessed first.

We can sum each interval to the left and right, and take the minimum value.

You can get the answer by accumulating. Well... A similar approach to reverse order

#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm>using namespace STD; #define MAN 50010#define lo (x) (x& (×)) #define READ (x) scanf ("%d", &x)/*test*/int n;struct node{int x, y;} E[man<<2];/*b_tree*/int c[man<<2];inline void Update (int x,int val) {while (x<=2*n) {C[x]+=val;x+=lo (x) ;} return;} inline int query (int x) {int Ans=0;while (x>0) {Ans+=c[x];x-=lo (x);} return ans;} inline int calc (int x,int y) {int l=query (x), int r=query (y), R=r-l;return min (l,r),//from 1 position to left endpoint, from left endpoint position +1 to right endpoint}/*sort*/ inline int cmp (node A,node b) {return a.x<b.x;} int main () {read (n); for (int i=1;i<=2*n;i++) {int tmp;read (TMP); if (e[tmp].x>0)     e[tmp].y=i;    

  

Rokua P3660 [usaco17feb]why did the Cow cross the Road III G (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.