In the w array, the value is 1. The array is initially 0. How many sort methods are available to make the array Lucky. The total number of solutions should be provided after each update.
Dynamic Planning can be used to obtain the result after the merge of adjacent two intervals.
F (I, j) indicates the total number of solutions starting with I and ending with j in the current interval. In combination with the update operation, we can use the line segment tree for maintenance. Each node in the tree has an f array to record the number of solutions for the current interval. For each updated query, you only need to sum the root node.
For status transfer, see code.
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>using namespace std;#define N 80000#define Mod 777777777#define For(i, s, t) for(int i=s; i<=t; i++)typedef long long ll;ll f[N<<2][4][4];int n, m, w[4][4];void Up(int rt) { For(i, 1, 3) For(j, 1, 3) { f[rt][i][j] = 0; For(p, 1, 3) For(q, 1, 3) f[rt][i][j] += w[p][q] ? f[rt<<1][i][p]*f[rt<<1|1][q][j] : 0; f[rt][i][j] %= Mod; }}void build(int L, int R, int rt) { if (L == R) { For(i, 1, 3) For(j, 1, 3) f[rt][i][j] = (i==j)?1:0; return ; } int Mid = (L + R) >> 1; build(L, Mid, rt<<1); build(Mid+1, R, rt<<1|1); Up(rt);}void update(int v, int t, int L, int R, int rt) { if (L == R) { if (t == 0) { For(i, 1, 3) For(j, 1, 3) f[rt][i][j] = (i==j)?1:0; } else { For(i, 1, 3) f[rt][i][i] = (i==t)?1:0; } return ; } int Mid = (L + R) >> 1; if (v <= Mid) update(v, t, L, Mid, rt<<1); else update(v, t, Mid+1, R, rt<<1|1); Up(rt);}int main() { scanf("%d%d", &n, &m); For(i, 1, 3) For(j, 1, 3) scanf("%d", &w[i][j]); build(1, n, 1); int v, t; ll ans = 0; while(m--) { scanf("%d%d", &v, &t); update(v, t, 1, n, 1); ans = 0; For(i, 1, 3) For(j, 1, 3) ans += f[1][i][j]; cout << ans % Mod << endl; } return 0;}