线段树/维护转移矩阵(中石油组队赛 K: Addition Robot)

2023-11-22 09:40

本文主要是介绍线段树/维护转移矩阵(中石油组队赛 K: Addition Robot),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述
显然是个线段树,如果初始值为x,y。则最后答案显然是 x ′ = a 1 x + b 1 y , y ′ = a 2 x + b 2 y x'=a_1x+b_1y, \ \ y'=a_2x+b_2y x=a1x+b1y,  y=a2x+b2y我们就是用线段树维护这个系数。题目是关于状态累加的,显然可以用矩阵来维护转移状态。而矩阵乘是满足结合律的,显然可以用线段树维护(其实我们队一开始想的是分块,可是一拍脑门,既然都可以结合合并了,那还用个pi的分块)。这里我们如果将初始x,y写入矩阵:
a n s = ( x y 0 0 ) ans=\begin{pmatrix} x & y \\ 0 & 0 \end{pmatrix} \quad ans=(x0y0)
而对于字符’A‘有转移矩阵:
a = ( 1 0 1 1 ) a=\begin{pmatrix} 1 & 0 \\ 1 & 1 \end{pmatrix} \quad a=(1101)
同理对于’B‘:
b = ( 1 1 0 1 ) b=\begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix} \quad b=(1011)
显然,例如对于序列”AABBAB“,最终目标矩阵是:
a n s ′ = a n s ∗ a ∗ a ∗ b ∗ b ∗ a ∗ b ans'=ans*a*a*b*b*a*b ans=ansaabbab
其中 x ′ = a n s 0 , 0 ′ y ′ = a n s 0 , 1 ′ x'=ans'_{0,0}\ \ y'=ans'_{0,1} x=ans0,0  y=ans0,1

对于反转,我们对于每个区间,事先计算反转前后的矩阵。然后进行laz标记,决定用哪一个矩阵。即可。
ps.好像有大佬是直接维护的系数,和矩阵其实是一样的。(但是常数小。QAQ菜鸡流泪)
下面是ac代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include <map>
#include <queue>
#include <set>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <bitset>
#include <array>
#include <cctype>
#include <time.h>#pragma GCC optimize(2)void read_f() { freopen("1.in", "r", stdin); freopen("1.out", "w", stdout);}
void fast_cin() { std::ios::sync_with_stdio(false); std::cin.tie(); }
void run_time() { std::cout << "ESC in : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms" << std::endl; }#define ll long long
#define ull unsigned ll
#define _min(x, y) ((x)>(y)?(y):(x))
#define _max(x, y) ((x)>(y)?(x):(y))
#define max3(x, y, z) ( max( (x), max( (y), (z) ) ) )
#define min3(x, y, z) ( min( (x), min( (y), (z) ) ) )
#define pr(x, y) (make_pair((x), (y)))
#define pb(x) push_back(x);
using namespace std;const int N = 1e5+5;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;struct M
{ll a[2][2];friend M operator * (const M & a, const M b){M c;for (int i = 0; i < 2; i++){for (int j = 0; j < 2; j++){c.a[i][j] = 0;for (int k = 0; k < 2; k++)c.a[i][j] = (c.a[i][j] + a.a[i][k] * b.a[k][j] % mod) % mod;}}return c;}M(){};M(int flag) {if (flag == 0) a[0][0] = a[1][0] = a[1][1] = 1, a[0][1] = 0;else a[0][0] = a[0][1] = a[1][1] = 1, a[1][0] = 0;}M(int _a, int _b){a[0][0] = _a, a[0][1] = _b;a[1][0] = a[1][1] = 0;}
}a, b, ans;
struct Node
{int l, r;int laz;M ans1, ans2;
} tr[N<<2];
char su[N];
inline void pushup(int p)
{tr[p].ans1 = tr[p<<1].ans1 * tr[p<<1|1].ans1;tr[p].ans2 = tr[p<<1].ans2 * tr[p<<1|1].ans2;
}
inline void spread(int p)
{if (tr[p].laz == 1){tr[p].laz = 0;tr[p<<1].laz ^= 1; tr[p<<1|1].laz ^= 1;swap(tr[p<<1].ans1, tr[p<<1].ans2);swap(tr[p<<1|1].ans1, tr[p<<1|1].ans2);}
}
void build(int p, int l, int r)
{tr[p].l = l; tr[p].r = r;tr[p].laz = 0;if (l == r) {if (su[l] == 'A') tr[p].ans1 = a, tr[p].ans2 = b;else tr[p].ans2 = a, tr[p].ans1= b;return;}int mid = (l + r) >> 1;build(p<<1, l, mid);build(p<<1|1, mid+1, r);pushup(p);
}
void change(int p, int l, int r)
{//cout << p << " " << tr[p].l << " " << tr[p].r <<endl;if (l <= tr[p].l && tr[p].r <= r) {tr[p].laz ^= 1;swap(tr[p].ans1, tr[p].ans2);return;}spread(p);int mid = (tr[p].l + tr[p].r) >> 1;if (l <= mid) change(p<<1, l, r);if (r > mid) change(p<<1|1, l, r);pushup(p);
}
void ask(int p, int l, int r)
{if (l <= tr[p].l && tr[p].r <= r){ans = ans * tr[p].ans1;return;}spread(p);int mid = (tr[p].l + tr[p].r) >> 1;if(l <= mid) ask(p<<1, l, r);if (r > mid) ask(p<<1|1, l, r);
}
void printm(M a)
{printf("---------------------\n");for (int i = 0; i < 2; i++){for (int j = 0; j <  2; j++)cout << a.a[i][j] << " ";cout << endl;}
}
int main()
{a = M(0); b = M(1);int n, m; scanf("%d%d", &n, &m);scanf("%s", su+1);build(1, 1, n);while(m--){int op, l, r; scanf("%d%d%d", &op, &l, &r);if (op == 1){change(1, l, r);}else{int x, y; scanf("%d%d", &x, &y);ans = M(x, y);//printm(ans);ask(1, l, r);//printm(ans);printf("%lld %lld\n", ans.a[0][0], ans.a[0][1]);}}return 0;
}

这篇关于线段树/维护转移矩阵(中石油组队赛 K: Addition Robot)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/409228

相关文章

Window Server创建2台服务器的故障转移群集的图文教程

《WindowServer创建2台服务器的故障转移群集的图文教程》本文主要介绍了在WindowsServer系统上创建一个包含两台成员服务器的故障转移群集,文中通过图文示例介绍的非常详细,对大家的... 目录一、 准备条件二、在ServerB安装故障转移群集三、在ServerC安装故障转移群集,操作与Ser

windos server2022的配置故障转移服务的图文教程

《windosserver2022的配置故障转移服务的图文教程》本文主要介绍了windosserver2022的配置故障转移服务的图文教程,以确保服务和应用程序的连续性和可用性,文中通过图文介绍的非... 目录准备环境:步骤故障转移群集是 Windows Server 2022 中提供的一种功能,用于在多个

Nacos客户端本地缓存和故障转移方式

《Nacos客户端本地缓存和故障转移方式》Nacos客户端在从Server获得服务时,若出现故障,会通过ServiceInfoHolder和FailoverReactor进行故障转移,ServiceI... 目录1. ServiceInfoHolder本地缓存目录2. FailoverReactorinit

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

poj 1127 线段相交的判定

题意: 有n根木棍,每根的端点坐标分别是 px, py, qx, qy。 判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。 并且通过相连的木棍相连的木棍也是相连的。 解析: 线段相交的判定。 首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定; 然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。 最后

hdu 4565 推倒公式+矩阵快速幂

题意 求下式的值: Sn=⌈ (a+b√)n⌉%m S_n = \lceil\ (a + \sqrt{b}) ^ n \rceil\% m 其中: 0<a,m<215 0< a, m < 2^{15} 0<b,n<231 0 < b, n < 2^{31} (a−1)2<b<a2 (a-1)^2< b < a^2 解析 令: An=(a+b√)n A_n = (a +