其他分享
首页 > 其他分享> > [洛谷P4340][SHOI2016]随机序列

[洛谷P4340][SHOI2016]随机序列

作者:互联网

题目大意:有$n(n\leqslant10^5)$个数,每两个数之间可以加入$+-\times$三种符号,$q(q\leqslant10^5)$次询问,每次询问修改一个数后,所有表达式可能的值的和

题解:发现任意一个表达式,把所有的$+-$取反,后面的值为相反数,相互抵消,而第一项的连乘,符号一定是正的。所以只有最开始连乘的一段是有用的,线段树区间修改即可

卡点:

 

C++ Code:

#include <cstdio>
#include <iostream>
#define maxn 100010
const int mod = 1e9 + 7;

#define mul(x, y) static_cast<long long> (x) * (y) % mod
inline void reduce(int &x) { x += x >> 31 & mod; }
inline int pw(int base, int p) {
	static int res;
	for (res = 1; p; p >>= 1, base = mul(base, base)) if (p & 1) res = mul(res, base);
	return res;
}
inline int inv(int x) { return pw(x, mod - 2); }

int n, q;
int w[maxn], s[maxn];
namespace SgT {
	int V[maxn << 2], tg[maxn << 2];

	void build(int rt, int l, int r) {
		tg[rt] = 1;
		if (l == r) {
			V[rt] = w[l];
			return ;
		}
		const int mid = l + r >> 1;
		build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r);
		reduce(V[rt] = V[rt << 1] + V[rt << 1 | 1] - mod);
	}
	inline void pushdown(int rt) {
		int &__tg = tg[rt];
		V[rt << 1] = mul(V[rt << 1], __tg);
		tg[rt << 1] = mul(tg[rt << 1], __tg);
		V[rt << 1 | 1] = mul(V[rt << 1 | 1], __tg);
		tg[rt << 1 | 1] = mul(tg[rt << 1 | 1], __tg);
		__tg = 1;
	}

	int L, R, v;
	void __modify(int rt, int l, int r) {
		if (L <= l && R >= r) {
			V[rt] = mul(V[rt], v);
			tg[rt] = mul(tg[rt], v);
			return ;
		}
		if (tg[rt] != 1) pushdown(rt);
		const int mid = l + r >> 1;
		if (L <= mid) __modify(rt << 1, l, mid);
		if (R > mid) __modify(rt << 1 | 1, mid + 1, r);
		reduce(V[rt] = V[rt << 1] + V[rt << 1 | 1] - mod);
	}
	void modify(int __p, int __v) {
		L = __p, R = n, v = __v;
		__modify(1, 1, n);
	}
}

int main() {
	std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
	std::cin >> n >> q;
	w[0] = 1;
	for (int i = 1; i <= n; ++i) {
		std::cin >> s[i];
		w[i] = mul(s[i], w[i - 1]);
	}
	for (int i = 1; i < n; ++i) reduce(w[i] = mul(w[i], pw(3, n - i - 1)) * 2 - mod);
	SgT::build(1, 1, n);
	while (q --> 0) {
		static int x, y;
		std::cin >> x >> y;
		SgT::modify(x, mul(inv(s[x]), y));
		s[x] = y;
		std::cout << SgT::V[1] << '\n';
	}
	return 0;
}

  

标签:rt,洛谷,int,res,P4340,base,mul,SHOI2016,mod
来源: https://www.cnblogs.com/Memory-of-winter/p/10397263.html