forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdead_pixels.cpp
More file actions
98 lines (82 loc) · 2.36 KB
/
dead_pixels.cpp
File metadata and controls
98 lines (82 loc) · 2.36 KB
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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define INF 1000000000
typedef long long LL;
class hline {
public:
hline(int lt, int rt, int yt, int tt) : left(lt), right(rt), y(yt), type(tt) {}
int left, right, y, type;
bool operator<(const hline& rhs) const {
if (y != rhs.y) return y < rhs.y;
return left < rhs.left;
}
};
int W, H, P, Q, N, X, Y, a, b, c, d;
void run() {
cin >> W >> H >> P >> Q >> N >> X >> Y >> a >> b >> c >> d;
vector<pair<int, int> > mm;
int xx = X, yy = Y;
mm.push_back(make_pair(xx, yy));
FOR(i,1,N-1) {
int tx = (xx * a + yy * b + 1) % W;
int ty = (xx * c + yy * d + 1) % H;
mm.push_back(make_pair(tx, ty));
xx = tx, yy = ty;
}
sort(mm.begin(), mm.end());
mm.erase(unique(mm.begin(), mm.end()), mm.end());
vector<hline> hh;
vector<int> vv;
REP(i,mm.size()) {
xx = mm[i].first, yy = mm[i].second;
int left = max(0, xx - (P - 1));
int top = max(0, yy - (Q - 1));
int right = xx + 1;
int bottom = yy + 1;
hh.push_back(hline(left, right, top, 0));
hh.push_back(hline(left, right, bottom, 1));
vv.push_back(left);
vv.push_back(right);
}
hh.push_back(hline(0, W, H - Q + 1, 0));
hh.push_back(hline(0, W, H, 1));
vv.push_back(0);
vv.push_back(W);
hh.push_back(hline(W - P + 1, W, 0, 0));
hh.push_back(hline(W - P + 1, W, H, 1));
vv.push_back(W - P + 1);
sort(hh.begin(), hh.end());
sort(vv.begin(), vv.end());
vv.erase(unique(vv.begin(), vv.end()), vv.end());
int nv = vv.size(), nh = hh.size();
int area = 0, cnt = 0, top, bot;
FOR(i,0,nv-2) {
int lt = vv[i], rt = vv[i + 1], d = rt - lt;
REP(j,nh) {
if (hh[j].left <= lt && hh[j].right >= rt) {
bot = hh[j].y;
if (cnt > 0) {
area += d * (bot - top);
}
if (hh[j].type == 1) --cnt;
else ++cnt;
top = bot;
}
}
}
int total = W * H;
cout << total - area << endl;
}
int main() {
int m;
cin >> m;
FOR(i,1,m) {
cout << "Case #" << i << ": ";
run();
}
return 0;
}