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)\ ...
随机推荐
- sql server - 修改表名、列名
EXEC sp_rename '旧表名', '新表名'; 例子 EXEC sp_rename 'saveremark', 'drawingLooking'; EXEC sp_rename '表名.[列 ...
- spring security +MySQL + BCryptPasswordEncoder 单向加密验证 + 权限拦截 --- 心得
1.前言 前面学习了 security的登录与登出 , 但是用户信息 是 application 配置 或内存直接注入进去的 ,不具有实用性,实际上的使用还需要权限管理,有些 访问接口需要某些权限才可 ...
- selenium实现并发
for循环和多线程 + selenium 实例一 for循环 # -*- coding: utf-8 -*- """ Datetime: 2019/6/22 Author ...
- List转换Map的三种方式
1.for循环 ... 2.使用guava Map<Long, User> maps = Maps.uniqueIndex(userList, new Function<User, ...
- css中两种居中方式text-align:center和margin:0 auto 的使用场景
关于使用text-align:center和margin:0 auto 两种居中方式的比较 前言:最近由于要学习后端,需要提前学习一部分前端知识,补了补css知识,发现狂神在讲这一部分讲的不是特别清楚 ...
- Android官方文档翻译 十八 4.2Pausing and Resuming an Activity
Pausing and Resuming an Activity 暂停和恢复一个activity This lesson teaches you to 这节课教给你 Pause Your Activi ...
- Python常用的数据结构
一.list 列表 1.列表的特点 有序的,元素可以重复,列表中的元素可以进行增上改查,可以存放不同的数据类型 2.创建列表 中括号创建并填充 --->[] 通过构造函数创建 list() 列表 ...
- 记录python2.7迁移到python3.6过程中的一些代码差异
python2.7 python 3.6 import urllib2 import urllib import urlparse import urllib import exceptions 废弃 ...
- 集合框架-LinkedList集合(有序不唯一)
1 package cn.itcast.p2.linkedlist.demo; 2 3 import java.util.Iterator; 4 import java.util.LinkedList ...
- IP:网络上的击鼓传花
链接,而不是直达 在之前<听说你很懂 DNS?>中我们分析过用户在浏览器里面输入 www.baidu.com 后,浏览器如何通过 DNS 解析拿到 IP 地址,然后请求该 IP 地址获取网 ...