zoukankan      html  css  js  c++  java
  • P3120 [USACO15FEB]牛跳房子(金)Cow Hopscotch (Gold)

    看到一堆人用n^4的算法水过去感觉糟蹋了一道好题,估计是之前误传了银组的数据,跟luogu管理员联系了一下,把金组的数据换了上来,现在n^4的算法过不去了。

    首先n^4的算法容易想到:暴力记下每个方块的方案,对于每个方块把左上和它颜色不同的方块的方案数暴力加上去就行。
    可是这样是通过不了(750)的数据规模的。需要优化。
    首先可以想到容斥。可以用总方案数减去不成立的方案数就是每个格子的答案。可是这并没有优化时间复杂度。
    于是考虑给每种颜色开一个线段树,记录不合法的方案,再用一颗线段树记录总方案数。这样时间复杂度就降低到了(O(RClogC)).
    空间不够?动态开点.
    注意,在洛谷的空间限制下,指针版的动态开点的内存会超出限制约40MB,也就是说我这份代码过不了。

    #include <cstdio>
    #include <cstring>
    #include <cassert>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const int MAXN = 750 + 10;
    const int MOD = 1e9 + 7;
    inline int read()
    {
        int x = 0; char ch = getchar();
        while(!isdigit(ch)) ch = getchar();
        while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
        return x;
    }
    
    int R, C, K;
    int a[MAXN][MAXN];
    
    namespace stree
    {
        #define mid ((l + r) >> 1)
    
        struct Node *null;
        struct Node
        {
            int val;
            Node *ls, *rs;
            Node(int val = 0) : val(val), ls(null), rs(null) {}
            inline void pushup(){
                this->val = (ls->val + rs->val) % MOD;
            }
        }pool[(MAXN * MAXN) << 2], *col[MAXN * MAXN], *tot, *pit = pool;
    
        inline Node *newnode(){
            static int cnt = 1;
            if(cnt < ((MAXN * MAXN) << 2)) return ++cnt, new (pit++) Node();
            return new Node();
        }
    
        void init(){
            null = newnode();
            null->ls = null->rs = null;
        }
    
        int query(Node *o, int l, int r, int a, int b){
            if(l > b || r < a || o == null) return 0;
            if(a <= l && r <= b) return o->val;
            return (query(o->ls, l, mid, a, b) + query(o->rs, mid + 1, r, a, b)) % MOD;
        }
    
        Node *modify(Node *o, int l, int r, int p, int v){
            if(o == null || !o) o = newnode();
            if(l == r) return (o->val += (v % MOD)) %= MOD, o;
            if(p <= mid) o->ls = modify(o->ls, l, mid, p, v);
            else o->rs = modify(o->rs, mid + 1, r, p, v);
            return o->pushup(), o;
        }
    }
    
    int f[MAXN][MAXN];
    int main()
    {
        // freopen("p3120.in", "r", stdin);
        using namespace stree;
        cin>>R>>C>>K; init(); tot = null;
        for(int i = 1; i <= R; i++)
            for(int j = 1; j <= C; j++){
                a[i][j] = read();
                col[a[i][j]] = null;
            }
    
        f[1][1] = 1; 
        for(int i = 1; i <= R; i++){
            if(i != 1){
                for(int j = 1; j <= C; j++)
                    f[i][j] = (query(tot, 1, C, 1, j - 1) - query(col[a[i][j]], 1, C, 1, j - 1) + MOD) % MOD;
            }
            for(int j = 1; j <= C; j++){
                tot = modify(tot, 1, C, j, f[i][j]);
                col[a[i][j]] = modify(col[a[i][j]], 1, C, j, f[i][j]);
            }
        }
        cout<<f[R][C]<<endl;
        return 0;
    }
    
    
  • 相关阅读:
    P1908 逆序对
    P1967 货车运输
    成也DP,败也DP(AFO?)
    Review Before THUWC2020
    THUWC2020游记
    loj6295. 无意识之外的捉迷藏
    loj6504. 「雅礼集训 2018 Day5」Convex
    某道XJ题
    loj2304. 「NOI2017」泳池
    loj6435. 「PKUSC2018」星际穿越
  • 原文地址:https://www.cnblogs.com/wsmrxc/p/9515218.html
Copyright © 2011-2022 走看看