Poj 2299 ultra-quicksort (line segment tree + discretization)

Source: Internet
Author: User

Address: poj 2299

This topic has been sorted by merging, and can be done by adding a line segment tree to discretization. Generally, the line segment tree times out.

The maximum number of this question is 10 ^ 10 to the power. It is obviously too large, but the subscript can be used. The total number of subscripts is only 50 W. You can add a value to the tree from the beginning of the number, and then count the number of numbers on the left when the subscript is smaller than it. Because when you add a number, the number greater than this number has been added. At this time, the coordinate on the left is a pair of reverse orders.

But there is another problem with this question, that is, the repetition of numbers. At this time, the subscript can be placed before the sorting, which ensures that there will be no duplicates in the number of subscripts smaller than the subscripts.

Note that _ int64 is used.

The Code is as follows:

# Include <iostream> # include <cstdio> # include <string> # include <cstring> # include <stdlib. h> # include <math. h> # include <ctype. h> # include <queue> # include <map> # include <set> # include <algorithm> using namespace STD; # define ll _ int64 # define lson L, mid, RT <1 # define rson Mid + 1, R, RT <1 | 1ll sum [2100000]; struct node {ll X, ID;} Fei [600000]; bool CMP (node X, node y) {If (X. X = y. x) return X. id> Y. ID; return X. X> Y. x;} void pushup (int rt) {sum [RT] = sum [RT <1] + sum [RT <1 | 1];} void Update (ll x, int L, int R, int RT) {If (L = r) {sum [RT] ++; return;} int mid = L + r> 1; if (x <= mid) Update (x, lson); else Update (x, rson); pushup (RT);} ll query (int ll, int RR, int l, int R, int RT) {If (LL <= L & RR> = r) {return sum [RT];} ll ans = 0; int mid = L + r> 1; if (LL <= mid) ans + = query (LL, RR, lson); If (RR> mid) ans + = query (LL, RR, rson); Return Ans;} int main () {int N, I, j; ll ans; while (scanf ("% d", & N )! = EOF & N) {memset (sum, 0, sizeof (SUM); for (I = 1; I <= N; I ++) {scanf ("% i64d", & Fei [I]. x); Fei [I]. id = I;} Sort (FEI + 1, Fei + n + 1, CMP); ans = 0; for (I = 1; I <= N; I ++) {ans + = query (1, Fei [I]. ID, 1, n, 1); Update (FEI [I]. ID, 1, n, 1);} printf ("% i64d \ n", ANS);} return 0 ;}


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.