【Cf #449 C】Willem, Chtholly and Seniorious(set维护线段)

时间:2022-09-03 17:47:55

【Cf #449 C】Willem, Chtholly and Seniorious(set维护线段)

这里介绍以个小$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&amp&semi;&amp&semi;CC百套计划1 Codeforces Round &num;449 C&period; Willem&comma; Chtholly and Seniorious &lpar;Old Driver Tree&rpar;

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

  2. Codeforces Round &num;449 &lpar;Div&period; 1&rpar; Willem&comma; Chtholly and Seniorious &lpar;ODT维护&rpar;

    题意 给你一个长为 \(n\) 的序列 \(a_i\) 需要支持四个操作. \(1~l~r~x:\) 把 \(i \in [l, r]\) 的 \(a_i\) 加 \(x\) . \(2~l~r~x: ...

  3. Willem&comma; Chtholly and Seniorious

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

  4. Codeforces Round &num;449 &lpar;Div&period; 1&rpar;C - Willem&comma; Chtholly and Seniorious

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

  5. 【ODT】cf896C - Willem&comma; Chtholly and Seniorious

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

  6. &lbrack;Codeforces896C&rsqb; Willem&comma; Chtholly and Seniorious &lpar;ODT-珂朵莉树&rpar;

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

  7. &lbrack;CF896C&rsqb;Willem&comma; Chtholly and Seniorious&lpar;珂朵莉树&rpar;

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

  8. 2019&period;01&period;19 codeforces896C&period;Willem&comma; Chtholly and Seniorious(ODT)

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

  9. cf896C&period; Willem&comma; Chtholly and Seniorious&lpar;ODT&rpar;

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

随机推荐

  1. 删除MSSQL中所有表的数据

    CREATE PROCEDURE sp_DeleteAllDataASEXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'EXEC ...

  2. fatal error C1854&colon; 无法覆盖在创建对象文件&period;obj”的预编译头过程中形成的信息

    原因: 将stdafx.cpp 的预编译头属性  由 创建预编译头(/Yc) 改成了 使用预编译头(/Yu) 解决: 改回为 创建预编译头(/Yc) 参考文档 http://blog.csdn.net ...

  3. Linux 命令之last命令详解

    last:命令解释show listing of last logged in users 指令所在路径:/usr/bin/last 命令输出字段介绍: 第一列:用户名 第二列:终端位置.pts/0 ...

  4. prototype原型链继承

    依旧是恶补js基础,上代码: 1.定义父类及父类方法 function Animal(){ this.name = "动物"; } Animal.prototype.eat = f ...

  5. Eclipse 为jar包加入 Java Source和Javadoc(如何向Eclipse中导入源码和doc)

    : 当我们在MyEclipse中加入Struct,只是引入了jar包,这时使用jar包里面的类,是无法看到源码的,看到的只是这样 是反编译后的一些东西 加入源码 那么如果看到源码呢,就需要导入了 找到 ...

  6. &lbrack;C&num;参考&rsqb;byte数组和Image的相互转换

    功能需求 1.把一张图片(png bmp jpeg bmp gif)转换为byte数组在内存中操作. 2.把内存中的byte数组转换成Image对象,赋值给相应的控件显示. 3.从图片byte数组得到 ...

  7. C&plus;&plus;中的动态链接库

    一,动态链接库的基本概念 1.DLL(Dynamic Linker Library)的概念 可以把DLL看做是一个仓库,它提供了一些可以直接拿来用的变量,函数或者类(但是不建议从DLL中获取变量),在 ...

  8. Java开发笔记(八十八)文件字节I/O流

    前面介绍了如何使用字符流读写文件,并指出字符流工具的处理局限,进而给出随机文件工具加以改进.随机文件工具除了支持访问文件内部的任意位置,更关键的一点是通过字节数组读写文件数据,采取字节方式比起字符方式 ...

  9. dubbo 概述和使用

    dubbo核心概念 apache是一款高性能.轻量级的开源java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现 官网:http://dubbo ...

  10. fastext 中文文本分类

    1. 输入文本预处理, 通过jieba分词, 空格" "拼接文本串.  每行一个样本, 最后一个单词为双下划线表明label,  __label__'xxx' . eg: 邱县 继 ...