Splay tree (interval k small)--poj 2761 Feed the Dogs

Source: Internet
Author: User
Tags spl

Corresponding POJ topic: Click to open link

Feed the Dogs
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 16655 Accepted: 5203

Description

Wind loves pretty dogs very much, and she had n pet dogs. So Jiajia have to feed the dogs every day for wind. Jiajia loves wind, and not the dogs, so Jiajia use a special the-to feed the dogs. At lunchtime, the dogs would stand on one line, numbered from 1 to N, the leftmost one is 1, the second one is 2, and so on . In each feeding, Jiajia choose a inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia have his own a-deciding, the pretty value of each dog. It should is noted that Jiajia does not want to feeds any position too much, because it may cause some death of dogs. If So, wind would be angry and the aftereffect would be serious. Hence Any feeding inteval won't contain another completely, though the intervals may intersect with each of the other.

Your task is to help Jiajia calculate which dog ate the food after each feeding.

Input

The first line contains N and m, indicates the number of dogs and the number of feedings.

The second line contains n integers, and describe the pretty value of each of the dog from left to right. You should notice, the dog with lower pretty, value is prettier.

Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding.

You can assume that n<100001 and m<50001.

Output

Output file has m lines. The i-th line should contain the pretty value of the dog, who got the food in the i-th feeding.

Sample Input

7 21 5 2 6 3 7 41 5 32 7 1

Sample Output

32

Test Instructions:

For n and M interval queries (there may be intersections between each query interval, but will not overwrite), the number of K small for each query output sequence.

Ideas:

It is said that various trees can be used.

To stretch the tree, first save all the queries, the left edge of the query in ascending order, and then the sort of each sany between the establishment of a separate tree, as for the first k small, you can directly calculate, when the next query interval, if there is no intersection, the whole tree is deleted from the new; You can delete the previous query interval without the intersection of the node, there is the intersection of the left, and then the current query interval without the intersection of the addition.

#include <cstdio> #include <cstdlib> #include <string> #include <algorithm> #include < string.h> #include <cmath> #include <iostream> #define MAX (x, y) ((x) > (y)? ( x):(y)) const int MAXN = 100100;using namespace Std;typedef int Type; Type A[MAXN], b[maxn>>1];struct q{int x, y, z, id; Q () {id = x = y = z = 0;}}; Q Q[maxn>>1];bool CMP (q Q1, q Q2) {if (q1.x! = q2.x) return q1.x < Q2.x;else return q1.y < q2.y;}    typedef struct TREE{TYPE val;    TREE *fa, *l, *r; int sz; The sum of the tree with the root of the node is}tree; Tree *MARK[MAXN]; Save node location struct Splaytree{public:splaytree () {RT = Null;inf = 1000000000;} void Push_up (Tree *t) {T->sz = (t->l? t->l->sz:0) + (t->r? t->r->sz:0) + 1;} void NewNode (tree *pre, tree *&t, Type v) {T = (tree *) malloc (sizeof (tree)); T->val = v; T->sz = 1; T-&GT;FA = pre; T->l = T->r = NULL;} void Init () {NewNode (NULL, RT,-inf); NewNode (RT, Rt->r, INF); rt->sz = 2;} void R_rotate (tree *x) {Tree *y = x->fa; TrEE *z = y->fa; Tree *k = x->r;y->l = K;x->r = Y;if (z) {if (y = = z->l) Z->l = X;else z->r = x;} if (k) K->fa = Y;y->fa = X;x->fa = Z; Push_up (y);} void L_rotate (tree *x) {Tree *y = x->fa; Tree *z = y->fa; Tree *k = X->l;y->r = K;x->l = Y;if (z) {if (y = = z->r) Z->r = X;else z->l = x;} if (k) K->fa = Y;y->fa = X;x->fa = Z; Push_up (y);} Search for node x number of nodes tree *findtag (int x) {if (NULL = = RT) return null; Tree *p;p = RT; Type sum = (p->l? p->l->sz:0) + 1;while (sum! = x) {if (sum < x) {p = p->r;x-= sum;} else P = p->l;if (NULL = = p) break;sum = (p->l? p->l->sz:0) + 1;} return p;} void Splay (tree *x, tree *&t) {tree *p, *end;end = T->fa;while (X->fa! = end) {p = x->fa;if (end = = P-&GT;FA) {// P is the root node if (x = = p->l) r_rotate (x); else l_rotate (x); break;} P is not the root node if (x = = P->l) {if (p = = p->fa->l) {r_rotate (P);//llr_rotate (x);//ll}else{r_rotate (x);//rll_rotate (x) ;}} Else{if (p = = p->fa->r) {//rrl_rotate (P); L_rotate (X);} else{//lrl_rotate (X); R_rotate (X);}}} T = X; PUSH_UP (T);} void Insert (Type *a, int x, int y) {int i;//test pointer Tricks code tree **link, *p;for (i = x; i <= y; i++) {link = &rt;while (*link) {p = *link;if (A[i] < p->val) Link = &p->l; Else link = &p->r;} NewNode (P, *link, A[i]); mark[i] = *link; Splay (*link, RT);}} void Delete (Type *a, int x, int y) {int i; Tree *t;for (i = x; i <= y; i++) {t = Mark[i]; Splay (T, rt); t = rt->l;while (t->r) t = t->r; Splay (t, rt->l); t = Rt;rt = Rt->l;rt->r = T->r;free (t); rt->r->fa = Rt;rt->fa = NULL; PUSH_UP (RT); Remember to update sz}}void Query (int id, int x) {x + +;//increment 1 Since there is a-oo node tree *p;p = Rt;int sum = (p->l? p->l->sz:0) + 1;while ( sum = x) {if (sum < x) {p = p->r;x-= sum;} else P = p->l;if (NULL = = p) break;sum = (p->l? p->l->sz:0) + 1;} B[id] = p->val;} void Show () {inorder (RT);p rintf ("\ n");} void Inorder (Tree *t) {if (NULL = = T) return;inorder (t->l);p rintf ("%d", t->val); Inorder (t->r);} void free () {Freetree (RT);} void Freetree (Tree *t) {if (NULL = = T) return; Freetree (T-&GT;L); Freetree (T->r); free (T);} Private:type inf; Tree *rt;}; Splaytree Spl;int Main () {//freopen ("In.txt", "R", stdin); int n, M, I;while (scanf ("%d%d", &n, &m) = = 2) {for (i = 1; i <= N; i++) scanf ("%d", A + i), for (i = 1; I <= m; i++) {scanf ("%d%d%d", &q[i].x, &q[i].y, &q[i].z); q[i].id = i;//position Make note of the convenient output}sort (q + 1, q + M + 1, CMP); for (i = 1; I <= m; i++) {if (q[i].x <= q[i-1].y) {//have intersection SPL. Delete (A, q[i-1].x, q[i].x-1), SPL. Insert (A, q[i-1].y + 1, q[i].y);} Else{spl. Free (); Erase the whole tree from SPL. Init (); Spl. Insert (A, q[i].x, q[i].y);} Spl. Query (Q[i].id, q[i].z);} Spl. Free (); for (i = 1; I <= m; i++) printf ("%d\n", B[i]);} return 0;}


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

Splay tree (interval k small)--poj 2761 Feed the Dogs

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.