线段树/维护转移矩阵(中石油组队赛 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

相关文章

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 +

HDU4737线段树

题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i,j)<=m的总数。 const int Max_N = 100008 ;int sum[Max_N<<2] , x[Max_N] ;int n , m ;void push_up(int t){sum[t] = sum[t<<1] | sum[t<<1|1] ;}void upd

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

圆与线段的交点

poj 3819  给出一条线段的两个端点,再给出n个圆,求出这条线段被所有圆覆盖的部分占了整条线段的百分比。 圆与线段的交点 : 向量AB 的参数方程  P = A + t * (B - A)      0<=t<=1 ; 将点带入圆的方程即可。  注意: 有交点 0 <= t <= 1 ; 此题求覆盖的部分。 则 若求得 t  满足 ; double ask(d