답안 #106663

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
106663 2019-04-19T12:53:46 Z polyfish 무지개나라 (APIO17_rainbow) C++14
74 / 100
3000 ms 117160 KB
#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;

#define debug(x) cerr << #x << " = " << x << '\n';
#define BP() cerr << "OK!\n";
#define PR(A, n) {cerr << #A << " = "; for (int _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define PR0(A, n) {cerr << #A << " = "; for (int _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define FILE_NAME "data"

const int X[] = {1, 0, -1, 0};
const int Y[] = {0, 1, 0, -1};
const int INF = 1e9;

struct fenwick_tree_2D {

    int m, n;
    vector<vector<int> > bit;

    void init(int _m, int _n) {
        m = _m;
        n = _n;
        bit.resize(m+1);
        for (int i=0; i<=m; ++i)
            bit[i].resize(n+1);
    }

    void add(int x, int y) {
        if (!bit[x][y])
            ++bit[x][y];
    }

    void process() {
        for (int i=1; i<=m; ++i) {
            for (int j=1; j<=n; ++j)
                bit[i][j] += bit[i-1][j] + bit[i][j-1] - bit[i-1][j-1];
        }
    }

    int get(int x1, int y1, int x2, int y2) {
        if (x1>x2 || y1>y2)
            return 0;
        return bit[x2][y2] - bit[x2][y1-1] - bit[x1-1][y2] + bit[x1-1][y1-1];
    }
};

int m, n, min_x, max_x, min_y, max_y;
vector<pair<int, int> > snake;
bool visited[52][52];
fenwick_tree_2D T1, T2, R, C;

void add(pair<int, int> p) {
    T1.add(p.first, p.second);

    for (int i=0; i<4; ++i) {
        T2.add(p.first, p.second);
        p.first += X[i];
        p.second += Y[i];
    }

    C.add(p.first, p.second);
    C.add(p.first, p.second+1);
    R.add(p.first, p.second);
    R.add(p.first+1, p.second);
}

void init(int _R, int _C, int sr, int sc, int M, char *S) {
    m = _R;
    n = _C;
    max_x = max_y = -INF;
    min_x = min_y = INF;

    snake.resize(M+1);
    snake[0] = {sr, sc};
    for (int i=1; i<=M; ++i) {
        snake[i] = snake[i-1];
        if (S[i-1]=='N')
            --snake[i].first;
        else if (S[i-1]=='S')
            ++snake[i].first;
        else if (S[i-1]=='W')
            --snake[i].second;
        else
            ++snake[i].second;
    }

    sort(snake.begin(), snake.end());
    snake.resize(unique(snake.begin(), snake.end()) - snake.begin());

    if (1LL*m*n<=1000000) {
        T1.init(m+1, n+1);
        T2.init(m+1, n+1);
        R.init(m+1, n+1);
        C.init(m+1, n+1);
    }

    for (auto p : snake) {
        min_x = min(min_x, p.first);
        max_x = max(max_x, p.first);
        min_y = min(min_y, p.second);
        max_y = max(max_y, p.second);

        if (1LL*m*n<=1000000)
            add(p);
    }

    if (1LL*m*n<=1000000) {
        T1.process();
        T2.process();
        R.process();
        C.process();
    }
}

int subtask3(int x1, int y1, int x2, int y2) {
    set<pair<int, int> > v;
    set<pair<pair<int, int>, pair<int, int> > > e;

    int res = 0;

    for (int i=x1; i<=x2+1; ++i) {
        v.insert({i, y1});
        v.insert({i, y2+1});
        if (i<=x2) {
            pair<int, int> tmp1(i, y1), tmp2(i+1, y1);
            e.insert({min(tmp1, tmp2), max(tmp1, tmp2)});
            pair<int, int> tmp3(i, y2+1), tmp4(i+1, y2+1);
            e.insert({min(tmp3, tmp4), max(tmp3, tmp4)});
        }
    }

    for (int i=y1; i<=y2+1; ++i) {
        v.insert({x1, i});
        v.insert({x2+1, i});
        if (i<=y2) {
            pair<int, int> tmp1(x1, i), tmp2(x1, i+1);
            e.insert({min(tmp1, tmp2), max(tmp1, tmp2)});
            pair<int, int> tmp3(x2+1, i), tmp4(x2+1, i+1);
            e.insert({min(tmp3, tmp4), max(tmp3, tmp4)});
        }
    }

    for (auto p : snake) {
        if (x1<=p.first && p.first<=x2 && y1<=p.second && p.second<=y2) {
            --res;
            for (int i=0; i<4; ++i) {
                v.insert(p);
                pair<int, int> tmp = p;
                p.first += X[i];
                p.second += Y[i];
                e.insert({min(p, tmp), max(p, tmp)});
            }
        }
    }
//    debug(v.size());
//    debug(e.size());

    res += (int)e.size() - (int)v.size() + 1;

    if (x1<min_x && max_x<x2 && y1<min_y && max_y<y2)
        ++res;

    return res;
}

int colour(int x1, int y1, int x2, int y2) {
    if (1LL*m*n>1000000)
        return subtask3(x1, y1, x2, y2);

    int v = T2.get(x1+1, y1+1, x2, y2);
    int e = R.get(x1+1, y1, x2, y2) + C.get(x1, y1+1, x2, y2);
//    debug(R.get(x1+1, y1+1, x2, y2-1));

    int res = e - v + 1;
    if (x1<min_x && max_x<x2 && y1<min_y && max_y<y2)
        ++res;
    res -= T1.get(x1, y1, x2, y2);

    return res;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 115 ms 14584 KB Output is correct
4 Correct 111 ms 14968 KB Output is correct
5 Correct 141 ms 15096 KB Output is correct
6 Correct 107 ms 15096 KB Output is correct
7 Correct 97 ms 15224 KB Output is correct
8 Correct 115 ms 15000 KB Output is correct
9 Correct 121 ms 14844 KB Output is correct
10 Correct 144 ms 14968 KB Output is correct
11 Correct 116 ms 15096 KB Output is correct
12 Correct 93 ms 14964 KB Output is correct
13 Correct 97 ms 14968 KB Output is correct
14 Correct 113 ms 15032 KB Output is correct
15 Correct 107 ms 15028 KB Output is correct
16 Correct 127 ms 14840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 409 ms 48292 KB Output is correct
3 Correct 343 ms 48228 KB Output is correct
4 Correct 355 ms 51436 KB Output is correct
5 Correct 164 ms 25180 KB Output is correct
6 Correct 467 ms 55388 KB Output is correct
7 Correct 675 ms 84088 KB Output is correct
8 Correct 17 ms 6400 KB Output is correct
9 Correct 21 ms 6912 KB Output is correct
10 Correct 17 ms 2560 KB Output is correct
11 Correct 39 ms 6048 KB Output is correct
12 Correct 96 ms 13320 KB Output is correct
13 Correct 110 ms 13304 KB Output is correct
14 Correct 95 ms 13468 KB Output is correct
15 Correct 105 ms 13176 KB Output is correct
16 Correct 365 ms 44924 KB Output is correct
17 Correct 321 ms 40852 KB Output is correct
18 Correct 458 ms 55140 KB Output is correct
19 Correct 545 ms 62044 KB Output is correct
20 Correct 499 ms 56824 KB Output is correct
21 Correct 22 ms 6400 KB Output is correct
22 Correct 25 ms 6976 KB Output is correct
23 Correct 201 ms 28280 KB Output is correct
24 Correct 299 ms 41680 KB Output is correct
25 Correct 1039 ms 117092 KB Output is correct
26 Correct 918 ms 117140 KB Output is correct
27 Correct 948 ms 117160 KB Output is correct
28 Correct 932 ms 110048 KB Output is correct
29 Correct 764 ms 93100 KB Output is correct
30 Correct 898 ms 98908 KB Output is correct
31 Correct 998 ms 117112 KB Output is correct
32 Correct 924 ms 111736 KB Output is correct
33 Correct 937 ms 111840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 132 ms 17924 KB Output is correct
19 Correct 72 ms 2680 KB Output is correct
20 Correct 61 ms 1268 KB Output is correct
21 Correct 75 ms 1656 KB Output is correct
22 Correct 83 ms 2680 KB Output is correct
23 Correct 88 ms 2680 KB Output is correct
24 Correct 79 ms 1192 KB Output is correct
25 Correct 70 ms 1912 KB Output is correct
26 Correct 96 ms 2624 KB Output is correct
27 Correct 118 ms 17840 KB Output is correct
28 Correct 130 ms 17784 KB Output is correct
29 Correct 140 ms 17764 KB Output is correct
30 Correct 132 ms 17784 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 102 ms 17788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 132 ms 17924 KB Output is correct
19 Correct 72 ms 2680 KB Output is correct
20 Correct 61 ms 1268 KB Output is correct
21 Correct 75 ms 1656 KB Output is correct
22 Correct 83 ms 2680 KB Output is correct
23 Correct 88 ms 2680 KB Output is correct
24 Correct 79 ms 1192 KB Output is correct
25 Correct 70 ms 1912 KB Output is correct
26 Correct 96 ms 2624 KB Output is correct
27 Correct 118 ms 17840 KB Output is correct
28 Correct 130 ms 17784 KB Output is correct
29 Correct 140 ms 17764 KB Output is correct
30 Correct 132 ms 17784 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 102 ms 17788 KB Output is correct
33 Correct 409 ms 48292 KB Output is correct
34 Correct 343 ms 48228 KB Output is correct
35 Correct 355 ms 51436 KB Output is correct
36 Correct 164 ms 25180 KB Output is correct
37 Correct 467 ms 55388 KB Output is correct
38 Correct 675 ms 84088 KB Output is correct
39 Correct 17 ms 6400 KB Output is correct
40 Correct 21 ms 6912 KB Output is correct
41 Correct 17 ms 2560 KB Output is correct
42 Correct 39 ms 6048 KB Output is correct
43 Correct 96 ms 13320 KB Output is correct
44 Correct 110 ms 13304 KB Output is correct
45 Correct 95 ms 13468 KB Output is correct
46 Correct 105 ms 13176 KB Output is correct
47 Correct 365 ms 44924 KB Output is correct
48 Correct 321 ms 40852 KB Output is correct
49 Correct 458 ms 55140 KB Output is correct
50 Correct 545 ms 62044 KB Output is correct
51 Correct 499 ms 56824 KB Output is correct
52 Correct 22 ms 6400 KB Output is correct
53 Correct 25 ms 6976 KB Output is correct
54 Correct 201 ms 28280 KB Output is correct
55 Correct 299 ms 41680 KB Output is correct
56 Correct 1039 ms 117092 KB Output is correct
57 Correct 918 ms 117140 KB Output is correct
58 Correct 948 ms 117160 KB Output is correct
59 Correct 932 ms 110048 KB Output is correct
60 Correct 764 ms 93100 KB Output is correct
61 Correct 898 ms 98908 KB Output is correct
62 Correct 998 ms 117112 KB Output is correct
63 Correct 924 ms 111736 KB Output is correct
64 Correct 937 ms 111840 KB Output is correct
65 Correct 151 ms 9920 KB Output is correct
66 Correct 135 ms 10332 KB Output is correct
67 Execution timed out 3034 ms 6544 KB Time limit exceeded
68 Halted 0 ms 0 KB -