Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- namespace solve1 {
- int Kn, Km;
- string bin(int x, int y) {
- string w = "";
- for (int i = 0; i < Kn; i++) {
- w += (char)(x % 2 + '0');
- x >>= 1;
- }
- for (int i = 0; i < Km; i++) {
- w += (char)(y % 2 + '0');
- y >>= 1;
- }
- return w;
- }
- pair<int, int> nbin(const vector<char>& s, int& i) {
- int x = 0, y = 0;
- for (int j = i + Km + Kn - 1; j >= i + Kn; j--)
- y = 2 * y + (s[j] - '0');
- for (int j = i + Kn - 1; j >= i; j--)
- x = 2 * x + (s[j] - '0');
- i += Kn + Km;
- return { x, y };
- }
- int sum(vector<vector<int>>& pref, int x, int y, int x2, int y2) {
- if (x == 0 && y == 0) {
- return pref[x2][y2];
- }
- if (x == 0) {
- return pref[x2][y2] - pref[x2][y - 1];
- }
- if (y == 0) {
- return pref[x2][y2] - pref[x - 1][y2];
- }
- return pref[x2][y2] - pref[x - 1][y2] - pref[x2][y - 1] + pref[x - 1][y - 1];
- }
- string insert(vector<vector<char>>& a, vector<vector<int>>& pref, int x, int y) {
- int x2 = x, y2 = y;
- x2++;
- while (x2 < a.size() && sum(pref, x, y, x2, y2) == (x2 - x + 1) * (y2 - y + 1)) {
- ++x2;
- }
- --x2;
- y2++;
- while (y2 < a[0].size() && sum(pref, x, y, x2, y2) == (x2 - x + 1) * (y2 - y + 1)) {
- ++y2;
- }
- --y2;
- string an = "";
- for (int i = x; i <= x2; ++i) {
- for (int j = y; j <= y2; ++j) {
- a[i][j] = '2';
- }
- }
- an = bin(x, y) + bin(x2, y2);
- return an;
- }
- string decode(vector<vector<char>>& a) {
- string ans = "";
- int n = a.size();
- int m = a[0].size();
- vector<vector<int>> pref_sum(n, vector<int>(m));
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- pref_sum[i][j] = a[i][j] - '0';
- if (0 < i && 0 < j) {
- pref_sum[i][j] += pref_sum[i - 1][j] + pref_sum[i][j - 1] - pref_sum[i - 1][j - 1];
- }
- if (0 < i && j == 0) {
- pref_sum[i][j] += pref_sum[i - 1][j];
- }
- if (i == 0 && 0 < j) {
- pref_sum[i][j] += pref_sum[i][j - 1];
- }
- }
- }
- for (int j = 0; j < m; ++j) {
- for (int i = 0; i < n; ++i) {
- if (a[i][j] == '1') {
- ans += insert(a, pref_sum, i, j);
- }
- }
- }
- return ans;
- }
- void pol(vector<vector<char>>& a, int x, int y, int x2, int y2) {
- for (int i = x; i <= x2; ++i) {
- for (int j = y; j <= y2; ++j) {
- a[i][j] = '1';
- }
- }
- }
- string main(string t) {
- if (t == "encode") {
- int n, m, k;
- cin >> n >> m >> k;
- for (; (1 << Kn) < n; ++Kn);
- for (; (1 << Km) < m; ++Km);
- int pKn = Kn, pKm = Km;
- Kn = Km = 4;
- cout << bin(pKn, pKm);
- Kn = pKn; Km = pKm;
- cout << bin(n, m);
- vector<vector<char>> a(n, vector<char>(m));
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cin >> a[i][j];
- }
- }
- return "0" + decode(a);
- } else {
- vector<char> a;
- char c;
- while (cin >> c) {
- a.push_back(c);
- }
- Kn = Km = 4;
- int p = 0;
- pair<int, int> gg = nbin(a, p);
- Kn = gg.first; Km = gg.second;
- int n = 0, m = 0;
- pair<int, int> t = nbin(a, p);
- n = t.first;
- m = t.second;
- vector<vector<char>> ans(n, vector<char>(m, '0'));
- while (p < a.size()) {
- int x = 0, y = 0, x2 = 0, y2 = 0;
- pair<int, int> t2 = nbin(a, p);
- x = t2.first;
- y = t2.second;
- pair<int, int> t3 = nbin(a, p);
- x2 = t3.first;
- y2 = t3.second;
- pol(ans, x, y, x2, y2);
- }
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cout << ans[i][j];
- }
- cout << "\n";
- }
- return "";
- }
- }
- }
- namespace solve2 {
- string bin(int x) {
- string s;
- for (int i = 0; i < 10; ++i) {
- s += '0' + x % 2;
- x /= 2;
- }
- reverse(s.begin(), s.end());
- return s;
- }
- int sum(vector<vector<int>>& pref, int x, int y, int x2, int y2) {
- if (x == 0 && y == 0) {
- return pref[x2][y2];
- }
- if (x == 0) {
- return pref[x2][y2] - pref[x2][y - 1];
- }
- if (y == 0) {
- return pref[x2][y2] - pref[x - 1][y2];
- }
- return pref[x2][y2] - pref[x - 1][y2] - pref[x2][y - 1] + pref[x - 1][y - 1];
- }
- string insert(vector<vector<char>>& a, vector<vector<int>>& pref, int x, int y) {
- int x2 = x, y2 = y;
- x2++;
- while (x2 < a.size() && sum(pref, x, y, x2, y2) == (x2 - x + 1) * (y2 - y + 1)) {
- ++x2;
- }
- --x2;
- y2++;
- while (y2 < a[0].size() && sum(pref, x, y, x2, y2) == (x2 - x + 1) * (y2 - y + 1)) {
- ++y2;
- }
- --y2;
- string an = "";
- for (int i = x; i <= x2; ++i) {
- for (int j = y; j <= y2; ++j) {
- a[i][j] = '2';
- }
- }
- an = bin(x) + bin(y) + bin(x2) + bin(y2);
- return an;
- }
- string decode(vector<vector<char>>& a) {
- string ans = "";
- int n = a.size();
- int m = a[0].size();
- vector<vector<int>> pref_sum(n, vector<int>(m));
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- pref_sum[i][j] = a[i][j] - '0';
- if (0 < i && 0 < j) {
- pref_sum[i][j] += pref_sum[i - 1][j] + pref_sum[i][j - 1] - pref_sum[i - 1][j - 1];
- }
- if (0 < i && j == 0) {
- pref_sum[i][j] += pref_sum[i - 1][j];
- }
- if (i == 0 && 0 < j) {
- pref_sum[i][j] += pref_sum[i][j - 1];
- }
- }
- }
- for (int j = 0; j < m; ++j) {
- for (int i = 0; i < n; ++i) {
- if (a[i][j] == '1') {
- ans += insert(a, pref_sum, i, j);
- }
- }
- }
- return ans;
- }
- void pol(vector<vector<char>>& a, int x, int y, int x2, int y2) {
- for (int i = x; i <= x2; ++i) {
- for (int j = y; j <= y2; ++j) {
- a[i][j] = '1';
- }
- }
- }
- string main(string t) {
- if (t == "encode") {
- int n, m, k;
- cin >> n >> m >> k;
- cout << bin(n) << bin(m);
- vector<vector<char>> a(n, vector<char>(m));
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cin >> a[i][j];
- }
- }
- return "1" + decode(a);
- } else {
- vector<char> a;
- char c;
- while (cin >> c) {
- a.push_back(c);
- }
- int n = 0, m = 0;
- int p = 0;
- for (int i = 0; i < 10; ++i) n = n * 2 + (a[p++] - '0');
- for (int i = 0; i < 10; ++i) m = m * 2 + (a[p++] - '0');
- vector<vector<char>> ans(n, vector<char>(m, '0'));
- while (p < a.size()) {
- int x = 0, y = 0, x2 = 0, y2 = 0;
- for (int i = 0; i < 10; ++i) x = x * 2 + (a[p++] - '0');
- for (int i = 0; i < 10; ++i) y = y * 2 + (a[p++] - '0');
- for (int i = 0; i < 10; ++i) x2 = x2 * 2 + (a[p++] - '0');
- for (int i = 0; i < 10; ++i) y2 = y2 * 2 + (a[p++] - '0');
- pol(ans, x, y, x2, y2);
- }
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cout << ans[i][j];
- }
- cout << "\n";
- }
- return "";
- }
- }
- }
- int main() {
- string t; cin >> t;
- if (t == "encode") {
- auto s1 = solve1::main(t);
- auto s2 = solve2::main(t);
- if (s1.size() << s2.size()) {
- cout << s1;
- } else {
- cout << s2;
- }
- } else {
- char c; cin >> c;
- if (c == '0') {
- solve1::main(t);
- } else {
- solve2::main(t);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement