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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <cstdio>
#include <vector>
#include <unordered_map>
#define lli long long int
#define FOR(i,a,b) for(int i=(int)(a); i<(int)(b); ++i)
using namespace std;

struct Brick {
    int x, y, gid;
    Brick *up, *down, *left, *right;
    bool used;
    Brick(int _x, int _y):x(_x),y(_y),gid(-1),up(0),down(0),left(0),right(0),used(false){}
};

struct Group {
    int removable, size;
    bool active;
    Group():removable(0),size(0),active(true){}
};

typedef Brick *BrickPtr;
typedef unordered_map<int, BrickPtr> SingleMap;
typedef unordered_map<int, SingleMap> DoubleMap;

int n, m, k, q, result;
DoubleMap YXMap;
vector<Group> groups;

BrickPtr get_brick(int x, int y) {
    if (YXMap.find(y) == YXMap.end()) return 0;
    if (YXMap[y].find(x) == YXMap[y].end()) return 0;
    return YXMap[y][x];
}

inline void try_to_enqueue(vector<BrickPtr> &Q, BrickPtr b) {
    if (b == 0 || b->used) return;
    if (((b->left == 0 || b->left->used) && (b->right == 0 || b->right->used)) ||
        ((b->up == 0 || b->up->used) && (b->down == 0 || b->down->used))){
            b->used = true;
            Q.push_back(b);
    }
}

void create_group(BrickPtr b0) {
    int gid = groups.size();
    groups.push_back(Group());
    Group &group = groups[gid];

    // Phase 1 - find all connected bricks and put them into new group (set "gid")
    vector<BrickPtr> group_bricks, Q;
    b0->gid = gid;
    Q.push_back(b0);
    while(!Q.empty()) {
        BrickPtr b = Q.back(); Q.pop_back();
        ++group.size;
        group_bricks.push_back(b);
        if (b->right != 0 && b->right->gid != gid) { b->right->gid = gid; Q.push_back(b->right); }
        if (b->left != 0 && b->left->gid != gid) { b->left->gid = gid; Q.push_back(b->left); }
        if (b->down != 0 && b->down->gid != gid) { b->down->gid = gid; Q.push_back(b->down); }
        if (b->up != 0 && b->up->gid != gid) { b->up->gid = gid; Q.push_back(b->up); }
    }

    // Phase 2 - Find all removable bricks inside new group
    FOR(i,0,group_bricks.size()) group_bricks[i]->used = false;
    FOR(i,0,group_bricks.size()) try_to_enqueue(Q, group_bricks[i]);
    while (!Q.empty()) {
        BrickPtr b = Q.back(); Q.pop_back();
        ++group.removable;
        try_to_enqueue(Q, b->right);
        try_to_enqueue(Q, b->left);
        try_to_enqueue(Q, b->down);
        try_to_enqueue(Q, b->up);
    }

    result += group.removable;
}

void invalidate_group(BrickPtr b) {
    Group &group = groups[b->gid];
    group.active = false;
    result -= group.removable;
}

int main() {
    scanf("%d %d %d %d", &n, &m, &k, &q);

    // Read bricks and build map
    FOR(i,0,k) {
        int x, y;
        scanf("%d %d", &x, &y);
        YXMap[y][x] = new Brick(x, y);
    }

    // Build structure
    for(DoubleMap::iterator ity = YXMap.begin(); ity != YXMap.end(); ++ity) {
        for(SingleMap::iterator itx = ity->second.begin(); itx != ity->second.end(); ++itx) {
            BrickPtr b = itx->second;
            b->right = get_brick(b->x+1, b->y);
            b->left = get_brick(b->x-1, b->y);
            b->down = get_brick(b->x, b->y-1);
            b->up = get_brick(b->x, b->y+1);
        }
    }

    // Create groups
    result = 0;
    for(DoubleMap::iterator ity = YXMap.begin(); ity != YXMap.end(); ++ity) {
        for(SingleMap::iterator itx = ity->second.begin(); itx != ity->second.end(); ++itx) {
            BrickPtr b = itx->second;
            if (b->gid == -1) create_group(b);
        }
    }
    printf("%d\n", result);

    // Handle requests
    FOR(i,0,q) {
        int x, y;
        scanf("%d %d", &x, &y);
        BrickPtr b = get_brick(x, y);

        // Existing brick must be removed
        if (b != 0) {

            // Invalidate brick group
            invalidate_group(b);

            // Unregister brick from structure
            if (b->right) b->right->left = 0;
            if (b->left) b->left->right = 0;
            if (b->down) b->down->up = 0;
            if (b->up) b->up->down = 0;
            YXMap[b->y].erase(b->x);
            if (YXMap[b->y].empty()) YXMap.erase(b->y);

            // Create new groups around removed brick
            if (b->right != 0 && groups[b->right->gid].active == false) create_group(b->right);
            if (b->left != 0 && groups[b->left->gid].active == false) create_group(b->left);
            if (b->down != 0 && groups[b->down->gid].active == false) create_group(b->down);
            if (b->up != 0 && groups[b->up->gid].active == false) create_group(b->up);

            // Delete brick
            delete b;
        }

        // New brick must be created
        else {

            // Create new brick
            b = new Brick(x, y);
            b->right = get_brick(x+1, y);
            b->left = get_brick(x-1, y);
            b->down = get_brick(x, y-1);
            b->up = get_brick(x, y+1);

            // Remove existing groups around new brick
            if (b->right != 0 && groups[b->right->gid].active == true) invalidate_group(b->right);
            if (b->left != 0 && groups[b->left->gid].active == true) invalidate_group(b->left);
            if (b->down != 0 && groups[b->down->gid].active == true) invalidate_group(b->down);
            if (b->up != 0 && groups[b->up->gid].active == true) invalidate_group(b->up);

            // Register new brick inside structure
            if (b->right) b->right->left = b;
            if (b->left) b->left->right = b;
            if (b->down) b->down->up = b;
            if (b->up) b->up->down = b;
            YXMap[y][x] = b;

            // Create new group for brick
            create_group(b);
        }

        printf("%d\n", result);
    }
}