problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p03050 | C++ | Runtime Error | #include <bits/stdc++.h>
#ifdef HIRO
#define d(f_, ...) printf((f_), ##__VA_ARGS__)
#else
#define d(f_, ...)
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pllll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vllll;
typedef map<int, int> mii;
typedef map<long long, long long> mllll;
typedef queue<int> qi;
typedef queue<pair<int, int>> qp;
#define mp make_pair
#define pi 3.14159265358979323846
#define MAX max_element
#define MIN min_element
#define all(v) v.begin(), v.end()
#define gt(T) greater<T>()
#define sum(v) accumulate(v.begin(), v.end(), 0ll)
#define uni(v) \
do { \
sort(v.begin(), v.end()); \
decltype(v)::iterator result = unique(v.begin(), v.end()); \
v.erase(result, v.end()); \
} while (0)
#define fi(init, n) for (int i = init; i < n; ++i)
#define fj(init, n) for (int j = init; j < n; ++j)
#define fk(init, n) for (int k = init; k < n; ++k)
#define zero(x) memset(x, 0, sizeof(x))
#define negative(x) memset(x, ~0, sizeof(x))
#define bitsum(x) __builtin_popcount(x)
const ll MOD = 1e9 + 7;
const int INF = 1e9;
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T> int finder(vector<T> vec, T number) {
auto itr = find(vec.begin(), vec.end(), number);
size_t index = distance(vec.begin(), itr);
if (index != vec.size())
return 1;
else
return 0;
}
ll frac(ll n) {
if (n == 0)
return 1;
return (n * frac(n - 1)) % MOD;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main(int argc, char **argv) {
#ifdef HIRO
string quiz(argv[1]);
string id(argv[2]);
string filename("input/" + quiz + "/" + id + ".txt");
ifstream fin(filename);
if (fin.peek() == ifstream::traits_type::eof()) {
cout << "No input.";
return 0;
}
ifstream cin(filename);
std::cin.rdbuf(cin.rdbuf());
#endif
ll ans = 0;
ll n, m;
cin >> n;
m = n;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i)
continue;
ll x = n / i - 1;
if (n / x == n % x)
ans += n / i - 1;
// int y = i - 1;
// if(y > 0 && n / y == n % y)
// ans += y;
}
// unordered_set<int> st;
// ans += n - 1;
// while(n % 2 == 0) {
// if(st.count(2) == 0) {
// ans += m / 2 - 1;
// st.insert(2);
// }
// n /= 2;
// }
// for(int i = 3; i <= sqrt(n); i += 2) {
// while(n % i == 0) {
// if(st.count(i == 0)) {
// ans += m / i - 1;
// st.insert(i);
// }
// n /= i;
// }
// }
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#ifdef HIRO
#define d(f_, ...) printf((f_), ##__VA_ARGS__)
#else
#define d(f_, ...)
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pllll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vllll;
typedef map<int, int> mii;
typedef map<long long, long long> mllll;
typedef queue<int> qi;
typedef queue<pair<int, int>> qp;
#define mp make_pair
#define pi 3.14159265358979323846
#define MAX max_element
#define MIN min_element
#define all(v) v.begin(), v.end()
#define gt(T) greater<T>()
#define sum(v) accumulate(v.begin(), v.end(), 0ll)
#define uni(v) \
do { \
sort(v.begin(), v.end()); \
decltype(v)::iterator result = unique(v.begin(), v.end()); \
v.erase(result, v.end()); \
} while (0)
#define fi(init, n) for (int i = init; i < n; ++i)
#define fj(init, n) for (int j = init; j < n; ++j)
#define fk(init, n) for (int k = init; k < n; ++k)
#define zero(x) memset(x, 0, sizeof(x))
#define negative(x) memset(x, ~0, sizeof(x))
#define bitsum(x) __builtin_popcount(x)
const ll MOD = 1e9 + 7;
const int INF = 1e9;
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T> int finder(vector<T> vec, T number) {
auto itr = find(vec.begin(), vec.end(), number);
size_t index = distance(vec.begin(), itr);
if (index != vec.size())
return 1;
else
return 0;
}
ll frac(ll n) {
if (n == 0)
return 1;
return (n * frac(n - 1)) % MOD;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main(int argc, char **argv) {
#ifdef HIRO
string quiz(argv[1]);
string id(argv[2]);
string filename("input/" + quiz + "/" + id + ".txt");
ifstream fin(filename);
if (fin.peek() == ifstream::traits_type::eof()) {
cout << "No input.";
return 0;
}
ifstream cin(filename);
std::cin.rdbuf(cin.rdbuf());
#endif
ll ans = 0;
ll n, m;
cin >> n;
m = n;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i)
continue;
ll x = n / i - 1;
if (x > 0 && n / x == n % x)
ans += n / i - 1;
// int y = i - 1;
// if(y > 0 && n / y == n % y)
// ans += y;
}
// unordered_set<int> st;
// ans += n - 1;
// while(n % 2 == 0) {
// if(st.count(2) == 0) {
// ans += m / 2 - 1;
// st.insert(2);
// }
// n /= 2;
// }
// for(int i = 3; i <= sqrt(n); i += 2) {
// while(n % i == 0) {
// if(st.count(i == 0)) {
// ans += m / i - 1;
// st.insert(i);
// }
// n /= i;
// }
// }
cout << ans << endl;
return 0;
}
| replace | 95 | 96 | 95 | 96 | 0 | |
p03050 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
long n;
cin >> n;
long res = 0;
for (long i = 1; i * i <= n; ++i) {
if ((n - i) % i > 0)
continue;
auto m = (n - i) / i;
if (n / m == n % m)
res += m;
}
cout << res << endl;
}
| #include <algorithm>
#include <iostream>
using namespace std;
int main() {
long n;
cin >> n;
long res = 0;
for (long i = 1; i * i <= n; ++i) {
if ((n - i) % i > 0)
continue;
auto m = (n - i) / i;
if (m > 0 and n / m == n % m)
res += m;
}
cout << res << endl;
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p03050 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(v) v.begin(), v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) \
do { \
sort(v.begin(), v.end()); \
v.erase(unique(v.begin(), v.end()), v.end()); \
} while (0)
#define y0 y3487465
#define y1 y8687969
#define m0(x) memset(x, 0, sizeof(x))
#define m1(x) memset(x, 63, sizeof(x))
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
double EPS = 1e-9;
int INFi = 1000000005;
long long INFll = 1000000000000000005ll;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
ll MOD = 1000000007;
ll N;
int main() {
cin >> N;
ll ans = 0;
for (ll i = 1; i * i <= N; i++) {
if (N % i == 0) {
ll m = N / i - 1;
if (N / m == i) {
ans += m;
}
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(v) v.begin(), v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) \
do { \
sort(v.begin(), v.end()); \
v.erase(unique(v.begin(), v.end()), v.end()); \
} while (0)
#define y0 y3487465
#define y1 y8687969
#define m0(x) memset(x, 0, sizeof(x))
#define m1(x) memset(x, 63, sizeof(x))
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
double EPS = 1e-9;
int INFi = 1000000005;
long long INFll = 1000000000000000005ll;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
ll MOD = 1000000007;
ll N;
int main() {
cin >> N;
ll ans = 0;
for (ll i = 1; i * i <= N; i++) {
if (N % i == 0) {
ll m = N / i - 1;
if (m != 0 && N / m == i) {
ans += m;
}
}
}
cout << ans << endl;
return 0;
}
| replace | 60 | 61 | 60 | 61 | 0 | |
p03050 | C++ | Runtime Error | #include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
int main(void) {
ll n, m, ans = 0;
cin >> n;
for (ll k = 1; k < sqrt(n) + 1; k++) {
if (n % k == 0) {
m = n / k - 1;
if (n % m == k)
ans += m;
}
}
cout << ans << endl;
return 0;
} | #include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
int main(void) {
ll n, m, ans = 0;
cin >> n;
for (ll k = 1; k < sqrt(n) + 1; k++) {
if (n % k == 0) {
m = n / k - 1;
if (m > 0 && n % m == k)
ans += m;
}
}
cout << ans << endl;
return 0;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p03050 | C++ | Runtime Error | #pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()
#pragma region UnionFind
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma endregion
int main() {
ll n;
cin >> n;
vector<ll> div;
for (ll i = 2; i * i <= n; ++i) {
if (n % i != 0)
continue;
div.push_back(i);
if (n / i != i)
div.push_back(n / i);
}
div.push_back(n);
ll res = 0;
for (ll m : div) {
--m;
if (n / m == n % m)
res += m;
}
cout << res << endl;
} | #pragma region
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s)-1; (i) >= 0; --(i))
#define all(x) x.begin(), x.end()
#pragma region UnionFind
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#pragma endregion
#pragma region chmin
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma region chmax
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma endregion
int main() {
ll n;
cin >> n;
vector<ll> div;
for (ll i = 2; i * i <= n; ++i) {
if (n % i != 0)
continue;
div.push_back(i);
if (n / i != i)
div.push_back(n / i);
}
if (n != 1)
div.push_back(n);
ll res = 0;
for (ll m : div) {
--m;
if (n / m == n % m)
res += m;
}
cout << res << endl;
} | replace | 96 | 97 | 96 | 98 | 0 | |
p03050 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
using namespace std;
using Graph = vector<vector<int>>;
signed main() {
int N;
cin >> N;
int ans = 0;
for (int i = 1; i <= sqrt(N); i++) {
if (N % i == 0) {
int A = N / i - 1;
int B = N / A;
if (B == N % A)
ans += A;
if (i == 1 || i == N / i)
continue;
A = i - 1;
B = N / A;
if (B == N % A)
ans += A;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
using Graph = vector<vector<int>>;
signed main() {
int N;
cin >> N;
int ans = 0;
for (int i = 1; i <= sqrt(N); i++) {
if (N % i == 0) {
int A = N / i - 1;
if (A == 0)
continue;
int B = N / A;
if (B == N % A)
ans += A;
if (i == 1 || i == N / i)
continue;
A = i - 1;
B = N / A;
if (B == N % A)
ans += A;
}
}
cout << ans << endl;
}
| insert | 14 | 14 | 14 | 16 | 0 | |
p03050 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define el endl
#define INF 1e8
typedef long long ll;
typedef long double ld;
int main() {
ll n;
cin >> n;
ll ans = 0;
for (ll k = 1; k * k <= n; k++) {
if (n % k == 0) {
ll m = (n / k) - 1LL;
if (n / m == n % m)
ans += m;
if (k > 1) {
m = k - 1;
if (n / m == n % m)
ans += m;
}
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define el endl
#define INF 1e8
typedef long long ll;
typedef long double ld;
int main() {
ll n;
cin >> n;
ll ans = 0;
for (ll k = 1; k * k < n; k++) {
if (n % k == 0) {
ll m = (n / k) - 1LL;
if (n / m == n % m)
ans += m;
if (k > 1) {
m = k - 1;
if (n / m == n % m)
ans += m;
}
}
}
cout << ans << endl;
}
| replace | 15 | 16 | 15 | 16 | 0 | |
p03050 | C++ | Runtime Error | #include <iostream>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(0);
ll N;
cin >> N;
ll odp = 0;
for (ll d1 = 1; d1 * d1 <= N; ++d1) {
if (N % d1 == 0) {
ll d2 = N / d1 - 1;
if (N / d2 == N % d2)
odp += d2;
if (d1 * d1 != N)
if (d1 != 1 && N / (d1 - 1) == N % (d1 - 1))
odp += d1 - 1;
}
}
cout << odp;
return 0;
}
| #include <iostream>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(0);
ll N;
cin >> N;
ll odp = 0;
for (ll d1 = 1; d1 * d1 <= N; ++d1) {
if (N % d1 == 0) {
ll d2 = N / d1 - 1;
if (d2 != 0 && N / d2 == N % d2)
odp += d2;
if (d1 * d1 != N)
if (d1 != 1 && N / (d1 - 1) == N % (d1 - 1))
odp += d1 - 1;
}
}
cout << odp;
return 0;
}
| replace | 14 | 15 | 14 | 15 | 0 | |
p03050 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
long long ones[2000001];
int now;
void it(long long x) {
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) {
if (i * i != x)
ones[now++] = i, ones[now++] = x / i;
else
ones[now++] = i;
}
}
}
long long cnt;
int main() {
long long n;
cin >> n;
it(n);
ones[now++] = n;
for (int i = 0; i < now; i++)
if (n % (ones[i] - 1) == n / (ones[i] - 1))
cnt += ones[i] - 1;
cout << cnt << endl;
} | #include <bits/stdc++.h>
using namespace std;
long long ones[2000001];
int now;
void it(long long x) {
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) {
if (i * i != x)
ones[now++] = i, ones[now++] = x / i;
else
ones[now++] = i;
}
}
}
long long cnt;
int main() {
long long n;
cin >> n;
it(n);
ones[now++] = n;
for (int i = 0; i < now; i++)
if (ones[i] - 1 != 0 && n % (ones[i] - 1) == n / (ones[i] - 1))
cnt += ones[i] - 1;
cout << cnt << endl;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p03050 | Python | Runtime Error | import math
N = int(input())
ans = 0
for i in range(1, math.ceil(math.sqrt(N)) + 1):
if N % i:
continue
n = N // i - 1
if N % n == N // n:
ans += n
print(ans)
| import math
N = int(input())
ans = 0
for i in range(1, math.ceil(math.sqrt(N)) + 1):
if N % i:
continue
n = N // i - 1
if n and N % n == N // n:
ans += n
print(ans)
| replace | 8 | 9 | 8 | 9 | 0 | |
p03051 | C++ | Runtime Error | #include <bits/stdc++.h>
// #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #include "boost/multiprecision/cpp_int.hpp"
// typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
ll c = a % i_7;
if (c >= 0)
return c;
return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
if (a < b)
return b;
else
return a;
}
ll min(ll a, ll b) {
if (a > b)
return b;
else
return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define fi first
#define se second
#define endl "\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
// template<class T>void max(T a,T b){if(a<b)return b;else return a;}
// template<class T>void min(T a,T b){if(a>b)return b;else return a;}
// template<class T>bool Max(T&a, T b){if(a < b){a = b;return 1;}return 0;}
// template<class T>bool Min(T&a, T b){if(a > b){a = b;return 1;}return 0;}
//////////////////////////
ll po(ll i, ll p) {
if (p == 0)
return 1;
else {
i = mod(i);
if (p == 1)
return i;
if (p % 2 == 0)
return po(mod(i * i), p / 2);
return mod(i * po(i, p - 1));
}
}
ll bunbo(ll n) { return po(n, i_5); }
// #define N (1<<20)
#define N 16
int main() {
fastio ll n;
cin >> n;
ll a[n];
rep(i, 0, n - 1) cin >> a[i];
ll s[n];
s[0] = a[0];
rep(i, 1, n - 1) s[i] = s[i - 1] ^ a[i];
ll dp[N][2];
memset(dp, 0, sizeof(dp));
rep(i, 1, N - 1) dp[i][0] = 1;
ll cntsum = 0;
ll cnt[N];
memset(cnt, 0, sizeof(cnt));
rep(i, 0, n - 1) {
if (s[i] == 0) {
/*
rep(x,1,N-1){
if(i<=last[x]){
dp[x][0]+=dp[x][1];
}
}*/
/*
rep(x,1,N-1){
cnt[x]++;
}*/
cntsum++;
} else {
ll x = s[i];
// dp[x][1]+=dp[x][0];
/*
dp[x][0]+=cnt[x]*dp[x][1];
cnt[x]=0;
dp[x][1]+=dp[x][0];*/
Add(dp[x][0], (cntsum - cnt[x]) * dp[x][1]);
cnt[x] = cntsum;
Add(dp[x][1], dp[x][0]);
}
}
// rep(i,0,n-1)cout<<s[i]<<" ";cout<<endl;
// rep(i,1,N-1)cout<<i<<":"<<dp[i][0]<<" "<<dp[i][1]<<endl;
if (s[n - 1] == 0) {
/*
ll last[N];memset(last,-1,sizeof(last));
rep(i,0,n-1){
last[s[i]]=i;
}*/
ll ans = po(2, cntsum - 1);
rep(i, 1, N - 1) { Add(ans, dp[i][1]); }
cout << mod(ans) << endl;
} else {
cout << mod(dp[s[n - 1]][0]) << endl;
}
return 0;
}
| #include <bits/stdc++.h>
// #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #include "boost/multiprecision/cpp_int.hpp"
// typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
ll c = a % i_7;
if (c >= 0)
return c;
return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
if (a < b)
return b;
else
return a;
}
ll min(ll a, ll b) {
if (a > b)
return b;
else
return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define fi first
#define se second
#define endl "\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
// template<class T>void max(T a,T b){if(a<b)return b;else return a;}
// template<class T>void min(T a,T b){if(a>b)return b;else return a;}
// template<class T>bool Max(T&a, T b){if(a < b){a = b;return 1;}return 0;}
// template<class T>bool Min(T&a, T b){if(a > b){a = b;return 1;}return 0;}
//////////////////////////
ll po(ll i, ll p) {
if (p == 0)
return 1;
else {
i = mod(i);
if (p == 1)
return i;
if (p % 2 == 0)
return po(mod(i * i), p / 2);
return mod(i * po(i, p - 1));
}
}
ll bunbo(ll n) { return po(n, i_5); }
#define N (1 << 20)
// #define N 16
int main() {
fastio ll n;
cin >> n;
ll a[n];
rep(i, 0, n - 1) cin >> a[i];
ll s[n];
s[0] = a[0];
rep(i, 1, n - 1) s[i] = s[i - 1] ^ a[i];
ll dp[N][2];
memset(dp, 0, sizeof(dp));
rep(i, 1, N - 1) dp[i][0] = 1;
ll cntsum = 0;
ll cnt[N];
memset(cnt, 0, sizeof(cnt));
rep(i, 0, n - 1) {
if (s[i] == 0) {
/*
rep(x,1,N-1){
if(i<=last[x]){
dp[x][0]+=dp[x][1];
}
}*/
/*
rep(x,1,N-1){
cnt[x]++;
}*/
cntsum++;
} else {
ll x = s[i];
// dp[x][1]+=dp[x][0];
/*
dp[x][0]+=cnt[x]*dp[x][1];
cnt[x]=0;
dp[x][1]+=dp[x][0];*/
Add(dp[x][0], (cntsum - cnt[x]) * dp[x][1]);
cnt[x] = cntsum;
Add(dp[x][1], dp[x][0]);
}
}
// rep(i,0,n-1)cout<<s[i]<<" ";cout<<endl;
// rep(i,1,N-1)cout<<i<<":"<<dp[i][0]<<" "<<dp[i][1]<<endl;
if (s[n - 1] == 0) {
/*
ll last[N];memset(last,-1,sizeof(last));
rep(i,0,n-1){
last[s[i]]=i;
}*/
ll ans = po(2, cntsum - 1);
rep(i, 1, N - 1) { Add(ans, dp[i][1]); }
cout << mod(ans) << endl;
} else {
cout << mod(dp[s[n - 1]][0]) << endl;
}
return 0;
}
| replace | 70 | 72 | 70 | 72 | 0 | |
p03051 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int M = int(1e9 + 7);
signed main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i)
cin >> A[i];
vector<int> pre(N + 1);
{
for (int i = 0; i < N; ++i) {
pre[i + 1] = pre[i] ^ A[i];
}
if (pre[N]) {
while (1 << 17 <= N + 1)
;
static int dp[1 << 17][2][2][2];
dp[1][0][1][1] = 1;
for (int i = 1; i <= N; ++i) {
for (int c = 0; c < 2; ++c) {
if (pre[i] == 0) {
(dp[i + 1][c][1][1] += dp[i][c][0][0]) %= M;
(dp[i + 1][c][1][1] += dp[i][c][0][1]) %= M;
} else if (pre[i] == pre[N]) {
(dp[i + 1][c ^ 1][0][1] += dp[i][c][1][0]) %= M;
(dp[i + 1][c ^ 1][0][1] += dp[i][c][1][1]) %= M;
}
(dp[i + 1][c][0][0] += dp[i][c][0][0]) %= M;
(dp[i + 1][c][1][0] += dp[i][c][1][0]) %= M;
(dp[i + 1][c][0][0] += dp[i][c][0][1]) %= M;
(dp[i + 1][c][1][0] += dp[i][c][1][1]) %= M;
}
}
int ans = (dp[N + 1][0][0][1] + dp[N + 1][1][0][1]) % M;
cout << ans << endl;
return 0;
}
}
vector<int> pz(pre.size() + 1);
{
for (int i = 0; i < pre.size(); ++i) {
pz[i + 1] = pz[i] + (pre[i] == 0);
}
}
map<int, vector<int>> dp;
{
map<int, int> cnt;
map<int, int> last;
for (int i = 0; i < pre.size(); ++i) {
if (pre[i]) {
if (last.count(pre[i])) {
int l = last.count(pre[i]) ? last[pre[i]] : 0;
int cz = pz[i] - pz[l];
if (cz)
dp[pre[i]].push_back(cz);
}
++cnt[pre[i]];
last[pre[i]] = i;
} else {
for (auto it = cnt.begin(); it != cnt.end(); ++it) {
dp[it->first].push_back(it->second);
}
cnt.clear();
}
}
}
int ans = 1;
{
for (int i = 1; i + 1 < pre.size(); ++i) {
if (pre[i] == 0)
ans = 2LL * ans % M;
}
for (auto it = dp.begin(); it != dp.end(); ++it) {
auto s = it->second;
vector<int> dpdp(2);
dpdp[0] = 1;
for (int i = 0; i < s.size(); ++i) {
(dpdp[~i & 1] += 1LL * s[i] * dpdp[i & 1] % M) %= M;
}
(ans += dpdp[1]) %= M;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int M = int(1e9 + 7);
signed main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i)
cin >> A[i];
vector<int> pre(N + 1);
{
for (int i = 0; i < N; ++i) {
pre[i + 1] = pre[i] ^ A[i];
}
if (pre[N]) {
static int dp[1 << 19][2][2][2];
dp[1][0][1][1] = 1;
for (int i = 1; i <= N; ++i) {
for (int c = 0; c < 2; ++c) {
if (pre[i] == 0) {
(dp[i + 1][c][1][1] += dp[i][c][0][0]) %= M;
(dp[i + 1][c][1][1] += dp[i][c][0][1]) %= M;
} else if (pre[i] == pre[N]) {
(dp[i + 1][c ^ 1][0][1] += dp[i][c][1][0]) %= M;
(dp[i + 1][c ^ 1][0][1] += dp[i][c][1][1]) %= M;
}
(dp[i + 1][c][0][0] += dp[i][c][0][0]) %= M;
(dp[i + 1][c][1][0] += dp[i][c][1][0]) %= M;
(dp[i + 1][c][0][0] += dp[i][c][0][1]) %= M;
(dp[i + 1][c][1][0] += dp[i][c][1][1]) %= M;
}
}
int ans = (dp[N + 1][0][0][1] + dp[N + 1][1][0][1]) % M;
cout << ans << endl;
return 0;
}
}
vector<int> pz(pre.size() + 1);
{
for (int i = 0; i < pre.size(); ++i) {
pz[i + 1] = pz[i] + (pre[i] == 0);
}
}
map<int, vector<int>> dp;
{
map<int, int> cnt;
map<int, int> last;
for (int i = 0; i < pre.size(); ++i) {
if (pre[i]) {
if (last.count(pre[i])) {
int l = last.count(pre[i]) ? last[pre[i]] : 0;
int cz = pz[i] - pz[l];
if (cz)
dp[pre[i]].push_back(cz);
}
++cnt[pre[i]];
last[pre[i]] = i;
} else {
for (auto it = cnt.begin(); it != cnt.end(); ++it) {
dp[it->first].push_back(it->second);
}
cnt.clear();
}
}
}
int ans = 1;
{
for (int i = 1; i + 1 < pre.size(); ++i) {
if (pre[i] == 0)
ans = 2LL * ans % M;
}
for (auto it = dp.begin(); it != dp.end(); ++it) {
auto s = it->second;
vector<int> dpdp(2);
dpdp[0] = 1;
for (int i = 0; i < s.size(); ++i) {
(dpdp[~i & 1] += 1LL * s[i] * dpdp[i & 1] % M) %= M;
}
(ans += dpdp[1]) %= M;
}
}
cout << ans << endl;
return 0;
}
| replace | 21 | 24 | 21 | 22 | TLE | |
p03051 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() { cerr << '\n'; }
template <class FIRST, class... REST> void Debug(FIRST arg, REST... rest) {
cerr << arg << " ";
Debug(rest...);
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
out << "[";
if (!v.empty()) {
rep(i, 0, sz(v) - 1) out << v[i] << ", ";
out << v.back();
}
out << "]";
return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &v) {
out << "(" << v.first << ", " << v.second << ")";
return out;
}
const int MAX_N = 300010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng; // use it by rng() % mod, shuffle(all(vec), rng)
///////////////////////////////////////////////////////////////////////////////////////////////////
ll fac[MAX_N], inv[MAX_N], fiv[MAX_N]; // fiv:inv(fac(i))
ll mod_pow(ll a, ll n) {
if (n == 0)
return 1;
ll res = mod_pow(a, n / 2);
if (n % 2 == 0)
return res * res % mod;
else
return a * res % mod * res % mod;
}
ll invf(ll a) { return mod_pow(a, mod - 2); }
void C_init(int n) {
fac[0] = fac[1] = 1;
inv[1] = 1;
fiv[0] = fiv[1] = 1;
rep(i, 2, n + 1) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
fiv[i] = fiv[i - 1] * inv[i] % mod;
}
}
ll C(int a, int b) { // assume a >= b
if (a < b || a < 0 || b < 0)
return 0;
return fac[a] * fiv[b] % mod * fiv[a - b] % mod;
}
struct Ruiseki {
int N;
vector<ll> B;
void init(int n) {
N = n;
B = vector<ll>(N + 1, 0);
}
Ruiseki() {}
Ruiseki(int n) { init(n); }
void add(int a, ll v) {
if (a < 0 || N <= a)
return;
B[a + 1] += v;
}
void sweep() { rep(i, 1, N + 1) B[i] += B[i - 1]; }
ll get(int a, int b) { //[a, b)
if (a >= b)
return 0;
a = max(a, 0);
b = min(b, N);
return B[b] - B[a];
}
};
int N;
vector<int> G[1 << 20];
int A[MAX_N];
Ruiseki rui;
void solve() {
cin >> N;
ll ans = 0;
C_init(N);
rui.init(N);
rep(i, 0, N) cin >> A[i];
int sum = 0, cnt = 1;
rep(i, 0, N) {
sum ^= A[i];
if (sum == 0) {
rui.add(i, 1);
// debug(i);
cnt++;
} else
G[sum].pb(i);
}
rui.sweep();
// debug(sum, cnt);
if (sum == 0) {
ADD(ans, mod_pow(2, cnt - 2));
}
rep(i, 1, (1 << 20)) {
if (sz(G[i]) == 0)
continue;
ll suma = 0, sumz = 1;
// debug(G[i]);
rep(j, 0, sz(G[i])) {
// debug(i, j, suma, sumz);
ll v = sumz;
ADD(suma, v);
if (j != sz(G[i]) - 1) {
ll c = rui.get(G[i][j], G[i][j + 1]);
ADD(sumz, suma * c % mod);
// debug(G[i][j], G[i][j + 1], c);
}
}
if (sum == 0) {
ADD(ans, suma);
} else if (sum == i) {
ADD(ans, sumz);
}
}
cout << ans << "\n";
}
uint32_t rd() {
uint32_t res;
#ifdef __MINGW32__
asm volatile("rdrand %0" : "=a"(res)::"cc");
#else
res = std::random_device()();
#endif
return res;
}
int main() {
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(0);
#endif
cout << fixed;
cout.precision(20);
cerr << fixed;
cerr.precision(6);
rng.seed(rd());
#ifdef LOCAL
// freopen("in.txt", "wt", stdout); //for tester
freopen("in.txt", "rt", stdin);
#endif
solve();
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
return 0;
}
| #include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() { cerr << '\n'; }
template <class FIRST, class... REST> void Debug(FIRST arg, REST... rest) {
cerr << arg << " ";
Debug(rest...);
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
out << "[";
if (!v.empty()) {
rep(i, 0, sz(v) - 1) out << v[i] << ", ";
out << v.back();
}
out << "]";
return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &v) {
out << "(" << v.first << ", " << v.second << ")";
return out;
}
const int MAX_N = 500010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng; // use it by rng() % mod, shuffle(all(vec), rng)
///////////////////////////////////////////////////////////////////////////////////////////////////
ll fac[MAX_N], inv[MAX_N], fiv[MAX_N]; // fiv:inv(fac(i))
ll mod_pow(ll a, ll n) {
if (n == 0)
return 1;
ll res = mod_pow(a, n / 2);
if (n % 2 == 0)
return res * res % mod;
else
return a * res % mod * res % mod;
}
ll invf(ll a) { return mod_pow(a, mod - 2); }
void C_init(int n) {
fac[0] = fac[1] = 1;
inv[1] = 1;
fiv[0] = fiv[1] = 1;
rep(i, 2, n + 1) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
fiv[i] = fiv[i - 1] * inv[i] % mod;
}
}
ll C(int a, int b) { // assume a >= b
if (a < b || a < 0 || b < 0)
return 0;
return fac[a] * fiv[b] % mod * fiv[a - b] % mod;
}
struct Ruiseki {
int N;
vector<ll> B;
void init(int n) {
N = n;
B = vector<ll>(N + 1, 0);
}
Ruiseki() {}
Ruiseki(int n) { init(n); }
void add(int a, ll v) {
if (a < 0 || N <= a)
return;
B[a + 1] += v;
}
void sweep() { rep(i, 1, N + 1) B[i] += B[i - 1]; }
ll get(int a, int b) { //[a, b)
if (a >= b)
return 0;
a = max(a, 0);
b = min(b, N);
return B[b] - B[a];
}
};
int N;
vector<int> G[1 << 20];
int A[MAX_N];
Ruiseki rui;
void solve() {
cin >> N;
ll ans = 0;
C_init(N);
rui.init(N);
rep(i, 0, N) cin >> A[i];
int sum = 0, cnt = 1;
rep(i, 0, N) {
sum ^= A[i];
if (sum == 0) {
rui.add(i, 1);
// debug(i);
cnt++;
} else
G[sum].pb(i);
}
rui.sweep();
// debug(sum, cnt);
if (sum == 0) {
ADD(ans, mod_pow(2, cnt - 2));
}
rep(i, 1, (1 << 20)) {
if (sz(G[i]) == 0)
continue;
ll suma = 0, sumz = 1;
// debug(G[i]);
rep(j, 0, sz(G[i])) {
// debug(i, j, suma, sumz);
ll v = sumz;
ADD(suma, v);
if (j != sz(G[i]) - 1) {
ll c = rui.get(G[i][j], G[i][j + 1]);
ADD(sumz, suma * c % mod);
// debug(G[i][j], G[i][j + 1], c);
}
}
if (sum == 0) {
ADD(ans, suma);
} else if (sum == i) {
ADD(ans, sumz);
}
}
cout << ans << "\n";
}
uint32_t rd() {
uint32_t res;
#ifdef __MINGW32__
asm volatile("rdrand %0" : "=a"(res)::"cc");
#else
res = std::random_device()();
#endif
return res;
}
int main() {
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(0);
#endif
cout << fixed;
cout.precision(20);
cerr << fixed;
cerr.precision(6);
rng.seed(rd());
#ifdef LOCAL
// freopen("in.txt", "wt", stdout); //for tester
freopen("in.txt", "rt", stdin);
#endif
solve();
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
return 0;
}
| replace | 42 | 43 | 42 | 43 | 0 | Time elapsed: 0.087168 s.
|
p03051 | C++ | Runtime Error | #include <bits/stdc++.h>
#define PII pair<int, int>
#define LL long long
using namespace std;
const int MAXN = 500005;
const LL INF = (LL)1e9 + 5;
const LL MOD = 1000000007;
LL N, a[MAXN], sum[MAXN], dp[MAXN], ans;
LL dp_sum[1 << 20], cntz[MAXN], pre[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> a[i];
sum[i] = sum[i - 1] ^ a[i];
}
dp[0] = 1;
for (int i = 1; i <= N; i++) {
if (pre[sum[i]] == 0) {
dp[i] = 1;
} else {
dp[i] = ((dp[pre[sum[i]]] - 1) +
dp_sum[sum[i]] * (cntz[i - 1] - cntz[pre[sum[i]] - 1])) %
MOD;
// cout << "Add " << dp_sum[sum[i]] * (cntz[i - 1]
//- cntz[pre[sum[i]] - 1]) << ' ' << dp[pre[sum[i]]] - 1 << '\n'; cout <<
//"Before " << i << " get " << dp[i] << '\n';
if (sum[i] == 0)
dp[i] = (dp[i] - dp[pre[sum[i]]]) % MOD;
// cout << "After " << i << " get " << dp[i] <<
//'\n';
dp[i] = (dp[i] + 1) % MOD;
// dp[i] = 1;
// for (int j = 1; j < i; j++) {
// if (sum[j] == sum[i]) {
// for (int k = j + 1; k < i; k++)
//{ if (sum[k] == 0) { dp[i] = (dp[i] + dp[j]) % MOD;
// }
// }
// }
// }
}
pre[sum[i]] = i;
cntz[i] = cntz[i - 1] + (sum[i] == 0);
dp_sum[sum[i]] = (dp_sum[sum[i]] + dp[i]) % MOD;
if (sum[N] == 0)
ans += dp[i];
// cout << "At " << i << " get " << dp[i] << '\n';
}
if (sum[N] != 0)
ans = dp[N];
cout << (ans % MOD + MOD) % MOD << '\n';
return 0;
} | #include <bits/stdc++.h>
#define PII pair<int, int>
#define LL long long
using namespace std;
const int MAXN = 500005;
const LL INF = (LL)1e9 + 5;
const LL MOD = 1000000007;
LL N, a[MAXN], sum[MAXN], dp[MAXN], ans;
LL dp_sum[1 << 20], cntz[MAXN], pre[1 << 20];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> a[i];
sum[i] = sum[i - 1] ^ a[i];
}
dp[0] = 1;
for (int i = 1; i <= N; i++) {
if (pre[sum[i]] == 0) {
dp[i] = 1;
} else {
dp[i] = ((dp[pre[sum[i]]] - 1) +
dp_sum[sum[i]] * (cntz[i - 1] - cntz[pre[sum[i]] - 1])) %
MOD;
// cout << "Add " << dp_sum[sum[i]] * (cntz[i - 1]
//- cntz[pre[sum[i]] - 1]) << ' ' << dp[pre[sum[i]]] - 1 << '\n'; cout <<
//"Before " << i << " get " << dp[i] << '\n';
if (sum[i] == 0)
dp[i] = (dp[i] - dp[pre[sum[i]]]) % MOD;
// cout << "After " << i << " get " << dp[i] <<
//'\n';
dp[i] = (dp[i] + 1) % MOD;
// dp[i] = 1;
// for (int j = 1; j < i; j++) {
// if (sum[j] == sum[i]) {
// for (int k = j + 1; k < i; k++)
//{ if (sum[k] == 0) { dp[i] = (dp[i] + dp[j]) % MOD;
// }
// }
// }
// }
}
pre[sum[i]] = i;
cntz[i] = cntz[i - 1] + (sum[i] == 0);
dp_sum[sum[i]] = (dp_sum[sum[i]] + dp[i]) % MOD;
if (sum[N] == 0)
ans += dp[i];
// cout << "At " << i << " get " << dp[i] << '\n';
}
if (sum[N] != 0)
ans = dp[N];
cout << (ans % MOD + MOD) % MOD << '\n';
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p03051 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// #define FILE_IO
const int VMAX = (1 << 20) + 5;
const int NMAX = 5e2 + 5;
const int mod = 1e9 + 7;
int N;
int v[NMAX], sum[NMAX], dp[NMAX], cnt0[NMAX];
int sumdp[VMAX], sumdpcnt[VMAX];
vector<int> pos[VMAX];
int power(int x, int y) {
if (x == 0)
return 0;
if (y <= 0)
return 1;
int ans = power((1LL * x * x) % mod, y >> 1);
if (y & 1)
ans = (1LL * ans * x) % mod;
return ans;
}
int main() {
#ifdef FILE_IO
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> v[i];
sum[i] = sum[i - 1] ^ v[i];
}
int sumall = sum[N];
if (sumall != 0) {
sumdp[0] = 1;
dp[0] = 1;
for (int i = 1; i <= N; i++) {
int needval = sum[i] ^ sumall;
dp[i] = sumdp[needval];
sumdp[sum[i]] = (sumdp[sum[i]] + dp[i]) % mod;
}
printf("%d\n", dp[N]);
exit(0);
}
cnt0[0] = 1;
for (int i = 1; i <= N; i++) {
cnt0[i] = cnt0[i - 1] + (sum[i] == 0);
pos[sum[i]].push_back(i);
}
for (int i = 1; i <= N; i++) {
if (sum[i] == 0)
continue;
int s = sum[i];
dp[i] = ((1LL * cnt0[i] * sumdp[s]) % mod - sumdpcnt[s] + mod + 1) % mod;
sumdp[s] = (sumdp[s] + dp[i]) % mod;
sumdpcnt[s] = (sumdpcnt[s] + (1LL * dp[i] * cnt0[i]) % mod) % mod;
}
int ans = power(2, (int)pos[0].size() - 1);
for (int i = 1; i <= N; i++)
if (sum[i] != 0)
ans = (ans + dp[i]) % mod;
printf("%d\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// #define FILE_IO
const int VMAX = (1 << 20) + 5;
const int NMAX = 5e5 + 5;
const int mod = 1e9 + 7;
int N;
int v[NMAX], sum[NMAX], dp[NMAX], cnt0[NMAX];
int sumdp[VMAX], sumdpcnt[VMAX];
vector<int> pos[VMAX];
int power(int x, int y) {
if (x == 0)
return 0;
if (y <= 0)
return 1;
int ans = power((1LL * x * x) % mod, y >> 1);
if (y & 1)
ans = (1LL * ans * x) % mod;
return ans;
}
int main() {
#ifdef FILE_IO
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> v[i];
sum[i] = sum[i - 1] ^ v[i];
}
int sumall = sum[N];
if (sumall != 0) {
sumdp[0] = 1;
dp[0] = 1;
for (int i = 1; i <= N; i++) {
int needval = sum[i] ^ sumall;
dp[i] = sumdp[needval];
sumdp[sum[i]] = (sumdp[sum[i]] + dp[i]) % mod;
}
printf("%d\n", dp[N]);
exit(0);
}
cnt0[0] = 1;
for (int i = 1; i <= N; i++) {
cnt0[i] = cnt0[i - 1] + (sum[i] == 0);
pos[sum[i]].push_back(i);
}
for (int i = 1; i <= N; i++) {
if (sum[i] == 0)
continue;
int s = sum[i];
dp[i] = ((1LL * cnt0[i] * sumdp[s]) % mod - sumdpcnt[s] + mod + 1) % mod;
sumdp[s] = (sumdp[s] + dp[i]) % mod;
sumdpcnt[s] = (sumdpcnt[s] + (1LL * dp[i] * cnt0[i]) % mod) % mod;
}
int ans = power(2, (int)pos[0].size() - 1);
for (int i = 1; i <= N; i++)
if (sum[i] != 0)
ans = (ans + dp[i]) % mod;
printf("%d\n", ans);
return 0;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p03051 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 1e-5
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007LL
#define MOD 998244353LL
#define seg_size 262144 * 4
#define REP(a, b) for (long long a = 0; a < b; ++a)
long long grid[600000] = {};
vector<long long> finding_X[2000000];
long long zero[600000] = {};
long long cnt_zero(long long l, long long r) {
long long ans = zero[r];
if (l != 0) {
ans -= zero[l - 1];
}
return ans;
}
pair<long long, long long> calc(long long X) {
pair<long long, long long> ans = make_pair(1, 0);
if (finding_X[X].size() != 0) {
ans.second = 1;
}
for (int i = 1; i < finding_X[X].size(); ++i) {
long long zero_move = cnt_zero(finding_X[X][i - 1], finding_X[X][i]);
ans.first += zero_move * ans.second;
ans.first %= MAX_MOD;
ans.second += ans.first;
ans.second %= MAX_MOD;
}
return ans;
}
int main() {
iostream::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
REP(i, n) {
cin >> grid[i + 1];
grid[i + 1] ^= grid[i];
}
for (int i = 1; i <= n; ++i) {
zero[i] = zero[i - 1];
if (grid[i] == 0) {
zero[i]++;
} else {
finding_X[grid[i]].push_back(i);
}
}
if (grid[n] != 0) {
cout << calc(grid[n]).first << endl;
return 0;
}
assert(0 == 1);
} | #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 1e-5
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007LL
#define MOD 998244353LL
#define seg_size 262144 * 4
#define REP(a, b) for (long long a = 0; a < b; ++a)
long long grid[600000] = {};
vector<long long> finding_X[2000000];
long long zero[600000] = {};
long long cnt_zero(long long l, long long r) {
long long ans = zero[r];
if (l != 0) {
ans -= zero[l - 1];
}
return ans;
}
pair<long long, long long> calc(long long X) {
pair<long long, long long> ans = make_pair(1, 0);
if (finding_X[X].size() != 0) {
ans.second = 1;
}
for (int i = 1; i < finding_X[X].size(); ++i) {
long long zero_move = cnt_zero(finding_X[X][i - 1], finding_X[X][i]);
ans.first += zero_move * ans.second;
ans.first %= MAX_MOD;
ans.second += ans.first;
ans.second %= MAX_MOD;
}
return ans;
}
int main() {
iostream::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
REP(i, n) {
cin >> grid[i + 1];
grid[i + 1] ^= grid[i];
}
for (int i = 1; i <= n; ++i) {
zero[i] = zero[i - 1];
if (grid[i] == 0) {
zero[i]++;
} else {
finding_X[grid[i]].push_back(i);
}
}
if (grid[n] != 0) {
cout << calc(grid[n]).first << endl;
return 0;
}
long long ans = 1;
for (int i = 0; i < zero[n] - 1; ++i) {
ans *= 2;
ans %= MAX_MOD;
}
for (int i = 1; i < (1 << 20); ++i) {
ans += calc(i).second;
ans %= MAX_MOD;
}
cout << ans << endl;
} | replace | 83 | 84 | 83 | 93 | -6 | e082ea97-66f2-4a60-b7bf-480b17d215af.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03051/C++/s894172899.cpp:88: int main(): Assertion `0 == 1' failed.
|
p03051 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <complex>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define revrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define pb push_back
#define f first
#define s second
void BinarySay(ll x, ll y = 60) {
rep(i, y) cout << (x >> (y - 1 - i) & 1);
cout << endl;
}
const ll INFL = 1LL << 60; // 10^18 = 2^60
ll MOD = 1000000007;
// ll MOD = 998244353;
// vector<mint> dp(N, Mint);
// vector<vector<mint>> dp2(N, vector<mint>(N, Mint));
// vector<vector<vector<mint>>> dp3(N, vector<vector<mint>>(N, vector<mint>(N,
// Mint)));
struct mint {
ll x;
mint(ll x = 0) : x(x % MOD) {}
mint &operator+=(const mint a) {
(x += a.x) %= MOD;
return *this;
}
mint &operator-=(const mint a) {
(x += MOD - a.x) %= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint &operator%=(const mint a) {
(x %= a.x);
return *this;
}
mint &operator++(int) {
(x += 1) %= MOD;
return *this;
}
mint &operator--(int) {
(x += MOD - 1) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint operator%(const mint a) const {
mint res(*this);
return res %= a;
}
mint po(ll t) const {
if (!t)
return 1;
mint a = po(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
mint inverse() const { return po(MOD - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inverse(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
bool operator==(const mint a) { return this->x == a.x; }
bool operator!=(const mint a) { return this->x != a.x; }
void get() { cout << x << endl; }
void ge() { cout << x << " "; }
};
const mint Mint = 0;
mint pow_mod(ll x, ll k) {
mint res = 1;
mint a = x;
while (k > 0) {
if (k % 2) {
res *= a;
}
a *= a;
k /= 2;
}
return res;
}
mint inverse(ll x) { return pow_mod(x, MOD - 2); }
// 二項演算
const int MAXcomb = 200010;
ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAXcomb; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
mint comb(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
mint res = fac[n];
res *= finv[k] * finv[n - k];
return res;
}
mint comb_naive(ll N, ll K) { // Kが小さい時
mint res = 1;
rep(i, K) res *= (N - i);
mint k = 1;
rep(i, K) k *= (i + 1);
res /= k;
return res;
}
// 第二種スターリング数
const ll MAXStir2 = 3010;
vector<vector<mint>> Stir2memo(MAXStir2, vector<mint>(MAXStir2, Mint));
vector<mint> Bellmemo(MAXStir2, Mint);
void Stir2init() {
Stir2memo[0][0] = 1;
rep(i, MAXStir2 - 1) rep(j, i + 1) Stir2memo[i + 1][j + 1] =
Stir2memo[i][j] + Stir2memo[i][j + 1] * (j + 1);
rep(i, MAXStir2) {
Bellmemo[i] = 0;
rep(j, i + 1) Bellmemo[i] += Stir2memo[i][j];
}
}
mint Stir2(ll i, ll j) { // 区別できるi個をjグループに分ける場合の数
if (i < 0 || j < 0 || i < j)
return 0;
return Stir2memo[i][j];
}
mint Bell(ll x) { // 区別できるx個をグループ分けする方法全ての場合の数
if (x < 0)
return 0;
return Bellmemo[x];
}
mint kai_mod(ll K) {
if (K < 0)
return 0;
if (K == 0)
return 1;
return kai_mod(K - 1) * K;
}
// 約数の列挙O(√n)
vector<ll> divisor(ll n) {
vector<ll> res(0);
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i != n / i)
res.push_back(n / i);
}
}
sort(res.begin(), res.end());
return res;
}
void compress(vector<ll> &S) {
vector<ll> xs;
rep(i, S.size()) xs.push_back(S[i]);
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
rep(i, S.size()) {
S[i] = lower_bound(xs.begin(), xs.end(), S[i]) - xs.begin();
}
}
// Range Sum Query
const ll N_ = (1 << 18);
ll seg[2 * N_ - 1];
void add(ll k, ll x) {
k += N_ - 1;
seg[k] += x;
while (k > 0) {
k = (k - 1) / 2;
seg[k] = seg[2 * k + 1] + seg[2 * k + 2];
}
}
ll get_sum(ll x, ll y, ll k = 0, ll left = 0, ll right = N_) {
if (right <= x || y <= left)
return 0;
if (x <= left && right <= y)
return seg[k];
ll mid = (left + right) / 2;
ll l = get_sum(x, y, 2 * k + 1, left, mid);
ll r = get_sum(x, y, 2 * k + 2, mid, right);
return l + r;
}
ll N;
vector<ll> A;
void solve() {
ll XOR = 0;
ll cnt = 0;
rep(i, N) XOR ^= A[i];
if (XOR == 0) {
ll x = 0;
vector<ll> B;
ll now = 0;
rep(i, N) {
now ^= A[i];
B.pb(now);
if (now == 0) {
add(i, 1);
cnt++;
}
}
compress(B);
ll X = *max_element(B.begin(), B.end());
vector<ll> L[X + 1];
rep(i, N) {
if (B[i] == 0)
continue;
L[B[i]].pb(i);
}
rep(i, X + 1) L[i].pb(N_);
mint ans = 0;
for (ll i = 1; i <= X; i++) {
if (L[i].size() == 1)
continue;
mint s = 0, t = 1;
for (ll j = 0; j < L[i].size() - 1; j++) {
ll x = get_sum(L[i][j], L[i][j + 1]);
s += t;
t += s * x;
}
ans += s;
}
ans += pow_mod(2, cnt - 1);
ans.get();
} else {
ll a = XOR;
ll b = 0;
ll now = 0;
mint s = 0, t = 1;
rep(i, N - 1) {
now ^= A[i];
if (now == a) {
s += t;
} else if (now == b) {
t += s;
}
}
t.get();
}
}
int main() {
cin >> N;
A.resize(N);
rep(i, N) cin >> A[i];
solve();
}
| #include <algorithm>
#include <bitset>
#include <complex>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define revrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define pb push_back
#define f first
#define s second
void BinarySay(ll x, ll y = 60) {
rep(i, y) cout << (x >> (y - 1 - i) & 1);
cout << endl;
}
const ll INFL = 1LL << 60; // 10^18 = 2^60
ll MOD = 1000000007;
// ll MOD = 998244353;
// vector<mint> dp(N, Mint);
// vector<vector<mint>> dp2(N, vector<mint>(N, Mint));
// vector<vector<vector<mint>>> dp3(N, vector<vector<mint>>(N, vector<mint>(N,
// Mint)));
struct mint {
ll x;
mint(ll x = 0) : x(x % MOD) {}
mint &operator+=(const mint a) {
(x += a.x) %= MOD;
return *this;
}
mint &operator-=(const mint a) {
(x += MOD - a.x) %= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint &operator%=(const mint a) {
(x %= a.x);
return *this;
}
mint &operator++(int) {
(x += 1) %= MOD;
return *this;
}
mint &operator--(int) {
(x += MOD - 1) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint operator%(const mint a) const {
mint res(*this);
return res %= a;
}
mint po(ll t) const {
if (!t)
return 1;
mint a = po(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
mint inverse() const { return po(MOD - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inverse(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
bool operator==(const mint a) { return this->x == a.x; }
bool operator!=(const mint a) { return this->x != a.x; }
void get() { cout << x << endl; }
void ge() { cout << x << " "; }
};
const mint Mint = 0;
mint pow_mod(ll x, ll k) {
mint res = 1;
mint a = x;
while (k > 0) {
if (k % 2) {
res *= a;
}
a *= a;
k /= 2;
}
return res;
}
mint inverse(ll x) { return pow_mod(x, MOD - 2); }
// 二項演算
const int MAXcomb = 200010;
ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAXcomb; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
mint comb(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
mint res = fac[n];
res *= finv[k] * finv[n - k];
return res;
}
mint comb_naive(ll N, ll K) { // Kが小さい時
mint res = 1;
rep(i, K) res *= (N - i);
mint k = 1;
rep(i, K) k *= (i + 1);
res /= k;
return res;
}
// 第二種スターリング数
const ll MAXStir2 = 3010;
vector<vector<mint>> Stir2memo(MAXStir2, vector<mint>(MAXStir2, Mint));
vector<mint> Bellmemo(MAXStir2, Mint);
void Stir2init() {
Stir2memo[0][0] = 1;
rep(i, MAXStir2 - 1) rep(j, i + 1) Stir2memo[i + 1][j + 1] =
Stir2memo[i][j] + Stir2memo[i][j + 1] * (j + 1);
rep(i, MAXStir2) {
Bellmemo[i] = 0;
rep(j, i + 1) Bellmemo[i] += Stir2memo[i][j];
}
}
mint Stir2(ll i, ll j) { // 区別できるi個をjグループに分ける場合の数
if (i < 0 || j < 0 || i < j)
return 0;
return Stir2memo[i][j];
}
mint Bell(ll x) { // 区別できるx個をグループ分けする方法全ての場合の数
if (x < 0)
return 0;
return Bellmemo[x];
}
mint kai_mod(ll K) {
if (K < 0)
return 0;
if (K == 0)
return 1;
return kai_mod(K - 1) * K;
}
// 約数の列挙O(√n)
vector<ll> divisor(ll n) {
vector<ll> res(0);
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i != n / i)
res.push_back(n / i);
}
}
sort(res.begin(), res.end());
return res;
}
void compress(vector<ll> &S) {
vector<ll> xs;
rep(i, S.size()) xs.push_back(S[i]);
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
rep(i, S.size()) {
S[i] = lower_bound(xs.begin(), xs.end(), S[i]) - xs.begin();
}
}
// Range Sum Query
const ll N_ = (1 << 20);
ll seg[2 * N_ - 1];
void add(ll k, ll x) {
k += N_ - 1;
seg[k] += x;
while (k > 0) {
k = (k - 1) / 2;
seg[k] = seg[2 * k + 1] + seg[2 * k + 2];
}
}
ll get_sum(ll x, ll y, ll k = 0, ll left = 0, ll right = N_) {
if (right <= x || y <= left)
return 0;
if (x <= left && right <= y)
return seg[k];
ll mid = (left + right) / 2;
ll l = get_sum(x, y, 2 * k + 1, left, mid);
ll r = get_sum(x, y, 2 * k + 2, mid, right);
return l + r;
}
ll N;
vector<ll> A;
void solve() {
ll XOR = 0;
ll cnt = 0;
rep(i, N) XOR ^= A[i];
if (XOR == 0) {
ll x = 0;
vector<ll> B;
ll now = 0;
rep(i, N) {
now ^= A[i];
B.pb(now);
if (now == 0) {
add(i, 1);
cnt++;
}
}
compress(B);
ll X = *max_element(B.begin(), B.end());
vector<ll> L[X + 1];
rep(i, N) {
if (B[i] == 0)
continue;
L[B[i]].pb(i);
}
rep(i, X + 1) L[i].pb(N_);
mint ans = 0;
for (ll i = 1; i <= X; i++) {
if (L[i].size() == 1)
continue;
mint s = 0, t = 1;
for (ll j = 0; j < L[i].size() - 1; j++) {
ll x = get_sum(L[i][j], L[i][j + 1]);
s += t;
t += s * x;
}
ans += s;
}
ans += pow_mod(2, cnt - 1);
ans.get();
} else {
ll a = XOR;
ll b = 0;
ll now = 0;
mint s = 0, t = 1;
rep(i, N - 1) {
now ^= A[i];
if (now == a) {
s += t;
} else if (now == b) {
t += s;
}
}
t.get();
}
}
int main() {
cin >> N;
A.resize(N);
rep(i, N) cin >> A[i];
solve();
}
| replace | 201 | 202 | 201 | 202 | 0 | |
p03051 | C++ | Runtime Error | #include <iostream>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
int a[500005];
ll dp1[2100000];
ll dp2[2100000];
ll s[500005];
ll last[500005];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
ll x = 0;
for (int i = 0; i < 2100000; i++)
dp2[i] = 1;
for (int i = 0; i < n; i++) {
x ^= a[i];
s[i + 1] = s[i];
if (x) {
dp2[x] = (dp2[x] + dp1[x] * (s[i + 1] - s[last[x]]) % MOD) % MOD;
dp1[x] = (dp1[x] + dp2[x]) % MOD;
last[x] = i + 1;
} else {
s[i + 1]++;
}
}
ll ans = 0;
if (x == 0) {
for (int i = 0; i < 2100000; i++)
ans = (ans + dp1[i]) % MOD;
ll c = 0;
for (int i = 0; i < n; i++) {
x ^= a[i];
if (x == 0)
c++;
}
ll k = 1;
for (int i = 0; i < c - 1; i++)
k = k * 2 % MOD;
ans = (ans + k) % MOD;
} else {
ans = dp2[x];
}
cout << ans << endl;
}
| #include <iostream>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
int a[500005];
ll dp1[2100000];
ll dp2[2100000];
ll s[500005];
ll last[2100000];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
ll x = 0;
for (int i = 0; i < 2100000; i++)
dp2[i] = 1;
for (int i = 0; i < n; i++) {
x ^= a[i];
s[i + 1] = s[i];
if (x) {
dp2[x] = (dp2[x] + dp1[x] * (s[i + 1] - s[last[x]]) % MOD) % MOD;
dp1[x] = (dp1[x] + dp2[x]) % MOD;
last[x] = i + 1;
} else {
s[i + 1]++;
}
}
ll ans = 0;
if (x == 0) {
for (int i = 0; i < 2100000; i++)
ans = (ans + dp1[i]) % MOD;
ll c = 0;
for (int i = 0; i < n; i++) {
x ^= a[i];
if (x == 0)
c++;
}
ll k = 1;
for (int i = 0; i < c - 1; i++)
k = k * 2 % MOD;
ans = (ans + k) % MOD;
} else {
ans = dp2[x];
}
cout << ans << endl;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p03051 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <tr1/unordered_map>
#define ll long long
#define maxn 100010
#define maxa (1 << 21)
#define mod 1000000007
using namespace std;
using namespace tr1;
inline int getint() {
char c = getchar();
int x = 0;
bool p = 0;
while ((c > '9' || c < '0') && c != '-')
c = getchar();
if (c == '-')
p = 1, c = getchar();
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
if (p)
x = -x;
return x;
}
int n, a[maxn], s1[maxa << 3], s2[maxa << 3], tg[maxa << 3];
inline void pushup(int o) {
s1[o] = (s1[o << 1] + s1[o << 1 | 1]) % mod;
s2[o] = (s2[o << 1] + s2[o << 1 | 1]) % mod;
}
inline void build(int o, int l, int r) {
tg[o] = 0;
if (l == r) {
s1[o] = 1;
s2[o] = 0;
return;
}
int mid = (l + r) >> 1;
build(o << 1, l, mid), build(o << 1 | 1, mid + 1, r);
pushup(o);
}
inline void gao(int o, int x) {
s1[o] = (s1[o] + 1ll * x * s2[o]) % mod;
tg[o] = (tg[o] + x) % mod;
}
inline void pushdown(int o) {
gao(o << 1, tg[o]), gao(o << 1 | 1, tg[o]);
tg[o] = 0;
}
inline void update(int o, int l, int r, int x, int p) {
if (l == r) {
if (p == n)
cout << s1[o] << endl;
s2[o] = (s2[o] + s1[o]) % mod;
return;
}
pushdown(o);
int mid = (l + r) >> 1;
if (x <= mid)
update(o << 1, l, mid, x, p);
else
update(o << 1 | 1, mid + 1, r, x, p);
pushup(o);
}
int main() {
n = getint();
for (int i = 1; i <= n; ++i)
a[i] = getint(), a[i] ^= a[i - 1];
build(1, 1, maxa);
int sum = 1;
for (int i = 1; i <= n; ++i) {
if (!a[i]) {
if (i == n)
cout << (s2[1] + sum) % mod << endl;
sum = 1ll * sum * 2 % mod;
gao(1, 1);
} else
update(1, 1, maxa, a[i], i);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <tr1/unordered_map>
#define ll long long
#define maxn 500050
#define maxa (1 << 21)
#define mod 1000000007
using namespace std;
using namespace tr1;
inline int getint() {
char c = getchar();
int x = 0;
bool p = 0;
while ((c > '9' || c < '0') && c != '-')
c = getchar();
if (c == '-')
p = 1, c = getchar();
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
if (p)
x = -x;
return x;
}
int n, a[maxn], s1[maxa << 3], s2[maxa << 3], tg[maxa << 3];
inline void pushup(int o) {
s1[o] = (s1[o << 1] + s1[o << 1 | 1]) % mod;
s2[o] = (s2[o << 1] + s2[o << 1 | 1]) % mod;
}
inline void build(int o, int l, int r) {
tg[o] = 0;
if (l == r) {
s1[o] = 1;
s2[o] = 0;
return;
}
int mid = (l + r) >> 1;
build(o << 1, l, mid), build(o << 1 | 1, mid + 1, r);
pushup(o);
}
inline void gao(int o, int x) {
s1[o] = (s1[o] + 1ll * x * s2[o]) % mod;
tg[o] = (tg[o] + x) % mod;
}
inline void pushdown(int o) {
gao(o << 1, tg[o]), gao(o << 1 | 1, tg[o]);
tg[o] = 0;
}
inline void update(int o, int l, int r, int x, int p) {
if (l == r) {
if (p == n)
cout << s1[o] << endl;
s2[o] = (s2[o] + s1[o]) % mod;
return;
}
pushdown(o);
int mid = (l + r) >> 1;
if (x <= mid)
update(o << 1, l, mid, x, p);
else
update(o << 1 | 1, mid + 1, r, x, p);
pushup(o);
}
int main() {
n = getint();
for (int i = 1; i <= n; ++i)
a[i] = getint(), a[i] ^= a[i - 1];
build(1, 1, maxa);
int sum = 1;
for (int i = 1; i <= n; ++i) {
if (!a[i]) {
if (i == n)
cout << (s2[1] + sum) % mod << endl;
sum = 1ll * sum * 2 % mod;
gao(1, 1);
} else
update(1, 1, maxa, a[i], i);
}
return 0;
} | replace | 6 | 7 | 6 | 7 | -11 | |
p03051 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define ll long long
#define MOD 1000000007
int n;
int a[500001];
int b[500002];
int zerob[500003];
vector<int> xat[1 << 20];
map<int, ll> dp[2];
int getNextX(int at, int tarX) {
vector<int> target = xat[tarX];
vector<int>::iterator position =
lower_bound(target.begin(), target.end(), at);
if (position == target.end())
return n;
return *position;
}
int getXnum(int l, int r, int tarX) {
vector<int> target = xat[tarX];
vector<int>::iterator posA = lower_bound(target.begin(), target.end(), l);
vector<int>::iterator posB = lower_bound(target.begin(), target.end(), r);
return posB - posA;
}
int get0num(int l, int r) { return zerob[r] - zerob[l]; }
int main() {
cin >> n;
// srand((unsigned)time(NULL));
// n = 500000;
for (int i = 0; i < n; i++) {
cin >> a[i];
// a[i] = i%40;
// a[i] = (1<<20)-1;
}
b[0] = 0;
for (int i = 0; i < n; i++) {
b[i + 1] = b[i] ^ a[i];
}
n++;
if (b[n - 1] == 0)
return 1;
zerob[0] = 0;
for (int i = 0; i < n; i++) {
xat[b[i]].push_back(i);
zerob[i + 1] = zerob[i] + (b[i] == 0 ? 1 : 0);
}
// for(int i = 0;i < n;i++){
// cout << b[i] << " ";
// }
// cout << endl;
// for(int i = 0;i < n;i++){
// cout << zerob[i] << " ";
// }
// cout << endl;
ll ans = 0;
map<int, int> fin;
for (int i = 0; i < n; i++) {
int X = b[i];
if (X == 0)
continue;
if (fin[X] == 1)
continue;
if (b[n - 1] != 0 && b[n - 1] != X)
continue;
// cout << X << endl;
fin[X] = 1;
vector<pair<int, int>> target;
int index_last = 0;
int count = 1;
if (b[n - 1] == 0)
xat[X].push_back(n); // 番兵
target.push_back(make_pair(0, 1));
// cout << xat[X].size() <<endl;
for (int j = 1; j < xat[X].size(); j++) { // 座標圧縮
int at = xat[X][j];
// cout << at << " ";
int num0 = get0num(xat[X][index_last], at);
if (num0 == 0) {
count++;
continue;
}
target.push_back(make_pair(X, count));
target.push_back(make_pair(0, num0));
index_last = j;
count = 1;
}
if (b[n - 1] != 0)
target.push_back(make_pair(X, count));
// cout << endl;
// cout << X << endl;
// for(int j = 0;j < target.size();j++){
// cout << target[j].first << " " << target[j].second << endl;
// }
dp[0].clear();
dp[1].clear();
int n2 = target.size();
dp[0][n2 - 1] = b[n - 1] == 0 ? 0 : 1;
dp[1][n2 - 1] = (dp[0][n2 - 1] + 1) % 2;
for (int at = n2 - 2; at != -1; at--) {
for (int before = 0; before < 2; before++) {
// cout << at << endl;
dp[before][at] = 0;
dp[before][at] += dp[before][at + 1];
int y = target[at].first;
int len = target[at].second;
if (y == 0 && before == 1) {
dp[before][at] += dp[0][at + 1] * len % MOD; // 選ぶ
} else if (y != 0 && before == 0) {
dp[before][at] += dp[1][at + 1] * len % MOD; // 選ぶ
}
dp[before][at] %= MOD;
}
}
ans += dp[0][0];
ans %= MOD;
}
if (b[n - 1] != 0) {
cout << ans << endl;
return 0;
}
int num0 = getXnum(0, n, 0);
ll ans0 = 1;
for (int i = 0; i < num0 - 2; i++) {
ans0 *= 2;
ans0 %= MOD;
}
cout << (ans0 + ans) % MOD << endl;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define ll long long
#define MOD 1000000007
int n;
int a[500001];
int b[500002];
int zerob[500003];
vector<int> xat[1 << 20];
map<int, ll> dp[2];
int getNextX(int at, int tarX) {
vector<int> target = xat[tarX];
vector<int>::iterator position =
lower_bound(target.begin(), target.end(), at);
if (position == target.end())
return n;
return *position;
}
int getXnum(int l, int r, int tarX) {
vector<int> target = xat[tarX];
vector<int>::iterator posA = lower_bound(target.begin(), target.end(), l);
vector<int>::iterator posB = lower_bound(target.begin(), target.end(), r);
return posB - posA;
}
int get0num(int l, int r) { return zerob[r] - zerob[l]; }
int main() {
cin >> n;
// srand((unsigned)time(NULL));
// n = 500000;
for (int i = 0; i < n; i++) {
cin >> a[i];
// a[i] = i%40;
// a[i] = (1<<20)-1;
}
b[0] = 0;
for (int i = 0; i < n; i++) {
b[i + 1] = b[i] ^ a[i];
}
n++;
zerob[0] = 0;
for (int i = 0; i < n; i++) {
xat[b[i]].push_back(i);
zerob[i + 1] = zerob[i] + (b[i] == 0 ? 1 : 0);
}
// for(int i = 0;i < n;i++){
// cout << b[i] << " ";
// }
// cout << endl;
// for(int i = 0;i < n;i++){
// cout << zerob[i] << " ";
// }
// cout << endl;
ll ans = 0;
map<int, int> fin;
for (int i = 0; i < n; i++) {
int X = b[i];
if (X == 0)
continue;
if (fin[X] == 1)
continue;
if (b[n - 1] != 0 && b[n - 1] != X)
continue;
// cout << X << endl;
fin[X] = 1;
vector<pair<int, int>> target;
int index_last = 0;
int count = 1;
if (b[n - 1] == 0)
xat[X].push_back(n); // 番兵
target.push_back(make_pair(0, 1));
// cout << xat[X].size() <<endl;
for (int j = 1; j < xat[X].size(); j++) { // 座標圧縮
int at = xat[X][j];
// cout << at << " ";
int num0 = get0num(xat[X][index_last], at);
if (num0 == 0) {
count++;
continue;
}
target.push_back(make_pair(X, count));
target.push_back(make_pair(0, num0));
index_last = j;
count = 1;
}
if (b[n - 1] != 0)
target.push_back(make_pair(X, count));
// cout << endl;
// cout << X << endl;
// for(int j = 0;j < target.size();j++){
// cout << target[j].first << " " << target[j].second << endl;
// }
dp[0].clear();
dp[1].clear();
int n2 = target.size();
dp[0][n2 - 1] = b[n - 1] == 0 ? 0 : 1;
dp[1][n2 - 1] = (dp[0][n2 - 1] + 1) % 2;
for (int at = n2 - 2; at != -1; at--) {
for (int before = 0; before < 2; before++) {
// cout << at << endl;
dp[before][at] = 0;
dp[before][at] += dp[before][at + 1];
int y = target[at].first;
int len = target[at].second;
if (y == 0 && before == 1) {
dp[before][at] += dp[0][at + 1] * len % MOD; // 選ぶ
} else if (y != 0 && before == 0) {
dp[before][at] += dp[1][at + 1] * len % MOD; // 選ぶ
}
dp[before][at] %= MOD;
}
}
ans += dp[0][0];
ans %= MOD;
}
if (b[n - 1] != 0) {
cout << ans << endl;
return 0;
}
int num0 = getXnum(0, n, 0);
ll ans0 = 1;
for (int i = 0; i < num0 - 2; i++) {
ans0 *= 2;
ans0 %= MOD;
}
cout << (ans0 + ans) % MOD << endl;
} | delete | 55 | 57 | 55 | 55 | 1 | |
p03051 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MN 1001000
#define MOD 1000000007
using namespace std;
typedef long long ll;
ll n, ans, a[MN], txor, ac[MN], dp[MN], adp[MN];
vector<int> occ[MN];
ll zs(int l, int r) {
int L = lower_bound(occ[0].begin(), occ[0].end(), l) - occ[0].begin();
int R = lower_bound(occ[0].begin(), occ[0].end(), r) - occ[0].begin();
return R - L;
}
ll p[MN][2];
ll solve(int pos, int empt) {
if (pos > n)
return !empt;
ll &x = p[pos][empt];
if (x != -1)
return x;
x = 0;
ll xo = a[pos], j = pos;
while (xo != 0) {
xo ^= a[++j];
}
x = (solve(j + 1, 0) + solve(j + 1, 1)) % MOD;
return x;
}
int main() {
scanf("%lld", &n);
memset(p, -1, sizeof p);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
txor ^= a[i];
}
a[0] = ac[0] = txor;
occ[txor].push_back(0);
for (int i = 1; i <= n; i++) {
txor ^= a[i];
ac[i] = txor;
occ[txor].push_back(i);
}
txor = ac[0];
dp[n + 1] = 0;
for (int i = n; i > 0; i--) {
int ai = ac[i - 1];
int l =
lower_bound(occ[ai].begin(), occ[ai].end(), i - 1) - occ[ai].begin();
int r = l + 1;
assert(l < occ[ai].size() && occ[ai][l] == i - 1);
if (r == occ[ai].size())
dp[i] = 1, adp[i] = 1;
else {
ll z = zs(i, occ[ai][r]);
dp[i] = (dp[occ[ai][r] + 1] + z * adp[occ[ai][r] + 1]) % MOD;
adp[i] = (adp[occ[ai][r] + 1] + dp[i]) % MOD;
}
}
if (txor == 0) {
for (int i = 1; i + 1 <= n; i++) {
if (ac[i])
ans += dp[i + 1];
if (ans >= MOD)
ans -= MOD;
}
ans += solve(1, 0);
if (ans >= MOD)
ans -= MOD;
} else
ans = dp[1];
printf("%lld\n", ans);
return 0;
}
| #include <bits/stdc++.h>
#define MN 2001000
#define MOD 1000000007
using namespace std;
typedef long long ll;
ll n, ans, a[MN], txor, ac[MN], dp[MN], adp[MN];
vector<int> occ[MN];
ll zs(int l, int r) {
int L = lower_bound(occ[0].begin(), occ[0].end(), l) - occ[0].begin();
int R = lower_bound(occ[0].begin(), occ[0].end(), r) - occ[0].begin();
return R - L;
}
ll p[MN][2];
ll solve(int pos, int empt) {
if (pos > n)
return !empt;
ll &x = p[pos][empt];
if (x != -1)
return x;
x = 0;
ll xo = a[pos], j = pos;
while (xo != 0) {
xo ^= a[++j];
}
x = (solve(j + 1, 0) + solve(j + 1, 1)) % MOD;
return x;
}
int main() {
scanf("%lld", &n);
memset(p, -1, sizeof p);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
txor ^= a[i];
}
a[0] = ac[0] = txor;
occ[txor].push_back(0);
for (int i = 1; i <= n; i++) {
txor ^= a[i];
ac[i] = txor;
occ[txor].push_back(i);
}
txor = ac[0];
dp[n + 1] = 0;
for (int i = n; i > 0; i--) {
int ai = ac[i - 1];
int l =
lower_bound(occ[ai].begin(), occ[ai].end(), i - 1) - occ[ai].begin();
int r = l + 1;
assert(l < occ[ai].size() && occ[ai][l] == i - 1);
if (r == occ[ai].size())
dp[i] = 1, adp[i] = 1;
else {
ll z = zs(i, occ[ai][r]);
dp[i] = (dp[occ[ai][r] + 1] + z * adp[occ[ai][r] + 1]) % MOD;
adp[i] = (adp[occ[ai][r] + 1] + dp[i]) % MOD;
}
}
if (txor == 0) {
for (int i = 1; i + 1 <= n; i++) {
if (ac[i])
ans += dp[i + 1];
if (ans >= MOD)
ans -= MOD;
}
ans += solve(1, 0);
if (ans >= MOD)
ans -= MOD;
} else
ans = dp[1];
printf("%lld\n", ans);
return 0;
}
| replace | 2 | 3 | 2 | 3 | 0 | |
p03051 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int add(int x, int y) {
x += y;
if (x >= mod)
x -= mod;
return x;
}
int sub(int x, int y) {
x -= y;
if (x < 0)
x += mod;
return x;
}
int mul(int x, int y) { return (x * 1LL * y) % mod; }
int n;
int arr[500005];
int sum[500005];
pair<int, int> tree[500005];
int doneUpdates[500005];
int updates;
void propagate(int ind) {
int toDo = updates - doneUpdates[ind];
doneUpdates[ind] = updates;
tree[ind].first = add(tree[ind].first, mul(tree[ind].second, toDo));
}
void update(int ind, int val) {
propagate(ind);
tree[ind].second = add(tree[ind].second, val);
}
void apply() { updates++; }
int get(int ind) {
propagate(ind);
return tree[ind].first;
}
int main() {
ios::sync_with_stdio(false);
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", arr + i), sum[i] = arr[i];
for (int i = 1; i < n; i++)
sum[i] ^= sum[i - 1];
int ans = 0;
int z = 0;
for (int i = n - 1; i >= 0; i--) {
int val = sum[i];
if (val == 0) {
apply();
if (sum[n - 1] == 0) {
int tmp = z;
if (i == n - 1)
tmp = 1;
z = add(tmp, z);
ans = add(ans, tmp);
}
} else {
int tmp = get(val);
if (sum[n - 1] == 0)
tmp = add(tmp, 1);
else if (i == n - 1)
tmp = 1;
ans = add(ans, tmp);
update(val, tmp);
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int add(int x, int y) {
x += y;
if (x >= mod)
x -= mod;
return x;
}
int sub(int x, int y) {
x -= y;
if (x < 0)
x += mod;
return x;
}
int mul(int x, int y) { return (x * 1LL * y) % mod; }
int n;
int arr[500005];
int sum[500005];
pair<int, int> tree[1 << 21];
int doneUpdates[1 << 21];
int updates;
void propagate(int ind) {
int toDo = updates - doneUpdates[ind];
doneUpdates[ind] = updates;
tree[ind].first = add(tree[ind].first, mul(tree[ind].second, toDo));
}
void update(int ind, int val) {
propagate(ind);
tree[ind].second = add(tree[ind].second, val);
}
void apply() { updates++; }
int get(int ind) {
propagate(ind);
return tree[ind].first;
}
int main() {
ios::sync_with_stdio(false);
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", arr + i), sum[i] = arr[i];
for (int i = 1; i < n; i++)
sum[i] ^= sum[i - 1];
int ans = 0;
int z = 0;
for (int i = n - 1; i >= 0; i--) {
int val = sum[i];
if (val == 0) {
apply();
if (sum[n - 1] == 0) {
int tmp = z;
if (i == n - 1)
tmp = 1;
z = add(tmp, z);
ans = add(ans, tmp);
}
} else {
int tmp = get(val);
if (sum[n - 1] == 0)
tmp = add(tmp, 1);
else if (i == n - 1)
tmp = 1;
ans = add(ans, tmp);
update(val, tmp);
}
}
cout << ans << endl;
return 0;
}
| replace | 21 | 23 | 21 | 23 | 0 | |
p03051 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using LL = long long;
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// std::vector Declaration
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
// std::vector Initialization
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename U> void fill_all(T &arr, const U &v) {
for (auto &i : arr)
fill_all(i, v);
}
// std::vector Debug
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
bool a = 1;
for (auto e : v) {
os << (a ? "" : " ");
os << e;
a = 0;
}
os << "]";
return os;
}
// std::pair Debug
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << " " << p.second << ")";
return os;
}
// std::set Debug
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
os << "{";
bool a = 1;
for (auto e : st) {
os << (a ? "" : " ");
os << e;
a = 0;
}
os << "}";
return os;
}
// std::map Debug
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
os << "{";
bool a = 1;
for (auto e : mp) {
os << (a ? "" : " ");
os << e.first << ":" << e.second;
a = 0;
}
os << "}";
return os;
}
// std::tuple Debug
template <int N, class Tuple> void out(ostream &os, const Tuple &t) {}
template <int N, class Tuple, class H, class... Ts>
void out(ostream &os, const Tuple &t) {
if (N)
os << " ";
os << get<N>(t);
out<N + 1, Tuple, Ts...>(os, t);
}
template <class... Ts> ostream &operator<<(ostream &os, const tuple<Ts...> &t) {
os << "(";
out<0, tuple<Ts...>, Ts...>(os, t);
os << ")";
return os;
}
// Debug
#define DUMP(x) cout << #x << " = " << (x) << endl
struct edge {
int to, cost;
};
const LL LINF = 1LL << 60;
const int IINF = 1 << 30;
const LL MOD = 1e9 + 7;
template <typename Type, typename Operators> struct AlgStruct {
Type x;
template <typename... Args> AlgStruct(Args... args) : x(args...) {}
template <typename... Args> decltype(auto) operator[](Args... args) {
return x.operator[](args...);
}
AlgStruct &operator=(const AlgStruct &value) & = default;
template <typename... Args> static const AlgStruct idAdd(Args... args) {
return Operators::IdAdd(args...);
}
template <typename... Args> static const AlgStruct idMul(Args... args) {
return Operators::IdMul(args...);
}
const AlgStruct operator+() const { return *this; }
const AlgStruct operator-() const { return Operators::Opposite(x); }
const AlgStruct reciprocal() const { return Operators::Reciprocal(x); }
AlgStruct &operator+=(const AlgStruct &rhs) {
x = Operators::Add(x, rhs.x);
return *this;
}
AlgStruct &operator-=(const AlgStruct &rhs) {
*this += -rhs;
return *this;
}
AlgStruct &operator*=(const AlgStruct &rhs) {
x = Operators::Mul(x, rhs.x);
return *this;
}
AlgStruct &operator/=(const AlgStruct &rhs) {
return *this *= rhs.reciprocal();
}
const AlgStruct operator+(const AlgStruct &rhs) const {
return AlgStruct(*this) += rhs;
}
const AlgStruct operator-(const AlgStruct &rhs) const {
return AlgStruct(*this) -= rhs;
}
const AlgStruct operator*(const AlgStruct &rhs) const {
return AlgStruct(*this) *= rhs;
}
const AlgStruct operator/(const AlgStruct &rhs) const {
return AlgStruct(*this) /= rhs;
}
const AlgStruct power(long long n) const { return Operators::Power(x, n); }
bool operator<(const AlgStruct &rhs) const {
return Operators::Less(x, rhs.x);
}
bool operator>(const AlgStruct &rhs) const { return rhs < *this; }
bool operator<=(const AlgStruct &rhs) const { return !(*this > rhs); }
bool operator>=(const AlgStruct &rhs) const { return !(*this < rhs); }
bool operator==(const AlgStruct &rhs) const {
return !(*this < rhs) && !(*this > rhs);
}
bool operator!=(const AlgStruct &rhs) const { return !(*this == rhs); }
};
template <typename Type, typename Operators>
const AlgStruct<Type, Operators>
operator+(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) + rhs;
}
template <typename Type, typename Operators>
const AlgStruct<Type, Operators>
operator-(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) - rhs;
}
template <typename Type, typename Operators>
const AlgStruct<Type, Operators>
operator*(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) * rhs;
}
template <typename Type, typename Operators>
const AlgStruct<Type, Operators>
operator/(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) / rhs;
}
template <typename Type, typename Operators>
bool operator<(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) < rhs;
}
template <typename Type, typename Operators>
bool operator>(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) > rhs;
}
template <typename Type, typename Operators>
bool operator<=(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) <= rhs;
}
template <typename Type, typename Operators>
bool operator>=(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) >= rhs;
}
template <typename Type, typename Operators>
bool operator==(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) == rhs;
}
template <typename Type, typename Operators>
bool operator!=(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) != rhs;
}
template <typename Type, typename Operators>
auto &operator<<(ostream &s, const AlgStruct<Type, Operators> &value) {
s << value.x;
return s;
}
template <typename T, T mod> struct GaloisFieldOperators {
static T Add(T lhs, T rhs) { return (lhs + rhs) % mod; };
static T Mul(T lhs, T rhs) { return (lhs * rhs) % mod; };
static T IdAdd() { return 0; };
static T IdMul() { return 1; };
static T Opposite(T value) { return ((-value % mod) + mod) % mod; };
static bool Less(T lhs, T rhs) { return lhs < rhs; };
static T Power(T value, long long n) {
T res = IdMul();
while (n > 0) {
if (n & 1)
res = Mul(res, value);
value = Mul(value, value);
n >>= 1;
}
return res;
}
static T Reciprocal(T value) { return Power(value, mod - 2); }
};
using GaloisField = AlgStruct<long long, GaloisFieldOperators<long long, MOD>>;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; ++i) {
cin >> a[i];
}
vector<int> s(N + 1);
for (int i = 0; i < N; ++i) {
s[i + 1] = s[i] ^ a[i];
}
if (s[N] != 0) {
int d = s[N];
vector<LL> p;
int cnt = 0;
for (int i = 0; i < N + 1; ++i) {
if (s[i] == d) {
p.push_back(cnt);
} else if (s[i] == 0) {
++cnt;
}
}
int D = p.size();
vector<GaloisField> dp(D + 1), sum0(D + 1), sum1(D + 1);
for (int i = 0; i < D; ++i) {
dp[i + 1] = 1LL - sum1[i] + p[i] * sum0[i];
sum0[i + 1] = sum0[i] + dp[i + 1];
sum1[i + 1] = sum1[i] + dp[i + 1] * p[i];
}
GaloisField ans = dp[D];
cout << ans << endl;
}
else {
const int dmax = 1 << 3;
vector<vector<LL>> p(dmax + 1);
int cnt = 0;
for (int i = 0; i < N + 1; ++i) {
if (s[i] == 0) {
++cnt;
} else {
p[s[i]].push_back(cnt);
}
}
GaloisField ans = GaloisField(2).power(cnt - 2);
for (int d = 1; d < dmax + 1; ++d) {
int D = p[d].size();
vector<GaloisField> dp(D + 1), sum0(D + 1), sum1(D + 1);
for (int i = 0; i < D; ++i) {
dp[i + 1] = 1LL - sum1[i] + p[d][i] * sum0[i];
sum0[i + 1] = sum0[i] + dp[i + 1];
sum1[i + 1] = sum1[i] + dp[i + 1] * p[d][i];
ans += dp[i + 1];
}
}
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using LL = long long;
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// std::vector Declaration
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
// std::vector Initialization
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename U> void fill_all(T &arr, const U &v) {
for (auto &i : arr)
fill_all(i, v);
}
// std::vector Debug
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
bool a = 1;
for (auto e : v) {
os << (a ? "" : " ");
os << e;
a = 0;
}
os << "]";
return os;
}
// std::pair Debug
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << " " << p.second << ")";
return os;
}
// std::set Debug
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
os << "{";
bool a = 1;
for (auto e : st) {
os << (a ? "" : " ");
os << e;
a = 0;
}
os << "}";
return os;
}
// std::map Debug
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
os << "{";
bool a = 1;
for (auto e : mp) {
os << (a ? "" : " ");
os << e.first << ":" << e.second;
a = 0;
}
os << "}";
return os;
}
// std::tuple Debug
template <int N, class Tuple> void out(ostream &os, const Tuple &t) {}
template <int N, class Tuple, class H, class... Ts>
void out(ostream &os, const Tuple &t) {
if (N)
os << " ";
os << get<N>(t);
out<N + 1, Tuple, Ts...>(os, t);
}
template <class... Ts> ostream &operator<<(ostream &os, const tuple<Ts...> &t) {
os << "(";
out<0, tuple<Ts...>, Ts...>(os, t);
os << ")";
return os;
}
// Debug
#define DUMP(x) cout << #x << " = " << (x) << endl
struct edge {
int to, cost;
};
const LL LINF = 1LL << 60;
const int IINF = 1 << 30;
const LL MOD = 1e9 + 7;
template <typename Type, typename Operators> struct AlgStruct {
Type x;
template <typename... Args> AlgStruct(Args... args) : x(args...) {}
template <typename... Args> decltype(auto) operator[](Args... args) {
return x.operator[](args...);
}
AlgStruct &operator=(const AlgStruct &value) & = default;
template <typename... Args> static const AlgStruct idAdd(Args... args) {
return Operators::IdAdd(args...);
}
template <typename... Args> static const AlgStruct idMul(Args... args) {
return Operators::IdMul(args...);
}
const AlgStruct operator+() const { return *this; }
const AlgStruct operator-() const { return Operators::Opposite(x); }
const AlgStruct reciprocal() const { return Operators::Reciprocal(x); }
AlgStruct &operator+=(const AlgStruct &rhs) {
x = Operators::Add(x, rhs.x);
return *this;
}
AlgStruct &operator-=(const AlgStruct &rhs) {
*this += -rhs;
return *this;
}
AlgStruct &operator*=(const AlgStruct &rhs) {
x = Operators::Mul(x, rhs.x);
return *this;
}
AlgStruct &operator/=(const AlgStruct &rhs) {
return *this *= rhs.reciprocal();
}
const AlgStruct operator+(const AlgStruct &rhs) const {
return AlgStruct(*this) += rhs;
}
const AlgStruct operator-(const AlgStruct &rhs) const {
return AlgStruct(*this) -= rhs;
}
const AlgStruct operator*(const AlgStruct &rhs) const {
return AlgStruct(*this) *= rhs;
}
const AlgStruct operator/(const AlgStruct &rhs) const {
return AlgStruct(*this) /= rhs;
}
const AlgStruct power(long long n) const { return Operators::Power(x, n); }
bool operator<(const AlgStruct &rhs) const {
return Operators::Less(x, rhs.x);
}
bool operator>(const AlgStruct &rhs) const { return rhs < *this; }
bool operator<=(const AlgStruct &rhs) const { return !(*this > rhs); }
bool operator>=(const AlgStruct &rhs) const { return !(*this < rhs); }
bool operator==(const AlgStruct &rhs) const {
return !(*this < rhs) && !(*this > rhs);
}
bool operator!=(const AlgStruct &rhs) const { return !(*this == rhs); }
};
template <typename Type, typename Operators>
const AlgStruct<Type, Operators>
operator+(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) + rhs;
}
template <typename Type, typename Operators>
const AlgStruct<Type, Operators>
operator-(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) - rhs;
}
template <typename Type, typename Operators>
const AlgStruct<Type, Operators>
operator*(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) * rhs;
}
template <typename Type, typename Operators>
const AlgStruct<Type, Operators>
operator/(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) / rhs;
}
template <typename Type, typename Operators>
bool operator<(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) < rhs;
}
template <typename Type, typename Operators>
bool operator>(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) > rhs;
}
template <typename Type, typename Operators>
bool operator<=(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) <= rhs;
}
template <typename Type, typename Operators>
bool operator>=(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) >= rhs;
}
template <typename Type, typename Operators>
bool operator==(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) == rhs;
}
template <typename Type, typename Operators>
bool operator!=(const Type &lhs, const AlgStruct<Type, Operators> &rhs) {
return AlgStruct<Type, Operators>(lhs) != rhs;
}
template <typename Type, typename Operators>
auto &operator<<(ostream &s, const AlgStruct<Type, Operators> &value) {
s << value.x;
return s;
}
template <typename T, T mod> struct GaloisFieldOperators {
static T Add(T lhs, T rhs) { return (lhs + rhs) % mod; };
static T Mul(T lhs, T rhs) { return (lhs * rhs) % mod; };
static T IdAdd() { return 0; };
static T IdMul() { return 1; };
static T Opposite(T value) { return ((-value % mod) + mod) % mod; };
static bool Less(T lhs, T rhs) { return lhs < rhs; };
static T Power(T value, long long n) {
T res = IdMul();
while (n > 0) {
if (n & 1)
res = Mul(res, value);
value = Mul(value, value);
n >>= 1;
}
return res;
}
static T Reciprocal(T value) { return Power(value, mod - 2); }
};
using GaloisField = AlgStruct<long long, GaloisFieldOperators<long long, MOD>>;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; ++i) {
cin >> a[i];
}
vector<int> s(N + 1);
for (int i = 0; i < N; ++i) {
s[i + 1] = s[i] ^ a[i];
}
if (s[N] != 0) {
int d = s[N];
vector<LL> p;
int cnt = 0;
for (int i = 0; i < N + 1; ++i) {
if (s[i] == d) {
p.push_back(cnt);
} else if (s[i] == 0) {
++cnt;
}
}
int D = p.size();
vector<GaloisField> dp(D + 1), sum0(D + 1), sum1(D + 1);
for (int i = 0; i < D; ++i) {
dp[i + 1] = 1LL - sum1[i] + p[i] * sum0[i];
sum0[i + 1] = sum0[i] + dp[i + 1];
sum1[i + 1] = sum1[i] + dp[i + 1] * p[i];
}
GaloisField ans = dp[D];
cout << ans << endl;
}
else {
const int dmax = 1 << 20;
vector<vector<LL>> p(dmax + 1);
int cnt = 0;
for (int i = 0; i < N + 1; ++i) {
if (s[i] == 0) {
++cnt;
} else {
p[s[i]].push_back(cnt);
}
}
GaloisField ans = GaloisField(2).power(cnt - 2);
for (int d = 1; d < dmax + 1; ++d) {
int D = p[d].size();
vector<GaloisField> dp(D + 1), sum0(D + 1), sum1(D + 1);
for (int i = 0; i < D; ++i) {
dp[i + 1] = 1LL - sum1[i] + p[d][i] * sum0[i];
sum0[i + 1] = sum0[i] + dp[i + 1];
sum1[i + 1] = sum1[i] + dp[i + 1] * p[d][i];
ans += dp[i + 1];
}
}
cout << ans << endl;
}
return 0;
}
| replace | 285 | 286 | 285 | 286 | 0 | |
p03051 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define fo(i, j, l) for (int i = j; i <= l; ++i)
#define fd(i, j, l) for (int i = j; i >= l; --i)
using namespace std;
typedef long long ll;
const ll N = 12e4, mo = 1e9 + 7;
int a[N], n, b[N], c[N], d[N];
int qz[N], hz[N];
int lx[N];
int bh[N];
ll f[N];
inline bool kmp(int x, int y) {
return (c[x] != c[y]) ? c[x] < c[y]
: ((b[x] != b[y]) ? (b[x] > b[y]) : (a[x] < a[y]));
}
int main() {
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
scanf("%d", &n);
fo(i, 1, n) scanf("%d", &a[i]);
fo(i, 1, n) qz[i] = qz[i - 1] ^ a[i];
fd(i, n, 1) hz[i] = hz[i + 1] ^ a[i];
fd(i, n, 1) lx[i] = lx[i + 1] + (hz[i] == 0);
fo(i, 1, n * 2) bh[i] = i;
fo(i, 1, n) a[i] = 1, b[i] = i, c[i] = qz[i];
fo(i, 1, n) a[i + n] = 2, b[i + n] = i, c[i + n] = hz[i];
sort(bh + 1, bh + n + n + 1, kmp);
int j = 0;
b[0] = -1;
ll ans = 1;
fd(i, n * 2, 1) if (c[bh[i]] != 0) j = i - 1;
fo(i, 1, n * 2) if (c[bh[i]] != c[bh[i + 1]] && c[bh[i]] != 0) {
fo(l, j + 1, i) d[l - j] = bh[l];
int cd = i - j, sz = c[bh[i]];
ll l1 = 0, l2 = 0, k1 = 0, k2 = 0;
int las = n + 1;
fo(l, 1, cd) if (a[d[l]] == 1) {
int po = b[d[l]] + 1;
if (hz[po] != 0 && hz[po] != sz)
continue;
if (hz[po] == 0)
ans = (ans + k2) % mo;
if (hz[po] == sz)
ans = (ans + k1) % mo;
}
else {
int po = b[d[l]];
ll j2 = (l2 + l1 * (lx[po] - lx[las])) % mo;
k1 = (1 + j2) % mo;
ll j1 = (l1 + j2 + 1) % mo;
k2 = j1;
l2 = j2;
l1 = j1;
las = po;
}
j = i;
}
int gs = 0;
if (qz[n] == 0) {
fo(i, 1, n - 1) if (qz[i] == 0)++ gs;
ll lj = 1;
fo(l, 1, gs) lj = lj * 2 % mo;
ans = (ans + lj - 1) % mo;
}
cout << ans;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define fo(i, j, l) for (int i = j; i <= l; ++i)
#define fd(i, j, l) for (int i = j; i >= l; --i)
using namespace std;
typedef long long ll;
const ll N = 12e5, mo = 1e9 + 7;
int a[N], n, b[N], c[N], d[N];
int qz[N], hz[N];
int lx[N];
int bh[N];
ll f[N];
inline bool kmp(int x, int y) {
return (c[x] != c[y]) ? c[x] < c[y]
: ((b[x] != b[y]) ? (b[x] > b[y]) : (a[x] < a[y]));
}
int main() {
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
scanf("%d", &n);
fo(i, 1, n) scanf("%d", &a[i]);
fo(i, 1, n) qz[i] = qz[i - 1] ^ a[i];
fd(i, n, 1) hz[i] = hz[i + 1] ^ a[i];
fd(i, n, 1) lx[i] = lx[i + 1] + (hz[i] == 0);
fo(i, 1, n * 2) bh[i] = i;
fo(i, 1, n) a[i] = 1, b[i] = i, c[i] = qz[i];
fo(i, 1, n) a[i + n] = 2, b[i + n] = i, c[i + n] = hz[i];
sort(bh + 1, bh + n + n + 1, kmp);
int j = 0;
b[0] = -1;
ll ans = 1;
fd(i, n * 2, 1) if (c[bh[i]] != 0) j = i - 1;
fo(i, 1, n * 2) if (c[bh[i]] != c[bh[i + 1]] && c[bh[i]] != 0) {
fo(l, j + 1, i) d[l - j] = bh[l];
int cd = i - j, sz = c[bh[i]];
ll l1 = 0, l2 = 0, k1 = 0, k2 = 0;
int las = n + 1;
fo(l, 1, cd) if (a[d[l]] == 1) {
int po = b[d[l]] + 1;
if (hz[po] != 0 && hz[po] != sz)
continue;
if (hz[po] == 0)
ans = (ans + k2) % mo;
if (hz[po] == sz)
ans = (ans + k1) % mo;
}
else {
int po = b[d[l]];
ll j2 = (l2 + l1 * (lx[po] - lx[las])) % mo;
k1 = (1 + j2) % mo;
ll j1 = (l1 + j2 + 1) % mo;
k2 = j1;
l2 = j2;
l1 = j1;
las = po;
}
j = i;
}
int gs = 0;
if (qz[n] == 0) {
fo(i, 1, n - 1) if (qz[i] == 0)++ gs;
ll lj = 1;
fo(l, 1, gs) lj = lj * 2 % mo;
ans = (ans + lj - 1) % mo;
}
cout << ans;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p03052 | C++ | Runtime Error | #include <bits/stdc++.h>
#define mod 1000000007
#define mk make_pair
#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;
const int N = 21, M = 410;
int n, m, T, S, cnt[1 << N], mul[M][M], e[1 << N];
typedef pair<int, int> pii;
pii dp[1 << N];
vector<pii> G[N];
void inc(int &x, int y) {
x += y;
if (x >= mod)
x -= mod;
}
void adde(int u, int v, int w) {
G[u].pb(mk(v, w));
G[v].pb(mk(u, w));
}
int dfs(int u, int fa = 0) {
if (u == T)
return 0;
for (int i = 0, t; i < (int)G[u].size(); ++i) {
int v = G[u][i].fi, w = G[u][i].se;
if (v == fa || !~(t = dfs(v, u)))
continue;
return t | (1 << (w - 1));
}
return -1;
}
int main() {
freopen("F.in", "r", stdin);
freopen("F.out", "w", stdout);
scanf("%d%d", &n, &m);
int all = 1 << (n - 1), tot = m - n + 1;
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
adde(u, v, i);
}
for (int i = 1; i <= tot; ++i) {
int u, v, t;
scanf("%d%d", &u, &v);
T = v;
t = dfs(u);
e[(all - 1) ^ t]++;
}
for (int i = 1; i < n; ++i)
for (int j = 0; j < all; ++j)
if (j >> (i - 1) & 1) {
e[j ^ (1 << (i - 1))] += e[j];
}
for (int i = 0; i < all; ++i) {
cnt[i] = cnt[i >> 1] + (i & 1);
e[i] = tot - e[i];
}
for (int i = 1; i <= m + 1; ++i) {
mul[i][i - 1] = 1;
for (int j = i; j <= m + 1; ++j) {
mul[i][j] = (ll)mul[i][j - 1] * j % mod;
}
}
dp[0] = mk(1, 0);
for (int i = 0; i < all; ++i)
for (int j = 1; j < n; ++j)
if (!(i >> (j - 1) & 1)) {
int b = cnt[i], a = b + e[i], c = dp[i].fi, s = dp[i].se;
int ni = i | (1 << (j - 1)), k = e[ni] - e[i];
c = (ll)c * mul[a + 1][a + k] % mod;
s = (ll)s * mul[a + 2][a + k + 1] % mod;
inc(s, (ll)c * (b + 1) % mod);
inc(dp[ni].fi, c);
inc(dp[ni].se, s);
}
cout << dp[all - 1].se << endl;
return 0;
} | #include <bits/stdc++.h>
#define mod 1000000007
#define mk make_pair
#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;
const int N = 21, M = 410;
int n, m, T, S, cnt[1 << N], mul[M][M], e[1 << N];
typedef pair<int, int> pii;
pii dp[1 << N];
vector<pii> G[N];
void inc(int &x, int y) {
x += y;
if (x >= mod)
x -= mod;
}
void adde(int u, int v, int w) {
G[u].pb(mk(v, w));
G[v].pb(mk(u, w));
}
int dfs(int u, int fa = 0) {
if (u == T)
return 0;
for (int i = 0, t; i < (int)G[u].size(); ++i) {
int v = G[u][i].fi, w = G[u][i].se;
if (v == fa || !~(t = dfs(v, u)))
continue;
return t | (1 << (w - 1));
}
return -1;
}
int main() {
// freopen("F.in","r",stdin);
// freopen("F.out","w",stdout);
scanf("%d%d", &n, &m);
int all = 1 << (n - 1), tot = m - n + 1;
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
adde(u, v, i);
}
for (int i = 1; i <= tot; ++i) {
int u, v, t;
scanf("%d%d", &u, &v);
T = v;
t = dfs(u);
e[(all - 1) ^ t]++;
}
for (int i = 1; i < n; ++i)
for (int j = 0; j < all; ++j)
if (j >> (i - 1) & 1) {
e[j ^ (1 << (i - 1))] += e[j];
}
for (int i = 0; i < all; ++i) {
cnt[i] = cnt[i >> 1] + (i & 1);
e[i] = tot - e[i];
}
for (int i = 1; i <= m + 1; ++i) {
mul[i][i - 1] = 1;
for (int j = i; j <= m + 1; ++j) {
mul[i][j] = (ll)mul[i][j - 1] * j % mod;
}
}
dp[0] = mk(1, 0);
for (int i = 0; i < all; ++i)
for (int j = 1; j < n; ++j)
if (!(i >> (j - 1) & 1)) {
int b = cnt[i], a = b + e[i], c = dp[i].fi, s = dp[i].se;
int ni = i | (1 << (j - 1)), k = e[ni] - e[i];
c = (ll)c * mul[a + 1][a + k] % mod;
s = (ll)s * mul[a + 2][a + k + 1] % mod;
inc(s, (ll)c * (b + 1) % mod);
inc(dp[ni].fi, c);
inc(dp[ni].se, s);
}
cout << dp[all - 1].se << endl;
return 0;
} | replace | 34 | 36 | 34 | 36 | -11 | |
p03052 | C++ | Time Limit Exceeded | #pragma GCC target("avx") // CPU 処理並列化
#pragma GCC optimize("O3") // CPU 処理並列化
#pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ =
// malloc(size);void * stack_extend_origin_memory_;char *
// stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long
// long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm
// volatile("mov %%rsp, %%rbx\nmov %%rax,
// %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax,
// %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <deque>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const long double EPS = 1e-9;
const long double INF = 1e+10;
const long double PI = acos(-1.0);
const int C_SIZE = 3100000;
const int UF_SIZE = 3100000;
namespace {
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
inline long long Comb(int a, int b) {
if (a < b || b < 0)
return 0;
return fact[a] * finv[b] % mod * finv[a - b] % mod;
}
void init_C(int n) {
fact[0] = finv[0] = inv[1] = 1;
for (int i = 2; i < n; i++) {
inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
}
for (int i = 1; i < n; i++) {
fact[i] = fact[i - 1] * i % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
long long pw(long long a, long long b) {
if (a < 0LL)
return 0;
if (b < 0LL)
return 0;
long long ret = 1;
while (b) {
if (b % 2)
ret = ret * a % mod;
a = a * a % mod;
b /= 2;
}
return ret;
}
long long pw_mod(long long a, long long b, long long M) {
if (a < 0LL)
return 0;
if (b < 0LL)
return 0;
long long ret = 1;
while (b) {
if (b % 2)
ret = ret * a % M;
a = a * a % M;
b /= 2;
}
return ret;
}
int pw_mod_int(int a, int b, int M) {
if (a < 0)
return 0;
if (b < 0)
return 0;
int ret = 1;
while (b) {
if (b % 2)
ret = (long long)ret * a % M;
a = (long long)a * a % M;
b /= 2;
}
return ret;
}
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
int UF[UF_SIZE];
void init_UF(int n) {
for (int i = 0; i < n; i++)
UF[i] = -1;
}
int FIND(int a) {
if (UF[a] < 0)
return a;
return UF[a] = FIND(UF[a]);
}
void UNION(int a, int b) {
a = FIND(a);
b = FIND(b);
if (a == b)
return;
if (UF[a] > UF[b])
swap(a, b);
UF[a] += UF[b];
UF[b] = a;
}
} // namespace
// ここから編集しろ
int L[310];
int R[310];
pair<long long, long long> dp[2][1 << 19];
int K[1 << 19];
int TMP[310];
vector<int> to[1 << 19];
int main() {
int a, b;
scanf("%d%d", &a, &b);
for (int i = 0; i < b; i++) {
scanf("%d%d", L + i, R + i);
L[i]--;
R[i]--;
}
int n = a - 1;
for (int i = 0; i < (1 << n); i++) {
init_UF(a);
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
UNION(L[j], R[j]);
K[i]++;
} else
to[i].push_back(1 << j);
}
for (int j = 0; j < a; j++)
TMP[j] = FIND(j);
for (int j = n; j < b; j++) {
if (TMP[L[j]] == TMP[R[j]])
K[i]++;
}
}
dp[0][0].first = 1;
for (int i = 0; i < b; i++) {
int h = i % 2;
int g = !h;
for (int j = 0; j < (1 << n); j++)
dp[g][j] = make_pair(0LL, 0LL);
for (int j = (1 << n) - 1; j >= 0; j--) {
if (!dp[h][j].first && !dp[h][j].second) {
continue;
}
if (K[j] > i) {
dp[g][j].first += dp[h][j].first * (K[j] - i);
dp[g][j].second += dp[h][j].second * (K[j] - i);
}
long long ad = dp[h][j].second + dp[h][j].first * (i + 1);
for (int k = 0; k < to[j].size(); k++) {
dp[g][j | to[j][k]].first += dp[h][j].first;
dp[g][j | to[j][k]].second += ad;
}
}
for (int j = 0; j < (1 << n); j++) {
dp[g][j].first %= mod;
dp[g][j].second %= mod;
}
}
printf("%lld\n", dp[b % 2][(1 << n) - 1].second % mod);
} | #pragma GCC target("avx") // CPU 処理並列化
#pragma GCC optimize("O3") // CPU 処理並列化
#pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ =
// malloc(size);void * stack_extend_origin_memory_;char *
// stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long
// long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm
// volatile("mov %%rsp, %%rbx\nmov %%rax,
// %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax,
// %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <deque>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const long double EPS = 1e-9;
const long double INF = 1e+10;
const long double PI = acos(-1.0);
const int C_SIZE = 3100000;
const int UF_SIZE = 3100000;
namespace {
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
inline long long Comb(int a, int b) {
if (a < b || b < 0)
return 0;
return fact[a] * finv[b] % mod * finv[a - b] % mod;
}
void init_C(int n) {
fact[0] = finv[0] = inv[1] = 1;
for (int i = 2; i < n; i++) {
inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
}
for (int i = 1; i < n; i++) {
fact[i] = fact[i - 1] * i % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
long long pw(long long a, long long b) {
if (a < 0LL)
return 0;
if (b < 0LL)
return 0;
long long ret = 1;
while (b) {
if (b % 2)
ret = ret * a % mod;
a = a * a % mod;
b /= 2;
}
return ret;
}
long long pw_mod(long long a, long long b, long long M) {
if (a < 0LL)
return 0;
if (b < 0LL)
return 0;
long long ret = 1;
while (b) {
if (b % 2)
ret = ret * a % M;
a = a * a % M;
b /= 2;
}
return ret;
}
int pw_mod_int(int a, int b, int M) {
if (a < 0)
return 0;
if (b < 0)
return 0;
int ret = 1;
while (b) {
if (b % 2)
ret = (long long)ret * a % M;
a = (long long)a * a % M;
b /= 2;
}
return ret;
}
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
int UF[UF_SIZE];
void init_UF(int n) {
for (int i = 0; i < n; i++)
UF[i] = -1;
}
int FIND(int a) {
if (UF[a] < 0)
return a;
return UF[a] = FIND(UF[a]);
}
void UNION(int a, int b) {
a = FIND(a);
b = FIND(b);
if (a == b)
return;
if (UF[a] > UF[b])
swap(a, b);
UF[a] += UF[b];
UF[b] = a;
}
} // namespace
// ここから編集しろ
int L[310];
int R[310];
pair<long long, long long> dp[2][1 << 19];
int K[1 << 19];
int TMP[310];
vector<int> to[1 << 19];
int main() {
int a, b;
scanf("%d%d", &a, &b);
for (int i = 0; i < b; i++) {
scanf("%d%d", L + i, R + i);
L[i]--;
R[i]--;
}
int n = a - 1;
for (int i = 0; i < (1 << n); i++) {
init_UF(a);
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
UNION(L[j], R[j]);
K[i]++;
} else
to[i].push_back(1 << j);
}
for (int j = 0; j < a; j++)
TMP[j] = FIND(j);
for (int j = n; j < b; j++) {
if (TMP[L[j]] == TMP[R[j]])
K[i]++;
}
}
dp[0][0].first = 1;
for (int i = 0; i < b; i++) {
int h = i % 2;
int g = !h;
for (int j = 0; j < (1 << n); j++)
dp[g][j] = make_pair(0LL, 0LL);
for (int j = (1 << n) - 1; j >= 0; j--) {
if (!dp[h][j].first && !dp[h][j].second) {
continue;
}
if (K[j] > i) {
dp[g][j].first += dp[h][j].first * (K[j] - i);
dp[g][j].second += dp[h][j].second * (K[j] - i);
}
long long ad = dp[h][j].second + dp[h][j].first * (i + 1);
for (int k = 0; k < to[j].size(); k++) {
dp[g][j | to[j][k]].first += dp[h][j].first;
dp[g][j | to[j][k]].second += ad;
}
}
for (int j = 0; j < (1 << n); j++) {
if (dp[g][j].first >= mod)
dp[g][j].first %= mod;
if (dp[g][j].second >= mod)
dp[g][j].second %= mod;
}
}
printf("%lld\n", dp[b % 2][(1 << n) - 1].second % mod);
} | replace | 180 | 182 | 180 | 184 | TLE | |
p03052 | C++ | Runtime Error | #include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif
namespace IO {
static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;
inline char myGetchar() {
static char buf[IN_BUF], *ps = buf, *pt = buf;
if (ps == pt) {
ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
}
return ps == pt ? EOF : *ps++;
}
template <typename T> inline bool read(T &x) {
bool op = 0;
char ch = getchar();
x = 0;
for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
op ^= (ch == '-');
}
if (ch == EOF) {
return false;
}
for (; isdigit(ch); ch = getchar()) {
x = x * 10 + (ch ^ '0');
}
if (op) {
x = -x;
}
return true;
}
inline int readStr(char *s) {
int n = 0;
char ch = getchar();
for (; isspace(ch) && ch != EOF; ch = getchar())
;
for (; !isspace(ch) && ch != EOF; ch = getchar()) {
s[n++] = ch;
}
s[n] = '\0';
return n;
}
inline void myPutchar(char x) {
static char pbuf[OUT_BUF], *pp = pbuf;
struct _flusher {
~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); }
};
static _flusher outputFlusher;
if (pp == pbuf + OUT_BUF) {
fwrite(pbuf, 1, OUT_BUF, stdout);
pp = pbuf;
}
*pp++ = x;
}
template <typename T> inline void print_(T x) {
if (x == 0) {
putchar('0');
return;
}
static int num[40];
if (x < 0) {
putchar('-');
x = -x;
}
for (*num = 0; x; x /= 10) {
num[++*num] = x % 10;
}
while (*num) {
putchar(num[*num] ^ '0');
--*num;
}
}
template <typename T> inline void print(T x, char ch = '\n') {
print_(x);
putchar(ch);
}
inline void printStr_(const char *s, int n = -1) {
if (n == -1) {
n = strlen(s);
}
for (int i = 0; i < n; ++i) {
putchar(s[i]);
}
}
inline void printStr(const char *s, int n = -1, char ch = '\n') {
printStr_(s, n);
putchar(ch);
}
} // namespace IO
using namespace IO;
const int N = 18, M = N * (N - 1) / 2, P = 1000000007;
int n, m;
struct Edge {
int u, v;
} E[M];
struct DSU {
int fa[N];
void init() {
for (int i = 0; i < n; ++i) {
fa[i] = i;
}
}
int find(int x) { return fa[x] == x ? x : (fa[x] = find(fa[x])); }
bool merge(int x, int y) {
x = find(x), y = find(y);
if (x == y) {
return true;
}
fa[y] = x;
return false;
}
} D;
int qpow(int a, int b) {
int s = 1;
for (; b; b >>= 1) {
if (b & 1) {
s = 1ll * s * a % P;
}
a = 1ll * a * a % P;
}
return s;
}
int fac[M + 1], inv[M + 1];
void init(int n) {
fac[0] = 1;
for (int i = 1; i <= n; ++i) {
fac[i] = 1ll * fac[i - 1] * i % P;
}
inv[n] = qpow(fac[n], P - 2);
for (int i = n; i; --i) {
inv[i - 1] = 1ll * inv[i] * i % P;
}
}
struct State {
int n, b, c, s;
} f[1 << (N - 1)];
int main() {
read(n), read(m);
for (int i = 0; i < m; ++i) {
read(E[i].u), read(E[i].v);
--E[i].u, --E[i].v;
}
for (int S = 0; S < (1 << (n - 1)); ++S) {
D.init();
f[S].b = n - 1, f[S].n = m;
for (int i = 0; i < n - 1; ++i) {
if (S >> i & 1) {
D.merge(E[i].u, E[i].v);
--f[S].b, --f[S].n;
}
}
for (int i = n - 1; i < m; ++i) {
if (D.find(E[i].u) == D.find(E[i].v)) {
--f[S].n;
}
}
}
init(m);
int U = (1 << (n - 1)) - 1;
f[U].c = 1, f[U].s = 0;
for (int S = U - 1; ~S; --S) {
for (int i = 0; i < n - 1; ++i) {
if (!(S >> i & 1)) {
int T = S | (1 << i);
int k = f[S].n - f[T].n - 1;
int c = 1ll * f[T].c * fac[f[T].n + k] % P * inv[f[T].n] % P;
int s = 1ll * f[T].s * fac[f[T].n + k + 1] % P * inv[f[T].n + 1] % P;
s = (s + 1ll * f[S].b * c) % P;
f[S].c = (f[S].c + c) % P;
f[S].s = (f[S].s + s) % P;
}
}
}
print(f[0].s);
}
| #include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif
namespace IO {
static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;
inline char myGetchar() {
static char buf[IN_BUF], *ps = buf, *pt = buf;
if (ps == pt) {
ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
}
return ps == pt ? EOF : *ps++;
}
template <typename T> inline bool read(T &x) {
bool op = 0;
char ch = getchar();
x = 0;
for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
op ^= (ch == '-');
}
if (ch == EOF) {
return false;
}
for (; isdigit(ch); ch = getchar()) {
x = x * 10 + (ch ^ '0');
}
if (op) {
x = -x;
}
return true;
}
inline int readStr(char *s) {
int n = 0;
char ch = getchar();
for (; isspace(ch) && ch != EOF; ch = getchar())
;
for (; !isspace(ch) && ch != EOF; ch = getchar()) {
s[n++] = ch;
}
s[n] = '\0';
return n;
}
inline void myPutchar(char x) {
static char pbuf[OUT_BUF], *pp = pbuf;
struct _flusher {
~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); }
};
static _flusher outputFlusher;
if (pp == pbuf + OUT_BUF) {
fwrite(pbuf, 1, OUT_BUF, stdout);
pp = pbuf;
}
*pp++ = x;
}
template <typename T> inline void print_(T x) {
if (x == 0) {
putchar('0');
return;
}
static int num[40];
if (x < 0) {
putchar('-');
x = -x;
}
for (*num = 0; x; x /= 10) {
num[++*num] = x % 10;
}
while (*num) {
putchar(num[*num] ^ '0');
--*num;
}
}
template <typename T> inline void print(T x, char ch = '\n') {
print_(x);
putchar(ch);
}
inline void printStr_(const char *s, int n = -1) {
if (n == -1) {
n = strlen(s);
}
for (int i = 0; i < n; ++i) {
putchar(s[i]);
}
}
inline void printStr(const char *s, int n = -1, char ch = '\n') {
printStr_(s, n);
putchar(ch);
}
} // namespace IO
using namespace IO;
const int N = 20, M = N * (N - 1) / 2, P = 1000000007;
int n, m;
struct Edge {
int u, v;
} E[M];
struct DSU {
int fa[N];
void init() {
for (int i = 0; i < n; ++i) {
fa[i] = i;
}
}
int find(int x) { return fa[x] == x ? x : (fa[x] = find(fa[x])); }
bool merge(int x, int y) {
x = find(x), y = find(y);
if (x == y) {
return true;
}
fa[y] = x;
return false;
}
} D;
int qpow(int a, int b) {
int s = 1;
for (; b; b >>= 1) {
if (b & 1) {
s = 1ll * s * a % P;
}
a = 1ll * a * a % P;
}
return s;
}
int fac[M + 1], inv[M + 1];
void init(int n) {
fac[0] = 1;
for (int i = 1; i <= n; ++i) {
fac[i] = 1ll * fac[i - 1] * i % P;
}
inv[n] = qpow(fac[n], P - 2);
for (int i = n; i; --i) {
inv[i - 1] = 1ll * inv[i] * i % P;
}
}
struct State {
int n, b, c, s;
} f[1 << (N - 1)];
int main() {
read(n), read(m);
for (int i = 0; i < m; ++i) {
read(E[i].u), read(E[i].v);
--E[i].u, --E[i].v;
}
for (int S = 0; S < (1 << (n - 1)); ++S) {
D.init();
f[S].b = n - 1, f[S].n = m;
for (int i = 0; i < n - 1; ++i) {
if (S >> i & 1) {
D.merge(E[i].u, E[i].v);
--f[S].b, --f[S].n;
}
}
for (int i = n - 1; i < m; ++i) {
if (D.find(E[i].u) == D.find(E[i].v)) {
--f[S].n;
}
}
}
init(m);
int U = (1 << (n - 1)) - 1;
f[U].c = 1, f[U].s = 0;
for (int S = U - 1; ~S; --S) {
for (int i = 0; i < n - 1; ++i) {
if (!(S >> i & 1)) {
int T = S | (1 << i);
int k = f[S].n - f[T].n - 1;
int c = 1ll * f[T].c * fac[f[T].n + k] % P * inv[f[T].n] % P;
int s = 1ll * f[T].s * fac[f[T].n + k + 1] % P * inv[f[T].n + 1] % P;
s = (s + 1ll * f[S].b * c) % P;
f[S].c = (f[S].c + c) % P;
f[S].s = (f[S].s + s) % P;
}
}
}
print(f[0].s);
}
| replace | 104 | 105 | 104 | 105 | 0 | |
p03052 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define ll long long
using namespace std;
const int N = 25, M = (1 << 20) + 10, mod = 1e9 + 7;
int n, m, len, a[M], cnt, head[N], x, y, val[N], f[2][M], g[2][M];
struct edge {
int to, nxt, i;
} e[N * N];
inline void upd(int &x, int y) {
x += y;
x -= x >= mod ? mod : 0;
}
void adde(int x, int y, int i) {
e[++cnt].to = y;
e[cnt].nxt = head[x];
head[x] = cnt;
e[cnt].i = i;
}
void dfs(int u, int par, int st) {
val[u] = st;
for (int i = head[u], v; i; i = e[i].nxt)
if (v = e[i].to, v != par)
dfs(v, u, st | (1 << (e[i].i - 1)));
}
void fmt(int *a) {
rep(i, 0, len - 1) rep(j, 0, (1 << len) - 1) if (!(j >> i & 1))
a[j | 1 << i] += a[j];
}
int main() {
scanf("%d%d", &n, &m);
len = n - 1;
rep(i, 1, n - 1) scanf("%d%d", &x, &y), adde(x, y, i), adde(y, x, i);
dfs(1, 0, 0);
rep(i, n, m) scanf("%d%d", &x, &y), a[val[x] ^ val[y]]++;
fmt(a);
int now = 0, tmp;
f[0][0] = 1;
rep(i, 1, m) {
now ^= 1;
memset(f[now], 0, sizeof(f[now]));
memset(g[now], 0, sizeof(g[now]));
rep(j, 0, (1 << len) - 1) if (tmp = f[now ^ 1][j], tmp) {
rep(k, 0, len - 1) if (!(j >> k & 1)) {
upd(f[now][j | (1 << k)], tmp);
upd(g[now][j | (1 << k)], (g[now ^ 1][j] + (ll)tmp * i % mod) % mod);
}
upd(f[now][j], (ll)tmp * (a[j] - (i - 1 - __builtin_popcount(j))) % mod);
upd(g[now][j],
(ll)g[now ^ 1][j] * (a[j] - (i - 1 - __builtin_popcount(j))) % mod);
}
}
printf("%d\n", g[now][(1 << len) - 1]);
return 0;
} | #include <bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define ll long long
using namespace std;
const int N = 25, M = (1 << 20) + 10, mod = 1e9 + 7;
int n, m, len, a[M], cnt, head[N], x, y, val[N], f[2][M], g[2][M];
struct edge {
int to, nxt, i;
} e[N * N];
inline void upd(int &x, int y) {
x += y;
x -= x >= mod ? mod : 0;
}
void adde(int x, int y, int i) {
e[++cnt].to = y;
e[cnt].nxt = head[x];
head[x] = cnt;
e[cnt].i = i;
}
void dfs(int u, int par, int st) {
val[u] = st;
for (int i = head[u], v; i; i = e[i].nxt)
if (v = e[i].to, v != par)
dfs(v, u, st | (1 << (e[i].i - 1)));
}
void fmt(int *a) {
rep(i, 0, len - 1) rep(j, 0, (1 << len) - 1) if (!(j >> i & 1))
a[j | 1 << i] += a[j];
}
int main() {
scanf("%d%d", &n, &m);
len = n - 1;
rep(i, 1, n - 1) scanf("%d%d", &x, &y), adde(x, y, i), adde(y, x, i);
dfs(1, 0, 0);
rep(i, n, m) scanf("%d%d", &x, &y), a[val[x] ^ val[y]]++;
fmt(a);
int now = 0, tmp;
f[0][0] = 1;
rep(i, 1, m) {
now ^= 1;
memset(f[now], 0, sizeof(f[now]));
memset(g[now], 0, sizeof(g[now]));
rep(j, 0, (1 << len) - 1) if (tmp = f[now ^ 1][j], tmp) {
int s = ((1 << len) - 1) ^ j;
for (int r = s; r; r -= r & -r) {
int k = r & -r;
if (!(j & k)) {
upd(f[now][j | k], tmp);
upd(g[now][j | k], (g[now ^ 1][j] + (ll)tmp * i % mod) % mod);
}
}
upd(f[now][j], (ll)tmp * (a[j] - (i - 1 - __builtin_popcount(j))) % mod);
upd(g[now][j],
(ll)g[now ^ 1][j] * (a[j] - (i - 1 - __builtin_popcount(j))) % mod);
}
}
printf("%d\n", g[now][(1 << len) - 1]);
return 0;
} | replace | 50 | 53 | 50 | 57 | TLE | |
p03052 | C++ | Runtime Error | /* cerberus97 - Hanit Banga */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define pb push_back
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 25, M = 200, S = (1 << 19) + 5;
const int mod = 1e9 + 7;
vector<pii> g[N];
int u[N], v[N], depth[N], par[N], up[N];
int dep[M];
int ctr[S];
ll ways[S], sum[S];
ll prod[M][M];
void dfs(int u, int p);
int main() {
// cout << (2 * sizeof(ways)) / 1e6 << endl;
// return 0;
fast_cin();
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> u[i] >> v[i];
if (i < n - 1) {
g[u[i]].pb({v[i], i});
g[v[i]].pb({u[i], i});
}
}
dfs(1, 0);
for (int i = n - 1; i < m; ++i) {
int eu = u[i], ev = v[i];
if (depth[eu] < depth[ev]) {
swap(eu, ev);
}
while (depth[eu] > depth[ev]) {
dep[i] |= (1 << up[eu]);
eu = par[eu];
}
while (eu != ev) {
dep[i] |= (1 << up[eu]);
dep[i] |= (1 << up[ev]);
eu = par[eu];
ev = par[ev];
}
}
for (int i = 1; i < M; ++i) {
prod[i][i - 1] = 1;
for (int j = i; j < M; ++j) {
prod[i][j] = prod[i][j - 1] * j;
prod[i][j] %= mod;
}
}
int subsets = (1 << (n - 1));
for (int mask = 0; mask < subsets; ++mask) {
// ctr[mask] = __builtin_popcount(mask);
for (int i = n - 1; i < m; ++i) {
if (dep[i] & mask) {
++ctr[mask];
}
}
}
ways[0] = 1;
for (int mask = 0; mask < subsets; ++mask) {
// cout << mask << ' ' << ways[mask] << ' ' << sum[mask] << ' ' << ctr[mask]
// << endl;
int b = __builtin_popcount(mask), tot = ctr[mask] + b;
for (int i = 0; i < n - 1; ++i) {
if (mask & (1 << i)) {
continue;
}
int new_mask = mask | (1 << i);
int k = ctr[new_mask] - ctr[mask];
ll temp = prod[tot + 1][tot + k] * ways[mask];
temp %= mod;
ways[new_mask] += temp;
ways[new_mask] %= mod;
sum[new_mask] += prod[tot + 2][tot + k + 1] * sum[mask];
sum[new_mask] += temp * (b + 1);
sum[new_mask] %= mod;
}
}
cout << sum[subsets - 1] << endl;
}
void dfs(int u, int p) {
for (auto &e : g[u]) {
int v = e.first, id = e.second;
if (v != p) {
depth[v] = depth[u] + 1;
par[v] = u;
up[v] = id;
dfs(v, u);
}
}
} | /* cerberus97 - Hanit Banga */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define pb push_back
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 25, M = 200, S = (1 << 19) + 5;
const int mod = 1e9 + 7;
vector<pii> g[N];
int u[M], v[M], depth[N], par[N], up[N];
int dep[M];
int ctr[S];
ll ways[S], sum[S];
ll prod[M][M];
void dfs(int u, int p);
int main() {
// cout << (2 * sizeof(ways)) / 1e6 << endl;
// return 0;
fast_cin();
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> u[i] >> v[i];
if (i < n - 1) {
g[u[i]].pb({v[i], i});
g[v[i]].pb({u[i], i});
}
}
dfs(1, 0);
for (int i = n - 1; i < m; ++i) {
int eu = u[i], ev = v[i];
if (depth[eu] < depth[ev]) {
swap(eu, ev);
}
while (depth[eu] > depth[ev]) {
dep[i] |= (1 << up[eu]);
eu = par[eu];
}
while (eu != ev) {
dep[i] |= (1 << up[eu]);
dep[i] |= (1 << up[ev]);
eu = par[eu];
ev = par[ev];
}
}
for (int i = 1; i < M; ++i) {
prod[i][i - 1] = 1;
for (int j = i; j < M; ++j) {
prod[i][j] = prod[i][j - 1] * j;
prod[i][j] %= mod;
}
}
int subsets = (1 << (n - 1));
for (int mask = 0; mask < subsets; ++mask) {
// ctr[mask] = __builtin_popcount(mask);
for (int i = n - 1; i < m; ++i) {
if (dep[i] & mask) {
++ctr[mask];
}
}
}
ways[0] = 1;
for (int mask = 0; mask < subsets; ++mask) {
// cout << mask << ' ' << ways[mask] << ' ' << sum[mask] << ' ' << ctr[mask]
// << endl;
int b = __builtin_popcount(mask), tot = ctr[mask] + b;
for (int i = 0; i < n - 1; ++i) {
if (mask & (1 << i)) {
continue;
}
int new_mask = mask | (1 << i);
int k = ctr[new_mask] - ctr[mask];
ll temp = prod[tot + 1][tot + k] * ways[mask];
temp %= mod;
ways[new_mask] += temp;
ways[new_mask] %= mod;
sum[new_mask] += prod[tot + 2][tot + k + 1] * sum[mask];
sum[new_mask] += temp * (b + 1);
sum[new_mask] %= mod;
}
}
cout << sum[subsets - 1] << endl;
}
void dfs(int u, int p) {
for (auto &e : g[u]) {
int v = e.first, id = e.second;
if (v != p) {
depth[v] = depth[u] + 1;
par[v] = u;
up[v] = id;
dfs(v, u);
}
}
} | replace | 32 | 33 | 32 | 33 | 0 | |
p03053 | C++ | Time Limit Exceeded | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define lfs cout << fixed << setprecision(10)
#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i, n, m) for (ll i = (n); i < (ll)(m); i++)
#define rrep(i, n, m) for (ll i = (ll)(m)-1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9 + 7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
ll median(ll a, ll b, ll c) {
return a + b + c - max({a, b, c}) - min({a, b, c});
}
void ans1(bool x) {
if (x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void ans2(bool x) {
if (x)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void ans3(bool x) {
if (x)
cout << "Yay!" << endl;
else
cout << ":(" << endl;
}
template <typename T1, typename T2> void ans(bool x, T1 y, T2 z) {
if (x)
cout << y << endl;
else
cout << z << endl;
}
template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
cout << v[i][0];
for (ll j = 1; j < w; j++)
cout spa v[i][j];
cout << endl;
}
};
void debug(vector<string> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
for (ll j = 0; j < w; j++)
cout << v[i][j];
cout << endl;
}
};
template <typename T> void debug(vector<T> &v, ll n) {
if (n != 0)
cout << v[0];
for (ll i = 1; i < n; i++)
cout spa v[i];
cout << endl;
};
template <typename T> vector<vector<T>> vec(ll x, ll y, T w) {
vector<vector<T>> v(x, vector<T>(y, w));
return v;
}
ll gcd(ll x, ll y) {
ll r;
while (y != 0 && (r = x % y) != 0) {
x = y;
y = r;
}
return y == 0 ? x : y;
}
vector<ll> dx = {1, 0, -1, 0, 1, 1, -1, -1};
vector<ll> dy = {0, 1, 0, -1, 1, -1, 1, -1};
template <typename T> vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << p.first << " " << p.second;
}
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
struct z {
ll x, y, d;
z(ll a, ll b, ll c) : x(a), y(b), d(c){};
};
vector<vector<ll>> dist(1005, vector<ll>(1005, INF));
void bfs(ll h, ll w, ll sx, ll sy, vector<string> &a) {
queue<struct z> que;
que.emplace(sx, sy, 0);
while (!que.empty()) {
auto pos = que.front();
que.pop();
if (dist[pos.x][pos.y] <= pos.d)
continue;
dist[pos.x][pos.y] = pos.d;
for (ll i = 0; i < 4; i++) {
ll x = pos.x + dx[i], y = pos.y + dy[i], d = pos.d + 1;
if (x < 0 || x >= h || y < 0 || y >= w)
continue;
que.emplace(x, y, d);
}
}
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
ll res = 0, buf = 0;
bool judge = true;
ll h, w;
cin >> h >> w;
vector<string> a(h);
rep(i, 0, h) cin >> a[i];
rep(i, 0, h) rep(j, 0, w) {
if (a[i][j] == '#')
bfs(h, w, i, j, a);
}
rep(i, 0, h) rep(j, 0, w) chmax(res, dist[i][j]);
cout << res << endl;
return 0;
} | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define lfs cout << fixed << setprecision(10)
#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i, n, m) for (ll i = (n); i < (ll)(m); i++)
#define rrep(i, n, m) for (ll i = (ll)(m)-1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9 + 7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
ll median(ll a, ll b, ll c) {
return a + b + c - max({a, b, c}) - min({a, b, c});
}
void ans1(bool x) {
if (x)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void ans2(bool x) {
if (x)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void ans3(bool x) {
if (x)
cout << "Yay!" << endl;
else
cout << ":(" << endl;
}
template <typename T1, typename T2> void ans(bool x, T1 y, T2 z) {
if (x)
cout << y << endl;
else
cout << z << endl;
}
template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
cout << v[i][0];
for (ll j = 1; j < w; j++)
cout spa v[i][j];
cout << endl;
}
};
void debug(vector<string> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
for (ll j = 0; j < w; j++)
cout << v[i][j];
cout << endl;
}
};
template <typename T> void debug(vector<T> &v, ll n) {
if (n != 0)
cout << v[0];
for (ll i = 1; i < n; i++)
cout spa v[i];
cout << endl;
};
template <typename T> vector<vector<T>> vec(ll x, ll y, T w) {
vector<vector<T>> v(x, vector<T>(y, w));
return v;
}
ll gcd(ll x, ll y) {
ll r;
while (y != 0 && (r = x % y) != 0) {
x = y;
y = r;
}
return y == 0 ? x : y;
}
vector<ll> dx = {1, 0, -1, 0, 1, 1, -1, -1};
vector<ll> dy = {0, 1, 0, -1, 1, -1, 1, -1};
template <typename T> vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << p.first << " " << p.second;
}
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
struct z {
ll x, y, d;
z(ll a, ll b, ll c) : x(a), y(b), d(c){};
};
vector<vector<ll>> dist(1005, vector<ll>(1005, INF));
void bfs(ll h, ll w, ll sx, ll sy, vector<string> &a) {
queue<struct z> que;
que.emplace(sx, sy, 0);
while (!que.empty()) {
auto pos = que.front();
que.pop();
if (dist[pos.x][pos.y] <= pos.d)
continue;
dist[pos.x][pos.y] = pos.d;
for (ll i = 0; i < 4; i++) {
ll x = pos.x + dx[i], y = pos.y + dy[i], d = pos.d + 1;
if (x < 0 || x >= h || y < 0 || y >= w)
continue;
que.emplace(x, y, d);
}
}
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
ll res = 0, buf = 0;
bool judge = true;
ll h, w;
cin >> h >> w;
vector<string> a(h);
rep(i, 0, h) cin >> a[i];
vector<ll> v(h * w);
iota(ALL(v), 0);
shuffle(ALL(v), mt);
for (auto x : v) {
ll i = x / w, j = x % w;
// cout<<i spa j<<endl;
if (a[i][j] == '#')
bfs(h, w, i, j, a);
}
rep(i, 0, h) rep(j, 0, w) chmax(res, dist[i][j]);
cout << res << endl;
return 0;
} | replace | 143 | 144 | 143 | 149 | TLE | |
p03053 | C++ | Runtime Error | #include <cassert>
#include <climits>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
class Point {
public:
int x;
int y;
int v;
Point(int _x, int _y, int _v) {
x = _x;
y = _y;
v = _v;
}
Point(const Point &p) {
x = p.x;
y = p.y;
v = p.v;
}
};
int main() {
int h, w;
scanf("%d %d\n", &h, &w);
vector<vector<int>> board(h, vector<int>(w, INT_MAX));
queue<Point> q;
for (int x = 0; x < h; x++) {
for (int y = 0; y < h; y++) {
char ch = getchar();
assert(ch == '#' || ch == '.');
if (ch == '#') {
q.push(Point(x, y, 0));
}
}
getchar(); // discard LF
}
int res = 0;
while (!q.empty()) {
Point p(q.front());
q.pop();
if (p.x < 0 || h <= p.x || p.y < 0 || w <= p.y) {
continue;
}
if (board.at(p.x).at(p.y) <= p.v) {
continue;
}
board.at(p.x).at(p.y) = p.v;
res = max(res, p.v);
q.push(Point(p.x - 1, p.y, p.v + 1));
q.push(Point(p.x + 1, p.y, p.v + 1));
q.push(Point(p.x, p.y - 1, p.v + 1));
q.push(Point(p.x, p.y + 1, p.v + 1));
}
printf("%d\n", res);
return 0;
}
| #include <cassert>
#include <climits>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
class Point {
public:
int x;
int y;
int v;
Point(int _x, int _y, int _v) {
x = _x;
y = _y;
v = _v;
}
Point(const Point &p) {
x = p.x;
y = p.y;
v = p.v;
}
};
int main() {
int h, w;
scanf("%d %d\n", &h, &w);
vector<vector<int>> board(h, vector<int>(w, INT_MAX));
queue<Point> q;
for (int x = 0; x < h; x++) {
for (int y = 0; y < w; y++) {
char ch = getchar();
assert(ch == '#' || ch == '.');
if (ch == '#') {
q.push(Point(x, y, 0));
}
}
getchar(); // discard LF
}
int res = 0;
while (!q.empty()) {
Point p(q.front());
q.pop();
if (p.x < 0 || h <= p.x || p.y < 0 || w <= p.y) {
continue;
}
if (board.at(p.x).at(p.y) <= p.v) {
continue;
}
board.at(p.x).at(p.y) = p.v;
res = max(res, p.v);
q.push(Point(p.x - 1, p.y, p.v + 1));
q.push(Point(p.x + 1, p.y, p.v + 1));
q.push(Point(p.x, p.y - 1, p.v + 1));
q.push(Point(p.x, p.y + 1, p.v + 1));
}
printf("%d\n", res);
return 0;
}
| replace | 31 | 32 | 31 | 32 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> hw(h, vector<char>(w));
int reach_cnt[h][w];
queue<pair<int, int>> que;
rep(i, h) rep(j, w) {
cin >> hw[i][j];
reach_cnt[i][j] = -1;
if (hw[i][j] == '#') {
que.push(make_pair(i, j));
reach_cnt[i][j] = 0;
}
}
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ans = 0;
while (!que.empty()) {
int size = que.size();
// cout << "size " << size << endl;
rep(rj, size) {
pair<int, int> now_p = que.front();
que.pop();
rep(ri, 4) {
int next_y = now_p.first + dy[ri];
int next_x = now_p.second + dx[ri];
if (next_y >= h || next_y < 0 || next_x >= w || next_x < 0) {
continue;
}
if (reach_cnt[next_y][next_x] != -1) {
continue;
}
reach_cnt[next_y][next_x] = reach_cnt[now_p.first][now_p.second] + 1;
// cout << "test" << endl;
// hw[next_y][next_x] = '#';
que.push(make_pair(next_y, next_x));
ans = max(ans, reach_cnt[next_y][next_x]);
}
}
bool end_flg = 1;
rep(i, h) {
rep(j, w) {
// cout << hw[i][j];
// cout << reach_flg[i][j] << " ";
if (reach_cnt[i][j] == -1) {
end_flg = 0;
}
}
// cout << endl;
}
if (end_flg)
break;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> hw(h, vector<char>(w));
int reach_cnt[h][w];
queue<pair<int, int>> que;
rep(i, h) rep(j, w) {
cin >> hw[i][j];
reach_cnt[i][j] = -1;
if (hw[i][j] == '#') {
que.push(make_pair(i, j));
reach_cnt[i][j] = 0;
}
}
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ans = 0;
while (!que.empty()) {
int size = que.size();
// cout << "size " << size << endl;
rep(rj, size) {
pair<int, int> now_p = que.front();
que.pop();
rep(ri, 4) {
int next_y = now_p.first + dy[ri];
int next_x = now_p.second + dx[ri];
if (next_y >= h || next_y < 0 || next_x >= w || next_x < 0) {
continue;
}
if (reach_cnt[next_y][next_x] != -1) {
continue;
}
reach_cnt[next_y][next_x] = reach_cnt[now_p.first][now_p.second] + 1;
// cout << "test" << endl;
// hw[next_y][next_x] = '#';
que.push(make_pair(next_y, next_x));
ans = max(ans, reach_cnt[next_y][next_x]);
}
}
// bool end_flg = 1;
// rep(i, h) {
// rep(j, w) {
//// cout << hw[i][j];
//// cout << reach_flg[i][j] << " ";
// if ( reach_cnt[i][j] == -1 ) {
// end_flg = 0;
// }
// }
// cout << endl;
// }
// if ( end_flg ) break;
}
cout << ans << endl;
return 0;
}
| replace | 45 | 58 | 45 | 57 | TLE | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int H, W;
cin >> H >> W;
vector<vector<int>> data;
queue<pair<int, int>> que;
vector<pair<int, int>> diff;
diff.emplace_back(0, 1);
diff.emplace_back(1, 0);
diff.emplace_back(0, -1);
diff.emplace_back(-1, 0);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char temp;
cin >> temp;
if (temp == '#') {
// 黒マスは初期値: 0
data[i][j] = 0;
que.emplace(i, j);
} else {
// 白マスは-1
data[i][j] = -1;
}
}
}
while (!que.empty()) {
auto cur = que.front();
que.pop();
// 現在のカーソル位置の上下左右を新たにカーソル位置とする
for (auto it = diff.begin(); it != diff.end(); it++) {
// 境界値チェック
int nx = cur.first + it->first;
int ny = cur.second + it->second;
if (nx < 0 || nx >= H || ny < 0 || ny >= W)
continue;
if (data[nx][ny] == -1) {
data[nx][ny] = data[cur.first][cur.second] + 1;
que.emplace(nx, ny);
}
}
}
int max = 0;
for (auto it_H : data) {
for (auto it : it_H) {
if (max < it) {
max = it;
}
}
}
cout << max << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int H, W;
cin >> H >> W;
vector<vector<int>> data(H, vector<int>(W, -1));
queue<pair<int, int>> que;
vector<pair<int, int>> diff;
diff.emplace_back(0, 1);
diff.emplace_back(1, 0);
diff.emplace_back(0, -1);
diff.emplace_back(-1, 0);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char temp;
cin >> temp;
if (temp == '#') {
// 黒マスは初期値: 0
data[i][j] = 0;
que.emplace(i, j);
} else {
// 白マスは-1
data[i][j] = -1;
}
}
}
while (!que.empty()) {
auto cur = que.front();
que.pop();
// 現在のカーソル位置の上下左右を新たにカーソル位置とする
for (auto it = diff.begin(); it != diff.end(); it++) {
// 境界値チェック
int nx = cur.first + it->first;
int ny = cur.second + it->second;
if (nx < 0 || nx >= H || ny < 0 || ny >= W)
continue;
if (data[nx][ny] == -1) {
data[nx][ny] = data[cur.first][cur.second] + 1;
que.emplace(nx, ny);
}
}
}
int max = 0;
for (auto it_H : data) {
for (auto it : it_H) {
if (max < it) {
max = it;
}
}
}
cout << max << endl;
return 0;
} | replace | 6 | 7 | 6 | 8 | -11 | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007
using namespace std;
typedef long long int ll;
const ll INF = (ll)1e18;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> a(h);
vector<vector<int>> d(h, vector<int>(w, -1));
queue<pair<int, int>> q;
REP(i, h) {
REP(j, w) {
char c;
cin >> c;
a[i].push_back(c);
if (c == '#') {
q.push(make_pair(i, j));
d[i][j] = 0;
}
}
}
int ans = 0;
while (!q.empty()) {
pair<int, int> pos = q.front();
q.pop();
REP(i, 4) {
int x = pos.first + dx[i];
int y = pos.second + dy[i];
if (x < 0 || x >= w || y < 0 || y >= h)
continue;
if (a[x][y] == '#')
continue;
a[x][y] = '#';
d[x][y] = d[pos.first][pos.second] + 1;
ans = max(ans, d[x][y]);
q.push(make_pair(x, y));
}
}
cout << ans << endl;
}
| #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007
using namespace std;
typedef long long int ll;
const ll INF = (ll)1e18;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> a(h);
vector<vector<int>> d(h, vector<int>(w, -1));
queue<pair<int, int>> q;
REP(i, h) {
REP(j, w) {
char c;
cin >> c;
a[i].push_back(c);
if (c == '#') {
q.push(make_pair(i, j));
d[i][j] = 0;
}
}
}
int ans = 0;
while (!q.empty()) {
pair<int, int> pos = q.front();
q.pop();
REP(i, 4) {
int x = pos.first + dx[i];
int y = pos.second + dy[i];
if (x < 0 || x >= h || y < 0 || y >= w)
continue;
if (a[x][y] == '#')
continue;
a[x][y] = '#';
d[x][y] = d[pos.first][pos.second] + 1;
ans = max(ans, d[x][y]);
q.push(make_pair(x, y));
}
}
cout << ans << endl;
}
| replace | 48 | 49 | 48 | 49 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
if (p)
cout << fixed << setprecision(p) << a << "\n";
else
cout << a << "\n";
}
// end of template
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
// source code
int H, W;
cin >> H >> W;
vector<string> A(H);
vector<vector<int>> B(H, vector<int>(W, -1));
rep(i, H) cin >> A[i];
vector<pair<int, int>> Q;
rep(i, H) rep(j, W) if (A[i][j] == '#') {
B[i][j] = 0;
Q.pb(mp(i, j));
}
vector<pair<int, int>> R;
rep(c, 2222) {
for (auto q : Q) {
rep(d, 4) {
if (q.first + dy[d] >= 0 && q.first + dy[d] < H &&
q.second + dx[d] >= 0 && q.second + dx[d] < W) {
if (B[q.first + dy[d]][q.second + dx[d]] < 0) {
B[q.first + dy[d]][q.second + dx[d]] = c + 1;
R.pb(mp(q.first + dy[d], q.second + dx[d]));
}
}
}
}
Q = R;
}
int ans = 0;
rep(i, H) rep(j, W) ans = max(ans, B[i][j]);
output(ans);
return 0;
}
| #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
if (p)
cout << fixed << setprecision(p) << a << "\n";
else
cout << a << "\n";
}
// end of template
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
// source code
int H, W;
cin >> H >> W;
vector<string> A(H);
vector<vector<int>> B(H, vector<int>(W, -1));
rep(i, H) cin >> A[i];
vector<pair<int, int>> Q;
rep(i, H) rep(j, W) if (A[i][j] == '#') {
B[i][j] = 0;
Q.pb(mp(i, j));
}
vector<pair<int, int>> R;
rep(c, 2222) {
for (auto q : Q) {
rep(d, 4) {
if (q.first + dy[d] >= 0 && q.first + dy[d] < H &&
q.second + dx[d] >= 0 && q.second + dx[d] < W) {
if (B[q.first + dy[d]][q.second + dx[d]] < 0) {
B[q.first + dy[d]][q.second + dx[d]] = c + 1;
R.pb(mp(q.first + dy[d], q.second + dx[d]));
}
}
}
}
Q = R;
R.clear();
}
int ans = 0;
rep(i, H) rep(j, W) ans = max(ans, B[i][j]);
output(ans);
return 0;
}
| insert | 63 | 63 | 63 | 64 | TLE | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <random>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
#define int ll
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef pair<int, int> pii;
typedef pair<pii, pii> piii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
#define inmin(a, b) a = min(a, (b))
#define inmax(a, b) a = max(a, (b))
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for (int i = 0; i < int(n); ++i)
#define SZ(a) ((int)((a).size()))
#define triple(T) tuple<T, T, T>
#define quad(T) tuple<T, T, T, T>
#define watch(x) cerr << (#x) << " = " << (x) << endl;
#ifdef RUS_HOME
#define cerr cout
#else
#define cerr \
if (false) \
cerr
#endif
const double PI = 2 * acos(0.0);
#define rand shittttty_shit
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count());
const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";
template <class T0, class T1>
inline ostream &operator<<(ostream &out, pair<T0, T1> &a) {
return out << "{" << a.first << ", " << a.second << "}";
}
template <class T0, class T1>
inline istream &operator>>(istream &in, pair<T0, T1> &a) {
return in >> a.first >> a.second;
}
template <class T0, class T1, class T2>
inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) {
return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a)
<< "}";
}
template <class T0, class T1, class T2, class T3>
inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) {
return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a)
<< ", " << get<3>(a) << "}";
}
template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) {
out << "[";
fori(i, a.size()) out << a[i]
<< vector<string>{", ", "] "}[i + 1 == a.size()];
return out;
}
void smain();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef RUS_HOME
freopen("input", "r", stdin);
clock_t start = clock();
#endif
cout << setprecision(12) << fixed;
smain();
#ifdef RUS_HOME
cout << "\n\n\n\nTOTAL EXECUTION TIME: "
<< float(clock() - start) / CLOCKS_PER_SEC << endl;
#endif
return 0;
}
const int N = 1e5;
int qx[N], qy[N];
vi dx = {0, 0, 1, -1};
vi dy = {1, -1, 0, 0};
void smain() {
int n, m, st = 0, fn = 0;
cin >> n >> m;
vector<string> s(n);
vector<vector<bool>> used(n, vector<bool>(m));
vvi b(n, vi(m));
for (int i = 0; i < n; i++) {
cin >> s[i];
for (int j = 0; j < m; j++) {
if (s[i][j] == '#') {
qx[fn] = i;
qy[fn] = j;
fn++;
used[i][j] = true;
}
}
}
int ans = 0;
while (st != fn) {
int x = qx[st], y = qy[st];
st++;
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx >= 0 && xx < n && yy >= 0 && yy < m && !used[xx][yy]) {
used[xx][yy] = true;
b[xx][yy] = b[x][y] + 1;
qx[fn] = xx;
qy[fn] = yy;
fn++;
inmax(ans, b[xx][yy]);
}
}
}
cout << ans;
} | #include <bits/stdc++.h>
#include <random>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
#define int ll
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef pair<int, int> pii;
typedef pair<pii, pii> piii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
#define inmin(a, b) a = min(a, (b))
#define inmax(a, b) a = max(a, (b))
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for (int i = 0; i < int(n); ++i)
#define SZ(a) ((int)((a).size()))
#define triple(T) tuple<T, T, T>
#define quad(T) tuple<T, T, T, T>
#define watch(x) cerr << (#x) << " = " << (x) << endl;
#ifdef RUS_HOME
#define cerr cout
#else
#define cerr \
if (false) \
cerr
#endif
const double PI = 2 * acos(0.0);
#define rand shittttty_shit
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count());
const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";
template <class T0, class T1>
inline ostream &operator<<(ostream &out, pair<T0, T1> &a) {
return out << "{" << a.first << ", " << a.second << "}";
}
template <class T0, class T1>
inline istream &operator>>(istream &in, pair<T0, T1> &a) {
return in >> a.first >> a.second;
}
template <class T0, class T1, class T2>
inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) {
return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a)
<< "}";
}
template <class T0, class T1, class T2, class T3>
inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) {
return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a)
<< ", " << get<3>(a) << "}";
}
template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) {
out << "[";
fori(i, a.size()) out << a[i]
<< vector<string>{", ", "] "}[i + 1 == a.size()];
return out;
}
void smain();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef RUS_HOME
freopen("input", "r", stdin);
clock_t start = clock();
#endif
cout << setprecision(12) << fixed;
smain();
#ifdef RUS_HOME
cout << "\n\n\n\nTOTAL EXECUTION TIME: "
<< float(clock() - start) / CLOCKS_PER_SEC << endl;
#endif
return 0;
}
const int N = 1e6 + 100;
int qx[N], qy[N];
vi dx = {0, 0, 1, -1};
vi dy = {1, -1, 0, 0};
void smain() {
int n, m, st = 0, fn = 0;
cin >> n >> m;
vector<string> s(n);
vector<vector<bool>> used(n, vector<bool>(m));
vvi b(n, vi(m));
for (int i = 0; i < n; i++) {
cin >> s[i];
for (int j = 0; j < m; j++) {
if (s[i][j] == '#') {
qx[fn] = i;
qy[fn] = j;
fn++;
used[i][j] = true;
}
}
}
int ans = 0;
while (st != fn) {
int x = qx[st], y = qy[st];
st++;
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx >= 0 && xx < n && yy >= 0 && yy < m && !used[xx][yy]) {
used[xx][yy] = true;
b[xx][yy] = b[x][y] + 1;
qx[fn] = xx;
qy[fn] = yy;
fn++;
inmax(ans, b[xx][yy]);
}
}
}
cout << ans;
} | replace | 107 | 108 | 107 | 108 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define mp make_pair
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define pii pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define mi map<ll, ll>
#define mii map<pii, ll>
const ll N = (1e7 + 1); // mod=998244353;
ll n, m, k; //
ll a[N], dp[N];
string s;
ll l = (1e7 + 2);
char ch[1000][1000];
int h, w, c = 0;
// bool vis[1000][1000]={0}
bool valid(int p, int q) {
if (p < 0 || p >= h)
return 0;
if (q < 0 || q >= w)
return 0;
if (ch[p][q] == '#') {
return 0;
}
return 1;
}
int main() {
cin >> h >> w;
queue<pii> q, q1;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> ch[i][j];
if (ch[i][j] == '#') {
q.push(mp(i, j));
// vis[i][j]=1;
c++;
}
}
}
int ans = 0;
while (c < h * w) {
// cout<<"c="<<c<<endl;
ans++;
while (q.size()) {
int x = q.front().F, y = q.front().S;
q.pop();
if (valid(x + 1, y)) {
ch[x + 1][y] = '#';
q1.push(mp(x + 1, y));
c++;
}
if (valid(x - 1, y)) {
ch[x - 1][y] = '#';
q1.push(mp(x - 1, y));
c++;
}
if (valid(x, y + 1)) {
ch[x][y + 1] = '#';
q1.push(mp(x, y));
c++;
}
if (valid(x, y - 1)) {
ch[x][y - 1] = '#';
q1.push(mp(x, y - 1));
c++;
}
}
q = q1;
while (q1.size())
q1.pop();
}
cout << ans << endl;
}
/*
2
5 2
3 6 4 7 2
5 3
3 -5 6 3 10
*/
| #include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define mp make_pair
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define pii pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define mi map<ll, ll>
#define mii map<pii, ll>
const ll N = (1e7 + 1); // mod=998244353;
ll n, m, k; //
ll a[N], dp[N];
string s;
ll l = (1e7 + 2);
char ch[1000][1000];
int h, w, c = 0;
// bool vis[1000][1000]={0}
bool valid(int p, int q) {
if (p < 0 || p >= h)
return 0;
if (q < 0 || q >= w)
return 0;
if (ch[p][q] == '#') {
return 0;
}
return 1;
}
int main() {
cin >> h >> w;
queue<pii> q, q1;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> ch[i][j];
if (ch[i][j] == '#') {
q.push(mp(i, j));
// vis[i][j]=1;
c++;
}
}
}
int ans = 0;
while (c < h * w) {
// cout<<"c="<<c<<endl;
ans++;
while (q.size()) {
int x = q.front().F, y = q.front().S;
q.pop();
if (valid(x + 1, y)) {
ch[x + 1][y] = '#';
q1.push(mp(x + 1, y));
c++;
}
if (valid(x - 1, y)) {
ch[x - 1][y] = '#';
q1.push(mp(x - 1, y));
c++;
}
if (valid(x, y + 1)) {
ch[x][y + 1] = '#';
q1.push(mp(x, y + 1));
c++;
}
if (valid(x, y - 1)) {
ch[x][y - 1] = '#';
q1.push(mp(x, y - 1));
c++;
}
}
q = q1;
while (q1.size())
q1.pop();
}
cout << ans << endl;
}
/*
2
5 2
3 6 4 7 2
5 3
3 -5 6 3 10
*/
| replace | 65 | 66 | 65 | 66 | TLE | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
int main() {
int h, w;
cin >> h >> w;
vector<string> a(h);
for (auto &e : a)
cin >> e;
vector<vector<int>> d(h, vector<int>(w, 1e9));
queue<pair<int, int>> q;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == '#') {
q.push({i, j});
d[i][j] = 0;
}
}
}
while (!q.empty()) {
auto p = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int y = p.first + dy[i], x = p.second + dx[i];
if (!(0 <= y && y < h && 0 <= x && x < w))
continue;
if (d[p.first][p.second] + 1 > d[y][x])
continue;
d[y][x] = d[p.first][p.second] + 1;
q.push({y, x});
}
}
int r = 0;
for (const auto &row : d) {
for (auto e : row) {
r = max(r, e);
}
}
cout << r << endl;
}
| #include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
int main() {
int h, w;
cin >> h >> w;
vector<string> a(h);
for (auto &e : a)
cin >> e;
vector<vector<int>> d(h, vector<int>(w, 1e9));
queue<pair<int, int>> q;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == '#') {
q.push({i, j});
d[i][j] = 0;
}
}
}
while (!q.empty()) {
auto p = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int y = p.first + dy[i], x = p.second + dx[i];
if (!(0 <= y && y < h && 0 <= x && x < w))
continue;
if (d[p.first][p.second] + 1 >= d[y][x])
continue;
d[y][x] = d[p.first][p.second] + 1;
q.push({y, x});
}
}
int r = 0;
for (const auto &row : d) {
for (auto e : row) {
r = max(r, e);
}
}
cout << r << endl;
}
| replace | 30 | 31 | 30 | 31 | TLE | |
p03053 | C++ | Runtime Error | // #includes {{{
#ifdef MY_DEBUG
#include "header/header.hpp"
#else
#include <bits/stdc++.h>
#endif
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define LET(x, a) __typeof(a) x(a)
// #define IFOR(i,it,c)
// for(__typeof((c).begin())it=(c).begin();it!=(c).end();++it,++i)
#define ALL(c) (c).begin(), (c).end()
#define MP make_pair
#define EXIST(e, s) ((s).find(e) != (s).end())
#define RESET(a) memset((a), 0, sizeof(a))
#define SET(a) memset((a), -1, sizeof(a))
#define PB push_back
#define DEC(it, command) __typeof(command) it = command
// debug
#define whole(f, x, ...) \
([&](decltype((x)) whole) { \
return (f)(begin(whole), end(whole), ##__VA_ARGS__); \
})(x)
typedef long long Int;
typedef unsigned long long uInt;
typedef long double rn;
template <class T> T inf() {
return numeric_limits<T>::has_infinity ? numeric_limits<T>::infinity()
: (numeric_limits<T>::max() / 2);
}
typedef pair<int, int> pii;
#ifdef MY_DEBUG
#include "debug.hpp"
#include "print.hpp"
#endif
// }}}
vector<vector<int>> dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
Int H;
Int W;
vector<string> A;
Int dist[1010][1010];
bool vis[1010][1010];
void solve() {
memset(vis, false, sizeof(vis));
queue<pii> q;
REP(i, H) REP(j, W) {
if (A[i][j] == '#')
dist[i][j] = 0, q.push({i, j});
else
dist[i][j] = inf<Int>();
}
Int ans = 0;
for (; !q.empty();) {
int x = q.front().first, y = q.front().second;
q.pop();
if (vis[x][y])
continue;
vis[x][y] = true;
ans = max(ans, dist[x][y]);
for (auto &&p : dir) {
int x2 = x + p[0], y2 = y + p[1];
if (x2 < 0 or H <= x2 or y2 < 0 or W <= y2)
continue;
if (dist[x2][y2] > dist[x][y] + 1) {
dist[x2][y2] = dist[x][y] + 1;
q.push({x2, y2});
}
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> H;
cin >> W;
A.assign(W, string());
for (int i = 0; i < W; i++) {
cin >> A[i];
}
solve();
return 0;
}
| // #includes {{{
#ifdef MY_DEBUG
#include "header/header.hpp"
#else
#include <bits/stdc++.h>
#endif
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define LET(x, a) __typeof(a) x(a)
// #define IFOR(i,it,c)
// for(__typeof((c).begin())it=(c).begin();it!=(c).end();++it,++i)
#define ALL(c) (c).begin(), (c).end()
#define MP make_pair
#define EXIST(e, s) ((s).find(e) != (s).end())
#define RESET(a) memset((a), 0, sizeof(a))
#define SET(a) memset((a), -1, sizeof(a))
#define PB push_back
#define DEC(it, command) __typeof(command) it = command
// debug
#define whole(f, x, ...) \
([&](decltype((x)) whole) { \
return (f)(begin(whole), end(whole), ##__VA_ARGS__); \
})(x)
typedef long long Int;
typedef unsigned long long uInt;
typedef long double rn;
template <class T> T inf() {
return numeric_limits<T>::has_infinity ? numeric_limits<T>::infinity()
: (numeric_limits<T>::max() / 2);
}
typedef pair<int, int> pii;
#ifdef MY_DEBUG
#include "debug.hpp"
#include "print.hpp"
#endif
// }}}
vector<vector<int>> dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
Int H;
Int W;
vector<string> A;
Int dist[1010][1010];
bool vis[1010][1010];
void solve() {
memset(vis, false, sizeof(vis));
queue<pii> q;
REP(i, H) REP(j, W) {
if (A[i][j] == '#')
dist[i][j] = 0, q.push({i, j});
else
dist[i][j] = inf<Int>();
}
Int ans = 0;
for (; !q.empty();) {
int x = q.front().first, y = q.front().second;
q.pop();
if (vis[x][y])
continue;
vis[x][y] = true;
ans = max(ans, dist[x][y]);
for (auto &&p : dir) {
int x2 = x + p[0], y2 = y + p[1];
if (x2 < 0 or H <= x2 or y2 < 0 or W <= y2)
continue;
if (dist[x2][y2] > dist[x][y] + 1) {
dist[x2][y2] = dist[x][y] + 1;
q.push({x2, y2});
}
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> H;
cin >> W;
A.assign(H, string());
for (int i = 0; i < H; i++) {
cin >> A[i];
}
solve();
return 0;
}
| replace | 95 | 97 | 95 | 97 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int H, W;
queue<pair<int, int>> search;
cin >> H >> W;
int a[1010][1010]; // 距離
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char tmp;
cin >> tmp;
if (tmp == '#') {
a[i][j] = 0;
search.push({i, j});
} else
a[i][j] = 10e6;
}
}
int dr[] = {-1, 1, 0, 0};
int dc[] = {0, 0, -1, 1};
while (!search.empty()) {
int r = search.front().first, c = search.front().second;
search.pop();
for (int i = 0; i < 4; i++) {
int newr = r + dr[i], newc = c + dc[i];
if (newr >= 0 && newr < H && newc >= 0 && newc < W &&
a[newr][newc] > a[r][c]) {
a[newr][newc] = a[r][c] + 1;
search.push({newr, newc});
}
}
}
int ans = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
ans = max(ans, a[i][j]);
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int H, W;
queue<pair<int, int>> search;
cin >> H >> W;
int a[1010][1010]; // 距離
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char tmp;
cin >> tmp;
if (tmp == '#') {
a[i][j] = 0;
search.push({i, j});
} else
a[i][j] = 10e6;
}
}
int dr[] = {-1, 1, 0, 0};
int dc[] = {0, 0, -1, 1};
while (!search.empty()) {
int r = search.front().first, c = search.front().second;
search.pop();
for (int i = 0; i < 4; i++) {
int newr = r + dr[i], newc = c + dc[i];
if (newr >= 0 && newr < H && newc >= 0 && newc < W &&
a[newr][newc] > a[r][c] + 1) {
a[newr][newc] = a[r][c] + 1;
search.push({newr, newc});
}
}
}
int ans = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
ans = max(ans, a[i][j]);
}
}
cout << ans << endl;
return 0;
} | replace | 28 | 29 | 28 | 29 | TLE | |
p03053 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)
using namespace std;
struct Point {
int x;
int y;
};
bool bmap[1000][1000] = {false};
bool smap[1000][1000] = {false};
void add_search(queue<Point> &search, int x, int y, int w, int h) {
if (x < 0 || w <= x)
return;
if (y < 0 || h <= y)
return;
if (bmap[x][y])
return;
if (smap[x][y])
return;
// smap[x][y] = true;
search.push({x, y});
}
int main() {
int h, w;
cin >> h >> w;
queue<Point> search;
FOR_LT(y, 0, h) {
FOR_LT(x, 0, w) {
char c;
cin >> c;
if (c == '#') {
bmap[x][y] = true;
add_search(search, x + 1, y, w, h);
add_search(search, x - 1, y, w, h);
add_search(search, x, y + 1, w, h);
add_search(search, x, y - 1, w, h);
}
}
}
int count = 0;
while (!search.empty()) {
bool has_change = false;
queue<Point> next_search;
while (!search.empty()) {
auto p = search.front();
search.pop();
int x = p.x;
int y = p.y;
if (!bmap[p.x][p.y]) {
bmap[p.x][p.y] = true;
has_change = true;
}
add_search(next_search, x + 1, y, w, h);
add_search(next_search, x - 1, y, w, h);
add_search(next_search, x, y + 1, w, h);
add_search(next_search, x, y - 1, w, h);
}
search = next_search;
if (has_change) {
count++;
}
}
cout << count << endl;
return 0;
} | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)
using namespace std;
struct Point {
int x;
int y;
};
bool bmap[1000][1000] = {false};
bool smap[1000][1000] = {false};
void add_search(queue<Point> &search, int x, int y, int w, int h) {
if (x < 0 || w <= x)
return;
if (y < 0 || h <= y)
return;
if (bmap[x][y])
return;
if (smap[x][y])
return;
smap[x][y] = true;
search.push({x, y});
}
int main() {
int h, w;
cin >> h >> w;
queue<Point> search;
FOR_LT(y, 0, h) {
FOR_LT(x, 0, w) {
char c;
cin >> c;
if (c == '#') {
bmap[x][y] = true;
add_search(search, x + 1, y, w, h);
add_search(search, x - 1, y, w, h);
add_search(search, x, y + 1, w, h);
add_search(search, x, y - 1, w, h);
}
}
}
int count = 0;
while (!search.empty()) {
bool has_change = false;
queue<Point> next_search;
while (!search.empty()) {
auto p = search.front();
search.pop();
int x = p.x;
int y = p.y;
if (!bmap[p.x][p.y]) {
bmap[p.x][p.y] = true;
has_change = true;
}
add_search(next_search, x + 1, y, w, h);
add_search(next_search, x - 1, y, w, h);
add_search(next_search, x, y + 1, w, h);
add_search(next_search, x, y - 1, w, h);
}
search = next_search;
if (has_change) {
count++;
}
}
cout << count << endl;
return 0;
} | replace | 47 | 48 | 47 | 48 | TLE | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()
using namespace std;
typedef long long ll;
static const long long MOD = 1000000007;
int ans = -1, h, w, d[1005][1005];
char a[1005][1005];
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
int main(void) {
cin >> h >> w;
rep(i, 0, h - 1) scanf("%s", a[i]);
memset(d, 0x3f, sizeof(d));
queue<pair<int, int>> q;
rep(i, 1, h) rep(j, 1, w) {
if (a[i - 1][j - 1] == '#') {
d[i][j] = 0;
q.push(mp(i, j));
}
}
while (!q.empty()) {
auto p = q.front();
q.pop();
int sy = p.first, sx = p.second;
rep(r, 0, 4) {
int ty = sy + dy[r], tx = sx + dx[r];
if (tx < 1 || tx > w || ty < 1 || ty > h)
continue;
if (d[sy][sx] + 1 < d[ty][tx]) {
d[ty][tx] = d[sy][sx] + 1;
q.push(mp(ty, tx));
}
}
}
/*
rep(i, 1, h) rep(j, 1, w) {
if (a[i-1][j-1]=='#') d[i][j] = 0;
}
自分の解答
rep(hi, 1, h) {
rep(wi, 1, w) d[hi][wi] = min(d[hi][wi], d[hi][wi-1]+1);
per(wi, 1, w) d[hi][wi] = min(d[hi][wi], d[hi][wi+1]+1);
}
rep(wi, 1, w) {
rep(hi, 1, h) d[hi][wi] = min(d[hi][wi], d[hi-1][wi]+1);
per(hi, 1, h) d[hi][wi] = min(d[hi][wi], d[hi+1][wi]+1);
}
*/
rep(i, 1, h) rep(j, 1, w) ans = max(ans, d[i][j]);
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()
using namespace std;
typedef long long ll;
static const long long MOD = 1000000007;
int ans = -1, h, w, d[1005][1005];
char a[1005][1005];
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
int main(void) {
cin >> h >> w;
rep(i, 0, h - 1) scanf("%s", a[i]);
memset(d, 0x3f, sizeof(d));
queue<pair<int, int>> q;
rep(i, 1, h) rep(j, 1, w) {
if (a[i - 1][j - 1] == '#') {
d[i][j] = 0;
q.push(mp(i, j));
}
}
while (!q.empty()) {
auto p = q.front();
q.pop();
int sy = p.first, sx = p.second;
rep(r, 0, 3) {
int ty = sy + dy[r], tx = sx + dx[r];
if (tx < 1 || tx > w || ty < 1 || ty > h)
continue;
if (d[sy][sx] + 1 < d[ty][tx]) {
d[ty][tx] = d[sy][sx] + 1;
q.push(mp(ty, tx));
}
}
}
/*
rep(i, 1, h) rep(j, 1, w) {
if (a[i-1][j-1]=='#') d[i][j] = 0;
}
自分の解答
rep(hi, 1, h) {
rep(wi, 1, w) d[hi][wi] = min(d[hi][wi], d[hi][wi-1]+1);
per(wi, 1, w) d[hi][wi] = min(d[hi][wi], d[hi][wi+1]+1);
}
rep(wi, 1, w) {
rep(hi, 1, h) d[hi][wi] = min(d[hi][wi], d[hi-1][wi]+1);
per(hi, 1, h) d[hi][wi] = min(d[hi][wi], d[hi+1][wi]+1);
}
*/
rep(i, 1, h) rep(j, 1, w) ans = max(ans, d[i][j]);
cout << ans << endl;
return 0;
}
| replace | 46 | 47 | 46 | 47 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int h, w;
int ans = 0;
cin >> h >> w;
char a[h][w];
queue<pair<int, int>> q;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
if (a[i][j] == '#') {
q.push(pair<int, int>(i, j));
}
}
}
vector<vector<int>> distance(h, vector<int>(w, 0));
while (q.size()) {
pair<int, int> start = q.front();
q.pop();
int x = start.first;
int y = start.second;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int cx = x + dx[i];
int cy = y + dy[i];
if (0 <= cx && cx < h && 0 <= cy && cy < w) {
distance[cx][cy] = distance[x][y] + 1;
q.push(pair<int, int>(cx, cy));
}
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
ans = max(ans, distance[i][j]);
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int h, w;
int ans = 0;
cin >> h >> w;
char a[h][w];
queue<pair<int, int>> q;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
if (a[i][j] == '#') {
q.push(pair<int, int>(i, j));
}
}
}
vector<vector<int>> distance(h, vector<int>(w, 0));
while (q.size()) {
pair<int, int> start = q.front();
q.pop();
int x = start.first;
int y = start.second;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int cx = x + dx[i];
int cy = y + dy[i];
if (0 <= cx && cx < h && 0 <= cy && cy < w && a[cx][cy] == '.' &&
distance[cx][cy] == 0) {
distance[cx][cy] = distance[x][y] + 1;
q.push(pair<int, int>(cx, cy));
}
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
ans = max(ans, distance[i][j]);
}
}
cout << ans << endl;
} | replace | 27 | 28 | 27 | 29 | TLE | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define INF 10000000
#define MOD 1000000007
typedef long long ll;
typedef pair<int, pair<int, int>> P;
int H, W, ans = 0;
char C[60][60];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
queue<P> que;
void bfs() {
while (que.size()) {
P p = que.front();
que.pop();
ans = max(ans, p.first);
// cout<<p.second.first<<" "<<p.second.second<<endl;
for (int i = 0; i < 4; i++) {
int ny = p.second.first + dy[i], nx = p.second.second + dx[i];
// cout<<ny<<" "<<nx<<" "<<"nyとnx"<<endl;
if (0 <= nx && nx < W && ny >= 0 && ny < H && C[ny][nx] == '.') {
// cout<<"unk"<<endl;
C[ny][nx] = '#';
que.push(make_pair(ans + 1, make_pair(ny, nx)));
}
}
}
}
int main() {
int count = 0;
cin >> H >> W;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> C[i][j];
if (C[i][j] == '#')
que.push(make_pair(0, make_pair(i, j)));
}
}
bfs();
// cout<<C[0][0]<<endl;
cout << ans << endl;
}
| #include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define INF 10000000
#define MOD 1000000007
typedef long long ll;
typedef pair<int, pair<int, int>> P;
int H, W, ans = 0;
char C[1100][1100];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
queue<P> que;
void bfs() {
while (que.size()) {
P p = que.front();
que.pop();
ans = max(ans, p.first);
// cout<<p.second.first<<" "<<p.second.second<<endl;
for (int i = 0; i < 4; i++) {
int ny = p.second.first + dy[i], nx = p.second.second + dx[i];
// cout<<ny<<" "<<nx<<" "<<"nyとnx"<<endl;
if (0 <= nx && nx < W && ny >= 0 && ny < H && C[ny][nx] == '.') {
// cout<<"unk"<<endl;
C[ny][nx] = '#';
que.push(make_pair(ans + 1, make_pair(ny, nx)));
}
}
}
}
int main() {
int count = 0;
cin >> H >> W;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> C[i][j];
if (C[i][j] == '#')
que.push(make_pair(0, make_pair(i, j)));
}
}
bfs();
// cout<<C[0][0]<<endl;
cout << ans << endl;
}
| replace | 15 | 16 | 15 | 16 | 0 | |
p03053 | Python | Runtime Error | from collections import deque
H, W = map(int, input().split())
A = [list("#" * (W + 2))]
q = deque()
A_append = A.append
q_append = q.append
for _ in range(H):
a = input()
A_append(list("#" + a + "#"))
A.append(list("#" * (W + 2)))
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
for i in range(1, H + 1):
for j in range(1, W + 1):
if A[i][j] == "#":
q_append([i, j, 0])
ans = 0
while len(q) != 0:
p = q.popleft()
for k in range(4):
y = p[1] + dy[k]
x = p[0] + dx[k]
c = p[2]
ans = max(ans, c)
if A[y][x] == ".":
A[y][x] = "#"
q_append([y, x, c + 1])
print(ans)
| from collections import deque
H, W = map(int, input().split())
A = [list("#" * (W + 2))]
q = deque()
A_append = A.append
q_append = q.append
for _ in range(H):
a = input()
A_append(list("#" + a + "#"))
A.append(list("#" * (W + 2)))
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
for i in range(1, H + 1):
for j in range(1, W + 1):
if A[i][j] == "#":
q_append([i, j, 0])
ans = 0
while len(q) != 0:
p = q.popleft()
for k in range(4):
y = p[0] + dy[k]
x = p[1] + dx[k]
c = p[2]
ans = max(ans, c)
if A[y][x] == ".":
A[y][x] = "#"
q_append([y, x, c + 1])
print(ans)
| replace | 25 | 27 | 25 | 27 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
int h, w;
cin >> h >> w;
vector<string> a(h);
for (int i = 0; i < h; i++)
cin >> a[i];
// 多点スタート
// スタートから0距離で到達できる場合には距離配列に0を格納
vector<vector<int>> dist(h, vector<int>(w, -1));
queue<pair<int, int>> que;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == '#') {
dist[i][j] = 0;
que.push(pair<int, int>(i, j));
}
}
}
while (!que.empty()) {
auto cur = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if (nx < 0 || nx >= w || ny < 0 || ny >= h)
continue;
if (dist[nx][ny] == -1) {
que.push(pair<int, int>(nx, ny));
dist[nx][ny] = dist[cur.first][cur.second] + 1;
}
}
}
int res = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
res = max(res, dist[i][j]);
}
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
int h, w;
cin >> h >> w;
vector<string> a(h);
for (int i = 0; i < h; i++)
cin >> a[i];
// 多点スタート
// スタートから0距離で到達できる場合には距離配列に0を格納
vector<vector<int>> dist(h, vector<int>(w, -1));
queue<pair<int, int>> que;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == '#') {
dist[i][j] = 0;
que.push(pair<int, int>(i, j));
}
}
}
while (!que.empty()) {
auto cur = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
continue;
if (dist[nx][ny] == -1) {
que.push(pair<int, int>(nx, ny));
dist[nx][ny] = dist[cur.first][cur.second] + 1;
}
}
}
int res = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
res = max(res, dist[i][j]);
}
}
cout << res << endl;
return 0;
} | replace | 33 | 34 | 33 | 34 | 0 | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
void printA(const vector<vector<char>> &a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) {
cout << a[i][j];
}
cout << endl;
}
}
void printAns(const vector<vector<int>> &a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) {
cout << a[i][j] << ' ';
}
cout << endl;
}
}
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> a(h, vector<char>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
}
}
vector<vector<int>> ans(w, vector<int>(h, INT_MAX));
// for right
for (int i = 0; i < h; i++) {
bool hitBlack = false;
for (int j = 0; j < w; j++) {
if (a[i][j] == '#') {
hitBlack = true;
ans[i][j] = 0;
} else if (hitBlack) {
chmin(ans[i][j], ans[i][j - 1] + 1);
}
}
}
// for left
for (int i = 0; i < h; i++) {
bool hitBlack = false;
for (int j = w - 1; j >= 0; j--) {
if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
hitBlack = true;
continue;
}
if (hitBlack) {
chmin(ans[i][j], ans[i][j + 1] + 1);
}
}
}
// for bottom
for (int j = 0; j < w; j++) {
bool hitBlack = false;
for (int i = 0; i < h; i++) {
if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
hitBlack = true;
continue;
}
if (hitBlack) {
chmin(ans[i][j], ans[i - 1][j] + 1);
}
}
}
// for top
for (int j = 0; j < w; j++) {
bool hitBlack = false;
for (int i = h - 1; i >= 0; i--) {
if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
hitBlack = true;
continue;
}
if (hitBlack) {
chmin(ans[i][j], ans[i + 1][j] + 1);
}
}
}
// printAns(ans);
int maxVal = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
chmax(maxVal, ans[i][j]);
}
}
cout << maxVal << endl;
return 0;
} | #include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
void printA(const vector<vector<char>> &a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) {
cout << a[i][j];
}
cout << endl;
}
}
void printAns(const vector<vector<int>> &a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) {
cout << a[i][j] << ' ';
}
cout << endl;
}
}
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> a(h, vector<char>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
}
}
vector<vector<int>> ans(h, vector<int>(w, INT_MAX));
// for right
for (int i = 0; i < h; i++) {
bool hitBlack = false;
for (int j = 0; j < w; j++) {
if (a[i][j] == '#') {
hitBlack = true;
ans[i][j] = 0;
} else if (hitBlack) {
chmin(ans[i][j], ans[i][j - 1] + 1);
}
}
}
// for left
for (int i = 0; i < h; i++) {
bool hitBlack = false;
for (int j = w - 1; j >= 0; j--) {
if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
hitBlack = true;
continue;
}
if (hitBlack) {
chmin(ans[i][j], ans[i][j + 1] + 1);
}
}
}
// for bottom
for (int j = 0; j < w; j++) {
bool hitBlack = false;
for (int i = 0; i < h; i++) {
if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
hitBlack = true;
continue;
}
if (hitBlack) {
chmin(ans[i][j], ans[i - 1][j] + 1);
}
}
}
// for top
for (int j = 0; j < w; j++) {
bool hitBlack = false;
for (int i = h - 1; i >= 0; i--) {
if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
hitBlack = true;
continue;
}
if (hitBlack) {
chmin(ans[i][j], ans[i + 1][j] + 1);
}
}
}
// printAns(ans);
int maxVal = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
chmax(maxVal, ans[i][j]);
}
}
cout << maxVal << endl;
return 0;
} | replace | 56 | 57 | 56 | 57 | 0 | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define pb push_back
#define mk make_pair
#define eb emplace_back
#define eps 1e-8
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
// #define int long long
using namespace std;
typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef vector<long double> vd;
const int inf = 1e9;
const long long INF = 1e18;
const int M = 1e9 + 7;
//__int128
int dp[1005][1005];
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
signed main() {
freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int h, w;
cin >> h >> w;
string s;
memset(dp, 0x3f, sizeof(dp));
priority_queue<pii, std::vector<pii>, greater<pii>> pq;
for (int i = 0; i < h; ++i) {
cin >> s;
for (int j = 0; j < w; ++j) {
if (s[j] == '#') {
dp[i][j] = 0;
pq.push(pii(0, i * w + j));
}
}
}
while (!pq.empty()) {
auto cur = pq.top();
pq.pop();
int x = cur.se / w, y = cur.se % w;
if (dp[x][y] < cur.fi)
continue;
// cout << x << ' ' << y << ' ' << cur.fi << endl;
for (int i = 0; i < 4; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (0 <= tx && tx < h && 0 <= ty && ty < w) {
if (dp[tx][ty] > cur.fi + 1) {
dp[tx][ty] = cur.fi + 1;
pq.push(pii(cur.fi + 1, tx * w + ty));
}
}
}
}
int res = 0;
for (int i = 0; i < h; ++i) {
res = max(res, *max_element(dp[i], dp[i] + w));
}
cout << res << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define pb push_back
#define mk make_pair
#define eb emplace_back
#define eps 1e-8
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
// #define int long long
using namespace std;
typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef vector<long double> vd;
const int inf = 1e9;
const long long INF = 1e18;
const int M = 1e9 + 7;
//__int128
int dp[1005][1005];
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
signed main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int h, w;
cin >> h >> w;
string s;
memset(dp, 0x3f, sizeof(dp));
priority_queue<pii, std::vector<pii>, greater<pii>> pq;
for (int i = 0; i < h; ++i) {
cin >> s;
for (int j = 0; j < w; ++j) {
if (s[j] == '#') {
dp[i][j] = 0;
pq.push(pii(0, i * w + j));
}
}
}
while (!pq.empty()) {
auto cur = pq.top();
pq.pop();
int x = cur.se / w, y = cur.se % w;
if (dp[x][y] < cur.fi)
continue;
// cout << x << ' ' << y << ' ' << cur.fi << endl;
for (int i = 0; i < 4; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (0 <= tx && tx < h && 0 <= ty && ty < w) {
if (dp[tx][ty] > cur.fi + 1) {
dp[tx][ty] = cur.fi + 1;
pq.push(pii(cur.fi + 1, tx * w + ty));
}
}
}
}
int res = 0;
for (int i = 0; i < h; ++i) {
res = max(res, *max_element(dp[i], dp[i] + w));
}
cout << res << endl;
return 0;
} | replace | 39 | 40 | 39 | 40 | 0 | |
p03053 | C++ | Runtime Error | #include <iostream>
#include <string>
using namespace std;
int main() {
int H, W;
cin >> H >> W;
char A;
int B[100][100];
int wcnt = 0;
int wcnt2 = 0;
int ans = 0;
int i, j;
for (i = 0; i < H; i++) {
for (j = 0; j < W; j++) {
cin >> A;
if (A == '#')
B[i][j] = 0;
else
B[i][j] = 2000;
}
}
for (i = 0; i < H; i++) {
wcnt = B[i][0];
wcnt2 = B[i][W - 1];
for (j = 0; j < W; j++) {
if (B[i][j] > wcnt) {
B[i][j] = wcnt + 1;
}
wcnt = B[i][j];
if (B[i][W - j - 1] > wcnt2) {
B[i][W - j - 1] = wcnt2 + 1;
}
wcnt2 = B[i][W - j - 1];
}
}
for (j = 0; j < W; j++) {
wcnt = B[0][j];
wcnt2 = B[H - 1][j];
for (i = 0; i < H; i++) {
if (B[i][j] > wcnt) {
B[i][j] = wcnt + 1;
}
wcnt = B[i][j];
if (B[H - i - 1][j] > wcnt2) {
B[H - i - 1][j] = wcnt2 + 1;
}
wcnt2 = B[H - i - 1][j];
}
}
for (i = 0; i < H; i++) {
wcnt = B[i][0];
wcnt2 = B[i][W - 1];
for (j = 0; j < W; j++) {
if (B[i][j] > wcnt) {
B[i][j] = wcnt + 1;
}
wcnt = B[i][j];
if (B[i][W - j - 1] > wcnt2) {
B[i][W - j - 1] = wcnt2 + 1;
}
wcnt2 = B[i][W - j - 1];
}
}
for (j = 0; j < W; j++) {
wcnt = B[0][j];
wcnt2 = B[H - 1][j];
for (i = 0; i < H; i++) {
if (B[i][j] > wcnt) {
B[i][j] = wcnt + 1;
}
wcnt = B[i][j];
if (B[H - i - 1][j] > wcnt2) {
B[H - i - 1][j] = wcnt2 + 1;
}
wcnt2 = B[H - i - 1][j];
if (ans < B[H - i - 1][j])
ans = B[H - i - 1][j];
}
}
cout << ans;
return 0;
} | #include <iostream>
#include <string>
using namespace std;
int main() {
int H, W;
cin >> H >> W;
char A;
int B[1000][1000];
int wcnt = 0;
int wcnt2 = 0;
int ans = 0;
int i, j;
for (i = 0; i < H; i++) {
for (j = 0; j < W; j++) {
cin >> A;
if (A == '#')
B[i][j] = 0;
else
B[i][j] = 2000;
}
}
for (i = 0; i < H; i++) {
wcnt = B[i][0];
wcnt2 = B[i][W - 1];
for (j = 0; j < W; j++) {
if (B[i][j] > wcnt) {
B[i][j] = wcnt + 1;
}
wcnt = B[i][j];
if (B[i][W - j - 1] > wcnt2) {
B[i][W - j - 1] = wcnt2 + 1;
}
wcnt2 = B[i][W - j - 1];
}
}
for (j = 0; j < W; j++) {
wcnt = B[0][j];
wcnt2 = B[H - 1][j];
for (i = 0; i < H; i++) {
if (B[i][j] > wcnt) {
B[i][j] = wcnt + 1;
}
wcnt = B[i][j];
if (B[H - i - 1][j] > wcnt2) {
B[H - i - 1][j] = wcnt2 + 1;
}
wcnt2 = B[H - i - 1][j];
}
}
for (i = 0; i < H; i++) {
wcnt = B[i][0];
wcnt2 = B[i][W - 1];
for (j = 0; j < W; j++) {
if (B[i][j] > wcnt) {
B[i][j] = wcnt + 1;
}
wcnt = B[i][j];
if (B[i][W - j - 1] > wcnt2) {
B[i][W - j - 1] = wcnt2 + 1;
}
wcnt2 = B[i][W - j - 1];
}
}
for (j = 0; j < W; j++) {
wcnt = B[0][j];
wcnt2 = B[H - 1][j];
for (i = 0; i < H; i++) {
if (B[i][j] > wcnt) {
B[i][j] = wcnt + 1;
}
wcnt = B[i][j];
if (B[H - i - 1][j] > wcnt2) {
B[H - i - 1][j] = wcnt2 + 1;
}
wcnt2 = B[H - i - 1][j];
if (ans < B[H - i - 1][j])
ans = B[H - i - 1][j];
}
}
cout << ans;
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int di[] = {-1, 0, 1, 0};
int dj[] = {0, -1, 0, 1};
int main() {
int h, w;
cin >> h >> w;
vector<string> a(h);
vector<vector<int>> dist(h, vector<int>(w, 1e9));
vector<pair<int, int>> p;
rep(i, h) cin >> a[i];
rep(i, h) rep(j, w) if (a[i][j] == '#') p.push_back(make_pair(i, j));
queue<pair<int, int>> q;
auto update = [&](int i, int j, int x) {
if (dist[i][j] < x)
return;
dist[i][j] = x;
q.push(make_pair(i, j));
};
rep(i, p.size()) { update(p[i].first, p[i].second, 0); }
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
q.pop();
rep(dir, 4) {
int ni = i + di[dir];
int nj = j + dj[dir];
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
update(ni, nj, dist[i][j] + 1);
}
}
int ans = 0;
rep(i, h) rep(j, w) { ans = max(ans, dist[i][j]); }
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int di[] = {-1, 0, 1, 0};
int dj[] = {0, -1, 0, 1};
int main() {
int h, w;
cin >> h >> w;
vector<string> a(h);
vector<vector<int>> dist(h, vector<int>(w, 1e9));
vector<pair<int, int>> p;
rep(i, h) cin >> a[i];
rep(i, h) rep(j, w) if (a[i][j] == '#') p.push_back(make_pair(i, j));
queue<pair<int, int>> q;
auto update = [&](int i, int j, int x) {
if (dist[i][j] != 1e9)
return;
dist[i][j] = x;
q.push(make_pair(i, j));
};
rep(i, p.size()) { update(p[i].first, p[i].second, 0); }
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
q.pop();
rep(dir, 4) {
int ni = i + di[dir];
int nj = j + dj[dir];
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
update(ni, nj, dist[i][j] + 1);
}
}
int ans = 0;
rep(i, h) rep(j, w) { ans = max(ans, dist[i][j]); }
cout << ans << endl;
return 0;
} | replace | 18 | 19 | 18 | 19 | TLE | |
p03053 | C++ | Runtime Error | ////////////////////////////////////////
/// tu3 pro-con template ///
////////////////////////////////////////
#include "bits/stdc++.h"
using namespace std;
// -- loop macros -- //
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (int)(s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define FOREACH(i, container) for (auto &&i : container)
#define allof(c) c.begin(), c.end()
#define partof(c, i, n) c.begin() + (i), c.begin() + (i) + (n)
// -- functors -- //
#define PREDICATE(t, a, exp) [&](const t &a) -> bool { return exp; }
#define COMPARISON(t, a, b, exp) \
[&](const t &a, const t &b) -> bool { return exp; }
#define PRED(a, exp) [&](const auto &a) -> bool { return exp; }
#define COMP(a, b, exp) \
[&](const auto &a, const auto &b) -> bool { return exp; }
#define CONV1(a, exp) \
[&](const auto &a) -> auto{ return exp; }
#define CONV2(a, b, exp) \
[&](const auto &a, const auto &b) -> auto{ return exp; }
#define CONV3(a, b, c, exp) \
[&](const auto &a, const auto &b, const auto &c) -> auto{ return exp; }
// -- typedefs -- //
#define EPS 1e-9
typedef unsigned int uint;
typedef long long llong;
typedef unsigned long long ullong;
// -- I/O Helper -- //
struct _Reader {
istream &cin;
template <class T> _Reader operator,(T &rhs) {
cin >> rhs;
return *this;
}
};
struct _Writer {
ostream &cout;
bool f{false};
template <class T> _Writer operator,(const T &rhs) {
cout << (f ? " " : "") << rhs;
f = true;
return *this;
}
};
#define READ(t, ...) \
t __VA_ARGS__; \
(_Reader{cin}), __VA_ARGS__
#define WRITE(...) (_Writer{cout}), __VA_ARGS__, "\n"
#define DEBUG(...) (_Writer{cerr}), __VA_ARGS__, "\n"
// -- vevector -- //
template <class T> struct vevector : public vector<vector<T>> {
vevector(size_t n = 0, size_t m = 0, const T &initial = T())
: vector<vector<T>>(n, vector<T>(m, initial)) {}
};
template <class T> struct vevevector : public vector<vevector<T>> {
vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T())
: vector<vevector<T>>(n, vevector<T>(m, l, initial)) {}
};
template <class T> struct vevevevector : public vector<vevevector<T>> {
vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0,
const T &initial = T())
: vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {}
};
namespace std {
template <class T1, class T2>
inline istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << p.first << " " << p.second;
return out;
}
} // namespace std
template <class T> T read() {
T t;
cin >> t;
return t;
}
template <class T> vector<T> read(int n) {
vector<T> v;
REP(i, n) { v.push_back(read<T>()); }
return v;
}
template <class T> vevector<T> read(int n, int m) {
vevector<T> v;
REP(i, n) v.push_back(read<T>(m));
return v;
}
template <class T> vector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) {
vevector<T> v;
REP(i, n) v.push_back(readjag<T>());
return v;
}
template <class T> struct iter_pair_t {
T beg, end;
};
template <class T> iter_pair_t<T> iter_pair(T beg, T end) {
return iter_pair_t<T>{beg, end};
}
template <class T> ostream &operator<<(ostream &out, iter_pair_t<T> v) {
if (v.beg != v.end) {
out << *v.beg++;
while (v.beg != v.end) {
out << " " << *v.beg++;
}
}
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
return out << iter_pair(begin(v), end(v));
}
// -- etc -- //
template <class T> T infinity_value();
#define DEFINE_INFINITY_VALUE(T, val) \
template <> constexpr T infinity_value<T>() { return (val); }
DEFINE_INFINITY_VALUE(int, 1 << 28);
DEFINE_INFINITY_VALUE(uint, 1u << 28);
DEFINE_INFINITY_VALUE(llong, 1ll << 60);
DEFINE_INFINITY_VALUE(ullong, 1ull << 60);
DEFINE_INFINITY_VALUE(double, HUGE_VAL);
DEFINE_INFINITY_VALUE(float, HUGE_VAL);
#define INF(T) infinity_value<T>()
inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
template <class TInt> bool in_range(TInt val, TInt min, TInt max) {
return val >= min && val < max;
}
template <> bool in_range<double>(double val, double min, double max) {
return val - min > -EPS && val - max < EPS;
}
template <> bool in_range<float>(float val, float min, float max) {
return val - min > -EPS && val - max < EPS;
}
template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) {
return x >= 0 && x < w && y >= 0 && y < h;
}
vector<int> iotavn(int start, int count) {
vector<int> r(count);
iota(allof(r), start);
return r;
}
//// start up ////
void solve();
int main() {
//// for local debugging
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// auto classic_table = ctype<char>::classic_table();
// vector<ctype<char>::mask> ctable(classic_table, classic_table +
// ctype<char>::table_size); ctable[':'] |= ctype_base::space; // as delimitor
// ctable[','] |= ctype_base::space; // as delimitor
// cin.imbue(locale(cin.getloc(), new ctype<char>(ctable.data())));
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed;
cout << setprecision(std::numeric_limits<double>::max_digits10);
solve();
return 0;
}
////////////////////
/// template end ///
////////////////////
void solve() {
READ(int, H, W);
vevector<int> f(H, W, INF(int));
queue<pair<int, int>> q;
{
auto F = read<string>(W);
REP(i, H) REP(j, W) {
if (F[i][j] == '#') {
f[i][j] = 0;
q.push({i, j});
}
}
}
while (!q.empty()) {
auto c = q.front();
q.pop();
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
REP(i, 4) {
int nx = c.first + dx[i];
int ny = c.second + dy[i];
if (in_range2d(nx, ny, H, W)) {
if (f[nx][ny] > f[c.first][c.second] + 1) {
f[nx][ny] = f[c.first][c.second] + 1;
q.push({nx, ny});
}
}
}
}
int ans = 0;
REP(i, H) REP(j, W) { ans = max(ans, f[i][j]); }
WRITE(ans);
} | ////////////////////////////////////////
/// tu3 pro-con template ///
////////////////////////////////////////
#include "bits/stdc++.h"
using namespace std;
// -- loop macros -- //
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (int)(s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define FOREACH(i, container) for (auto &&i : container)
#define allof(c) c.begin(), c.end()
#define partof(c, i, n) c.begin() + (i), c.begin() + (i) + (n)
// -- functors -- //
#define PREDICATE(t, a, exp) [&](const t &a) -> bool { return exp; }
#define COMPARISON(t, a, b, exp) \
[&](const t &a, const t &b) -> bool { return exp; }
#define PRED(a, exp) [&](const auto &a) -> bool { return exp; }
#define COMP(a, b, exp) \
[&](const auto &a, const auto &b) -> bool { return exp; }
#define CONV1(a, exp) \
[&](const auto &a) -> auto{ return exp; }
#define CONV2(a, b, exp) \
[&](const auto &a, const auto &b) -> auto{ return exp; }
#define CONV3(a, b, c, exp) \
[&](const auto &a, const auto &b, const auto &c) -> auto{ return exp; }
// -- typedefs -- //
#define EPS 1e-9
typedef unsigned int uint;
typedef long long llong;
typedef unsigned long long ullong;
// -- I/O Helper -- //
struct _Reader {
istream &cin;
template <class T> _Reader operator,(T &rhs) {
cin >> rhs;
return *this;
}
};
struct _Writer {
ostream &cout;
bool f{false};
template <class T> _Writer operator,(const T &rhs) {
cout << (f ? " " : "") << rhs;
f = true;
return *this;
}
};
#define READ(t, ...) \
t __VA_ARGS__; \
(_Reader{cin}), __VA_ARGS__
#define WRITE(...) (_Writer{cout}), __VA_ARGS__, "\n"
#define DEBUG(...) (_Writer{cerr}), __VA_ARGS__, "\n"
// -- vevector -- //
template <class T> struct vevector : public vector<vector<T>> {
vevector(size_t n = 0, size_t m = 0, const T &initial = T())
: vector<vector<T>>(n, vector<T>(m, initial)) {}
};
template <class T> struct vevevector : public vector<vevector<T>> {
vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T())
: vector<vevector<T>>(n, vevector<T>(m, l, initial)) {}
};
template <class T> struct vevevevector : public vector<vevevector<T>> {
vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0,
const T &initial = T())
: vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {}
};
namespace std {
template <class T1, class T2>
inline istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << p.first << " " << p.second;
return out;
}
} // namespace std
template <class T> T read() {
T t;
cin >> t;
return t;
}
template <class T> vector<T> read(int n) {
vector<T> v;
REP(i, n) { v.push_back(read<T>()); }
return v;
}
template <class T> vevector<T> read(int n, int m) {
vevector<T> v;
REP(i, n) v.push_back(read<T>(m));
return v;
}
template <class T> vector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) {
vevector<T> v;
REP(i, n) v.push_back(readjag<T>());
return v;
}
template <class T> struct iter_pair_t {
T beg, end;
};
template <class T> iter_pair_t<T> iter_pair(T beg, T end) {
return iter_pair_t<T>{beg, end};
}
template <class T> ostream &operator<<(ostream &out, iter_pair_t<T> v) {
if (v.beg != v.end) {
out << *v.beg++;
while (v.beg != v.end) {
out << " " << *v.beg++;
}
}
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
return out << iter_pair(begin(v), end(v));
}
// -- etc -- //
template <class T> T infinity_value();
#define DEFINE_INFINITY_VALUE(T, val) \
template <> constexpr T infinity_value<T>() { return (val); }
DEFINE_INFINITY_VALUE(int, 1 << 28);
DEFINE_INFINITY_VALUE(uint, 1u << 28);
DEFINE_INFINITY_VALUE(llong, 1ll << 60);
DEFINE_INFINITY_VALUE(ullong, 1ull << 60);
DEFINE_INFINITY_VALUE(double, HUGE_VAL);
DEFINE_INFINITY_VALUE(float, HUGE_VAL);
#define INF(T) infinity_value<T>()
inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
template <class TInt> bool in_range(TInt val, TInt min, TInt max) {
return val >= min && val < max;
}
template <> bool in_range<double>(double val, double min, double max) {
return val - min > -EPS && val - max < EPS;
}
template <> bool in_range<float>(float val, float min, float max) {
return val - min > -EPS && val - max < EPS;
}
template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) {
return x >= 0 && x < w && y >= 0 && y < h;
}
vector<int> iotavn(int start, int count) {
vector<int> r(count);
iota(allof(r), start);
return r;
}
//// start up ////
void solve();
int main() {
//// for local debugging
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// auto classic_table = ctype<char>::classic_table();
// vector<ctype<char>::mask> ctable(classic_table, classic_table +
// ctype<char>::table_size); ctable[':'] |= ctype_base::space; // as delimitor
// ctable[','] |= ctype_base::space; // as delimitor
// cin.imbue(locale(cin.getloc(), new ctype<char>(ctable.data())));
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed;
cout << setprecision(std::numeric_limits<double>::max_digits10);
solve();
return 0;
}
////////////////////
/// template end ///
////////////////////
void solve() {
READ(int, H, W);
vevector<int> f(H, W, INF(int));
queue<pair<int, int>> q;
{
auto F = read<string>(H);
REP(i, H) REP(j, W) {
if (F[i][j] == '#') {
f[i][j] = 0;
q.push({i, j});
}
}
}
while (!q.empty()) {
auto c = q.front();
q.pop();
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
REP(i, 4) {
int nx = c.first + dx[i];
int ny = c.second + dy[i];
if (in_range2d(nx, ny, H, W)) {
if (f[nx][ny] > f[c.first][c.second] + 1) {
f[nx][ny] = f[c.first][c.second] + 1;
q.push({nx, ny});
}
}
}
}
int ans = 0;
REP(i, H) REP(j, W) { ans = max(ans, f[i][j]); }
WRITE(ans);
} | replace | 193 | 194 | 193 | 194 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fr(i, n) for (int i = 0; i < n; i++)
#define ifr(i, n) for (int i = n - 1; i >= 0; i--)
int main() {
int h, w;
int x = 0;
cin >> h >> w;
string kari[h];
int a[h + 2][w + 2];
fr(i, h + 2) {
fr(j, w + 2) { a[i][j] = 10000; }
}
bool flag = true;
fr(i, h) { cin >> kari[i]; }
fr(i, h) {
fr(j, w) {
if (kari[i].substr(j, 1) == "#")
a[i + 1][j + 1] = 0;
if (kari[i].substr(j, 1) == ".")
a[i + 1][j + 1] = 10000;
}
}
for (x = 0; x < w + h; x++) {
flag = true;
fr(i, h) {
fr(j, w) {
if (a[i + 1][j + 1] > a[i][j + 1] + 1) {
a[i + 1][j + 1] = a[i][j + 1] + 1;
flag = false;
}
if (a[i + 1][j + 1] > a[i + 2][j + 1] + 1) {
a[i + 1][j + 1] = a[i + 2][j + 1] + 1;
flag = false;
}
if (a[i + 1][j + 1] > a[i + 1][j] + 1) {
a[i + 1][j + 1] = a[i + 1][j] + 1;
flag = false;
}
if (a[i + 1][j + 1] > a[i + 1][j + 2] + 1) {
a[i + 1][j + 1] = a[i + 1][j + 2] + 1;
flag = false;
}
}
}
if (flag == true)
break;
}
int max = 0;
fr(i, h) {
fr(j, w) {
if (a[i + 1][j + 1] > max)
max = a[i + 1][j + 1];
}
}
cout << max << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fr(i, n) for (int i = 0; i < n; i++)
#define ifr(i, n) for (int i = n - 1; i >= 0; i--)
int main() {
int h, w;
int x = 0;
cin >> h >> w;
string kari[h];
int a[h + 2][w + 2];
fr(i, h + 2) {
fr(j, w + 2) { a[i][j] = 10000; }
}
bool flag = true;
fr(i, h) { cin >> kari[i]; }
fr(i, h) {
fr(j, w) {
if (kari[i].substr(j, 1) == "#")
a[i + 1][j + 1] = 0;
if (kari[i].substr(j, 1) == ".")
a[i + 1][j + 1] = 10000;
}
}
for (x = 0; x < w + h; x++) {
flag = true;
fr(i, h) {
fr(j, w) {
if (a[i + 1][j + 1] > a[i][j + 1] + 1) {
a[i + 1][j + 1] = a[i][j + 1] + 1;
flag = false;
}
if (a[i + 1][j + 1] > a[i + 2][j + 1] + 1) {
a[i + 1][j + 1] = a[i + 2][j + 1] + 1;
flag = false;
}
if (a[i + 1][j + 1] > a[i + 1][j] + 1) {
a[i + 1][j + 1] = a[i + 1][j] + 1;
flag = false;
}
if (a[i + 1][j + 1] > a[i + 1][j + 2] + 1) {
a[i + 1][j + 1] = a[i + 1][j + 2] + 1;
flag = false;
}
}
}
if (flag == true)
break;
ifr(i, h) {
ifr(j, w) {
if (a[i + 1][j + 1] > a[i][j + 1] + 1) {
a[i + 1][j + 1] = a[i][j + 1] + 1;
flag = false;
}
if (a[i + 1][j + 1] > a[i + 2][j + 1] + 1) {
a[i + 1][j + 1] = a[i + 2][j + 1] + 1;
flag = false;
}
if (a[i + 1][j + 1] > a[i + 1][j] + 1) {
a[i + 1][j + 1] = a[i + 1][j] + 1;
flag = false;
}
if (a[i + 1][j + 1] > a[i + 1][j + 2] + 1) {
a[i + 1][j + 1] = a[i + 1][j + 2] + 1;
flag = false;
}
}
}
if (flag == true)
break;
}
int max = 0;
fr(i, h) {
fr(j, w) {
if (a[i + 1][j + 1] > max)
max = a[i + 1][j + 1];
}
}
cout << max << endl;
} | insert | 50 | 50 | 50 | 72 | TLE | |
p03053 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_set>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
typedef long long lolo;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef unsigned long long ulolo;
const int maxn = (int)1e5 + 5;
const lolo maxx = (lolo)10000001;
const int maxm = (int)2e3 + 5;
const int INF = 0x3f3f3f3f;
const lolo inf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const int MOD = (int)1e6 + 3;
template <class T> inline void read(T &x) {
x = 0;
T flag = 1;
char c;
do {
c = (char)getchar();
if (c == '-')
break;
} while (c < '0' || c > '9');
if (c == '-') {
c = '0', flag = -1;
}
do {
x = x * 10 + c - '0';
c = (char)getchar();
} while (c >= '0' && c <= '9');
x *= flag;
}
template <class T> inline void out(T x) {
if (x > 9)
out(x / 10);
x = x % 10;
putchar('0' + x);
}
char g[1005][1005];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
queue<pii> Q;
int main() {
freopen("../in.txt", "r", stdin);
int n, m;
read(n);
read(m);
for (int i = 0; i < n; ++i) {
scanf("%s", g[i]);
for (int j = 0; j < m; ++j) {
if (g[i][j] == '#') {
Q.push(make_pair(i, j));
}
}
}
int ccnt = Q.size();
int ncnt = 0, ans = 0;
while (!Q.empty()) {
pii x = Q.front();
Q.pop();
for (int i = 0; i < 4; ++i) {
int nx = x.first + dx[i], ny = x.second + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || g[nx][ny] == '#') {
continue;
}
g[nx][ny] = '#';
Q.push(make_pair(nx, ny));
}
if (++ncnt == ccnt) {
ncnt = 0;
ccnt = Q.size();
++ans;
}
}
printf("%d\n", ans - 1);
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_set>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
typedef long long lolo;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef unsigned long long ulolo;
const int maxn = (int)1e5 + 5;
const lolo maxx = (lolo)10000001;
const int maxm = (int)2e3 + 5;
const int INF = 0x3f3f3f3f;
const lolo inf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const int MOD = (int)1e6 + 3;
template <class T> inline void read(T &x) {
x = 0;
T flag = 1;
char c;
do {
c = (char)getchar();
if (c == '-')
break;
} while (c < '0' || c > '9');
if (c == '-') {
c = '0', flag = -1;
}
do {
x = x * 10 + c - '0';
c = (char)getchar();
} while (c >= '0' && c <= '9');
x *= flag;
}
template <class T> inline void out(T x) {
if (x > 9)
out(x / 10);
x = x % 10;
putchar('0' + x);
}
char g[1005][1005];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
queue<pii> Q;
int main() {
int n, m;
read(n);
read(m);
for (int i = 0; i < n; ++i) {
scanf("%s", g[i]);
for (int j = 0; j < m; ++j) {
if (g[i][j] == '#') {
Q.push(make_pair(i, j));
}
}
}
int ccnt = Q.size();
int ncnt = 0, ans = 0;
while (!Q.empty()) {
pii x = Q.front();
Q.pop();
for (int i = 0; i < 4; ++i) {
int nx = x.first + dx[i], ny = x.second + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || g[nx][ny] == '#') {
continue;
}
g[nx][ny] = '#';
Q.push(make_pair(nx, ny));
}
if (++ncnt == ccnt) {
ncnt = 0;
ccnt = Q.size();
++ans;
}
}
printf("%d\n", ans - 1);
return 0;
}
| delete | 60 | 61 | 60 | 60 | TLE | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define _repi(i, a, b) \
for (int i = int(a), i##_hoge = int(b); i < i##_hoge; ++i)
#define _rep(i, n) _repi(i, 0, n)
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep4(i, a, b, c) \
for (int i = int(a), i##_hogeb = int(b), i##_hogec = int(c); i < i##_hogeb; \
i += i##_hogec)
#define rep1(n) rep4(i, 0, n, 1)
#define rep2(i, n) rep4(i, 0, n, 1)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define REP(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = (n)-1; i >= 0; i--)
#define rrep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define rrep4(i, a, b, c) for (ll i = a + (b - a - 1) / c * c; i >= a; i -= c)
#define RREP(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define FOREACH(i, x) for (auto &i : x)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define print(x) cout << x << endl;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 9;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
template <class T> using vv = vector<vector<T>>;
using vint = vector<int>;
using vvint = vv<int>;
using vll = vector<ll>;
using vvll = vv<ll>;
using vbool = vector<bool>;
using vvbool = vv<bool>;
using vchar = vector<char>;
using vvchar = vv<char>;
// clang-format off
template <class T> bool chmax(T &a, const T &b) {if (a < b) {a = b;return 1;}return 0;}
template <class T> bool chmin(T &a, const T &b) {if (b < a) {a = b;return 1;}return 0;}
// clang-format on
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
int H, W;
cin >> H >> W;
vv<char> A(H + 2, vchar(W + 2, '#'));
REP(i, 1, H + 1) REP(j, 1, W + 1) cin >> A[i][j];
queue<pair<pii, int>> q;
REP(i, 1, H + 1) REP(j, 1, W + 1) {
if (A[i][j] == '#')
q.push(mp(mp(i, j), 0));
}
int x, y, c;
while (!q.empty()) {
auto now = q.front();
q.pop();
y = now.first.first;
x = now.first.second;
c = now.second;
REP(4) {
if (A[x + dx[i]][y + dy[i]] == '.') {
q.push(mp(mp(x + dx[i], y + dy[i]), c + 1));
A[x + dx[i]][y + dy[i]] = '#';
}
}
}
print(c);
} | #include <bits/stdc++.h>
using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define _repi(i, a, b) \
for (int i = int(a), i##_hoge = int(b); i < i##_hoge; ++i)
#define _rep(i, n) _repi(i, 0, n)
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep4(i, a, b, c) \
for (int i = int(a), i##_hogeb = int(b), i##_hogec = int(c); i < i##_hogeb; \
i += i##_hogec)
#define rep1(n) rep4(i, 0, n, 1)
#define rep2(i, n) rep4(i, 0, n, 1)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define REP(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = (n)-1; i >= 0; i--)
#define rrep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define rrep4(i, a, b, c) for (ll i = a + (b - a - 1) / c * c; i >= a; i -= c)
#define RREP(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define FOREACH(i, x) for (auto &i : x)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define print(x) cout << x << endl;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 9;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
template <class T> using vv = vector<vector<T>>;
using vint = vector<int>;
using vvint = vv<int>;
using vll = vector<ll>;
using vvll = vv<ll>;
using vbool = vector<bool>;
using vvbool = vv<bool>;
using vchar = vector<char>;
using vvchar = vv<char>;
// clang-format off
template <class T> bool chmax(T &a, const T &b) {if (a < b) {a = b;return 1;}return 0;}
template <class T> bool chmin(T &a, const T &b) {if (b < a) {a = b;return 1;}return 0;}
// clang-format on
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
int H, W;
cin >> H >> W;
vv<char> A(H + 2, vchar(W + 2, '#'));
REP(i, 1, H + 1) REP(j, 1, W + 1) cin >> A[i][j];
queue<pair<pii, int>> q;
REP(i, 1, H + 1) REP(j, 1, W + 1) {
if (A[i][j] == '#')
q.push(mp(mp(i, j), 0));
}
int x, y, c;
while (!q.empty()) {
auto now = q.front();
q.pop();
y = now.first.first;
x = now.first.second;
c = now.second;
REP(4) {
if (A[y + dy[i]][x + dx[i]] == '.') {
q.push(mp(mp(y + dy[i], x + dx[i]), c + 1));
A[y + dy[i]][x + dx[i]] = '#';
}
}
}
print(c);
} | replace | 69 | 72 | 69 | 72 | 0 | |
p03053 | Python | Time Limit Exceeded | import sys
import collections
def solve():
readline = sys.stdin.buffer.readline
h, w = list(map(int, readline().split()))
a = [[True] * w for _ in range(h)]
ql = []
ql = collections.deque(ql)
for i in range(h):
s = str(readline().rstrip().decode("utf-8"))
for j in range(w):
if s[j] == "#":
a[i][j] = False
ql.append([0, i, j])
mt = 0
while True:
if len(ql) != 0:
cost, x, y = ql.popleft()
mt = max(mt, cost)
for xv, yv in [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]:
if 0 <= xv < h and 0 <= yv < w:
if a[xv][yv]:
ql.append([cost + 1, xv, yv])
a[i][j] = False
else:
break
print(mt)
if __name__ == "__main__":
solve()
| import sys
import collections
def solve():
readline = sys.stdin.buffer.readline
h, w = list(map(int, readline().split()))
a = [[True] * w for _ in range(h)]
ql = []
ql = collections.deque(ql)
for i in range(h):
s = str(readline().rstrip().decode("utf-8"))
for j in range(w):
if s[j] == "#":
a[i][j] = False
ql.append([0, i, j])
mt = 0
while True:
if len(ql) != 0:
cost, x, y = ql.popleft()
mt = max(mt, cost)
for xv, yv in [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]:
if 0 <= xv < h and 0 <= yv < w:
if a[xv][yv]:
ql.append([cost + 1, xv, yv])
a[xv][yv] = False
else:
break
print(mt)
if __name__ == "__main__":
solve()
| replace | 26 | 27 | 26 | 27 | TLE | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
typedef pair<ll, ll> cp2;
typedef pair<ll, cp2> cp3;
#define fi first
#define se second
#define sec se.fi
#define thr se.se
const ll mod = 1000000007;
// 123456789
///////////////////////////////////////////////
//
//
///////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
int H, W;
int ans = 0;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
string A[1123];
int dp[1123][1123];
vector<cp2> S[1123];
int idx = 0;
int main() {
fill(dp[0], dp[1123], -1);
cin >> H >> W;
REP(i, H) cin >> A[i];
REP(i, H) {
REP(j, W) {
if (A[i][j] == '#') {
dp[i][j] = idx;
S[idx].push_back(cp2(i, j));
}
}
}
while (idx < H + W) {
if (S[idx].size() == 0)
break;
REP(i, S[idx].size()) {
int y = S[idx][i].fi;
int x = S[idx][i].se;
REP(j, 4) {
int nx = x + dx[j];
int ny = y + dy[j];
if (nx < W && nx >= 0 && ny < H && ny >= 0) {
if (dp[ny][nx] < 0) {
dp[ny][nx] = idx + 1;
S[idx + 1].push_back(cp2(ny, nx));
}
}
}
}
idx++;
}
REP(i, H) REP(j, W) ans = max(ans, dp[i][j]);
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
typedef pair<ll, ll> cp2;
typedef pair<ll, cp2> cp3;
#define fi first
#define se second
#define sec se.fi
#define thr se.se
const ll mod = 1000000007;
// 123456789
///////////////////////////////////////////////
//
//
///////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
int H, W;
int ans = 0;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
string A[1123];
int dp[1123][1123];
vector<cp2> S[2123];
int idx = 0;
int main() {
fill(dp[0], dp[1123], -1);
cin >> H >> W;
REP(i, H) cin >> A[i];
REP(i, H) {
REP(j, W) {
if (A[i][j] == '#') {
dp[i][j] = idx;
S[idx].push_back(cp2(i, j));
}
}
}
while (idx < H + W) {
if (S[idx].size() == 0)
break;
REP(i, S[idx].size()) {
int y = S[idx][i].fi;
int x = S[idx][i].se;
REP(j, 4) {
int nx = x + dx[j];
int ny = y + dy[j];
if (nx < W && nx >= 0 && ny < H && ny >= 0) {
if (dp[ny][nx] < 0) {
dp[ny][nx] = idx + 1;
S[idx + 1].push_back(cp2(ny, nx));
}
}
}
}
idx++;
}
REP(i, H) REP(j, W) ans = max(ans, dp[i][j]);
cout << ans << endl;
return 0;
} | replace | 39 | 40 | 39 | 40 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <type_traits>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)
#define rng(a) a.begin(), a.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using ld = long double;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
rep(i, (int)v.size()) {
if (i)
os << ",";
os << v[i];
}
os << "}";
return os;
}
template <typename T, size_t S> void printArray(const T (&array)[S]) {
for (auto val : array)
std::cout << val << ", ";
std::cout << "\n";
}
const int mod = 1e9 + 7;
const int inf = 1e9 + 5;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
int h, w;
std::cin >> h >> w;
vector<string> grid(h);
rep(i, h) std::cin >> grid[i];
vector<vi> cost(h, vi(h, inf));
vi que;
rep(i, h) rep(j, w) {
if (grid[i][j] == '.')
continue;
cost[i][j] = 0;
que.pb(i * w + j);
}
int ans = 0;
rep(i, que.size()) {
int x = que[i] % w, y = que[i] / w;
srep(dx, -1, 2) srep(dy, -1, 2) if (dx * dx + dy * dy == 1) {
int nx = x + dx, ny = y + dy;
if (nx < 0 || nx >= w || ny < 0 || ny >= h)
continue;
if (grid[ny][nx] == '#' || cost[ny][nx] != inf)
continue;
cost[ny][nx] = cost[y][x] + 1;
chmax(ans, cost[ny][nx]);
que.pb(ny * w + nx);
}
}
std::cout << ans << "\n";
}
| #include <bits/stdc++.h>
#include <type_traits>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)
#define rng(a) a.begin(), a.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using ld = long double;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
rep(i, (int)v.size()) {
if (i)
os << ",";
os << v[i];
}
os << "}";
return os;
}
template <typename T, size_t S> void printArray(const T (&array)[S]) {
for (auto val : array)
std::cout << val << ", ";
std::cout << "\n";
}
const int mod = 1e9 + 7;
const int inf = 1e9 + 5;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
int h, w;
std::cin >> h >> w;
vector<string> grid(h);
rep(i, h) std::cin >> grid[i];
vector<vi> cost(h, vi(w, inf));
vi que;
rep(i, h) rep(j, w) {
if (grid[i][j] == '.')
continue;
cost[i][j] = 0;
que.pb(i * w + j);
}
int ans = 0;
rep(i, que.size()) {
int x = que[i] % w, y = que[i] / w;
srep(dx, -1, 2) srep(dy, -1, 2) if (dx * dx + dy * dy == 1) {
int nx = x + dx, ny = y + dy;
if (nx < 0 || nx >= w || ny < 0 || ny >= h)
continue;
if (grid[ny][nx] == '#' || cost[ny][nx] != inf)
continue;
cost[ny][nx] = cost[y][x] + 1;
chmax(ans, cost[ny][nx]);
que.pb(ny * w + nx);
}
}
std::cout << ans << "\n";
}
| replace | 62 | 63 | 62 | 63 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef long long ll;
int dp[200][200];
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int h, w;
cin >> h >> w;
rep(i, h) {
rep(j, w) { dp[i][j] = 1e9; }
}
vector<string> s(h);
rep(i, h) { cin >> s[i]; }
queue<P> que;
rep(i, h) {
rep(j, w) {
if (s[i][j] == '#') {
que.push(P(i, j));
dp[i][j] = 0;
}
}
}
while (!que.empty()) {
P p = que.front();
que.pop();
int val = dp[p.first][p.second];
rep(i, 4) {
if (p.first + dy[i] >= h || p.first + dy[i] < 0) {
continue;
}
if (p.second + dx[i] >= w || p.second + dx[i] < 0) {
continue;
}
if (dp[p.first + dy[i]][p.second + dx[i]] > val + 1) {
dp[p.first + dy[i]][p.second + dx[i]] = val + 1;
que.push(P(p.first + dy[i], p.second + dx[i]));
}
}
}
int mx = 0;
rep(i, h) {
rep(j, w) { mx = max(mx, dp[i][j]); }
}
cout << mx << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef long long ll;
int dp[2000][2000];
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int h, w;
cin >> h >> w;
rep(i, h) {
rep(j, w) { dp[i][j] = 1e9; }
}
vector<string> s(h);
rep(i, h) { cin >> s[i]; }
queue<P> que;
rep(i, h) {
rep(j, w) {
if (s[i][j] == '#') {
que.push(P(i, j));
dp[i][j] = 0;
}
}
}
while (!que.empty()) {
P p = que.front();
que.pop();
int val = dp[p.first][p.second];
rep(i, 4) {
if (p.first + dy[i] >= h || p.first + dy[i] < 0) {
continue;
}
if (p.second + dx[i] >= w || p.second + dx[i] < 0) {
continue;
}
if (dp[p.first + dy[i]][p.second + dx[i]] > val + 1) {
dp[p.first + dy[i]][p.second + dx[i]] = val + 1;
que.push(P(p.first + dy[i], p.second + dx[i]));
}
}
}
int mx = 0;
rep(i, h) {
rep(j, w) { mx = max(mx, dp[i][j]); }
}
cout << mx << endl;
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(), a.end()
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
const long long MOD = 1000000007, INF = 1e18;
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;
}
string get_str(string s) {
s += ',';
string ret = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] == ',')
ret += "=%ld, ";
else
ret += s[i];
}
return ret;
}
#define dump(...) \
printf(get_str(#__VA_ARGS__).c_str(), __VA_ARGS__); \
cout << endl
#define endl '\n'
#define IOS() \
ios_base::sync_with_stdio(0); \
cin.tie(0)
//******************************************************************************
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
#define P2 pair<int, pair<int, int>>
signed main() {
// IOS();
int H, W;
cin >> H >> W;
auto s = make_v<char>(H, W);
auto dist = make_v<int>(H, W);
fill_v(dist, INF);
queue<P2> que;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> s[i][j];
if (s[i][j] == '#') {
que.push({0, {i, j}});
dist[i][j] = 0;
}
}
}
int dy[] = {1, 0, -1, 0};
int dx[] = {0, -1, 0, 1};
while (!que.empty()) {
int i = que.front().se.fi;
int j = que.front().se.se;
int cost = que.front().fi;
que.pop();
if (dist[i][j] != cost)
continue;
rep(k, 0, 4) {
int ni = i + dy[k];
int nj = j + dx[k];
if (ni < 0 || H <= ni || nj < 0 || W <= nj)
continue;
if (dist[ni][nj] < cost + 1)
continue;
dist[ni][nj] = cost + 1;
que.push({cost + 1, {ni, nj}});
}
}
int ans = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
// cout<<dist[i][j]<<endl;
chmax(ans, dist[i][j]);
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(), a.end()
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
const long long MOD = 1000000007, INF = 1e18;
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;
}
string get_str(string s) {
s += ',';
string ret = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] == ',')
ret += "=%ld, ";
else
ret += s[i];
}
return ret;
}
#define dump(...) \
printf(get_str(#__VA_ARGS__).c_str(), __VA_ARGS__); \
cout << endl
#define endl '\n'
#define IOS() \
ios_base::sync_with_stdio(0); \
cin.tie(0)
//******************************************************************************
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
#define P2 pair<int, pair<int, int>>
signed main() {
// IOS();
int H, W;
cin >> H >> W;
auto s = make_v<char>(H, W);
auto dist = make_v<int>(H, W);
fill_v(dist, INF);
queue<P2> que;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> s[i][j];
if (s[i][j] == '#') {
que.push({0, {i, j}});
dist[i][j] = 0;
}
}
}
int dy[] = {1, 0, -1, 0};
int dx[] = {0, -1, 0, 1};
while (!que.empty()) {
int i = que.front().se.fi;
int j = que.front().se.se;
int cost = que.front().fi;
que.pop();
if (dist[i][j] != cost)
continue;
rep(k, 0, 4) {
int ni = i + dy[k];
int nj = j + dx[k];
if (ni < 0 || H <= ni || nj < 0 || W <= nj)
continue;
if (dist[ni][nj] <= cost + 1)
continue;
dist[ni][nj] = cost + 1;
que.push({cost + 1, {ni, nj}});
}
}
int ans = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
// cout<<dist[i][j]<<endl;
chmax(ans, dist[i][j]);
}
}
cout << ans << endl;
} | replace | 97 | 98 | 97 | 98 | TLE | |
p03053 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
int main() {
int h, w;
cin >> h >> w;
vector<int> a(h * w);
vector<int> q;
vector<int> next_q;
char tmp;
for (int i = 0; i < h * w; i++) {
cin >> tmp;
if (tmp == '#') {
a[i] = 0;
q.push_back(i);
} else {
a[i] = 1;
}
}
int n, i, j, cnt = 0;
while (!q.empty()) {
while (!q.empty()) {
// for(int k=0;k<q.size();k++) cout<<q[k]<<" ";cout<<endl;
n = q[0];
q.erase(q.begin());
i = n / w;
j = n % w;
if (i != 0 && a[n - w] == 1) {
a[n - w] = 0;
next_q.push_back(n - w);
}
if (i != h - 1 && a[n + w] == 1) {
a[n + w] = 0;
next_q.push_back(n + w);
}
if (j != 0 && a[n - 1] == 1) {
a[n - 1] = 0;
next_q.push_back(n - 1);
}
if (j != w - 1 && a[n + 1] == 1) {
a[n + 1] = 0;
next_q.push_back(n + 1);
}
}
q = next_q;
next_q.clear();
cnt++;
}
cout << cnt - 1 << endl;
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
int h, w;
cin >> h >> w;
vector<int> a(h * w);
vector<int> q;
vector<int> next_q;
char tmp;
for (int i = 0; i < h * w; i++) {
cin >> tmp;
if (tmp == '#') {
a[i] = 0;
q.push_back(i);
} else {
a[i] = 1;
}
}
int n, i, j, cnt = 0;
while (!q.empty()) {
while (!q.empty()) {
// for(int k=0;k<q.size();k++) cout<<q[k]<<" ";cout<<endl;
n = q[q.size() - 1];
q.pop_back();
i = n / w;
j = n % w;
if (i != 0 && a[n - w] == 1) {
a[n - w] = 0;
next_q.push_back(n - w);
}
if (i != h - 1 && a[n + w] == 1) {
a[n + w] = 0;
next_q.push_back(n + w);
}
if (j != 0 && a[n - 1] == 1) {
a[n - 1] = 0;
next_q.push_back(n - 1);
}
if (j != w - 1 && a[n + 1] == 1) {
a[n + 1] = 0;
next_q.push_back(n + 1);
}
}
q = next_q;
next_q.clear();
cnt++;
}
cout << cnt - 1 << endl;
return 0;
} | replace | 23 | 25 | 23 | 25 | TLE | |
p03053 | C++ | Runtime Error | #include <queue>
#include <stdio.h>
#include <utility>
// #define large 1001
#define large 50
using namespace std;
int h, w;
char a[large][large];
int visited[large][large] = {}; // 黒からのユークリッド距離を保有
int vecx[] = {1, -1, 0, 0};
int vecy[] = {0, 0, 1, -1};
queue<pair<int, int>> que; //<x,y>
void bfs(int x, int y) {
for (int i = 0; i < 4; i++) {
if (!(x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
y + vecy[i] >= w) &&
a[x + vecx[i]][y + vecy[i]] == '.') {
a[x + vecx[i]][y + vecy[i]] = '#';
visited[x + vecx[i]][y + vecy[i]] = visited[x][y] + 1;
que.push(make_pair(x + vecx[i], y + vecy[i]));
}
}
}
int main() {
scanf("%d %d", &h, &w);
for (int i = 0; i < h; i++)
scanf("%s", &a[i]);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (a[i][j] == '#') {
que.push(make_pair(i, j));
}
while (!que.empty()) {
pair<int, int> p;
p = que.front();
que.pop();
// printf("bfs(%d,%d)\n",p.first,p.second);
bfs(p.first, p.second);
}
int max = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (max < visited[i][j])
max = visited[i][j];
printf("%d\n", max);
} | #include <queue>
#include <stdio.h>
#include <utility>
#define large 1001
// #define large 50
using namespace std;
int h, w;
char a[large][large];
int visited[large][large] = {}; // 黒からのユークリッド距離を保有
int vecx[] = {1, -1, 0, 0};
int vecy[] = {0, 0, 1, -1};
queue<pair<int, int>> que; //<x,y>
void bfs(int x, int y) {
for (int i = 0; i < 4; i++) {
if (!(x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
y + vecy[i] >= w) &&
a[x + vecx[i]][y + vecy[i]] == '.') {
a[x + vecx[i]][y + vecy[i]] = '#';
visited[x + vecx[i]][y + vecy[i]] = visited[x][y] + 1;
que.push(make_pair(x + vecx[i], y + vecy[i]));
}
}
}
int main() {
scanf("%d %d", &h, &w);
for (int i = 0; i < h; i++)
scanf("%s", &a[i]);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (a[i][j] == '#') {
que.push(make_pair(i, j));
}
while (!que.empty()) {
pair<int, int> p;
p = que.front();
que.pop();
// printf("bfs(%d,%d)\n",p.first,p.second);
bfs(p.first, p.second);
}
int max = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (max < visited[i][j])
max = visited[i][j];
printf("%d\n", max);
} | replace | 3 | 5 | 3 | 5 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define sz size
#define pb push_back
#define mper make_pair
#define all(x) x.begin(), x.end()
#define fe first
#define se second
#define erba continue
using namespace std;
const ll MAXN = 1e3 + 403;
int n, m;
vector<pair<int, int>> g[MAXN];
bool used[MAXN][MAXN];
string s[MAXN];
int main() {
cin >> n >> m;
int p = 0;
int sum = 0;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
for (int j = 1; j <= m; ++j) {
if (s[i][j - 1] == '#') {
used[i][j] = 1;
g[p].pb(mper(i, j));
sum++;
}
}
}
while (1) {
if (sum == n * m) {
break;
}
p++;
// cout << p << ' ';
for (int i = 0; i < g[p - 1].sz(); ++i) {
ll x = g[p - 1][i].fe;
ll y = g[p - 1][i].se;
if (!used[x][y + 1] && y + 1 <= m) {
g[p].pb(mper(x, y + 1));
used[x][y + 1] = 1;
sum++;
}
if (!used[x][y - 1] && y - 1 >= 1) {
g[p].pb(mper(x, y - 1));
used[x][y - 1] = 1;
sum++;
}
if (!used[x + 1][y] && x + 1 <= n) {
g[p].pb(mper(x + 1, y));
used[x + 1][y] = 1;
sum++;
}
if (!used[x - 1][y] && x - 1 >= 1) {
g[p].pb(mper(x - 1, y));
used[x - 1][y] = 1;
sum++;
}
}
}
cout << p;
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
#define sz size
#define pb push_back
#define mper make_pair
#define all(x) x.begin(), x.end()
#define fe first
#define se second
#define erba continue
using namespace std;
const ll MAXN = 1e3 + 1403;
int n, m;
vector<pair<int, int>> g[MAXN];
bool used[MAXN][MAXN];
string s[MAXN];
int main() {
cin >> n >> m;
int p = 0;
int sum = 0;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
for (int j = 1; j <= m; ++j) {
if (s[i][j - 1] == '#') {
used[i][j] = 1;
g[p].pb(mper(i, j));
sum++;
}
}
}
while (1) {
if (sum == n * m) {
break;
}
p++;
// cout << p << ' ';
for (int i = 0; i < g[p - 1].sz(); ++i) {
ll x = g[p - 1][i].fe;
ll y = g[p - 1][i].se;
if (!used[x][y + 1] && y + 1 <= m) {
g[p].pb(mper(x, y + 1));
used[x][y + 1] = 1;
sum++;
}
if (!used[x][y - 1] && y - 1 >= 1) {
g[p].pb(mper(x, y - 1));
used[x][y - 1] = 1;
sum++;
}
if (!used[x + 1][y] && x + 1 <= n) {
g[p].pb(mper(x + 1, y));
used[x + 1][y] = 1;
sum++;
}
if (!used[x - 1][y] && x - 1 >= 1) {
g[p].pb(mper(x - 1, y));
used[x - 1][y] = 1;
sum++;
}
}
}
cout << p;
return 0;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int h, w;
vector<string> s(1024);
queue<pair<int, int>> calc(queue<pair<int, int>> que) {
int n = que.size();
for (int _ = 0; _ < n; _++) {
pair<int, int> p = que.front();
que.pop();
if (p.first > 0) {
if (s[p.first - 1][p.second] == '.')
que.push(make_pair(p.first - 1, p.second));
}
if (p.first < h - 1) {
if (s[p.first + 1][p.second] == '.')
que.push(make_pair(p.first + 1, p.second));
}
if (p.second > 0) {
if (s[p.first][p.second - 1] == '.')
que.push(make_pair(p.first, p.second - 1));
}
if (p.second < w - 1) {
if (s[p.first][p.second + 1] == '.')
que.push(make_pair(p.first, p.second + 1));
}
}
n = que.size();
for (int _ = 0; _ < n; _++) {
pair<int, int> p = que.front();
que.pop();
s[p.first][p.second] = '#';
que.push(p);
}
return que;
}
int main() {
cin >> h >> w;
for (int i = 0; i < h; i++)
cin >> s[i];
queue<pair<int, int>> que;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++)
if (s[i][j] == '#')
que.push(make_pair(i, j));
}
int ans = 0;
while (!que.empty()) {
ans++;
que = calc(que);
}
cout << ans - 1 << endl;
} | #include <bits/stdc++.h>
using namespace std;
int h, w;
vector<string> s(1024);
queue<pair<int, int>> calc(queue<pair<int, int>> que) {
int n = que.size();
for (int _ = 0; _ < n; _++) {
pair<int, int> p = que.front();
que.pop();
if (s[p.first][p.second] != '#')
continue;
s[p.first][p.second] = 'x';
if (p.first > 0) {
if (s[p.first - 1][p.second] == '.')
que.push(make_pair(p.first - 1, p.second));
}
if (p.first < h - 1) {
if (s[p.first + 1][p.second] == '.')
que.push(make_pair(p.first + 1, p.second));
}
if (p.second > 0) {
if (s[p.first][p.second - 1] == '.')
que.push(make_pair(p.first, p.second - 1));
}
if (p.second < w - 1) {
if (s[p.first][p.second + 1] == '.')
que.push(make_pair(p.first, p.second + 1));
}
}
n = que.size();
for (int _ = 0; _ < n; _++) {
pair<int, int> p = que.front();
que.pop();
s[p.first][p.second] = '#';
que.push(p);
}
return que;
}
int main() {
cin >> h >> w;
for (int i = 0; i < h; i++)
cin >> s[i];
queue<pair<int, int>> que;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++)
if (s[i][j] == '#')
que.push(make_pair(i, j));
}
int ans = 0;
while (!que.empty()) {
ans++;
que = calc(que);
}
cout << ans - 1 << endl;
} | insert | 9 | 9 | 9 | 12 | TLE | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
string grid[1001];
int mem[1001][1001][4];
int di[2] = {1, -1};
int n, m;
bool valid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }
int solve(int i, int j, int d1, int d2) {
if (!valid(i, j)) {
return 1e9;
}
if (grid[i][j] == '#')
return 0;
int &ret = mem[i][j][d1 * 2 + d2];
if (~ret)
return ret;
ret = 1e9;
ret = min(ret, solve(i + di[d1], j, d1, d2) + 1);
ret = min(ret, solve(i, j + di[d2], d1, d2) + 1);
return ret;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
memset(mem, -1, sizeof(mem));
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int mn = 1e9;
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
mn = min(mn, solve(i, j, k, l));
}
}
res = max(res, mn);
}
}
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
string grid[1001];
int mem[1001][1001][4];
int di[2] = {1, -1};
int n, m;
bool valid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }
int solve(int i, int j, int d1, int d2) {
if (!valid(i, j)) {
return 1e9;
}
if (grid[i][j] == '#')
return 0;
int &ret = mem[i][j][d1 * 2 + d2];
if (~ret)
return ret;
ret = 1e9;
ret = min(ret, solve(i + di[d1], j, d1, d2) + 1);
ret = min(ret, solve(i, j + di[d2], d1, d2) + 1);
return ret;
}
int main() {
#ifndef ONLINE_JUDGE
// freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
memset(mem, -1, sizeof(mem));
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int mn = 1e9;
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
mn = min(mn, solve(i, j, k, l));
}
}
res = max(res, mn);
}
}
cout << res << endl;
return 0;
}
| replace | 28 | 29 | 28 | 29 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_set>
#include <vector>
using namespace std;
using INT = long long;
using UINT = unsigned long long;
unsigned gcd(unsigned a, unsigned b) {
while (1) {
if (a < b)
swap(a, b);
if (!b)
break;
a %= b;
}
return a;
}
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> a(h + 2, vector<char>(w + 2));
int bc = 0;
for (size_t i = 1; i < h + 1; i++) {
cin >> (a[i].data() + 1);
for (size_t j = 1; j < w + 1; j++) {
bc += (a[i][j] == '#') ? 1 : 0;
}
}
if (bc < h * w / 4) {
vector<vector<INT>> d(h, vector<INT>(w, 10000));
for (INT x = 0; x < w; x++) {
for (INT y = 0; y < h; y++) {
if (a[y + 1][x + 1] == '#') {
for (INT x2 = 0; x2 < w; x2++) {
for (INT y2 = 0; y2 < h; y2++) {
d[y2][x2] = min(d[y2][x2], abs(x - x2) + abs(y - y2));
}
}
}
}
}
INT ans = 0;
for (size_t x = 0; x < w; x++) {
for (size_t y = 0; y < h; y++) {
ans = max(d[y][x], ans);
}
}
cout << ans << endl;
} else {
bool con;
int ans = 0;
while (true) {
con = false;
for (size_t x = 1; x < w + 1; x++) {
for (size_t y = 1; y < h + 1; y++) {
if (a[y][x] == '.') {
con = true;
}
if (a[y][x] == '+') {
a[y][x] = '#';
}
}
}
if (!con) {
break;
}
for (size_t x = 1; x < w + 1; x++) {
for (size_t y = 1; y < h + 1; y++) {
if (a[y][x] == '#') {
a[y - 1][x] = a[y - 1][x] == '.' ? '+' : a[y - 1][x];
a[y + 1][x] = a[y + 1][x] == '.' ? '+' : a[y + 1][x];
a[y][x - 1] = a[y][x - 1] == '.' ? '+' : a[y][x - 1];
a[y][x + 1] = a[y][x + 1] == '.' ? '+' : a[y][x + 1];
}
}
}
ans++;
}
cout << ans << endl;
}
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_set>
#include <vector>
using namespace std;
using INT = long long;
using UINT = unsigned long long;
unsigned gcd(unsigned a, unsigned b) {
while (1) {
if (a < b)
swap(a, b);
if (!b)
break;
a %= b;
}
return a;
}
int main() {
int h, w;
cin >> h >> w;
vector<vector<char>> a(h + 2, vector<char>(w + 2));
int bc = 0;
for (size_t i = 1; i < h + 1; i++) {
cin >> (a[i].data() + 1);
for (size_t j = 1; j < w + 1; j++) {
bc += (a[i][j] == '#') ? 1 : 0;
}
}
if (bc < h * w / 10) {
vector<vector<INT>> d(h, vector<INT>(w, 10000));
for (INT x = 0; x < w; x++) {
for (INT y = 0; y < h; y++) {
if (a[y + 1][x + 1] == '#') {
for (INT x2 = 0; x2 < w; x2++) {
for (INT y2 = 0; y2 < h; y2++) {
d[y2][x2] = min(d[y2][x2], abs(x - x2) + abs(y - y2));
}
}
}
}
}
INT ans = 0;
for (size_t x = 0; x < w; x++) {
for (size_t y = 0; y < h; y++) {
ans = max(d[y][x], ans);
}
}
cout << ans << endl;
} else {
bool con;
int ans = 0;
while (true) {
con = false;
for (size_t x = 1; x < w + 1; x++) {
for (size_t y = 1; y < h + 1; y++) {
if (a[y][x] == '.') {
con = true;
}
if (a[y][x] == '+') {
a[y][x] = '#';
}
}
}
if (!con) {
break;
}
for (size_t x = 1; x < w + 1; x++) {
for (size_t y = 1; y < h + 1; y++) {
if (a[y][x] == '#') {
a[y - 1][x] = a[y - 1][x] == '.' ? '+' : a[y - 1][x];
a[y + 1][x] = a[y + 1][x] == '.' ? '+' : a[y + 1][x];
a[y][x - 1] = a[y][x - 1] == '.' ? '+' : a[y][x - 1];
a[y][x + 1] = a[y][x + 1] == '.' ? '+' : a[y][x + 1];
}
}
}
ans++;
}
cout << ans << endl;
}
return 0;
}
| replace | 44 | 45 | 44 | 45 | TLE | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int A = 0;
// int cnt=0;
int W, H;
vector<string> s(1100);
int cost[1100][1100];
void bfs(int a, int b) {
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
queue<pair<int, int>> que;
que.push(make_pair(a, b));
cost[a][b] = 0;
while (!que.empty()) {
pair<int, int> p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int ny = p.first + dy[i], nx = p.second + dx[i];
// マスの外
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (cost[p.first][p.second] + 1 >= cost[ny][nx]) {
continue;
}
if (s[ny][nx] == '#' && cost[ny][nx] != 0 && A <= 1000000) {
A++;
bfs(ny, nx);
continue;
}
if (s[ny][nx] == '#')
continue;
cost[ny][nx] = cost[p.first][p.second] + 1;
que.push(make_pair(ny, nx));
// cnt++;
}
}
}
int main() {
cin >> H >> W;
int ans = 0;
for (int i = 0; i < H; i++) {
cin >> s[i];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cost[i][j] = 2000000;
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (s[i][j] == '#' && cost[i][j] != 0) {
bfs(i, j);
}
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
ans = max(ans, cost[i][j]);
}
}
cout << ans << endl;
// cout << cnt << endl;
} | #include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int A = 0;
// int cnt=0;
int W, H;
vector<string> s(1100);
int cost[1100][1100];
void bfs(int a, int b) {
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
queue<pair<int, int>> que;
que.push(make_pair(a, b));
cost[a][b] = 0;
while (!que.empty()) {
pair<int, int> p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int ny = p.first + dy[i], nx = p.second + dx[i];
// マスの外
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (cost[p.first][p.second] + 1 >= cost[ny][nx]) {
continue;
}
if (s[ny][nx] == '#' && cost[ny][nx] != 0 && A <= 500000) {
A++;
bfs(ny, nx);
continue;
}
if (s[ny][nx] == '#')
continue;
cost[ny][nx] = cost[p.first][p.second] + 1;
que.push(make_pair(ny, nx));
// cnt++;
}
}
}
int main() {
cin >> H >> W;
int ans = 0;
for (int i = 0; i < H; i++) {
cin >> s[i];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cost[i][j] = 2000000;
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (s[i][j] == '#' && cost[i][j] != 0) {
bfs(i, j);
}
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
ans = max(ans, cost[i][j]);
}
}
cout << ans << endl;
// cout << cnt << endl;
}
| replace | 29 | 30 | 29 | 30 | 0 | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define SIZE(obj) (int)(obj).sizeT()
#define YESNO(cond, yes, no) \
{ cout << ((cond) ? (yes) : (no)) << endl; }
#define SORT(list) sort(ALL((list)));
#define RSORT(list) sort((list).rbegin(), (list).rend())
#define ASSERT(cond, mes) assert(cond &&mes)
constexpr int MOD = 1'000'000'007;
constexpr int INF = 1'050'000'000;
template <typename T> T round_up(const T &a, const T &b) {
return (a + (b - 1)) / b;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
os << p.first << p.second;
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
REP(i, (int)v.size()) is >> v[i];
return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
REP(i, (int)v.size()) os << v[i] << endl;
return os;
}
template <typename T> T clamp(T &n, T a, T b) {
if (n < a)
n = a;
if (n > b)
n = b;
return n;
}
template <typename T> static T GCD(T u, T v) {
T r;
while (v != 0) {
r = u % v;
u = v;
v = r;
}
return u;
}
template <typename T> static T LCM(T u, T v) { return u / GCD(u, v) * v; }
std::vector<int> enum_div(int n) {
std::vector<int> ret;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i != 1 && i * i != n) {
ret.push_back(n / i);
}
}
}
return ret;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct ToUpper {
char operator()(char c) { return toupper(c); }
};
struct ToLower {
char operator()(char c) { return tolower(c); }
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int H, W;
cin >> H >> W;
vector<vector<int>> field(H, vector<int>(W, -1));
queue<pair<int, int>> q;
REP(h, H) {
string S;
cin >> S;
REP(w, W) {
if (S[w] == '#') {
field[h][w] = 0;
q.push({w, h});
}
}
}
auto solve = [&]() {
int ans = 0;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
if (p.first - 1 >= 0 && field[p.second][p.first - 1] < 0) {
int n = field[p.second][p.first] + 1;
field[p.second][p.first - 1] = n;
chmax(ans, n);
q.push({p.first - 1, p.second});
}
if (p.first + 1 < W && field[p.second][p.first + 1] < 0) {
int n = field[p.second][p.first] + 1;
field[p.second][p.first + 1] = n;
chmax(ans, n);
q.push({p.first + 1, p.second});
}
if (p.second - 1 >= 0 && field[p.second - 1][p.first] < 0) {
int n = field[p.second][p.first] + 1;
field[p.second - 1][p.first] = n;
chmax(ans, n);
q.push({p.first, p.second - 1});
}
if (p.second + 1 < W && field[p.second + 1][p.first] < 0) {
int n = field[p.second][p.first] + 1;
field[p.second + 1][p.first] = n;
chmax(ans, n);
q.push({p.first, p.second + 1});
}
}
return ans;
};
cout << solve() << endl;
return 0;
} | #include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define SIZE(obj) (int)(obj).sizeT()
#define YESNO(cond, yes, no) \
{ cout << ((cond) ? (yes) : (no)) << endl; }
#define SORT(list) sort(ALL((list)));
#define RSORT(list) sort((list).rbegin(), (list).rend())
#define ASSERT(cond, mes) assert(cond &&mes)
constexpr int MOD = 1'000'000'007;
constexpr int INF = 1'050'000'000;
template <typename T> T round_up(const T &a, const T &b) {
return (a + (b - 1)) / b;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
os << p.first << p.second;
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
REP(i, (int)v.size()) is >> v[i];
return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
REP(i, (int)v.size()) os << v[i] << endl;
return os;
}
template <typename T> T clamp(T &n, T a, T b) {
if (n < a)
n = a;
if (n > b)
n = b;
return n;
}
template <typename T> static T GCD(T u, T v) {
T r;
while (v != 0) {
r = u % v;
u = v;
v = r;
}
return u;
}
template <typename T> static T LCM(T u, T v) { return u / GCD(u, v) * v; }
std::vector<int> enum_div(int n) {
std::vector<int> ret;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i != 1 && i * i != n) {
ret.push_back(n / i);
}
}
}
return ret;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct ToUpper {
char operator()(char c) { return toupper(c); }
};
struct ToLower {
char operator()(char c) { return tolower(c); }
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int H, W;
cin >> H >> W;
vector<vector<int>> field(H, vector<int>(W, -1));
queue<pair<int, int>> q;
REP(h, H) {
string S;
cin >> S;
REP(w, W) {
if (S[w] == '#') {
field[h][w] = 0;
q.push({w, h});
}
}
}
auto solve = [&]() {
int ans = 0;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
if (p.first - 1 >= 0 && field[p.second][p.first - 1] < 0) {
int n = field[p.second][p.first] + 1;
field[p.second][p.first - 1] = n;
chmax(ans, n);
q.push({p.first - 1, p.second});
}
if (p.first + 1 < W && field[p.second][p.first + 1] < 0) {
int n = field[p.second][p.first] + 1;
field[p.second][p.first + 1] = n;
chmax(ans, n);
q.push({p.first + 1, p.second});
}
if (p.second - 1 >= 0 && field[p.second - 1][p.first] < 0) {
int n = field[p.second][p.first] + 1;
field[p.second - 1][p.first] = n;
chmax(ans, n);
q.push({p.first, p.second - 1});
}
if (p.second + 1 < H && field[p.second + 1][p.first] < 0) {
int n = field[p.second][p.first] + 1;
field[p.second + 1][p.first] = n;
chmax(ans, n);
q.push({p.first, p.second + 1});
}
}
return ans;
};
cout << solve() << endl;
return 0;
} | replace | 167 | 168 | 167 | 168 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
bool toB(char c) {
if (c == '#') {
return true;
} else {
return false;
}
}
int dir[4][2] = {
{0, -1},
{0, 1},
{-1, 0},
{1, 0},
};
int dmap[1005][1005];
struct node {
int i, j;
bool operator>(const node &other) const {
if (i != other.i) {
return i > other.i;
} else if (this->j != other.j) {
return j > other.j;
} else {
return false;
}
}
bool operator<(const node &other) const {
if (i != other.i) {
return this->i < other.i;
} else if (this->j != other.j) {
return this->j < other.j;
} else {
return false;
}
}
};
using namespace std;
int main() {
int H, W;
string m[1005];
scanf("%d %d", &H, &W);
rep(i, H) { cin >> m[i]; }
priority_queue<pair<int, node>, vector<pair<int, node>>,
greater<pair<int, node>>>
pq;
rep(i, H) {
rep(j, W) {
dmap[i][j] = 3000;
if (toB(m[i][j])) {
pq.push(make_pair(0, (node){i, j}));
}
}
}
int ans = 0;
while (!pq.empty()) {
pair<int, node> temp = pq.top();
pq.pop();
int dist = temp.first;
node pl = temp.second;
if (dmap[pl.i][pl.j] <= dist) {
continue;
}
dmap[pl.i][pl.j] = dist;
ans = max(ans, dist);
rep(d, 4) {
int ni = pl.i + dir[d][0];
int nj = pl.j + dir[d][1];
if (ni >= 0 && ni < H && nj >= 0 && nj < W) {
pq.push(make_pair(dist + 1, (node){ni, nj}));
}
}
}
cout << ans << endl;
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
bool toB(char c) {
if (c == '#') {
return true;
} else {
return false;
}
}
int dir[4][2] = {
{0, -1},
{0, 1},
{-1, 0},
{1, 0},
};
int dmap[1005][1005];
struct node {
int i, j;
bool operator>(const node &other) const {
if (i != other.i) {
return i > other.i;
} else if (this->j != other.j) {
return j > other.j;
} else {
return false;
}
}
bool operator<(const node &other) const {
if (i != other.i) {
return this->i < other.i;
} else if (this->j != other.j) {
return this->j < other.j;
} else {
return false;
}
}
};
using namespace std;
int main() {
int H, W;
string m[1005];
scanf("%d %d", &H, &W);
rep(i, H) { cin >> m[i]; }
priority_queue<pair<int, node>, vector<pair<int, node>>,
greater<pair<int, node>>>
pq;
rep(i, H) {
rep(j, W) {
dmap[i][j] = 3000;
if (toB(m[i][j])) {
pq.push(make_pair(0, (node){i, j}));
}
}
}
int ans = 0;
while (!pq.empty()) {
pair<int, node> temp = pq.top();
pq.pop();
int dist = temp.first;
node pl = temp.second;
if (dmap[pl.i][pl.j] <= dist) {
continue;
}
dmap[pl.i][pl.j] = dist;
ans = max(ans, dist);
rep(d, 4) {
int ni = pl.i + dir[d][0];
int nj = pl.j + dir[d][1];
if (ni >= 0 && ni < H && nj >= 0 && nj < W) {
if (dmap[ni][nj] > dist + 1)
pq.push(make_pair(dist + 1, (node){ni, nj}));
}
}
}
cout << ans << endl;
}
| replace | 86 | 87 | 86 | 88 | TLE | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const int MOD = 1000000007;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int h, w, n;
vector<string> s;
vector<vector<int>> steps;
void bfs(vector<P> &starts) {
queue<P> q;
for (auto s : starts) {
q.push(s);
steps[s.first][s.second] = 0;
}
while (!q.empty()) {
int y, x;
tie(y, x) = q.front();
q.pop();
for (int k = 0; k < 4; k++) {
int ny = y + dy[k];
int nx = x + dx[k];
if (nx < 0 || w <= nx || ny < 0 || h <= ny)
continue;
if (s[ny][nx] == '#')
continue;
s[ny][nx] = '#';
steps[ny][nx] = min(steps[ny][nx], steps[y][x] + 1);
q.emplace(nx, ny);
}
}
return;
}
int main() {
#ifdef MYLOCAL
ifstream in("input.txt");
cin.rdbuf(in.rdbuf());
#endif
cin >> h >> w;
s.resize(h);
for (int i = 0; i < h; i++) {
cin >> s[i];
}
steps.resize(h);
for (int i = 0; i < h; i++) {
steps[i].resize(w);
}
vector<P> starts;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
steps[i][j] = INF32;
if (s[i][j] == '#') {
starts.push_back(make_pair(i, j));
}
}
}
bfs(starts);
int ans = 0;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
ans = max(ans, steps[i][j]);
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const int MOD = 1000000007;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int h, w, n;
vector<string> s;
vector<vector<int>> steps;
void bfs(vector<P> &starts) {
queue<P> q;
for (auto s : starts) {
q.push(s);
steps[s.first][s.second] = 0;
}
while (!q.empty()) {
int y, x;
tie(y, x) = q.front();
q.pop();
for (int k = 0; k < 4; k++) {
int ny = y + dy[k];
int nx = x + dx[k];
if (nx < 0 || w <= nx || ny < 0 || h <= ny)
continue;
if (s[ny][nx] == '#')
continue;
s[ny][nx] = '#';
steps[ny][nx] = min(steps[ny][nx], steps[y][x] + 1);
q.emplace(ny, nx);
}
}
return;
}
int main() {
#ifdef MYLOCAL
ifstream in("input.txt");
cin.rdbuf(in.rdbuf());
#endif
cin >> h >> w;
s.resize(h);
for (int i = 0; i < h; i++) {
cin >> s[i];
}
steps.resize(h);
for (int i = 0; i < h; i++) {
steps[i].resize(w);
}
vector<P> starts;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
steps[i][j] = INF32;
if (s[i][j] == '#') {
starts.push_back(make_pair(i, j));
}
}
}
bfs(starts);
int ans = 0;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
ans = max(ans, steps[i][j]);
}
}
cout << ans << endl;
return 0;
}
| replace | 37 | 38 | 37 | 38 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define mp make_pair
int main() {
int n, m;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = -1;
}
}
char s[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> s[i][j];
}
}
queue<pair<int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == '#') {
q.push(mp(i, j));
a[i][j] = 0;
}
}
}
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
q.pop();
if (x + 1 < n && a[x + 1][y] == -1) {
q.push(mp(x + 1, y));
a[x + 1][y] = a[x][y] + 1;
}
if (x - 1 >= 0 && a[x - 1][y] == -1) {
q.push(mp(x - 1, y));
a[x - 1][y] = a[x][y] + 1;
}
if (y + 1 < m && a[x][y + 1] == -1) {
q.push(mp(x, y + 1));
a[x][y + 1] = a[x][y] + 1;
}
if (y - 1 >= 0 && a[x][y - 1] == -1) {
q.push(mp(x, y - 1));
a[x][y - 1] = a[x][y] + 1;
}
}
int ans = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans = max(ans, a[i][j]);
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define mp make_pair
int main() {
int n, m;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = -1;
}
}
char s[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> s[i][j];
}
}
queue<pair<int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == '#') {
q.push(mp(i, j));
a[i][j] = 0;
}
}
}
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
q.pop();
if (x + 1 < n && a[x + 1][y] == -1) {
q.push(mp(x + 1, y));
a[x + 1][y] = a[x][y] + 1;
}
if (x - 1 >= 0 && a[x - 1][y] == -1) {
q.push(mp(x - 1, y));
a[x - 1][y] = a[x][y] + 1;
}
if (y + 1 < m && a[x][y + 1] == -1) {
q.push(mp(x, y + 1));
a[x][y + 1] = a[x][y] + 1;
}
if (y - 1 >= 0 && a[x][y - 1] == -1) {
q.push(mp(x, y - 1));
a[x][y - 1] = a[x][y] + 1;
}
}
int ans = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans = max(ans, a[i][j]);
}
}
cout << ans << endl;
return 0;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int h, w;
char a[1000][1000];
int d[1000][1000] = {};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
queue<P> que;
int dmax = 0;
void bfs() {
while (que.size()) {
P p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int nx = p.first + dx[i], ny = p.second + dy[i];
if (0 <= nx && nx <= w && 0 <= ny && ny <= h && a[nx][ny] == '.' &&
d[nx][ny] == 0) {
d[nx][ny] = d[p.first][p.second] + 1;
que.push(P(nx, ny));
}
}
}
return;
}
int main() {
cin >> h >> w;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
if (a[i][j] == '#') {
que.push(P(i, j));
}
}
}
bfs();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
dmax = max(d[i][j], dmax);
}
}
cout << dmax << endl;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int h, w;
char a[1000][1000];
int d[1000][1000] = {};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
queue<P> que;
int dmax = 0;
void bfs() {
while (que.size()) {
P p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int nx = p.first + dx[i], ny = p.second + dy[i];
if (0 <= nx && nx < h && 0 <= ny && ny < w && a[nx][ny] == '.' &&
d[nx][ny] == 0) {
d[nx][ny] = d[p.first][p.second] + 1;
que.push(P(nx, ny));
}
}
}
return;
}
int main() {
cin >> h >> w;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
if (a[i][j] == '#') {
que.push(P(i, j));
}
}
}
bfs();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
dmax = max(d[i][j], dmax);
}
}
cout << dmax << endl;
}
| replace | 23 | 24 | 23 | 24 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
struct coord {
int x;
int y;
};
int H, W;
char map[1005][1005];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ans;
long long blackcount;
std::queue<coord> queue_next;
void bfs() {
while (blackcount < H * W) {
std::queue<coord> queue(queue_next);
while (queue.size()) {
coord thiscoord = queue.front();
queue.pop();
for (int i = 0; i < 4; i++) {
if (thiscoord.x + dx[i] >= 0 && thiscoord.x + dx[i] < H &&
thiscoord.y + dy[i] >= 0 && thiscoord.y + dy[i] < W) {
if (map[thiscoord.x + dx[i]][thiscoord.y + dy[i]] == '.') {
map[thiscoord.x + dx[i]][thiscoord.y + dy[i]] = '#';
queue_next.push((coord){thiscoord.x + dx[i], thiscoord.y + dy[i]});
blackcount++;
// printf("map[%d][%d] is pushed to
// stack\n",thiscoord.x+dx[i],thiscoord.y+dy[i]);
}
}
}
}
ans++;
// printf("ans=%d\n",ans);
}
return;
}
int main() {
scanf("%d", &H);
scanf("%d", &W);
// printf("H=%d, W=%d\n",H,W);
std::string S;
blackcount = 0;
for (int i = 0; i < H; i++) {
std::cin >> S;
// printf("S=%s\n",S.c_str());
for (int j = 0; j < W; j++) {
map[i][j] = S[j];
if (S[j] == '#') {
queue_next.push((coord){i, j});
blackcount++;
}
}
}
// printf("read end\n");
// printf("blackcount=%lld\n",blackcount);
bfs();
printf("%d\n", ans);
return 0;
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
struct coord {
int x;
int y;
};
int H, W;
char map[1005][1005];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ans;
long long blackcount;
std::queue<coord> queue_next;
void bfs() {
while (blackcount < H * W) {
std::queue<coord> queue(queue_next);
while (queue.size()) {
coord thiscoord = queue.front();
queue.pop();
queue_next.pop();
for (int i = 0; i < 4; i++) {
if (thiscoord.x + dx[i] >= 0 && thiscoord.x + dx[i] < H &&
thiscoord.y + dy[i] >= 0 && thiscoord.y + dy[i] < W) {
if (map[thiscoord.x + dx[i]][thiscoord.y + dy[i]] == '.') {
map[thiscoord.x + dx[i]][thiscoord.y + dy[i]] = '#';
queue_next.push((coord){thiscoord.x + dx[i], thiscoord.y + dy[i]});
blackcount++;
// printf("map[%d][%d] is pushed to
// stack\n",thiscoord.x+dx[i],thiscoord.y+dy[i]);
}
}
}
}
ans++;
// printf("ans=%d\n",ans);
}
return;
}
int main() {
scanf("%d", &H);
scanf("%d", &W);
// printf("H=%d, W=%d\n",H,W);
std::string S;
blackcount = 0;
for (int i = 0; i < H; i++) {
std::cin >> S;
// printf("S=%s\n",S.c_str());
for (int j = 0; j < W; j++) {
map[i][j] = S[j];
if (S[j] == '#') {
queue_next.push((coord){i, j});
blackcount++;
}
}
}
// printf("read end\n");
// printf("blackcount=%lld\n",blackcount);
bfs();
printf("%d\n", ans);
return 0;
}
| insert | 27 | 27 | 27 | 28 | TLE | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using llong = long long;
using ldbl = long double;
using P = pair<llong, llong>;
#define ALL(x) x.begin(), x.end()
const llong inf = 1100100100100100ll;
const llong mod = 1e9 + 7;
int nw[9] = {0, 1, 0, -1, -1, 1, 1, -1, 0};
int nh[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
int main() {
llong H, W;
vector<string> A;
queue<P> q;
cin >> H >> W;
A.resize(H, string(W, '.'));
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++) {
cin >> A[i][j];
if (A[i][j] == '#') {
q.push(P(i, j));
}
}
llong ans = 0;
while (q.size()) {
bool check = false;
queue<P> n;
while (q.size()) {
P now = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
P next(now.first + nh[i], now.second + nw[i]);
if (next.first < 0 || next.second < 0 || next.first >= H ||
next.second >= W)
continue;
if (A[next.first][next.second] == '.') {
A[next.first][next.second] = '#';
n.push(next);
check = true;
}
}
}
for (auto x : A)
cerr << x << endl;
cerr << endl;
ans += check;
swap(q, n);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using llong = long long;
using ldbl = long double;
using P = pair<llong, llong>;
#define ALL(x) x.begin(), x.end()
const llong inf = 1100100100100100ll;
const llong mod = 1e9 + 7;
int nw[9] = {0, 1, 0, -1, -1, 1, 1, -1, 0};
int nh[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
int main() {
llong H, W;
vector<string> A;
queue<P> q;
cin >> H >> W;
A.resize(H, string(W, '.'));
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++) {
cin >> A[i][j];
if (A[i][j] == '#') {
q.push(P(i, j));
}
}
llong ans = 0;
while (q.size()) {
bool check = false;
queue<P> n;
while (q.size()) {
P now = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
P next(now.first + nh[i], now.second + nw[i]);
if (next.first < 0 || next.second < 0 || next.first >= H ||
next.second >= W)
continue;
if (A[next.first][next.second] == '.') {
A[next.first][next.second] = '#';
n.push(next);
check = true;
}
}
}
ans += check;
swap(q, n);
}
cout << ans << endl;
return 0;
}
| delete | 50 | 54 | 50 | 50 | TLE | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000000 + 7
#define INF 1000000000
typedef long long ll;
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int d[1000][1000];
char a[1000][1000];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int h = 0, w = 0;
queue<pair<int, int>> que;
int bfs() {
while (que.size()) {
pair<int, int> p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int ny = p.first + dy[i], nx = p.second + dx[i];
if (1 <= nx && nx <= w && 1 <= ny && ny <= h && a[ny][nx] != '#' &&
d[ny][nx] == INF) {
que.push(pair<int, int>(ny, nx));
d[ny][nx] = d[p.first][p.second] + 1;
}
}
}
int ans = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
ans = max(ans, d[i][j]);
}
}
return ans;
}
int main() {
cin >> h >> w;
string s;
for (int i = 0; i <= h; i++) {
for (int j = 0; j <= w; j++) {
d[i][j] = INF;
}
}
for (int i = 1; i <= h; i++) {
cin >> s;
for (int j = 1; j <= w; j++) {
a[i][j] = s[j - 1];
if (a[i][j] == '#') {
que.push(pair<int, int>(i, j));
d[i][j] = 0;
}
}
}
int res = bfs();
cout << res << endl;
} | #include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000000 + 7
#define INF 1000000000
typedef long long ll;
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int d[1010][1010];
char a[1010][1010];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int h = 0, w = 0;
queue<pair<int, int>> que;
int bfs() {
while (que.size()) {
pair<int, int> p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int ny = p.first + dy[i], nx = p.second + dx[i];
if (1 <= nx && nx <= w && 1 <= ny && ny <= h && a[ny][nx] != '#' &&
d[ny][nx] == INF) {
que.push(pair<int, int>(ny, nx));
d[ny][nx] = d[p.first][p.second] + 1;
}
}
}
int ans = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
ans = max(ans, d[i][j]);
}
}
return ans;
}
int main() {
cin >> h >> w;
string s;
for (int i = 0; i <= h; i++) {
for (int j = 0; j <= w; j++) {
d[i][j] = INF;
}
}
for (int i = 1; i <= h; i++) {
cin >> s;
for (int j = 1; j <= w; j++) {
a[i][j] = s[j - 1];
if (a[i][j] == '#') {
que.push(pair<int, int>(i, j));
d[i][j] = 0;
}
}
}
int res = bfs();
cout << res << endl;
} | replace | 18 | 20 | 18 | 20 | 0 | |
p03053 | C++ | Runtime Error | #include <ctype.h>
#include <stdio.h>
#include <string.h>
#ifndef LL
#define LL long long
#endif
#ifndef uLL
#define uLL unsigned long long
#endif
#ifndef uL
#define uL unsigned int
#endif
#ifndef iL
#define iL int
#endif
#ifndef iS
#define iS short
#endif
#ifndef uS
#define uS unsigned short
#endif
#ifndef uC
#define uC unsigned char
#endif
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define abs(a) (((a) < 0) ? (-(a)) : (a))
#define sgn(a) (((a) < 0) ? (1) : (0))
#define movs(a, b, l) (memmove((a), (b), sizeof(*(b)) * (l)))
#define stosb(a, b, l) (memset((a), (b), sizeof(*(a)) * (l)))
LL map[1000][1000];
LL di[][2] = {{1, 0}, {0, 1}, {(LL)0 - 1, 0}, {0, (LL)0 - 1}};
int legal(char ch) {
if (ch == '#') {
return 1;
}
if (ch == '.') {
return 0;
}
return 2;
}
LL que[2000000];
int main() {
// freopen("in.in","r",stdin);
char ch;
LL i, j, k, a, b, c, d, n, m, ans;
*que = 0;
scanf("%lld%lld", &n, &m);
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
RE:
scanf("%c", &ch);
if (legal(ch) == 2) {
goto RE;
}
map[i][j] = legal(ch);
if (map[i][j] == 1) {
que[++(*que)] = (i << 20) | j;
}
}
}
for (i = 1; i <= *que; i++) {
a = que[i] >> 20;
b = que[i] & 0xFFFFF;
// printf("%lld %lld\n",a,b);
for (j = 0; j < 4; j++) {
c = a + di[j][0];
d = b + di[j][1];
// printf("%lld %lld*\n",c,d);
if ((c >= 1) && (c <= n) && (d >= 1) && (d <= m) && (map[c][d] == 0)) {
map[c][d] = map[a][b] + 1;
que[++(*que)] = (c << 20) | d;
}
}
}
ans = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
ans = max(ans, map[i][j]);
}
}
printf("%lld\n", ans - 1);
return 0;
} | #include <ctype.h>
#include <stdio.h>
#include <string.h>
#ifndef LL
#define LL long long
#endif
#ifndef uLL
#define uLL unsigned long long
#endif
#ifndef uL
#define uL unsigned int
#endif
#ifndef iL
#define iL int
#endif
#ifndef iS
#define iS short
#endif
#ifndef uS
#define uS unsigned short
#endif
#ifndef uC
#define uC unsigned char
#endif
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define abs(a) (((a) < 0) ? (-(a)) : (a))
#define sgn(a) (((a) < 0) ? (1) : (0))
#define movs(a, b, l) (memmove((a), (b), sizeof(*(b)) * (l)))
#define stosb(a, b, l) (memset((a), (b), sizeof(*(a)) * (l)))
LL map[1007][1007];
LL di[][2] = {{1, 0}, {0, 1}, {(LL)0 - 1, 0}, {0, (LL)0 - 1}};
int legal(char ch) {
if (ch == '#') {
return 1;
}
if (ch == '.') {
return 0;
}
return 2;
}
LL que[2000000];
int main() {
// freopen("in.in","r",stdin);
char ch;
LL i, j, k, a, b, c, d, n, m, ans;
*que = 0;
scanf("%lld%lld", &n, &m);
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
RE:
scanf("%c", &ch);
if (legal(ch) == 2) {
goto RE;
}
map[i][j] = legal(ch);
if (map[i][j] == 1) {
que[++(*que)] = (i << 20) | j;
}
}
}
for (i = 1; i <= *que; i++) {
a = que[i] >> 20;
b = que[i] & 0xFFFFF;
// printf("%lld %lld\n",a,b);
for (j = 0; j < 4; j++) {
c = a + di[j][0];
d = b + di[j][1];
// printf("%lld %lld*\n",c,d);
if ((c >= 1) && (c <= n) && (d >= 1) && (d <= m) && (map[c][d] == 0)) {
map[c][d] = map[a][b] + 1;
que[++(*que)] = (c << 20) | d;
}
}
}
ans = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
ans = max(ans, map[i][j]);
}
}
printf("%lld\n", ans - 1);
return 0;
} | replace | 30 | 31 | 30 | 31 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int n, m, x, a, b, c;
bool arr[10005][10005];
char y;
queue<pair<pair<int, int>, int>> q;
int main() {
memset(arr, -1, sizeof(arr));
cin >> n >> m;
x = n * m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> y;
if (y == '#') {
arr[i][j] = 1;
q.push(make_pair(make_pair(i, j), 0));
}
}
}
while (x > 1) {
a = q.front().first.first;
b = q.front().first.second;
c = q.front().second;
q.pop();
x--;
if (a > 1 && arr[a - 1][b] == 0) {
q.push(make_pair(make_pair(a - 1, b), c + 1));
arr[a - 1][b] = 1;
}
if (a < n && arr[a + 1][b] == 0) {
q.push(make_pair(make_pair(a + 1, b), c + 1));
arr[a + 1][b] = 1;
}
if (b > 1 && arr[a][b - 1] == 0) {
q.push(make_pair(make_pair(a, b - 1), c + 1));
arr[a][b - 1] = 1;
}
if (b < m && arr[a][b + 1] == 0) {
q.push(make_pair(make_pair(a, b + 1), c + 1));
arr[a][b + 1] = 1;
}
}
c = q.front().second;
q.pop();
cout << c << endl;
} | #include <bits/stdc++.h>
using namespace std;
int n, m, x, a, b, c;
bool arr[10005][10005];
char y;
queue<pair<pair<int, int>, int>> q;
int main() {
cin >> n >> m;
x = n * m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> y;
if (y == '#') {
arr[i][j] = 1;
q.push(make_pair(make_pair(i, j), 0));
}
}
}
while (x > 1) {
a = q.front().first.first;
b = q.front().first.second;
c = q.front().second;
q.pop();
x--;
if (a > 1 && arr[a - 1][b] == 0) {
q.push(make_pair(make_pair(a - 1, b), c + 1));
arr[a - 1][b] = 1;
}
if (a < n && arr[a + 1][b] == 0) {
q.push(make_pair(make_pair(a + 1, b), c + 1));
arr[a + 1][b] = 1;
}
if (b > 1 && arr[a][b - 1] == 0) {
q.push(make_pair(make_pair(a, b - 1), c + 1));
arr[a][b - 1] = 1;
}
if (b < m && arr[a][b + 1] == 0) {
q.push(make_pair(make_pair(a, b + 1), c + 1));
arr[a][b + 1] = 1;
}
}
c = q.front().second;
q.pop();
cout << c << endl;
} | delete | 9 | 10 | 9 | 9 | 127 | /tmp/b4adf72a-cc36-4868-b576-a714ec175ebf.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
|
p03053 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int v[4][2] = {
{0, 1},
{1, 0},
{-1, 0},
{0, -1},
};
int main() {
int h, w;
cin >> h >> w;
char a[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
}
}
int count = h * w;
vector<pair<int, int>> s1;
s1.reserve(h * w);
vector<pair<int, int>> s2;
s2.reserve(h * w);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == '#') {
s1.push_back(make_pair(i, j));
count--;
}
}
}
int c = 0;
while (count > 0) {
while (!s1.empty()) {
auto p = s1.back();
// for(int i = 0; i < s1.size(); i++){
// cerr << s1[i].first << " " << s1[i].second << endl;
// }
// cerr << endl;
s1.pop_back();
for (auto &i : v) {
if (p.first + i[0] < 0 || h <= p.first + i[0] || p.second + i[1] < 0 ||
w <= p.second + i[1]) {
continue;
}
// cerr << p.first + first+ i[0] << " " << p.second +
// i[1] << endl;
if (a[p.first + i[0]][p.second + i[1]] == '.') {
a[p.first + i[0]][p.second + i[1]] = '#';
count--;
s2.push_back(make_pair(p.first + i[0], p.second + i[1]));
}
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cerr << a[i][j] << " ";
}
cerr << endl;
}
s1.clear();
copy(s2.begin(), s2.end(), back_inserter(s1));
s2.clear();
c++;
}
cout << c << endl;
return 0;
}
| #include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int v[4][2] = {
{0, 1},
{1, 0},
{-1, 0},
{0, -1},
};
int main() {
int h, w;
cin >> h >> w;
char a[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
}
}
int count = h * w;
vector<pair<int, int>> s1;
s1.reserve(h * w);
vector<pair<int, int>> s2;
s2.reserve(h * w);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == '#') {
s1.push_back(make_pair(i, j));
count--;
}
}
}
int c = 0;
while (count > 0) {
while (!s1.empty()) {
auto p = s1.back();
// for(int i = 0; i < s1.size(); i++){
// cerr << s1[i].first << " " << s1[i].second << endl;
// }
// cerr << endl;
s1.pop_back();
for (auto &i : v) {
if (p.first + i[0] < 0 || h <= p.first + i[0] || p.second + i[1] < 0 ||
w <= p.second + i[1]) {
continue;
}
// cerr << p.first + first+ i[0] << " " << p.second +
// i[1] << endl;
if (a[p.first + i[0]][p.second + i[1]] == '.') {
a[p.first + i[0]][p.second + i[1]] = '#';
count--;
s2.push_back(make_pair(p.first + i[0], p.second + i[1]));
}
}
}
// for(int i = 0; i < h; i++) {
// for(int j = 0; j < w; j++) {
// cerr << a[i][j] << " ";
// }
// cerr << endl;
// }
s1.clear();
copy(s2.begin(), s2.end(), back_inserter(s1));
s2.clear();
c++;
}
cout << c << endl;
return 0;
}
| replace | 63 | 69 | 63 | 69 | TLE | |
p03053 | C++ | Time Limit Exceeded | // #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize(3)
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target("sse3","sse2","sse")
// #pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3")
// #pragma GCC target("f16c")
// #pragma GCC
// optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma
// GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error
// "-fcse-skip-blocks" #pragma GCC diagnostic error
// "-funsafe-loop-optimizations" #pragma GCC diagnostic error "-std=c++14"
#include "bits/stdc++.h"
// #include "ext/pb_ds/tree_policy.hpp"
// #include "ext/pb_ds/assoc_container.hpp"
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x, "r", stdin)
#define fw(x) freopen(x, "w", stdout)
#define iout(x) printf("%d\n", x)
#define lout(x) printf("%lld\n", x)
#define REP(x, l, u) for (ll x = l; x < u; x++)
#define RREP(x, l, u) for (ll x = l; x >= u; x--)
#define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define mst(x, a) memset(x, a, sizeof(x))
#define all(a) a.begin(), a.end()
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define MP make_pair
#define sqr(x) ((x) * (x))
#define lowbit(x) ((x) & (-(x)))
#define lson (ind << 1)
#define rson (ind << 1 | 1)
#define se second
#define fi first
#define sz(x) ((int)x.size())
#define EX0 exit(0);
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
using namespace std;
typedef vector<ll> VLL;
typedef vector<int> VI;
const int block_size = 320;
typedef complex<ll> point;
const ll mod = 1e9 + 7;
const ll inf = 1e9 + 7;
const ld eps = 1e-9;
const db PI = atan(1) * 4;
template <typename T> inline int sign(const T &a) {
if (a < 0)
return -1;
if (a > 0)
return 1;
return 0;
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...) \
{}
#endif
template <typename T, typename S> inline bool upmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T, typename S> inline bool upmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}
template <typename T> inline void in(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
ll twop(int x) { return 1LL << x; }
// m must be positive
template <typename T> T MOD(T a, T m) {
a %= m;
if (a < 0)
a += m;
return a;
}
// a must be relatively prime to m
template <typename T> T inverse(T a, T m) {
a = MOD(a, m);
if (a <= 1)
return a;
return MOD((1 - inverse(m, a) * m) / a, m);
}
template <typename A, typename B> inline void in(A &x, B &y) {
in(x);
in(y);
}
template <typename A, typename B, typename C> inline void in(A &x, B &y, C &z) {
in(x);
in(y);
in(z);
}
template <typename A, typename B, typename C, typename D>
inline void in(A &x, B &y, C &z, D &d) {
in(x);
in(y);
in(z);
in(d);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
namespace SOLVE {
char grid[1010][1010];
int n, m;
int dis[1010][1010];
void main() {
in(n, m);
mst(grid, '#');
REP(i, 1, n + 1) scanf("%s", grid[i] + 1);
queue<PII> q;
REP(i, 1, n + 1) {
REP(j, 1, m + 1) {
dis[i][j] = inf;
if (grid[i][j] == '#') {
dis[i][j] = 0;
q.push(MP(i, j));
}
}
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
while (sz(q)) {
auto f = q.front();
q.pop();
REP(d, 0, 4) {
int tx = dx[d] + f.fi, ty = dy[d] + f.se;
if (dis[tx][ty] > dis[f.fi][f.se] + 1) {
dis[tx][ty] = dis[f.fi][f.se] + 1;
q.push(MP(tx, ty));
}
}
}
int ans = 0;
REP(i, 1, n + 1) {
REP(j, 1, m + 1) { upmax(ans, dis[i][j]); }
}
cout << ans << endl;
}
} // namespace SOLVE
signed main() {
#ifndef ONLINE_JUDGE
fr("/Users/zhangqingchuan/Desktop/cp/cp/input.txt");
fw("/Users/zhangqingchuan/Desktop/cp/cp/output.txt");
#endif
int t = 1;
// in(t);
while (t--) {
SOLVE::main();
}
return 0;
}
| // #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize(3)
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target("sse3","sse2","sse")
// #pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3")
// #pragma GCC target("f16c")
// #pragma GCC
// optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma
// GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error
// "-fcse-skip-blocks" #pragma GCC diagnostic error
// "-funsafe-loop-optimizations" #pragma GCC diagnostic error "-std=c++14"
#include "bits/stdc++.h"
// #include "ext/pb_ds/tree_policy.hpp"
// #include "ext/pb_ds/assoc_container.hpp"
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x, "r", stdin)
#define fw(x) freopen(x, "w", stdout)
#define iout(x) printf("%d\n", x)
#define lout(x) printf("%lld\n", x)
#define REP(x, l, u) for (ll x = l; x < u; x++)
#define RREP(x, l, u) for (ll x = l; x >= u; x--)
#define complete_unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define mst(x, a) memset(x, a, sizeof(x))
#define all(a) a.begin(), a.end()
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define MP make_pair
#define sqr(x) ((x) * (x))
#define lowbit(x) ((x) & (-(x)))
#define lson (ind << 1)
#define rson (ind << 1 | 1)
#define se second
#define fi first
#define sz(x) ((int)x.size())
#define EX0 exit(0);
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
using namespace std;
typedef vector<ll> VLL;
typedef vector<int> VI;
const int block_size = 320;
typedef complex<ll> point;
const ll mod = 1e9 + 7;
const ll inf = 1e9 + 7;
const ld eps = 1e-9;
const db PI = atan(1) * 4;
template <typename T> inline int sign(const T &a) {
if (a < 0)
return -1;
if (a > 0)
return 1;
return 0;
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...) \
{}
#endif
template <typename T, typename S> inline bool upmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T, typename S> inline bool upmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}
template <typename T> inline void in(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
ll twop(int x) { return 1LL << x; }
// m must be positive
template <typename T> T MOD(T a, T m) {
a %= m;
if (a < 0)
a += m;
return a;
}
// a must be relatively prime to m
template <typename T> T inverse(T a, T m) {
a = MOD(a, m);
if (a <= 1)
return a;
return MOD((1 - inverse(m, a) * m) / a, m);
}
template <typename A, typename B> inline void in(A &x, B &y) {
in(x);
in(y);
}
template <typename A, typename B, typename C> inline void in(A &x, B &y, C &z) {
in(x);
in(y);
in(z);
}
template <typename A, typename B, typename C, typename D>
inline void in(A &x, B &y, C &z, D &d) {
in(x);
in(y);
in(z);
in(d);
}
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
namespace SOLVE {
char grid[1010][1010];
int n, m;
int dis[1010][1010];
void main() {
in(n, m);
mst(grid, '#');
REP(i, 1, n + 1) scanf("%s", grid[i] + 1);
queue<PII> q;
REP(i, 1, n + 1) {
REP(j, 1, m + 1) {
dis[i][j] = inf;
if (grid[i][j] == '#') {
dis[i][j] = 0;
q.push(MP(i, j));
}
}
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
while (sz(q)) {
auto f = q.front();
q.pop();
REP(d, 0, 4) {
int tx = dx[d] + f.fi, ty = dy[d] + f.se;
if (dis[tx][ty] > dis[f.fi][f.se] + 1) {
dis[tx][ty] = dis[f.fi][f.se] + 1;
q.push(MP(tx, ty));
}
}
}
int ans = 0;
REP(i, 1, n + 1) {
REP(j, 1, m + 1) { upmax(ans, dis[i][j]); }
}
cout << ans << endl;
}
} // namespace SOLVE
signed main() {
int t = 1;
// in(t);
while (t--) {
SOLVE::main();
}
return 0;
}
| delete | 197 | 201 | 197 | 197 | TLE | |
p03053 | C++ | Runtime Error | #include <queue>
#include <stdio.h>
// #define large 1005
#define large 50
using namespace std;
int h, w;
char a[large][large];
int vecx[] = {1, -1, 0, 0};
int vecy[] = {0, 0, 1, -1};
int visited[large][large] = {};
queue<int> que;
void bfs(int x, int y) {
// if(visited[x][y]>0) return;
for (int i = 0; i < 4; i++) {
if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
y + vecy[i] >= w)
continue;
else {
if (a[x + vecx[i]][y + vecy[i]] == '.' &&
visited[x + vecx[i]][y + vecy[i]] == 0)
// if(a[x+vecx[i]][y+vecy[i]]=='.')
{
visited[x + vecx[i]][y + vecy[i]] = visited[x][y] + 1;
a[x + vecx[i]][y + vecy[i]] = '#';
que.push(x + vecx[i]);
que.push(y + vecy[i]);
}
}
}
return;
}
int main() {
scanf("%d %d", &h, &w);
for (int i = 0; i < h; i++)
scanf("%s", a[i]);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (a[i][j] == '#') {
que.push(i);
que.push(j);
}
while (!que.empty()) {
// for(int j=0;j<h;j++)
// printf("%s\n",a[j]);
// printf("\n");
int tmpx = que.front();
que.pop();
int tmpy = que.front();
que.pop();
// printf("bfs(%d,%d)\n",tmpx,tmpy);
bfs(tmpx, tmpy);
}
int ans = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
if (visited[i][j] > ans)
ans = visited[i][j];
}
printf("%d\n", ans);
return 0;
} | #include <queue>
#include <stdio.h>
#define large 1005
// #define large 50
using namespace std;
int h, w;
char a[large][large];
int vecx[] = {1, -1, 0, 0};
int vecy[] = {0, 0, 1, -1};
int visited[large][large] = {};
queue<int> que;
void bfs(int x, int y) {
// if(visited[x][y]>0) return;
for (int i = 0; i < 4; i++) {
if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
y + vecy[i] >= w)
continue;
else {
if (a[x + vecx[i]][y + vecy[i]] == '.' &&
visited[x + vecx[i]][y + vecy[i]] == 0)
// if(a[x+vecx[i]][y+vecy[i]]=='.')
{
visited[x + vecx[i]][y + vecy[i]] = visited[x][y] + 1;
a[x + vecx[i]][y + vecy[i]] = '#';
que.push(x + vecx[i]);
que.push(y + vecy[i]);
}
}
}
return;
}
int main() {
scanf("%d %d", &h, &w);
for (int i = 0; i < h; i++)
scanf("%s", a[i]);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (a[i][j] == '#') {
que.push(i);
que.push(j);
}
while (!que.empty()) {
// for(int j=0;j<h;j++)
// printf("%s\n",a[j]);
// printf("\n");
int tmpx = que.front();
que.pop();
int tmpy = que.front();
que.pop();
// printf("bfs(%d,%d)\n",tmpx,tmpy);
bfs(tmpx, tmpy);
}
int ans = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
if (visited[i][j] > ans)
ans = visited[i][j];
}
printf("%d\n", ans);
return 0;
} | replace | 2 | 4 | 2 | 4 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
typedef pair<int, int> P;
const int MAX_H = 50, MAX_W = 50;
int H, W;
int sy, sx;
int gy, gx;
char maze[MAX_H][MAX_W];
int d[MAX_H][MAX_W];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void bfs(int y, int x);
int main() {
cin >> H >> W;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> maze[i][j];
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
}
}
queue<P> que;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (maze[i][j] == '#') {
d[i][j] = 0;
que.push(P(i, j));
}
}
}
// BFS
while (que.size()) {
P p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int ny = p.first + dy[i], nx = p.second + dx[i];
if (0 <= ny && ny < H && 0 <= nx && nx < W && maze[ny][nx] != '#' &&
d[ny][nx] == INF) {
que.push(P(ny, nx));
d[ny][nx] = d[p.first][p.second] + 1;
}
}
}
int ans = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (d[i][j] > ans)
ans = d[i][j];
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
typedef pair<int, int> P;
const int MAX_H = 1000, MAX_W = 1000;
int H, W;
int sy, sx;
int gy, gx;
char maze[MAX_H][MAX_W];
int d[MAX_H][MAX_W];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void bfs(int y, int x);
int main() {
cin >> H >> W;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> maze[i][j];
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
}
}
queue<P> que;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (maze[i][j] == '#') {
d[i][j] = 0;
que.push(P(i, j));
}
}
}
// BFS
while (que.size()) {
P p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int ny = p.first + dy[i], nx = p.second + dx[i];
if (0 <= ny && ny < H && 0 <= nx && nx < W && maze[ny][nx] != '#' &&
d[ny][nx] == INF) {
que.push(P(ny, nx));
d[ny][nx] = d[p.first][p.second] + 1;
}
}
}
int ans = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (d[i][j] > ans)
ans = d[i][j];
}
}
cout << ans << endl;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen("./a.in", "r", stdin);
freopen("./a.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int h, w;
cin >> h >> w;
vector<string> grid(h);
for (int i = 0; i < h; i++) {
cin >> grid[i];
}
vector<vector<int>> dist(h, vector<int>(w, -1));
vector<pair<int, int>> que;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (grid[i][j] == '#') {
que.emplace_back(i, j);
dist[i][j] = 0;
}
}
}
for (int b = 0; b < que.size(); b++) {
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx * dx + dy * dy == 1) {
int xk = que[b].first + dx;
int yk = que[b].second + dy;
if (xk >= 0 && yk >= 0 && xk < h && yk < w && dist[xk][yk] == -1) {
que.emplace_back(xk, yk);
dist[xk][yk] = dist[que[b].first][que[b].second] + 1;
}
}
}
}
}
cout << dist[que.back().first][que.back().second] << endl;
// for(int i=0; i<h; i++){
// for(int j=0; j<w; j++){
// cout << dist[i][j];
// }
// cout << endl;
// }
} | #include <bits/stdc++.h>
using namespace std;
int main() {
// #ifndef ONLINE_JUDGE
// freopen("./a.in","r",stdin);
// freopen("./a.out","w",stdout);
// #endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int h, w;
cin >> h >> w;
vector<string> grid(h);
for (int i = 0; i < h; i++) {
cin >> grid[i];
}
vector<vector<int>> dist(h, vector<int>(w, -1));
vector<pair<int, int>> que;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (grid[i][j] == '#') {
que.emplace_back(i, j);
dist[i][j] = 0;
}
}
}
for (int b = 0; b < que.size(); b++) {
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx * dx + dy * dy == 1) {
int xk = que[b].first + dx;
int yk = que[b].second + dy;
if (xk >= 0 && yk >= 0 && xk < h && yk < w && dist[xk][yk] == -1) {
que.emplace_back(xk, yk);
dist[xk][yk] = dist[que[b].first][que[b].second] + 1;
}
}
}
}
}
cout << dist[que.back().first][que.back().second] << endl;
// for(int i=0; i<h; i++){
// for(int j=0; j<w; j++){
// cout << dist[i][j];
// }
// cout << endl;
// }
} | replace | 4 | 8 | 4 | 8 | -6 | terminate called after throwing an instance of 'std::length_error'
what(): cannot create std::vector larger than max_size()
|
p03053 | C++ | Time Limit Exceeded | #define _CRT_SECURE_NO_WARNINGS
#define MOD 998244353
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (ll i = (a); i > (b); i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n - 1, -1)
#define ALL(obj) (obj).begin(), (obj).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define optimize_cin() \
cin.tie(0); \
ios::sync_with_stdio(false)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll madd(ll a, ll b) {
a %= MOD;
b %= MOD;
return (a + b) % MOD;
}
inline ll msub(ll a, ll b) {
a %= MOD;
b %= MOD;
a += MOD;
return (a - b) % MOD;
}
inline ll mmul(ll a, ll b) {
a %= MOD;
b %= MOD;
return (a * b) % MOD;
}
inline ll mpow(ll a, ll r) {
long long res = 1;
while (r > 0) {
if (r & 1)
res = res * a % MOD;
a = a * a % MOD;
r >>= 1;
}
return res;
}
inline ll minv(ll a) {
a %= MOD;
ll b = MOD, u = 0, v = 1;
while (a) {
ll t = b / a;
b -= t * a;
swap(a, b);
u -= t * v;
swap(u, v);
}
if (u < 0)
u += MOD;
return u;
}
inline ll mdiv(ll a, ll b) {
a %= MOD;
a *= minv(b);
return a % MOD;
}
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool same(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
/*
int vs[200001];
int depth[200001];
int id[100001];
vvi G;
int rmqdat[200001];
int rmqind[200001];
void dfs(int v, int p, int d, int& k) {
id[v] = k;
vs[k] = v;
depth[k++] = d;
REP(i, G[v].size()) {
if (G[v][i] != p) {
dfs(G[v][i], v, d + 1, k);
vs[k] = v;
depth[k++] = d;
}
}
}
void init(int V) {
int k = 0;
dfs(0, -1, 0, k);
}
void rmq_init(int* dat, int datlen) {
int n = 1;
while (n < datlen) n *= 2;
REP(i, n * 2) rmqdat[i] = 1000000;
for (int i = 0, j = n - 1; i < datlen; i++, j++) {
rmqdat[j] = dat[i];
rmqind[j] = i;
}
RREP(i, n - 1) {
if (rmqdat[i * 2 + 1] < rmqdat[i * 2 + 2]) {
rmqdat[i] = rmqdat[i * 2 + 1];
rmqind[i] = rmqind[i * 2 + 1];
}
else {
rmqdat[i] = rmqdat[i * 2 + 2];
rmqind[i] = rmqind[i * 2 + 2];
}
}
}
pi query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) return pi(1000000, -1);
if (a <= l && r <= b) return pi(rmqdat[k], rmqind[k]);
else {
pi pl = query(a, b, k * 2 + 1, l, (l + r) / 2);
pi pr = query(a, b, k * 2 + 2, (l + r) / 2, r);
if (pl.first < pr.first) return pl;
else return pr;
}
}
int lca(int u, int v, int n) {
return vs[query(min(id[u], id[v]), max(id[u], id[v]) + 1, 0, 0, n * 2 -
1).second];
}
int n, u, v;
cin >> n;
REP(i, n + 1) G.push_back(vi());
REP(i, n - 1) {
cin >> u >> v;
u--; v--;
G[u].push_back(v);
G[v].push_back(u);
}
init(n);
rmq_init(depth, n * 2 - 1);
int q;
cin >> q;
REP(i, q) {
int x, y, a, b, k;
cin >> x >> y >> a >> b >> k;
}
*/
/*
ll gcd(ll a, ll b) {
if (a < 0) a *= -1;
if (b < 0) b *= -1;
if (a < b) {
ll c = b;
b = a; a = c;
}
if ((a % b) == 0) return b;
else return gcd(b, a % b);
}
pll kiyaku(ll a, ll b) {
if (a == 0) return pll(0, 1);
if (b == 0) return pll(1, 0);
return pll(a / gcd(a, b), b / gcd(a, b));
}
*/
template <typename T> struct BinaryIndexedTree {
vector<T> data;
BinaryIndexedTree(int sz) { data.assign(++sz, 0); }
T sum(int k) { // inclusive
T ret = 0;
for (++k; k > 0; k -= k & -k)
ret += data[k];
return (ret);
}
void add(int k, T x) {
for (++k; k < data.size(); k += k & -k)
data[k] += x;
}
};
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
/*
ll fact[MAXN_COMBI+1];
void init_combi(void){
fact[0] = 1;
REP(i, MAXN_COMBI) fact[i + 1] = mmul(fact[i], i+1);
REP(i, n) {
res = madd(res, combi(n / (i + 1)-1, k-1));
}
}
inline ll combi(int a, int b) {
if (a < b) return 0;
return mmul(fact[a], mmul(minv(fact[b]), minv(fact[a - b])));
}
*/
int main() {
optimize_cin();
int n, m;
cin >> n >> m;
vvi arr(n, vi(m));
stack<pi> st1, st2;
int cnt = 0;
REP(i, n) {
string s;
cin >> s;
REP(j, m) {
if (s[j] == '#') {
arr[i][j] = 1;
st1.push(pi(i, j));
cnt++;
}
}
}
if (cnt == n * m) {
cout << 0 << endl;
return 0;
}
int res = 0;
vector<pi> d;
d.push_back(pi(-1, 0));
d.push_back(pi(1, 0));
d.push_back(pi(0, 1));
d.push_back(pi(0, -1));
while (cnt < n * m) {
while (!st1.empty()) {
pi p = st1.top();
st1.pop();
for (pi dp : d) {
int ii = p.first + dp.first;
int jj = p.second + dp.second;
ii = min(ii, n - 1);
ii = max(ii, 0);
jj = min(jj, m - 1);
jj = max(jj, 0);
if (arr[ii][jj] == 0) {
arr[ii][jj] = 1;
cnt++;
st2.push(pi(ii, jj));
}
}
}
st1 = st2;
res++;
}
cout << res << endl;
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#define MOD 998244353
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (ll i = (a); i > (b); i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n - 1, -1)
#define ALL(obj) (obj).begin(), (obj).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define optimize_cin() \
cin.tie(0); \
ios::sync_with_stdio(false)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll madd(ll a, ll b) {
a %= MOD;
b %= MOD;
return (a + b) % MOD;
}
inline ll msub(ll a, ll b) {
a %= MOD;
b %= MOD;
a += MOD;
return (a - b) % MOD;
}
inline ll mmul(ll a, ll b) {
a %= MOD;
b %= MOD;
return (a * b) % MOD;
}
inline ll mpow(ll a, ll r) {
long long res = 1;
while (r > 0) {
if (r & 1)
res = res * a % MOD;
a = a * a % MOD;
r >>= 1;
}
return res;
}
inline ll minv(ll a) {
a %= MOD;
ll b = MOD, u = 0, v = 1;
while (a) {
ll t = b / a;
b -= t * a;
swap(a, b);
u -= t * v;
swap(u, v);
}
if (u < 0)
u += MOD;
return u;
}
inline ll mdiv(ll a, ll b) {
a %= MOD;
a *= minv(b);
return a % MOD;
}
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool same(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
/*
int vs[200001];
int depth[200001];
int id[100001];
vvi G;
int rmqdat[200001];
int rmqind[200001];
void dfs(int v, int p, int d, int& k) {
id[v] = k;
vs[k] = v;
depth[k++] = d;
REP(i, G[v].size()) {
if (G[v][i] != p) {
dfs(G[v][i], v, d + 1, k);
vs[k] = v;
depth[k++] = d;
}
}
}
void init(int V) {
int k = 0;
dfs(0, -1, 0, k);
}
void rmq_init(int* dat, int datlen) {
int n = 1;
while (n < datlen) n *= 2;
REP(i, n * 2) rmqdat[i] = 1000000;
for (int i = 0, j = n - 1; i < datlen; i++, j++) {
rmqdat[j] = dat[i];
rmqind[j] = i;
}
RREP(i, n - 1) {
if (rmqdat[i * 2 + 1] < rmqdat[i * 2 + 2]) {
rmqdat[i] = rmqdat[i * 2 + 1];
rmqind[i] = rmqind[i * 2 + 1];
}
else {
rmqdat[i] = rmqdat[i * 2 + 2];
rmqind[i] = rmqind[i * 2 + 2];
}
}
}
pi query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) return pi(1000000, -1);
if (a <= l && r <= b) return pi(rmqdat[k], rmqind[k]);
else {
pi pl = query(a, b, k * 2 + 1, l, (l + r) / 2);
pi pr = query(a, b, k * 2 + 2, (l + r) / 2, r);
if (pl.first < pr.first) return pl;
else return pr;
}
}
int lca(int u, int v, int n) {
return vs[query(min(id[u], id[v]), max(id[u], id[v]) + 1, 0, 0, n * 2 -
1).second];
}
int n, u, v;
cin >> n;
REP(i, n + 1) G.push_back(vi());
REP(i, n - 1) {
cin >> u >> v;
u--; v--;
G[u].push_back(v);
G[v].push_back(u);
}
init(n);
rmq_init(depth, n * 2 - 1);
int q;
cin >> q;
REP(i, q) {
int x, y, a, b, k;
cin >> x >> y >> a >> b >> k;
}
*/
/*
ll gcd(ll a, ll b) {
if (a < 0) a *= -1;
if (b < 0) b *= -1;
if (a < b) {
ll c = b;
b = a; a = c;
}
if ((a % b) == 0) return b;
else return gcd(b, a % b);
}
pll kiyaku(ll a, ll b) {
if (a == 0) return pll(0, 1);
if (b == 0) return pll(1, 0);
return pll(a / gcd(a, b), b / gcd(a, b));
}
*/
template <typename T> struct BinaryIndexedTree {
vector<T> data;
BinaryIndexedTree(int sz) { data.assign(++sz, 0); }
T sum(int k) { // inclusive
T ret = 0;
for (++k; k > 0; k -= k & -k)
ret += data[k];
return (ret);
}
void add(int k, T x) {
for (++k; k < data.size(); k += k & -k)
data[k] += x;
}
};
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
/*
ll fact[MAXN_COMBI+1];
void init_combi(void){
fact[0] = 1;
REP(i, MAXN_COMBI) fact[i + 1] = mmul(fact[i], i+1);
REP(i, n) {
res = madd(res, combi(n / (i + 1)-1, k-1));
}
}
inline ll combi(int a, int b) {
if (a < b) return 0;
return mmul(fact[a], mmul(minv(fact[b]), minv(fact[a - b])));
}
*/
int main() {
optimize_cin();
int n, m;
cin >> n >> m;
vvi arr(n, vi(m));
stack<pi> st1, st2;
int cnt = 0;
REP(i, n) {
string s;
cin >> s;
REP(j, m) {
if (s[j] == '#') {
arr[i][j] = 1;
st1.push(pi(i, j));
cnt++;
}
}
}
if (cnt == n * m) {
cout << 0 << endl;
return 0;
}
int res = 0;
vector<pi> d;
d.push_back(pi(-1, 0));
d.push_back(pi(1, 0));
d.push_back(pi(0, 1));
d.push_back(pi(0, -1));
while (cnt < n * m) {
while (!st1.empty()) {
pi p = st1.top();
st1.pop();
for (pi dp : d) {
int ii = p.first + dp.first;
int jj = p.second + dp.second;
ii = min(ii, n - 1);
ii = max(ii, 0);
jj = min(jj, m - 1);
jj = max(jj, 0);
if (arr[ii][jj] == 0) {
arr[ii][jj] = 1;
cnt++;
st2.push(pi(ii, jj));
}
}
}
st1 = st2;
st2 = stack<pi>();
res++;
}
cout << res << endl;
return 0;
}
| insert | 399 | 399 | 399 | 400 | TLE | |
p03053 | C++ | Runtime Error | #include <cstdio>
#include <queue>
#include <tuple>
using namespace std;
int main(int argc, char const *argv[]) {
int h, w;
scanf("%d %d\n", h, w);
char maze[h][w + 1];
for (int i = 0; i < h; i++) {
scanf("%s\n", maze[i]);
}
int ans = 0;
queue<tuple<int, int, int>> que;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (maze[i][j] == '#')
que.push(make_tuple(i, j, 0));
}
}
int dxy[] = {0, 1, 0, -1, 0};
while (!que.empty()) {
int x, y, d;
tie(x, y, d) = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dxy[i];
int ny = y + dxy[i + 1];
if (nx < 0 || h <= nx || ny < 0 || w <= ny)
continue;
else if (maze[nx][ny] == '.') {
maze[nx][ny] = '#';
que.push(make_tuple(nx, ny, d + 1));
ans = d + 1;
}
}
}
printf("%d\n", ans);
return 0;
}
| #include <cstdio>
#include <queue>
#include <tuple>
using namespace std;
int main(int argc, char const *argv[]) {
int h, w;
scanf("%d %d\n", &h, &w);
char maze[h][w + 1];
for (int i = 0; i < h; i++) {
scanf("%s\n", maze[i]);
}
int ans = 0;
queue<tuple<int, int, int>> que;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (maze[i][j] == '#')
que.push(make_tuple(i, j, 0));
}
}
int dxy[] = {0, 1, 0, -1, 0};
while (!que.empty()) {
int x, y, d;
tie(x, y, d) = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dxy[i];
int ny = y + dxy[i + 1];
if (nx < 0 || h <= nx || ny < 0 || w <= ny)
continue;
else if (maze[nx][ny] == '.') {
maze[nx][ny] = '#';
que.push(make_tuple(nx, ny, d + 1));
ans = d + 1;
}
}
}
printf("%d\n", ans);
return 0;
}
| replace | 8 | 9 | 8 | 9 | -11 | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reps(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))
typedef vector<int> vint;
typedef pair<int, int> pint;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T, typename U> inline bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T, typename U> inline bool chmin(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
struct SetUp {
SetUp() {
cout << fixed << setprecision(20);
cerr << fixed << setprecision(20);
};
} setUp;
int H, W;
bool board[1100000];
signed main() {
cin >> H >> W;
deque<pint> q;
rep(i, H) {
string A;
cin >> A;
rep(j, W) {
board[i * W + j] = A[j] == '#';
if (board[i * W + j]) {
q.push_front({i * W + j, 0});
}
}
}
int ans = 0;
while (!q.empty()) {
cerr << endl;
int cur = q.front().first;
int dist = q.front().second;
cerr << cur << endl;
rep(i, q.size()) { cerr << q[i].first << " "; }
q.pop_front();
int cy = cur / W, cx = cur % W;
chmax(ans, dist);
rep(i, 4) {
int ny = cy + DY[i], nx = cx + DX[i];
if (ny < 0 || ny >= H || nx < 0 || nx >= W) {
continue;
}
if (board[ny * W + nx]) {
continue;
}
board[ny * W + nx] = true;
q.push_back({ny * W + nx, dist + 1});
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reps(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))
typedef vector<int> vint;
typedef pair<int, int> pint;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T, typename U> inline bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T, typename U> inline bool chmin(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
struct SetUp {
SetUp() {
cout << fixed << setprecision(20);
cerr << fixed << setprecision(20);
};
} setUp;
int H, W;
bool board[1100000];
signed main() {
cin >> H >> W;
deque<pint> q;
rep(i, H) {
string A;
cin >> A;
rep(j, W) {
board[i * W + j] = A[j] == '#';
if (board[i * W + j]) {
q.push_front({i * W + j, 0});
}
}
}
int ans = 0;
while (!q.empty()) {
cerr << endl;
int cur = q.front().first;
int dist = q.front().second;
q.pop_front();
int cy = cur / W, cx = cur % W;
chmax(ans, dist);
rep(i, 4) {
int ny = cy + DY[i], nx = cx + DX[i];
if (ny < 0 || ny >= H || nx < 0 || nx >= W) {
continue;
}
if (board[ny * W + nx]) {
continue;
}
board[ny * W + nx] = true;
q.push_back({ny * W + nx, dist + 1});
}
}
cout << ans << endl;
} | delete | 56 | 58 | 56 | 56 | TLE | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define dunk(a) cout << (a) << endl
#define rall(a) (a).rbegin(), (a).rend()
const int INF = 2e9;
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;
char G[55][55];
int dist[55][55];
queue<pair<int, int>> que;
// North, west, south, east
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};
int main() {
int h, w;
cin >> h >> w;
int ans = 0;
rep(i, h) {
rep(j, w) {
cin >> G[i][j];
dist[i][j] = -1;
if (G[i][j] == '#') {
que.push(P(i, j));
dist[i][j] = 0;
}
}
}
while (!que.empty()) {
P p = que.front();
que.pop();
rep(i, 4) {
int ny = p.first + dy[i];
int nx = p.second + dx[i];
if (-1 < ny && ny < h && -1 < nx && nx < w && G[ny][nx] == '.' &&
dist[ny][nx] == -1) {
dist[ny][nx] = dist[p.first][p.second] + 1;
que.push(P(ny, nx));
}
}
}
rep(i, h) {
rep(j, w) { ans = max(ans, dist[i][j]); }
}
dunk(ans);
} | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define dunk(a) cout << (a) << endl
#define rall(a) (a).rbegin(), (a).rend()
const int INF = 2e9;
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;
char G[1100][1100];
int dist[1100][1100];
queue<pair<int, int>> que;
// North, west, south, east
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};
int main() {
int h, w;
cin >> h >> w;
int ans = 0;
rep(i, h) {
rep(j, w) {
cin >> G[i][j];
dist[i][j] = -1;
if (G[i][j] == '#') {
que.push(P(i, j));
dist[i][j] = 0;
}
}
}
while (!que.empty()) {
P p = que.front();
que.pop();
rep(i, 4) {
int ny = p.first + dy[i];
int nx = p.second + dx[i];
if (-1 < ny && ny < h && -1 < nx && nx < w && G[ny][nx] == '.' &&
dist[ny][nx] == -1) {
dist[ny][nx] = dist[p.first][p.second] + 1;
que.push(P(ny, nx));
}
}
}
rep(i, h) {
rep(j, w) { ans = max(ans, dist[i][j]); }
}
dunk(ans);
} | replace | 11 | 13 | 11 | 13 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
char s[1005][1005];
int d[1005][1005];
int x[1005], y[1005];
int h, w;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int main() {
scanf("%d%d", &h, &w);
getchar();
int t = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
scanf("%c", &s[i][j]);
if (s[i][j] == '.')
d[i][j] = 1e9;
else {
d[i][j] = 0;
x[t] = i;
y[t++] = j;
}
}
getchar();
}
int s = 0, ans = 0;
while (s < t) {
int xx = x[s], yy = y[s++];
for (int i = 0; i < 4; i++) {
int u = xx + dx[i];
int v = yy + dy[i];
if (u < 1 || u > h || v < 1 || v > w || d[u][v] <= d[xx][yy] + 1)
continue;
d[u][v] = d[xx][yy] + 1;
ans = max(ans, d[u][v]);
x[t] = u;
y[t++] = v;
}
}
printf("%d\n", ans);
} | #include <bits/stdc++.h>
using namespace std;
char s[1005][1005];
int d[1005][1005];
int x[1000005], y[1000005];
int h, w;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int main() {
scanf("%d%d", &h, &w);
getchar();
int t = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
scanf("%c", &s[i][j]);
if (s[i][j] == '.')
d[i][j] = 1e9;
else {
d[i][j] = 0;
x[t] = i;
y[t++] = j;
}
}
getchar();
}
int s = 0, ans = 0;
while (s < t) {
int xx = x[s], yy = y[s++];
for (int i = 0; i < 4; i++) {
int u = xx + dx[i];
int v = yy + dy[i];
if (u < 1 || u > h || v < 1 || v > w || d[u][v] <= d[xx][yy] + 1)
continue;
d[u][v] = d[xx][yy] + 1;
ans = max(ans, d[u][v]);
x[t] = u;
y[t++] = v;
}
}
printf("%d\n", ans);
} | replace | 4 | 5 | 4 | 5 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y
using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
#define FOR(i, start, end) \
for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end); \
i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) for (i64 i = 1; i <= n; ++i)
#define RREP(i, n) for (i64 i = n - 1; i >= 0; --i)
#define RREPS(i, n) for (i64 i = n; i >= 1; --i)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define pb push_back
#define mp make_pair
#define F first
#define S second
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename C> i64 SIZE(const C &c) {
return static_cast<i64>(c.size());
}
template <typename T, size_t N> i64 SIZE(const T (&)[N]) {
return static_cast<i64>(N);
}
struct ProconInit {
static constexpr int IOS_PREC = 15;
static constexpr bool AUTOFLUSH = false;
ProconInit() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(IOS_PREC);
if (AUTOFLUSH)
cout << unitbuf;
}
} PROCON_INIT;
#define MAX_H 1000
#define MAX_W 1000
#define INF 1152921504606846976
typedef pair<i64, i64> P;
i64 H, W;
char G[MAX_H][MAX_W];
i64 d[MAX_H][MAX_W];
vector<P> start;
i64 dy[] = {1, 0, -1, 0};
i64 dx[] = {0, 1, 0, -1};
bool inside(i64 y, i64 x) { return (0 <= y < H && 0 <= x < H); }
void solve() {
REP(y, H) {
REP(x, W) { d[y][x] = INF; }
}
queue<P> que;
for (auto it = start.begin(); it != start.end(); it++) {
que.push(*it);
d[(*it).F][(*it).S] = 0;
}
i64 ny, nx;
while (!que.empty()) {
P yx = que.front();
que.pop();
REP(i, 4) {
ny = yx.F + dy[i];
nx = yx.S + dx[i];
if (inside(ny, nx) && d[ny][nx] == INF && G[ny][nx] == '.') {
que.push(mp(ny, nx));
d[ny][nx] = d[yx.F][yx.S] + 1;
G[ny][nx] = 'o';
}
}
}
i64 maxd = 0;
REP(y, H) {
REP(x, W) {
if (G[y][x] == 'o') {
chmax(maxd, d[y][x]);
}
}
}
cout << maxd << endl;
}
int main() {
cin >> H >> W;
REP(y, H) {
REP(x, W) {
cin >> G[y][x];
if (G[y][x] == '#') {
start.pb(mp(y, x));
}
}
}
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y
using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
#define FOR(i, start, end) \
for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end); \
i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) for (i64 i = 1; i <= n; ++i)
#define RREP(i, n) for (i64 i = n - 1; i >= 0; --i)
#define RREPS(i, n) for (i64 i = n; i >= 1; --i)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define pb push_back
#define mp make_pair
#define F first
#define S second
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename C> i64 SIZE(const C &c) {
return static_cast<i64>(c.size());
}
template <typename T, size_t N> i64 SIZE(const T (&)[N]) {
return static_cast<i64>(N);
}
struct ProconInit {
static constexpr int IOS_PREC = 15;
static constexpr bool AUTOFLUSH = false;
ProconInit() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(IOS_PREC);
if (AUTOFLUSH)
cout << unitbuf;
}
} PROCON_INIT;
#define MAX_H 1000
#define MAX_W 1000
#define INF 1152921504606846976
typedef pair<i64, i64> P;
i64 H, W;
char G[MAX_H][MAX_W];
i64 d[MAX_H][MAX_W];
vector<P> start;
i64 dy[] = {1, 0, -1, 0};
i64 dx[] = {0, 1, 0, -1};
bool inside(i64 y, i64 x) { return (0 <= y && y < H && 0 <= x && x < W); }
void solve() {
REP(y, H) {
REP(x, W) { d[y][x] = INF; }
}
queue<P> que;
for (auto it = start.begin(); it != start.end(); it++) {
que.push(*it);
d[(*it).F][(*it).S] = 0;
}
i64 ny, nx;
while (!que.empty()) {
P yx = que.front();
que.pop();
REP(i, 4) {
ny = yx.F + dy[i];
nx = yx.S + dx[i];
if (inside(ny, nx) && d[ny][nx] == INF && G[ny][nx] == '.') {
que.push(mp(ny, nx));
d[ny][nx] = d[yx.F][yx.S] + 1;
G[ny][nx] = 'o';
}
}
}
i64 maxd = 0;
REP(y, H) {
REP(x, W) {
if (G[y][x] == 'o') {
chmax(maxd, d[y][x]);
}
}
}
cout << maxd << endl;
}
int main() {
cin >> H >> W;
REP(y, H) {
REP(x, W) {
cin >> G[y][x];
if (G[y][x] == '#') {
start.pb(mp(y, x));
}
}
}
solve();
return 0;
} | replace | 90 | 91 | 90 | 91 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
const int MOD = 1000000007;
const int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H, W;
cin >> H >> W;
vector<string> s(H);
for (int i = 0; i < H; ++i) {
cin >> s[i];
}
int dist[51][51];
fill(dist[0], dist[H], INF);
queue<P> que;
rep(i, H) {
rep(j, W) {
if (s[i][j] == '#') {
dist[i][j] = 0;
que.push(P(i, j));
}
}
}
int ans = 0;
while (!que.empty()) {
P p = que.front();
que.pop();
int x = p.first;
int y = p.second;
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || nx >= H || ny < 0 || ny >= W)
continue;
if (s[nx][ny] == '#')
continue;
if (dist[nx][ny] > dist[x][y] + 1) {
dist[nx][ny] = dist[x][y] + 1;
que.push(P(nx, ny));
ans = max(ans, dist[nx][ny]);
s[nx][ny] = '#';
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
const int MOD = 1000000007;
const int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H, W;
cin >> H >> W;
vector<string> s(H);
for (int i = 0; i < H; ++i) {
cin >> s[i];
}
int dist[H][W];
fill(dist[0], dist[H], INF);
queue<P> que;
rep(i, H) {
rep(j, W) {
if (s[i][j] == '#') {
dist[i][j] = 0;
que.push(P(i, j));
}
}
}
int ans = 0;
while (!que.empty()) {
P p = que.front();
que.pop();
int x = p.first;
int y = p.second;
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || nx >= H || ny < 0 || ny >= W)
continue;
if (s[nx][ny] == '#')
continue;
if (dist[nx][ny] > dist[x][y] + 1) {
dist[nx][ny] = dist[x][y] + 1;
que.push(P(nx, ny));
ans = max(ans, dist[nx][ny]);
s[nx][ny] = '#';
}
}
}
cout << ans << endl;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
struct S {
int x, y, num;
};
char in[105][105];
int N, M, A[105][105], ans;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
queue<S> q;
bool safe(int x, int y) {
if (0 < x && x <= N && 0 < y && y <= M)
return 1;
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++)
cin >> in[i] + 1;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if (in[i][j] == '#') {
A[i][j] = 1;
q.push({i, j, 0});
}
}
}
while (!q.empty()) {
int x = q.front().x, y = q.front().y, num = q.front().num;
q.pop();
ans = max(ans, num);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (A[nx][ny] == 0 && safe(nx, ny)) {
A[nx][ny] = 1;
q.push({nx, ny, num + 1});
}
}
}
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
struct S {
int x, y, num;
};
char in[1005][1005];
int N, M, A[1005][1005], ans;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
queue<S> q;
bool safe(int x, int y) {
if (0 < x && x <= N && 0 < y && y <= M)
return 1;
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++)
cin >> in[i] + 1;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if (in[i][j] == '#') {
A[i][j] = 1;
q.push({i, j, 0});
}
}
}
while (!q.empty()) {
int x = q.front().x, y = q.front().y, num = q.front().num;
q.pop();
ans = max(ans, num);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (A[nx][ny] == 0 && safe(nx, ny)) {
A[nx][ny] = 1;
q.push({nx, ny, num + 1});
}
}
}
cout << ans;
}
| replace | 11 | 13 | 11 | 13 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int H, W;
cin >> H >> W;
vector<vector<char>> Masu(H, vector<char>(W));
vector<vector<int>> Map(H, vector<int>(W, -1));
deque<int> queY;
deque<int> queX;
getchar();
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
Masu[i][j] = getchar();
if (Masu[i][j] == '#') {
queY.push_back(i);
queX.push_back(j);
Map[i][j] = 0;
}
}
getchar();
}
int dy[4] = {1, 0, -1, 0};
int dx[4] = {0, 1, 0, -1};
int ans = 0;
while (!queY.empty()) {
int y = queY.front(), x = queX.front();
queY.pop_front();
queX.pop_front();
int cnt = Map[y][x] + 1;
for (int i = 0; i < 4; i++) {
int newy = y + dy[i], newx = x + dx[i];
if (newy < 0 || newy >= H || newx < 0 || newx >= W)
continue;
else if (Map[newy][newx] == -1) {
queY.push_back(newy);
queX.push_back(newx);
Map[newy][newx] = cnt;
ans = max(ans, cnt);
}
}
}
cout << ans << "\n";
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int H, W;
cin >> H >> W;
vector<vector<char>> Masu(H, vector<char>(W));
vector<vector<int>> Map(H, vector<int>(W, -1));
deque<int> queY;
deque<int> queX;
getchar();
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
Masu[i][j] = getchar();
if (Masu[i][j] == '#') {
queY.push_back(i);
queX.push_back(j);
Map[i][j] = 0;
}
}
getchar();
}
int dy[4] = {1, 0, -1, 0};
int dx[4] = {0, 1, 0, -1};
int ans = 0;
while (!queY.empty()) {
int y = queY.front(), x = queX.front();
queY.pop_front();
queX.pop_front();
int cnt = Map[y][x] + 1;
for (int i = 0; i < 4; i++) {
int newy = y + dy[i], newx = x + dx[i];
if (newy < 0 || newy >= H || newx < 0 || newx >= W)
continue;
else if (Map[newy][newx] == -1) {
queY.push_back(newy);
queX.push_back(newx);
Map[newy][newx] = cnt;
ans = max(ans, cnt);
}
}
}
cout << ans << "\n";
} | replace | 10 | 12 | 10 | 12 | 0 | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vec;
typedef vector<vec> vec2;
typedef map<ll, ll> MPll;
typedef set<ll> setl;
const ll INF = 1ll << 60;
const ld EPS = 1e-10;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// for文
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define FORE(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define REP(i, size) for (ll i = (ll)0; i < size; i++)
#define REPE(i, size) for (ll i = (ll)0; i <= size; i++)
#define REPR(i, size) for (ll i = (ll)size; i >= 0; i--)
#define FOREACH(it, vec) for (auto it = vec.begin(); it != vec.end(); it++)
// ソート
#define ALL(vec) (vec).begin(), (vec).end()
#define SORT(vec) sort(ALL(vec))
#define SORTA(arr) sort(arr, arr + (sizeof(arr) / sizeof(ll)))
#define INVSORT(vec) sort((vec).rbegin(), (vec).rend())
#define REV(vec) reverse(ALL(vec))
#define REVA(arr) reverse(arr, arr + (sizeof(arr) / sizeof(ll)))
#define INVSORTA(arr) sort(arr, arr + (sizeof(arr) / sizeof(ll))), REVA(arr)
// 最大値最小値
#define MAX(vec) *max_element(ALL(vec))
#define UNIQ(vec) \
SORT(vec); \
vec.erase(unique(ALL(vec)), vec.end())
#define MIN(vec) *min_element(ALL(vec))
// 出力
#define printl(a) cout << a << "\n"
#define print(a) cout << a
#define OUT(a) printf("%lld\n", a)
#define OUTA(array) \
REP(i, sizeof(array) / sizeof(ll)) printf("%lld\n", array[i])
#define OUTV(vec) REP(i, vec.size()) printf("%lld\n", vec[i])
#define SP printf(" ")
// 入力
#define IN(x) scanf("%lld", &x)
#define INV(vec) REP(i, vec.size()) scanf("%lld", &vec[i])
#define INA(array) REP(i, sizeof(array) / sizeof(ll)) scanf("%lld", array + i)
#define INS(x) cin >> x
#define INCH(x) scanf(" %c", &x)
// 型
#define P pair
#define vp vector<P>
#define F first
#define S second
// その他
#define PB push_back
#define MP make_pair
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define INFI(a) memset(a, INF, sizeof(a))
#define MEM(a, b) memset(a, (b), sizeof(a))
// 関数
template <class T> inline void amax(T &a, const T &b) {
if (a < b)
a = b;
}
template <class T> inline void amin(T &a, const T &b) {
if (a > b)
a = b;
}
/*
struct structure{
ll num1,num2,string s;
bool operator<(const rest &another) const{
return this->s < another.s;
}
structure(ll a,ll b,string s){
this->num1 = a,this->num2=b,this->s=s;
}
};a
*/
// 特殊
// #define int ll
#define _CRT_SECURE_NO_WARNINGS
#define DEBUG
#ifdef DEBUG
#define debugl(x) cerr << #x << ":" << x << "\n"
#define debug(x) cerr << x
#define debugV(V) \
REP(i, V.size()) { cerr << i << ":" << V[i] << endl; }
#define debugA(A) \
REP(i, sizeof(A) / sizeof(ll)) { cerr << i << ":" << V[i] << endl; }
#else
#define debug(x)
#define debugV(x)
#define debugA(x)
#endif
signed main() {
ll H, W;
IN(H);
IN(W);
char a;
vec2 dis(W, vec(H, -1));
ll ans = 0;
queue<P<int, int>> pos;
FOR(i, 0, H) {
FOR(j, 0, W) {
INCH(a);
if (a == '#') {
pos.push(MP(i, j));
dis[i][j] = 0;
}
}
}
pair<int, int> look;
int look_x;
int look_y;
int next_x;
int next_y;
while (!pos.empty()) {
look = pos.front();
pos.pop();
look_x = look.F;
look_y = look.S;
amax(ans, dis[look_x][look_y]);
FOR(i, 0, 4) {
next_x = look_x + dx[i];
next_y = look_y + dy[i];
if (next_x < 0 or next_x >= W or next_y < 0 or next_y >= H)
continue;
if (dis[next_x][next_y] == -1) {
dis[next_x][next_y] = dis[look_x][look_y] + 1;
pos.push(MP(next_x, next_y));
}
}
}
OUT(ans);
}
| #include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vec;
typedef vector<vec> vec2;
typedef map<ll, ll> MPll;
typedef set<ll> setl;
const ll INF = 1ll << 60;
const ld EPS = 1e-10;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// for文
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define FORE(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define REP(i, size) for (ll i = (ll)0; i < size; i++)
#define REPE(i, size) for (ll i = (ll)0; i <= size; i++)
#define REPR(i, size) for (ll i = (ll)size; i >= 0; i--)
#define FOREACH(it, vec) for (auto it = vec.begin(); it != vec.end(); it++)
// ソート
#define ALL(vec) (vec).begin(), (vec).end()
#define SORT(vec) sort(ALL(vec))
#define SORTA(arr) sort(arr, arr + (sizeof(arr) / sizeof(ll)))
#define INVSORT(vec) sort((vec).rbegin(), (vec).rend())
#define REV(vec) reverse(ALL(vec))
#define REVA(arr) reverse(arr, arr + (sizeof(arr) / sizeof(ll)))
#define INVSORTA(arr) sort(arr, arr + (sizeof(arr) / sizeof(ll))), REVA(arr)
// 最大値最小値
#define MAX(vec) *max_element(ALL(vec))
#define UNIQ(vec) \
SORT(vec); \
vec.erase(unique(ALL(vec)), vec.end())
#define MIN(vec) *min_element(ALL(vec))
// 出力
#define printl(a) cout << a << "\n"
#define print(a) cout << a
#define OUT(a) printf("%lld\n", a)
#define OUTA(array) \
REP(i, sizeof(array) / sizeof(ll)) printf("%lld\n", array[i])
#define OUTV(vec) REP(i, vec.size()) printf("%lld\n", vec[i])
#define SP printf(" ")
// 入力
#define IN(x) scanf("%lld", &x)
#define INV(vec) REP(i, vec.size()) scanf("%lld", &vec[i])
#define INA(array) REP(i, sizeof(array) / sizeof(ll)) scanf("%lld", array + i)
#define INS(x) cin >> x
#define INCH(x) scanf(" %c", &x)
// 型
#define P pair
#define vp vector<P>
#define F first
#define S second
// その他
#define PB push_back
#define MP make_pair
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define INFI(a) memset(a, INF, sizeof(a))
#define MEM(a, b) memset(a, (b), sizeof(a))
// 関数
template <class T> inline void amax(T &a, const T &b) {
if (a < b)
a = b;
}
template <class T> inline void amin(T &a, const T &b) {
if (a > b)
a = b;
}
/*
struct structure{
ll num1,num2,string s;
bool operator<(const rest &another) const{
return this->s < another.s;
}
structure(ll a,ll b,string s){
this->num1 = a,this->num2=b,this->s=s;
}
};a
*/
// 特殊
// #define int ll
#define _CRT_SECURE_NO_WARNINGS
#define DEBUG
#ifdef DEBUG
#define debugl(x) cerr << #x << ":" << x << "\n"
#define debug(x) cerr << x
#define debugV(V) \
REP(i, V.size()) { cerr << i << ":" << V[i] << endl; }
#define debugA(A) \
REP(i, sizeof(A) / sizeof(ll)) { cerr << i << ":" << V[i] << endl; }
#else
#define debug(x)
#define debugV(x)
#define debugA(x)
#endif
signed main() {
ll H, W;
IN(H);
IN(W);
char a;
vec2 dis(W, vec(H, -1));
ll ans = 0;
queue<P<int, int>> pos;
FOR(i, 0, H) {
FOR(j, 0, W) {
INCH(a);
if (a == '#') {
pos.push(MP(j, i));
dis[j][i] = 0;
}
}
}
pair<int, int> look;
int look_x;
int look_y;
int next_x;
int next_y;
while (!pos.empty()) {
look = pos.front();
pos.pop();
look_x = look.F;
look_y = look.S;
amax(ans, dis[look_x][look_y]);
FOR(i, 0, 4) {
next_x = look_x + dx[i];
next_y = look_y + dy[i];
if (next_x < 0 or next_x >= W or next_y < 0 or next_y >= H)
continue;
if (dis[next_x][next_y] == -1) {
dis[next_x][next_y] = dis[look_x][look_y] + 1;
pos.push(MP(next_x, next_y));
}
}
}
OUT(ans);
}
| replace | 144 | 146 | 144 | 146 | 0 | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, x, n) for (int i = x; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
#define sp(p) cout << setprecision(16) << fixed << p << endl;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define SORT(a) sort(all(a))
#define RSORT(a) sort(rall(a))
#define UNIQ(a) unique(all(a))
#define VOUT(v, i) \
cout << #v << ": "; \
for (int i = 0; i < (int)v.size(); i++) { \
cout << v[i] << " "; \
} \
cout << "\n";
#define vout(v) VOUT(v, z);
#define vmin(v) *min_element(all(v))
#define vmax(v) *max_element(all(v))
#define vsum(v) accumulate(all(v), 0LL);
#define MOUT(m, r, c) \
rep(i, r) { \
rep(j, c) { cout << m[i][j] << " "; } \
cout << endl; \
}
#define mout(m) MOUT(m, m.size(), m[0].size())
#define debg(a) cout << #a << " " << a << endl;
#define show(a) \
for (cont & y : a) { \
for (cont & x : y) { \
cout << x << " "; \
} \
cout << endl; \
}
template <class T> inline int out(const T &t) {
print(t);
putchar('\n');
return 0;
}
template <class T> inline T gcd(T a, T b) {
if (b == 0)
return a;
return (gcd(b, a % b));
}
template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
// const long long MOD=1000000007;
const long long INF = 1e18;
#define EPS (1e-7)
#define PI (acos(-1))
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;
}
vector<vector<ll>> num(1010, vector<ll>(1010, -1));
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
bool isRange(ll y, ll x, ll H, ll W) {
return (0 <= y && y < H && 0 <= x && x < W);
}
void solve(long long H, long long W, std::vector<std::string> A) {
ll val = 0;
queue<P> q;
for (ll i = 0; i < H; i++) {
for (ll j = 0; j < W; j++) {
if (A[i][j] == '#') {
q.push(P(i, j));
num[i][j] = 0;
}
}
}
while (q.size()) {
P p = q.front();
q.pop();
for (ll i = 0; i < 4; i++) {
ll ny = p.first + dy[i], nx = p.second + dx[i];
if (isRange(ny, nx, H, W) && A[ny][nx] == '.' && num[ny][nx] == -1) {
num[ny][nx] = num[p.first][p.second] + 1;
q.push(P(ny, nx));
chmax(val, num[ny][nx]);
}
}
}
// MOUT(num, H, W);
cout << val << "\n";
}
int main() {
long long H;
scanf("%lld", &H);
long long W;
scanf("%lld", &W);
std::vector<std::string> A(W);
for (int i = 0; i < W; i++) {
std::cin >> A[i];
}
solve(H, W, std::move(A));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, x, n) for (int i = x; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
#define sp(p) cout << setprecision(16) << fixed << p << endl;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define SORT(a) sort(all(a))
#define RSORT(a) sort(rall(a))
#define UNIQ(a) unique(all(a))
#define VOUT(v, i) \
cout << #v << ": "; \
for (int i = 0; i < (int)v.size(); i++) { \
cout << v[i] << " "; \
} \
cout << "\n";
#define vout(v) VOUT(v, z);
#define vmin(v) *min_element(all(v))
#define vmax(v) *max_element(all(v))
#define vsum(v) accumulate(all(v), 0LL);
#define MOUT(m, r, c) \
rep(i, r) { \
rep(j, c) { cout << m[i][j] << " "; } \
cout << endl; \
}
#define mout(m) MOUT(m, m.size(), m[0].size())
#define debg(a) cout << #a << " " << a << endl;
#define show(a) \
for (cont & y : a) { \
for (cont & x : y) { \
cout << x << " "; \
} \
cout << endl; \
}
template <class T> inline int out(const T &t) {
print(t);
putchar('\n');
return 0;
}
template <class T> inline T gcd(T a, T b) {
if (b == 0)
return a;
return (gcd(b, a % b));
}
template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
// const long long MOD=1000000007;
const long long INF = 1e18;
#define EPS (1e-7)
#define PI (acos(-1))
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;
}
vector<vector<ll>> num(1010, vector<ll>(1010, -1));
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
bool isRange(ll y, ll x, ll H, ll W) {
return (0 <= y && y < H && 0 <= x && x < W);
}
void solve(long long H, long long W, std::vector<std::string> A) {
ll val = 0;
queue<P> q;
for (ll i = 0; i < H; i++) {
for (ll j = 0; j < W; j++) {
if (A[i][j] == '#') {
q.push(P(i, j));
num[i][j] = 0;
}
}
}
while (q.size()) {
P p = q.front();
q.pop();
for (ll i = 0; i < 4; i++) {
ll ny = p.first + dy[i], nx = p.second + dx[i];
if (isRange(ny, nx, H, W) && A[ny][nx] == '.' && num[ny][nx] == -1) {
num[ny][nx] = num[p.first][p.second] + 1;
q.push(P(ny, nx));
chmax(val, num[ny][nx]);
}
}
}
// MOUT(num, H, W);
cout << val << "\n";
}
int main() {
long long H;
scanf("%lld", &H);
long long W;
scanf("%lld", &W);
std::vector<std::string> A(H);
for (int i = 0; i < H; i++) {
std::cin >> A[i];
}
solve(H, W, std::move(A));
return 0;
}
| replace | 107 | 109 | 107 | 109 | 0 | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int DYX[4][2]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int H, W;
int ans = 0;
string graph[50];
int seen[50][50];
queue<P> que;
void bfs();
int main() {
cin >> H >> W;
for (int h = 0; h < H; h++) {
cin >> graph[h];
for (int w = 0; w < W; w++) {
seen[h][w] = -1;
if (graph[h][w] == '#') {
que.push(P(h, w));
seen[h][w] = 0;
}
}
}
bfs();
cout << ans << "\n";
return 0;
}
void bfs() {
while (que.size()) {
P next = que.front();
que.pop();
int y = next.first, x = next.second;
for (auto dyx : DYX) {
int ny = y + dyx[0], nx = x + dyx[1];
if (0 <= ny && ny < H && 0 <= nx && nx < W && graph[ny][nx] != '#' &&
seen[ny][nx] == -1) {
seen[ny][nx] = seen[y][x] + 1;
ans = max(seen[ny][nx], ans);
que.push(P(ny, nx));
}
}
}
return;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int DYX[4][2]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int H, W;
int ans = 0;
string graph[1000];
int seen[1000][1000];
queue<P> que;
void bfs();
int main() {
cin >> H >> W;
for (int h = 0; h < H; h++) {
cin >> graph[h];
for (int w = 0; w < W; w++) {
seen[h][w] = -1;
if (graph[h][w] == '#') {
que.push(P(h, w));
seen[h][w] = 0;
}
}
}
bfs();
cout << ans << "\n";
return 0;
}
void bfs() {
while (que.size()) {
P next = que.front();
que.pop();
int y = next.first, x = next.second;
for (auto dyx : DYX) {
int ny = y + dyx[0], nx = x + dyx[1];
if (0 <= ny && ny < H && 0 <= nx && nx < W && graph[ny][nx] != '#' &&
seen[ny][nx] == -1) {
seen[ny][nx] = seen[y][x] + 1;
ans = max(seen[ny][nx], ans);
que.push(P(ny, nx));
}
}
}
return;
} | replace | 16 | 18 | 16 | 18 | 0 | |
p03053 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define ran 1011
const int go[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int n, m;
char s[ran][ran];
int d[ran][ran];
int q[ran * ran], l, r;
int main() {
scanf("%d%d", &n, &m);
memset(d, -1, sizeof d);
l = r = 0;
for (int i = 0; i < n; i++) {
scanf("%s", s[i]);
for (int j = 0; j < m; j++)
if (s[i][j] == '#') {
q[r++] = i;
q[r++] = j;
d[i][j] = 0;
}
}
int ans = 0;
while (l < r) {
int cur_x = q[l++], cur_y = q[l++];
int cur_d = d[cur_x][cur_y];
for (int i = 0; i < 4; i++) {
int x = cur_x + go[i][0], y = cur_y + go[i][1];
if (x < 0 || x >= n || y < 0 || y >= m)
continue;
if (d[x][y] >= 0)
continue;
d[x][y] = cur_d + 1;
q[r++] = x;
q[r++] = y;
ans = max(ans, d[x][y]);
}
}
printf("%d\n", ans);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define ran 1011
const int go[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int n, m;
char s[ran][ran];
int d[ran][ran];
int q[ran * ran * 2], l, r;
int main() {
scanf("%d%d", &n, &m);
memset(d, -1, sizeof d);
l = r = 0;
for (int i = 0; i < n; i++) {
scanf("%s", s[i]);
for (int j = 0; j < m; j++)
if (s[i][j] == '#') {
q[r++] = i;
q[r++] = j;
d[i][j] = 0;
}
}
int ans = 0;
while (l < r) {
int cur_x = q[l++], cur_y = q[l++];
int cur_d = d[cur_x][cur_y];
for (int i = 0; i < 4; i++) {
int x = cur_x + go[i][0], y = cur_y + go[i][1];
if (x < 0 || x >= n || y < 0 || y >= m)
continue;
if (d[x][y] >= 0)
continue;
d[x][y] = cur_d + 1;
q[r++] = x;
q[r++] = y;
ans = max(ans, d[x][y]);
}
}
printf("%d\n", ans);
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#define REP(i, x, y) for (int i = (x); i < (y); i++)
#define RREP(i, x, y) for (int i = (y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
// #define int long long
using namespace std;
// conversion
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
#else
#define eprintf(...) 42
#define dump(x) 42
#define debug(x) 42
#endif
typedef long long ll;
const int MAX_N = 100000;
int solve(vector<vector<bool>> &grids, const int H, const int W) {
vector<vector<int>> distances(H, vector<int>(W, INT_MAX));
priority_queue<pair<int, pair<int, int>>> que;
REP(i, 0, H) {
REP(j, 0, W) {
if (grids[i][j]) {
distances[i][j] = 0;
que.push({0, {i, j}});
}
}
}
vector<vector<int>> didjs{{-1, 0}, {+1, 0}, {0, -1}, {0, +1}};
int answer = 0;
while (!que.empty()) {
auto el = que.top();
que.pop();
int distance = el.first;
int i = el.second.first, j = el.second.second;
for (const auto didj : didjs) {
int di = didj[0];
int dj = didj[1];
if (i + di < 0 || i + di >= H || j + dj < 0 || j + dj >= W)
continue;
if (!grids[i + di][j + dj] && distances[i + di][j + dj] > distance + 1) {
distances[i + di][j + dj] = distance + 1;
que.push({distance + 1, {i + di, j + dj}});
}
}
}
REP(i, 0, H) {
REP(j, 0, W) { answer = max(distances[i][j], answer); }
}
return answer;
}
signed main() {
// to shorten execution time for iostream
cin.tie(0);
ios::sync_with_stdio(false);
int H, W;
cin >> H >> W;
vector<vector<bool>> grids(H, vector<bool>(W, false));
REP(i, 0, H) {
REP(j, 0, W) {
char grid;
cin >> grid;
grids[i][j] = grid == '#';
}
}
cout << solve(grids, H, W) << "\n";
return 0;
}
| #include "bits/stdc++.h"
#define REP(i, x, y) for (int i = (x); i < (y); i++)
#define RREP(i, x, y) for (int i = (y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
// #define int long long
using namespace std;
// conversion
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
#else
#define eprintf(...) 42
#define dump(x) 42
#define debug(x) 42
#endif
typedef long long ll;
const int MAX_N = 100000;
int solve(vector<vector<bool>> &grids, const int H, const int W) {
vector<vector<int>> distances(H, vector<int>(W, INT_MAX));
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
greater<pair<int, pair<int, int>>>>
que;
REP(i, 0, H) {
REP(j, 0, W) {
if (grids[i][j]) {
distances[i][j] = 0;
que.push({0, {i, j}});
}
}
}
vector<vector<int>> didjs{{-1, 0}, {+1, 0}, {0, -1}, {0, +1}};
int answer = 0;
while (!que.empty()) {
auto el = que.top();
que.pop();
int distance = el.first;
int i = el.second.first, j = el.second.second;
for (const auto didj : didjs) {
int di = didj[0];
int dj = didj[1];
if (i + di < 0 || i + di >= H || j + dj < 0 || j + dj >= W)
continue;
if (!grids[i + di][j + dj] && distances[i + di][j + dj] > distance + 1) {
distances[i + di][j + dj] = distance + 1;
que.push({distance + 1, {i + di, j + dj}});
}
}
}
REP(i, 0, H) {
REP(j, 0, W) { answer = max(distances[i][j], answer); }
}
return answer;
}
signed main() {
// to shorten execution time for iostream
cin.tie(0);
ios::sync_with_stdio(false);
int H, W;
cin >> H >> W;
vector<vector<bool>> grids(H, vector<bool>(W, false));
REP(i, 0, H) {
REP(j, 0, W) {
char grid;
cin >> grid;
grids[i][j] = grid == '#';
}
}
cout << solve(grids, H, W) << "\n";
return 0;
}
| replace | 40 | 41 | 40 | 43 | TLE | |
p03053 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h, w;
vector<string> a;
inline bool can(int x, int y, vector<vector<int>> dist) {
return x >= 0 && x < h && y >= 0 && y < w && dist[x][y] == -1;
}
int vectormax(vector<vector<int>> dist) {
int ans = 0;
for (int i = 0; i < dist.size(); i++) {
ans = max(ans, *max_element(dist[i].begin(), dist[i].end()));
}
return ans;
}
int main() {
cin >> h >> w;
a.resize(h);
for (int i = 0; i < h; i++) {
cin >> a[i];
}
queue<pair<int, int>> que;
vector<vector<int>> dist(h, vector<int>(w, -1));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == '#') {
dist[i][j] = 0;
que.push(make_pair(i, j));
}
}
}
while (!que.empty()) {
pair<int, int> temp = que.front();
que.pop();
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int x, y;
x = temp.first + dx[i];
y = temp.second + dy[i];
if (can(x, y, dist)) {
dist[x][y] = dist[temp.first][temp.second] + 1;
que.emplace(make_pair(x, y));
}
}
}
cout << vectormax(dist) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h, w;
vector<string> a;
bool can(int x, int y, vector<vector<int>> &dist) {
return x >= 0 && x < h && y >= 0 && y < w && dist[x][y] == -1;
}
int vectormax(vector<vector<int>> dist) {
int ans = 0;
for (int i = 0; i < dist.size(); i++) {
ans = max(ans, *max_element(dist[i].begin(), dist[i].end()));
}
return ans;
}
int main() {
cin >> h >> w;
a.resize(h);
for (int i = 0; i < h; i++) {
cin >> a[i];
}
queue<pair<int, int>> que;
vector<vector<int>> dist(h, vector<int>(w, -1));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == '#') {
dist[i][j] = 0;
que.push(make_pair(i, j));
}
}
}
while (!que.empty()) {
pair<int, int> temp = que.front();
que.pop();
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int x, y;
x = temp.first + dx[i];
y = temp.second + dy[i];
if (can(x, y, dist)) {
dist[x][y] = dist[temp.first][temp.second] + 1;
que.emplace(make_pair(x, y));
}
}
}
cout << vectormax(dist) << endl;
}
| replace | 6 | 7 | 6 | 7 | TLE | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int Int;
typedef pair<Int, Int> pii;
typedef pair<Int, double> pid;
typedef pair<double, double> pdd;
typedef pair<Int, pii> pip;
typedef pair<double, Int> pdp;
#define A first
#define B second
#define pb(x) push_back(x)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (x).size()
#define CLR(x) memset(x, 0, sizeof x)
#define pdebug() printf("%d\n", __LINE__)
#define Itype(a) typeof(a.begin())
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define FORO(i, n) REP(i, 0, (int)n - 1)
#define FORI(i, n) REP(i, 1, (int)n)
#define FORIT(i, t) for (Itype(t) i = t.begin(); i != t.end(); i++)
#define eps 1e-6
#define sqr(x) ((x) * (x))
#define dist(_a, _b) sqrt(sqr(_a.A - _b.A) + sqr(_a.B - _b.B))
#define norm(_a) sqrt(sqr(_a.A) + sqr(_a.B))
template <typename T> void getMin(T &a, T b) {
if (a > b)
a = b;
}
template <typename T> void getMax(T &a, T b) {
if (a < b)
a = b;
}
const int inf = 1000000007;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
char A[128][128];
int d[128][128];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int H, W;
cin >> H >> W;
queue<pii> q;
FORO(i, H) {
cin >> A[i];
FORO(j, W) {
d[i][j] = inf;
if (A[i][j] == '#') {
d[i][j] = 0;
q.push(pii(i, j));
}
}
}
int ans = 0;
while (!q.empty()) {
pii p = q.front();
q.pop();
getMax(ans, d[p.A][p.B]);
// cout << p.A << " " << p.B << endl;
for (int r = 0; r < 4; r++) {
int nx = p.B + dx[r];
int ny = p.A + dy[r];
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (d[ny][nx] < inf)
continue;
d[ny][nx] = d[p.A][p.B] + 1;
q.push(pii(ny, nx));
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int Int;
typedef pair<Int, Int> pii;
typedef pair<Int, double> pid;
typedef pair<double, double> pdd;
typedef pair<Int, pii> pip;
typedef pair<double, Int> pdp;
#define A first
#define B second
#define pb(x) push_back(x)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (x).size()
#define CLR(x) memset(x, 0, sizeof x)
#define pdebug() printf("%d\n", __LINE__)
#define Itype(a) typeof(a.begin())
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define FORO(i, n) REP(i, 0, (int)n - 1)
#define FORI(i, n) REP(i, 1, (int)n)
#define FORIT(i, t) for (Itype(t) i = t.begin(); i != t.end(); i++)
#define eps 1e-6
#define sqr(x) ((x) * (x))
#define dist(_a, _b) sqrt(sqr(_a.A - _b.A) + sqr(_a.B - _b.B))
#define norm(_a) sqrt(sqr(_a.A) + sqr(_a.B))
template <typename T> void getMin(T &a, T b) {
if (a > b)
a = b;
}
template <typename T> void getMax(T &a, T b) {
if (a < b)
a = b;
}
const int inf = 1000000007;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
char A[1024][1024];
int d[1024][1024];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int H, W;
cin >> H >> W;
queue<pii> q;
FORO(i, H) {
cin >> A[i];
FORO(j, W) {
d[i][j] = inf;
if (A[i][j] == '#') {
d[i][j] = 0;
q.push(pii(i, j));
}
}
}
int ans = 0;
while (!q.empty()) {
pii p = q.front();
q.pop();
getMax(ans, d[p.A][p.B]);
// cout << p.A << " " << p.B << endl;
for (int r = 0; r < 4; r++) {
int nx = p.B + dx[r];
int ny = p.A + dy[r];
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (d[ny][nx] < inf)
continue;
d[ny][nx] = d[p.A][p.B] + 1;
q.push(pii(ny, nx));
}
}
cout << ans << endl;
return 0;
} | replace | 38 | 40 | 38 | 40 | 0 | |
p03053 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
using LL = long long;
using VLL = std::vector<LL>;
using VVLL = std::vector<VLL>;
using VVVLL = std::vector<VVLL>;
using LD = long double;
using VLD = std::vector<LD>;
using VVLD = std::vector<VLD>;
using VVVLD = std::vector<VVLD>;
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;
}
#define REP(i, n) for (LL(i) = 0; (i) < (n); (i)++)
#define ALL(v) v.begin(), v.end()
#define PRINTLN(v) \
{ \
LL i = 0; \
for (auto(a) : v) { \
cerr << i << " : " << a << endl; \
i++; \
} \
}
#define PRINTSP(v) \
for (auto(a) : v) { \
cerr << a << " "; \
} \
cerr << endl
#define cmax(a, b) \
{ \
if (a < b) { \
a = b; \
} \
}
#define cmin(a, b) \
{ \
if (a > b) { \
a = b; \
} \
}
#define EACH(a, A) for (auto a : A)
// #define cmax(a,b) a = (a>b ? a:b)
// #define cmin(a,b) a = (a<b ? a:b)
void func() {
LL H, W;
cin >> H >> W;
vector<string> M(H);
REP(i, H) { cin >> M[i]; }
queue<pair<LL, LL>> Q;
set<pair<LL, LL>> S;
REP(i, H) {
REP(j, W) {
if (M[i][j] == '#') {
Q.emplace(i, j);
S.emplace(i, j);
}
}
}
LL cnt = 0;
while (not S.empty()) {
cerr << cnt << endl;
EACH(m, M) { PRINTSP(m); }
set<pair<LL, LL>> T;
for (auto s : S) {
LL y = s.first;
LL x = s.second;
if (y + 1 < H and M[y + 1][x] == '.') {
M[y + 1][x] = '#';
T.emplace(y + 1, x);
}
if (x + 1 < W and M[y][x + 1] == '.') {
M[y][x + 1] = '#';
T.emplace(y, x + 1);
}
if (y - 1 >= 0 and M[y - 1][x] == '.') {
M[y - 1][x] = '#';
T.emplace(y - 1, x);
}
if (x - 1 >= 0 and M[y][x - 1] == '.') {
M[y][x - 1] = '#';
T.emplace(y, x - 1);
}
}
S = T;
cnt++;
}
EACH(m, M) {
// PRINTSP(m);
}
cout << cnt - 1;
return;
}
int main() {
func();
return 0;
}
| #include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
using LL = long long;
using VLL = std::vector<LL>;
using VVLL = std::vector<VLL>;
using VVVLL = std::vector<VVLL>;
using LD = long double;
using VLD = std::vector<LD>;
using VVLD = std::vector<VLD>;
using VVVLD = std::vector<VVLD>;
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;
}
#define REP(i, n) for (LL(i) = 0; (i) < (n); (i)++)
#define ALL(v) v.begin(), v.end()
#define PRINTLN(v) \
{ \
LL i = 0; \
for (auto(a) : v) { \
cerr << i << " : " << a << endl; \
i++; \
} \
}
#define PRINTSP(v) \
for (auto(a) : v) { \
cerr << a << " "; \
} \
cerr << endl
#define cmax(a, b) \
{ \
if (a < b) { \
a = b; \
} \
}
#define cmin(a, b) \
{ \
if (a > b) { \
a = b; \
} \
}
#define EACH(a, A) for (auto a : A)
// #define cmax(a,b) a = (a>b ? a:b)
// #define cmin(a,b) a = (a<b ? a:b)
void func() {
LL H, W;
cin >> H >> W;
vector<string> M(H);
REP(i, H) { cin >> M[i]; }
set<pair<LL, LL>> S;
REP(i, H) {
REP(j, W) {
if (M[i][j] == '#') {
S.emplace(i, j);
}
}
}
LL cnt = 0;
while (not S.empty()) {
// cerr << cnt<< endl;
EACH(m, M) {
// PRINTSP(m);
}
set<pair<LL, LL>> T;
for (auto s : S) {
LL y = s.first;
LL x = s.second;
if (y + 1 < H and M[y + 1][x] == '.') {
M[y + 1][x] = '#';
// T.emplace(y+1,x);
T.insert(make_pair(y + 1, x));
}
if (x + 1 < W and M[y][x + 1] == '.') {
M[y][x + 1] = '#';
// T.emplace(y,x+1);
T.insert(make_pair(y, x + 1));
}
if (y - 1 >= 0 and M[y - 1][x] == '.') {
M[y - 1][x] = '#';
// T.emplace(y-1,x);
T.insert(make_pair(y - 1, x));
}
if (x - 1 >= 0 and M[y][x - 1] == '.') {
M[y][x - 1] = '#';
// T.emplace(y,x-1);
T.insert(make_pair(y, x - 1));
}
}
S = T;
cnt++;
}
EACH(m, M) {
// PRINTSP(m);
}
cout << cnt - 1;
return;
}
void _func() {
LL H, W;
cin >> H >> W;
vector<string> M(H);
REP(i, H) { cin >> M[i]; }
queue<pair<LL, LL>> Q;
set<pair<LL, LL>> S;
REP(i, H) {
REP(j, W) {
if (M[i][j] == '#') {
Q.emplace(i, j);
S.emplace(i, j);
}
}
}
LL cnt = 0;
while (not S.empty()) {
cerr << cnt << endl;
EACH(m, M) { PRINTSP(m); }
set<pair<LL, LL>> T;
for (auto s : S) {
LL y = s.first;
LL x = s.second;
if (y + 1 < H and M[y + 1][x] == '.') {
M[y + 1][x] = '#';
T.emplace(y + 1, x);
}
if (x + 1 < W and M[y][x + 1] == '.') {
M[y][x + 1] = '#';
T.emplace(y, x + 1);
}
if (y - 1 >= 0 and M[y - 1][x] == '.') {
M[y - 1][x] = '#';
T.emplace(y - 1, x);
}
if (x - 1 >= 0 and M[y][x - 1] == '.') {
M[y][x - 1] = '#';
T.emplace(y, x - 1);
}
}
S = T;
cnt++;
}
EACH(m, M) {
// PRINTSP(m);
}
cout << cnt - 1;
return;
}
int main() {
func();
return 0;
}
| insert | 66 | 66 | 66 | 122 | TLE | |
p03053 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
if (x < y)
swap(x, y);
if (y == 0)
return x;
return gcd(y, x % y);
}
template <class T1, class T2> void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
int val = ((a % mod) + (b % mod)) % mod;
if (val < 0) {
val += mod;
}
a = val;
}
////////////////////////////////////////
int H, W;
char A[1010][1010];
int d[1010][1010];
int main() {
cin >> H >> W;
queue<pii> Q;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
cin >> A[i][j];
if (A[i][j] == '#') {
Q.push({i, j});
d[i][j] = 0;
} else {
d[i][j] = INF;
}
}
}
while (!Q.empty()) {
int y = Q.front().first;
int x = Q.front().second;
Q.pop();
for (int i = 0; i < 4; ++i) {
int ny = y + dy[i];
int nx = x + dx[i];
if (0 <= ny && ny < H && 0 <= nx && nx < W && A[ny][nx] == '.') {
Q.push({ny, nx});
d[ny][nx] = min(d[ny][nx], d[y][x] + 1);
}
}
}
int ans = 0;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
if (d[i][j] == 0)
continue;
ans = max(ans, d[i][j]);
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
if (x < y)
swap(x, y);
if (y == 0)
return x;
return gcd(y, x % y);
}
template <class T1, class T2> void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
int val = ((a % mod) + (b % mod)) % mod;
if (val < 0) {
val += mod;
}
a = val;
}
////////////////////////////////////////
int H, W;
char A[1010][1010];
int d[1010][1010];
int main() {
cin >> H >> W;
queue<pii> Q;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
cin >> A[i][j];
if (A[i][j] == '#') {
Q.push({i, j});
d[i][j] = 0;
} else {
d[i][j] = INF;
}
}
}
while (!Q.empty()) {
int y = Q.front().first;
int x = Q.front().second;
Q.pop();
for (int i = 0; i < 4; ++i) {
int ny = y + dy[i];
int nx = x + dx[i];
if (0 <= ny && ny < H && 0 <= nx && nx < W && A[ny][nx] == '.' &&
d[ny][nx] == INF) {
Q.push({ny, nx});
d[ny][nx] = min(d[ny][nx], d[y][x] + 1);
}
}
}
int ans = 0;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
if (d[i][j] == 0)
continue;
ans = max(ans, d[i][j]);
}
}
cout << ans << endl;
return 0;
}
| replace | 76 | 77 | 76 | 78 | TLE | |
p03053 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (int i = b - 1; i >= a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;
const ll INF = (1LL << 50);
set<pll> black;
vector<vector<bool>> used;
bool in_range(ll i, ll l, ll r) { return l <= i && i < r; }
void solve(long long H, long long W, std::vector<std::string> A) {
used.resize(H);
set<pll> tonari;
tonari.insert(pll(1, 0));
tonari.insert(pll(-1, 0));
tonari.insert(pll(0, 1));
tonari.insert(pll(0, -1));
For(i, H) {
used[i].resize(W);
fill(used[i].begin(), used[i].end(), false);
}
For(i, H) For(j, W) {
if (A[i][j] == '#') {
black.insert(pll(i, j));
used[i][j] = true;
}
}
set<pll> next;
ll ans = 0;
while (!black.empty()) {
next.clear();
for (auto &&i : black) {
for (auto &&j : tonari) {
if (in_range(i.first + j.first, 0, H) &&
in_range(i.second + j.second, 0, W) &&
used[i.first + j.first][i.second + j.second] == false) {
used[i.first + j.first][i.second + j.second] = true;
next.insert(pll(i.first + j.first, i.second + j.second));
}
}
}
if (next.empty())
break;
ans++;
dump(next.size());
black = next;
}
cout << ans << endl;
}
int main() {
long long H;
scanf("%lld", &H);
long long W;
scanf("%lld", &W);
std::vector<std::string> A(W);
for (int i = 0; i < H; i++) {
std::cin >> A[i];
}
solve(H, W, std::move(A));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (int i = b - 1; i >= a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;
const ll INF = (1LL << 50);
set<pll> black;
vector<vector<bool>> used;
bool in_range(ll i, ll l, ll r) { return l <= i && i < r; }
void solve(long long H, long long W, std::vector<std::string> A) {
used.resize(H);
set<pll> tonari;
tonari.insert(pll(1, 0));
tonari.insert(pll(-1, 0));
tonari.insert(pll(0, 1));
tonari.insert(pll(0, -1));
For(i, H) {
used[i].resize(W);
fill(used[i].begin(), used[i].end(), false);
}
For(i, H) For(j, W) {
if (A[i][j] == '#') {
black.insert(pll(i, j));
used[i][j] = true;
}
}
set<pll> next;
ll ans = 0;
while (!black.empty()) {
next.clear();
for (auto &&i : black) {
for (auto &&j : tonari) {
if (in_range(i.first + j.first, 0, H) &&
in_range(i.second + j.second, 0, W) &&
used[i.first + j.first][i.second + j.second] == false) {
used[i.first + j.first][i.second + j.second] = true;
next.insert(pll(i.first + j.first, i.second + j.second));
}
}
}
if (next.empty())
break;
ans++;
dump(next.size());
black = next;
}
cout << ans << endl;
}
int main() {
long long H;
scanf("%lld", &H);
long long W;
scanf("%lld", &W);
std::vector<std::string> A(H);
for (int i = 0; i < H; i++) {
std::cin >> A[i];
}
solve(H, W, std::move(A));
return 0;
}
| replace | 72 | 73 | 72 | 73 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.