[洛谷P3765]总统选举
题目大意:有$n(n\leqslant5\times10^5)$个数,有$m(m\leqslant5\times10^5)$次询问。
一次询问形如$l\;r\;s\;k\;w_1\;w_2\dots w_k:$每次询问$[l_i,r_i]$内的出现次数大于一半的数,如果没有,则为$s$。这次询问后结束后$k(\sum k\leqslant10^6)$个位置的数$w_i$变成询问答案。
题解:求大于一半的数可以用[洛谷P2397]yyy loves Maths VI (mode)来做,带修改,可以用线段树,维护区间最大的数,以及这个数比一半多了多少。但是求出来的答案只是可能的解(因为没有出现次数保证大于一半),可以用平衡树求出这个数在这个区间中出现次数,判断是否合法。修改暴力改
卡点:初值计数器赋成$-1$,导致转移出锅
C++ Code:
#include <cstdio>
#include <cctype>
//#define ONLINE_JUDGE
namespace R {
int x;
#ifdef ONLINE_JUDGE
#define M 1 << 27
char op[M], *ch;
inline void init() {fread(ch = op, 1, M, stdin);}
inline int read() {
while (isspace(*ch)) ch++;
for (x = *ch & 15, ch++; isdigit(*ch); ch++) x = x * 10 + (*ch & 15);
return x;
}
#else
char ch;
inline int read() {
ch = getchar();
while (isspace(ch)) ch = getchar();
for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
return x;
}
#endif
}
using R::read; #define maxn 500010 int n, m;
int s[maxn]; namespace SgT {
struct node {
int s, cnt; //s为区间可能众数,cnt这个数比一半区间长度多多少
inline node(int __s = 0, int __cnt = 0) {s = __s, cnt = __cnt;}
inline friend node operator + (const node &lhs, const node &rhs) {
if (lhs.s == rhs.s) return node(lhs.s, lhs.cnt + rhs.cnt);
if (lhs.cnt > rhs.cnt) return node(lhs.s, lhs.cnt - rhs.cnt);
else return node(rhs.s, rhs.cnt - lhs.cnt);
}
} V[maxn << 2];
inline void update(int rt) {
V[rt] = V[rt << 1] + V[rt << 1 | 1];
} void build(int rt, int l, int r) {
if (l == r) {
V[rt] = node(s[l], 1);
return ;
}
int mid = l + r >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
update(rt);
} int pos, num;
void __modify(int rt, int l, int r) {
if (l == r) {
V[rt] = node(num, 1);
return ;
}
int mid = l + r >> 1;
if (pos <= mid) __modify(rt << 1, l, mid);
else __modify(rt << 1 | 1, mid + 1, r);
update(rt);
}
void modify(int __pos, int __num) {
pos = __pos, num = __num;
__modify(1, 1, n);
} int L, R;
node __query(int rt, int l, int r) {
if (L <= l && R >= r) return V[rt];
int mid = l + r >> 1;
node ans;
if (L <= mid) ans = __query(rt << 1, l, mid);
if (R > mid) ans = ans + __query(rt << 1 | 1, mid + 1, r);
return ans;
}
int query(int __L, int __R, int tg = 0) {
L = __L, R = __R;
return __query(1, 1, n).s;
}
} namespace Treap {
int root[maxn];
#define N 1000010 + maxn
int seed = 20040826;
inline int rand() {return seed *= 48271;}
int ta, tb, tmp, res; int lc[N], rc[N], pri[N], V[N], sz[N], idx;
inline int nw(int x) {
pri[++idx] = rand();
sz[idx] = 1;
V[idx] = x;
return idx;
}
inline int update(int rt) {
sz[rt] = sz[lc[rt]] + sz[rc[rt]] + 1;
return rt;
} void split(int rt, int k, int &x, int &y) {
if (!rt) x = y = 0;
else {
if (V[rt] <= k) split(rc[rt], k, rc[rt], y), x = update(rt);
else split(lc[rt], k, x, lc[rt]), y = update(rt);
}
}
int merge(int x, int y) {
if (!x || !y) return x | y;
if (pri[x] > pri[y]) {rc[x] = merge(rc[x], y); return update(x);}
else {lc[y] = merge(x, lc[y]); return update(y);}
} void insert(int &rt, int x) {
if (!rt) rt = nw(x);
else {
split(rt, x, ta, tb);
rt = merge(merge(ta, nw(x)), tb);
}
}
void erase(int &rt, int x) {
split(rt, x, ta, tb);
split(ta, x - 1, ta, tmp);
rt = merge(ta, tb);
} int gtrnk(int &rt, int x) {
split(rt, x, ta, tb);
res = sz[ta];
merge(ta, tb);
return res;
} int query(int &rt, int l, int r) {
// printf("%d %d\n", gtrnk(rt, r), gtrnk(rt, l));
return gtrnk(rt, r) - gtrnk(rt, l - 1);
}
void modify(int tg, int &before, int after) {
erase(root[before], tg);
insert(root[after], tg);
before = after;
}
#undef N
}
using Treap::root; int main() {
#ifdef ONLINE_JUDGE
R::init();
#endif
n = read(), m = read();
for (int i = 1; i <= n; i++) Treap::insert(root[s[i] = read()], i);
SgT::build(1, 1, n);
while (m --> 0) {
int l = read(), r = read(), s = read(), k = read();
int tmp = SgT::query(l, r);
int CNT = Treap::query(root[tmp], l, r);
if (CNT <= (r - l + 1 >> 1)) tmp = s;
printf("%d\n", tmp);
for (int i = 0, x; i < k; i++) {
x = read();
SgT::modify(x, tmp);
Treap::modify(x, ::s[x], tmp);
}
}
int tmp = SgT::query(1, n);
int CNT = Treap::query(root[tmp], 1, n);
if (CNT <= n >> 1) tmp = -1;
printf("%d\n", tmp);
return 0;
}
[洛谷P3765]总统选举的更多相关文章
- 洛谷 P3765 总统选举 解题报告
P3765 总统选举 题目背景 黑恶势力的反攻计划被小C成功摧毁,黑恶势力只好投降.秋之国的人民解放了,举国欢庆.此时,原秋之国总统因没能守护好国土,申请辞职,并请秋之国人民的大救星小C钦定下一任.作 ...
- luogu P3765 总统选举(线段树维护摩尔投票+平衡树)
这题需要一个黑科技--摩尔投票.这是一个什么东西?一个神奇的方法求一个序列中出现次数大于长度一半的数. 简而言之就是同加异减: 比如有一个代表投票结果的序列. \[[1,2,1,1,2,1,1]\] ...
- [洛谷P2491] [SDOI2011]消防
洛谷题目链接:[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超 ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP
题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...
- 洛谷P1710 地铁涨价
P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交 讨论 题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...
- 洛谷P1371 NOI元丹
P1371 NOI元丹 71通过 394提交 题目提供者洛谷OnlineJudge 标签云端评测 难度普及/提高- 提交 讨论 题解 最新讨论 我觉得不需要讨论O long long 不够 没有取 ...
随机推荐
- 使用Python第三方库生成二维码
本文主要介绍两个可用于生成二维码的Python第三方库:MyQR和qrcode. MyQR的使用: 安装: pip install MyQR 导入: from MyQR import myqr imp ...
- 浅谈localStorage的用法
今天接到一个任务,说是让自动调节textarea标记的输入高度,而且还要记录下来,下次登录的时候还是调节后的高度,我第一时间就想到了localStorage的用法,直接代码献上: <html l ...
- redis搭建
redis 1.简介.安装 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.redis 和 Memcached 缓存服务很像 ...
- C++常量(const)的使用
#include <iostream> using namespace std; class MyClass { public: int GetValue() const ; int Ge ...
- (数据科学学习手札14)Mean-Shift聚类法简单介绍及Python实现
不管之前介绍的K-means还是K-medoids聚类,都得事先确定聚类簇的个数,而且肘部法则也并不是万能的,总会遇到难以抉择的情况,而本篇将要介绍的Mean-Shift聚类法就可以自动确定k的个数, ...
- python2.7入门---Number(数字)
今天咱们来简单分享一下关于python中的一种数据类型和操作方法.费话不多说哈,咱们直接来进行实践加理论.首先,我们要知道,Python Number 数据类型用于存储数.数据类型是不允许改变 ...
- Hystrix入门指南
Introduction 1.Where does the name come from? hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与 ...
- allegro导入网表过程中出现的错误信息
1. 找不到焊盘PAD,下面这句话的意思是器件封装找不到焊盘46.pad WARNING(SPMHNI-): Unable to load symbol ): Could not find padst ...
- python简单的数据清洗,数据筛选方法归类
创建数组有两种方式,1.直接赋值 2.随机变量生成随机生成包括4种:np.arange(20),np.linspace(0,10,5),np.logspace(0,2,5),np.random.ran ...
- NodeJS微信公众平台开发
微信是手机用户必备的App,微信最开始只是作为社交通讯应用供用户使用,但随着用户量不断的增加,微信的公众号在微信上表现出来了它强大的一面,微信公众平台具有四大优势:1.平台更加稳固:2.用户关系更加平 ...