Add tags

Add tags to the following comment

map を一気に作るようにしてみましたが、それだけじゃ不足みたいですね・・・速くはなったけど依然時間何に終わらず。飽きたのでとりあえずここまで。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
#include <cassert>

#define REP(i, b, e) for (size_t i = b; i < e; ++i)

struct rect
{
    long left, bottom, right, top;
};

template <typename T>
void sort_and_unique(std::vector<T>& v)
{
    std::sort(v.begin(), v.end());

    v.erase(std::unique(v.begin(), v.end()), v.end());
}

template <typename T>
size_t find(const std::vector<T>& v, const T& value)
{
    std::vector<T>::const_iterator it = std::lower_bound(v.begin(), v.end(), value);

    assert(it != v.end());

    return it - v.begin();
}

int main()
{
    long w, h;

    std::cin >> w >> h;

    assert(0 <= w && w <= 1000000);
    assert(0 <= h && h <= 1000000);

    size_t n;

    std::cin >> n;

    assert(1 <= n && n <= 1000);

    std::vector<rect> tapes;

    for (; n; --n)
    {
        rect r;

        std::cin >> r.left >> r.bottom >> r.right >> r.top;

        assert(0 <= r.left && r.left < r.right && r.right <= w);

        assert(0 <= r.bottom && r.bottom < r.top && r.top <= h);

        tapes.push_back(r);
    }

    std::vector<long> xs(2); xs[0] = 0; xs[1] = w;
    std::vector<long> ys(2); ys[0] = 0; ys[1] = h;

    for (std::vector<rect>::const_iterator it = tapes.begin(); it != tapes.end(); ++it)
    {
        xs.push_back(it->left);
        xs.push_back(it->right);
        ys.push_back(it->bottom);
        ys.push_back(it->top);
    }

    sort_and_unique(xs);
    sort_and_unique(ys);

    const size_t xn = xs.size() - 1;
    const size_t yn = ys.size() - 1;

    std::vector<std::vector<int> > map(xn, std::vector<int>(yn, true));

    for (std::vector<rect>::const_iterator it = tapes.begin(); it != tapes.end(); ++it)
    {
        const size_t lower_xi = find(xs, it->left);
        const size_t upper_xi = find(xs, it->right);
        const size_t lower_yi = find(ys, it->bottom);
        const size_t upper_yi = find(ys, it->top);

        REP(xi, lower_xi, upper_xi) REP(yi, lower_yi, upper_yi)
        {
            map[xi][yi] = false;
        }
    }

    size_t count = 0;

    REP(xi, 0, xn) REP(yi, 0, yn) if (map[xi][yi])
    {
        ++count;

        std::queue<std::pair<size_t, size_t> > q;

#define PUSH(XI, YI) if (map[XI][YI]) { map[XI][YI] = false; q.push(std::make_pair(XI, YI)); }

        PUSH(xi, yi);

        while (! q.empty())
        {
            const size_t xj = q.front().first;

            const size_t yj = q.front().second;

            q.pop();

            if (xj >= 1) // left
            {
                PUSH(xj - 1, yj);
            }

            if (yj >= 1) // top
            {
                PUSH(xj, yj - 1);
            }

            if (xj + 1 < xn) // right
            {
                PUSH(xj + 1, yj);
            }

            if (yj + 1 < yn) // bottom
            {
                PUSH(xj, yj + 1);
            }
        }

#undef PUSH

    }

    std::cout << count << std::endl;
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...