solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
const int c = 100;
int n;
int a[2 * c][2 * c];
int main() {
scanf("%d", &n);
a[c][c] = n;
while (1) {
bool flag = true;
for (int i = 0; i < 2 * c; i++) {
for (int j = 0; j < 2 * c; j++) {
if (a[i][j] >= 4) {
flag = false;
a[i + 1][j] += a[i][j] / 4;
a[i - 1][j] += a[i][j] / 4;
a[i][j - 1] += a[i][j] / 4;
a[i][j + 1] += a[i][j] / 4;
a[i][j] %= 4;
}
}
}
if (flag) break;
}
int T;
scanf("%d", &T);
while (T--) {
int x, y;
scanf("%d%d", &x, &y);
if (x + c < 0 || y + c < 0 || x + c >= 2 * c || y + c >= 2 * c)
printf("0\n");
else
printf("%d\n", a[x + c][y + c]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int p[1000][1000], used[1000][1000];
queue<pair<int, int> > q;
int main() {
int n, t, k, l, x, y, i;
scanf("%d%d", &n, &t);
q.push(make_pair(500, 500));
p[500][500] = n;
used[500][500] = 1;
while (q.size()) {
x = q.front().first;
y = q.front().second;
q.pop();
k = p[x][y] / 4;
p[x][y] %= 4;
p[x + 1][y] += k;
if (!used[x + 1][y] && p[x + 1][y] >= 4)
used[x + 1][y] = 1, q.push(make_pair(x + 1, y));
p[x - 1][y] += k;
if (!used[x - 1][y] && p[x - 1][y] >= 4)
used[x - 1][y] = 1, q.push(make_pair(x - 1, y));
p[x][y + 1] += k;
if (!used[x][y + 1] && p[x][y + 1] >= 4)
used[x][y + 1] = 1, q.push(make_pair(x, y + 1));
p[x][y - 1] += k;
if (!used[x][y - 1] && p[x][y - 1] >= 4)
used[x][y - 1] = 1, q.push(make_pair(x, y - 1));
used[x][y] = 0;
}
for (i = 1; i <= t; i++) {
scanf("%d%d", &x, &y);
if (abs(x) > 400 || abs(y) > 400)
cout << 0 << endl;
else
cout << p[500 + x][500 + y] << endl;
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int num[2000][2000], vst[2000][2000];
int dr[] = {1, 0, -1, 0};
int dc[] = {0, 1, 0, -1};
queue<int> q;
int main() {
int N, r, c, rr, cc, i, Q;
scanf("%d", &N);
num[1000][1000] = N;
vst[1000][1000] = 1;
q.push(1000);
q.push(1000);
while (!q.empty()) {
r = q.front();
q.pop();
c = q.front();
q.pop();
vst[r][c] = 0;
for (i = 0; i < 4; i++) {
rr = r + dr[i], cc = c + dc[i];
num[rr][cc] += num[r][c] / 4;
if (num[rr][cc] >= 4 && !vst[rr][cc]) {
q.push(rr);
q.push(cc);
vst[rr][cc] = 1;
}
}
num[r][c] %= 4;
}
scanf("%d", &Q);
for (i = 0; i < Q; i++) {
scanf("%d%d", &r, &c);
r += 1000;
c += 1000;
if (r < 0 || c < 0 || r >= 2000 || c >= 2000)
puts("0");
else
printf("%d\n", num[r][c]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 300;
int a[2 * N + 1][2 * N + 1] = {0};
int b[2 * N + 1][2 * N + 1] = {0};
int main() {
int n;
cin >> n;
int x0 = N;
int y0 = N;
int L = 0;
a[x0][y0] = n;
while (true) {
bool isEnd = true;
int incL = 0;
for (int x = x0 - L; x <= x0 + L; ++x) {
for (int y = y0 - L; y <= y0 + L; ++y) {
if (a[x][y] >= 4) {
isEnd = false;
b[x][y] += -4;
++b[x + 1][y];
++b[x][y + 1];
++b[x - 1][y];
++b[x][y - 1];
if (x == x0 - L || x == x0 + L || y == y0 - L || y == y0 + L) {
incL = 1;
}
}
}
}
L += incL;
if (isEnd) {
break;
}
for (int x = x0 - L; x <= x0 + L; ++x) {
for (int y = y0 - L; y <= y0 + L; ++y) {
a[x][y] += b[x][y];
b[x][y] = 0;
}
}
}
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
int x;
int y;
cin >> x >> y;
if ((-300 < x && x < 300) && (-300 < y && y < 300)) {
cout << a[x + x0][y + y0] << endl;
} else {
cout << 0 << endl;
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void chmax(T& a, const T& b) {
a = max(a, b);
}
template <class T>
void chmin(T& a, const T& b) {
a = min(a, b);
}
template <class T>
void uniq(T& c) {
sort(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
}
template <class T>
string to_s(const T& a) {
ostringstream os;
os << a;
return os.str();
}
template <class T>
T to_T(const string& s) {
istringstream is(s);
T res;
is >> res;
return res;
}
template <typename T>
void print_container(ostream& os, const T& c) {
const char* _s = " ";
if (!c.empty()) {
__typeof__(c.begin()) last = --c.end();
for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) {
os << *it;
if (it != last) cout << _s;
}
}
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& c) {
print_container(os, c);
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& c) {
print_container(os, c);
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& c) {
print_container(os, c);
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const deque<T>& c) {
print_container(os, c);
return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const map<T, U>& c) {
print_container(os, c);
return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
os << "( " << p.first << ", " << p.second << " )";
return os;
}
template <class T>
void print(T a, int n, const string& deli = " ", int br = 1) {
for (int i = 0; i < n; ++i) {
cout << a[i];
if (i + 1 != n) cout << deli;
}
while (br--) cout << endl;
}
template <class T>
void print2d(T a, int w, int h, int width = -1, int br = 1) {
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (width != -1) cout.width(width);
cout << a[i][j] << ' ';
}
cout << endl;
}
while (br--) cout << endl;
}
template <class T>
void input(T& a, int n) {
for (int i = 0; i < n; ++i) cin >> a[i];
}
template <class T>
void input(T* a, int n) {
for (int i = 0; i < n; ++i) cin >> a[i];
}
void fix_pre(int n) {
cout.setf(ios::fixed, ios::floatfield);
cout.precision(10);
}
void fast_io() {
cin.tie(0);
ios::sync_with_stdio(false);
}
bool in_rect(int x, int y, int w, int h) {
return 0 <= x && x < w && 0 <= y && y < h;
}
bool in_seg(int n, int l, int r) { return l <= n && n <= r; }
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const double PI = acos(-1.0);
int o = 70;
int a[150][150];
int na[150][150];
int& get(int x, int y) { return a[y + o][x + o]; }
int& getn(int x, int y) { return na[y + o][x + o]; }
void simulate(int n) {
memset(a, 0, sizeof(a));
get(0, 0) = n;
for (bool updated = true; updated;) {
updated = false;
memset(na, 0, sizeof(na));
for (int y = -70; y <= 70; ++y) {
for (int x = -70; x <= 70; ++x) {
int t = get(x, y);
int p = t / 4, q = t % 4;
if (p > 0)
for (int i = 0; i < (int)(4); ++i) {
updated = true;
getn(x + dx[i], y + dy[i]) += p;
}
getn(x, y) += q;
}
}
memcpy(a, na, sizeof(a));
}
}
int main() {
fast_io();
int n, q;
cin >> n >> q;
simulate(n);
while (q--) {
int x, y;
cin >> x >> y;
x = abs(x), y = abs(y);
int res;
if (max(x, y) < 70)
res = get(x, y);
else
res = 0;
cout << res << endl;
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int ZERO = 1000, NMAX = 2010;
const int direct[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int T, table[NMAX][NMAX], UP;
bool in[NMAX][NMAX];
deque<pair<int, int> > Q;
pair<int, int> x;
int main() {
int i, tmp, p, q;
scanf("%d %d", &table[ZERO][ZERO], &T);
Q.push_back(make_pair(ZERO, ZERO));
in[ZERO][ZERO] = true;
while (!Q.empty()) {
x = Q.front();
Q.pop_front();
in[x.first][x.second] = false;
tmp = table[x.first][x.second] / 4;
UP = max(UP, x.first - ZERO);
for (i = 0; i < 4; i += 1) {
table[x.first + direct[i][0]][x.second + direct[i][1]] += tmp;
if (!in[x.first + direct[i][0]][x.second + direct[i][1]] &&
table[x.first + direct[i][0]][x.second + direct[i][1]] >= 4) {
Q.push_back(make_pair(x.first + direct[i][0], x.second + direct[i][1]));
in[x.first + direct[i][0]][x.second + direct[i][1]] = true;
}
}
table[x.first][x.second] %= 4;
}
for (i = 1; i <= T; i += 1) {
scanf("%d %d", &p, &q);
if (abs(p) <= UP + 1 && abs(q) <= UP + 1)
printf("%d\n", table[p + ZERO][q + ZERO]);
else
printf("0\n");
}
exit(0);
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int cnt[1000][1000], vst[1000][1000], flag;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
int N, z, T, x, y, i, xx, yy;
while (scanf("%d%d", &N, &T) == 2) {
memset(cnt, 0, sizeof(cnt));
cnt[500][500] = N;
queue<int> q = queue<int>();
if (N >= 4) {
q.push(500);
q.push(500);
}
flag++;
while (!q.empty()) {
x = q.front();
q.pop();
y = q.front();
q.pop();
vst[x][y] = 0;
z = cnt[x][y] / 4;
cnt[x][y] %= 4;
for (i = 0; i < 4; i++) {
xx = x + dx[i];
yy = y + dy[i];
cnt[xx][yy] += z;
if (cnt[xx][yy] >= 4) {
if (vst[xx][yy] != flag) {
vst[xx][yy] = flag;
q.push(xx);
q.push(yy);
}
}
}
}
for (; T--;) {
scanf("%d%d", &x, &y);
if ((x + 500 < 0 || x + 500 >= 1000 || y + 500 < 0 || y + 500 >= 1000))
puts("0");
else
printf("%d\n", cnt[x + 500][y + 500]);
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int LX = 1000;
int a[LX << 2][LX << 2];
int n, t, x, y;
void gao(int x, int y, int add) {
a[x][y] += add;
if (a[x][y] >= 4) {
a[x][y] -= 4;
gao(x + 1, y, 1);
gao(x, y + 1, 1);
gao(x, y - 1, 1);
gao(x - 1, y, 1);
if (a[x][y] >= 4) gao(x, y, 0);
}
}
int main() {
while (cin >> n >> t) {
memset(a, 0, sizeof(a));
gao(LX, LX, n);
while (t--) {
cin >> x >> y;
if (x < -LX || x > LX || y < -LX || y > LX)
cout << 0 << endl;
else {
cout << a[x + LX][y + LX] << endl;
}
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct P {
int f, s;
P(int f = 0, int s = 0) : f(f), s(s) {}
};
int nd[500][500];
bool up[500][500];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int main() {
int n, t;
scanf("%d %d", &n, &t);
nd[250][250] = n;
queue<P> que;
memset(up, false, sizeof(up));
if (n >= 4) {
que.push(P(250, 250));
up[250][250] = true;
}
while (!que.empty()) {
P p = que.front();
que.pop();
int x = p.f, y = p.s;
up[x][y] = false;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
nd[nx][ny] += nd[x][y] / 4;
if (nd[nx][ny] >= 4 && !up[nx][ny]) {
up[nx][ny] = true;
que.push(P(nx, ny));
}
}
nd[x][y] %= 4;
}
for (int i = 0; i < t; i++) {
int x, y;
scanf("%d %d", &x, &y);
x += 250;
y += 250;
if (0 <= x && x < 500 && 0 <= y && y < 500)
printf("%d\n", nd[x][y]);
else
puts("0");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 32000;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int a[1000][1000];
int b[1000][1000];
int x[maxN];
int y[maxN];
int nx[maxN], ny[maxN];
int len, len2;
int used[1000][1000];
int main() {
int n;
cin >> n;
int centerx = 500, centery = 500;
a[centerx][centery] = n;
x[0] = centerx, y[0] = centery;
len = 1;
int niter = 0;
queue<pair<int, int> > q;
q.push(make_pair(centerx, centery));
++used[centerx][centery];
int total = 0;
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
int cx = cur.first, cy = cur.second;
--used[cx][cy];
if (a[cx][cy] >= 4) {
int val = a[cx][cy] / 4;
for (int k = 0; k < 4; ++k) {
a[cx + dx[k]][cy + dy[k]] += val;
if (!used[cx + dx[k]][cy + dy[k]]) {
q.push(make_pair(cx + dx[k], cy + dy[k]));
++used[cx + dx[k]][cy + dy[k]];
}
++total;
}
}
a[cx][cy] %= 4;
}
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
if (x < 0) x = -x;
if (y < 0) y = -y;
x += centerx, y += centery;
if (x < 0 || x >= 1000 || y < 0 || y >= 1000) {
printf("0\n");
} else {
printf("%d\n", a[x][y]);
}
}
return 0;
cout << niter << endl;
map<pair<int, int>, int> ants;
ants[make_pair(0, 0)] = n;
int iter = 0;
while (true) {
map<pair<int, int>, int> nants;
bool good = false;
for (map<pair<int, int>, int>::iterator it = ants.begin(); it != ants.end();
++it) {
int cx = it->first.first;
int cy = it->first.second;
int howmuch = it->second;
if (howmuch >= 4) {
for (int k = 0; k < 4; ++k) {
nants[make_pair(cx + dx[k], cy + dy[k])] += howmuch / 4;
}
good = true;
nants[make_pair(cx, cy)] += howmuch % 4;
} else {
nants[make_pair(cx, cy)] += howmuch;
}
}
if (good) {
++iter;
ants = nants;
} else {
break;
}
}
cout << iter << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int cnt[2 * 700 + 70][2 * 700 + 70];
bool inq[2 * 700 + 70][2 * 700 + 70];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n;
void push(queue<pair<int, int> > &q, const pair<int, int> &p) {
if (inq[p.first + 700][p.second + 700]) return;
inq[p.first + 700][p.second + 700] = true;
q.push(p);
}
void simulate(void) {
scanf("%d", &n);
queue<pair<int, int> > q;
pair<int, int> p;
int i, k;
cnt[700][700] = n;
q.push(pair<int, int>(0, 0));
while (!q.empty()) {
p = q.front();
q.pop();
inq[p.first + 700][p.second + 700] = false;
if (cnt[p.first + 700][p.second + 700] < 4) continue;
k = cnt[p.first + 700][p.second + 700] / 4;
for (i = 0; i < 4; i = i + 1) {
cnt[p.first + dx[i] + 700][p.second + dy[i] + 700] += k;
push(q, pair<int, int>(p.first + dx[i], p.second + dy[i]));
}
cnt[p.first + 700][p.second + 700] -= k * 4;
}
}
int answer(const int &first, const int &second) {
if (first < 0) return (answer(-first, second));
if (second < 0) return (answer(first, -second));
if (first > 700) return (0);
if (second > 700) return (0);
return (cnt[first + 700][second + 700]);
}
void process(void) {
int i, q, first, second;
scanf("%d", &q);
for (i = 1; i <= q; i = i + 1) {
scanf("%d", &first);
scanf("%d", &second);
printf("%d\n", answer(first, second));
}
}
int main(void) {
simulate();
process();
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
template <typename T>
T gcd(T a, T b) {
if (a == 0) return b;
return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long m) {
T ans = 1;
while (b > 0) {
if (b % 2 == 1) ans = (ans * a) % mod;
b /= 2;
a = (a * a) % mod;
}
return ans % mod;
}
const int maxi = 1000;
int arr[maxi][maxi];
int n, m, k, l, j;
void dfs(int x, int y) {
int antsdiv = arr[x][y] / 4;
arr[x][y] -= 4 * antsdiv;
arr[x - 1][y] += antsdiv;
if (arr[x - 1][y] >= 4) dfs(x - 1, y);
arr[x + 1][y] += antsdiv;
if (arr[x + 1][y] >= 4) dfs(x + 1, y);
arr[x][y - 1] += antsdiv;
if (arr[x][y - 1] >= 4) dfs(x, y - 1);
arr[x][y + 1] += antsdiv;
if (arr[x][y + 1] >= 4) dfs(x, y + 1);
}
int main() {
cin >> n;
arr[500][500] = n;
dfs(500, 500);
cin >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
if (abs(x) > 100 || abs(y) > 100) {
cout << "0\n";
continue;
}
cout << arr[x + 500][y + 500] << endl;
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int oo = 1 << 30;
const double PI = M_PI;
const double EPS = 1e-15;
const int MaxM = 500;
const int OFFSET = MaxM >> 1;
int n, t;
int C[MaxM][MaxM];
int used[MaxM][MaxM];
int utime;
int X[2][MaxM * MaxM];
int Y[2][MaxM * MaxM];
int size[2];
void push(int idx, int r, int c) {
if (used[r][c] != utime && C[r][c] >= 4) {
used[r][c] = utime;
X[idx][size[idx]] = r;
Y[idx][size[idx]] = c;
size[idx] = size[idx] + 1;
}
}
void put(int k) {
int curr = 0;
C[OFFSET][OFFSET] = k;
size[curr] = 1;
X[curr][0] = OFFSET;
Y[curr][0] = OFFSET;
if (k < 4) return;
while (size[curr] > 0) {
for (int i = 0; i < size[curr]; i++) {
int r = C[X[curr][i]][Y[curr][i]] / 4;
C[X[curr][i]][Y[curr][i]] -= r * 4;
C[X[curr][i] - 1][Y[curr][i]] += r;
C[X[curr][i] + 1][Y[curr][i]] += r;
C[X[curr][i]][Y[curr][i] - 1] += r;
C[X[curr][i]][Y[curr][i] + 1] += r;
}
utime = utime + 1;
size[1 - curr] = 0;
for (int i = 0; i < size[curr]; i++) {
push(1 - curr, X[curr][i], Y[curr][i]);
push(1 - curr, X[curr][i] - 1, Y[curr][i]);
push(1 - curr, X[curr][i] + 1, Y[curr][i]);
push(1 - curr, X[curr][i], Y[curr][i] - 1);
push(1 - curr, X[curr][i], Y[curr][i] + 1);
}
curr = 1 - curr;
}
}
int main() {
cin.sync_with_stdio(false);
cin >> n >> t;
put(4 * (n / 4));
put(n % 4);
for (int i = 0; i < t; i++) {
int _x, _y;
cin >> _x >> _y;
_x += OFFSET;
_y += OFFSET;
if (0 <= _x && _x < MaxM && 0 <= _y && _y < MaxM)
cout << C[_x][_y] << "\n";
else
cout << 0 << "\n";
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int tot[160][160];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
void work(int m, int kk) {
int tmp, tt, x, y, ttn;
int i, j, dir, flag;
memset(tot, 0, sizeof(tot));
tot[75][75] = m;
while (1) {
flag = 0;
for (i = 0; i <= 150; ++i) {
for (j = 0; j <= 150; ++j)
if (tot[i][j] >= 4) {
flag = 1;
ttn = tot[i][j] / 4;
tot[i][j] %= 4;
for (dir = 0; dir < 4; ++dir) {
x = i + dx[dir];
y = j + dy[dir];
tot[x][y] += ttn;
}
}
}
if (flag == 0) break;
}
}
inline int abs(int a) { return a < 0 ? -a : a; }
int main() {
int m, t;
int i, j;
scanf("%d%d", &m, &t);
work(m, 4);
int a, b;
for (i = 1; i <= t; ++i) {
scanf("%d%d", &a, &b);
if (abs(a) > 74 || abs(b) > 74)
printf("0\n");
else
printf("%d\n", tot[a + 75][b + 75]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 200;
int n, t;
int mp[N << 1][N << 1];
int main() {
scanf("%d%d", &n, &t);
memset(mp, 0, sizeof mp);
bool flag = 1;
mp[N][N] = n;
while (flag) {
flag = 0;
for (int i = 0; i < N * 2; i++) {
for (int j = 0; j < N * 2; j++) {
if (mp[i][j] >= 4) {
flag = 1;
int now = mp[i][j] / 4;
mp[i + 1][j] += now;
mp[i - 1][j] += now;
mp[i][j + 1] += now;
mp[i][j - 1] += now;
mp[i][j] -= now * 4;
}
}
}
}
for (int i = 1; i <= t; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (abs(x) > N || abs(y) > N) {
printf("0\n");
} else {
printf("%d\n", mp[x + N][y + N]);
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int arr[2][200][200];
stack<int> s1, s2;
int computeIterative(void) {
int pre = 0;
int now = 1;
while (true) {
bool changed = false;
for (int i = 0; i < 200; i++) {
for (int j = 0; j < 200; j++) {
arr[now][i][j] += arr[pre][i][j];
if (arr[pre][i][j] >= 4) {
changed = true;
int inc = arr[pre][i][j] / 4;
int dec = 4 * inc;
arr[now][i][j] -= dec;
arr[now][i + 1][j] += inc;
arr[now][i - 1][j] += inc;
arr[now][i][j + 1] += inc;
arr[now][i][j - 1] += inc;
}
}
}
if (!changed) return now;
memset(arr[pre], 0, sizeof(arr[pre]));
pre = 1 - pre;
now = 1 - now;
}
}
int main() {
int n, t;
scanf("%d %d", &n, &t);
memset(arr, 0, sizeof(arr));
arr[0][100][100] = n;
int ver = computeIterative();
for (int i = 0; i < t; i++) {
int x, y;
scanf("%d %d", &x, &y);
if (x >= 100 || x < -100 || y >= 100 || y < -100)
printf("0\n");
else {
printf("%d\n", arr[ver][x + 100][y + 100]);
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
char Gc() {
static char now[1 << 20], *S, *T;
if (T == S) {
T = (S = now) + std::fread(now, 1, 1 << 20, stdin);
if (T == S) return EOF;
}
return *S++;
}
template <typename T>
void Read(T &x) {
x = 0;
int f = 1;
char c;
while ((c = Gc()) < '0' || c > '9')
if (c == '-') f = -1;
x = c - '0';
while ((c = Gc()) >= '0' && c <= '9') x = x * 10 + (c - '0');
x *= f;
}
template <typename T, typename... Args>
void Read(T &x, Args &...args) {
Read(x);
Read(args...);
}
template <typename T>
void checkmax(T &x, T y) {
if (x < y) x = y;
}
template <typename T>
void checkmin(T &x, T y) {
if (x > y) x = y;
}
int n, q, a[601][601];
void Dfs(int x, int y) {
if (a[x][y] < 4) return;
int t = a[x][y] >> 2;
a[x][y] &= 3;
a[x - 1][y] += t;
a[x + 1][y] += t;
a[x][y - 1] += t;
a[x][y + 1] += t;
Dfs(x - 1, y);
Dfs(x + 1, y);
Dfs(x, y - 1);
Dfs(x, y + 1);
}
int main(int argc, char const *argv[]) {
Read(n, q);
a[300][300] = n;
Dfs(300, 300);
while (q--) {
int x, y;
Read(x, y);
if (x < -300 || x > 300 || y < -300 || y > 300)
std::printf("0\n");
else
std::printf("%d\n", a[x + 300][y + 300]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[400][400];
void add(int x, int y) {
a[x + 200][200 + y] += 1;
if (a[x + 200][200 + y] >= 4) {
a[x + 200][200 + y] -= 4;
add(x + 1, y);
add(x - 1, y);
add(x, y - 1);
add(x, y + 1);
}
}
int main() {
int n, t;
while (scanf("%d%d", &n, &t) != EOF) {
int s = n;
memset(a, 0, sizeof(a));
a[200][200] = s;
while (a[200][200] >= 4)
a[200][200] -= 4, add(1, 0), add(0, 1), add(0, -1), add(-1, 0);
for (int i = 1; i <= t; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (x <= -200 || x >= 200 || y <= -200 || y >= 200)
printf("0\n");
else
printf("%d\n", a[x + 200][y + 200]);
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
int main() {
static int kk[200 + 1 + 200][200 + 1 + 200];
int n, t, x, y, k, updated;
scanf("%d%d", &n, &t);
kk[200][200] = n;
while (1) {
updated = 0;
for (x = 1; x < 200 + 200; x++)
for (y = 1; y < 200 + 200; y++)
if (kk[x][y] >= 4) {
k = kk[x][y] / 4;
kk[x][y] %= 4;
kk[x - 1][y] += k;
kk[x + 1][y] += k;
kk[x][y - 1] += k;
kk[x][y + 1] += k;
updated = 1;
}
if (!updated) break;
}
while (t--) {
scanf("%d%d", &x, &y);
printf("%d\n", x < -200 || x > 200 || y < -200 || y > 200
? 0
: kk[200 + x][200 + y]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > q;
int n, t;
int grid[100][100];
int moves[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int main() {
cin >> n;
grid[0][0] = n;
if (n > 3) q.push_back(make_pair(0, 0));
int minx = 0;
int nit = 0;
while (!q.empty()) {
nit++;
pair<int, int> cur = q[q.size() - 1];
assert(grid[cur.first][cur.second] > 3);
if (cur.first > minx) minx = cur.first;
grid[cur.first][cur.second] -= 4;
if (grid[cur.first][cur.second] < 4) q.pop_back();
for (int i = 0; i < 4; i++) {
int nx = cur.first + moves[i][0];
int ny = cur.second + moves[i][1];
if (nx < 0 || ny < 0) continue;
int inc = (1 + (!nx && cur.first)) * (1 + (!ny && cur.second));
bool ch = grid[nx][ny] < 4;
grid[nx][ny] += inc;
if (grid[nx][ny] >= 4 && ch) q.push_back(make_pair(nx, ny));
}
}
minx++;
cin >> t;
while (t--) {
int a, b;
cin >> a >> b;
if (a < 0) a *= -1;
if (b < 0) b *= -1;
if (a > minx || b > minx)
cout << 0 << "\n";
else
cout << grid[a][b] << "\n";
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[1111111], b[1111111], dp[4111][4111];
int n, l = 0, m;
int q1 = 0, q2 = 0;
long long ans = 0;
void add(int x, int y) {
dp[x][y]++;
if (dp[x][y] > 3) {
dp[x][y] -= 4;
add(x + 1, y);
add(x - 1, y);
add(x, y + 1);
add(x, y - 1);
}
}
int main() {
int j, i, n = 0, m = 0;
cin >> n;
dp[200][200] = n;
while (dp[200][200] > 3) {
dp[200][200] -= 4;
add(200 + 1, 200);
add(200 - 1, 200);
add(200, 200 - 1);
add(200, 200 + 1);
}
cin >> m;
while (m--) {
int x, y;
cin >> x >> y;
x = abs(x);
y = abs(y);
if (max(x, y) > 199)
cout << 0 << endl;
else
cout << dp[200 + x][200 + y] << endl;
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
void stop() {
printf("press any key to continue...\n");
while (getchar() != '\n') {
}
}
const int LEN = 131;
int ants[2][LEN][LEN];
void print(int t) {
for (int y = 0; y < LEN; y++) {
for (int x = 0; x < LEN; x++) {
if (ants[t][y][x] == 0) {
cout << ' ';
} else {
printf("%d", ants[t][y][x]);
}
}
cout << endl;
}
}
int main() {
istream& in = cin;
int n, t;
in >> n >> t;
memset(ants, 0, sizeof(ants));
ants[0][LEN / 2][LEN / 2] = n;
int d = 1;
int s = 0;
while (true) {
bool update = false;
memset(ants[d], 0, sizeof(ants[d]));
for (int y = 0; y < LEN; y++) {
for (int x = 0; x < LEN; x++) {
if (ants[s][y][x] >= 4) {
if (y == 0) {
cout << endl;
}
update = true;
int add = ants[s][y][x] / 4;
int rem = ants[s][y][x] % 4;
ants[d][y][x + 1] += add;
ants[d][y][x - 1] += add;
ants[d][y + 1][x] += add;
ants[d][y - 1][x] += add;
ants[d][y][x] += rem;
} else {
ants[d][y][x] += ants[s][y][x];
}
}
}
if (!update) break;
swap(s, d);
}
int MAX_ABS = LEN / 2;
for (int i = 0; i < t; i++) {
int x, y;
in >> x >> y;
if (abs(x) > MAX_ABS || abs(y) > MAX_ABS) {
printf("%d\n", 0);
} else {
x += LEN / 2;
y += LEN / 2;
printf("%d\n", ants[s][y][x]);
}
}
}
| 8 | CPP |
#include <bits/stdc++.h>
int a[2000 + 1][2000 + 1];
char pus[2000 + 1][2000 + 1];
int ql[100 * 30000], qc[100 * 30000], st, dr;
int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int main() {
int n, t, i, x, l, c, nl, nc, cx;
char g = 1;
scanf("%d%d", &n, &t);
st = 0, dr = 1;
ql[0] = qc[0] = 0;
pus[0][0] = 1;
a[0][0] = n;
while (st != dr) {
l = ql[st];
c = qc[st];
st++;
if (st == 100 * 30000) st = 0;
pus[l][c] = 0;
x = a[l][c] / 4;
a[l][c] %= 4;
for (i = 0; i < 4; i++) {
nl = l + d[i][0];
nc = c + d[i][1];
if (nl == 0 && nc == 0)
cx = 4 * x;
else if ((l != 0 && nl == 0) || (c != l && nc == nl))
cx = 2 * x;
else
cx = x;
if (nl >= 0 && nc >= 0 && nl <= nc) {
a[nl][nc] += cx;
if (!pus[nl][nc] && a[nl][nc] >= 4) {
ql[dr] = nl;
qc[dr] = nc;
dr++;
if (dr == 100 * 30000) dr = 0;
pus[nl][nc] = 1;
}
}
}
}
int y, aux;
for (i = 0; i < t; i++) {
scanf("%d%d", &x, &y);
if (x < 0) x = -x;
if (y < 0) y = -y;
if (y < x) {
aux = x;
x = y;
y = aux;
}
if (x <= 2000 && y <= 2000)
printf("%d\n", a[x][y]);
else
printf("0\n");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
long long powmod(long long a, long long b) {
long long tmp = 1;
a %= (10000000000);
for (; b; b >>= 1) {
if (b & 1) tmp = tmp * a % (10000000000);
a = a * a % (10000000000);
}
return tmp;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <class T>
inline void R(T &xx) {
xx = 0;
char ch = getchar();
bool F = 0;
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') F = 1, ch = getchar();
while (ch >= '0' && ch <= '9')
xx = xx + xx + (xx << 3) + ch - 48, ch = getchar();
if (F) xx = -xx;
}
template <class T>
inline void add(int &x, T y) {
for (x += y; x >= (10000000000); x -= (10000000000))
;
}
int f[222][222];
int t;
int main() {
R(f[100][100]);
R(t);
for (bool F = 1; F;) {
F = 0;
for (int i = 1; i < (int)201; i++)
for (int j = 1; j < (int)201; j++)
if (f[i][j] >= 4) {
F = 1;
f[i + 1][j] += f[i][j] / 4;
f[i - 1][j] += f[i][j] / 4;
f[i][j + 1] += f[i][j] / 4;
f[i][j - 1] += f[i][j] / 4;
f[i][j] %= 4;
}
}
for (int x, y; t--;) {
R(x);
R(y);
printf("%d\n", (abs(x) <= 100 && abs(y) <= 100) ? f[x + 100][y + 100] : 0);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > q;
int ht[205][205], tmp[205][205];
map<pair<int, int>, int>::iterator its;
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int n, t;
cin >> n >> t;
ht[100][100] = n;
while (1) {
bool check = 0;
for (int i = 0; i <= 200; i++) {
for (int j = 0; j <= 200; j++) {
if (ht[i][j] < 4) continue;
check = 1;
for (int k = 0; k < 4; k++)
tmp[i + dir[k][0]][j + dir[k][1]] += ht[i][j] / 4;
ht[i][j] %= 4;
}
}
if (!check) break;
for (int i = 0; i <= 200; i++) {
for (int j = 0; j <= 200; j++) {
ht[i][j] += tmp[i][j];
tmp[i][j] = 0;
}
}
}
while (t--) {
int uy, ux;
cin >> uy >> ux;
uy += 100, ux += 100;
if (uy < 0 || uy > 200 || ux < 0 || ux > 200)
printf("0\n");
else
printf("%d\n", ht[uy][ux]);
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 7;
const int INF = 1e9 + 7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int n, t, lx, ly, rx, ry;
int a[N << 2][N << 2];
int add[N << 2][N << 2];
queue<pair<int, int> > que[2];
void solve() {
a[N][N] = n;
lx = rx = 0, ly = ry = 0;
int qid = 0;
if (n >= 4) que[qid].push(make_pair(N, N));
while (!que[qid].empty()) {
while (!que[qid].empty()) {
pair<int, int> cell = que[qid].front();
lx = min(lx, cell.first - N);
rx = max(rx, cell.first - N);
ly = min(ly, cell.second - N);
ry = max(ry, cell.second - N);
int num = a[cell.first][cell.second];
a[cell.first][cell.second] = num & 3;
que[qid].pop();
for (int i = (0); i < (4); ++i) {
int x = cell.first + dx[i];
int y = cell.second + dy[i];
add[x][y] += num >> 2;
que[qid ^ 1].push(make_pair(x, y));
}
}
qid ^= 1;
while (!que[qid].empty()) {
pair<int, int> cell = que[qid].front();
que[qid].pop();
lx = min(lx, cell.first - N);
rx = max(rx, cell.first - N);
ly = min(ly, cell.second - N);
ry = max(ry, cell.second - N);
if (!add[cell.first][cell.second]) continue;
a[cell.first][cell.second] += add[cell.first][cell.second];
add[cell.first][cell.second] = 0;
if (a[cell.first][cell.second] >= 4) que[qid ^ 1].push(cell);
}
qid ^= 1;
}
}
int main() {
scanf("%d%d", &n, &t);
solve();
for (int i = (0); i < (t); ++i) {
int x, y, ans;
scanf("%d%d", &x, &y);
if (x < lx || x > rx || y < ly || y > ry) {
ans = 0;
} else {
ans = a[x + N][y + N];
}
printf("%d\n", ans);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int cnt[1100110];
int inq[1100110];
queue<int> q;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
void spfa(int k) {
int st = (500 << 10) | 500;
cnt[st] = k;
if (k < 4) return;
q.push(st);
inq[st] = 1;
while (!q.empty()) {
st = q.front();
q.pop();
int x = st & 1023, y = st >> 10, num = (cnt[st] -= 4);
if (num < 4)
inq[st] = 0;
else
q.push(st);
for (int i = 0; i < 4; ++i) {
int nst = (x + dx[i]) | ((y + dy[i]) << 10);
int nnum = (cnt[nst] += 1);
if (nnum < 4) continue;
if (inq[nst]) continue;
q.push(nst);
inq[nst] = 1;
}
}
}
int main() {
int n, t;
cin >> n >> t;
spfa(n);
int x, y;
while (t--) {
scanf("%d %d", &x, &y);
x += 500;
y += 500;
if (x < 0 || x > 1000 || y < 0 || y > 1000)
puts("0");
else
printf("%d\n", cnt[(y << 10) | x]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 140;
int a[MaxN][MaxN], b[MaxN][MaxN];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main(void) {
int n, t;
while (scanf("%d %d", &n, &t) != EOF) {
int i, j, d, f = 1, x, y, mn = MaxN / 2, mx = MaxN / 2;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
a[MaxN / 2][MaxN / 2] = n;
b[MaxN / 2][MaxN / 2] = n;
while (f) {
f = 0;
for (i = mn; i <= mx; i++)
for (j = mn; j <= mx; j++)
if (a[i][j] >= 4) {
b[i][j] -= 4;
for (d = 0; d < 4; d++) b[i + dy[d]][j + dx[d]] += 1;
}
for (i = mn - 1; i <= mx + 1; i++)
for (j = mn - 1; j <= mx + 1; j++) {
a[i][j] = b[i][j];
if (a[i][j] >= 4) {
f = 1;
if (j > mx) mx = j;
}
}
for (i = mn; i <= mx; i++)
for (j = mn; j <= mx; j++)
if (b[i][j] >= 4) {
a[i][j] -= 4;
for (d = 0; d < 4; d++) a[i + dy[d]][j + dx[d]] += 1;
}
for (i = mn - 1; i <= mx + 1; i++)
for (j = mn - 1; j <= mx + 1; j++) {
b[i][j] = a[i][j];
if (b[i][j] >= 4) {
f = 1;
if (j > mx) mx = j;
}
}
mn = MaxN / 2 - (mx - MaxN / 2);
}
for (i = mn; i <= mx; i++)
for (j = mn; j <= mx; j++)
if (a[i][j] >= 4) {
b[i][j] -= 4;
for (d = 0; d < 4; d++) b[i + dy[d]][j + dx[d]] += 1;
}
for (i = mn - 1; i <= mx + 1; i++)
for (j = mn - 1; j <= mx + 1; j++) {
a[i][j] = b[i][j];
if (a[i][j] >= 4) {
f = 1;
if (j > mx) mx = j;
}
}
for (i = 0; i < t; i++) {
scanf("%d %d", &x, &y);
if (abs(x) > MaxN / 2 - 2 || abs(y) > MaxN / 2 - 2)
printf("%d\n", 0);
else
printf("%d\n", a[x + MaxN / 2][y + MaxN / 2]);
}
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = long unsigned long;
using ld = double long;
const int H = 6000;
uint16_t a[H][H];
uint16_t t[H][H];
const int dx[5] = {0, 1, 0, -1, 0};
const int dy[5] = {1, 0, -1, 0, 0};
int main() {
ios::sync_with_stdio(!cin.tie(0));
int n, qe;
cin >> n >> qe;
a[0][0] = n;
vector<pair<int, int>> maybe = {{0, 0}};
for (int x = 0; x <= H; x++) {
int f = 0;
for (auto [i, j] : maybe) {
uint16_t& r = a[i][j];
if (r >= 4) {
f = 1;
int q = r / 4;
r %= 4;
t[i][j + 1] += q;
if (j == 1) {
t[i][0] += 2 * q;
} else if (j > 0) {
t[i][j - 1] += q;
}
t[i + 1][j] += q;
if (i == 1) {
t[0][j] += 2 * q;
} else if (i > 0) {
t[i - 1][j] += q;
}
}
}
if (!f) {
break;
}
vector<pair<int, int>> tmp_maybe;
for (auto [i, j] : maybe) {
for (int dir = 0; dir < 5; dir++) {
int x = i + dx[dir];
int y = j + dy[dir];
if (x < 0 || y < 0) continue;
if (t[x][y]) {
a[x][y] += t[x][y];
t[x][y] = 0;
tmp_maybe.emplace_back(x, y);
}
}
}
swap(maybe, tmp_maybe);
}
while (qe--) {
int x, y;
cin >> x >> y;
x = abs(x);
y = abs(y);
if (max(x, y) >= H) {
cout << "0\n";
} else {
cout << a[x][y] << '\n';
}
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int max_x = 400;
const int max_y = 400;
const int max_queue = 1024 * 32;
bool there[2 * max_x][2 * max_y];
int grid[2 * max_x][2 * max_y];
void simulate(int n) {
pair<int, int> q[max_queue];
int head = 0;
int tail = 0;
grid[max_x][max_y] = n;
q[tail] = make_pair(max_x, max_y);
there[max_x][max_y] = true;
tail++;
while (head != tail) {
pair<int, int> point = q[head % max_queue];
head++;
int x = point.first;
int y = point.second;
int ants = grid[x][y];
if (ants < 4) continue;
grid[x][y] = ants - 4;
there[x][y] = false;
if (grid[x][y] >= 4 && !there[x][y]) {
q[tail % max_queue] = make_pair(x, y);
there[x][y] = true;
tail++;
}
grid[x + 1][y] += 1;
if (grid[x + 1][y] >= 4 && !there[x + 1][y]) {
q[tail % max_queue] = make_pair(x + 1, y);
there[x + 1][y] = true;
tail++;
}
grid[x - 1][y] += 1;
if (grid[x - 1][y] >= 4 && !there[x - 1][y]) {
q[tail % max_queue] = make_pair(x - 1, y);
there[x - 1][y] = true;
tail++;
}
grid[x][y + 1] += 1;
if (grid[x][y + 1] >= 4 && !there[x][y + 1]) {
q[tail % max_queue] = make_pair(x, y + 1);
there[x][y + 1] = true;
tail++;
}
grid[x][y - 1] += 1;
if (grid[x][y - 1] >= 4 && !there[x][y - 1]) {
q[tail % max_queue] = make_pair(x, y - 1);
there[x][y - 1] = true;
tail++;
}
}
}
int main() {
int n, t;
cin >> n >> t;
vector<int> answers;
simulate(n);
for (int i = 0; i < t; i++) {
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
if (abs(x) > 300 || abs(y) > 300)
answers.push_back(0);
else
answers.push_back(grid[max_x + x][max_y + y]);
}
for (int i = 0; i < t; i++) {
cout << answers[i] << endl;
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MOD(1000000007);
const int INF((1 << 30) - 1);
const int MAXN(150);
int a[2][MAXN][MAXN];
int main() {
int n, t;
scanf("%d%d", &n, &t);
int K = 67, now = 1, prev = 0, Move = 1;
a[prev][K][K] = n;
int MAX = 0;
while (Move) {
Move = 0;
for (int i = K - 66; i <= K + 66; i++)
for (int j = K - 66; j <= K + 66; j++) {
int k = a[prev][i][j] / 4;
if (k > 0) {
Move = 1;
a[now][i + 1][j] += k;
a[now][i - 1][j] += k;
a[now][i][j + 1] += k;
a[now][i][j - 1] += k;
a[now][i][j] += a[prev][i][j] - 4 * k;
} else
a[now][i][j] += a[prev][i][j];
a[prev][i][j] = 0;
}
now ^= 1, prev ^= 1;
}
while (t--) {
int x, y;
scanf("%d%d", &x, &y);
x += 67, y += 67;
if (x < 0 || x > 133 || y < 0 || y > 133)
printf("0\n");
else
printf("%d\n", a[prev][x][y]);
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, cnt;
int ants[500][500];
int main() {
scanf("%d %d", &n, &cnt);
int cx = 250, cy = 250;
int l = 1, r = 500 - 2, t = 1, b = 500 - 2;
ants[cx][cy] = n;
bool movement;
while (true) {
movement = false;
for (int i = t; i <= b; i++) {
for (int j = l; j <= r; j++) {
while (ants[i][j] >= 4) {
movement = true;
ants[i][j] -= 4;
ants[i][j + 1] += 1;
ants[i][j - 1] += 1;
ants[i + 1][j] += 1;
ants[i - 1][j] += 1;
}
}
}
if (!movement) break;
}
int tx, ty;
for (int i = 0; i < cnt; i++) {
scanf("%d %d", &tx, &ty);
if (tx + cx < l || tx + cx > r || ty + cy > b || ty + cy < t)
printf("0\n");
else
printf("%d\n", ants[tx + cx][ty + cy]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int T = 1000;
int a[2 * T + 1][2 * T + 1];
void ant(int x, int y) {
if (a[x][y] / 4 == 0) {
return;
}
int r = a[x][y] / 4;
a[x][y - 1] += r;
a[x][y + 1] += r;
a[x - 1][y] += r;
a[x + 1][y] += r;
a[x][y] -= 4 * r;
ant(x, y - 1);
ant(x, y + 1);
ant(x + 1, y);
ant(x - 1, y);
}
int main() {
int n, i, e, r1, r2;
cin >> n >> e;
a[T][T] = n;
ant(T, T);
for (i = 0; i < e; i++) {
cin >> r1 >> r2;
r1 += T;
r2 += T;
if (r1 < 2 * T + 1 && r2 < 2 * T + 1 && r1 > 0 && r2 > 0) {
cout << a[r1][r2] << endl;
} else {
cout << 0 << endl;
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
using db = long double;
using pll = pair<long long, long long>;
const int maxN = 32000;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int a[2000][2000];
int b[1000][1000];
int used[1000][1000];
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t, i, j, z, k, n;
cin >> n;
int centerx = 500, centery = 500;
a[centerx][centery] = n;
queue<pair<int, int> > q;
q.push(make_pair(centerx, centery));
++used[centerx][centery];
int total = 0;
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
int cx = cur.first, cy = cur.second;
--used[cx][cy];
if (a[cx][cy] >= 4) {
int val = a[cx][cy] / 4;
for (int k = 0; k < 4; ++k) {
a[cx + dx[k]][cy + dy[k]] += val;
if (!used[cx + dx[k]][cy + dy[k]]) {
q.push(make_pair(cx + dx[k], cy + dy[k]));
++used[cx + dx[k]][cy + dy[k]];
}
++total;
}
}
a[cx][cy] %= 4;
}
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int x = 0, y = 0;
cin >> x >> y;
x += centerx, y += centery;
if (x < 0 || x >= 1000 || y < 0 || y >= 1000) {
printf("0\n");
} else {
printf("%d\n", a[x][y]);
}
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int E = 70;
const int N = 2 * E;
int d[N][N];
int add[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
bool flag = 1;
int main() {
int n;
cin >> n;
d[E][E] = n;
while (flag) {
flag = 0;
for (int i = 1; i < N - 1; i++) {
for (int j = 1; j < N - 1; j++) {
for (int k = 0; k < 4; k++) add[i + dx[k]][j + dy[k]] += d[i][j] / 4;
d[i][j] %= 4;
}
}
for (int i = 1; i < N - 1; i++) {
for (int j = 1; j < N - 1; j++) {
d[i][j] += add[i][j];
add[i][j] = 0;
if (d[i][j] >= 4) flag = 1;
}
}
}
int m;
cin >> m;
for (int q = 0; q < m; q++) {
int x, y;
cin >> x >> y;
if (x + E < N && x + E >= 0 && y + E < N && y + E >= 0)
cout << d[x + E][y + E];
else
cout << 0;
cout << "\n";
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool In[140][140];
int g, x, y, T;
int F[140][140];
queue<pair<int, int>> Q;
int main() {
cin >> F[70][70] >> T;
Q.push(make_pair(70, 70));
while (!Q.empty()) {
pair<int, int> p = Q.front();
x = p.first;
y = p.second;
In[x][y] = false;
Q.pop();
g = F[x][y] / 4;
x = p.first;
y = p.second;
F[x + 1][y] += g;
if (F[x + 1][y] >= 4 && !In[x + 1][y]) {
In[x + 1][y] = true;
Q.push(make_pair(x + 1, y));
}
F[x - 1][y] += g;
if (F[x - 1][y] >= 4 && !In[x - 1][y]) {
In[x - 1][y] = true;
Q.push(make_pair(x - 1, y));
}
F[x][y + 1] += g;
if (F[x][y + 1] >= 4 && !In[x][y + 1]) {
In[x][y + 1] = true;
Q.push(make_pair(x, y + 1));
}
F[x][y - 1] += g;
if (F[x][y - 1] >= 4 && !In[x][y - 1]) {
In[x][y - 1] = true;
Q.push(make_pair(x, y - 1));
}
F[x][y] %= 4;
}
while (T--) {
cin >> x >> y;
if (x < -70 || x >= 70 || y < -70 || y >= 70)
cout << "0\n";
else
cout << F[x + 70][y + 70] << "\n";
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int visited[410][410];
int X[] = {1, -1, 0, 0};
int Y[] = {0, 0, 1, -1};
void dfs(int x, int y) {
if (visited[x][y] < 4) return;
int d = visited[x][y] / 4;
visited[x][y] %= 4;
for (int i = 0; i < 4; i++) {
int x1 = x + X[i];
int y1 = y + Y[i];
visited[x1][y1] += d;
dfs(x1, y1);
}
}
int main() {
int n, t;
cin >> n >> t;
visited[200][200] = n;
dfs(200, 200);
while (t--) {
long long x, y;
cin >> x >> y;
if (abs(x) > 200 || abs(y) > 200)
cout << "0" << endl;
else
cout << visited[200 + x][200 + y] << endl;
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y;
Point() {}
Point(int _x, int _y) : x(_x), y(_y) {}
Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }
} stk[2048 * 2048];
struct Mat {
int m[2048][2048];
void clear() { memset(m, 0, sizeof(m)); }
int &operator[](const Point &p) { return m[p.x + 1024][p.y + 1024]; }
} mat;
const Point d[] = {Point(0, -1), Point(-1, 0), Point(0, 1), Point(1, 0)};
int top;
void init(int n) {
int i;
Point p(0, 0), q;
mat.clear();
top = 0;
if ((mat[p] = n) >= 4) stk[++top] = p;
while (top) {
p = stk[top--];
for (i = 0; i < 4; ++i) {
mat[q = p + d[i]] += (mat[p] >> 2);
if (mat[q] >= 4) stk[++top] = q;
}
mat[p] &= 3;
}
}
int main() {
int n, m, i;
Point p;
while (~scanf("%d %d", &n, &m)) {
init(n);
for (i = 0; i < m; ++i) {
scanf("%d %d", &p.x, &p.y);
if (abs(p.x) < 1024 && abs(p.y) < 1024)
printf("%d\n", mat[p]);
else
printf("0\n");
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int dat[1111][1111];
int ok;
int n, t;
int maxi = 0;
int pos;
int main() {
cin >> n >> t;
pos = 500;
dat[pos][pos] = n;
ok = 1;
while (ok) {
ok = 0;
for (int i = pos - maxi; i <= pos + maxi; i++) {
for (int j = pos - maxi; j <= pos + maxi; j++) {
if (dat[i][j] >= 4) {
ok = 1;
int t1 = i - pos, t2 = j - pos;
if (maxi == max(t1 > 0 ? t1 : -t1, t2 > 0 ? t2 : -t2)) {
maxi++;
}
dat[i - 1][j] += dat[i][j] / 4;
dat[i + 1][j] += dat[i][j] / 4;
dat[i][j - 1] += dat[i][j] / 4;
dat[i][j + 1] += dat[i][j] / 4;
dat[i][j] %= 4;
}
}
}
}
while (t--) {
int x, y;
cin >> x >> y;
if ((x > 0 ? x : -x) > maxi || (y > 0 ? y : -y) > maxi)
cout << 0 << '\n';
else
cout << dat[pos + x][pos + y] << '\n';
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int vis[1100][1100], Ans[1100][1100];
int n, t, Q;
struct Node {
int x, y;
Node() { x = y = 0; };
Node(int x_, int y_) {
x = x_;
y = y_;
}
};
void BFS() {
queue<Node> q;
q.push(Node(200, 200));
vis[200][200] = 1;
Ans[200][200] = t;
while (!q.empty()) {
Node s = q.front();
q.pop();
int x = s.x;
int y = s.y;
vis[x][y] = 0;
int tmp = Ans[x][y];
if (tmp < 4) continue;
Ans[x][y] %= 4;
for (int dx = (-1); dx <= (1); ++dx)
for (int dy = (-1); dy <= (1); ++dy) {
if (dx == 0 && dy == 0) continue;
if (dx != 0 && dy != 0) continue;
if (!vis[x + dx][y + dy]) {
vis[x + dx][y + dy] = 1;
q.push(Node(x + dx, y + dy));
}
Ans[x + dx][y + dy] = Ans[x + dx][y + dy] + tmp / 4;
}
}
int Minx = 0x3f3f3f3f, Miny = 0x3f3f3f3f;
for (int x = (1); x <= (400); ++x)
for (int y = (1); y <= (400); ++y) {
if (Ans[x][y]) {
Minx = min(Minx, x);
Miny = min(Miny, y);
}
}
}
int main() {
scanf("%d%d", &t, &Q);
BFS();
for (int i = 1; i <= Q; ++i) {
int x, y;
scanf("%d%d", &x, &y);
x += 200;
y += 200;
if (x <= 1 || x >= 400 || y <= 1 || y >= 400) {
printf("0\n");
continue;
}
printf("%d\n", Ans[x][y]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int map1[200 + 1][200 + 1];
int map2[200 + 1][200 + 1];
int main() {
int n, t;
cin >> n >> t;
map1[200 / 2][200 / 2] = n;
int flag = 1;
int turn = 1;
while (flag) {
flag = 0;
if (turn == 1) {
memset(map2, 0, (200 + 1) * (200 + 1) * sizeof(int));
for (int i = 0; i <= 200; i++) {
for (int j = 0; j <= 200; j++) {
if (map1[i][j] < 4) {
map2[i][j] += map1[i][j];
} else {
map2[i - 1][j] += map1[i][j] / 4;
map2[i + 1][j] += map1[i][j] / 4;
map2[i][j + 1] += map1[i][j] / 4;
map2[i][j - 1] += map1[i][j] / 4;
map2[i][j] += map1[i][j] % 4;
flag = 1;
}
}
}
turn = 2;
} else {
memset(map1, 0, (200 + 1) * (200 + 1) * sizeof(int));
for (int i = 0; i <= 200; i++) {
for (int j = 0; j <= 200; j++) {
if (map2[i][j] < 4) {
map1[i][j] += map2[i][j];
} else {
map1[i - 1][j] += map2[i][j] / 4;
map1[i + 1][j] += map2[i][j] / 4;
map1[i][j + 1] += map2[i][j] / 4;
map1[i][j - 1] += map2[i][j] / 4;
map1[i][j] += map2[i][j] % 4;
flag = 1;
}
}
}
turn = 1;
}
}
int x, y;
for (int i = 0; i < t; i++) {
cin >> x >> y;
if (abs(x) > 200 / 2 || abs(y) >= 200 / 2) {
cout << 0 << endl;
} else {
if (turn == 1) {
cout << map1[x + 200 / 2][y + 200 / 2] << endl;
} else {
cout << map2[x + 200 / 2][y + 200 / 2] << endl;
}
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
int n[1000][1000];
void dfs(int i, int j, int v) {
int m;
n[i][j] += v;
if (n[i][j] < 4) return;
m = n[i][j] / 4;
n[i][j] %= 4;
dfs(i - 1, j, m);
dfs(i, j - 1, m);
dfs(i + 1, j, m);
dfs(i, j + 1, m);
}
int main() {
int x, t;
while (scanf("%d%d", &x, &t) == 2) {
memset(n, 0, sizeof(n));
dfs(500, 500, x);
for (int i = 0; i < t; ++i) {
int x, y;
scanf("%d%d", &x, &y);
x += 500;
y += 500;
if (x >= 1000 || y >= 1000 || x < 0 || y < 0) {
printf("0\n");
continue;
}
printf("%d\n", n[x][y]);
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long x, y, n, t;
int a[202][202];
void go(int x, int y) {
if (a[x][y] < 4) return;
int g = a[x][y] / 4;
a[x][y] %= 4;
a[x][y - 1] += g;
go(x, y - 1);
a[x][y + 1] += g;
go(x, y + 1);
a[x - 1][y] += g;
go(x - 1, y);
a[x + 1][y] += g;
go(x + 1, y);
}
int main() {
cin >> a[100][100];
go(100, 100);
cin >> t;
while (t--) {
cin >> x >> y;
x = abs(x + 100);
y = abs(y + 100);
if (x > 200 || y > 200) {
cout << 0 << endl;
continue;
}
cout << a[x][y] << endl;
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool dah[135][135];
int m[135][135];
queue<int> q;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, -1, 0, 1};
int n, nq;
int x, y;
int main() {
scanf("%d%d", &n, &nq);
memset(dah, 0, sizeof dah);
dah[65][65] = 1;
m[65][65] = n;
q.push((65 << 10) | 65);
while (!q.empty()) {
int ui = q.front() >> 10;
int uj = q.front() & 1023;
q.pop();
int d = m[ui][uj] >> 2;
m[ui][uj] -= 4 * d;
for (int i = 0; i < 4; i++) {
int ni = ui + dr[i];
int nj = uj + dc[i];
if (((ni == 65) && (ni != ui)) || ((nj == 65) && (nj != uj))) {
m[ni][nj] += 2 * d;
} else {
m[ni][nj] += d;
}
if (m[ni][nj] >= 4 && !dah[ni][nj] && (nj >= 65) && (ni >= 65)) {
dah[ni][nj] = 1;
q.push((ni << 10) | nj);
}
}
if (m[ui][uj] >= 4) {
dah[ui][uj] = 1;
q.push((ui << 10) | uj);
} else {
dah[ui][uj] = 0;
}
}
for (int i = 0; i < nq; i++) {
scanf("%d%d", &x, &y);
x = abs(x) + 65;
y = abs(y) + 65;
if ((x >= 135) || (y >= 135)) {
printf("0\n");
} else {
printf("%d\n", m[x][y]);
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int d = 101;
int A[500][500], B[500][500];
int main() {
int n, t;
cin >> n >> t;
bool ok;
int minx = 0, maxx = 0, miny = 0, maxy = 0;
A[d][d] = n;
B[d][d] = n;
int cnt = 0;
do {
cnt++;
ok = false;
for (int i = minx; i <= maxx; ++i)
for (int j = miny; j <= maxy; ++j) {
int aux = A[i + d][j + d];
while (aux >= 4) {
ok = true;
B[i + d + 1][j + d]++;
if (i + 1 > maxx) maxx = i + 1;
B[i + d][j + d + 1]++;
if (j + 1 > maxy) maxy = j + 1;
B[i + d - 1][j + d]++;
if (i - 1 < minx) minx = i - 1;
B[i + d][j + d - 1]++;
if (j - 1 < miny) miny = j - 1;
B[i + d][j + d] -= 4;
aux -= 4;
}
}
for (int i = minx; i <= maxx; ++i)
for (int j = miny; j <= maxy; ++j) A[i + d][j + d] = B[i + d][j + d];
} while (ok);
for (int i = 1; i <= t; ++i) {
int a, b;
cin >> a >> b;
if (a < minx || a > maxx || b < miny || b > maxy)
cout << 0 << "\n";
else
cout << A[a + d][b + d] << "\n";
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int num, m, a[1012][1012];
int main() {
scanf("%d%d", &num, &m);
a[200][200] = num;
int lx = 200, rx = 200, ly = 200, ry = 200;
while (1) {
int ok = 0;
for (int i = lx; i <= rx; i++)
for (int j = ly; j <= ry; j++)
if (a[i][j] >= 4) {
int x = a[i][j] / 4;
a[i - 1][j] += x;
a[i + 1][j] += x;
a[i][j - 1] += x;
a[i][j + 1] += x;
a[i][j] -= x * 4;
ok = 1;
lx = min(lx, i - 1);
rx = max(rx, i + 1);
ly = min(ly, j - 1);
ry = max(ry, j + 1);
}
if (ok == 0) break;
}
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
x += 200, y += 200;
if (lx <= x && x <= rx && ly <= y && y <= ry)
printf("%d\n", a[x][y]);
else
printf("0\n");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct point {
int X, Y;
};
vector<vector<int> > G;
vector<point> Q;
void reku(int x, int y) {
if (G[x][y] >= 4) {
G[x][y + 1] += 1;
G[x + 1][y] += 1;
G[x][y - 1] += 1;
G[x - 1][y] += 1;
G[x][y] -= 4;
reku(x, y);
}
if (G[x][y + 1] >= 4) reku(x, y + 1);
if (G[x + 1][y] >= 4) reku(x + 1, y);
if (G[x][y - 1] >= 4) reku(x, y - 1);
if (G[x - 1][y] >= 4) reku(x - 1, y);
return;
}
void Cout(int M, int x, int y) {
int A, B, X, Y;
for (int k = 0; k < M; k++) {
cin >> A >> B;
if (A > 2500 || A < -2500) {
cout << "0" << endl;
continue;
}
if (B > 2500 || B < -2500) {
cout << "0" << endl;
continue;
}
cout << G[x + A][y + B] << endl;
}
}
int main() {
G.resize(5000, vector<int>(5000));
int N, M, R;
point S;
cin >> N >> M;
R = N / 4;
G[5000 / 2][5000 / 2] = N - (R * 4);
G[5000 / 2][5000 / 2 + 1] = R;
G[5000 / 2 + 1][5000 / 2] = R;
G[5000 / 2][5000 / 2 - 1] = R;
G[5000 / 2 - 1][5000 / 2] = R;
reku(5000 / 2, 5000 / 2);
Cout(M, 5000 / 2, 5000 / 2);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 30000;
int ans[400][400];
int n, q;
void dfs(int x, int y) {
if (x > 200 || y > 200 || x < 0 || y < 0) return;
int add = 0;
if (ans[x][y] >= 4) {
add = ans[x][y] / 4;
ans[x][y] = ans[x][y] % 4;
} else {
return;
}
ans[x + 1][y] += add;
if (x > 0) ans[x - 1][y] += add;
ans[x][y + 1] += add;
if (y > 0) ans[x][y - 1] += add;
dfs(x + 1, y);
dfs(x - 1, y);
dfs(x, y + 1);
dfs(x, y - 1);
}
int main() {
while (cin >> n >> q) {
memset(ans, 0, sizeof(ans));
int cmp = ceil(0.5 * log2(n));
ans[100][100] = n;
dfs(100, 100);
for (int i = (1); i <= (q); ++i) {
int a, b;
scanf("%d%d", &a, &b);
if (abs(a) > 100 || abs(b) > 100) {
cout << 0 << endl;
continue;
}
cout << ans[a + 100][b + 100] << endl;
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int inf = 50000000;
const int maxn = 610;
const int off = 305;
int dp[maxn][maxn];
bool vis[maxn][maxn];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int main() {
int n, m, i, j, x, y, nx, ny, maxx = 0, g;
scanf("%d", &n);
dp[off][off] = n;
queue<pair<int, int> > q;
q.push(make_pair(off, off));
vis[off][off] = 1;
while (!q.empty()) {
x = q.front().first;
y = q.front().second;
g = dp[x][y] / 4;
dp[x][y] -= (g * 4);
vis[x][y] = 0;
q.pop();
if (!g) continue;
for (i = 0; i < 4; i++) {
nx = x + dx[i];
ny = y + dy[i];
dp[nx][ny] += g;
if (dp[nx][ny] >= 4 && !vis[nx][ny]) {
vis[nx][ny] = 1;
q.push(make_pair(nx, ny));
}
}
}
scanf("%d", &m);
while (m--) {
scanf("%d", &x), scanf("%d", &y);
x += off, y += off;
if (x >= maxn || y >= maxn || x < 0 || y < 0) {
printf("0\n");
continue;
}
printf("%d\n", dp[x][y]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long rdtsc() {
long long tmp;
asm("rdtsc" : "=A"(tmp));
return tmp;
}
const int maxx = 130;
int value[maxx][maxx], tmp[maxx][maxx];
const int shift = maxx / 2;
void push(int y, int x) {
assert(y && x && y < maxx - 1 && x < maxx - 1);
int cnt = value[y][x] / 4;
value[y][x] -= 4 * cnt;
value[y + 1][x] += cnt;
value[y - 1][x] += cnt;
value[y][x + 1] += cnt;
value[y][x - 1] += cnt;
}
void solver(int n) {
if (!n) {
memset(value, 0, sizeof(value));
return;
}
solver(n / 4);
memcpy(tmp, value, sizeof(value));
for (int i = 1; i < maxx - 1; i++)
for (int j = 1; j < maxx - 1; j++) {
value[i][j] =
tmp[i - 1][j] + tmp[i][j - 1] + tmp[i][j + 1] + tmp[i + 1][j];
}
value[shift][shift] += n % 4;
while (1) {
bool ch = 0;
for (int i = 0; i < maxx; i++)
for (int j = 0; j < maxx; j++)
if (value[i][j] >= 4) {
push(i, j);
ch = 1;
}
if (!ch) break;
}
}
int main() {
srand(rdtsc());
int n, t;
while (scanf("%d%d", &n, &t) >= 1) {
solver(n);
for (int i = -4; i <= 4; i++)
for (int j = -4; j <= 4; j++)
fprintf(stderr, "%d%c", value[i + shift][j + shift], " \n"[j == 4]);
for (int i = 0; i < t; i++) {
int x, y;
scanf("%d%d", &x, &y);
x += shift, y += shift;
if (x < 0 || y < 0 || abs(x) >= maxx || abs(y) >= maxx)
printf("0\n");
else
printf("%d\n", value[y][x]);
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1000000000,1000000000")
using namespace std;
const long long inf = 1e18 + 7;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
const double PI = 2 * acos(0.0);
const double E = 2.71828;
long long a[205][205], used[205][205], timer = 1;
long long dx[] = {0, 0, 1, -1, 0};
long long dy[] = {1, -1, 0, 0, 0};
long long qx[40005], qy[40005], bx[40005], by[40005], sz = 0;
void process() {
for (long long(q) = 0; (q) < (long long)(sz); (q)++) {
long long i = qx[q], j = qy[q];
long long now = a[i][j] >> 2LL;
a[i][j] -= now * 4;
for (long long(k) = 0; (k) < (long long)(4); (k)++) {
i += dx[k];
j += dy[k];
a[i][j] += now;
i -= dx[k];
j -= dy[k];
}
}
long long size = sz;
sz = 0;
for (long long(i) = 0; (i) < (long long)(size); (i)++)
bx[i] = qx[i], by[i] = qy[i];
for (long long(q) = 0; (q) < (long long)(size); (q)++) {
long long i = bx[q], j = by[q];
for (long long(k) = 0; (k) < (long long)(5); (k)++) {
i += dx[k];
j += dy[k];
if (a[i][j] >= 4 && used[i][j] != timer) {
used[i][j] = timer;
qx[sz] = i;
qy[sz] = j;
sz++;
}
i -= dx[k];
j -= dy[k];
}
}
}
void simple() {
long long n = 205;
long long mini = inf, maxi = -inf, minj = inf, maxj = -inf;
for (long long(i) = 0; (i) < (long long)(n); (i)++)
for (long long(j) = 0; (j) < (long long)(n); (j)++) {
cin >> a[i][j];
if (a[i][j]) {
maxi = max(maxi, i);
maxj = max(maxj, j);
mini = min(mini, i);
minj = min(minj, j);
}
}
cout << maxi << " " << mini << " " << maxj << " " << minj << endl;
exit(0);
}
int main(void) {
long long n, t;
cin >> n >> t;
a[105][105] = n;
if (n >= 4) {
qx[sz] = 105;
qy[sz] = 105;
sz++;
}
while (sz) {
process();
timer++;
}
for (long long(i) = 0; (i) < (long long)(t); (i)++) {
int x, y;
scanf("%d%d", &x, &y);
x += 105;
y += 105;
if (x < 0 || y < 0 || x > 204 || y > 204)
puts("0");
else {
printf("%d\n", int(a[x][y]));
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
ifstream Cin("input.txt");
ofstream Cout("output.txt");
const long long INF = (long long)1e15;
int m[401][401] = {};
void Try(int x, int y, int v);
void dfs(int x, int y);
int main() {
int n, t, i, j, k, x, y, it;
cin >> n >> k;
m[200][200] = n;
dfs(200, 200);
for (i = 1; i <= k; ++i) {
cin >> x >> y;
if (x < -200 || x > 200 || y < -200 || y > 200)
cout << 0 << endl;
else
cout << m[x + 200][y + 200] << endl;
}
}
void Try(int x, int y, int v) {
m[x][y] += v;
if (m[x][y] >= 4) dfs(x, y);
}
void dfs(int x, int y) {
int d = m[x][y] / 4;
m[x][y] %= 4;
Try(x + 1, y, d);
Try(x - 1, y, d);
Try(x, y + 1, d);
Try(x, y - 1, d);
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long convertToNum(string s) {
long long val = 0;
for (int i = 0; i < (s.size()); i++) val = val * 10 + s[i] - '0';
return val;
}
char bu[50];
string convertToString(int a) {
sprintf(bu, "%d", a);
return string(bu);
}
long long GCD(long long x, long long y) {
if (!x) return y;
if (!y) return x;
if (x == y) return x;
if (x < y)
return GCD(x, y % x);
else
return GCD(x % y, y);
}
long long POW(long long x, long long y, long long Base) {
if (!y) return 1;
long long u = POW(x, y / 2, Base);
u = (u * u) % Base;
if (y % 2)
return (u * x) % Base;
else
return u;
}
void extended_euclid(long long A, long long B, long long &x, long long &y) {
if (A == 1 && B == 0) {
x = 1;
y = 0;
return;
}
if (A < B)
extended_euclid(B, A, y, x);
else {
long long xx, yy;
extended_euclid(A % B, B, xx, yy);
x = xx;
y = yy - (A / B) * xx;
}
}
set<pair<int, pair<int, int> > > se;
map<pair<int, int>, int> Map;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int n, q;
int res[1000][1000];
int TEST;
int maxC = 100;
int dd[1000][1000];
vector<pair<int, int> > ls[2];
int main() {
cin >> n >> q;
res[0 + maxC][0 + maxC] = n;
ls[0].push_back(make_pair(0, 0));
int flag = 0;
for (int iter = 0; iter < (30000); iter++) {
TEST++;
ls[1 - flag].clear();
if (!ls[flag].size()) {
break;
}
for (int i = 0; i < (ls[flag].size()); i++) {
int u = ls[flag][i].first, v = ls[flag][i].second;
int delta = res[u + maxC][v + maxC];
if (delta >= 4) {
for (int k = 0; k < (4); k++) {
res[u + dx[k] + maxC][v + dy[k] + maxC] += delta / 4;
if (res[u + dx[k] + maxC][v + dy[k] + maxC] >= 4 &&
dd[u + dx[k] + maxC][v + dy[k] + maxC] != TEST) {
dd[u + dx[k] + maxC][v + dy[k] + maxC] = TEST;
ls[1 - flag].push_back(make_pair(u + dx[k], v + dy[k]));
}
}
res[u + maxC][v + maxC] -= (delta / 4) * 4;
}
}
flag = !flag;
}
int u, v;
for (int i = (1); i <= (q); i++) {
scanf("%d%d", &u, &v);
if (abs(u) > 90 || abs(v) > 90)
printf("0\n");
else
printf("%d\n", res[u + maxC][v + maxC]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int qf, ql;
struct ppair {
int first, second;
inline ppair(int x, int y) {
first = x;
second = y;
}
inline ppair() {}
};
inline ppair make_ppair(int x, int y) { return ppair(x, y); }
ppair q[400000];
int field[4000][4000];
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
bool inq[2000 * 2][2000 * 2];
int main() {
int n;
scanf("%d", &n);
memset(field, 0, sizeof field);
memset(inq, 0, sizeof inq);
field[0 + 2000][0 + 2000] = n;
qf = ql = 0;
q[ql++] = (make_ppair(0, 0));
int it = 0;
while (ql != qf) {
it++;
ppair p = q[qf++];
inq[p.first + 2000][p.second + 2000] = false;
if (qf == 400000) qf = 0;
if (field[p.first + 2000][p.second + 2000] < 4) continue;
field[p.first + 2000][p.second + 2000] -= 4;
if (field[p.first + 2000][p.second + 2000] >= 4) {
q[ql++] = p;
inq[p.first + 2000][p.second + 2000] = true;
}
if (ql == 400000) ql = 0;
for (int i = 0; i < 4; i++) {
field[p.first + 2000 + dx[i]][p.second + 2000 + dy[i]]++;
if (field[p.first + 2000 + dx[i]][p.second + 2000 + dy[i]] >= 4 &&
!inq[p.first + 2000 + dx[i]][p.second + 2000 + dy[i]]) {
q[ql++] = (make_ppair(p.first + dx[i], p.second + dy[i]));
inq[p.first + 2000 + dx[i]][p.second + 2000 + dy[i]] = true;
}
if (ql == 400000) ql = 0;
}
}
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (abs(x) <= 2000 && abs(y) <= 2000)
printf("%d\n", field[2000 + x][2000 + y]);
else
printf("0\n");
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 205;
int n, t, ans[N][N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> t;
ans[N / 2][N / 2] = n;
bool change = true;
while (change) {
change = false;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (ans[i][j] < 4) continue;
change = true;
ans[i][j - 1] += ans[i][j] / 4;
ans[i][j + 1] += ans[i][j] / 4;
ans[i + 1][j] += ans[i][j] / 4;
ans[i - 1][j] += ans[i][j] / 4;
ans[i][j] = ans[i][j] % 4;
}
}
}
int x, y;
for (int i = 0; i < t; i++) {
cin >> x >> y;
x += N / 2;
y += N / 2;
if (x < 0 || x >= N || y < 0 || y >= N) {
cout << 0 << '\n';
} else {
cout << ans[x][y] << '\n';
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, M[129][129];
memset(M, 0, sizeof M);
scanf("%d", &n);
M[64][64] = n;
vector<pair<int, int> > s1, s2;
bool used[129][129];
if (n >= 4) s1.push_back(make_pair(64, 64));
int it = 0;
while (!s1.empty()) {
memset(used, false, sizeof used);
s2.clear();
for (int i = s1.size() - 1; i >= 0; --i) {
int cx = s1[i].first, cy = s1[i].second;
M[cx][cy] -= 4;
}
for (int i = s1.size() - 1; i >= 0; --i) {
int cx = s1[i].first, cy = s1[i].second;
++M[cx - 1][cy];
++M[cx + 1][cy];
++M[cx][cy - 1];
++M[cx][cy + 1];
if (M[cx - 1][cy] >= 4 && !used[cx - 1][cy])
s2.push_back(make_pair(cx - 1, cy)), used[cx - 1][cy] = true;
if (M[cx + 1][cy] >= 4 && !used[cx + 1][cy])
s2.push_back(make_pair(cx + 1, cy)), used[cx + 1][cy] = true;
if (M[cx][cy - 1] >= 4 && !used[cx][cy - 1])
s2.push_back(make_pair(cx, cy - 1)), used[cx][cy - 1] = true;
if (M[cx][cy + 1] >= 4 && !used[cx][cy + 1])
s2.push_back(make_pair(cx, cy + 1)), used[cx][cy + 1] = true;
if (M[cx][cy] >= 4 && !used[cx][cy])
s2.push_back(make_pair(cx, cy)), used[cx][cy] = true;
}
s1 = s2;
}
int Q, qx, qy;
scanf("%d", &Q);
while (Q--) {
scanf("%d %d", &qx, &qy);
if (abs(qx) <= 64 && abs(qy) <= 64)
printf("%d\n", M[qx + 64][qy + 64]);
else
printf("0\n");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int ant[70][70];
int main() {
int n, tt;
scanf("%d%d", &n, &tt);
ant[0][0] = n;
int to;
to = 0;
while (1) {
bool check = false;
for (int i = 0; i <= to; i++) {
for (int j = 0; j <= i; j++) {
if (ant[i][j] >= 4) {
int temp = ant[i][j] / 4;
if (i - 1 == 0 && j == 0)
ant[0][0] += temp * 4;
else if (i - 1 == j)
ant[i - 1][j] += temp * 2;
else
ant[i - 1][j] += temp;
ant[i + 1][j] += temp;
if (j - 1 == 0)
ant[i][j - 1] += temp * 2;
else
ant[i][j - 1] += temp;
if (i == j + 1)
ant[i][j + 1] += temp * 2;
else
ant[i][j + 1] += temp;
ant[i][j] %= 4;
if (ant[i + 1][j] >= 4) to = max(to, i + 1);
check = true;
}
}
}
if (!check) break;
}
for (int case_ = 0; case_ < tt; case_++) {
int y, x;
scanf("%d%d", &x, &y);
if (abs(x) > 65 || abs(y) > 65)
cout << "0\n";
else
cout << ant[max(abs(x), abs(y))][min(abs(x), abs(y))] << "\n";
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int INF = (1 << 30) - 1;
const int MAXN = 100010;
const int mod = 1e9 + 7;
map<pair<int, int>, int> mp, tmp;
map<pair<int, int>, int>::iterator it, tit;
int n, t;
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int main() {
scanf("%d%d", &n, &t);
mp[make_pair(0, 0)] = n;
int cnt = 0;
while (1) {
++cnt;
bool f = false;
tmp.clear();
for (it = mp.begin(); it != mp.end(); it++) {
if ((*it).second >= 4) {
f = true;
int k = (*it).second / 4;
pair<int, int> cur = (*it).first;
mp[cur] %= 4;
for (int i = 0; i < 4; ++i) {
int tfirst = cur.first + dir[i][0];
int tsecond = cur.second + dir[i][1];
if (tfirst < 0 || tsecond < 0 || tsecond > tfirst) continue;
int C = 1;
if (!tfirst && !tsecond)
C = 4;
else if (tfirst == tsecond)
C = 2;
else if (tsecond == 0 && cur.second)
C = 2;
tmp[make_pair(cur.first + dir[i][0], cur.second + dir[i][1])] +=
C * k;
}
}
}
for (tit = tmp.begin(); tit != tmp.end(); tit++) {
mp[(*tit).first] += (*tit).second;
}
if (!f) break;
}
for (int i = 1; i <= t; ++i) {
int a, b;
scanf("%d%d", &a, &b);
if (a < 0) a = -a;
if (b < 0) b = -b;
if (b > a) swap(a, b);
printf("%d\n", mp[make_pair(a, b)]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2, class T3>
struct trio {
T1 first;
T2 second;
T3 third;
trio() : first(T1()), second(T2()), third(T3()) {}
trio(const T1 &x, const T2 &y, const T3 &z) : first(x), second(y), third(z) {}
template <class X, class Y, class Z>
trio(const trio<X, Y, Z> &p)
: first(p.first), second(p.second), third(p.third) {}
};
template <class T1, class T2, class T3>
bool operator<(const trio<T1, T2, T3> &x, const trio<T1, T2, T3> &y) {
if (x.first < y.first)
return true;
else if (x.first > y.first)
return false;
if (x.second < y.second)
return true;
else if (x.second > y.second)
return false;
if (x.third < y.third)
return true;
else if (x.third > y.third)
return false;
return true;
}
template <typename T>
ostream &operator<<(ostream &o, set<T> &v) {
for (typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
cout << *it << " ";
o << endl;
return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &x) {
o << x.first << "," << x.second << " ";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, vector<T> &v) {
for (typeof(v.size()) i = 0; i < v.size(); ++i) o << v[i] << ' ';
o << endl;
return o;
}
template <typename T>
istream &operator>>(istream &i, vector<T> &v) {
int n;
i >> n;
while (n--) {
int x;
i >> x;
v.push_back(x);
}
return i;
}
char vis[500 * 2][500 * 2];
int ans[500 * 2][500 * 2];
int dx[] = {-1, +1, 0, 0};
int dy[] = {-0, +0, 1, -1};
void preprocess(int n) {
queue<pair<int, int> > q;
q.push(pair<int, int>(500, 500));
ans[500][500] = n;
if (n < 4) return;
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
int x = cur.first;
int y = cur.second;
vis[x][y] = 0;
int d = ans[x][y] >> 2;
ans[x][y] &= 3;
for (int i = 0; i < 4; ++i) {
int newx = x + dx[i];
int newy = y + dy[i];
if ((ans[newx][newy] += d) > 3)
if (vis[newx][newy] == 0) {
vis[newx][newy] = 1;
q.push(pair<int, int>(newx, newy));
}
}
}
}
int main() {
int n;
cin >> n;
preprocess(n);
int points;
cin >> points;
while (points--) {
int x, y;
cin >> x >> y;
x = max(x, -x);
y = max(y, -y);
if ((x > 500) || (y > 500)) {
cout << "0" << endl;
continue;
}
cout << ans[500 + x][500 + y] << endl;
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int cnt[1000][1000], vst[1000][1000], flag;
int dr[] = {1, 0, -1, 0};
int dc[] = {0, 1, 0, -1};
int main() {
int N, i, T, v, r, c, rr, cc;
scanf("%d%d", &N, &T);
memset(cnt, 0, sizeof(cnt));
cnt[500][500] = N;
queue<int> q[2];
q[0] = q[1] = queue<int>();
q[0].push(500);
q[0].push(500);
v = 1;
while (1) {
v = 1 - v;
flag++;
if (q[v].empty()) break;
while (!q[v].empty()) {
r = q[v].front();
q[v].pop();
c = q[v].front();
q[v].pop();
if (cnt[r][c] < 4) continue;
cnt[r][c] -= 4;
if (cnt[r][c] >= 4) {
if (vst[r][c] != flag) {
vst[r][c] = flag;
q[1 - v].push(r);
q[1 - v].push(c);
}
}
for (i = 0; i < 4; i++) {
rr = r + dr[i];
cc = c + dc[i];
cnt[rr][cc]++;
if (cnt[rr][cc] >= 4 && vst[rr][cc] != flag) {
q[1 - v].push(rr);
q[1 - v].push(cc);
vst[rr][cc] = flag;
}
}
}
}
for (i = 0; i < T; i++) {
scanf("%d%d", &r, &c);
if (abs(r) > 480 || abs(c) > 480)
puts("0");
else
printf("%d\n", cnt[r + 500][c + 500]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int SZ = 100;
int cnt[2 * SZ + 1][2 * SZ + 1];
int used[2 * SZ + 1][2 * SZ + 1];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
const int MX = 11111;
int Px[MX], Py[MX], s = 0, e = 0;
void push(int first, int second) {
Px[e] = first;
Py[e++] = second;
if (e >= MX) e -= MX;
}
void pop() {
s++;
if (s >= MX) s -= MX;
}
int main() {
int n, t;
scanf("%d%d", &n, &t);
cnt[0 + SZ][0 + SZ] = n;
push(0, 0);
while (s != e) {
int tx = Px[s], ty = Py[s];
pop();
if (cnt[tx + SZ][ty + SZ] >= 4) {
for (int k = (0); k <= ((4) - 1); k++) {
int was = cnt[tx + dx[k] + SZ][ty + dy[k] + SZ];
cnt[tx + dx[k] + SZ][ty + dy[k] + SZ] += cnt[tx + SZ][ty + SZ] / 4;
if (was < 4 && cnt[tx + dx[k] + SZ][ty + dy[k] + SZ] >= 4) {
push(tx + dx[k], ty + dy[k]);
}
}
cnt[tx + SZ][ty + SZ] %= 4;
}
}
for (int i = (0); i <= ((t)-1); i++) {
int first, second;
scanf("%d%d", &first, &second);
if (abs(first) >= 80 || abs(second) >= 80)
printf("0\n");
else
printf("%d\n", cnt[first + SZ][second + SZ]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int N = 1e3 + 5, lg = 27, mod = 5e4 + 7;
int da[N][N];
void solve() {
int n, t;
cin >> n >> t;
queue<pair<int, int>> q;
q.push({500, 500});
da[500][500] = n;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
while (!q.empty()) {
int u = q.front().first, v = q.front().second;
q.pop();
int z = da[u][v] / 4;
if (da[u][v] < 4) {
continue;
}
da[u][v] %= 4;
for (int i = 0; i < 4; i++) {
int x = u + dx[i], y = v + dy[i];
da[x][y] += z;
if ((da[x][y]) / 4) {
q.push({x, y});
}
}
}
while (t--) {
int x, y;
cin >> x >> y;
x += 500, y += 500;
if (x < 0 || y < 0 || x >= 1000 || y >= 1000) {
cout << 0 << endl;
} else {
cout << da[x][y] << endl;
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
int testCases = 1;
while (testCases--) {
solve();
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int N, Q, a[309][309], ap[309][309];
queue<pair<int, int> > cc;
void try_add(int x, int y) {
if (a[x][y] >= 4 && ap[x][y] == 0) ap[x][y] = 1, cc.push(make_pair(x, y));
}
int main() {
scanf("%d", &N);
a[150][150] = N;
ap[150][150] = 1;
cc.push(make_pair(150, 150));
while (!cc.empty()) {
pair<int, int> curr = cc.front();
cc.pop();
int X = curr.first, Y = curr.second;
ap[X][Y] = 0;
if (a[X][Y] < 4) continue;
a[X - 1][Y] += a[X][Y] >> 2, try_add(X - 1, Y);
a[X + 1][Y] += a[X][Y] >> 2, try_add(X + 1, Y);
a[X][Y + 1] += a[X][Y] >> 2, try_add(X, Y + 1);
a[X][Y - 1] += a[X][Y] >> 2, try_add(X, Y - 1);
a[X][Y] &= 3;
}
int mini = 10000, minj = 10000, maxi = -10000, maxj = -10000;
for (int i = 1; i < 300; i++)
for (int j = 1; j < 300; j++)
if (a[i][j]) {
if (i > maxi) maxi = i;
if (i < mini) mini = i;
if (j > maxj) maxj = j;
if (j < minj) minj = j;
}
scanf("%d", &Q);
while (Q) {
Q--;
int x, y;
scanf("%d %d", &x, &y);
x += 150, y += 150;
if (x >= mini && x <= maxi && y >= minj && y <= maxj)
printf("%d\n", a[x][y]);
else
printf("0\n");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
int mp[5555][5555], n, t;
int xx[4] = {0, 0, -1, 1}, yy[4] = {1, -1, 0, 0};
void dfs(int x, int y, int numb) {
mp[x][y] += numb;
int now = mp[x][y];
if (mp[x][y] < 4) return;
mp[x][y] %= 4;
for (int i = 0; i < 4; i++) {
dfs(x + xx[i], y + yy[i], now / 4);
}
}
int main() {
while (scanf("%d%d", &n, &t) != EOF) {
memset(mp, 0, sizeof(mp));
dfs(2000, 2000, n);
while (t--) {
int x, y;
scanf("%d%d", &x, &y);
if (x + 2000 > 5000 || y + 2000 > 5000 || x + 2000 < 0 || y + 2000 < 0)
printf("0\n");
else
printf("%d\n", mp[x + 2000][y + 2000]);
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[200][200];
int main() {
int n, t;
cin >> n >> t;
a[100][100] = n;
bool b = 1;
while (b) {
b = 0;
for (int x = 1; x < 200 - 1; x++) {
for (int y = 1; y < 200 - 1; y++) {
if (a[x][y] >= 4) {
a[x - 1][y] += a[x][y] / 4;
a[x][y - 1] += a[x][y] / 4;
a[x + 1][y] += a[x][y] / 4;
a[x][y + 1] += a[x][y] / 4;
b = 1;
a[x][y] %= 4;
}
}
}
}
for (int x = 0; x < t; x++) {
int c, d;
cin >> c >> d;
if (abs(c) >= 90 || abs(d) >= 90)
cout << 0 << endl;
else
cout << a[c + 100][d + 100] << endl;
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long infll = 0x3f3f3f3f3f3f3f3fLL;
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int a[1000][1000];
void solve(int x, int y) {
if (a[x][y] < 3)
a[x][y]++;
else {
a[x][y] = 0;
solve(x + 1, y);
solve(x, y + 1);
solve(x, y - 1);
solve(x - 1, y);
}
}
int main() {
int n = read(), q = read();
for (int i = 0; i < n; i++) solve(500, 500);
for (int i = 0; i < q; i++) {
int x = read(), y = read();
if (x > 100 || x < -100 || y > 100 || y < -100)
cout << "0" << endl;
else
printf("%d\n", a[x + 500][y + 500]);
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int v[200 + 10][200 + 10];
bool f[200 + 10][200 + 10];
queue<pair<int, int> > q;
pair<int, int> tmp[10000];
int sz;
int main() {
int n, t;
cin >> n >> t;
int bs = 100;
for (int i = 0; i <= 200; ++i)
for (int j = 0; j <= 200; ++j) f[i][j] = false, v[i][j] = 0;
v[bs][bs] = n;
if (n >= 4) {
f[bs][bs] = true;
q.push(make_pair(bs, bs));
}
while (true) {
if (q.empty()) break;
int i, j;
sz = 0;
while (!q.empty()) {
i = q.front().first;
j = q.front().second;
q.pop();
f[i][j] = false;
v[i][j] -= 4;
++v[i + 1][j];
++v[i - 1][j];
++v[i][j - 1];
++v[i][j + 1];
tmp[sz++] = make_pair(i, j);
}
for (int t = 0; t < sz; ++t) {
i = tmp[t].first;
j = tmp[t].second;
if (v[i][j] >= 4 && !f[i][j]) {
f[i][j] = true;
q.push(make_pair(i, j));
}
if (v[i + 1][j] >= 4 && !f[i + 1][j]) {
f[i + 1][j] = true;
q.push(make_pair(i + 1, j));
}
if (v[i - 1][j] >= 4 && !f[i - 1][j]) {
f[i - 1][j] = true;
q.push(make_pair(i - 1, j));
}
if (v[i][j - 1] >= 4 && !f[i][j - 1]) {
f[i][j - 1] = true;
q.push(make_pair(i, j - 1));
}
if (v[i][j + 1] >= 4 && !f[i][j + 1]) {
f[i][j + 1] = true;
q.push(make_pair(i, j + 1));
}
}
}
for (int i = 0; i < t; i += 1) {
int x, y;
scanf("%d %d", &x, &y);
if (abs(x) > 65 || abs(y) > 65) {
printf("0");
} else {
printf("%d", v[bs - y][bs + x]);
}
printf("\n");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename C, typename T = decay<decltype(*begin(declval<C>()))>,
typename enable_if<!is_same<C, string>::value>::type * = nullptr>
ostream &operator<<(ostream &os, const C &c) {
bool f = true;
os << "[";
for (const auto &x : c) {
if (!f) os << ", ";
f = false;
os << x;
}
return os << "]";
}
template <typename T>
void debug(string s, T x) {
cerr << s << " = " << x << "\n";
}
template <typename T, typename... Args>
void debug(string s, T x, Args... args) {
cerr << s.substr(0, s.find(',')) << " = " << x << " | ";
debug(s.substr(s.find(',') + 2), args...);
}
int grid[1005][1005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, t;
cin >> n >> t;
grid[500][500] = n;
int sz = 0;
bool done = false;
while (!done) {
done = true;
for (int i = 500 - sz; i <= 500 + sz; i++)
for (int j = 500 - sz; j <= 500 + sz; j++)
if (grid[i][j] >= 4) {
if (max(abs(i - 500), abs(j - 500)) == sz) sz++;
done = false;
grid[i + 1][j] += grid[i][j] / 4;
grid[i - 1][j] += grid[i][j] / 4;
grid[i][j + 1] += grid[i][j] / 4;
grid[i][j - 1] += grid[i][j] / 4;
grid[i][j] %= 4;
}
}
while (t--) {
int x, y;
cin >> x >> y;
if (max(abs(x), abs(y)) > sz)
cout << "0\n";
else
cout << grid[x + 500][y + 500] << "\n";
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int ans[200][200];
void mov(int x, int y) {
if (ans[x][y] < 3) {
ans[x][y]++;
} else {
ans[x][y] = 0;
mov(x - 1, y);
mov(x, y - 1);
mov(x + 1, y);
mov(x, y + 1);
}
}
int main() {
int n, m;
scanf("%d", &n);
for (int i = 0; i < n; i++) mov(100, 100);
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
if (x < -70 || y < -70 || x > 70 || y > 70)
puts("0");
else {
x += 100;
y += 100;
printf("%d\n", ans[x][y]);
}
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[1005][1005];
int n, m, x, y;
void dfs(int b, int c, int d) {
a[b][c] += d;
if (a[b][c] < 4) {
return;
}
a[b][c] -= 4;
dfs(b, c, 0);
dfs(b + 1, c, 1);
dfs(b - 1, c, 1);
dfs(b, c + 1, 1);
dfs(b, c - 1, 1);
return;
}
int main() {
scanf("%d %d", &n, &m);
dfs(500, 500, n);
for (int i = 0; i < m; i++) {
scanf("%d %d", &x, &y);
if (x > 500 || y > 500 || x < -500 || y < -500) {
printf("0\n");
continue;
}
printf("%d\n", a[x + 500][y + 500]);
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct point {
int x, y;
};
bool f[2000][2000];
deque<point> st;
int mang[2000][2000], base = 1000, n, top, day, dx[5], dy[5];
void solve() {
memset(f, true, sizeof(f));
memset(mang, 0, sizeof(mang));
mang[0 + base][0 + base] = n;
point u, v;
u.x = 0;
u.y = 0;
top = 1;
day = 1;
st.push_back(u);
int value, t;
while (!st.empty()) {
u = st.front();
st.pop_front();
f[u.x + base][u.y + base] = false;
if (mang[u.x + base][u.y + base] >= 4) {
for (t = 1; t <= 4; t++) {
v.x = u.x + dx[t];
v.y = u.y + dy[t];
value = mang[v.x + base][v.y + base];
mang[v.x + base][v.y + base] += mang[u.x + base][u.y + base] / 4;
if (value < 4) {
if (mang[v.x + base][v.y + base] >= 4) st.push_back(v);
}
}
mang[u.x + base][u.y + base] %= 4;
}
}
}
void init() {
dx[1] = 1;
dx[2] = -1;
dx[3] = 0;
dx[4] = 0;
dy[1] = 0;
dy[2] = 0;
dy[3] = 1;
dy[4] = -1;
}
int main() {
int i, test, a, b;
cin >> n >> test;
init();
solve();
for (i = 1; i <= test; i++) {
scanf("%d%d", &a, &b);
if ((a + base >= 0) && (a + base < 2000) && (b + base >= 0) &&
(b + base < 2000))
printf("%d\n", mang[a + base][b + base]);
else
printf("0\n");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
using namespace std;
int n, t;
int m[1000][1000];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
cin >> n >> t;
m[500][500] = n;
bool flag = n >= 4;
int sz = 0;
while (flag) {
flag = false;
for (int i = 500 - sz; i <= 500 + sz; ++i) {
for (int j = 500 - sz; j <= 500 + sz; ++j) {
for (int k = 0; k < 4; ++k) m[i + dx[k]][j + dy[k]] += m[i][j] / 4;
flag |= m[i][j] >= 4;
if (m[i][j] >= 4) {
sz = max(sz, max(abs(i - 500), abs(j - 500)) + 1);
}
m[i][j] %= 4;
}
}
}
while (t--) {
int x, y;
scanf("%d%d", &x, &y);
if (max(abs(x), abs(y)) > 500) {
printf("0\n");
continue;
}
printf("%d\n", m[x + 500][y + 500]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
const int N = 201;
const int M = 100010;
const int inf = 1061109567;
const double eps = 1e-10;
const double dinf = 1e10;
const double Pi = 3.14159265358;
using namespace std;
int n, m;
int p[N][N];
int main() {
cin >> n >> m;
memset(p, 0, sizeof(p));
p[100][100] = n;
bool pass = 1;
while (pass) {
pass = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (p[i][j] >= 4) {
if (i > 0) p[i - 1][j] += (p[i][j] >> 2);
if (j > 0) p[i][j - 1] += (p[i][j] >> 2);
if (j + 1 < N) p[i][j + 1] += (p[i][j] >> 2);
if (i + 1 < N) p[i + 1][j] += (p[i][j] >> 2);
p[i][j] = p[i][j] & 3;
pass = 1;
}
}
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (abs(x) <= 100 && abs(y) <= 100)
printf("%d\n", p[x + 100][y + 100]);
else
printf("0\n");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int grid[500][500], n, m, x, y, endi = 400;
int p1[] = {0, 1, 0, -1}, p2[] = {1, 0, -1, 0};
void Solve(int X, int Y) {
if (grid[X][Y] < 4) return;
long long int z = grid[X][Y] / 4;
grid[X][Y] -= (z * 4);
for (int i = 0; i < 4; i++)
grid[X + p1[i]][Y + p2[i]] += z, Solve(X + p1[i], Y + p2[i]);
}
int main() {
scanf("%I64d %I64d", &n, &m);
x += 200, y += 200, grid[x][y] = n, Solve(x, y);
for (int i = 0; i < m; i++) {
scanf("%I64d %I64d", &x, &y);
x += 200, y += 200;
if (x >= endi || y >= endi || x < 0 || y < 0)
printf("0\n");
else
printf("%I64d\n", grid[x][y]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[800][800];
int main() {
ios_base::sync_with_stdio(0);
int n, t;
cin >> n >> t;
a[200][200] = n;
queue<pair<int, int> > myqueue;
myqueue.push(make_pair(200, 200));
while (!myqueue.empty()) {
int x = myqueue.front().first;
int y = myqueue.front().second;
myqueue.pop();
if (a[x][y] / 4 > 0) {
a[x + 1][y] += a[x][y] / 4;
a[x - 1][y] += a[x][y] / 4;
a[x][y + 1] += a[x][y] / 4;
a[x][y - 1] += a[x][y] / 4;
a[x][y] = a[x][y] % 4;
myqueue.push(make_pair(x + 1, y));
myqueue.push(make_pair(x - 1, y));
myqueue.push(make_pair(x, y + 1));
myqueue.push(make_pair(x, y - 1));
}
}
while (t--) {
int xx, yy;
cin >> xx >> yy;
xx = xx + 200;
yy = yy + 200;
if (xx < 0 || xx > 400 || yy < 0 || yy > 400)
cout << 0 << endl;
else
cout << a[xx][yy] << endl;
}
}
| 8 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
int a, b, c, d, n, m, k;
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};
int mas[129][129], nx[129][129];
int main() {
scanf("%d", &n);
mas[64][64] = n;
int iter = 0;
int mx = 0;
while (1) {
memset((nx), 0, sizeof(nx));
bool upd = 0;
for (int _n(128), i(63); i <= _n; i++) {
for (int _n(128), j(63); j <= _n; j++) {
if (mas[i][j] < 4) {
nx[i][j] += mas[i][j];
continue;
}
upd = 1;
for (int _n((4) - 1), z(0); z <= _n; z++) {
int ni = i + di[z];
int nj = j + dj[z];
if (ni < 0 || ni > 128 || nj < 0 || nj > 128) {
continue;
}
++nx[ni][nj];
}
nx[i][j] += mas[i][j] - 4;
}
}
for (int _n((65) - 1), i(0); i <= _n; i++) {
for (int _n((65) - 1), j(0); j <= _n; j++) {
mas[i + 64][j + 64] = nx[i + 64][j + 64];
mas[64 - i][j + 64] = nx[i + 64][j + 64];
mas[i + 64][64 - j] = nx[i + 64][j + 64];
mas[64 - i][64 - j] = nx[i + 64][j + 64];
}
}
if (!upd) break;
}
scanf("%d", &m);
for (int _n((m)-1), i(0); i <= _n; i++) {
scanf("%d%d", &a, &b);
if (abs(a) > 64 || abs(b) > 64)
printf("0\n");
else
printf("%d\n", mas[a + 64][b + 64]);
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, M[129][129];
memset(M, 0, sizeof M);
scanf("%d", &n);
M[64][64] = n;
vector<pair<int, int> > s1, s2;
bool used[129][129];
if (n >= 4) s1.push_back(make_pair(64, 64));
int it = 0;
while (!s1.empty()) {
memset(used, false, sizeof used);
s2.clear();
for (int i = s1.size() - 1; i >= 0; --i) {
int cx = s1[i].first, cy = s1[i].second;
M[cx][cy] -= 4;
}
for (int i = s1.size() - 1; i >= 0; --i) {
int cx = s1[i].first, cy = s1[i].second;
if (cx == 0 || cy == 0 || cx == 128 || cy == 128) cout << ":O" << endl;
++M[cx - 1][cy];
++M[cx + 1][cy];
++M[cx][cy - 1];
++M[cx][cy + 1];
if (M[cx - 1][cy] >= 4 && !used[cx - 1][cy])
s2.push_back(make_pair(cx - 1, cy)), used[cx - 1][cy] = true;
if (M[cx + 1][cy] >= 4 && !used[cx + 1][cy])
s2.push_back(make_pair(cx + 1, cy)), used[cx + 1][cy] = true;
if (M[cx][cy - 1] >= 4 && !used[cx][cy - 1])
s2.push_back(make_pair(cx, cy - 1)), used[cx][cy - 1] = true;
if (M[cx][cy + 1] >= 4 && !used[cx][cy + 1])
s2.push_back(make_pair(cx, cy + 1)), used[cx][cy + 1] = true;
if (M[cx][cy] >= 4 && !used[cx][cy])
s2.push_back(make_pair(cx, cy)), used[cx][cy] = true;
}
s1 = s2;
}
int Q, qx, qy;
scanf("%d", &Q);
while (Q--) {
scanf("%d %d", &qx, &qy);
if (abs(qx) <= 64 && abs(qy) <= 64)
printf("%d\n", M[qx + 64][qy + 64]);
else
printf("0\n");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[70][70];
int main() {
long i, j, n, t, x[50000], y[50000];
int ri, rj, p;
bool ch;
cin >> n >> t;
for (i = 0; i < t; i++) cin >> x[i] >> y[i];
if (n <= 3) {
for (i = 0; i < t; i++)
if ((x[i] == 0) && (y[i] == 0))
cout << n << "\n";
else
cout << 0 << "\n";
} else {
a[0][0] = n % 4;
a[0][1] = n / 4;
rj = 2;
ch = true;
while (ch) {
ch = false;
for (j = 0; j < rj; j++)
for (i = 0; i <= j; i++)
if (a[i][j] > 3) {
rj = (j + 1 == rj) ? rj + 1 : rj;
p = a[i][j];
ch = true;
a[i][j + 1] += p / 4;
if (i == 1) a[0][j] += 2 * (p / 4);
if (i > 1) a[i - 1][j] += p / 4;
if (i == j - 1) {
a[i + 1][j] += 2 * (p / 4);
a[i][j - 1] += 2 * (p / 4);
if (i == 0) a[0][0] += 2 * (p / 4);
}
if (i < j - 1) {
a[i + 1][j] += p / 4;
a[i][j - 1] += p / 4;
}
a[i][j] = p % 4;
}
}
for (i = 0; i < t; i++)
if ((abs(x[i]) > 69) || (abs(y[i]) > 69))
cout << 0 << "\n";
else if (abs(x[i]) > abs(y[i]))
cout << a[abs(y[i])][abs(x[i])] << "\n";
else
cout << a[abs(x[i])][abs(y[i])] << "\n";
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxt = 5e4 + 100;
pair<long long, long long> a[maxt];
int x[1002][1002];
void f(int a, int b, int c) {
x[a][b] += c;
int em = x[a][b] / 4;
if (em <= 0) {
return;
}
x[a][b] %= 4;
f(a, b - 1, em);
f(a, b + 1, em);
f(a - 1, b, em);
f(a + 1, b, em);
}
int main() {
long long n, t;
cin >> n >> t;
int asl = 501;
for (int i = 0; i < t; i++) cin >> a[i].first >> a[i].second;
f(asl, asl, n);
for (int i = 0; i < t; i++) {
a[i].first += asl;
a[i].second += asl;
if (a[i].first < 0 || a[i].first >= asl * 2 || a[i].second < 0 ||
a[i].second >= asl * 2)
cout << 0 << endl;
else {
cout << x[a[i].first][a[i].second] << endl;
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int inf = INT_MAX;
int a[1000][1000];
void put(int x, int y) {
if (a[x][y] != 3) {
a[x][y]++;
return;
}
a[x][y] = 0;
put(x, y - 1);
put(x, y + 1);
put(x - 1, y);
put(x + 1, y);
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) put(500, 500);
while (q--) {
int x, y;
cin >> x >> y;
if (x < 0) x = -x;
if (y < 0) y = -y;
if (x > 400 || y > 400)
cout << 0 << "\n";
else
cout << a[500 + x][500 + y] << "\n";
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int S = 80;
int dp[2][2 * S + 5][2 * S + 5];
int ans[2 * S + 5][2 * S + 5];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, t;
int main() {
while (cin >> n >> t) {
memset(dp, 0, sizeof(dp));
int cur = 0;
dp[0][0 + S][0 + S] = n;
for (int i = 1; i <= 10000; i++) {
cur ^= 1;
memset(dp[cur], 0, sizeof(dp[cur]));
for (int x = 0; x <= 2 * S; x++)
for (int y = 0; y <= 2 * S; y++) {
int Move = dp[cur ^ 1][x][y] / 4;
if (Move) {
for (int j = 0; j < 4; j++) {
int xx = x + dx[j], yy = y + dy[j];
dp[cur][xx][yy] += Move;
}
}
dp[cur][x][y] += dp[cur ^ 1][x][y] % 4;
}
bool flag = false;
for (int x = 0; (!flag && x <= 2 * S); x++)
for (int y = 0; (!flag && y <= 2 * S); y++)
if (dp[cur][x][y] != dp[cur ^ 1][x][y]) flag = true;
if (!flag) break;
}
while (t--) {
int x, y;
scanf("%d%d", &x, &y);
x += S;
y += S;
if (x < 0 || x > 2 * S || y < 0 || y > 2 * S)
printf("0\n");
else
printf("%d\n", dp[cur][x][y]);
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using namespace std::chrono;
const int SZ = 65;
int cur[SZ][SZ], NESDF[SZ][SZ];
int get(int x, int y) {
if (x >= SZ or y >= SZ) return 0;
return cur[x][y];
}
void inc(int x, int y, int cnt, int y_fr) {
if (x < 0 or y < 0 or y > x) return;
if (x == y) cnt *= 2;
if (y == 0 and (x == 0 or y_fr != 0)) cnt *= 2;
NESDF[x][y] += cnt;
}
void init(int n) {
cur[0][0] = n;
int iter = 0;
bool change = true;
while (change) {
++iter;
change = false;
memset(NESDF, 0, sizeof(NESDF));
for (int i = 0; i < SZ; ++i)
for (int j = 0; j < SZ; ++j) {
auto p = make_pair(make_pair(i, j), cur[i][j]);
if (p.second >= 4) {
change = true;
int cnt = p.second / 4;
auto pos = p.first;
NESDF[i][j] += p.second % 4;
inc(pos.first - 1, pos.second, cnt, pos.second);
inc(pos.first + 1, pos.second, cnt, pos.second);
inc(pos.first, pos.second - 1, cnt, pos.second);
inc(pos.first, pos.second + 1, cnt, pos.second);
} else if (p.second > 0) {
NESDF[i][j] += p.second;
}
}
memcpy(cur, NESDF, sizeof(NESDF));
};
}
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
int n, t;
cin >> n >> t;
init(n);
for (int i = 0; i < t; i++) {
int x, y;
cin >> x >> y;
x = abs(x);
y = abs(y);
if (y > x) swap(x, y);
cout << get(x, y) << "\n";
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
int main() {
int i, j, n, x, y, temp, f, t;
scanf("%d%d", &n, &t);
int a[140][140];
for (i = 0; i < 140; i++) {
for (j = 0; j < 140; j++) {
a[i][j] = 0;
}
}
a[70][70] = n;
while (1) {
f = 0;
for (i = 0; i < 140; i++) {
for (j = 0; j < 140; j++) {
if (a[i][j] > 3) {
f = 1;
temp = a[i][j] / 4;
a[i - 1][j] += temp;
a[i][j + 1] += temp;
a[i + 1][j] += temp;
a[i][j - 1] += temp;
a[i][j] = a[i][j] % 4;
}
}
}
if (!f) break;
}
for (i = 0; i < t; i++) {
scanf("%d%d", &x, &y);
x += 70;
y += 70;
if (x >= 140 || x < 0 || y >= 140 || y < 0)
printf("0\n");
else
printf("%d\n", a[x][y]);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 1e5 + 100;
int t[4 * max_n];
int n;
int max_on_seg(int v, int tl, int tr, int l, int r) {
if (tl == l && tr == r) return t[v];
int m = (tl + tr) >> 1;
if (r <= m) return max_on_seg(2 * v, tl, m, l, r);
if (l > m) return max_on_seg(2 * v + 1, m + 1, tr, l, r);
return max(max_on_seg(2 * v, tl, m, l, m),
max_on_seg(2 * v + 1, m + 1, tr, m + 1, r));
}
void update(int v, int tl, int tr, int x, int d) {
if (tl == tr) {
t[v] = d;
} else {
int m = (tl + tr) >> 1;
if (x <= m)
update(2 * v, tl, m, x, d);
else
update(2 * v + 1, m + 1, tr, x, d);
t[v] = max(t[2 * v], t[2 * v + 1]);
}
}
int main() {
cin >> n;
int ans = 0;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
int d = max_on_seg(1, 1, n, 1, a) + 1;
ans = max(ans, d);
update(1, 1, n, a, d);
}
cout << ans << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool umax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
int arr[100009], s[100009 << 2], dp[100009];
void upd(int p, int v, int nd, int x, int y) {
if (x == y) {
s[nd] = v;
return;
}
int mid = (x + y) >> 1;
if (p <= mid)
upd(p, v, nd << 1, x, mid);
else
upd(p, v, nd << 1 | 1, mid + 1, y);
s[nd] = max(s[nd << 1], s[nd << 1 | 1]);
}
int tap(int l, int r, int nd, int x, int y) {
if (l > y or x > r) return 0;
if (l <= x and y <= r) return s[nd];
int mid = (x + y) >> 1;
int i1 = tap(l, r, nd << 1, x, mid);
int i2 = tap(l, r, nd << 1 | 1, mid + 1, y);
return max(i1, i2);
}
int main() {
int a;
scanf("%d", &a);
for (int i = 0; i < a; i++) scanf("%d", arr + i);
for (int i = a - 1; i >= 0; i--) {
dp[i] = tap(arr[i] + 1, a, 1, 1, a) + 1;
upd(arr[i], dp[i], 1, 1, a);
}
int mx = *max_element(dp, dp + a);
printf("%d\n", mx);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
const long long MAXLL = 0x0FFFFFFFFFFFFFFFll;
const int MAXINT = 0x0FFFFFFF;
const int MAXN = 100005;
int n;
int a[MAXN];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
set<int> st;
set<int>::iterator it;
st.clear();
for (int i = 0; i < n; i++) {
st.insert(a[i]);
it = st.find(a[i]);
it++;
if (it != st.end()) st.erase(it);
}
cout << st.size() << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int l[500010], r[500010], s[500010], a[500010], f[500010];
int n, i, z, ans;
int max(int a, int b) {
if (a > b) return a;
return b;
}
void build(int x, int a, int b) {
int m;
l[x] = a;
r[x] = b;
if (b - a > 1) {
m = (a + b) >> 1;
build(2 * x, a, m);
build(2 * x + 1, m, b);
}
}
void change(int x, int a, int b, int c) {
int m;
if (r[x] - l[x] == 1) {
s[x] = c;
return;
} else {
m = (l[x] + r[x]) >> 1;
if (a < m) change(2 * x, a, b, c);
if (m < b) change(2 * x + 1, a, b, c);
s[x] = max(s[2 * x], s[2 * x + 1]);
}
}
int query(int x, int a, int b) {
int m, ans;
if ((a <= l[x]) && (r[x] <= b)) return s[x];
m = (l[x] + r[x]) >> 1;
ans = 0;
if (a < m) ans = max(ans, query(2 * x, a, b));
if (m < b) ans = max(ans, query(2 * x + 1, a, b));
return ans;
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
build(1, 0, n);
for (i = 1; i <= n; i++) {
if (a[i] - 1)
z = query(1, 0, a[i] - 1);
else
z = 0;
f[i] = z + 1;
change(1, a[i] - 1, a[i], f[i]);
}
for (i = 1; i <= n; i++) ans = max(ans, f[i]);
printf("%d", ans);
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, l, idx;
vector<int> a(2, -1);
int main() {
cin >> n;
a.back() = 1000000010;
for (int i = 0; i < n; i++) {
cin >> m;
int left = 0, right = a.size() - 1, l;
while (true) {
l = (left + right) / 2;
if (a[l] <= m && m < a[l + 1]) {
a[l + 1] = m;
if (l + 2 == a.size()) a.push_back(1000000010);
break;
}
if (a[l] > m)
right = l;
else
left = l + 1;
}
}
cout << a.size() - 2;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[100010] = {0};
int b[100010] = {0};
int getpos(int lst[], int start, int end, int val) {
int mid = 0;
while (start < end - 1) {
mid = (start + end) >> 1;
if (lst[mid] >= val) {
end = mid - 1;
} else {
start = mid;
}
}
mid = (start + end) >> 1;
while (mid <= end && lst[mid] < val) {
mid++;
}
while (mid > end || lst[mid] >= val) {
mid--;
}
return mid + 1;
}
int main() {
int n = 0, k = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (a[i] > b[k]) {
k++;
b[k] = a[i];
} else {
int pos = getpos(b, 1, k, a[i]);
b[pos] = a[i];
}
}
cout << k << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100010];
int b[100010];
int main() {
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) b[i] = 10000000;
int ans = 0;
for (int i = 1; i <= n; i++) {
int l = 0, r = i - 1;
while (l < r) {
int mid = (l + r) / 2 + 1;
if (b[mid] < a[i])
l = mid;
else
r = mid - 1;
}
b[l + 1] = min(b[l + 1], a[i]);
ans = max(ans, l + 1);
}
printf("%d\n", ans);
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long a[100005], t[100005], n;
long long query(long long i) {
long long ans = 0;
while (i) {
ans = max(ans, t[i]);
i -= (i & -i);
}
return ans;
}
void update(long long i) {
long long val = query(i - 1) + 1;
while (i <= n) {
t[i] = max(t[i], val);
i += (i & -i);
}
}
int32_t main() {
long long i;
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 1; i <= n; i++) update(a[i]);
cout << query(n) << "\n";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
long long int prod(long long int a, long long int b) {
long long int x, d;
if (b == 0)
return 1;
else {
d = prod(a, b / 2);
x = (d * d) % 1000000007;
if (b % 2 == 0)
return x;
else
return (x * (a % 1000000007)) % 1000000007;
}
}
long long int CeilIndex(std::vector<long long int>& v, long long int l,
long long int r, long long int key) {
while (r - l > 1) {
long long int m = l + (r - l) / 2;
if (v[m] >= key)
r = m;
else
l = m;
}
return r;
}
long long int LongestIncreasingSubsequenceLength(
std::vector<long long int>& v) {
if (v.size() == 0) return 0;
std::vector<long long int> tail(v.size(), 0);
long long int length = 1;
tail[0] = v[0];
for (size_t i = 1; i < v.size(); i++) {
if (v[i] < tail[0])
tail[0] = v[i];
else if (v[i] > tail[length - 1])
tail[length++] = v[i];
else
tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
}
return length;
}
int main() {
long long int i, n, a;
vector<long long int> v;
scanf("%lld", &n);
for (i = 0; i < n; i++) {
scanf("%lld", &a);
v.push_back(a);
}
cout << LongestIncreasingSubsequenceLength(v) << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct dt {
int a, b, c;
};
dt dat[100005];
int bit[100005];
int ans, tmp;
int n;
bool cmp1(dt x, dt y) { return (x.a < y.a); }
bool cmp2(dt x, dt y) { return (x.b < y.b); }
inline int bitquery(int x) {
int res = 0;
for (; x > 0; x -= (x & (-x))) res = ((res) > (bit[x]) ? (res) : (bit[x]));
return res;
}
inline void bitupdate(int x, int y) {
for (; x < 100005; x += x & (-x)) bit[x] = ((y) > (bit[x]) ? (y) : (bit[x]));
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> dat[i].c;
dat[i].a = dat[i].c;
dat[i].b = i;
}
sort(dat, dat + n, cmp1);
for (int i = 0; i < n; ++i) {
if (i > 0 && dat[i - 1].c == dat[i].c)
dat[i].a = dat[i - 1].a;
else
dat[i].a = i + 2;
}
sort(dat, dat + n, cmp2);
ans = 0;
for (int i = 0; i < n; ++i) {
tmp = bitquery(dat[i].a - 1) + 1;
ans = ((ans) > (tmp) ? (ans) : (tmp));
bitupdate(dat[i].a, tmp);
}
cout << ans << '\n';
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
long long n, m, k, x, y, l, r, ans, a[N], L[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < (int)(n); ++i) cin >> a[i];
for (int i = 0; i < (int)(n); ++i) {
l = 1, r = ans;
while (l <= r) {
m = ceil((l + r) / 2);
if (a[L[m]] < a[i])
l = m + 1;
else
r = m - 1;
}
k = l;
L[k] = i;
if (k > ans) ans = k;
}
cout << ans << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9;
void nen_so(vector<int> &a) {
set<int> s(a.begin(), a.end());
vector<int> b(s.begin(), s.end());
for (int &x : a) x = lower_bound(b.begin(), b.end(), x) - b.begin() + 1;
}
int bit[N], n;
void update(int pos, int x) {
for (; pos <= n; pos += (pos & (-pos))) bit[pos] = max(bit[pos], x);
}
int get(int pos) {
int cnt = 0;
for (; pos > 0; pos -= (pos & (-pos))) cnt = max(cnt, bit[pos]);
return cnt;
}
int main() {
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
for (int x : a) update(x, get(x - 1) + 1);
cout << get(n);
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005], n;
int fin(int len, int p) {
int l, r, mid;
l = 1, r = len, mid = (l + r) >> 1;
while (l <= r) {
if (p > b[mid])
l = mid + 1;
else if (p < b[mid])
r = mid - 1;
else
return mid;
mid = (l + r) >> 1;
}
return l;
}
int LIS() {
int i, j, len = 1;
b[1] = a[0];
for (i = 1; i < n; i++) {
j = fin(len, a[i]);
b[j] = a[i];
if (j >= len) len = j;
}
return len;
}
int main() {
int i;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", a + i);
printf("%d", LIS());
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, BIT[N];
void update(int idx, int val) {
while (idx <= n) {
BIT[idx] = max(val, BIT[idx]);
idx += idx & -idx;
}
}
int get(int idx) {
int ret = 0;
while (idx > 0) {
ret = max(BIT[idx], ret);
idx -= idx & -idx;
}
return ret;
}
int main() {
scanf("%d", &n);
int ans = 0;
for (int i = 0, x; i < n; ++i) {
scanf("%d", &x);
update(x, get(x) + 1);
}
printf("%d\n", get(n));
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[200020], dp[200020], n, maxx = 0, m;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
dp[i] = 0;
}
for (int i = 0; i < n; i++) {
if (a[i] > dp[maxx]) {
maxx++;
dp[maxx] = a[i];
} else {
m = 1;
while (a[i] > dp[m]) {
m++;
}
if (dp[m] > a[i]) {
dp[m] = a[i];
}
}
}
cout << maxx;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int getCeil(int *arr, int s, int e, int x) {
while (s <= e) {
if (s == e) return s;
if (s + 1 == e) {
if (arr[s] > x)
return s;
else
return e;
}
int mid = (s + e) / 2;
if (arr[mid] > x && arr[mid - 1] <= x)
return mid;
else if (arr[mid] > x && arr[mid - 1] > x)
e = mid - 1;
else if (arr[mid] < x)
s = mid + 1;
}
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
int seq[n];
seq[0] = arr[0];
int curLen = 1;
for (int i = 1; i < n; i++) {
if (arr[i] < seq[0])
seq[0] = arr[i];
else if (arr[i] >= seq[curLen - 1])
seq[curLen++] = arr[i];
else
seq[getCeil(seq, 0, curLen - 1, arr[i])] = arr[i];
}
printf("%d\n", curLen);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const double eps = 1e-9;
const int maxN = 100 * 1024;
int N, K;
int a[maxN];
int lis[maxN];
int main() {
cin >> N;
for (int i = (1); i < (N + 1); i++) cin >> a[i];
for (int i = (1); i < (N + 2); i++) lis[i] = oo;
for (int i = (1); i < (N + 1); i++) {
int len = upper_bound(lis + 1, lis + N + 1, a[i]) - lis;
lis[len] = min(lis[len], a[i]);
}
int best = lower_bound(lis + 1, lis + N + 2, oo) - lis - 1;
cout << best << endl;
}
| 8 | CPP |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.