Submission #1335358

#TimeUsernameProblemLanguageResultExecution timeMemory
1335358doantaolaaidiBob (COCI14_bob)C++20
120 / 120
987 ms25072 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define AC "test"
#define foru(i, l, r) for (int i = (l); i <= (r); i++)
#define ford(i, l, r) for (int i = (l); i >= (r); i--)
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vii;
typedef vector<ll> vll;

const ll inf = 1e9 + 7;
const ll linf = 1e18 + 7;
const int mod = 1e9 + 7;
const int maxn = 1e3 + 7;
const int base = 31;

void fastIO(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
}

ll mul(ll a, ll b){
    a%=mod;
    ll res = 0;
    while (b){
        if (b%2) res = (res + a)%mod;
        a = (a + a)%mod;
        b/=2;
    }
    return res;
}

ll Pow(ll a, ll b){
    ll ans = 1;
    while (b){
        if (b % 2) ans = mul(ans, a);
        a = mul(a, a);
        b/=2;
    }
    return ans;
}

ll res, n, m, a[maxn][maxn], h[maxn][maxn], visited[maxn][maxn], st1[4*maxn], st2[4*maxn], lazy1[4*maxn], lazy2[4*maxn], val3, cnt;
deque<ll> q1, q2;

void dfs(int x, int y){
    h[x][y] = 1;
    visited[x][y] = 1;
    if (a[x + 1][y] == a[x][y]){
        dfs(x + 1, y);
        h[x][y] = h[x + 1][y] + 1;
    }
}

void down1(int id1, int left, int right){
    if (lazy1[id1] == -1) return;
    q1.push_front(id1 * 2);
    q1.push_front(id1 * 2 + 1);
    long long t = lazy1[id1];
    int mid = (left + right)/2;
    lazy1[id1*2] = t;
    st1[id1*2] = t*(mid-left+1);
    lazy1[id1*2+1] = t;
    st1[id1*2+1] = t*(right-mid);
    lazy1[id1] = -1;
}

void update1(int id, int l, int r, int u , int v, long long val){
    if (v < l || r < u) return;
    q1.push_front(id);
    if (u <= l && r <= v){
        st1[id] = val*(r-l+1);
        lazy1[id] = val;
        return;
    }
    int m = (l+r)/2;
    down1(id, l, r);
    update1(id*2, l, m, u, v, val);
    update1(id*2+1, m+1, r, u, v, val);
    st1[id] = st1[id*2] + st1[id*2+1];
}

long long get1(int id, int l , int r, int u, int v){
    if (v < l || r < u) return 0;
    q1.push_front(id);
    if (u <= l && r <= v) return st1[id];
    down1(id, l, r);
    int m = (l+r) >> 1;
    return get1(id*2, l, m, u, v) + get1(id*2+1, m+1, r, u, v);
}

void down2(int id1, int left, int right){
    if (lazy2[id1] == -1) return;
    q2.push_front(id1 * 2);
    q2.push_front(id1 * 2 + 1);
    long long t = lazy2[id1];
    int mid = (left + right)/2;
    lazy2[id1*2] = t;
    st2[id1*2] = t*(mid-left+1);
    lazy2[id1*2+1] = t;
    st2[id1*2+1] = t*(right-mid);
    lazy2[id1] = -1;
}

void update2(int id, int l, int r, int u , int v, long long val){
    if (v < l || r < u) return ;
    q2.push_front(id);
    if (u <= l && r <= v){
        st2[id] = val*(r-l+1);
        lazy2[id] = val;
        return;
    }
    int m = (l+r)/2;
    down2(id, l, r);
    update2(id*2, l, m, u, v, val);
    update2(id*2+1, m+1, r, u, v, val);
    st2[id] = st2[id*2] + st2[id*2+1];
}

long long get2(int id, int l , int r, int u, int v){
    if (v < l || r < u) return 0;
    q2.push_front(id);
    if (u <= l && r <= v) return st2[id];
    down2(id, l, r);
    int m = (l+r) >> 1;
    return get2(id*2, l, m, u, v) + get2(id*2+1, m+1, r, u, v);
}

void solve(){
    cin >> n >> m;
    foru(i, 1, n){
        foru(j, 1 ,m){
            cin >> a[i][j];
        }
    }
    foru(i, 1, n){
        foru(j, 1, m){
            if (!visited[i][j]) dfs(i, j);
        }
    }
    foru(i, 1, n){
        foru(j, 1, m){
            if (a[i][j] != a[i][j - 1]){
                while (!q1.empty()){
                    st1[q1.front()] = 0;
                    lazy1[q1.front()] = -1;
                    q1.pop_front();
                }
                while (!q2.empty()){
                    st2[q2.front()] = 0;
                    lazy2[q2.front()] = -1;
                    q2.pop_front();
                }
            }
            val3 = get2(1, 1, 1000, h[i][j], h[i][j]);
            cnt = get2(1, 1, 1000, h[i][j] + 1, 1000);
            update2(1, 1, 1000, h[i][j] + 1, 1000, 0);
            update2(1, 1, 1000, h[i][j], h[i][j], val3 + cnt + 1);
            update1(1, 1, 1000, h[i][j] + 1, 1000, 0);
            update1(1, 1, 1000, h[i][j], h[i][j], (val3 + cnt + 1) * h[i][j]);
            res += get1(1, 1, 1000, 1, h[i][j]);
        }
    }
    cout << res;
}

int main(){
    fastIO();
    if (fopen(AC".inp", "r")){
        freopen(AC".inp", "r", stdin);
        freopen(AC".out", "w", stdout);
    }
    solve();
}

Compilation message (stderr)

bob.cpp: In function 'int main()':
bob.cpp:175:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  175 |         freopen(AC".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
bob.cpp:176:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  176 |         freopen(AC".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...