Codeforces Round #449 (Div. 1) Willem, Chtholly and Seniorious (ODT维护)
题意
给你一个长为 \(n\) 的序列 \(a_i\) 需要支持四个操作。
- \(1~l~r~x:\) 把 \(i \in [l, r]\) 的 \(a_i\) 加 \(x\) 。
- \(2~l~r~x:\) 把 \(i \in [l, r]\) 的 \(a_i\) 赋值成 \(x\) 。
- \(3~l~r~x:\) 求 \([l, r]\) 区间的第 \(x\) 小元素。
- \(4~l~r~x~y:\) 求 \((\sum_{i = l}^{r} {a_i}^x) \mod y\) 。
一共会操作 \(m\) 次。
保证数据随机(给了你一个随机数据生成器)。
\(1 \le n, m \le 10^5, 1 \le a_i \le 10^9\)
题解
对于这种 数据随机 并且有 区间赋值 的题,常常有一个很牛逼的乱搞算法,叫做 \(\mathrm{ODT(Old~Driver~Tree)}\) ,也有叫 \(\mathrm{Chtholly Tree}\) 的。
至于这个怎么做的呢?其实核心就一条,优化暴力。
如何优化呢?因为有赋值操作,考虑把相邻的数值一样的点合并成一个整的区间就行了。
至于如何实现呢?我们用 std :: set<Node> 维护一段段区间就行了。
Node 内容如下 mutalbe 是为了支持指针修改 :
struct Node {
int l, r; mutable ll val;
}
inline bool operator < (const Node &lhs, const Node &rhs) {
return lhs.l < rhs.l;
}
修改的时候,我们把 \([l, r]\) 这段区间 \(Split\) 出来,也就是我们把 \(l\) 与 \(l-1\) 分开,\(r\) 与 \(r + 1\) 分开就行了。
具体实现如下:
void Split(int pos) {
auto it = S.lower_bound((Node){pos, 0, 0});
if (it == S.end() || it -> l > pos) {
-- it;
S.emplace(it -> l, pos - 1, it -> val);
S.emplace(pos, it -> r, it -> val);
S.erase(it);
}
} // 将 pos 与 pos - 1 分开。
然后对于操作 \([l, r]\) 区间,只需要暴力遍历指针就行了。(怎么暴力怎么来)
只有区间赋值那里需要搞搞操作,一次可以抹去一连续区间的迭代器。
也就是 S.erase(itl, itr) 会把 [itl, itr) 的所有元素全部删除,复杂度也挺优秀的。(比单个删除快)
然后这样操作后复杂度就变成 \(O(m \log n)\) 的,十分的优秀。
总结
乱搞大法好啊!!\(o)/~
代码
好写好调,真是一个优秀的暴力做法。
#include <bits/stdc++.h>
#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, int> PII;
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }
inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
}
void File() {
#ifdef zjp_shadow
freopen ("C.in", "r", stdin);
freopen ("C.out", "w", stdout);
#endif
}
const int N = 1e5 + 1e3;
int n, m, seed, v, a[N];
inline int rnd() {
int res = seed;
seed = (seed * 7ll + 13) % 1000000007;
return res;
}
inline int Get(int lim) {
return rnd() % lim + 1;
}
struct Node {
int l, r; mutable ll val;
Node(int l_ = 0, int r_ = 0, ll val_ = 0): l(l_), r(r_), val(val_) {
}
};
inline bool operator < (const Node &lhs, const Node &rhs) {
return lhs.l < rhs.l;
}
multiset<Node> S;
vector<PII> V;
void Split(int pos) {
auto it = S.lower_bound((Node){pos, 0, 0});
if (it == S.end() || it -> l > pos) {
-- it;
S.emplace(it -> l, pos - 1, it -> val);
S.emplace(pos, it -> r, it -> val);
S.erase(it);
}
}
int Mod;
inline int fpm(int x, int power) {
int res = 1;
for (; power; power >>= 1, x = 1ll * x * x % Mod)
if (power & 1) res = 1ll * res * x % Mod;
return res;
}
int main () {
File();
n = read(); m = read(); seed = read(); v = read();
For (i, 1, n) a[i] = Get(v);
for (int i = 1, j = 1; i <= n; i = j) {
while (a[j] == a[i]) ++ j;
S.emplace(i, j - 1, a[i]);
}
For (i, 1, m) {
int opt = Get(4), l = Get(n), r = Get(n), x, y;
if (l > r) swap(l, r);
x = opt == 3 ? Get(r - l + 1) : Get(v);
y = opt == 4 ? Get(v) : 0;
Split(l); if (r < n) Split(r + 1);
auto itl = S.lower_bound((Node) {l, 0, 0}),
itr = S.upper_bound((Node) {r, 0, 0});
if (opt == 1) {
for (auto it = itl; it != itr; ++ it)
it -> val += x;
}
if (opt == 2) {
S.erase(itl, itr);
S.emplace(l, r, x);
}
if (opt == 3) {
V.clear();
for (auto it = itl; it != itr; ++ it)
V.pb(mp(it -> val, it -> r - it -> l + 1));
sort(V.begin(), V.end());
for (auto it : V)
if (x <= it.sec) {
printf ("%lld\n", it.fir); break;
} else x -= it.sec;
}
if (opt == 4) {
int ans = 0; Mod = y;
for (auto it = itl; it != itr; ++ it)
ans = (ans + 1ll * fpm(it -> val % Mod, x) * (it -> r - it -> l + 1)) % Mod;
printf ("%d\n", ans);
}
}
return 0;
}
Codeforces Round #449 (Div. 1) Willem, Chtholly and Seniorious (ODT维护)的更多相关文章
- Codeforces Round #449 (Div. 2) B. Chtholly's request【偶数位回文数】
B. Chtholly's request time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #449 (Div. 2)
Codeforces Round #449 (Div. 2) https://codeforces.com/contest/897 A #include<bits/stdc++.h> us ...
- Codeforces Round #449 (Div. 1)C - Willem, Chtholly and Seniorious
ODT(主要特征就是推平一段区间) 其实就是用set来维护三元组,因为数据随机所以可以证明复杂度不超过O(NlogN),其他的都是暴力维护 主要操作是split,把区间分成两个,用lowerbound ...
- Codeforces Round #449 (Div. 2)-897A.Scarborough Fair(字符替换水题) 897B.Chtholly's request(处理前一半) 897C.Nephren gives a riddle(递归)
A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #449 (Div. 2) D. Ithea Plays With Chtholly
题目链接 交互题. 题意:给你三个数n,m,k.让你完成至多m次互动,每次给你一个q,让你从n个位置选一个位置放这个数,覆盖已经放过的数.让你再m次使得n个位置的数不递减,达到直接退出. 解法:暴力, ...
- Codeforces Round #449 (Div. 2)ABCD
又掉分了0 0. A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #449 (Div. 2) A. Scarborough Fair【多次区间修改字符串】
A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #449 Div. 2 A B C (暂时)
A. Scarborough Fair 题意 对给定的长度为\(n\)的字符串进行\(m\)次操作,每次将一段区间内的某一个字符替换成另一个字符. 思路 直接模拟 Code #include < ...
- Codeforces Round #449 Div. 1
B:注意到nc/2<=m,于是以c/2为界决定数放在左边还是右边,保证序列满足性质的前提下替换掉一个数使得其更靠近边界即可. #include<iostream> #include& ...
随机推荐
- 通过Webstorm上传代码到Github、更新代码后同步到github及克隆github代码到本地的方法
导读: Github做为IT爱好者分享代码的一个知名的平台,广受大家喜欢,那么我们平时该怎么将自己写的代码上传到github上面保存并且提供给其他人参考? 我想方法不外乎如下几个: 1.直接在gith ...
- 使用git将本地项目推送到码云私有仓库
https://blog.csdn.net/qq_33876553/article/details/80111946 2018年04月27日 19:53:33 桥路丶 阅读数:2958 前言 之前博主 ...
- semantic-ui 容器与栅格
semantic中可以指定one-sixteen这16个单词来指定网格column所占的长度.也就是说,在网页中,一行最多只有16个column,超过16个之后,自动移到下一行. 栅格可以使用i,di ...
- vue组件封装选项卡
<template> <myMenu :arr='arr' :arrcontent='content'></myMenu> </template> &l ...
- springboot注解@SpringBootApplication分析
@SpringBootApplication注解用在Spring Boot的入口类上面,是Spring Boot提供的应用启动相关的注解. 直接上注解的源码: @Target(ElementType. ...
- Oracle RMAN备份与还原
RMAN在数据库服务器的帮助下实现数据库文件.控制文件.数据库文件与控制文件的映像副本.归档日志文件.数据库服务器参数文件的备份. RMAN的特点: (1) 支持增量备份:传统的exp与expdp备份 ...
- embed标签的flash层级太高问题
因为客户要求,项目得兼容IE的兼容模式 页面到了flash都会遮挡底部悬浮的导航. 改变浮动窗口和embed的层级还是不可以.应该不是层级的关系. 最后百度解决方案:在embed标签内添加了wmode ...
- MyBatis全局配置文件的各项标签3
mapper 将sql映射注册到全局配置中,这个我们在上一章已经使用过了, resource 这个属性是用来引用类路径下的sql映射文件 url 这个属性是用来引用网络路径或磁盘路径下的sql映射文件 ...
- 免费苹果账号(apple id)申请ios证书p12真机调试
HBuilder可以直接打包越狱版的ipa包,但需要越狱手机才能安装,如果需要安装到没越狱的手机安装,需要自己申请ios证书打包. 一般是需要一个付费了的苹果开发者账号才能申请ios证书打包. 这里介 ...
- Android——MaterialDesign之四 FloatingActionButton、Snackbar、CoordinaterLayout
FloatingActionButton 悬浮按钮,默认colorAccent来作为按钮的颜色 <android.support.design.widget.FloatingActionButt ...