BZOJ 3524 POI 2014 Couriers 主席树

4 篇文章 0 订阅
1 篇文章 0 订阅

题目大意

给出一个序列,问一段区间内有没有出现过一半以上的数字。

思路

用主席树取区间出来,在权值线段树上找。

CODE

#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 500010
#define MAXR 10000010
using namespace std;

struct SegTree{
    SegTree *son[2];
    int cnt;
}mempool[MAXR], *C = mempool, *root[MAX];

SegTree *NewSegTree(SegTree *_, SegTree *__, int ___)
{
    C->son[0] = _;
    C->son[1] = __;
    C->cnt = ___;
    return C++;
}

SegTree *Insert(SegTree *consult, int l, int r, int x)
{
    if(l == r)
        return NewSegTree(consult->son[0], consult->son[1], consult->cnt + 1);
    int mid = (l + r) >> 1;
    if(x <= mid)    
        return NewSegTree(Insert(consult->son[0], l, mid, x), consult->son[1], consult->cnt + 1);
    else
        return NewSegTree(consult->son[0], Insert(consult->son[1], mid + 1, r, x), consult->cnt + 1);
}

int Ask(SegTree *left, SegTree *right, int l, int r, int c)
{
    if(l == r)  return l;
    int mid = (l + r) >> 1;
    if(right->son[0]->cnt - left->son[0]->cnt > c)
        return Ask(left->son[0], right->son[0], l, mid, c);
    else if(right->son[1]->cnt - left->son[1]->cnt > c)
        return Ask(left->son[1], right->son[1], mid + 1, r, c);
    return 0;
}

int cnt, asks;

int main()
{
    root[0] = NewSegTree(C, C, 0);
    cin >> cnt >> asks;
    for(int x, i = 1; i <= cnt; ++i) {
        scanf("%d", &x);
        root[i] = Insert(root[i - 1], 1, cnt, x);
    }
    for(int x, 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值