hdu 5107 K-short Problem(線段樹),hdu5107

來源:互聯網
上載者:User

hdu 5107 K-short Problem(線段樹),hdu5107

題目連結:hdu 5107 K-short Problem

題目大意:有N個點,M次詢問,每次詢問點X,Y,K,表示在點集合{(x,y)|x≤X,y≤Y}中高度第K小的值是多少,沒有的

話輸出-1。

解題思路:線段樹,每個節點維護10個高度(因為K最大為10),將詢問和點按照x,y的大小排序,從左向右,從下向

上,每次詢問就查詢[0,idx(y)]即可。注意如果詢問和點的位置相同,要先插入點。

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 60005;struct node {    int n, high[15];    node() { n = 0; }    void insert(int h) {        if (n < 10)            high[n++] = h;        else if (h < high[n-1])            high[n-1] = h;        sort(high, high + n);    }};#define lson(x) ((x)<<1)#define rson(x) (((x)<<1)|1)int lc[maxn << 2], rc[maxn << 2];node nd[maxn << 2];inline node merge (node a, node b) {    node ret;    int p = 0, q = 0;    while ((p < a.n || q < b.n) && ret.n <= 10) {        if (q >= b.n || (p < a.n && a.high[p] < b.high[q]) )            ret.high[ret.n++] = a.high[p++];        else            ret.high[ret.n++] = b.high[q++];    }    return ret;}void build(int u, int l, int r) {    lc[u] = l;    rc[u] = r;    if (l == r) {        nd[u].n = 0;        return ;    }    int mid = (l + r) >> 1;    build(lson(u), l, mid);    build(rson(u), mid + 1, r);    nd[u] = merge(nd[lson(u)], nd[rson(u)]);}void insert (int u, int x, int h) {    if (x == lc[u] && rc[u] == x) {        nd[u].insert(h);        return;    }    int mid = (lc[u] + rc[u]) >> 1;    if (x <= mid)        insert(lson(u), x, h);    else        insert(rson(u), x, h);    nd[u] = merge(nd[lson(u)], nd[rson(u)]);}node query(int u, int l, int r) {    if (l <= lc[u] && rc[u] <= r)        return nd[u];    node ret;    int mid = (lc[u] + rc[u]) >> 1;    if (l <= mid)        ret = merge(ret, query(lson(u), l, r));    if (r > mid)        ret = merge(ret, query(rson(u), l, r));    return ret;}struct state {    int x, y, h, id;}cmd[maxn + 5];vector<int> pos, tmp;int N, M, T, ans[30005];void init () {    T = N + M;    pos.clear();    tmp.clear();    int x, y, h;    for (int i = 0; i < N; i++) {        scanf("%d%d%d", &x, &y, &h);        tmp.push_back(y);        cmd[i] = (state) {x, y, h, 0};    }    for (int i = 0; i < M; i++) {        scanf("%d%d%d", &x, &y, &h);        tmp.push_back(y);        cmd[i+N] = (state) {x, y, h, i+1};    }    sort(tmp.begin(), tmp.end());    pos.push_back(tmp[0]);    for (int i = 1; i < tmp.size(); i++) {        if (tmp[i] != tmp[i-1])            pos.push_back(tmp[i]);    }    build(1, 0, pos.size()-1);}inline int find(int x) {    return lower_bound(pos.begin(), pos.end(), x) - pos.begin();}inline bool cmp (const state& a, const state& b) {    if (a.x != b.x)        return a.x < b.x;    if (a.y != b.y)        return a.y < b.y;    return a.id < b.id;}void solve () {    sort(cmd, cmd + T, cmp);    for (int i = 0; i < T; i++) {        if (cmd[i].id) {            node tmp = query(1, 0, find(cmd[i].y));            int kth = cmd[i].h;            ans[cmd[i].id] = (tmp.n >= kth ? tmp.high[kth-1] : -1);        } else            insert(1, find(cmd[i].y), cmd[i].h);    }}int main () {    while (scanf("%d%d", &N, &M) == 2) {        init();        solve();        for (int i = 1; i <= M; i++)            printf("%d\n", ans[i]);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.