Static interval K-Large (split bucket method and square partition) __poj

Source: Internet
Author: User

POJ 2104 For example thought:

The method described in the challenge process design competition.

Split Bucket method: a row of goods or planes into a bucket, each barrel to maintain their own internal information, has achieved efficient computing purposes.

Set a total of n n number, each B is divided into a bucket, and the elements in the bucket sorting. For a given interval, find the number of numbers less than x for the bucket that is fully contained within the range, the direct binary finds the number of satisfies the condition, and the time for each barrel to process O (LOGB) O (logb). The remaining not completely distributed in the number of other buckets, one by one, each element processing requires O (1) O (1) time. As you can see, you should make the number of barrels a little less than the number of elements in the bucket.

If B=n−−√b=\sqrt N, then this is called the Square division, the lookup process time complexity is O (N−−√LOGN) o ({\sqrt n}logn), if B=nlogn−−−−−√b=\sqrt{nlogn}, then the complexity of O ( nlogn−−−−−√) O (\sqrt{nlogn}), plus the outermost two points, the overall time complexity is O (nlogn+mn−−√log1.5n) O (nlogn+m\sqrt{n}log^{1.5}n). Code:

#include <cstdio> #include <vector> #include <cmath> #include <algorithm> #include <iostream
> Using namespace std;
const int MAXN = 1e5+5, b = 1200;
int A[MAXN], num[maxn];//[) vector<int>v[maxn/b];
    int main (void) {int n, m;scanf ("%d%d", &n,&m);
    int b = Floor (sqrt (n));
        for (int i = 0; i < n; i++) {scanf ("%d", &a[i]);
        V[i/b].push_back (A[i]);
    Num[i] = A[i];
    Sort (num, num + N);
    for (int i = 0; I <= n/b i++) sort (V[i].begin (), V[i].end ());
    int L, R, Mid;
    int lo, RO, K, TL, tr;
        while (m--) {scanf ("%d%d%d", &tl,&tr,&k);
        int L = 0, r = N;
            while (L < r-1) {int c = 0;
            MID = L + (r-l)/2;
            Lo = tl-1, ro = tr;
            while (Lo<ro && lo%b!= 0) if (a[lo++] < Num[mid]) C + +;
            while (Lo<ro && ro%b!= 0) if (A[--ro] < Num[mid]) C + +; for (int i = lo/b I < Ro/b;
            i++) C +=lower_bound (V[i].begin (), V[i].end (), Num[mid])-V[i].begin ();
            if (c <= k-1) L = mid;
        else R = Mid;
    printf ("%d\n", Num[l]);
return 0; }//11000ms

The

Barrel method thought get, but this run is really a bit slow ...

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.