题意

给你一个长为 \(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维护)的更多相关文章

  1. 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 ...

  2. Codeforces Round #449 (Div. 2)

    Codeforces Round #449 (Div. 2) https://codeforces.com/contest/897 A #include<bits/stdc++.h> us ...

  3. Codeforces Round #449 (Div. 1)C - Willem, Chtholly and Seniorious

    ODT(主要特征就是推平一段区间) 其实就是用set来维护三元组,因为数据随机所以可以证明复杂度不超过O(NlogN),其他的都是暴力维护 主要操作是split,把区间分成两个,用lowerbound ...

  4. 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 ...

  5. Codeforces Round #449 (Div. 2) D. Ithea Plays With Chtholly

    题目链接 交互题. 题意:给你三个数n,m,k.让你完成至多m次互动,每次给你一个q,让你从n个位置选一个位置放这个数,覆盖已经放过的数.让你再m次使得n个位置的数不递减,达到直接退出. 解法:暴力, ...

  6. Codeforces Round #449 (Div. 2)ABCD

    又掉分了0 0. A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. 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 ...

  8. Codeforces Round #449 Div. 2 A B C (暂时)

    A. Scarborough Fair 题意 对给定的长度为\(n\)的字符串进行\(m\)次操作,每次将一段区间内的某一个字符替换成另一个字符. 思路 直接模拟 Code #include < ...

  9. Codeforces Round #449 Div. 1

    B:注意到nc/2<=m,于是以c/2为界决定数放在左边还是右边,保证序列满足性质的前提下替换掉一个数使得其更靠近边界即可. #include<iostream> #include& ...

随机推荐

  1. 利用lnmp一键安装的php环境忘记mysql,root用户密码解决方法

    1.cd /lnmp1.5/tools/ 2.sh reset_mysql_root_password.sh 这样,即可完成修改!

  2. centos 7 network.service control process exited

    一.service network restart 出错 问题描述: vmware 12 下centos 7 网络模式,NAT 昨晚作者打算更新自己虚拟机python,发现没网络ping www.ba ...

  3. 文本文档中各字母出现次数汇总(java)

    package 字母频率统计; import java.io.*; public class Inputfile { public static void main(String args[]) { ...

  4. setState的参数接收函数

  5. switch变种玩法

    标准版本: switch(表达式) { case 值1: 语句体1; break; case 值2: 语句体2; break; ... default: 语句体n+; break; } switch: ...

  6. mysql异常:Packet for query is too large (10240 > 1024). You can change this value

    出现这个问题的原因是:mysql的配置文件中 max_allowed_packet 设置过小,mysql根据配置文件会限制server接受的数据包大小. 还有人会说我操作的数据量明显没有超过这个值为啥 ...

  7. png8、16、24、32位的区别

    我们都知道一张图片可以保存为很多种不同的格式,比如bmp/png/jpeg/gif等等.这个是从文件格式的角度看,我们抛开文件格式,看图片本身,我们可以分为8位, 16位, 24位, 32位等. 单击 ...

  8. phpstorm显示页面不停的在indexing转圈中,并且文件名还一直在刷新

    打开 File下的 Invalidate Caches / Restart...下的 Invalidate and Restart. 便可以了 ......

  9. 在linux命令下访问url

    1.elinks - lynx-like替代角色模式WWW的浏览器 例如: elinks --dump http://www.baidu.com 2.wget 这个会将访问的首页下载到本地 [root ...

  10. Maven 项目 查找指定包的引用位置