Submission #766314

# Submission time Handle Problem Language Result Execution time Memory
766314 2023-06-25T13:35:11 Z abysmal Sandcastle 2 (JOI22_ho_t5) C++14
80 / 100
5000 ms 34384 KB
#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<iomanip>
#include<algorithm>
#include<utility>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<deque>
#include<math.h>
#include<assert.h>

using namespace std;

const int64_t INF = (int64_t) 2 * 1e9 + 100;
const int64_t mINF = (int64_t) 1e9 + 5;
const int64_t MOD = 1e9 + 7;
const int nbit = 31;
const int nchar = 26;
const int D = 4;
int dr[D] = {-1, 0, 1, 0};
int dc[D] = {0, 1, 0, -1};

struct Cell
{
    int r;
    int c;

    Cell(int r_, int c_) : r(r_), c(c_) {}
};

struct Solution
{
    int n;
    int m;
    int nmask;
    vector<int> pow;
    vector<vector<int> > g;
    vector<vector<vector<int> > > a;
    vector<vector<vector<int> > > prefix;
    Solution() {}

    void solve()
    {
        cin >> n >> m;

        if(n > m)
        {
            g.resize(m, vector<int>(n, 0));
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < m; j++)
                {
                    cin >> g[j][i];
                }
            }

            swap(n, m);
        }
        else
        {
            g.resize(n, vector<int>(m, 0));
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < m; j++)
                {
                    cin >> g[i][j];
                }
            }
        }

        if(n == 1) Sub1();
        else Sub4();
    }

    void Sub1()
    {
        int t = 0;
        int cnt = 1;
        int64_t ans = 0;
        for(int i = 1; i < m; i++)
        {
            int x = 1;
            if(g[0][i] < g[0][i - 1]) x = -1;
            if(t != x && t != 0)
            {
                ans += 1LL * cnt * (cnt - 1) / 2;
                cnt = 1;
            }

            cnt++;
            t = x;
        }

        ans += 1LL * cnt * (cnt - 1) / 2;
        cout << ans + m << "\n";
    }

    void Sub4()
    {
        int k = 3;
        nmask = 81;
        pow.resize(k + 1, 1);
        for(int i = 1; i <= k; i++)
        {
            pow[i] = pow[i - 1] * 3;
        }

        a.resize(nmask, vector<vector<int> >(n + 1, vector<int>(m + 1, 0)));
        for(int mask = 0; mask < nmask; mask++)
        {
            vector<int> c;
            int tmp = mask;
            for(int i = k; i >= 0; i--)
            {
                int d = tmp / pow[i];
                tmp %= pow[i];

                c.push_back(d);
            }
            reverse(c.begin(), c.end());
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < m; j++)
                {
                    if(!check(c, i, j)) continue;

                    int cnt = 0;
                    for(int t = 0; t < D; t++)
                    {
                        if(c[t] == 0) continue;

                        Cell v(i + dr[t], j + dc[t]);
                        if(g[v.r][v.c] < g[i][j]) continue;

                        int max_ = g[i][j];
                        if(c[t] == 2)
                        {
                            int val = g[v.r + dr[t]][v.c + dc[t]];
                            if(val < g[v.r][v.c]) max_ = max(max_, val);
                        }

                        int nx = t + 1; if(nx == D) nx -= D;
                        int pr = t - 1; if(pr < 0) pr += D;
                        if(c[nx] != 0)
                        {
                            int val = g[v.r + dr[nx]][v.c + dc[nx]];
                            if(val < g[v.r][v.c]) max_ = max(max_, val);
                        }
                        if(c[pr] != 0)
                        {
                            int val = g[v.r + dr[pr]][v.c + dc[pr]];
                            if(val < g[v.r][v.c]) max_ = max(max_, val);
                        }

                        if(max_ == g[i][j]) cnt++;
                    }

                    if(cnt == 0) a[mask][i][j]++;
                }
            }
        }

        prefix.resize(nmask, vector<vector<int> >(n + 1, vector<int>(m + 1, 0)));
        for(int mask = 0; mask < nmask; mask++)
        {
            for(int i = 1; i <= n; i++)
            {
                for(int j = 1; j <= m; j++)
                {
                    prefix[mask][i][j] -= prefix[mask][i - 1][j - 1];
                    prefix[mask][i][j] += a[mask][i - 1][j - 1] + prefix[mask][i - 1][j] + prefix[mask][i][j - 1];
                }
            }
        }

        int64_t ans = 0;
        vector<int> cnt(n * m + 5, 0);
        for(int i = 0; i < n; i++)
        {
            for(int r = i; r < n; r++)
            {
                for(int j = 0; j < m; j++)
                {
                    for(int c = j; c <= min(j + k, m - 1); c++)
                    {
                        int sum = Count(i, j, r, c);

                        if(sum == 1) ans++;
                    }
                }

                int mid = 0;
                vector<int> tmp;
                for(int j = 3; j < m; j++)
                {
                    int right = 0;
                    int left = 0;

                    Calc(i, r, j, left, mid, right);

                    ans += cnt[right + mid];
                    if(-left + mid + 1 >= 0)
                    {
                        tmp.push_back(-left + mid + 1);
                        cnt[-left + mid + 1]++;
                    }
                }

                for(int j = 0; j < (int) tmp.size(); j++)
                {
                    cnt[tmp[j]]--;
                }
            }
        }

        cout << ans << "\n";
    }

    void Calc(int i, int r, int j, int& left, int& mid, int& right)
    {
        int lx = i;
        int rx = r;
        int t = j - 2;
        while(lx <= rx)
        {
            int t1 = getMask(i, j - 3, r, j, lx, j);
            int t2 = getMask(i, j - 3, r, j, lx, j - 1);
            int t3 = getMask(i, j - 3, r, j, rx, j);
            int t4 = getMask(i, j - 3, r, j, rx, j - 1);
            if(t1 == t3) right += prefix[t1][rx + 1][j + 1] - prefix[t1][rx + 1][j] - prefix[t1][lx][j + 1] + prefix[t1][lx][j];
            else right += a[t1][lx][j] + a[t3][rx][j];
            if(t2 == t4) right += prefix[t2][rx + 1][j] - prefix[t2][rx + 1][j - 1] - prefix[t2][lx][j] + prefix[t2][lx][j - 1];
            else right += a[t2][lx][j - 1] + a[t4][rx][j - 1];

            int t5 = getMask(i, t - 2, r, t + 2, lx, t);
            int t6 = getMask(i, t - 2, r, t + 2, rx, t);
            if(t5 == t6) mid += prefix[t5][rx + 1][t + 1] - prefix[t5][rx + 1][t] - prefix[t5][lx][t + 1] + prefix[t5][lx][t];
            else mid += a[t5][lx][t] + a[t6][rx][t];

            int t7 = getMask(i, t - 1, r, t + 2, lx, t);
            int t8 = getMask(i, t - 1, r, t + 2, lx, t - 1);
            int t9 = getMask(i, t - 1, r, t + 2, rx, t);
            int t10 = getMask(i, t - 1, r, t + 2, rx, t - 1);

            if(t7 == t9) left += prefix[t7][rx + 1][t + 1] - prefix[t7][rx + 1][t] - prefix[t7][lx][t + 1] + prefix[t7][lx][t];
            else left += a[t7][lx][t] + a[t9][rx][t];

            if(t8 == t10)
            {
                left += prefix[t8][rx + 1][t] - prefix[t8][rx + 1][t - 1] - prefix[t8][lx][t] + prefix[t8][lx][t - 1];
                break;
            }
            else left += a[t8][lx][t - 1] + a[t10][rx][t - 1];

            lx++; rx--;
        }
    }

    int Count(int i, int j, int r, int c)
    {
        int lx = i;
        int rx = r;
        int sum = 0;
        while(lx <= rx)
        {
            int ly = j;
            int ry = c;

            while(ly <= ry)
            {
                int t1 = getMask(i, j, r, c, lx, ly);
                int t2 = getMask(i, j, r, c, lx, ry);
                int t3 = getMask(i, j, r, c, rx, ly);
                int t4 = getMask(i, j, r, c, rx, ry);
                if(t3 == t4 && lx != rx) sum += prefix[t3][rx + 1][ry + 1] - prefix[t3][rx + 1][ly] - prefix[t3][rx][ry + 1] + prefix[t3][rx][ly];
                else if(t3 != t4 && lx != rx) sum += a[t3][rx][ly] + a[t4][rx][ry];

                if(t1 == t2)
                {
                    sum += prefix[t1][lx + 1][ry + 1] - prefix[t1][lx + 1][ly] - prefix[t2][lx][ry + 1] + prefix[t2][lx][ly];
                    break;
                }
                else if(t1 != t2) sum += a[t1][lx][ly] + a[t2][lx][ry];
                ly++; ry--;
            }

            lx++; rx--;
        }

        return sum;
    }

    int getMask(int i, int j, int r, int c, int x, int y)
    {
        int u = min(x - i, 2); int d = min(r - x, 2);
        int l = min(y - j, 2); int rt = min(c - y, 2);
        return u * pow[0] + rt * pow[1] + d * pow[2] + l * pow[3];
    }

    bool check(vector<int>& p, int r, int c)
    {
        return 0 <= r - p[0] && r + p[2] < n &&
               0 <= c - p[3] && c + p[1] < m;
    }

    int modadd(int t1, int t2)
    {
        int res = t1 + t2;
        if(res >= MOD) res -= MOD;
        return res;
    }

    int modmul(int t1, int t2)
    {
        int64_t res = 1LL * t1 * t2;
        return res % MOD;
    }

    int Abs(int tmp)
    {
        if(tmp < 0) return -tmp;
        return tmp;
    }

    int64_t MASK(int j)
    {
        return (1LL << j);
    }

    bool BIT(int64_t tmp, int j)
    {
        return (tmp & MASK(j));
    }
};

void __setup()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
}

int main()
{
    __setup();

    int t = 1;
//    cin >> t;
    for(int i = 1; i <= t; i++)
    {
        Solution().solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 4 ms 596 KB Output is correct
3 Correct 5 ms 596 KB Output is correct
4 Correct 4 ms 596 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 17 ms 1572 KB Output is correct
10 Correct 12 ms 1492 KB Output is correct
11 Correct 2 ms 1692 KB Output is correct
12 Correct 2 ms 1748 KB Output is correct
13 Correct 17 ms 1588 KB Output is correct
14 Correct 10 ms 1108 KB Output is correct
15 Correct 14 ms 1492 KB Output is correct
16 Correct 15 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 17 ms 1572 KB Output is correct
10 Correct 12 ms 1492 KB Output is correct
11 Correct 2 ms 1692 KB Output is correct
12 Correct 2 ms 1748 KB Output is correct
13 Correct 17 ms 1588 KB Output is correct
14 Correct 10 ms 1108 KB Output is correct
15 Correct 14 ms 1492 KB Output is correct
16 Correct 15 ms 1492 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 143 ms 5216 KB Output is correct
19 Correct 35 ms 5264 KB Output is correct
20 Correct 272 ms 5332 KB Output is correct
21 Correct 302 ms 5444 KB Output is correct
22 Correct 291 ms 5332 KB Output is correct
23 Correct 278 ms 5288 KB Output is correct
24 Correct 168 ms 4748 KB Output is correct
25 Correct 241 ms 5296 KB Output is correct
26 Correct 264 ms 5332 KB Output is correct
27 Correct 244 ms 5300 KB Output is correct
28 Correct 286 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 17 ms 1572 KB Output is correct
10 Correct 12 ms 1492 KB Output is correct
11 Correct 2 ms 1692 KB Output is correct
12 Correct 2 ms 1748 KB Output is correct
13 Correct 17 ms 1588 KB Output is correct
14 Correct 10 ms 1108 KB Output is correct
15 Correct 14 ms 1492 KB Output is correct
16 Correct 15 ms 1492 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 143 ms 5216 KB Output is correct
19 Correct 35 ms 5264 KB Output is correct
20 Correct 272 ms 5332 KB Output is correct
21 Correct 302 ms 5444 KB Output is correct
22 Correct 291 ms 5332 KB Output is correct
23 Correct 278 ms 5288 KB Output is correct
24 Correct 168 ms 4748 KB Output is correct
25 Correct 241 ms 5296 KB Output is correct
26 Correct 264 ms 5332 KB Output is correct
27 Correct 244 ms 5300 KB Output is correct
28 Correct 286 ms 5368 KB Output is correct
29 Correct 4 ms 596 KB Output is correct
30 Correct 1006 ms 33300 KB Output is correct
31 Execution timed out 5059 ms 34384 KB Time limit exceeded
32 Halted 0 ms 0 KB -