OJ题号:BZOJ3524、BZOJ2223、洛谷3567

思路:

维护一颗可持久化权值线段树,记录每次加入数字时,不同数字出现的个数。
对于每一个询问$[l,r]$,同时查询以$r$和$l-1$为根的线段树,每次比较两个节点左右字子树的权值和,如果大于$[l,r]$区间的一半就说明这一子区间可能有答案,递归查询即可。

 1 #include<cstdio>
 2 #include<cctype>
 3 #include<cstring>
 4 inline int getint() {
 5     char ch;
 6     while(!isdigit(ch=getchar()));
 7     int x=ch^'0';
 8     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
 9     return x;
10 }
11 const int N=500001,SZ=10000001;
12 class FotileTree {
13     private:
14         int val[SZ],sz,left[SZ],right[SZ];
15         int newnode() {
16             return sz++;
17         }
18     public:
19         FotileTree() {
20             sz=0;
21             memset(val,0,sizeof val);
22         }
23         int root[N];
24         int build(const int b,const int e) {
25             int new_p=newnode();
26             if(b==e) return new_p;
27             int mid=(b+e)>>1;
28             left[new_p]=build(b,mid);
29             right[new_p]=build(mid+1,e);
30             return new_p;
31         }
32         int modify(const int p,const int b,const int e,const int x) {
33             int new_p=newnode();
34             val[new_p]=val[p]+1;
35             if(b==e) return new_p;
36             int mid=(b+e)>>1;
37             if(x<=mid) left[new_p]=modify(left[p],b,mid,x),right[new_p]=right[p];
38             if(x>mid) right[new_p]=modify(right[p],mid+1,e,x),left[new_p]=left[p];
39             return new_p;
40         }
41         int query(const int p1,const int p2,const int b,const int e,const int k) {
42             if(b==e) return b;
43             int mid=(b+e)>>1;
44             if(val[left[p2]]-val[left[p1]]>k) return query(left[p1],left[p2],b,mid,k);
45             if(val[right[p2]]-val[right[p1]]>k) return query(right[p1],right[p2],mid+1,e,k);
46             return 0;
47         }
48 };
49 FotileTree t;
50 int main() {
51     int n=getint(),m=getint();
52     t.root[0]=t.build(1,n);
53     for(int i=1;i<=n;i++) t.root[i]=t.modify(t.root[i-1],1,n,getint());
54     while(m--) {
55         int l=getint(),r=getint();
56         printf("%d\n",t.query(t.root[l-1],t.root[r],1,n,(r-l+1)>>1));
57     }
58     return 0;
59 }

转载于:https://www.cnblogs.com/skylee03/p/7183211.html

[POI2014]Couriers相关推荐

  1. [骗分技巧——随机化Ⅱ] [Poi2014]Couriers,CodeChef - TKCONVEX

    文章目录 [Poi2014]Couriers problem solution code CodeChef - TKCONVEX problem solution code 随机算法的典型套路:枚举太 ...

  2. BZOJ 3524: [Poi2014]Couriers【主席树】

    3524: [Poi2014]Couriers [题目描述] 传送门 [题解] 主席树的裸题,要找出现次数大于一半的,那么他肯定出现在当前的左子树或右子树中,就这样查找就可以了. 代码如下 #incl ...

  3. 【BZOJ】3524 [POI2014] Couriers(主席树)

    题目 传送门:QWQ 传送到洛谷QWQ 分析 把求区间第k大的改一改就ok了. 代码 1 #include <bits/stdc++.h> 2 using namespace std; 3 ...

  4. bzoj3524 [Poi2014]Couriers

    随机乱搞,调调参数勉强过 代码 1 #include<cstdio> 2 #include<vector> 3 #include<ctime> 4 #include ...

  5. 【BZOJ3524】 [Poi2014]Couriers

    Description 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0 ...

  6. 主席树【bzoj3524(p3567)】[POI2014]Couriers

    Description 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0 ...

  7. BZOJ.3524.[POI2014]Couriers(主席树)

    题目链接 //119964kb 5780ms //主席树裸题啊.. #include <cstdio> #include <cctype> #define gc() getch ...

  8. bzoj3524: [Poi2014]Couriers / bzoj2223: [Coci 2009]PATULJCI 主席树

    主席树模板题目 #include<bits/stdc++.h> using namespace std; int root[500010],a[500010],hash[500010],d ...

  9. 【BZOJ3524】Couriers,第一次的主席树

    传送门(权限) 题面 3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 128 MB Submit: 1449 Solved: 508 ...

  10. 【以前的空间】主席树

    主席树 周五晚蔡大神讲了下,觉得不是很难的东西,然后就可是敲,发现好像就是个持久化线段树(?!) 当晚笔记: 主席树,其实就是多棵可持久化权值线段树 那什么是可持久化线段树呢? 如树1中就在子树i,树 ...

最新文章

  1. python接口自动化测试(三)-requests.post()
  2. 成功解决Exception unhandled OSError cannot open resource File: F:\Program Files\Python\Python36\Lib\si
  3. java + httpclient +post请求(记录下)
  4. 三位数的茎叶图怎么看_贝德玛化妆品生产日期怎么看?贝德玛化妆品保质期怎么看?...
  5. 5 天 6 亿 3000 万数据泄露,怎么做才能跑赢骗子?
  6. 好用的开源软件_推荐_良心软件
  7. 什么是蜂窝移动网络?
  8. 【JZOJ】1183:【入门】判断是否构成三角形
  9. java飞翔的小鸟游戏实验报告,BirdGame 飞翔的小鸟游戏源码,仿照 编写的java版本 ,所有的 功能完整。 s 249万源代码下载- www.pudn.com...
  10. Periodic Strings
  11. Superset可视化之制作地图
  12. Excel数据透视表: GetPivotData
  13. thinkphp6 通过命令行快速生成多应用模块报 【Command “build“ is not defined.】错误的解决办法
  14. leetcode-1833. 雪糕的最大数量(排序+贪心)
  15. 会议纪要的正确记录姿势
  16. 从Maxwell方程组到波动方程
  17. 图像质量调试色温值对照2021-04-20
  18. 「 论文投稿 」《IEEE Robotics and Automation Letters》与ICRA会议,录用经历
  19. Activiti判断流程是否结束
  20. windows ce linux,Win CE + Linux双系统 总结

热门文章

  1. ORACLE 分页SQL
  2. Cocos2d-X3.0 刨根问底(九)----- 场景切换(TransitionScene)源代码分析
  3. Bitmap Style Designer非官方说明
  4. 像素,色彩,分辨率什么的
  5. 深入浅出之正则表达式(一)(转)
  6. 用VMWARE学习组网(二)
  7. 好的项目需要有好的需求
  8. 以太坊智能合约部署与交互
  9. Django学习-22-Form
  10. 如何用Eclipse进行单元测试