题意

给你一个长为 \(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. 使用VBA进行JS加密的反混淆,还原JS代码。

    本文地址:http://www.cnblogs.com/Charltsing/p/JSEval.html 联系QQ:564955427 类似下面的代码是登陆 全国企业信用信息公示系统(安徽)(网址:h ...

  2. Echarts x轴文本内容太长的几种解决方案

    Echarts 标签中文本内容太长的时候怎么办 ? - 1对文本进行倾斜 在xAxis.axisLabe中修改rotate的值 xAxis: { data: ["衬衫11111", ...

  3. Elasticsearch IK+pinyin

    如何在Elasticsearch中安装中文分词器(IK+pinyin)   如果直接使用Elasticsearch的朋友在处理中文内容的搜索时,肯定会遇到很尴尬的问题——中文词语被分成了一个一个的汉字 ...

  4. CopyOnWriteArrayList源码分析

    基于jdk1.7源码 一.无锁容器 CopyOnWriteArrayList是JDK5中添加的新的容器,除此之外,还有CopyOnWriteArraySet.ConcurrentHahshMap和Co ...

  5. Linux 环境变量梳理

    Linux中的环境变量有两种:全局变量和局部变量: 定义.访问.删除局部变量 查看全局变量 可以使用printenv或者env命令来打印所有的全局变量. 访问某一项全局变量,可以使用printenv ...

  6. MYSQL: 1292 - Truncated incorrect DOUBLE value: '184B3C0A-C411-47F7-BE45-CE7C0818F420'

    MySQL Bugs: #63112: Truncated incorrect DOUBLE valuehttps://bugs.mysql.com/bug.php?id=63112 Error Co ...

  7. 基于redis实现的点赞功能设计思路详解

    点赞其实是一个很有意思的功能.基本的设计思路有大致两种, 一种自然是用mysql等 数据库直接落地存储, 另外一种就是利用点赞的业务特征来扔到redis(或memcache)中, 然后离线刷回mysq ...

  8. 如何入门vue之二

    学习完指令之后我们需要学习的就是组件. 在学习组件前我们要了解一下 methods 用来处理事件的. computed用来计算属性  他就是类似于data一样只不过是动态的处理数据 里面写的方法当成属 ...

  9. 调整分区大小 转载--------------http://blog.csdn.net/perfectzq/article/details/73606119

    centos7重新调整分区大小 centos 7 调整 root 和 home 的容量大小 查看磁盘的空间大小: df -h  备份/home : cp -r /home/ homebak/ 卸载​  ...

  10. vue图片被加了盗链

    https://www.cnblogs.com/dongcanliang/archive/2017/04/01/6655061.html <meta name="referrer&qu ...