「LibreOJ NOI Round #2」单枪匹马

嘟嘟嘟


这题没卡带一个(log)的,那么就很水了。
然后我因为好长时间没写矩阵优化dp,就只敲了一个暴力分……看来复习还是很关键的啊。


这个函数显然是从后往前递推的,那么令第(i)位的分子分母为(x', y'),第(i + 1)的为(x, y),因为(f(i) = a_i + frac{1}{f(i + 1)} = frac{a_i * f(i + 1) + 1}{f(i + 1)}),所以(x' = a_i * x + y, y' = x)
这样我们把(x, y)看成(f[i][0],f[i][1]),就很容易构造矩阵了。


然后线段树维护矩阵即可。

#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 int maxn = 1e6 + 5;
const ll mod = 998244353;
inline ll read()
{
	ll ans = 0;
	char ch = getchar(), last = ' ';
	while(!isdigit(ch)) last = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(last == '-') ans = -ans;
	return ans;
}
inline 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, N, cnt, T;

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

#define LS t[now].ls
#define RS t[now].rs
struct Tree
{
	int ls, rs;
	ll a[2][2];
	In Tree operator + (const Tree& oth)const
	{
		Tree ret; Mem(ret.a, 0);
		for(int i = 0; i < 2; ++i)
			for(int j = 0; j < 2; ++j)
				for(int k = 0; k < 2; ++k)
					ret.a[i][j] = inc(ret.a[i][j], a[i][k] * oth.a[k][j] % mod);
		return ret;
	}
}t[maxn * 20];
int tcnt = 0, root = 0;
In void insert(int l, int r, int& now, int id, ll d)
{
	if(!now) now = ++tcnt;
	if(l == r)
	{
		t[now].a[0][0] = d, t[now].a[1][1] = 0;
		t[now].a[1][0] = t[now].a[0][1] = 1;
		return;
	}
	int mid = (l + r) >> 1;
	if(id <= mid) insert(l, mid, LS, id, d);
	else insert(mid + 1, r, RS, id, d);
	int tp1 = LS, tp2 = RS;
	t[now] = t[LS] + t[RS];
	LS = tp1, RS = tp2;
}
In Tree query(int l, int r, int now, int L, int R)
{
	if(l == L && r == R) return t[now];
	int mid = (l + r) >> 1;
	if(R <= mid) return query(l, mid, LS, L, R);
	else if(L > mid) return query(mid + 1, r, RS, L, R);
	else return query(l, mid, LS, L, mid) + query(mid + 1, r, RS, mid + 1, R);
}

int main()
{
//	MYFILE();
	n = read(), m = read(), T = read();
	N = n + m, cnt = n;
	for(int i = 1; i <= cnt; ++i) insert(1, N, root, i, read());
	ll ansX = 0, ansY = 0;
	for(int i = 1; i <= m; ++i)
	{
		int op = read();
		if(op == 1)
		{
			int x = read();
			if(T) x ^= ansX ^ ansY;
			insert(1, N, root, ++cnt, x);
		}
		else
		{
			int L = read(), R = read();
			if(T) L ^= ansX ^ ansY, R ^= ansX ^ ansY;
			Tree tp = query(1, N, root, L, R);
			write(ansX = tp.a[0][0]), space, write(ansY = tp.a[1][0]), enter;
		}
	}
	return 0;	
}
原文地址:https://www.cnblogs.com/mrclr/p/11160498.html