这里介绍以个小$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. Unity在OpenGL模式下Shader编译报错

    报错信息 GLSL compilation failed: 0(21) : error C7528: OpenGL reserves names containing '__' 双击报错VS自动打开V ...

  2. SQL Server存储过程用法介绍

    存储过程其实就是已预编译为可执行过程的一个或多个SQL语句. 通过调用和传递参数即可完成该存储过程的功能. 前面有介绍过存储过程的一些语法,但是没有详细示例,今天我们来一起研究一下存储过程. 提高性能 ...

  3. JS中判断对象是不是数组的方法

    JavaScript中检测对象的方法 1.typeof操作符 这种方法对于一些常用的类型来说那算是毫无压力,比如Function.String.Number.Undefined等,但是要是检测Arra ...

  4. PHP版本对比【转】

    其他历史http://www.cnblogs.com/yjf512/p/3588466.html php5.3 改动: 1.realpath() 现在是完全与平台无关的. 结果是非法的相对路径比如FI ...

  5. 第24次Scrum会议(11/12)【欢迎来怼】

    一.小组信息 队名:欢迎来怼 小组成员 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/11/12 17:05~17:32,总计27min. 地 ...

  6. php之 常用的 流程管理

    1.流程管理的用法是什么样的? 2.怎么发起想要的流程? 3.审批的人要是怎么审批通过? 4.流程审核是不是要挨个走过? 一.要有数据库的内容的 肯定会有表的,首先就是用户表了,然后就是流程表,用户编 ...

  7. 校园跳蚤市场-Sprint计划(第二阶段)

  8. 项目冲刺Beta第二篇博客

    Beta版本冲刺计划安排 1.当天站立式会议照片: 2.工作分工: 团队成员 分工 张洪滨060  排行榜界面美化 陈敬轩059  注册成功界面美化 黄兴067  登录界面美化 林国梽068  答题界 ...

  9. js中的let\var\const

    在JavaScript中有三种声明变量的方式:var.let.const.下文给大家介绍js中三种定义变量的方式const, var, let的区别. 1.const定义的变量不可以修改,而且必须初始 ...

  10. 微信小程序之Flex布局

    微信小程序页面布局方式采用的是Flex布局.Flex布局,是W3c在2009年提出的一种新的方案,可以简便,完整,响应式的实现各种页面布局.Flex布局提供了元素在容器中的对齐,方向以及顺序,甚至他们 ...