Bzoj 3524 POI Couriers Chairman Tree

Source: Internet
Author: User

Main topic

Give a sequence that asks if there are more than half the numbers in the interval.

Ideas

Use the Chairman tree to take the interval out and find it in the weight segment tree.

CODE
#define _crt_secure_no_warnings#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 500010#define MAXR 10000010using namespace STD;structsegtree{Segtree *son[2];intCNT;} MEMPOOL[MAXR], *c = Mempool, *root[max]; Segtree *newsegtree (Segtree *_, Segtree *__,int___) {c->son[0] = _; c->son[1] = __; c->cnt = ___;returnC + +;} Segtree *insert (Segtree *consult,intLintRintx) {if(L = = r)returnNewsegtree (consult->son[0], consult->son[1], consult->cnt +1);intMid = (L + r) >>1;if(x <= mid)returnNewsegtree (Insert (consult->son[0], L, Mid, X), consult->son[1], consult->cnt +1);Else        returnNewsegtree (consult->son[0], Insert (consult->son[1], Mid +1, R, X), consult->cnt +1);}intAsk (Segtree *left, Segtree *right,intLintRintc) {if(L = = r)returnLintMid = (L + r) >>1;if(right->son[0]->cnt-left->son[0]->cnt > C)returnAsk (left->son[0], right->son[0], L, Mid, c);Else if(right->son[1]->cnt-left->son[1]->cnt > C)returnAsk (left->son[1], right->son[1], Mid +1, R, c);return 0;}intCNT, asks;intMain () {root[0] = Newsegtree (c, C,0);Cin>> CNT >> asks; for(intX, i =1; I <= CNT; ++i) {scanf("%d", &x); Root[i] = Insert (Root[i-1],1, CNT, x); } for(intX, y, i =1; I <= asks; ++i) {scanf("%d%d", &x, &y);printf("%d\n", Ask (Root[x-1], Root[y],1, CNT, (Y-x +1) /2)); }return 0;}

Bzoj 3524 POI Couriers Chairman Tree

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.