CF 256E Lucky Arrarys line segment tree + DP

Source: Internet
Author: User

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;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.