这里介绍以个小$trick$,民间流传为$Old Driver Tree$,实质上就是$set$维护线段。

我们将所有连续一段权值相同的序列合并成一条线段,扔到$set$里去,于是$set$里的所有线段的并就是原序列,并且都不相交。

我们在操作的时候很暴力,每次把$[l, r]$的线段抠出来,暴力枚举一遍算答案。对于每一个非区间赋值的操作,最多断两条线段,新加两条线段。

实现起来很方便,思路也非常简单,但是局限性也很明显,因为其复杂度是基于随机的,并且必须存在区间赋值的操作。

但$set$维护线段的技巧还是很常见的,我用$set$和$map$都实现的一遍,发现这里用$map$相当好写。

$set$的实现:

#include <set>
#include <cstdio>
#include <algorithm>
using namespace std; typedef long long LL;
const int N = ; int n, m, vmax, tp;
pair<LL, int> st[N]; struct Seg {
int l, r; LL v;
friend bool operator < (Seg a, Seg b) {
return (a.l != b.l)? (a.l < b.l) : (a.r < b.r);
}
};
multiset<Seg> S;
typedef multiset<Seg>::iterator Saber; LL Pow(LL x, int b, int p, LL re = ) {
for (x %= p; b; b >>= , x = x * x % p)
if (b & ) re = re * x % p;
return re;
} namespace R {
const int mod = 1e9 + ;
int seed, ret;
int rnd() {
ret = seed;
seed = (seed * 7LL + ) % mod;
return ret;
}
} int main() {
scanf("%d%d%d%d", &n, &m, &R::seed, &vmax);
for (int i = ; i <= n; ++i)
S.insert((Seg){ i, i, R::rnd() % vmax + });
for (int op, l, r, x, y; m; --m) {
op = (R::rnd() & ) + ;
l = R::rnd() % n + ;
r = R::rnd() % n + ;
if (l > r) swap(l, r);
if (op == ) x = R::rnd() % (r - l + ) + ;
else x = R::rnd() % vmax + ;
if (op == ) y = R::rnd() % vmax + ; Saber p = --S.lower_bound((Seg){ l + , , });
Saber q = --S.lower_bound((Seg){ r + , , });
if ((*p).l < l) S.insert((Seg){ (*p).l, l - , (*p).v });
if (r < (*q).r) S.insert((Seg){ r + , (*q).r, (*q).v });
if (op == ) {
for (Saber z = p, rin; z != q; ) {
rin = z; ++z;
S.erase(rin);
}
S.erase(q);
S.insert((Seg){ l, r, x });
continue;
}
Saber np, nq;
if (p == q) {
np = nq = S.insert((Seg){ l, r, (*p).v });
S.erase(p);
} else {
np = S.insert((Seg){ l, (*p).r, (*p).v });
nq = S.insert((Seg){ (*q).l, r, (*q).v });
S.erase(p); S.erase(q);
}
if (op == ) {
Seg ins;
for (Saber z = np, rin; z != nq; ) {
rin = z; ++z;
ins = *rin; ins.v += x;
S.erase(rin); S.insert(ins);
}
ins = *nq; ins.v += x;
S.erase(nq); S.insert(ins);
}
if (op == ) {
tp = ;
for (Saber z = np; z != nq; ++z)
st[++tp] = make_pair((*z).v, (*z).r - (*z).l + );
st[++tp] = make_pair((*nq).v, (*nq).r - (*nq).l + );
sort(st + , st + + tp);
for (int i = ; i <= tp; x -= st[i].second, ++i) {
if (st[i].second >= x) {
printf("%lld\n", st[i].first);
x = ;
break;
}
}
if (x > ) {
puts("I love you, love you forever.");
return ;
}
}
if (op == ) {
int ans = ;
for (Saber z = np; z != nq; ++z)
ans = (ans + Pow((*z).v, x, y) * ((*z).r - (*z).l + )) % y;
ans = (ans + Pow((*nq).v, x, y) * ((*nq).r - (*nq).l + )) % y;
printf("%d\n", ans);
}
} return ;
}

$map$的实现:

#include <map>
#include <cstdio>
#include <algorithm>
using namespace std; typedef long long LL;
const int N = ; int n, m, vmax, tp;
pair<LL, int> st[N];
map<int, LL> M; LL Pow(LL x, int b, int p, LL re = ) {
for (x %= p; b; b >>= , x = x * x % p)
if (b & ) re = re * x % p;
return re;
} namespace R {
const int mod = 1e9 + ;
int seed, ret;
int rnd() {
ret = seed;
seed = (seed * 7LL + ) % mod;
return ret;
}
} int main() {
scanf("%d%d%d%d", &n, &m, &R::seed, &vmax);
for (int i = ; i <= n; ++i)
M[i] = R::rnd() % vmax + ;
M[n + ] = ;
for (int op, l, r, x, y; m; --m) {
op = (R::rnd() & ) + ;
l = R::rnd() % n + ;
r = R::rnd() % n + ;
if (l > r) swap(l, r);
if (op == ) x = R::rnd() % (r - l + ) + ;
else x = R::rnd() % vmax + ;
if (op == ) y = R::rnd() % vmax + ; auto p = --M.upper_bound(l);
auto q = M.upper_bound(r);
if (p->first < l) M[l] = p->second, ++p;
if (r + < q->first) --q, M[r + ] = q->second, ++q;
if (op == ) {
for (; p != q; ++p) p->second += x;
}
if (op == ) {
while (p != q) M.erase(p++);
M[l] = x;
}
if (op == ) {
tp = ;
for (auto rin = p; p != q; ++p) {
st[++tp] = { p->second, (++rin)->first - p->first };
}
sort(st + , st + + tp);
for (int i = ; i <= tp; x -= st[i].second, ++i) {
if (x <= st[i].second) {
printf("%lld\n", st[i].first);
break;
}
}
}
if (op == ) {
int ans = ;
for (auto rin = p; p != q; ++p) {
ans = (ans + Pow(p->second, x, y) * ((++rin)->first - p->first)) % y;
}
printf("%d\n", ans);
}
} return ;
}

【Cf #449 C】Willem, Chtholly and Seniorious(set维护线段)的更多相关文章

  1. CF&&CC百套计划1 Codeforces Round #449 C. Willem, Chtholly and Seniorious (Old Driver Tree)

    http://codeforces.com/problemset/problem/896/C 题意: 对于一个随机序列,执行以下操作: 区间赋值 区间加 区间求第k小 区间求k次幂的和 对于随机序列, ...

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

  3. Willem, Chtholly and Seniorious

    Willem, Chtholly and Seniorious https://codeforces.com/contest/897/problem/E time limit per test 2 s ...

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

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

  5. 【ODT】cf896C - Willem, Chtholly and Seniorious

    仿佛没用过std::set Seniorious has n pieces of talisman. Willem puts them in a line, the i-th of which is ...

  6. [Codeforces896C] Willem, Chtholly and Seniorious (ODT-珂朵莉树)

    无聊学了一下珂朵莉树 珂朵莉树好哇,是可以维护区间x次方和查询的高效数据结构. 思想大致就是一个暴力(相对而言)的树形数据结构 lxl毒瘤太强了,发明了ODT算法(Old Driver Tree老司机 ...

  7. [CF896C]Willem, Chtholly and Seniorious(珂朵莉树)

    https://www.cnblogs.com/WAMonster/p/10181214.html 主要用于支持含有较难维护的区间操作与查询的问题,要求其中区间赋值操作(assign())是纯随机的. ...

  8. 2019.01.19 codeforces896C.Willem, Chtholly and Seniorious(ODT)

    传送门 ODTODTODT出处(万恶之源) 题目简述: 区间赋值 区间加 区间所有数k次方和 区间第k小 思路:直接上ODTODTODT. 不会的点这里 代码: #include<bits/st ...

  9. cf896C. Willem, Chtholly and Seniorious(ODT)

    题意 题目链接 Sol ODT板子题.就是用set维护连续段的大暴力.. 然鹅我抄的板子本题RE提交AC??.. 具体来说,用50 50 658073485 946088556这个数据测试下面的代码, ...

随机推荐

  1. lua字符串类型

    Lua中字符串结构体的定义是: typedef union TString { L_Umaxalign dummy; /* ensures maximum alignment for strings ...

  2. NO--14 微信小程序,左右联动二

    上一篇讲解了左=>右联动,那个还比较简单,本篇写剩下比较核心的部分,也是本次开发过程中遇到最难的部分,右=>左联动,先简单看一下演示   右左联动.gif 一.关键技术: (1) 小程序 ...

  3. [Unity3D]MonoDeveloper快捷键(补全代码补全引用中文乱码tab转空格)

    Hello亲爱的观众朋友们大家好,我是09. vs支持各种插件,一般推荐用vs.不过总有人(例如我)由于各种原因用MonoDeveloper.苦于每次上网找各种设置,此处集中写下我用MonoDevel ...

  4. 关于几个与IO相关的重要概念

    1.读/写IO 读IO就是发指令从磁盘读取某段序号连续的扇区内容.指令一般是通知磁盘开始扇区位置,然后给出需要从这个初始扇区往后读取的连续扇区个数,同时给出动作是读还是写.磁盘收到这条指令就会按照指令 ...

  5. Kubernetes网络方案 Flannel和calico

    摘抄某博客 1.   Flannel Flannel是为kubernetes设计的一个非常简洁的多节点三层网络方案,解决不同host上的容器互联问题,原理是为每个host分配一个subnet,容器从此 ...

  6. Hadoop错误码速查

    经常遇到的exception是:PipeMapRed.waitOutputThreads(): subprocess failed with code N "OS error code 1: ...

  7. flume handler

    1.classpath classpath中需要这两项:Flume Agent configuration file and the second are the Flume client jars ...

  8. gulp-babel 阻止了js文件编译的进程?

    现象 :   编译打包的js没有输出到目标文件夹里,只是单单的生成了一个目标目录,目录里没有文件 解决方法:gulp-babel ^8.0.0 使用了 ^7.0.1的 依赖插件.统一gulp-babe ...

  9. Linux 基础入门第一次实验笔记

    第一节.实验介绍 本节主要介绍 Linux 的历史,Linux 与 Windows 的区别等入门知识.如果你已经有过充分的了解,可以跳过本节,直接进入下一个实验. 一.Linux 为何物 Linux ...

  10. fullPage全屏高度自适应

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...