Chairman Tree Template

Source: Internet
Author: User

#include <stdio.h>#include<iostream>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN = 1e5 +Ten;intN, M;intCNT;structnode{intL, R;//point to left and right subtrees, respectively    intSum//The number of points within the range that the node governsnode () {Sum=0; }}TREE[MAXN* -];structvalue{intX//the size of the value    intId//position before discretization in the original array}VALUE[MAXN];BOOLCMP (Value v1, value v2) {returnv1.x <v2.x;}intROOT[MAXN];//root node of multiple segment treesintRANK[MAXN];//array after the original array is discretevoidinit () {CNT=1; root[0] =0; tree[0]. L = tree[0]. R = tree[0].sum =0;}voidUpdateintNumint&rt,intLintR) {tree[cnt++] =Tree[rt]; RT= CNT-1; Tree[rt].sum++;if(L = = r)return; intMID = L + ((r-l) >>1); if(Num <=mid) Update (NUM, Tree[rt].    L, L, mid); ElseUpdate (num, Tree[rt]. R, Mid +1, R);}intQueryintIintJintKintLintR) {    intd = tree[tree[j]. L].sum-Tree[tree[i].    L].sum; if(L = = r)returnl; intMID = L + ((r-l) >>1); if(k <= D)returnquery (Tree[i]. L, Tree[j].    L, K, L, mid); Else returnQuery (Tree[i]. R, Tree[j]. R, k-d, Mid +1, R);}intMain () {intT; scanf ("%d",&T);  while(t--) {scanf ("%d%d", &n, &m);  for(inti =1; I <= N; i++) {scanf ("%d", &value[i].x); Value[i].id=i; }        //be discretizedSort (Value +1, Value + n +1, CMP);  for(inti =1; I <= N; i++) {Rank[value[i].id]=i;        } init ();  for(inti =1; I <= N; i++) {Root[i]= Root[i-1];Update (Rank[i], root[i],1, N); }        intLeft , right, K; for(inti =1; I <= m; i++) {scanf ("%d%d%d", &left, &right, &k); printf ("%d\n", Value[query (Root[left-1], Root[right], K,1, N)].        x); }    }    return 0;}

#include <cstdio>using namespacestd;#defineN 500001structnode{intV,LC,RC;} t[n* +];introot[n],e;voidInsert (intPreintCurintPintLintR) {    if(l==r) {t[cur].v=t[pre].v+1; return; }    intM= (l+r>>1); if(p<=m) {t[cur].lc=++e; T[cur].rc=t[pre].rc;      Insert (T[PRE].LC,T[CUR].LC,P,L,M); }    Else{t[cur].rc=++e; T[CUR].LC=t[pre].lc; Insert (T[pre].rc,t[cur].rc,p,m+1, R); } t[cur].v=t[t[cur].lc].v+t[t[cur].rc].v;}intGoal;intQuery (intLintRintLintR) {    if(L==R)returnl; intM= (l+r>>1); if(T[t[r].lc].v-t[t[l].lc].v>goal)returnQuery (t[l].lc,t[r].lc,l,m); Else if(T[t[r].rc].v-t[t[l].rc].v>goal)returnQuery (t[l].rc,t[r].rc,m+1, R); return 0;}intn,m;intMain () {intx, y; scanf ("%d%d",&n,&m);  for(intI=1; i<=n;++i) {scanf ("%d",&x); Root[i]=++e; Insert (Root[i-1],root[i],x,1, N); }     for(; m;--m) {scanf ("%d%d",&x,&y); Goal= (y-x+1>>1); printf ("%d\n", Query (root[x-1],root[y],1, N)); }    return 0;}

Chairman Tree Template

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.