Solution -「CSP-S 2020」函数调用
Description
大家应该都读过题。
Solution
赛后变摩托。
我们对每一个操作 \(3\) 连边建图,然后可以知道只是一个 \(\texttt{DAG}\)。
考虑操作 \(2\),我们只需要最后计算即可。
现在来看加法操作。我们设我们当前的操作为 \(a_{p}\leftarrow a_{p}+v\),后面一共有 \(k\) 个乘法操作,我们设为分别让序列整体乘上 \(b_{1,2,\cdots,k}\)。那么 \(a_{p}\leftarrow a_{p}+v\) 一共会对最后的 \(a_{p}\) 产生 \(v\times\prod_{i=1}^{k}b_{i}\) 的贡献。
于是我们可以把操作离线下来,倒着来处理。
那么最后来考虑操作 \(3\),我们来看操作 \(3\) 连出的图。
对于图上的每一个节点我们维护一个值,表示执行后会带来的系数。
那么和操作 \(1\) 类似的,我们对整张图进行拓扑,然后倒着处理,处理出每个节点的操作的加法计算了多少次,然后传播(指所有相邻节点)到加法操作的节点。
最后处理原序列即可。
(代码从 \(\texttt{Vim}\) 里面复制出来可能缩进有问题)
#include <queue>
#include <cstdio>
#define mod ( 998244353 )
using namespace std;
typedef long long LL;
const int MAXN = 1e6 + 5;
template<typename _T>
void read( _T &x ){
x = 0; char c = getchar(); _T f = 1;
while( c < '0' || c > '9' ){ if( c == '-' ) f = -1; c = getchar(); }
while( c >= '0' && c <= '9' ){ x = ( x << 3 ) + ( x << 1 ) + ( c & 15 ); c = getchar(); }
x *= f;
}
template<typename _T>
void write( _T x ){
if( x < 0 ){ putchar( '-' ); x = -x; }
if( x > 9 ) write( x / 10 );
putchar( x % 10 + '0' );
}
struct starS{
int to, nxt;
starS( int T = 0, int N = 0 ){ to = T; nxt = N; }
} as[MAXN * 2];
struct operateS{
int Tp, pos;
LL add, mul, sum;
operateS( int T = 0, int P = 0, LL A = 0, LL M = 0, LL S = 0 ){ Tp = T; pos = P; add = A; mul = M; sum = S; }
} opS[MAXN];
int N, M, Q;
int totE, totT;
int A[MAXN], topS[MAXN], degS[MAXN], firS[MAXN], queS[MAXN];
void pushEdge( int u, int v ){ as[++ totE] = starS( v, firS[u] ); firS[u] = totE; }
void TopSort( ){
queue<int> align;
for( int i = 1; i <= M; ++ i ){
if( ! degS[i] ) align.push( i );
}
while( ! align.empty( ) ){
int u = align.front( ); align.pop( );
topS[++ totT] = u;
for( int i = firS[u]; i; i = as[i].nxt ){
int v = as[i].to; degS[v] --;
if( ! degS[v] ) align.push( v );
}
}
}
int main( ){
read( N );
for( int i = 1; i <= N; ++ i ) read( A[i] );
read( M );
for( int i = 1; i <= M; ++ i ){
read( opS[i].Tp );
if( opS[i].Tp == 1 ){
read( opS[i].pos ); read( opS[i].add );
opS[i].mul = 1;
}
else if( opS[i].Tp == 2 ) {
read( opS[i].mul );
opS[i].add = opS[i].mul;
}
else{
read( opS[i].pos );
opS[i].mul = 1;
for( int j = 1, to; j <= opS[i].pos; ++ j ){ read( to ); pushEdge( i, to ); degS[to] ++; }
}
}
TopSort( );
for( int i = M; i; -- i ){
int u = topS[i];
for( int j = firS[u]; j; j = as[j].nxt ){
int v = as[j].to;
opS[u].mul = ( LL )opS[u].mul * opS[v].mul % mod;
}
}
read( Q ); int now = 1;
for( int i = 1; i <= Q; ++ i ) read( queS[i] );
for( int i = Q; i; -- i ){ opS[queS[i]].sum = ( ( LL )opS[queS[i]].sum + now ) % mod; now = ( LL )now * opS[queS[i]].mul % mod; }
for( int i = 1; i <= M; ++ i ){
int u = topS[i], now = 1;
for( int j = firS[u]; j; j = as[j].nxt ){
int v = as[j].to;
opS[v].sum = ( ( LL )opS[u].sum * now % mod + opS[v].sum ) % mod;
now = ( LL )now * opS[v].mul % mod;
}
}
for( int i = 1; i <= N; ++ i ) A[i] = ( LL )A[i] * now % mod;
for( int i = 1; i <= M; ++ i ){
if( opS[i].Tp == 1 ) A[opS[i].pos] = ( A[opS[i].pos] + ( LL )opS[i].add * opS[i].sum % mod ) % mod;
}
for( int i = 1; i <= N; ++ i ) write( A[i] ), putchar( ' ' );
return 0;
}
Solution -「CSP-S 2020」函数调用的更多相关文章
- Solution -「多校联训」排水系统
\(\mathcal{Description}\) Link. 在 NOIP 2020 A 的基础上,每条边赋权值 \(a_i\),随机恰好一条边断掉,第 \(i\) 条段的概率正比于 \(a ...
- Note/Solution -「洛谷 P5158」「模板」多项式快速插值
\(\mathcal{Description}\) Link. 给定 \(n\) 个点 \((x_i,y_i)\),求一个不超过 \(n-1\) 次的多项式 \(f(x)\),使得 \(f(x ...
- Solution -「洛谷 P4198」楼房重建
\(\mathcal{Description}\) Link. 给定点集 \(\{P_n\}\),\(P_i=(i,h_i)\),\(m\) 次修改,每次修改某个 \(h_i\),在每次修改后 ...
- Solution -「洛谷 P6577」「模板」二分图最大权完美匹配
\(\mathcal{Description}\) Link. 给定二分图 \(G=(V=X\cup Y,E)\),\(|X|=|Y|=n\),边 \((u,v)\in E\) 有权 \(w( ...
- Solution -「多校联训」I Love Random
\(\mathcal{Description}\) 给定排列 \(\{p_n\}\),可以在其上进行若干次操作,每次选取 \([l,r]\),把其中所有元素变为原区间最小值,求能够得到的所有不同序 ...
- Solution -「多校联训」签到题
\(\mathcal{Description}\) Link. 给定二分图 \(G=(X\cup Y,E)\),求对于边的一个染色 \(f:E\rightarrow\{1,2,\dots,c\ ...
- Solution -「多校联训」朝鲜时蔬
\(\mathcal{Description}\) Link. 破案了,朝鲜时蔬 = 超现实树!(指写得像那什么一样的题面. 对于整数集 \(X\),定义其 好子集 为满足 \(Y\sub ...
- Solution -「多校联训」消失的运算符
\(\mathcal{Description}\) Link. 给定长度为 \(n\) 的合法表达式序列 \(s\),其中数字仅有一位正数,运算符仅有 - 作为占位.求将其中恰好 \(k\) ...
- Solution -「多校联训」假人
\(\mathcal{Description}\) Link. 一种物品有 长度 和 权值 两种属性,现给定 \(n\) 组物品,第 \(i\) 组有 \(k_i\) 个,分别为 \((1,a ...
- Solution -「多校联训」古老的序列问题
\(\mathcal{Description}\) Link. 给定序列 \(\{a_n\}\),和 \(q\) 次形如 \([L,R]\) 的询问,每次回答 \[\sum_{[l,r]\su ...
随机推荐
- @Target元注解的使用
@Target注解标记另外的注解用于限制此注解可以应用哪种Java元素类型.先看Java SE 8中@Target是如何声明的: package java.lang.annotation; publi ...
- 【Java学习】 Spring的基础理解 IOC、AOP以及事务
一.简介 官网: https://spring.io/projects/spring-framework#overview 官方下载工具: https://repo.spring.io ...
- AB实验:科学归因与增长的利器
第一章 AB实验的基本原理和应用 AB实验的相关概念: 3个基本参数:实验参与单元.实验控制参数.实验指标 2个核心价值:验证因果关系.量化策略效果 2个关键特性:先验性.并行性 基本流程:分流 -& ...
- Mysql 5.7 的安装
Mysql的安装 1 windows两种安装方式,入门选手推荐第二种(win10演示) Mysql官网下载地址:https://dev.mysql.com/downloads/mysql/ 2 开始准 ...
- Web网页音视频通话之基于Sipjs
简述 本文是以FreeSwitch作为信令服务器,通过sipjs(基于webRtc) 进行媒体协商,网络协商后,进行P2P媒体传输. 参考知识: sip.js https://sipjs.com/ w ...
- 防火墙(iptables与firewalld)
防火墙 iptables 疏通和堵 进行路由选择前处理的数据包:prerouting 处理流入的数据包:input 处理流出的数据包:output 处理转发的数据包:forward 进行路由选择后处理 ...
- 分布式数据库oceanBase部署
分布式数据库oceanBase部署 相关链接 文档中心 视频中心 软件下载 OceanBase数据库基本操作 OceanBase简介 SQL执行计划 基本概念 为了更好地管理 OceanBase 数据 ...
- 学好Linux的必经之路
学好Linux的必经之路 学习动机的培养对于一个人学习习惯的形成有着重要的作用.当我们在学习某一个事物时,建立属于我们自己的学习方法,以此培养我们学习Linux系统的学习动机. 当前,Linux系统属 ...
- IIS 应用程序池 PowerShell 脚本更改高级属性的方法
## IIS WebAdmin Module Import-Module WebAdministration $AppPool = "mqttService(8011)" $Sit ...
- Django reset framework: 序列化
序列化与反序列化 将模型转换为json 称之为 序列化 将json转换为模型 称之为 反序列化 何时进行序列化与反序列化 序列化:当后端将数据库中信息取出返回给前端时,要进行序列化操作 反序列化:当需 ...