Hdu 5412 CRB and Queries (segment tree set flute Karlshu-dynamic interval K-Large)

Source: Internet
Author: User

Title Links: Hdu 5412 CRB and Queries


First, all occurrences of the values are sorted, a segment tree is established, and each segment tree node is a Cartesian tree, and the Cartesian tree records interval subscript values.


#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm>using namespace std ; #define Lson (x) (x<<1) #define Rson (x) ((x<<1) | |) const INT MAXN = 2 * 1e5 + 5;const int inf = 0x3f3f3f3f;int I dx;struct Node {int key, Val, CNT, Siz, ch[2];} ND[MAXN * 20];int root[maxn << 2];void init () {idx = 0;//nd[idx].key = 0;nd[idx].val =-inf;nd[idx].cnt = Nd[idx].s iz = 0;//nd[idx].ch[0] = nd[idx].ch[1] = 0;} int newNode (int key) {Idx++;nd[idx].key = Key;nd[idx].val = rand (); nd[idx].cnt = Nd[idx].siz = 1;nd[idx].ch[0] = Nd[idx]. CH[1] = 0;return idx;} void maintain (int u) {nd[u].siz = Nd[nd[u].ch[0]].siz + Nd[nd[u].ch[1]].siz + nd[u].cnt;}  void rotate (int& u, int d) {int k = nd[u].ch[d];  ND[U].CH[D] = nd[k].ch[d^1];nd[k].ch[d^1] = u;maintain (u); maintain (k); U = k;} void Insert (int& u, int key) {if (U = = 0) u = newNode (key); else if (Nd[u].key = = key) Nd[u].cnt++;else {int d = nd[u] . Key < Key;insert (Nd[u].ch[d], key); if (Nd[u].val < nd[nd[u].ch[d]].val) Rotate (U, d);} Maintain (u);} void Erase (int& u, int key) {if (Nd[u].key = = key) {if (nd[u].cnt = = 1) {if (nd[u].ch[0] = = 0 && nd[u].ch[1] = = 0) {u = 0; return;} Rotate (U, nd[nd[u].ch[0]].val < nd[nd[u].ch[1]].val); Erase (U, key);} elsend[u].cnt--;} Elseerase (Nd[u].ch[nd[u].key < key], key); Maintain (u);} int count (int u, int p) {if (U = = 0) return 0;if (Nd[u].key > P) return count (Nd[u].ch[0], p); return nd[u].cnt + nd[nd [U].ch[0]].siz + count (nd[u].ch[1], p);} void modify (int u, int l, int r, int p, int x, int v) {//if (v < 0)//printf ("%d%d\n", L, R), if (V > 0) Insert (root  [u], x), Else erase (Root[u], x), if (L = = r) Return;int mid = (L + R) >> 1;if (P <= mid) Modify (Lson (U), L, Mid, p, x, v); else Modify (Rson (U), Mid + 1, r, p, X, v);} int query (int u, int l, int r, int x, int y, int k) {//printf ("%d%d\n", L, R), if (L = = r) Return l;int mid = (L + R) ;> 1;int siz = count (Root[lson (u)], y)-Count (Root[lson (U)], x); if (Siz >= k) return query (Lson (U), L, Mid, X, Y, K), Else return query (Rson (u), Mid + 1, r, X, Y, k-siz);} int N, M, Q, A[MAXN], B[MAXN], ORDER[MAXN], L[MAXN], R[MAXN], k[maxn];int find (int x) {return lower_bound (b, B + M, x)- B + 1;} int main () {while (scanf ("%d", &n) = = 1) {memset (root, 0, sizeof (root)), for (int i = 0; i < N; i++) scanf ("%d", &A mp A[i]); for (int i = 0; i < N; i++) b[i] = A[i]; M = n;scanf ("%d", &q), for (int i = 0; i < Q; i++) {scanf ("%d%d%d", &order[i], &l[i], &r[i]); if (order[i ] = = 1) b[m++] = r[i];elsescanf ("%d", &k[i]);} Init (); sort (b, B + M); M = unique (b, B + M)-b;for (int i = 0; i < N; i++) {Modify (1, 1, M, find (A[i]), i + 1, 1);} for (int i = 0; i < Q; i++) {if (order[i] = = 1) {Modify (1, 1, M, find (A[l[i]-1]), L[i], 1); A[l[i]-1] = r[i];modify (1, 1, M, find (A[l[i]-1]), L[i], 1);} else {int id = query (1, 1, M, L[i]-1, R[i], k[i]);p rintf ("%d\n", B[id-1]);}} return 0;}


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

Hdu 5412 CRB and Queries (segment tree set flute Karlshu-dynamic interval K-Large)

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.