CSP-S2020 函数调用(洛谷民间数据)

传送门


其实除了第一题,剩下几道出的都挺不错的,至于赛制的改变我也不知道会带来了什么影响,毕竟我已经不是OI选手了(似乎又搞起了ACM)。


废话不多说。这道题思维难度真的挺大的,想了半天也没想出正解,题解也是看了好长一段时间才懂,感觉自己原来没这么菜啊……
所以这里先隆重推出一篇题解:AK DREAM的[CSP2020-函数调用]


我们把函数调用关系的图建出来,容易看出这是一个DAG,而且只有出度为1的点是功能一或功能二。
首先考虑如果只有功能二,那么我们只要记录每一个函数(下文就叫做节点好了)被调用的次数,最后在DAG上DP就行了,复杂度是线性的。


现在添加了功能一,就得换一种思路:每一个加法操作执行的次数都相当于他后面的所有乘法操作之积。
所以我们倒着来处理执行的函数。
记mul[u]表示调用节点(u)后执行的所有乘法操作之积。这个开始在DAG上预处理一下就行:功能一和功能三的mul初始化为1,功能二的mul刚开始就是他要乘的值。然后倒着在DAG上跑一遍就能算出所有mul[i]了。


处理完mul后,倒着执行函数的时候,首先要有一个累乘值(M),表示从最后有多少个乘法操作,那么如果调用了函数(u)(u)包含的所有加法操作都至少要执行(M)次,因此我们给(u)的标记加(M),然后(M)要再乘以mul[u]。


为什么说是至少执行(M)次呢?因为(u)内部可能是加法乘法操作混合的。因此下传标记的时候也要按(u)自己调用函数的顺序下传:对于(u)调用的一个节点(v),他要乘的值除了(M)还有(v)之前(u)调用的节点的mul,乘上然后下传给(v)即可。


这样最后对于每一个数,除了乘上(M),再加上所有加法操作乘以他们的标记就好啦。


最后下传标记的时候应该是每一个节点倒着访问他的每一条出边,但是因为链前存图本身就是倒着的,因此刚好符合了这一点。
写完题解后感觉不是很难,话说这就是思维题的魅力嘛,难的不会,会的不难。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<queue>
#include<assert.h>
#include<ctime>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const ll mod = 998244353;
const int maxn = 1e5 + 5;
In ll read()
{
	ll ans = 0;
	char ch = getchar(), las = ' ';
	while(!isdigit(ch)) las = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(las == '-') ans = -ans;
	return ans;
}
In void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
}

int n, m, Q, a[maxn], q[maxn];
ll Mul = 1;
struct Node
{
	int p, v;
	ll mul, sum;
}t[maxn];
struct Edge
{
	int nxt, to;
}e[maxn * 10];
int head[maxn], du[maxn], ecnt = -1;
In void addEdge(int x, int y)
{
	e[++ecnt] = (Edge){head[x], y};
	head[x] = ecnt;
	++du[y];
}

In ll ADD(ll a, ll b) {return a + b < mod ? a + b : a + b - mod;}

int d[maxn], cnt = 0;
In void topo()
{
	queue<int> q;
	for(int i = 1; i <= m; ++i) if(!du[i]) q.push(i);
	while(!q.empty())
	{
		int now = q.front(); q.pop();
		d[++cnt] = now;
		forE(i, now, v) if(!--du[v]) q.push(v);
	}
}

In void calc_mul()
{
	for(int i = m; i; --i)
	{
		int now = d[i];
		forE(j, now, v) t[now].mul = t[now].mul * t[v].mul % mod;
	}
}

In void calc_sum()
{
	for(int i = 1; i <= m; ++i)
	{
		int now = d[i]; ll tp = 1;
		forE(j, now, v)
		{
			t[v].sum = ADD(t[v].sum, t[now].sum * tp % mod);
			tp = tp * t[v].mul % mod;
		}
	}
}

int main()
{
//	MYFILE();
	Mem(head, -1), ecnt = -1;
	n = read();
	for(int i = 1; i <= n; ++i) a[i] = read();
	m = read();
	for(int i = 1; i <= m; ++i)
	{
		int op = read();
		if(op == 1)
		{
			t[i].p = read(), t[i].v = read();
			t[i].mul = 1;	
		}
		else if(op == 2) t[i].mul = read();
		else
		{
			t[i].mul = 1;
			int K = read();
			for(int j = 1; j <= K; ++j) addEdge(i, read());
		}
	}
	Q = read();
	for(int i = 1; i <= Q; ++i) q[i] = read();
	topo();
	calc_mul();
	for(int i = Q; i; --i)
	{
		int now = q[i];
		t[now].sum = ADD(t[now].sum, Mul);
		Mul = Mul * t[now].mul % mod;
	}
	calc_sum();
	for(int i = 1; i <= n; ++i) a[i] = 1LL * a[i] * Mul % mod;
	for(int i = 1; i <= m; ++i) if(t[i].p)
		a[t[i].p] = ADD(a[t[i].p], t[i].v * t[i].sum % mod);
	for(int i = 1; i <= n; ++i) write(a[i]), space; enter;
	return 0;
}
原文地址:https://www.cnblogs.com/mrclr/p/13970956.html