USACO 2015 Gold February P1: Cow Hopscotch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <bits/stdc++.h>
using namespace std;

const int N = 752, M=1e9+7;
int K, R, C, grid[N][N];
vector<map<int, int>> m;
vector<vector<int>> colorVector;

#ifdef NEW_SEGMENT_TREE
//https://usaco.guide/gold/PURS?lang=cpp
template<class T> struct Seg {
    const T ID = 0; T comb(T grid, T b){ return (grid+b+M)%M; }

    int n=0; vector<T> seg;
    void init(int _n) { n = _n; seg.assign(2*n,ID); }

    void pull(int p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
    void upd(int p, T val) {
        seg[p += n] += val; seg[p] %= M; for (p /= 2; p; p /= 2) pull(p); }

    T query(int l, int r) {
        T ra = ID, rb = ID;
        for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
            if (l&1) ra = comb(ra,seg[l++]);
            if (r&1) rb = comb(seg[--r],rb);
        }
        return comb(ra,rb);
    }
};
#else
struct BIT {
    vector<int> tree;
    void init(int _n) { tree.resize(_n); }

    void upd(int i, int x) {
        while (i < tree.size()) {
            tree[i] = (tree[i] + x);
            //if (tree[i] < 0) tree[i] += M;
            if (tree[i] > M) tree[i] -= M;
            tree[i] = (tree[i] + M) % M;
            i += (i & (-i));
        }
    }

    long long query(int i) {
        long long ans = 0;
        while (i > 0) {
            ans += tree[i];
            //if (ans < 0) ans += M;
            if (ans > M) ans -= M;
            ans = (ans + M) % M;
            i -= (i & (-i));
        }
        return ans;
    }
};
#endif

long long ans[N];

int main() {
    ifstream cin("hopscotch.in");
    ofstream cout("hopscotch.out");

    cin >> R >> C >> K;

    colorVector.resize(K + 1);
    m.resize(K + 1);

#ifdef NEW_SEGMENT_TREE
    Seg<long long> colors[K+3],allColors;
#else
    BIT colors[K + 3], allColors;
#endif
    allColors.init(C+1);

    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            int c;
            cin >> c;
            c--;
            grid[i][j] = c;
            colorVector[c].push_back(j);
        }
    }

    for(int i = 0; i < colorVector.size(); i++) {
        sort(colorVector[i].begin(), colorVector[i].end());
        for (auto j : colorVector[i]) {
            if (! m[i][j]) m[i][j] = m[i].size() + 1;
        }
    }

    for (int i = 0; i < m.size(); i++)
        colors[i].init(m[i].size()+2);

    allColors.upd(1,1);
    colors[grid[1][1]].upd(m[grid[1][1]][1],1);

    for(int i=2; i<=R; i++){
        for(int j=C; j>=2; j--){
            int x = grid[i][j];
#ifdef NEW_SEGMENT_TREE
            ans[j] = allColors.query(1,j-1) - colors[x].query(1, m[x][j]-1) + M;
#else
            ans[j] = allColors.query(j-1) - colors[x].query(m[x][j]-1) + M;
#endif
            ans[j] %= M;
            allColors.upd(j, ans[j]);
            colors[x].upd(m[x][j], ans[j]);
        }
    }
    cout << ans[C];
}