POJ 2104 k-th Number (Chairman tree)

Source: Internet
Author: User

Topic Link: Click to open the link

See the code for details:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long long ll;typedef long double ld;const ld EPS = 1e-9, PI = 3.14159265358979323846264 33832795;const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;const int seed = 131;const ll INF64 = ll (1e18); const in T MAXN = 1e5 + 10;int n,m,cnt,root[maxn],a[maxn],x,y,k;struct Node {int L, r, sum;} T[MAXN * 40];vector<int> v;int getid (int x) {return lower_bound (V.begin (), V.end (), X)-V.begin () + 1;}    void Update (int l, int r, int& x, int y, int pos) {t[++cnt] = T[y];    t[cnt].sum++;    x = CNT;    if (L = = r) return; int mid = (L + R) &Gt;> 1;    If (Mid >= pos) update (L, Mid, T[x].l, T[Y].L, POS); else Update (mid+1, R, T[X].R, T[Y].R, POS);}    int query (int l, int r, int x, int y, int k) {if (L = = r) return l;    int mid = (L + r) >> 1;    int sum = t[t[y].l].sum-t[t[x].l].sum;    if (sum >= k) return query (L, Mid, t[x].l, T[Y].L, K); else return query (mid+1, R, T[X].R, T[Y].R, k-sum);} int main () {while (~scanf ("%d%d", &n,&m)) {for (int i = 1; I <= n; i++) scanf ("%d", &a[i]), V.push_        Back (A[i]); Sort (V.begin (), V.end ());        V.erase (Unique (V.begin (), V.end ()), V.end ());        for (int i = 1; I <= n; i++) update (1, N, Root[i], root[i-1], GetID (A[i]));            while (m--) {scanf ("%d%d%d", &x,&y,&k);        printf ("%d\n", V[query (1, N, Root[x-1],root[y], K)-1]); }} return 0;}


POJ 2104 k-th Number (Chairman 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.