求区间内是否有个数大于二分之一的数,有的话输出这个数,没有的话输出0.

在询问的时候,如果左边有sum大于这个limit,就可以继续求,如果右边有sum大于limit  也递归,

如果都不行,返回 0;

 #include<cstdio>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=5e5+;
int Root[maxn],cnt;
struct node
{
int ln,rn,sum;
}tree[maxn*];
void add(int &x,int y,int l,int r,int num)
{
tree[++cnt]=tree[y];tree[cnt].sum++;x=cnt;
if(l==r) return;
int mid=l+r>>;
if(num<=mid) add(tree[x].ln,tree[y].ln,l,mid,num);
else add(tree[x].rn,tree[y].rn,mid+,r,num);
}
int query(int x,int y,int l,int r,int limit)
{
if(l==r){
if(tree[x].sum-tree[y].sum>=limit) return l;
else return ;
}
int left1=tree[x].ln,left2=tree[y].ln;
int right1=tree[x].rn,right2=tree[y].rn;
int mid=l+r>>;
if(tree[left1].sum-tree[left2].sum>=limit){
return query(left1,left2,l,mid,limit);
}
else if(tree[right1].sum-tree[right2].sum>=limit){
return query(right1,right2,mid+,r,limit);
}
else return ;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
int tmp;
scanf("%d",&tmp);
add(Root[i],Root[i-],,n,tmp);
}
for(int i=;i<=m;i++){
int l,r;
scanf("%d%d",&l,&r);
int limit=(r-l+)/+;
printf("%d\n",query(Root[r],Root[l-],,n,limit));
}
return ;
}

主席树 模板题 luogu([POI2014]KUR-Couriers)的更多相关文章

  1. 【POJ 2104】 K-th Number 主席树模板题

    达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...

  2. 主席树:POJ2104 K-th Number (主席树模板题)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44952   Accepted: 14951 Ca ...

  3. poj2104 主席树模板题

    题意 给出n个数字组成的数字序列,有m组询问.每次询问包含三个数字l,r,k.对于每个询问输出序列区间[l,r]中第k大的数字. 分析 这是主席树的模板题,套板子就可以 #include <cs ...

  4. 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题

    达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...

  5. SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]

    题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...

  6. POJ 2104 K-th Number(主席树模板题)

    http://poj.org/problem?id=2104 题意:求区间$[l,r]$的第k小. 思路:主席树不好理解啊,简单叙述一下吧. 主席树就是由多棵线段树组成的,对于数组$a[1,2...n ...

  7. hdu2665(主席树模板题)

    hdu2665 题意 求区间第 k 小. 分析 参考 这类题目做法挺多的,例如 划分树. 这里使用主席树再写一发,不得不说主席树相比而言要好写的多,比起普通线段树,主席树就是复用了线段树共有的信息. ...

  8. POJ 2104 主席树模板题

    #include <iostream> #include <cstdio> #include <algorithm> int const maxn = 200010 ...

  9. POJ-2104-K-th Number(区间第K大+主席树模板题)

    Description You are working for Macrohard company in data structures department. After failing your ...

随机推荐

  1. cURL error 60: SSL certificate problem: unable to get local issuer certificate 解决方法

    微信开发的时,请求接口报错如下: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see ...

  2. [Python]BeautifulSoup安装与使用

    1.BeautifulSoup简介 BeautifulSoup4和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据 ...

  3. SQLyog怎么导入mysql数据库

    参考链接:https://jingyan.baidu.com/article/647f0115c5ad9f7f2148a8c6.html

  4. 题解【AcWing10】有依赖的背包问题

    题面 树形 DP 的经典问题. 我们设 \(dp_{i,j}\) 表示当前节点为 \(i\),当前节点的子树(包含当前节点)最多装的体积是 \(j\) 的最大价值. 我们遍历节点的过程就相当于做了一遍 ...

  5. php执行shell脚本

    本次想要配置webhook钩子,   做钩子大多是走 ssh 协议, coding 里配置部署公钥   之前用 docker 写钩子, 也是 ssh 权限的问题   包工具: 1.composer r ...

  6. Xshell 5

    Xshell 5安装步骤(带安装包):http://www.cnblogs.com/ysocean/p/7702243.html

  7. vue源码的入口(四)

    我们之前提到过 Vue.js 构建过程,在 web 应用下,我们来分析 Runtime + Compiler 构建出来的 Vue.js,它的入口是 src/platforms/web/entry-ru ...

  8. JS实现“隐藏与显示”功能(多种方法)

    1,通过按钮实现隐藏与显示: 这个是通过按钮点击实现的隐藏与显示,具体代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  9. centos7修改xshell默认访问端口由22修改为62058

    1.vim /etc/ssh/sshd_config 2.新加端口62058:Port 62058 3.重启sshd服务:systemctl restart sshd 4.将新加端口添加到防火墙并重启 ...

  10. [Codechef TASTR] Level of Difference - 后缀数组,容斥原理

    [Codechef TASTR] Level of Difference Description 给定两个字符串,求恰好在一个字符串中出现过的本质不同的子串数量. Solution 设 \(U(S)\ ...