Poj2481-tree Array application

Source: Internet
Author: User

//************************************** ***************************** *******
For this problem, which has two keywords and is monotonic, the general method for tree array optimization is to fix a keyword (or the endpoint of the interval ), then, query the first n items and Properties of another keyword to determine the answer to the current object.

1. first sort by S from small to big, if S is the same according to T from big to small sort (so that the previous impact after) use a tree array to query the number of more current than the E-1, pay special attention to handling the exact same interval.
// ************************************* This commonly used * ***************
2. The plug-in Q & amp; A template is the number of smaller numbers before this number.
For this question, t is first sorted in descending order. If they are the same, S is sorted in ascending order, and then the number of s smaller than the current value is sum (I ): that is, the number of smaller than I, and then modify (I );

Poj 2481 cows solution: similar to stars,
FJ has a nheaded ox (numbered 1 ~ N), each cow has a test value [s, E, when their test values meet the following conditions, it proves that Niu I is stronger than Niu J: Si <= SJ and j <= EI and EI-Si> ej-SJ. It is now known that each cow's test value is required to output the number of cows per cow to be stronger than its own.

Package tree array _ insert question segment ;//!!!!!!!!!! // When a property in the question is monotonically decreasing, I-sum (I-1) can be used to query a number larger than I in a tree array; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. io. outputstreamwriter; import Java. io. printwriter; import Java. io. streamtokenizer; import Java. util. arrays; public class poj2481two {static int max = 100010; static int C [] = new int [Max]; static int ans [] = new int [Max]; static d [] D; static streamtokenizer in = New streamtokenizer (New bufferedreader (New inputstreamreader (system. in); static printwriter out = new printwriter (New outputstreamwriter (system. out); Final Static int nextint () throws ioexception {In. nexttoken (); Return (INT) in. nval;} static int lowbit (int I) {int ans = I & (-I); Return ans;} // Insert the vertex question segment ,,,,,,,,, this template is used to query the number of smaller numbers before it. Static void modify (int K, int d) {While (k <= max) {C [k] + = D; K + = lowbit (k) ;}} static int Sum (int n) {int result = 0; while (n> 0) {result + = C [N]; n-= lowbit (n) ;}return result ;} public static void main (string [] ARGs) throws ioexception {int num; while (num = nextint ())! = 0) {arrays. fill (C, 0); arrays. fill (ANS, 0); D = new D [num]; for (INT I = 0; I <num; I ++) {d [I] = new D (nextint () + 1, nextint () + 1); // cannot start from scratch d [I]. index = I;} arrays. sort (D, 0, num); For (INT I = 0; I <num; I ++) {if (I> 0 & D [I]. S = d [I-1]. S & D [I]. E = d [I-1]. e) {// modify (COW [I]. s, 1); ans [d [I]. index] = ans [d [I-1]. index];} else {ans [d [I]. index] = I-sum (d [I]. e-1);} modify (d [I]. e, 1);} For (INT I = 0; I <num-1; I ++) out. print (ANS [I] + ""); out. println (ANS [num-1]);} Out. flush (); out. close () ;}} Class D implements comparable <D> {int s; int e; int index; Public D (int s, int e) {This. S = s; this. E = e ;}@ overridepublic int compareto (d o) {// todo auto-generated method stubif (this. S <O. s) Return-1; else if (this. S = O. s) {If (this. e> O. e) Return-1; elsereturn 1;} elsereturn 1 ;}}

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.