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& ...
随机推荐
- eclipse、myeclipse写类时,自动生成注释
在类的上边/**+enter自动生成注释. 设置方法:Window--Prefences--Java--Code Style--Code Templates--Comments--Types--Edi ...
- 用python表白了!!!
用python 画一颗心,代码: import numpy as np import matplotlib.pyplot as plt x = np.linspace(-8 , 8, 1024) ...
- P66 整环的零元
R/I=0的零因子是0+I吗? 如果不是,那请问R/I的零因子是什么呢? R/I没有零因子 R/I的零元 是I中的元素定义的等价类 么 a是理想I的元素,自然也是R的元素
- 【kindle笔记】之 《解忧杂货店》-2018-3-13
[kindle笔记]读书记录-总 <解忧杂货店>-2018-3-13 东野的大ID加上此书的大ID,今天终于在回来天津的火车上一口气读完了. 此前在微信读书上看过这本书,只看了前一部分,感 ...
- Oracle的数据类型详述
数据类型 (1)字符型 CHAR: 定长最多(2000字节)特定情况下用 VARCHAR2:可变长度的字符串最多(4000字节) LONG:大文本类型最多(2个G) (2)数值型 NUMBER:可以是 ...
- vue的地图插件amap
https://www.jianshu.com/p/0011996b81e2(amap) npm install vue-amap --save
- Day 6-3 粘包现象
服务端: import socket import subprocess phone = socket.socket(family=socket.AF_INET, type=socket.SOCK_S ...
- Day3-1 函数
定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 语法: def calc(x, y): ...
- idea中 maven打包时时报错User setting file does not exist C:\Users\lenevo\.m2\setting.xml,
第一种错误 :idea中 maven打包时时报错User setting file does not exist C:\Users\lenevo\.m2\setting.xml, 解决方案如下:将ma ...
- java获取本机ip(排除虚拟机等一些ip)最终解,总算找到方法了
本文参考https://blog.csdn.net/u011809209/article/details/77236602 本文参考https://blog.csdn.net/yinshuomail/ ...