Solution -「UOJ #46」玄学
\(\mathcal{Description}\)
Link.
给定序列 \(\{a_n\}\) 和 \(q\) 次操作,操作内容如下:
- 给出 \(l,r,k,b\),声明一个修改方案,表示 \(\forall i\in[l,r],~a_i\leftarrow (ka_i+b)\bmod m\)。
- 给出 \(l,r,x\),求将第 \(l\) 到第 \(r\) 个修改方案作用于序列时,\(a_x\) 的值。
强制在线,\(n\le10^5\),\(q\le6\times10^5\)。
\(\mathcal{Solution}\)
一种类似在线建线段树的 trick。
若允许离线,自然可以建立关于修改方案的线段树,每个结点维护对应区间内的修改依次作用后,每个 \(a\) 值会变成的 \(ka+b\)。不同的 \((k,b)\) 形成的区间个数是与结点对应区间长度同阶的,所以一共有 \(\mathcal O(n\log n)\) 个区间。查询时,在每个区间内二分找到会影响 \(x\) 位置的 \((k,b)\),更新答案,单次复杂度是 \(\mathcal O(\log^2n)\) 的。
转为在线,注意到当线段树结点对应区间内的修改操作全部声明时,这个结点的信息才有效,所以当且仅当区间内修改操作全部声明时,在结点处归并左右儿子信息,均摊复杂度就是离线建树的复杂度。最终复杂度为 \(\mathcal O(n\log n+q\log^2n)\)。
\(\mathcal{Code}\)
/*~Rainybunny~*/
#include <cstdio>
#include <vector>
#include <cassert>
#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )
inline int rint() {
int x = 0, f = 1, s = getchar();
for ( ; s < '0' || '9' < s; s = getchar() ) f = s == '-' ? -f : f;
for ( ; '0' <= s && s <= '9'; s = getchar() ) x = x * 10 + ( s ^ '0' );
return x * f;
}
template<typename Tp>
inline void wint( Tp x ) {
if ( x < 0 ) putchar( '-' ), x = -x;
if ( 9 < x ) wint( x / 10 );
putchar( x % 10 ^ '0' );
}
const int MAXN = 1e5, MAXQ = 6e5;
int type, n, M, q, ary[MAXN + 5];
inline int mul( const long long a, const int b ) { return a * b % M; }
inline int add( const long long a, const int b ) { return ( a + b ) % M; }
struct Section {
int l, r, k, b;
inline bool operator < ( const Section& s ) const {
return l != s.l ? l < s.l : r < s.r;
}
};
typedef std::vector<Section> Atom;
inline Atom mergeSec( const Atom& u, const Atom& v ) {
static Atom ret; ret.clear();
int i = 0, j = 0, las = 1, us = int( u.size() ), vs = int( v.size() );
while ( i < us && j < vs ) {
if ( u[i].r >= v[j].r ) {
ret.push_back( { las, v[j].r, mul( u[i].k, v[j].k ),
add( mul( v[j].k, u[i].b ), v[j].b ) } );
las = v[j].r + 1;
if ( u[i].r == v[j++].r ) ++i;
} else {
ret.push_back( { las, u[i].r, mul( u[i].k, v[j].k ),
add( mul( v[j].k, u[i].b ), v[j].b ) } );
las = u[i++].r + 1;
}
}
assert( las == n + 1 );
return ret;
}
struct SegmentTree {
Atom sec[MAXQ << 2];
int upc[MAXQ << 2];
inline void insert( const int u, const int l, const int r,
const int x, const int i, const int j, const int a, const int b ) {
if ( l == r ) {
++upc[u];
if ( i > 1 ) sec[u].push_back( { 1, i - 1, 1, 0 } );
sec[u].push_back( { i, j, a, b } );
if ( j < n ) sec[u].push_back( { j + 1, n, 1, 0 } );
return ;
}
int mid = l + r >> 1;
if ( x <= mid ) insert( u << 1, l, mid, x, i, j, a, b );
else insert( u << 1 | 1, mid + 1, r, x, i, j, a, b );
if ( ( upc[u] = upc[u << 1] + upc[u << 1 | 1] ) == r - l + 1 ) {
sec[u] = mergeSec( sec[u << 1], sec[u << 1 | 1] );
}
}
inline void query( const int u, const int l, const int r,
const int ql, const int qr, const int x, int& v ) {
if ( ql <= l && r <= qr ) {
int sid = std::upper_bound( sec[u].begin(), sec[u].end(),
Section{ x + 1, 0, 0, 0 } ) - sec[u].begin() - 1;
assert( 0 <= sid && sid < int( sec[u].size() ) );
assert( sec[u][sid].l <= x && x <= sec[u][sid].r );
v = add( mul( v, sec[u][sid].k ), sec[u][sid].b );
return ;
}
int mid = l + r >> 1;
if ( ql <= mid ) query( u << 1, l, mid, ql, qr, x, v );
if ( mid < qr ) query( u << 1 | 1, mid + 1, r, ql, qr, x, v );
}
} sgt;
int main() {
type = rint() & 1, n = rint(), M = rint();
rep ( i, 1, n ) ary[i] = rint();
q = rint();
for ( int qid = 1, ans = 0, cnt = 0, op, i, j, a, b; qid <= q; ++qid ) {
op = rint(), i = rint(), j = rint(), a = rint();
if ( type ) i ^= ans, j ^= ans;
if ( op == 1 ) {
b = rint();
sgt.insert( 1, 1, q, ++cnt, i, j, a, b );
} else {
if ( type ) a ^= ans;
sgt.query( 1, 1, q, i, j, a, ans = ary[a] );
wint( ans ), putchar( '\n' );
}
}
return 0;
}
Solution -「UOJ #46」玄学的更多相关文章
- Solution -「UOJ #87」mx 的仙人掌
\(\mathcal{Description}\) Link. 给出含 \(n\) 个结点 \(m\) 条边的仙人掌图.\(q\) 次询问,每次询问给出一个点集 \(S\),求 \(S\) 内 ...
- Solution -「UOJ #450」复读机
\(\mathcal{Description}\) Link. 求从 \(m\) 种颜色,每种颜色无限多的小球里选 \(n\) 个构成排列,使得每种颜色出现次数为 \(d\) 的倍数的排列方案 ...
- Solution -「ARC 104E」Random LIS
\(\mathcal{Description}\) Link. 给定整数序列 \(\{a_n\}\),对于整数序列 \(\{b_n\}\),\(b_i\) 在 \([1,a_i]\) 中等概率 ...
- Solution -「UNR #5」「UOJ #671」诡异操作
\(\mathcal{Desciprtion}\) Link. 给定序列 \(\{a_n\}\),支持 \(q\) 次操作: 给定 \(l,r,v\),\(\forall i\in[l,r], ...
- Solution -「JOISC 2020」「UOJ #509」迷路的猫
\(\mathcal{Decription}\) Link. 这是一道通信题. 给定一个 \(n\) 个点 \(m\) 条边的连通无向图与两个限制 \(A,B\). 程序 Anthon ...
- Solution -「UR #21」「UOJ #632」挑战最大团
\(\mathcal{Description}\) Link. 对于简单无向图 \(G=(V,E)\),定义它是"优美"的,当且仅当 \[\forall\{a,b,c,d\ ...
- Solution -「UR #2」「UOJ #32」跳蚤公路
\(\mathcal{Description}\) Link. 给定一个 \(n\) 个点 \(m\) 条边的带权有向图,每条边还有属性 \(s\in\{-1,0,1\}\).对于每个 \(u ...
- Solution -「CTS 2019」「洛谷 P5404」氪金手游
\(\mathcal{Description}\) Link. 有 \(n\) 张卡牌,第 \(i\) 张的权值 \(w_i\in\{1,2,3\}\),且取值为 \(k\) 的概率正比于 \ ...
- Solution -「BZOJ 3812」主旋律
\(\mathcal{Description}\) Link. 给定含 \(n\) 个点 \(m\) 条边的简单有向图 \(G=(V,E)\),求 \(H=(V,E'\subseteq E)\ ...
随机推荐
- java 线程 总结
1.前言 (1)线程的上一级是进程,进程是程序的一次执行过程,是系统运行程序的基本单位,因此进程是动态的. (2)线程与进程相似,但线程是一个比进程更小的执行单位,也被称为轻量级进程.一个进程在其执行 ...
- android入门--环境搭建
运行环境 windows 7 下载地址 环境下载 最近开接触Android(安卓)嵌入式开发,首要问题是搭建Andoid开发环境,由于本人用的是windows7的笔记本,也就只能到Windows中搭建 ...
- 如何向内核提交补丁?——FirstKernelPatch
参考 https://kernelnewbies.org/FirstKernelPatch
- 梯度下降法实现(Python语言描述)
原文地址:传送门 import numpy as np import matplotlib.pyplot as plt %matplotlib inline plt.style.use(['ggplo ...
- VictoriaMetrics:使用vmctl来实现vm-storage向victoria-metrics-prod(单机版)迁移数据
前一篇提到了,vm-storage的备份数据,无法被victoria-metrics-prod(单机版)读取. 继续翻文档发现vmctl可以实现这个效果: 1.启动vm-restore恢复数据 vmr ...
- js复制标题和链接
问题 常常在写博客和作业时候,需要附上参考链接. 希望可以一键得到标题和链接. 解决方案 普通元素 可以使用findid然后复制 但是标题无法使用 <!DOCTYPE html> < ...
- manjora20使用体验
#第一次评估 适合小白日常办公,不适合计算机研究人员 ##优点 稳定流畅,运行速度比Ubuntu快很多 界面优美,UI很好 强大的aur软件库应有尽有,几乎是所有软件都有 中国源和arch社区提供的包 ...
- javascript的AMD规法--esl与requirejs浅介。
AMD规范,全称是Asynchronous Module Definition,即异步模块加载机制.从它的规范描述页面看,AMD很短也很简单,但它却完整描述了模块的定义,依赖关系,引用关系以及加载机制 ...
- shell $用法
$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...
- 抽象类 final
抽象类 1.用abstract关键字来修饰一个类时,这个类叫做抽象类,用abstract来修饰一个方法时,这个方法叫抽象方法. 2.含有抽象方法的类必须被声明为抽象类,抽象类必须被继承,抽象方法必须被 ...