【题目大意】

一个$n$个数的序列,$m$次操作,每次选择一段区间$[l, r]$,求出$[l, r]$中出现超过一半的数。

如果没有超过一半的数,那么就把答案钦定为$s$,每次会有$k$个数进行改变,给出下标,改变成当前的答案$s$。

$n, m \leq 5*10^5, \sum k\leq 10^6$

By FJSDFZ ditoly

【题解】

用这题的方法进行线段树操作即可:http://www.cnblogs.com/galaxies/p/20170602_c.html

但是这样需要验证是否可行,那么问题变成待修改的询问$[l, r]$区间内$x$的出现次数。

如果没有修改,我会分块!

修改就写个平衡树(开始想STL发现set不支持iterator减法,就gg了)

然后慢的要死。(论不会treap的危害)

后来一气之下找了pb_ds::tree来用。

# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> S[M];

每次只要调用order_of_key(x)即可,注意这个操作是右开区间,所以本题中可能需要些许变换。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + ;
const int mod = 1e9+; inline int getint() {
int x = ; char ch = getchar();
while(!isdigit(ch)) ch = getchar();
while(isdigit(ch)) x = (x<<) + (x<<) + ch - '', ch = getchar();
return x;
} # define gi getint() int n, a[M]; struct node {
int w, t;
node() {}
node(int w, int t) : w(w), t(t) {}
friend node operator + (node a, node b) {
if(a.w == b.w) return node(a.w, a.t + b.t);
if(a.t >= b.t) return node(a.w, a.t - b.t);
else return node(b.w, b.t - a.t);
}
}; /*
int rt[M];
struct Splay {
int ch[M][2], fa[M], val[M], s[M];
inline void up(int x) {
if(!x) return ;
s[x] = 1 + s[ch[x][0]] + s[ch[x][1]];
}
inline void rotate(int x, int &rt) {
int y = fa[x], z = fa[y], ls = ch[y][1] == x, rs = ls^1;
if(rt == y) rt = x;
else ch[z][ch[z][1] == y] = x;
fa[ch[x][rs]] = y, fa[y] = x, fa[x] = z;
ch[y][ls] = ch[x][rs]; ch[x][rs] = y;
up(y), up(x);
}
inline void splay(int x, int &rt) {
while(x != rt) {
int y = fa[x], z = fa[y];
if(y != rt) {
if((ch[z][0] == y) ^ (ch[y][0] == x)) rotate(x, rt);
else rotate(y, rt);
}
rotate(x, rt);
}
} inline int find(int x, int pos) {
if(pos == val[x]) return x;
if(pos < val[x]) return find(ch[x][0], pos);
else return find(ch[x][1], pos);
} inline int gmax(int x) {
while(ch[x][1]) x = ch[x][1];
return x;
} inline int del(int &rt, int pos) {
int x = find(rt, pos);
splay(x, rt);
if(!ch[x][0]) {
rt = ch[x][1]; fa[rt] = 0; ch[x][1] = 0;
return x;
}
if(!ch[x][1]) {
rt = ch[x][0]; fa[rt] = 0; ch[x][0] = 0;
return x;
}
int y = gmax(ch[x][0]); splay(y, ch[x][0]);
rt = y; ch[y][1] = ch[x][1]; fa[ch[x][1]] = y;
ch[x][0] = ch[x][1] = 0; fa[y] = 0;
up(y);
return x;
} inline void ins(int &x, int y, int pos, int id) {
if(!x) {
x = id; fa[x] = y; val[x] = pos; s[x] = 1; ch[x][0] = ch[x][1] = 0;
return ;
}
if(pos < val[x]) ins(ch[x][0], x, pos, id);
else ins(ch[x][1], x, pos, id);
up(x);
} inline void Ins(int &rt, int pos, int id) {
ins(rt, 0, pos, id);
splay(id, rt);
} inline int rank(int x, int d) {
if(!x) return 0;
if(d < val[x]) return rank(ch[x][0], d);
else return s[ch[x][0]] + 1 + rank(ch[x][1], d);
} inline void DEBUG(int x) {
if(!x) return ;
DEBUG(ch[x][0]);
printf("x = %d, ls = %d, rs = %d, pos = %d, sz = %d\n", x, ch[x][0], ch[x][1], val[x], s[x]);
DEBUG(ch[x][1]);
} inline void debug() {
for (int i=1; i<=5; ++i) {
printf("rt %d ======================\n", i);
DEBUG(rt[i]);
}
puts("\n");
}
}S;
*/ # include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> S[M]; struct SMT {
node w[M << ];
# define ls (x<<)
# define rs (x<<|)
inline void up(int x) {
w[x] = w[ls] + w[rs];
}
inline void build(int x, int l, int r) {
if(l == r) {
w[x] = node(a[l], );
return ;
}
int mid = l+r>>;
build(ls, l, mid), build(rs, mid+, r);
up(x);
}
inline void edt(int x, int l, int r, int ps, int d) {
if(l == r) {
S[w[x].w].erase(l);
w[x] = node(d, );
S[d].insert(l);
return ;
}
int mid = l+r>>;
if(ps <= mid) edt(ls, l, mid, ps, d);
else edt(rs, mid+, r, ps, d);
up(x);
}
inline node query(int x, int l, int r, int L, int R) {
if(L <= l && r <= R) return w[x];
int mid = l+r>>;
if(R <= mid) return query(ls, l, mid, L, R);
else if(L > mid) return query(rs, mid+, r, L, R);
else return query(ls, l, mid, L, mid) + query(rs, mid+, r, mid+, R);
}
inline void debug(int x, int l, int r) {
printf("x = %d, [%d, %d], w[x] = {%d, %d}\n", x, l, r, w[x].w, w[x].t);
if(l == r) return ;
int mid = l+r>>;
debug(ls, l, mid);
debug(rs, mid+, r);
}
# undef ls
# undef rs
}T; inline bool check(int l, int r, int d) {
int times = S[d].order_of_key(r+) - S[d].order_of_key(l);
// cout << "times = " << times << endl;
if(times <= (r-l+)/) return false;
return true;
} int main() {
freopen("president.in", "r", stdin);
freopen("president.out", "w", stdout);
n = gi; int Q = gi;
for (int i=; i<=n; ++i) {
a[i] = gi;
S[a[i]].insert(i);
}
T.build(, , n);
int l, r, s, k, leader; node ans;
while(Q--) {
if(Q % == ) cerr << Q << endl;
// S.debug();
l = gi, r = gi, s = gi, k = gi;
ans = T.query(, , n, l, r);
if(check(l, r, ans.w)) leader = ans.w;
else leader = s;
for (int i=, t; i<=k; ++i) {
t = gi;
T.edt(, , n, t, leader);
}
printf("%d\n", leader);
// T.debug(1, 1, n);
}
ans = T.query(, , n, , n);
if(check(, n, ans.w)) printf("%d\n", ans.w);
else puts("-1");
return ;
}

注释里面是平衡树。。T得不要不要的。。

复杂度都是$O((n + m + \sum K) log n)$

省队集训Day1 总统选举的更多相关文章

  1. FJ省队集训DAY1 T1

    题意:有一堆兔子,还有一个r为半径的圆,要求找到最大集合满足这个集合里的兔子两两连边的直线不经过圆. 思路:发现如果有两个点之间连边不经过圆,那么他们到圆的切线会构成一段区间,那么这两个点的区间一定会 ...

  2. 省队集训 Day1 残缺的字符串

    [题目大意] 双串带通配符匹配. $|S|, |T| \leq 5 * 10^5$ TL: 2s [题解] 参考bzoj 4503 可以设计如下函数 A[i] * B[i] * (A[i] - B[i ...

  3. 省队集训Day1 过河

    [题目大意] 小奇特别喜欢猪,于是他养了$n$只可爱的猪,但这些猪被魔法猪教会了魔法,一不看着某些猪就会自己打起来. 小奇要带着他的猪讨伐战狂,路途中遇到了一条河.小奇找到了一条船,可惜这条船一次只能 ...

  4. 省队集训Day1 睡觉困难综合征

    传送门:https://www.luogu.org/problem/show?pid=3613 [题解] 按二进制位分开,对于每一位,用“起床困难综合征”的方法贪心做. 写棵LCT,维护正反两种权值, ...

  5. HN2018省队集训

    HN2018省队集训 Day1 今天的题目来自于雅礼的高二学长\(dy0607\). 压缩包下载 密码: 27n7 流水账 震惊!穿着该校校服竟然在四大名校畅通无阻?霸主地位已定? \(7:10\)从 ...

  6. 【欧拉回路+最小生成树】SD开车@山东2018省队一轮集训day1

    目录 [欧拉回路+最小生成树]SD开车@山东2018省队一轮集训day1 PROBLEM 题目描述 输入 输出 样例输入 样例输出 提示 SOLUTION CODE [欧拉回路+最小生成树]SD开车@ ...

  7. JS省队集训记

    不知不觉省队集训已经结束,离noi也越来越近了呢 论考前实战训练的重要性,让我随便总结一下这几天的考试 Day 1 T1 唉,感觉跟xj测试很像啊?meet in middle,不过这种题不多测是什么 ...

  8. [2018HN省队集训D9T1] circle

    [2018HN省队集训D9T1] circle 题意 给定一个 \(n\) 个点的竞赛图并在其中钦定了 \(k\) 个点, 数据保证删去钦定的 \(k\) 个点后这个图没有环. 问在不删去钦定的这 \ ...

  9. [2018HN省队集训D8T1] 杀毒软件

    [2018HN省队集训D8T1] 杀毒软件 题意 给定一个 \(m\) 个01串的字典以及一个长度为 \(n\) 的 01? 序列. 对这个序列进行 \(q\) 次操作, 修改某个位置的字符情况以及查 ...

随机推荐

  1. android 出现Make sure the Cursor is initialized correctly before accessing data from it

    Make sure the Cursor is initialized correctly before accessing data from it 详细错误是:java.lang.IllegalS ...

  2. win7 php连接远程oracle

    <?php /* 先下载oracle客户端 下载地址 http://www.oracle.com/technetwork/topics/winx64soft-089540.html 下载如下三个 ...

  3. 理解BitSet

    先来看几道面试题: 1.统计40亿个数据中没有出现的数据,将40亿个不同数据进行排序. 2.现在有1千万个随机数,随机数的范围在1到1亿之间,要求写出一种算法,将1到1亿之间没有在随机数中的数求出来. ...

  4. 【Redis】- 安装为windows服务

    1.安装redis服务 echo install redis-server redis-server.exe --service-install redis.windows.conf --loglev ...

  5. 【Docker 命令】- rmi命令

    docker rmi : 删除本地一个或多个镜像. 语法 docker rmi [OPTIONS] IMAGE [IMAGE...] OPTIONS说明: -f :强制删除: --no-prune : ...

  6. MVP开发模式的理解

    1.MVP是什么 如果从层次关系来讲,MVP属于Presentation层的设计模式.对于一个UI模块来说,它的所有功能被分割为三个部分,分别通过Model.View和Presenter来承载.Mod ...

  7. 简述在akka中发送消息的过程

    在flink的数据传输过程中,有两类数据,一类数据是控制流数据,比如提交作业,比如连接jm,另一类数据是业务数据.flink对此采用了不同的传输机制,控制流数据的传输采用akka进行,业务类数据传输在 ...

  8. Git无法删除文件问题:fatal: pathspec 'readme.txt' did not match any files

    在使用Git时,不小心创建了一个不需要的文件,想要删除一个文件时,出现了错误: fatal: pathspec 'readme.txt' did not match any files 原因是新建的这 ...

  9. HDU3949:XOR——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=3949 求n个数的异或和第k小. 参考:https://blog.sengxian.com/algorithms/ ...

  10. NOIP2009 codevs1173 洛谷P1073 最优贸易

    Description: 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通 ...