Problem:
平面に長方形のシートが何枚か置かれている. これらのシートで覆われている部分の面積や周長を求めるプログラムを作成せよ.
ただし, 平面を座標平面とみなしたとき, シートの配置は次の条件 (1), (2) を満たすものとする.
(1) 各シートの長方形の 4 頂点の
x
,
y
座標は全て 0 以上 10000 以下の整数であり, 長方形の各辺は
x
軸, または
y
軸に平行である.
(2) シートの枚数は高々 10000 枚以下である.
入力データ の 1 行目に長方形の個数
n
と問題の種類を表す整数
r
が空白で区切って書かれている. 2 行目以降の各行には, 各シートの左下頂点座標 (
x
1
,
y
1
) と右上頂点座標 (
x
2
,
y
2
) の座標値が
x
1
,
y
1
,
x
2
,
y
2
の順で空白で区切って書いてある.
出力には
r
= 1 のとき 1 行目に面積を,
r
= 2 のとき 1 行目に面積, 2 行目に周囲の長さを出力する. いずれの場合も最後に改行を入れること.
また, テストデータの 40% では長方形の頂点の座標が 0 以上 100 以下で, そのうちの 1/2 では面積だけを求める問題である. さらに, 全体の 1/2 は面積だけを求める問題である.
入力例１
入力例２
入力例３
入力例４
5 1
5 2
2 2
3 2
0 0 3 2
0 0 3 2
0 0 8 9
2 2 8 8
1 1 2 5
1 1 2 5
0 0 9 8
3 0 4 9
0 4 6 5
0 4 6 5
5 0 7 9
3 3 5 6
3 3 5 6
5 0 7 6
5 0 7 6
出力例１
出力例２
出力例３
出力例４
29
29
80
45
38
36
36
入力
入力は複数のデータセットからなる．
n
,
r
がともに 0 のとき入力が終了する．データセットの数は 10 を超えない．
出力
データセットごとに,
r
= 1 のとき 1 行目に面積を,
r
= 2 のとき 1 行目に面積, 2 行目に周囲の長さを出力する.
入力例
5 1
0 0 3 2
1 1 2 5
0 4 6 5
3 3 5 6
5 0 7 6
5 2
0 0 3 2
1 1 2 5
0 4 6 5
3 3 5 6
5 0 7 6
2 2
0 0 8 9
0 0 9 8
3 2
2 2 8 8
3 0 4 9
5 0 7 9
0 0
出力例
29
29
38
80
36
45
36
上記問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;

constexpr int MAX_Y = 10003;
constexpr int MAX_X = 10003;

int main() {
    int n, r;
    while(cin >> n >> r, n) {
        vector<vector<P>> v(MAX_Y);
        for(int i=0; i<n; ++i) {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            ++x1; ++y1; ++x2; ++y2;
            v[y1].emplace_back(x1, 1);
            v[y1].emplace_back(x2, -1);
            v[y2].emplace_back(x1, -1);
            v[y2].emplace_back(x2, 1);
        }
        vector<vector<int>> sum(2, vector<int>(MAX_X));
        int S = 0, len = 0;
        for(int i=0; i<MAX_Y; ++i) {
            int now = i % 2, pre = (i+1) % 2;
            fill(sum[now].begin(), sum[now].end(), 0);
            for(int j=0; j<v[i].size(); ++j) {
                sum[now][v[i][j].first] += v[i][j].second;
            }
            for(int j=0; j<MAX_X; ++j) {
                sum[now][j+1] += sum[now][j];
                sum[now][j] += sum[pre][j];
            }
            for(int j=0; j<MAX_X; ++j) {
                S += sum[now][j] > 0;
                len += ((sum[pre][j] > 0) != (sum[now][j] > 0));
                len += ((sum[now][j] > 0) != (sum[now][j+1] > 0));
            }
        }
        
        if(r == 1) {
            cout << S << endl;
        } else {
            cout << S << '\n' << len << endl;
        }
    }
}
accuracy: 1
time_ms: 2170
memory_kb: 4012
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while (true) {
        int A, B;
        cin >> A >> B;
        if (!cin || A == 0) 
            break;  // A＝0 で入力終了

        // y 座標ごとに「開始＋1」「終了−1」を記録する差分配列
        static constexpr int MAX_W = 10002;
        vector<vector<pair<int,int>>> add(MAX_W), sub(MAX_W);

        for (int i = 0; i < A; i++) {
            int l, u, r, d;
            cin >> l >> u >> r >> d;
            // 1-index → 差分しやすいように +1
            ++l; ++r;
            add[u].emplace_back(l, r);
            sub[d].emplace_back(l, r);
        }

        // 現在の行・次の行のオンセル数
        vector<int> curr(MAX_W, 0), next = curr;
        long long area = 0;    // 面積（オンセルの合計）
        long long circ = 0;    // 周囲長

        // 各行 y をスキャン
        for (int y = 0; y < MAX_W - 1; y++) {
            // 差分を反映
            next = curr;
            for (auto [l, r] : add[y]) {
                for (int x = l; x < r; x++) 
                    next[x]++;
            }
            for (auto [l, r] : sub[y]) {
                for (int x = l; x < r; x++) 
                    next[x]--;
            }
            // セルごとに面積・境界を数える
            for (int x = 0; x < MAX_W; x++) {
                bool on    = next[x] != 0;
                bool above = curr[x] != 0;
                bool left  = (x > 0) && (next[x-1] != 0);

                // 面積: on セルをカウント
                if (on) 
                    area++;

                // 上下境界: 前の行との違い
                if (on != above) 
                    circ++;

                // 左右境界: 左のセルとの違い
                if (x > 0 && (on != left)) 
                    circ++;
            }
            curr.swap(next);
        }

        // 出力
        cout << area << "\n";
        if (B == 2) 
            cout << circ << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 1330
memory_kb: 4452
final_rank: 12

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

const int N = 10003;
int s[N],pre[N];

void slv(int n,int r) {
    vector<pair<pair<int,int>,pair<int,int>>> v;
    while (n--) {
    	int x,y,xx,yy; cin >> x >> y >> xx >> yy;
    	x++; y++;
    	v.push_back({{x,1}, {y,yy+1}});
    	v.push_back({{xx+1, -1}, {y,yy+1}});
    }
    int sum = 0;
    int len = 0;
    sort(v.begin(), v.end());
    auto nonzero = [&](int x) -> bool {
    	return x != 0;	
    };
    int idx = 0;
    for (int i = 0; i < N; i++) {
        s[i] = pre[i] = 0;
    }
    for (int i = 1; i < N; i++) {
    	while (idx < v.size() and v[idx].first.first == i) {
    		s[v[idx].second.first] += v[idx].first.second;
    		s[v[idx].second.second] -= v[idx].first.second;
    		idx++;
    	}
    	int cur = 0;
    	for (int j = 0; j < N; j++) {
    		cur += s[j];
    		if (nonzero(cur) != nonzero(pre[j])) len++;
    		if (nonzero(cur)) sum++;
    		pre[j] = cur;
    		if (j and nonzero(pre[j]) != nonzero(pre[j-1])) len++;
    	}
    }
    cout << sum << endl;
    if (r == 2) cout << len << endl;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,r;
    while (cin >> n >> r, n+r) {
        slv(n,r);
    }
}
accuracy: 1
time_ms: 1300
memory_kb: 3756
final_rank: 1

code_snippet: #include <iostream>
#include <vector>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;

using Pii = pair<int, int>;

const int max_y = 10002;
const int max_x = 10002;

int main() {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int n, r;

	while (cin >> n >> r, n) {
		vector<vector<Pii>> imos(max_y);
		for (int i = 0; i < n; ++i) {
			int x0, y0, x1, y1;

			cin >> x0 >> y0 >> x1 >> y1;
			imos[y0 + 1].emplace_back(x0 + 1, 1);
			imos[y0 + 1].emplace_back(x1 + 1, -1);
			imos[y1 + 1].emplace_back(x0 + 1, -1);
			imos[y1 + 1].emplace_back(x1 + 1, 1);
		}

		vector<vector<int>> cum_sum(2, vector<int>(max_x));
		int area = 0, length = 0;

		for (int i = 0; i < max_y; ++i) {
			int cur = i % 2, pre = (i + 1) % 2;

			fill(cum_sum[cur].begin(), cum_sum[cur].end(), 0);
			for (int j = 0; j < (int)imos[i].size(); ++j) {
				cum_sum[cur][imos[i][j].first] += imos[i][j].second;
			}
			for (int j = 0; j < max_x - 1; ++j) {
				cum_sum[cur][j + 1] += cum_sum[cur][j];
				cum_sum[cur][j] += cum_sum[pre][j];
			}
			for (int j = 0; j < max_x - 1; ++j) {
				area += cum_sum[cur][j] > 0;
				length += ((cum_sum[pre][j] > 0) != (cum_sum[cur][j] > 0));
				length += ((cum_sum[cur][j] > 0) != (cum_sum[cur][j + 1] > 0));
			}
		}

		cout << area << endl;
		if (r == 2) {
			cout << length << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 3230
memory_kb: 3692
final_rank: 17

code_snippet: #include <iostream>
#include <vector>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;

using Pii = pair<int, int>;

const int max_y = 10002;
const int max_x = 10002;

int main() {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int n, r;
	while (cin >> n >> r, n) {
		vector<vector<Pii>> imos(max_y);
		for (int i = 0; i < n; ++i) {
			int x0, y0, x1, y1;
			cin >> x0 >> y0 >> x1 >> y1;
			imos[y0 + 1].emplace_back(x0 + 1, 1);
			imos[y0 + 1].emplace_back(x1 + 1, -1);
			imos[y1 + 1].emplace_back(x0 + 1, -1);
			imos[y1 + 1].emplace_back(x1 + 1, 1);
		}
		vector<vector<int>> cum_sum(2, vector<int>(max_x));
		int area = 0, length = 0;
		for (int i = 0; i < max_y; ++i) {
			int cur = i % 2, pre = (i + 1) % 2;
			fill(cum_sum[cur].begin(), cum_sum[cur].end(), 0);
			for (int j = 0; j < (int)imos[i].size(); ++j) {
				cum_sum[cur][imos[i][j].first] += imos[i][j].second;
			}
			for (int j = 0; j < max_x - 1; ++j) {
				cum_sum[cur][j + 1] += cum_sum[cur][j];
				cum_sum[cur][j] += cum_sum[pre][j];
			}
			for (int j = 0; j < max_x - 1; ++j) {
				area += cum_sum[cur][j] > 0;
				length += ((cum_sum[pre][j] > 0) != (cum_sum[cur][j] > 0));
				length += ((cum_sum[cur][j] > 0) != (cum_sum[cur][j + 1] > 0));
			}
		}

		cout << area << endl;
		if (r == 2) {
			cout << length << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 3230
memory_kb: 3552
final_rank: 9

code_snippet: #include <iostream>
#include <vector>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;

using Pii = pair<int, int>;

const int max_y = 10002;
const int max_x = 10003;

int main() {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int n, r;
	while (cin >> n >> r, n) {
		vector<vector<Pii>> imos(max_y);
		for (int i = 0; i < n; ++i) {
			int x0, y0, x1, y1;
			cin >> x0 >> y0 >> x1 >> y1;
			imos[y0 + 1].emplace_back(x0 + 1, 1);
			imos[y0 + 1].emplace_back(x1 + 1, -1);
			imos[y1 + 1].emplace_back(x0 + 1, -1);
			imos[y1 + 1].emplace_back(x1 + 1, 1);
		}
		vector<vector<int>> cum_sum(2, vector<int>(max_x));
		int area = 0, length = 0;
		for (int i = 0; i < max_y; ++i) {
			int cur = i % 2, pre = (i + 1) % 2;
			fill(cum_sum[cur].begin(), cum_sum[cur].end(), 0);
			for (int j = 0; j < (int)imos[i].size(); ++j) {
				cum_sum[cur][imos[i][j].first] += imos[i][j].second;
			}
			for (int j = 0; j < max_x - 1; ++j) {
				cum_sum[cur][j + 1] += cum_sum[cur][j];
				cum_sum[cur][j] += cum_sum[pre][j];
			}
			for (int j = 0; j < max_x - 1; ++j) {
				area += cum_sum[cur][j] > 0;
				length += ((cum_sum[pre][j] > 0) != (cum_sum[cur][j] > 0));
				length += ((cum_sum[cur][j] > 0) != (cum_sum[cur][j + 1] > 0));
			}
		}

		cout << area << endl;
		if (r == 2) {
			cout << length << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 3230
memory_kb: 3560
final_rank: 11

code_snippet: #include <iostream>
#include <vector>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;

using Pii = pair<int, int>;

const int max_y = 10003;
const int max_x = 10003;

int main() {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int n, r;
	while (cin >> n >> r, n) {
		vector<vector<Pii>> imos(max_y);
		for (int i = 0; i < n; ++i) {
			int x0, y0, x1, y1;
			cin >> x0 >> y0 >> x1 >> y1;
			imos[y0 + 1].emplace_back(x0 + 1, 1);
			imos[y0 + 1].emplace_back(x1 + 1, -1);
			imos[y1 + 1].emplace_back(x0 + 1, -1);
			imos[y1 + 1].emplace_back(x1 + 1, 1);
		}
		vector<vector<int>> cum_sum(2, vector<int>(max_x));
		int area = 0, length = 0;
		for (int i = 0; i < max_y; ++i) {
			int cur = i % 2, pre = (i + 1) % 2;
			fill(cum_sum[cur].begin(), cum_sum[cur].end(), 0);
			for (int j = 0; j < (int)imos[i].size(); ++j) {
				cum_sum[cur][imos[i][j].first] += imos[i][j].second;
			}
			for (int j = 0; j < max_x - 1; ++j) {
				cum_sum[cur][j + 1] += cum_sum[cur][j];
				cum_sum[cur][j] += cum_sum[pre][j];
			}
			for (int j = 0; j < max_x - 1; ++j) {
				area += cum_sum[cur][j] > 0;
				length += ((cum_sum[pre][j] > 0) != (cum_sum[cur][j] > 0));
				length += ((cum_sum[cur][j] > 0) != (cum_sum[cur][j + 1] > 0));
			}
		}

		cout << area << endl;
		if (r == 2) {
			cout << length << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 3220
memory_kb: 3560
final_rank: 10

code_snippet: #include <iostream>
#include <vector>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;

using Pii = pair<int, int>;

const int max_y = 10003;
const int max_x = 10003;

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int n, r;
	while (cin >> n >> r, n) {
		vector<vector<Pii>> imos(max_y);
		for (int i = 0; i < n; ++i) {
			int x0, y0, x1, y1;
			cin >> x0 >> y0 >> x1 >> y1;
			imos[y0 + 1].emplace_back(x0 + 1, 1);
			imos[y0 + 1].emplace_back(x1 + 1, -1);
			imos[y1 + 1].emplace_back(x0 + 1, -1);
			imos[y1 + 1].emplace_back(x1 + 1, 1);
		}
		vector<vector<int>> cum_sum(2, vector<int>(max_x));
		int area = 0, length = 0;
		for (int i = 0; i < max_y; ++i) {
			int cur = i % 2, pre = (i + 1) % 2;
			fill(cum_sum[cur].begin(), cum_sum[cur].end(), 0);
			for (int j = 0; j < imos[i].size(); ++j) {
				cum_sum[cur][imos[i][j].first] += imos[i][j].second;
			}
			for (int j = 0; j < max_x - 1; ++j) {
				cum_sum[cur][j + 1] += cum_sum[cur][j];
				cum_sum[cur][j] += cum_sum[pre][j];
			}
			for (int j = 0; j < max_x - 1; ++j) {
				area += cum_sum[cur][j] > 0;
				length += ((cum_sum[pre][j] > 0) != (cum_sum[cur][j] > 0));
				length += ((cum_sum[cur][j] > 0) != (cum_sum[cur][j + 1] > 0));
			}
		}

		cout << area << endl;
		if (r == 2) {
			cout << length << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 3240
memory_kb: 3700
final_rank: 19

code_snippet: #include <iostream>
#include <vector>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;

using Pii = pair<int, int>;

constexpr int max_y = 10003;
constexpr int max_x = 10003;

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int n, r;
	while (cin >> n >> r, n) {
		vector<vector<Pii>> imos(max_y);
		for (int i = 0; i < n; ++i) {
			int x0, y0, x1, y1;
			cin >> x0 >> y0 >> x1 >> y1;
			imos[y0 + 1].emplace_back(x0 + 1, 1);
			imos[y0 + 1].emplace_back(x1 + 1, -1);
			imos[y1 + 1].emplace_back(x0 + 1, -1);
			imos[y1 + 1].emplace_back(x1 + 1, 1);
		}
		vector<vector<int>> cum_sum(2, vector<int>(max_x));
		int area = 0, length = 0;
		for (int i = 0; i < max_y; ++i) {
			int cur = i % 2, pre = (i + 1) % 2;
			fill(cum_sum[cur].begin(), cum_sum[cur].end(), 0);
			for (int j = 0; j < imos[i].size(); ++j) {
				cum_sum[cur][imos[i][j].first] += imos[i][j].second;
			}
			for (int j = 0; j < max_x - 1; ++j) {
				cum_sum[cur][j + 1] += cum_sum[cur][j];
				cum_sum[cur][j] += cum_sum[pre][j];
			}
			for (int j = 0; j < max_x - 1; ++j) {
				area += cum_sum[cur][j] > 0;
				length += ((cum_sum[pre][j] > 0) != (cum_sum[cur][j] > 0));
				length += ((cum_sum[cur][j] > 0) != (cum_sum[cur][j + 1] > 0));
			}
		}

		cout << area << endl;
		if(r == 2) {
			cout << length << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 3230
memory_kb: 3692
final_rank: 17

code_snippet: #include <iostream>
#include <vector>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;

using Pii = pair<int, int>;

constexpr int max_y = 10003;
constexpr int max_x = 10003;

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int n, r;
	while (cin >> n >> r, n) {
		vector<vector<Pii>> imos(max_y);
		for (int i = 0; i < n; ++i) {
			int x0, y0, x1, y1;
			cin >> x0 >> y0 >> x1 >> y1;
			imos[y0 + 1].emplace_back(x0 + 1, 1);
			imos[y0 + 1].emplace_back(x1 + 1, -1);
			imos[y1 + 1].emplace_back(x0 + 1, -1);
			imos[y1 + 1].emplace_back(x1 + 1, 1);
		}
		vector<vector<int>> cum_sum(2, vector<int>(max_x));
		int area = 0, length = 0;
		for (int i = 0; i < max_y; ++i) {
			int cur = i % 2, pre = (i + 1) % 2;
			fill(cum_sum[cur].begin(), cum_sum[cur].end(), 0);
			for (int j = 0; j < imos[i].size(); ++j) {
				cum_sum[cur][imos[i][j].first] += imos[i][j].second;
			}
			for (int j = 0; j < max_x - 1; ++j) {
				cum_sum[cur][j + 1] += cum_sum[cur][j];
				cum_sum[cur][j] += cum_sum[pre][j];
			}
			for (int j = 0; j < max_x - 1; ++j) {
				area += cum_sum[cur][j] > 0;
				length += ((cum_sum[pre][j] > 0) != (cum_sum[cur][j] > 0));
				length += ((cum_sum[cur][j] > 0) != (cum_sum[cur][j + 1] > 0));
			}
		}

		if (r == 1) {
			cout << area << endl;
		}
		else {
			cout << area << '\n' << length << endl;
		}
	}
}
accuracy: 1
time_ms: 3220
memory_kb: 3612
final_rank: 15

code_snippet: #include <iostream>
#include <vector>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;

using Pii = pair<int, int>;

constexpr int max_y = 10003;
constexpr int max_x = 10003;

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int n, r;
	while (cin >> n >> r, n) {
		vector<vector<Pii>> imos(max_y);
		for (int i = 0; i < n; ++i) {
			int x0, y0, x1, y1;
			cin >> x0 >> y0 >> x1 >> y1;
			imos[y0 + 1].emplace_back(x0 + 1, 1);
			imos[y0 + 1].emplace_back(x1 + 1, -1);
			imos[y1 + 1].emplace_back(x0 + 1, -1);
			imos[y1 + 1].emplace_back(x1 + 1, 1);
		}
		vector<vector<int>> sum(2, vector<int>(max_x));
		int S = 0, len = 0;
		for (int i = 0; i < max_y; ++i) {
			int now = i % 2, pre = (i + 1) % 2;
			fill(sum[now].begin(), sum[now].end(), 0);
			for (int j = 0; j < imos[i].size(); ++j) {
				sum[now][imos[i][j].first] += imos[i][j].second;
			}
			for (int j = 0; j < max_x - 1; ++j) {
				sum[now][j + 1] += sum[now][j];
				sum[now][j] += sum[pre][j];
			}
			for (int j = 0; j < max_x - 1; ++j) {
				S += sum[now][j] > 0;
				len += ((sum[pre][j] > 0) != (sum[now][j] > 0));
				len += ((sum[now][j] > 0) != (sum[now][j + 1] > 0));
			}
		}

		if (r == 1) {
			cout << S << endl;
		}
		else {
			cout << S << '\n' << len << endl;
		}
	}
}
accuracy: 1
time_ms: 3220
memory_kb: 3552
final_rank: 8

code_snippet: #include <iostream>
#include <vector>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;

using Pii = pair<int, int>;

constexpr int max_y = 10003;
constexpr int max_x = 10003;

int main() {
#if READ_FROM_FILE
    std::ifstream ifs("test1.txt");
    cin.rdbuf(ifs.rdbuf());
#endif
    int n, r;
    while(cin >> n >> r, n) {
        vector<vector<Pii>> v(max_y);
        for(int i=0; i<n; ++i) {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            v[y1+1].emplace_back(x1+1, 1);
            v[y1+1].emplace_back(x2+1, -1);
            v[y2+1].emplace_back(x1+1, -1);
            v[y2+1].emplace_back(x2+1, 1);
        }
        vector<vector<int>> sum(2, vector<int>(max_x));
        int S = 0, len = 0;
        for(int i=0; i<max_y; ++i) {
            int now = i % 2, pre = (i+1) % 2;
            fill(sum[now].begin(), sum[now].end(), 0);
            for(int j=0; j<v[i].size(); ++j) {
                sum[now][v[i][j].first] += v[i][j].second;
            }
            for(int j=0; j<max_x - 1; ++j) {
                sum[now][j+1] += sum[now][j];
                sum[now][j] += sum[pre][j];
            }
            for(int j=0; j<max_x - 1; ++j) {
                S += sum[now][j] > 0;
                len += ((sum[pre][j] > 0) != (sum[now][j] > 0));
                len += ((sum[now][j] > 0) != (sum[now][j+1] > 0));
            }
        }
        
        if(r == 1) {
            cout << S << endl;
        } else {
            cout << S << '\n' << len << endl;
        }
    }
}
accuracy: 1
time_ms: 3230
memory_kb: 3604
final_rank: 14

code_snippet: #include <iostream>
#include <vector>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;

using P = pair<int, int>;

constexpr int MAX_Y = 10003;
constexpr int MAX_X = 10003;

int main() {
#if READ_FROM_FILE
    std::ifstream ifs("test1.txt");
    cin.rdbuf(ifs.rdbuf());
#endif
    int n, r;
    while(cin >> n >> r, n) {
        vector<vector<P>> v(MAX_Y);
        for(int i=0; i<n; ++i) {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            v[y1+1].emplace_back(x1+1, 1);
            v[y1+1].emplace_back(x2+1, -1);
            v[y2+1].emplace_back(x1+1, -1);
            v[y2+1].emplace_back(x2+1, 1);
        }
        vector<vector<int>> sum(2, vector<int>(MAX_X));
        int S = 0, len = 0;
        for(int i=0; i<MAX_Y; ++i) {
            int now = i % 2, pre = (i+1) % 2;
            fill(sum[now].begin(), sum[now].end(), 0);
            for(int j=0; j<v[i].size(); ++j) {
                sum[now][v[i][j].first] += v[i][j].second;
            }
            for(int j=0; j<MAX_X - 1; ++j) {
                sum[now][j+1] += sum[now][j];
                sum[now][j] += sum[pre][j];
            }
            for(int j=0; j<MAX_X - 1; ++j) {
                S += sum[now][j] > 0;
                len += ((sum[pre][j] > 0) != (sum[now][j] > 0));
                len += ((sum[now][j] > 0) != (sum[now][j+1] > 0));
            }
        }
        
        if(r == 1) {
            cout << S << endl;
        } else {
            cout << S << '\n' << len << endl;
        }
    }
}
accuracy: 1
time_ms: 3220
memory_kb: 3696
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;

constexpr int MAX_Y = 10003;
constexpr int MAX_X = 10003;

int main() {
    int n, r;
    while(cin >> n >> r, n) {
        vector<vector<P>> v(MAX_Y);
        for(int i=0; i<n; ++i) {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            v[y1+1].emplace_back(x1+1, 1);
            v[y1+1].emplace_back(x2+1, -1);
            v[y2+1].emplace_back(x1+1, -1);
            v[y2+1].emplace_back(x2+1, 1);
        }
        vector<vector<int>> sum(2, vector<int>(MAX_X));
        int S = 0, len = 0;
        for(int i=0; i<MAX_Y; ++i) {
            int now = i % 2, pre = (i+1) % 2;
            fill(sum[now].begin(), sum[now].end(), 0);
            for(int j=0; j<v[i].size(); ++j) {
                sum[now][v[i][j].first] += v[i][j].second;
            }
            for(int j=0; j<MAX_X; ++j) {
                sum[now][j+1] += sum[now][j];
                sum[now][j] += sum[pre][j];
            }
            for(int j=0; j<MAX_X; ++j) {
                S += sum[now][j] > 0;
                len += ((sum[pre][j] > 0) != (sum[now][j] > 0));
                len += ((sum[now][j] > 0) != (sum[now][j+1] > 0));
            }
        }
        
        if(r == 1) {
            cout << S << endl;
        } else {
            cout << S << '\n' << len << endl;
        }
    }
}
accuracy: 1
time_ms: 3220
memory_kb: 3536
final_rank: 7

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

int main() {
    int n, r;
    while(cin >> n >> r, n){
        vector<vector<tuple<int, int, int>>> que(10010);
        for (int i = 0; i < n; ++i) {
            int a, b, c, d;
            cin >> a >> b >> c >> d;
            que[a].emplace_back(b, d, 1);
            que[c].emplace_back(b, d, -1);
        }
        vector<int> val(10010), prv(10010);
        int round = 0, area = 0;
        for (int i = 0; i < 10010; ++i) {
            copy(val.begin(),val.end(), prv.begin());
            for (auto [l, r, d] : que[i]) {
                for (int j = l+1; j <= r; ++j) {
                    val[j] += d;
                }
            }
            for (int j = 0; j < 10010; ++j) {
                if(!prv[j] != !val[j]) round++;
            }
            for (int j = 1; j < 10010; ++j) {
                if(!val[j] != !(val[j-1])) round++;
                if(val[j]) area++;
            }
        }
        cout << area << "\n";
        if(r == 2){
            cout << round << "\n";
        }
    }

    return 0;
}
accuracy: 1
time_ms: 1910
memory_kb: 4084
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
constexpr int MAX_Y = 10003;
constexpr int MAX_X = 10003;
int main() {
    int n, r;
    while(cin >> n >> r, n) {
        vector<vector<P>> v(MAX_Y);
        for(int i=0; i<n; ++i) {
            int x1, y1, x2, y2;cin >> x1 >> y1 >> x2 >> y2;++x1; ++y1; ++x2; ++y2;
            v[y1].emplace_back(x1, 1);
            v[y1].emplace_back(x2, -1);
            v[y2].emplace_back(x1, -1);
            v[y2].emplace_back(x2, 1);
        }
        vector<vector<int>> sum(2, vector<int>(MAX_X));
        int S = 0, len = 0;
        for(int i=0; i<MAX_Y; ++i) {
            int now = i % 2, pre = (i+1) % 2;
            fill(sum[now].begin(), sum[now].end(), 0);
            for(int j=0; j<v[i].size(); ++j) {
                sum[now][v[i][j].first] += v[i][j].second;
            }
            for(int j=0; j<MAX_X; ++j) {
                sum[now][j+1] += sum[now][j];
                sum[now][j] += sum[pre][j];
            }
            for(int j=0; j<MAX_X; ++j) {
                S += sum[now][j] > 0;
                len += ((sum[pre][j] > 0) != (sum[now][j] > 0));
                len += ((sum[now][j] > 0) != (sum[now][j+1] > 0));
            }
        }
        cout << S << endl;
        if(r==2) cout << len << endl;
    }
}
accuracy: 1
time_ms: 3200
memory_kb: 3612
final_rank: 13

code_snippet: // imos kyoukai -> subtraction 
// MLEのため row by row 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll,ll> P;
using VP = vector<P>; using VVP = vector<VP>;
using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>;
const int inf=1e9+7;
const ll INF=1LL<<61;
const ll mod=1e9+7;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

int f[2][10101]; 
vector<P> p[10101];

int main(){
    int i,j;
    int n,q;
    while(1){
        cin>>n>>q;
        if(n==0) break;
        memset(f,0,sizeof(f));
        for(i=0;i<10101;i++) p[i].clear();
        int h=0,w=0;
        for(i=0;i<n;i++){
            int a,b,c,d;
            cin>>a>>b>>c>>d;
            a++;
            b++;
            c++;
            d++;
            p[b].pb(P(a,1));
            p[b].pb(P(c,-1));
            p[d].pb(P(a,-1));
            p[d].pb(P(c,1));
            h = max(h, d);
            w = max(w, c);
        }
        int area = 0;
        int length = 0;
        for(i=0;i<h+2;i++){
            memset(f[1], 0, sizeof(f[1]));
            for(P u:p[i]) f[1][u.fi]+=u.se;
            for(j=0;j<w+1;j++){
                f[1][j+1]+=f[1][j];
            }
            for(j=0;j<w+2;j++){
                f[1][j]+=f[0][j];
            }
            for(j=0;j<w+2;j++){
                if(f[1][j]) area++;
                //if(f[1][j]) cout<<i<<" "<<j<<endl;
                if((f[0][j] > 0) != (f[1][j] > 0)) length++;
                if((f[1][j] > 0) != (f[1][j+1] > 0)) length++;
            }
            swap(f[0],f[1]);
        }
        cout<<area<<endl;
        if(q==2) cout<<length<<endl;
    }

}
accuracy: 1
time_ms: 800
memory_kb: 4212
final_rank: 2

code_snippet: #include <bits/stdc++.h>
 
//    #include <boost/multiprecision/cpp_int.hpp>
 #define int long long
 #define inf  1000000007
 #define pa pair<int,int>
 #define ll long long
 #define pal pair<double,double>
 #define ppap pair<pa,int>
  #define PI 3.14159265358979323846
  #define paa pair<int,char>
  #define  mp make_pair
  #define  pb push_back
  #define EPS (1e-8)
                                          
    int dx[8]={0,1,0,-1,1,1,-1,-1};
    int dy[8]={1,0,-1,0,-1,1,1,-1};
                                            using namespace std;
                                   			class pa3{
                                            	public:
                                            	int x;
                                   				int y,z;
                                            	pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {}
                                            	bool operator < (const pa3 &p) const{
                                            		if(x!=p.x) return x<p.x;
                                            		if(y!=p.y) return y<p.y;
                                            		 return z<p.z;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                   				bool operator > (const pa3 &p) const{
                                            		if(x!=p.x) return x>p.x;
                                            		if(y!=p.y) return y>p.y;
                                            		 return z>p.z;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa3 &p) const{
                                            		return x==p.x && y==p.y && z==p.z;
                                            	}
                                            		bool operator != (const pa3 &p) const{
                                            			return !( x==p.x && y==p.y && z==p.z);
                                            	}
                                            
                                            };
                                            
                                            class pa4{
                                            	public:
                                            	int x;
                                            	int y,z,w;
                                            	pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {}
                                            	bool operator < (const pa4 &p) const{
                                            		if(x!=p.x) return x<p.x;
                                            		if(y!=p.y) return y<p.y;
                                            		if(z!=p.z)return z<p.z;
                                            		return w<p.w;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator > (const pa4 &p) const{
                                            		if(x!=p.x) return x>p.x;
                                            		if(y!=p.y) return y>p.y;
                                            		if(z!=p.z)return z>p.z;
                                            		return w>p.w;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa4 &p) const{
                                            		return x==p.x && y==p.y && z==p.z &&w==p.w;
                                            	}
                                            		
                                            
                                            };
                                            class pa2{
                                            	public:
                                            	int x,y;
                                            	pa2(int x=0,int y=0):x(x),y(y) {}
                                            	pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}
                                            	pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);}
                                            	bool operator < (const pa2 &p) const{
                                            		return y != p.y ? y<p.y: x<p.x;
                                            	}
                                            	bool operator > (const pa2 &p) const{
                                            		return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa2 &p) const{
                                            		return abs(x-p.x)==0 && abs(y-p.y)==0;
                                            	}
                                            	bool operator != (const pa2 &p) const{
                                            		return !(abs(x-p.x)==0 && abs(y-p.y)==0);
                                            	}
                                            		
                                            
                                            };
                                            
 
                      
                                string itos( int i ) {
                                ostringstream s ;
                                s << i ;
                                return s.str() ;
                                }
                                 
                                int gcd(int v,int b){
                                	if(v==0) return b;
                                	if(b==0) return v;
                                	if(v>b) return gcd(b,v);
                                	if(v==b) return b;
                                	if(b%v==0) return v;
                                	return gcd(v,b%v);
                                }
                 
                            
                                int mod;
int extgcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int d = extgcd(b, a%b, y, x);
    y -= a/b * x;
    return d;
}
pa operator+(const pa & l,const pa & r) {   
    return {l.first+r.first,l.second+r.second};                                    
}    
pa operator-(const pa & l,const pa & r) {   
    return {l.first-r.first,l.second-r.second};                                    
}  
                                
                int pr[10000100];
                int inv[10000010];
                
                int beki(int wa,int rr,int warukazu){
                	if(rr==0) return 1%warukazu;
                	if(rr==1) return wa%warukazu;
                	wa%=warukazu;
                	if(rr%2==1) return ((ll)beki(wa,rr-1,warukazu)*(ll)wa)%warukazu;
                	ll zx=beki(wa,rr/2,warukazu);
                	return (zx*zx)%warukazu;
                }
 
                
    			int comb(int nn,int rr){
    				if(rr<0 || rr>nn || nn<0) return 0;
    				int r=pr[nn]*inv[rr];
    				r%=mod;
    				r*=inv[nn-rr];
    				r%=mod;
    				return r;
    			}
               
                void gya(int ert){
                	pr[0]=1;
                	for(int i=1;i<=ert;i++){
                		pr[i]=(pr[i-1]*i)%mod;
                	}
                		inv[ert]=beki(pr[ert],mod-2,mod);
                	for(int i=ert-1;i>=0;i--){
                		inv[i]=inv[i+1]*(i+1)%mod;
                	}
                }
                
              //   cin.tie(0);
    		//	ios::sync_with_stdio(false);
    			//priority_queue<pa3,vector<pa3>,greater<pa3>> pq;            
                 //sort(ve.begin(),ve.end(),greater<int>());
 //   mt19937(clock_per_sec);
  //  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ;
      

vector<pa> ve[10010];

int mae[10020];
int dp[10022];
signed main(){
	
	
   cin.tie(0);
	ios::sync_with_stdio(false);
while(1){

	
	int n,P;
	cin>>n>>P;
if(n+P==0)exit(0);
for(int i=0;i<10010;i++)ve[i].clear();
	for(int i=0;i<n;i++){
int x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		x1++,y1++,x2++,y2++;
		ve[x1].pb(mp(y1,1));
		ve[x2].pb(mp(y1,-1));
		ve[x1].pb(mp(y2,-1));
		ve[x2].pb(mp(y2,1));
		
		
	}
	int cnt=0;
	int sa=0;
	for(int i=1;i<=10005;i++){
		for(int j=1;j<=10005;j++)dp[j]=0;
		for(auto v:ve[i])dp[v.first]+=v.second;
		for(int j=1;j<=10005;j++){
			dp[j]=dp[j]+mae[j]+dp[j-1]-mae[j-1];
			if(dp[j]>0)cnt++;
			if(dp[j]>0 && mae[j]==0)sa++;
			if(dp[j]==0 && mae[j]>0)sa++;
			if(dp[j]>0 && dp[j-1]==0)sa++;
			if(dp[j]==0 && dp[j-1]>0)sa++;
			
		}
		
		for(int j=1;j<=10005;j++)mae[j]=dp[j];
		
	}
	cout<<cnt<<endl;
	if(P==2)cout<<sa<<endl;
}
	return 0;
	

 
 }
accuracy: 1
time_ms: 2370
memory_kb: 4516
final_rank: 20

code_snippet: #include <stdio.h>
#include <vector>
#define MAX (10000+2)
using namespace std;
int main(){
    int n, r, x1, x2, y1, y2;
    while(true){
        scanf("%d %d", &n, &r);
        if(n==0 && r==0) break;
        int plane[2][MAX]={0};
        vector<vector<pair<int, int> > > sheets(MAX);
        for(int i=0; i<n; i++){
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            sheets[x1+1].push_back(make_pair(y1+1,+1));
            sheets[x1+1].push_back(make_pair(y2+1,-1));
            sheets[x2+1].push_back(make_pair(y1+1,-1));
            sheets[x2+1].push_back(make_pair(y2+1,+1));
        }
        if(r==2){
            int area=0, length=0;
            for(int x=1; x<MAX; x++){
                for(int i=0; i<MAX; i++){
                    plane[x%2][i]=0;
                }
                for(vector<pair<int, int> >::iterator sheet=sheets[x].begin(); sheet!=sheets[x].end(); sheet++){
                    plane[x%2][(*sheet).first]+=(*sheet).second;
                }
                for(int y=1; y<MAX; y++){
                    plane[x%2][y]+=plane[x%2][y-1];
                }
                for(int y=1; y<MAX; y++){
                    plane[x%2][y]+=plane[(x+1)%2][y];
                    area+=plane[x%2][y]>0 ? 1 : 0;
                    length+=(plane[x%2][y-1]>0)!=(plane[x%2][y]>0) ? 1 : 0;
                    length+=(plane[x%2][y]>0)!=(plane[(x+1)%2][y]>0) ? 1 : 0;
                }
            }
            printf("%d\n%d\n", area, length);
        }else{
            int area=0;
            for(int x=1; x<MAX; x++){
                for(int i=0; i<MAX; i++){
                    plane[x%2][i]=0;
                }
                for(vector<pair<int, int> >::iterator sheet=sheets[x].begin(); sheet!=sheets[x].end(); sheet++){
                    plane[x%2][(*sheet).first]+=(*sheet).second;
                }
                for(int y=1; y<MAX; y++){
                    plane[x%2][y]+=plane[x%2][y-1];
                }
                for(int y=1; y<MAX; y++){
                    plane[x%2][y]+=plane[(x+1)%2][y];
                    area+=plane[x%2][y]>0 ? 1 : 0;
                }
            }
            printf("%d\n", area);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 3230
memory_kb: 3176
final_rank: 3

code_snippet: #include <stdio.h>
#include <vector>
#define MAX (10000+2)
using namespace std;
int main(){
    int n, r, x1, x2, y1, y2;
    while(true){
        scanf("%d %d", &n, &r);
        if(n==0 && r==0) break;
        int plane[2][MAX]={0};
        vector<vector<pair<int, int> > > sheets(MAX);
        for(int i=0; i<n; i++){
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            sheets[x1+1].push_back(make_pair(y1+1,+1));
            sheets[x1+1].push_back(make_pair(y2+1,-1));
            sheets[x2+1].push_back(make_pair(y1+1,-1));
            sheets[x2+1].push_back(make_pair(y2+1,+1));
        }
        int area=0, length=0;
        for(int x=1; x<MAX; x++){
            for(int i=0; i<MAX; i++){
                plane[x%2][i]=0;
            }
            for(vector<pair<int, int> >::iterator sheet=sheets[x].begin(); sheet!=sheets[x].end(); sheet++){
                plane[x%2][(*sheet).first]+=(*sheet).second;
            }
            for(int y=1; y<MAX; y++){
                plane[x%2][y]+=plane[x%2][y-1];
            }
            for(int y=1; y<MAX; y++){
                plane[x%2][y]+=plane[(x+1)%2][y];
                area+=plane[x%2][y]>0 ? 1 : 0;
                length+=(plane[x%2][y-1]>0)!=(plane[x%2][y]>0) ? 1 : 0;
                length+=(plane[x%2][y]>0)!=(plane[(x+1)%2][y]>0) ? 1 : 0;
            }
        }
        printf("%d\n", area);
        if(r==2){
            printf("%d\n", length);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 3720
memory_kb: 3292
final_rank: 5