結果

問題 No.655 E869120 and Good Triangles
ユーザー mamekinmamekin
提出日時 2018-03-25 11:30:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,537 bytes
コンパイル時間 1,469 ms
コンパイル使用メモリ 120,404 KB
実行使用メモリ 441,920 KB
最終ジャッジ日時 2023-09-07 11:26:41
合計ジャッジ時間 27,586 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1,736 ms
441,920 KB
testcase_11 AC 1,469 ms
441,636 KB
testcase_12 AC 1,488 ms
441,864 KB
testcase_13 AC 1,509 ms
441,776 KB
testcase_14 AC 1,480 ms
441,736 KB
testcase_15 AC 1,521 ms
441,760 KB
testcase_16 AC 1,568 ms
441,656 KB
testcase_17 AC 1,578 ms
441,740 KB
testcase_18 AC 1,589 ms
441,700 KB
testcase_19 AC 1,514 ms
441,804 KB
testcase_20 AC 1,575 ms
441,592 KB
testcase_21 AC 1,537 ms
441,652 KB
testcase_22 AC 1,476 ms
441,920 KB
testcase_23 AC 1,493 ms
441,684 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

template <class T>
class cumulativeSum
{
private:
    int ny, nx;
    vector<vector<T> > sum;
public:
    cumulativeSum(){}
    cumulativeSum(const vector<vector<T> >& a)
    {
        ny = a.size();
        nx = a[0].size();
        sum.assign(ny+1, vector<T>(nx+1, 0));
        for(int i=0; i<ny; ++i){
            for(int j=0; j<nx; ++j){
                sum[i+1][j+1] = a[i][j] + sum[i][j+1] + sum[i+1][j] - sum[i][j];
            }
        }
    }
    T getSum(int y1, int x1, int y2, int x2)
    {
        y1 = max(y1, 0);
        x1 = max(x1, 0);
        y2 = min(y2, ny-1);
        x2 = min(x2, nx-1);
        if(y1 > y2 || x1 > x2)
            return 0;
        return sum[y2+1][x2+1] - sum[y1][x2+1] - sum[y2+1][x1] + sum[y1][x1];
    }
};

class TriangleSum
{
private:
    int n;
    cumulativeSum<long long> cs1, cs2;
public:
    TriangleSum(const vector<vector<long long> >& v){
        n = v.size();
        vector<vector<long long> > a(n, vector<long long>(n, 0));
        vector<vector<long long> > b(n, vector<long long>(n, 0));
        for(int i=0; i<n; ++i){
            for(int j=0; j<=i; ++j){
                a[i][j] = v[i][j];
                b[i][n-1-i+j] = v[i][j];
            }
        }
        cs1 = cumulativeSum<long long>(a);
        a.clear();
        cs2 = cumulativeSum<long long>(b);
    }
    long long getSum(int y, int x, int size){
        long long ans = cs1.getSum(y, x, y+size-1, n-1);
        ans -= cs2.getSum(y, n-y+x, y+size-1, n-1);
        return ans;
    }
};

void bfs(int n, const vector<int>& by, const vector<int>& bx, vector<vector<long long> >& dist)
{
    static const int dy[] = {-1, -1, 0, 0, 1, 1};
    static const int dx[] = {-1, 0, -1, 1, 0, 1};

    dist.resize(n);
    for(int i=0; i<n; ++i)
        dist[i].assign(i+1, -1);

    int k = by.size();
    queue<pair<int, int> > q;
    for(int i=0; i<k; ++i){
        dist[by[i]][bx[i]] = 0;
        q.push(make_pair(by[i], bx[i]));
    }

    while(!q.empty()){
        int y, x;
        tie(y, x) = q.front();
        q.pop();
        for(int i=0; i<6; ++i){
            int y2 = y + dy[i];
            int x2 = x + dx[i];
            if(0 <= y2 && y2 < n && 0 <= x2 && x2 <= y2 && dist[y2][x2] == -1){
                dist[y2][x2] = dist[y][x] + 1;
                q.push(make_pair(y2, x2));
            }
        }
    }
}

int main()
{
    int n, k, p;
    cin >> n >> k >> p;

    vector<int> by(k), bx(k);
    for(int i=0; i<k; ++i){
        cin >> by[i] >> bx[i];
        -- by[i];
        -- bx[i];
    }

    vector<vector<long long> > dist;
    bfs(n, by, bx, dist);

    TriangleSum ts(dist);
    long long ans = 0;
    for(int y=0; y<n; ++y){
        for(int x=0; x<=y; ++x){
            int left = 1;
            int right = n - y + 1;
            while(left < right){
                int mid = (left + right) / 2;
                if(ts.getSum(y, x, mid) < p)
                    left = mid + 1;
                else
                    right = mid;
            }
            ans += (n - y + 1) - left;
        }
    }
    cout << ans << endl;

    return 0;
}
0