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)\ ...
随机推荐
- Win10如何更改C:\Users\下的用户名
详细操作步骤博文原址 : https://blog.csdn.net/wls666/article/details/103334152 但是,改完后会出现报错 这是微软应用商城出现问题 ,每次开机 ...
- 上传自己的pip模块
对于模块开发者本质上需要做3件事: 编写模块 将模块进行打包 上传到PyPI(需要先注册PyPI账号) 注册PyPI账号 安装上传工具 基于工具进行上传 对于模块的使用者来说,只需要做2件事: 通过p ...
- SQLAlchemy完全入门
最近想要学习SQLAlchemy, 发现网上的中文文档大多是机翻的, 读起来特别变扭, 因此对照着最新的英文文档梳理了一遍, 写下来记录一下 目前SQLAlchemy的版本为1.4.x, 风格处于1. ...
- 51 Nod 1133 不重叠的线段 (贪心算法)
原题链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1133 题目分析:感觉这到第不应该被分到二级算法题,比 109 ...
- SSRF打内网redis
0x00 redis基础 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用AN ...
- linux 下安装PostgreSql 并配置远程访问
1.官网下载PostgreSql 安装包 (https://www.enterprisedb.com/downloads/postgres-postgresql-downloads) 我下载的是 9. ...
- DEEP LEARNING WITH PYTORCH: A 60 MINUTE BLITZ | TENSORS
Tensor是一种特殊的数据结构,非常类似于数组和矩阵.在PyTorch中,我们使用tensor编码模型的输入和输出,以及模型的参数. Tensor类似于Numpy的数组,除了tensor可以在GPU ...
- java原码、反码、补码、位运算
1.对于有符号的数(java中的数都是有符号的) 二进制的最高位是符号位:0表示正数,1表示负数 正数的原码,反码,补码都一样 负数的反码=它的原码符号位不变,其它位取反 负数的补码=它的反码+1 0 ...
- 科技爱好者周刊(第 175 期):知识广度 vs 知识深度
这里记录每周值得分享的科技内容,周五发布. 本杂志开源(GitHub: ruanyf/weekly),欢迎提交 issue,投稿或推荐科技内容. 周刊讨论区的帖子<谁在招人?>,提供大量程 ...
- JVM专题1: 类和类加载机制
合集目录 JVM专题1: 类和类加载机制 Java对象的结构 在HotSpot虚拟机中, 对象在内存中存储的布局可以分为3块区域 对象头Header 实例数据Instance Data 对齐填充Pad ...