Hangzhou electric acm2665--kth number~~ partition tree

Source: Internet
Author: User

The meaning of the topic: to the point interval [a, b], find the number of K, and POJ2104, just the time limit on the Hdu 5000MS, with my method on the POJ, too, will time out.

The code of this problem, change the input of the main function, you can directly AC the POJ on the 2104.

This problem, with the division of the method, WR, Tangled up one night, finally gave up, really do not know where the wrong. Then the method of dividing tree was used to learn the establishment and search of the partition tree.

Partition tree: Mainly used to solve the sequence of the interval [a, b] on the K-large number, that is, the interval [a, b] from small to large order, the first K.

The main algorithm ideas are:

1, achievements: First sorted well stored in an array, and then found in the middle of the number, in the original array will be less than the number of intermediate values on the left, greater than the number on the right. Build a tree in this way. Record the number of points entering the left subtree, which will be used in later lookups.

The picture is stolen, there is no time to do it yourself.


2, find: Find the number of k on the interval [a, b]. If the number of children entering the left child is greater than or equal to K, go to the left subtree to find, if less than, then go to the right subtree lookup, find the number of k-s large, s for the interval [a, b] into the number of left subtree. Update interval [A, b].


The code can be used as a template.


#include <iostream> #include <cstdio> #include <algorithm>using namespace std;const int M = 100010;    Class Node{public:int num[m];    The position enters the number of left child int val[m]; The value of the current position}t[30];int sorted[m];void build (int left, int. right, int p) {if (left = right) Return;int I, Mid = (left + right)/ 2;int isame = Mid-left + 1;/*isame is used to determine if there is the same element as Sorted[mid] starting with the assumption that there are mid-left + 1, minus the smaller than Sorted[mid], the rest is to insert the left child's */for (i = le Ft I <= right; i++)//judgment {if (T[p].val[i] < sorted[mid]) isame--;} int L = left, r = Mid + 1;for (i = left; I <= right; i++) {if (i = = left) T[p].num[i] = 0;elset[p].num[i] = t[p].num[i-1] if (T[p].val[i] < Sorted[mid])//Less than, go to left child {t[p].num[i]++;t[p + 1].val[l++] = T[p].val[i];} else if (T[p].val[i] > Sorted[mid])//greater than, go to right child t[p + 1].val[r++] = t[p].val[i];else//equals, determine if there is equality, no, enter right child, have , go to left child {if (isame) {isame--;t[p].num[i]++;t[p + 1].val[l++] = T[p].val[i];} Else{t[p + 1].val[r++] = T[p].val[i];}}} Build (left, Mid, p + 1); Build (mid + 1, right, p + 1);} int query(int a, int b, int k, int p, int left, int. right) {if (a = = B) return t[p].val[a];int S, ss, Mid = (left + right)/2;/* s for the interval [A, a] enter the number of children of SS for the interval [left, A-1] Enter the number of children, the following can also be seen Come out */if (a = = left) {s = t[p].num[b];ss = 0;} Else{s = t[p].num[b]-t[p].num[a-1];ss = T[p].num[a-1];}           if (s >= k) {a = left + ss; B = left + SS + S-1;return query (A, B, K, p + 1, left, mid);} else//recursive right child, that is, to find the number of k-s, because there is an S in front of the left child {a = Mid + 1 + a-left-ss;b = mid + 1 + b-left-t[p].num[b];return query (a , B, K-s, p + 1, mid + 1, right);}} int main () {int T, n, m;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &m), for (int i = 1; I <= n; i++)//input, {scanf ("%d", &sorted[i]); T[0].val[i] = Sorted[i];}    Sort (sorted + 1, sorted + n + 1); build (1, n, 0); achievements int A, B, k;for (int j = 0; J < m; j + +)//Find {scanf ("%d%d%d", &a, &b, &k);p rintf ("%d\n", query (A, B, K, 0, 1, n));}} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hangzhou electric acm2665--kth number~~ partition tree

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.