Submission #859774

# Submission time Handle Problem Language Result Execution time Memory
859774 2023-10-10T15:41:59 Z Tri2k6 Bob (COCI14_bob) C++17
120 / 120
108 ms 20304 KB
#include <bits/stdc++.h>
using namespace std;

template<class integer>
inline integer to_int(const string& s, size_t* idx = 0, int base = 10) {
    size_t n = s.size(), i = idx ? *idx : 0; bool sign = false; integer x = 0;
    if (s[i] == '-')
        ++i, sign = true;
    function<int(char)> char_to_digit = [&](char c) {
        static const int d[] = {'a'-10,'0'}; 
        return tolower(c)-d[isdigit(c)]; };
    while (i < n)
        x *= base, x += char_to_digit(s[i++]);
    if (idx)
        *idx = i; 
    return sign ? -x : x; }
 
template<class integer>
inline string to_string(integer x, int base = 10) {
    bool sign = false; string s;
    if (x < 0)
        x = -x, sign = true;
    function<char(int)> digit_to_char = [](int d) {
        static const char c[] = {'a'-10,'0'};
        return c[d < 10]+d; };
    do 
        s += digit_to_char(x%base), x /= base;
    while (x > 0); 
    if (sign)
        s += '-';
    reverse(s.begin(),s.end());
    return s; }
 
template<class integer>
inline istream& read(istream& is, integer& x) {
    string s; is >> s, x = to_int<integer>(s); return is; }
 
template<class integer>
inline ostream& write(ostream& os, integer x) { return os << to_string(x); }
 
using  lll =   signed __int128; 
using ulll = unsigned __int128;
 
inline istream& operator>>(istream& is,  lll &x) { return  read(is,x); }
inline istream& operator>>(istream& is, ulll &x) { return  read(is,x); }
inline ostream& operator<<(ostream& os,  lll  x) { return write(os,x); }
inline ostream& operator<<(ostream& os, ulll  x) { return write(os,x); }

#define input cin
#define output cout
#define ll long long 
#define ull unsigned long long
#define pii pair<int,int>
#define endl '\n'
#define all(v) v.begin(),v.end()
#define sorta(v) sort(v.begin(),v.end())
#define mem(a,b) memset(a,b,sizeof(a));
#define GetBit(u) (u & -u)
#define bit(u,i) ((u >> i) & 1)
#define mask(i) (1ll << i)
#define vi vector<int>
#define int128 __int128
#define fi first
#define se second
#define heap_max(a) priority_queue<a>
#define heap_min(a) priority_queue<a, vector<a>, greater <a>>
#define pb push_back
#define eb emplace_back
#define TASK "TASK"

const int cs = 1e6 + 7;
const int mod = 1e9 + 7;
const int INF = mod;
const int maxN = 2e3 + 7;
const int block_size = 350;
const ll oo = 1e18 + 7;

template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        X eps = 1e-9;
        if (x > y + eps) {
            x = y;
            return true;
        } else return false;
    }

template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        X eps = 1e-9;
        if (x + eps < y) {
            x = y;
            return true;
        } else return false;
    }

template<class T>
    T Abs(const T &x) {
        return (x < 0 ? -x : x);
    }

template<class X, class Y>
    void Add(X &x , const Y &y) {
        x += y;
        if (x > mod) x -= mod;
    }

template<class X, class Y>
    void Sub(X &x, const Y &y) {
        x -= y;
        if (x < 0) x += mod;
    }

// 

int n,m;
int a[maxN][maxN];

void nhap() {
    input >> n >> m;
    for (int i = 1;i <= n;i++) {
        for (int j = 1;j <= m;j++) {
            input >> a[i][j];
        }
    }
}

int h[cs];

ll miniSolve(vi a) {
    stack<int> st;
    int len = a.size() - 1;
    vi L(a.size() + 1, 0);
    vi F(a.size() + 1, 0);
    for (int i = 1;i <= len;i++) {
        while (st.size() && a[i] < a[st.top()]) st.pop();
        L[i] = st.empty() ? 1 : st.top() + 1;
        st.push(i);
    }

    ll ans = 0;

    for (int i = 1;i <= len;i++) {
        int k = L[i] - 1;
        F[i] = 1ll * a[i] *(i - k) + F[k];
        ans += F[i];
    }
    return ans;
}

void Solve() {
    nhap();
    ll ans = 0;
    for (int i = 1;i <= n;i++) {
        if (i == 1) for (int j = 1;j <= m;j++) h[j] = 1;
        else {
            for (int j = 1;j <= m;j++) {
                if (a[i][j] != a[i - 1][j]) h[j] = 1;
                else h[j]++;
            }
        }
        
        for (int j = 1;j <= m;) {
            vi v;
            v.push_back(0);
            int val = a[i][j];
            while (a[i][j] == val) {
                v.push_back(h[j]);
                j++;
            }
            
            ans += miniSolve(v);
        }

    }
    output << ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    int T = 1;
    // input >> T;
    while (T--) {
        Solve();
    }
    return 0;
}

Compilation message

bob.cpp: In function 'int main()':
bob.cpp:183:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  183 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bob.cpp:184:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  184 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 7004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 7516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 7876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 8056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 17236 KB Output is correct
2 Correct 51 ms 12720 KB Output is correct
3 Correct 60 ms 12612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 20292 KB Output is correct
2 Correct 53 ms 12624 KB Output is correct
3 Correct 46 ms 12624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 20304 KB Output is correct
2 Correct 46 ms 12652 KB Output is correct
3 Correct 47 ms 12832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 20300 KB Output is correct
2 Correct 46 ms 12652 KB Output is correct
3 Correct 46 ms 12608 KB Output is correct