Submission #528298

# Submission time Handle Problem Language Result Execution time Memory
528298 2022-02-19T20:39:01 Z doowey Sandcastle 2 (JOI22_ho_t5) C++14
80 / 100
5000 ms 17088 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

vector<vector<int>> A;
vector<vector<ll>> S4;
vector<vector<ll>> S5;
vector<vector<ll>> S6;
vector<vector<ll>> S7;
vector<vector<ll>> S8;

int n, m;

const int inf = 1e6;
int calc(int i, int j, int mode){
    vector<int> V;
    if(mode == 0){
        V = {A[i + 1][j], A[i][j+1]};
    }
    else if(mode == 1){
        V = {A[i + 1][j], A[i][j-1]};
    }
    else if(mode == 2){
        V = {A[i - 1][j], A[i][j+1]};
    }
    else if(mode == 3){
        V = {A[i - 1][j], A[i][j-1]};
    }
    else if(mode == 4){
        V = {A[i][j-1], A[i+1][j], A[i][j+1]};
    }
    else if(mode == 5){
        V = {A[i-1][j], A[i+1][j], A[i][j-1]};
    }
    else if(mode == 6){
        V = {A[i-1][j], A[i][j-1], A[i][j+1]};
    }
    else if(mode == 7){
        V = {A[i-1][j], A[i+1][j], A[i][j+1]};
    }
    else{
        V = {A[i-1][j], A[i+1][j], A[i][j+1],A[i][j-1]};
    }
    int maxi = 0;
    for(auto x : V){
        if(x < A[i][j]){
            maxi = max(maxi, x);
        }
    }

    int high = inf;
    for(auto x : V){
        if(x > A[i][j]){
            high = min(high, x);
        }
    }
    int cum = high - A[i][j];
    if(maxi == 0) cum += A[i][j];
    return cum;
}

map<ll, int> QR;

int main(){
    fastIO;
    //freopen("in.txt","r",stdin);
    cin >> n >> m;
    A.resize(n);
    S4.resize(n);
    S5.resize(n);
    S6.resize(n);
    S7.resize(n);
    S8.resize(n);
    for(int i = 0 ; i < n; i ++ ){
        A[i].resize(m);
        S4[i].resize(m);
        S5[i].resize(m);
        S6[i].resize(m);
        S7[i].resize(m);
        S8[i].resize(m);
    }
    vector<int> C;
    for(int i = 0 ; i < n; i ++ ){
        for(int j = 0 ; j < m ; j ++ ){
            cin >> A[i][j];
            C.push_back(A[i][j]);
        }
    }
    sort(C.begin(), C.end());
    C.resize(unique(C.begin(), C.end()) - C.begin());
    int id;
    for(int i = 0 ; i < n; i ++ ){
        for(int j = 0 ; j < m; j ++ ){
            A[i][j] = upper_bound(C.begin(), C.end(), A[i][j]) - C.begin();
        }
    }
    for(int i = 0; i < n; i ++ ){
        for(int j = 0; j < m; j ++ ){
            if(j > 0 && j + 1 < m && i + 1 < n)S4[i][j] = calc(i, j, 4);
            if(i > 0 && i + 1 < n && j > 0) S5[i][j] = calc(i, j, 5);
            if(i > 0 && j > 0 && j + 1 < m) S6[i][j] = calc(i, j, 6);
            if(j + 1 < m && i > 0 && i + 1 < n) S7[i][j] = calc(i, j, 7);
            if(i > 0 && j > 0 && i + 1 < n && j + 1 < m)S8[i][j] = calc(i, j, 8);
            if(i){
                S5[i][j] += S5[i-1][j];
                S7[i][j] += S7[i-1][j];
                S8[i][j] += S8[i-1][j];
            }
            if(j){
                S4[i][j] += S4[i][j-1];
                S6[i][j] += S6[i][j-1];
                S8[i][j] += S8[i][j-1];
            }
            if(i && j){
                S8[i][j] -= S8[i-1][j-1];
            }
        }
    }
    ll res = 0;
    res += n * m;
    for(int i = 0 ; i < n; i ++ ){
        for(int j = 0 ; j < m ; j ++ ){
            for(int k = j + 1 ; k < m ; k ++ ){
                if(A[i][k] > A[i][k - 1]){
                    break;
                }
                res ++ ;
            }
            for(int k = j - 1; k >= 0 ; k -- ){
                if(A[i][k] > A[i][k + 1]){
                    break;
                }
                res ++ ;
            }
            for(int k = i + 1; k < n; k ++ ){
                if(A[k][j] > A[k - 1][j]){
                    break;
                }
                res ++ ;
            }
            for(int k = i - 1; k >= 0; k -- ){
                if(A[k][j] > A[k + 1][j]){
                    break;
                }
                res ++ ;
            }
        }
    }
    ll add;
    int jj;
    for(int i = 0 ; i < n; i ++ ){
        for(int ii = i + 1; ii < n; ii ++ ){
            QR.clear();
            for(int j = 0; j < m; j ++ ){
                if(j > 0){
                    jj = j;
                    add = 0;
                    add += S8[ii-1][jj-1] - S8[i][jj-1];
                    add += calc(ii,jj,3);
                    add += calc(i,jj,1);
                    add += S4[i][jj-1];
                    add += S6[ii][jj-1];
                    add += S5[ii-1][jj]-S5[i][jj];
                    res += QR[inf-add];

                }
                if(j + 1 < m){
                    add = S8[i][j] - S8[ii-1][j];
                    add += calc(i,j,0);
                    add += calc(ii,j,2);
                    add -= S4[i][j];
                    add -= S6[ii][j];
                    add += S7[ii-1][j]-S7[i][j];

                    QR[add] ++ ;
                }
            }
        }
    }
    cout << res << "\n";
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:99:9: warning: unused variable 'id' [-Wunused-variable]
   99 |     int id;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 447 ms 2792 KB Output is correct
3 Correct 14 ms 2768 KB Output is correct
4 Correct 437 ms 2792 KB Output is correct
5 Correct 31 ms 2912 KB Output is correct
6 Correct 14 ms 2768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 6 ms 716 KB Output is correct
8 Correct 5 ms 716 KB Output is correct
9 Correct 8 ms 332 KB Output is correct
10 Correct 6 ms 404 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 50 ms 460 KB Output is correct
13 Correct 9 ms 400 KB Output is correct
14 Correct 5 ms 332 KB Output is correct
15 Correct 13 ms 332 KB Output is correct
16 Correct 12 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 6 ms 716 KB Output is correct
8 Correct 5 ms 716 KB Output is correct
9 Correct 8 ms 332 KB Output is correct
10 Correct 6 ms 404 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 50 ms 460 KB Output is correct
13 Correct 9 ms 400 KB Output is correct
14 Correct 5 ms 332 KB Output is correct
15 Correct 13 ms 332 KB Output is correct
16 Correct 12 ms 332 KB Output is correct
17 Correct 106 ms 2688 KB Output is correct
18 Correct 140 ms 680 KB Output is correct
19 Correct 16 ms 724 KB Output is correct
20 Correct 46 ms 680 KB Output is correct
21 Correct 66 ms 588 KB Output is correct
22 Correct 82 ms 668 KB Output is correct
23 Correct 89 ms 660 KB Output is correct
24 Correct 101 ms 588 KB Output is correct
25 Correct 106 ms 680 KB Output is correct
26 Correct 91 ms 672 KB Output is correct
27 Correct 122 ms 588 KB Output is correct
28 Correct 89 ms 672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 6 ms 716 KB Output is correct
8 Correct 5 ms 716 KB Output is correct
9 Correct 8 ms 332 KB Output is correct
10 Correct 6 ms 404 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 50 ms 460 KB Output is correct
13 Correct 9 ms 400 KB Output is correct
14 Correct 5 ms 332 KB Output is correct
15 Correct 13 ms 332 KB Output is correct
16 Correct 12 ms 332 KB Output is correct
17 Correct 106 ms 2688 KB Output is correct
18 Correct 140 ms 680 KB Output is correct
19 Correct 16 ms 724 KB Output is correct
20 Correct 46 ms 680 KB Output is correct
21 Correct 66 ms 588 KB Output is correct
22 Correct 82 ms 668 KB Output is correct
23 Correct 89 ms 660 KB Output is correct
24 Correct 101 ms 588 KB Output is correct
25 Correct 106 ms 680 KB Output is correct
26 Correct 91 ms 672 KB Output is correct
27 Correct 122 ms 588 KB Output is correct
28 Correct 89 ms 672 KB Output is correct
29 Execution timed out 5035 ms 17088 KB Time limit exceeded
30 Halted 0 ms 0 KB -