[POJ2761] Feed the Dogs (treap)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=2761

The main topic: give you n number, m times query, M query is a,b,k, query the following table from A to B of the K small element is which. The M-bands do not contain one another.

Treap, I learned to write a board, stay paste spare.

Offline operation, move the interval, delete the old add new.

#include <cstdio>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<Set>#include<string>#include<bitset>#include<cmath>#include<numeric>#include<iterator>#include<iostream>#include<cstdlib>#include<functional>#include<queue>#include<stack>#include<list>#include<ctime>using namespacestd;Const intINF = ~0U>>1; typedefLong LongLL;structTreapnode {intKey, Val, Siz; Treapnode* ch[2]; Treapnode (): Val (0), Siz (1) {key = rand ()-1; ch[0] = ch[1] =NULL;} voidRZ () {siz=1; if(ch[0]) Siz + = ch[0]->siz; if(ch[1]) Siz + = ch[1]->siz; }    ~Treapnode () {if(ch[0])Deletech[0]; if(ch[1])Deletech[1]; }};voidROT (treapnode*& RT,BOOLd) {Treapnode* C = rt->Ch[d]; RT-&GT;CH[D] = c->ch[!d]; C-&GT;CH[!D] =RT; RT->rz (); C->RZ (); RT=C;}voidInsert (treapnode*& RT,intval) {    if(!RT) {RT =NewTreapnode (); Rt->val = val;return; } //if (val = = rt->val) return;    BOOLD = val > rt->Val; Insert (Rt-Ch[d], Val); if(Rt->ch[d]->key < rt->key) Rot (RT, D); ElseRt->RZ ();}voidDelete (treapnode*& RT,intval) {    if(RT = = NULL)return; if(Rt->val = =val) {        if(rt->ch[0] = = NULL && rt->ch[1] ==NULL) {            DeleteRT; RT=NULL; return; }        Else if(rt->ch[0] ==NULL) {rot (RT,1); Delete (Rt->ch[0], Val); }        Else if(rt->ch[1] ==NULL) {rot (RT,0); Delete (Rt->ch[1], Val); }        Else {            BOOLD = rt->ch[1]->key > rt->ch[0]->key;            Rot (RT, D); Delete (Rt->ch[!d], Val); }    }    Else {        BOOLD = val > rt->Val; Delete (Rt-Ch[d], Val); } RT-RZ ();}intSelect (treapnode* RT,intk) {if(!RT)return-inf; if(Rt->siz < k)return-inf; intR =0; if(!rt->ch[0]) R =0; Elser = rt->ch[0]->siz; if(r = = k-1)returnRt->Val; ifK1< R)returnSelect (rt->ch[0], k); returnSelect (rt->ch[1], K-r-1);}intN, M;inta[100001], ans[50001];//int ptr = 0;structbwl{intL, R, K, IDX; BOOL operator< (Constbwl& b)Const{        returnL <B.L; }};BWL ee[50001];intMain () {Srand (0));  while(SCANF ("%d%d", &n, &m)! =EOF) {Treapnode*root =NULL;  for(inti =0; I < n; i++) {scanf ("%d", &A[i]); }         for(inti =0; I < m; i++) {scanf ("%d%d%d", &AMP;EE[I].L, &AMP;EE[I].R, &EE[I].K); Ee[i].idx=i; } sort (ee, EE+m); intPre =1, last =1;  for(inti =0; I < m; i++) {            if(i = =0) {Pre= last =EE[I].L; }             while(Pre <= last && pre <EE[I].L) {Delete (root, A[pre-1]); Pre++; }             while(Last <= n && last <=EE[I].R) {Insert (root, A[last-1]); Last++; } Ans[ee[i].idx]=Select (root, EE[I].K); }         for(inti =0; I < m; i++) {printf ("%d\n", Ans[i]); }        DeleteRoot; }    return 0;}/*7 2 1 5 2 6 3 7 4 1 5 3 2 7 1 9 3 1 3 2 4 7 7 8 2 1 3 3 5 9 2 3 8 4*/

[POJ2761] Feed the Dogs (treap)

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.