text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const int maxn = 300 + 21; const int maxl = 10; int n, m, adj[maxn][maxn], mat[maxl][maxn][maxn], idn[maxn][maxn], ans[maxn][maxn], tmp[maxn][maxn], cnt; void multi(int (&a)[maxn][maxn], int b[][maxn], int c[][maxn]) { memset(a, 63, sizeof a); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) a[i][j] = min(a[i][j], b[i][k] + c[k][j]); a[i][j] = min(a[i][j], min(b[i][j], c[i][j])); } } bool get(int a[][maxn]) { for (int i = 0; i < n; i++) if (a[i][i] < 0) return 1; return 0; } int main() { cin >> n >> m; memset(adj, 63, sizeof adj); for (int i = 0; i < m; i++) { int v, u, w1, w2; cin >> v >> u >> w1 >> w2; v--, u--, w1 = -w1, w2 = -w2; adj[v][u] = w1; adj[u][v] = w2; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mat[0][i][j] = adj[i][j]; memset(idn, 63, sizeof idn); for (int i = 0; i < n; i++) idn[i][i] = 0; multi(ans, idn, idn); for (int i = 1; i < maxl; i++) multi(mat[i], mat[i - 1], mat[i - 1]); for (int i = maxl - 1; i >= 0; i--) { multi(tmp, ans, mat[i]); if (!get(tmp)) { multi(ans, tmp, idn); cnt += (1 << i); } } if (cnt >= n) { cout << "0\n"; return 0; } cout << cnt + 1; }
#include <bits/stdc++.h> using namespace std; const int mod1 = 998244353, mod = 1e9 + 7; const int MAXN = 1e5 + 6, MAXM = 2e5 + 5; const int inf = 2e9; const long long linf = 1e18; const long long maxS = 303; long long n, m; struct Mat { long long a[maxS][maxS]; Mat() { for (long long i = 1; i <= 300; i++) { for (long long j = 1; j <= 300; j++) { a[i][j] = -inf; } } for (long long i = 1; i <= 300; i++) { a[i][i] = 0; } } long long* operator[](long long x) { return a[x]; } const long long* operator[](long long x) const { return a[x]; } friend Mat operator*(const Mat& a, const Mat& b) { Mat r; for (long long i = 1; i <= n; i++) { for (long long j = 1; j < n; j++) { for (long long k = 1; k <= n; k++) { r[i][j] = max(r[i][j], a[i][k] + b[k][j]); } } } return r; } bool check() { for (long long i = 1; i <= n; i++) { if (a[i][i] > 0) return 1; } return 0; } } ff[10]; void Solve() { cin >> n >> m; for (long long i = 1, u, v, w, t; i <= m; i++) { cin >> u >> v >> w >> t; ff[0][u][v] = w; ff[0][v][u] = t; } for (long long i = 1; i < 10; i++) ff[i] = ff[i - 1] * ff[i - 1]; if (!ff[9].check()) { cout << 0; return; } Mat cur; long long res = 0; for (long long i = 9; i >= 0; i--) { Mat nxt = cur * ff[i]; if (nxt.check()) { continue; } else { cur = nxt; res += (1 << i); } } res++; cout << res; } signed main() { if (fopen("" ".inp", "r")) { freopen( "" ".inp", "r", stdin); freopen( "" ".out", "w", stdout); }; std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long TC = 1; while (TC--) Solve(); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int N = 2e5 + 5, INF = 1e9; const bool debug = 0; int n, m, a, b, c1, c2; struct matrix { vector<vector<int> > M; int n, m; matrix() {} matrix(int nn, int mm) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); } matrix(int nn, int mm, int z) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) M[i][j] = z; } vector<int>& operator[](int x) { return M[x]; } const vector<int>& operator[](int x) const { return M[x]; } }; matrix operator*(const matrix& a, const matrix& b) { matrix c(a.n, b.m, -INF); for (int i = 0; i < a.n; ++i) for (int j = 0; j < b.m; ++j) for (int k = 0; k < a.m; ++k) if (a[i][k] > -INF && b[k][j] > -INF && c[i][j] < a[i][k] + b[k][j]) c[i][j] = a[i][k] + b[k][j]; return c; } bool h(const matrix& a) { for (int i = 0; i < a.n; ++i) if (a[i][i] > 0) return true; return false; } int main() { if (debug) freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; matrix g(n, n, -INF); for (int i = 0; i < n; ++i) g[i][i] = 0; for (int i = 0; i < m; ++i) { cin >> a >> b >> c1 >> c2; --a; --b; g[a][b] = max(g[a][b], c1); g[b][a] = max(g[b][a], c2); } int l = 1, r = n + 1; vector<matrix> stpd(10); stpd[0] = g; for (int i = 1; i < 10; ++i) stpd[i] = stpd[i - 1] * stpd[i - 1]; for (int k = 9; k >= 0; --k) { matrix pg = g * stpd[k]; if (!h(pg)) { l += (1 << k); g = pg; } } cout << (l + 1 > n ? 0 : l + 1); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int N = 2e5 + 5, INF = 1e9; const bool debug = 0; int n, m, a, b, c1, c2; struct matrix { vector<vector<int>> M; int n, m; matrix() { n = m = 0; } matrix(int nn, int mm) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); } matrix(int nn, int mm, int z) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) M[i][j] = z; } vector<int>& operator[](int x) { return M[x]; } }; matrix operator*(matrix& a, matrix& b) { matrix c(a.n, b.m, -INF); for (int i = 0; i < a.n; ++i) for (int j = 0; j < b.m; ++j) for (int k = 0; k < a.m; ++k) if (a[i][k] > -INF && b[k][j] > -INF && c[i][j] < a[i][k] + b[k][j]) c[i][j] = a[i][k] + b[k][j]; return c; } matrix operator^(matrix a, int b) { if (b == 1) { return a; } matrix res = a ^ (b / 2); res = res * res; if (b & 1) res = res * a; return res; } matrix edmtx(matrix& a) { matrix res(a.m, a.m, -INF); for (int i = 0; i < res.n; ++i) res[i][i] = 0; return res; } bool h(matrix& a) { for (int i = 0; i < a.n; ++i) if (a[i][i] > 0) return true; return false; } ostream& operator<<(ostream& out, matrix& a) { for (int i = 0; i < a.n; ++i, out << endl) for (int j = 0; j < a.m; ++j) out << a[i][j] << " "; return out; } int main() { if (debug) freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; matrix g(n, n, -INF); for (int i = 0; i < n; ++i) g[i][i] = 0; for (int i = 0; i < m; ++i) { cin >> a >> b >> c1 >> c2; --a; --b; g[a][b] = max(g[a][b], c1); g[b][a] = max(g[b][a], c2); } int l = 0, r = n + 1; vector<matrix> stpd(10); stpd[0] = g; for (int i = 1; i < 10; ++i) stpd[i] = stpd[i - 1] * stpd[i - 1]; g = edmtx(g); for (int k = 9; k >= 0; --k) { matrix pg = g * stpd[k]; if (!h(pg)) { g = pg; l += (1 << k); } } cout << (l + 1 > r ? 0 : l + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxl = 3 * 100 + 5; const int INF = 1000 * 1000 * 1000 + 1; int n, m; int dis[maxl][maxl], res[maxl][maxl], tmp[maxl][maxl]; int ans; void belman(int x) { if (x > 1) belman(x / 2); if (x == 1) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) res[i][j] = dis[i][j]; return; } memset(tmp, 63, sizeof tmp); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) tmp[i][j] = min(tmp[i][j], res[i][k] + res[k][j]); memset(res, 63, sizeof res); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if ((x & 1)) for (int k = 0; k < n; k++) res[i][j] = min(res[i][j], dis[i][k] + tmp[k][j]); else res[i][j] = tmp[i][j]; } bool chk() { for (int i = 0; i < n; i++) if (res[i][i] < 0) return true; return false; } int main() { cin >> n >> m; memset(dis, 63, sizeof dis); for (int i = 0; i < m; i++) { int a, b, w1, w2; cin >> a >> b >> w1 >> w2; a--; b--; dis[a][b] = -w1; dis[b][a] = -w2; } for (int i = 0; i < n; i++) dis[i][i] = 0; int first = 1, last = n + 1; while (last - first != 1) { int mid = (first + last) / 2; belman(mid); if (chk()) last = mid; else first = mid; } if (last == n + 1) ans = 0; else ans = last; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 305, oo = 1e18, LOG = 10; long long n, m, g[N][N]; struct Matrix { vector<vector<long long>> mat; Matrix() { mat.assign(n + 5, vector<long long>(n + 5, -oo)); for (long long i = 1; i <= n; ++i) mat[i][i] = 0; } } F[LOG]; Matrix operator+(const Matrix &a, const Matrix &b) { Matrix res; for (long long i = 1; i <= n; ++i) { for (long long j = 1; j <= n; ++j) { for (long long k = 1; k <= n; ++k) { res.mat[i][j] = max(res.mat[i][j], a.mat[i][k] + b.mat[k][j]); } } } return res; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (long long i = 1; i <= n; ++i) { for (long long j = 1; j <= n; ++j) g[i][j] = -oo; } for (long long i = 0; i < LOG; ++i) F[i] = Matrix(); for (long long i = 1; i <= m; ++i) { long long u, v; cin >> u >> v; cin >> g[u][v] >> g[v][u]; F[0].mat[u][v] = g[u][v]; F[0].mat[v][u] = g[v][u]; } for (long long i = 1; i <= 9; ++i) F[i] = F[i - 1] + F[i - 1]; long long res = 0; Matrix cur; for (long long i = 9; i >= 0; --i) { Matrix tmp = cur + F[i]; bool f = false; for (long long j = 1; j <= n; ++j) if (tmp.mat[j][j] > 0) f = true; if (!f) { if (i == 9) { cout << 0 << endl; return 0; } cur = tmp, res += (1 << i); } } cout << res + 1 << endl; return 0; };
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int N = 2e5 + 5, INF = 1e9; const bool debug = 0; int n, m, a, b, c1, c2; struct matrix { vector<vector<int> > M; int n, m; matrix() {} matrix(int nn, int mm) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); } matrix(int nn, int mm, int z) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) M[i][j] = z; } vector<int>& operator[](int x) { return M[x]; } const vector<int>& operator[](int x) const { return M[x]; } }; matrix operator*(const matrix& a, const matrix& b) { matrix c(a.n, b.m, -INF); for (int i = 0; i < a.n; ++i) for (int j = 0; j < b.m; ++j) for (int k = 0; k < a.m; ++k) if (a[i][k] > -INF && b[k][j] > -INF && c[i][j] < a[i][k] + b[k][j]) c[i][j] = a[i][k] + b[k][j]; return c; } bool h(const matrix& a) { for (int i = 0; i < a.n; ++i) if (a[i][i] > 0) return true; return false; } int main() { if (debug) freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; matrix g(n, n, -INF); for (int i = 0; i < n; ++i) g[i][i] = 0; for (int i = 0; i < m; ++i) { cin >> a >> b >> c1 >> c2; --a; --b; g[a][b] = max(g[a][b], c1); g[b][a] = max(g[b][a], c2); } int l = 1, r = n + 1; vector<matrix> stpd(10); stpd[0] = g; for (int i = 1; i < 10; ++i) stpd[i] = stpd[i - 1] * stpd[i - 1]; for (int k = 9; k >= 0; --k) { matrix pg = g * stpd[k]; if (!h(pg)) { l += (1 << k); g = pg; } } cout << (l + 1 > n ? 0 : l + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; int n, m; int f[10][320][320] = {0}; int g[2][320][320] = {0}; int old = 0, now = 1; bool HasCircle(int b) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { g[now][i][j] = -INF; for (int k = 1; k <= n; k++) { if (g[old][i][k] + f[b][k][j] > g[now][i][j]) { g[now][i][j] = g[old][i][k] + f[b][k][j]; } } } if (g[now][i][i] > 0) { return true; } } old ^= 1; now ^= 1; return false; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { f[0][i][j] = -INF; } } for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; cin >> f[0][a][b] >> f[0][b][a]; } for (int i = 1; i <= n; i++) { f[0][i][i] = 0; } int FL = 0; for (int len = 1; len <= 10; len++) { if ((1 << (len - 1)) > n) { bool flag = false; for (int i = 1; i <= n; i++) { if (f[len - 1][i][i] > 0) { flag = true; } } if (!flag) { cout << 0 << endl; return 0; } FL = len - 1; break; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { f[len][i][j] = -INF; for (int k = 1; k <= n; k++) { if (f[len - 1][i][k] + f[len - 1][k][j] > f[len][i][j]) { f[len][i][j] = f[len - 1][i][k] + f[len - 1][k][j]; } } } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { g[old][i][j] = -INF; } } for (int i = 1; i <= n; i++) { g[old][i][i] = 0; } int ans = 0; for (int len = FL - 1; len >= 0; len--) { if (!HasCircle(len)) { ans = ans + (1 << len); } } cout << ans + 1 << endl; return 0; }
#include <bits/stdc++.h> inline int read() { int data = 0, w = 1; char ch = getchar(); while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar(); return data * w; } const int N(305), T(10); int n, m, ans; struct Matrix { int a[N][N]; inline int *operator[](const int &x) { return a[x]; } inline const int *operator[](const int &x) const { return a[x]; } } A[T]; inline void Init(Matrix &x) { memset(x.a, 0xc0, sizeof x.a); } Matrix operator*(const Matrix &lhs, const Matrix &rhs) { Matrix ans; Init(ans); for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans[i][j] = std::max(ans[i][j], lhs[i][k] + rhs[k][j]); return ans; } int main() { n = read(), m = read(); Matrix X, Y; Init(X), Init(Y), Init(A[0]); for (int i = 1; i <= n; i++) X[i][i] = A[0][i][i] = 0; for (int i = 1, a, b; i <= m; i++) a = read(), b = read(), A[0][a][b] = read(), A[0][b][a] = read(); for (int i = 1; i < T; i++) A[i] = A[i - 1] * A[i - 1]; for (int i = T - 1; ~i; i--) { Y = X * A[i]; int fl = 0; for (int j = 1; j <= n; j++) if (Y[j][j] > 0) { fl = 1; break; } if (fl) continue; else ans += (1 << i), X = Y; } printf("%d\n", ans > n ? 0 : ans + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 305; int c[MAXN][MAXN]; int n, m; int mar[15][MAXN][MAXN]; int now[MAXN][MAXN]; int tmp[MAXN][MAXN]; int main() { int m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) c[i][j] = -100000000; for (int i = 1; i <= n; i++) c[i][i] = 0; while (m--) { int p, q; scanf("%d%d", &p, &q); scanf("%d%d", &c[p][q], &c[q][p]); } memset(mar, -20, sizeof(mar)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) mar[0][i][j] = c[i][j]; for (int t = 1; t <= 10; t++) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) if (mar[t - 1][i][j] + mar[t - 1][j][k] > mar[t][i][k]) mar[t][i][k] = mar[t - 1][i][j] + mar[t - 1][j][k]; } bool flag = false; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (mar[10][i][i] > 0) flag = true; if (!flag) { printf("0\n"); return 0; } memset(now, -20, sizeof(now)); int ans = 0; for (int i = 1; i <= n; i++) now[i][i] = 0; for (int t = 10; t >= 0; t--) { bool flag = false; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (mar[t][i][j] + now[j][i] > 0) flag = true; if (flag) continue; ans += (1 << t); memset(tmp, -20, sizeof(tmp)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) if (mar[t][i][j] + now[j][k] > tmp[i][k]) tmp[i][k] = mar[t][i][j] + now[j][k]; memcpy(now, tmp, sizeof(now)); } printf("%d\n", ans + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; int n, m, ans = 1; inline int mymax(const int& a, const int& b) { return a > b ? a : b; } inline int getlog(int _) { int ret = 0; while (_) { ret++; _ >>= 1; } return ret - 1; } struct mat { int num[333][333]; inline mat times(const mat& b) { mat res; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { res.num[i][j] = i == j ? 0 : -INF; for (int k = 0; k < n; k++) if (num[i][k] != -INF && b.num[k][j] != -INF) res.num[i][j] = mymax(res.num[i][j], num[i][k] + b.num[k][j]); } return res; } } g, f[10], t; void init() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g.num[i][j] = i == j ? 0 : -INF; while (m--) { int i, j, x, y; scanf("%d%d%d%d", &i, &j, &x, &y); i--; j--; g.num[i][j] = x; g.num[j][i] = y; } f[0] = g; for (int i = 1; (1 << i) <= n; i++) f[i] = f[i - 1].times(f[i - 1]); return; } inline bool check(const mat& _) { for (int i = 0; i < n; i++) if (_.num[i][i] > 0) return 1; return 0; } void slove() { t = g; for (int i = getlog(n); i >= 0; i--) { mat T; T = t.times(f[i]); if (!check(T)) { t = T; ans += 1 << i; } } t = t.times(g); printf("%d\n", check(t) ? ans + 1 : 0); return; } int main() { init(); slove(); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, res = 0x3f3f3f3f, f[30][305][305], g[305][305], h[305][305]; long long dis[305]; bool vis[305]; int mmax(int x, int y) { return x > y ? x : y; } int main() { scanf("%d%d", &n, &m); int x, y, z, zz; memset(f, -0x3f, sizeof(f)); memset(g, -0x3f, sizeof(g)); for (int i = 1; i <= m; i++) scanf("%d%d%d%d", &x, &y, &z, &zz), f[0][x][y] = z, f[0][y][x] = zz; for (int i = 1; i <= n; i++) f[0][i][i] = 0, g[i][i] = 0; int len = 0; while ((1 << len + 1) <= n) len++; for (int i = 1; (1 << i) <= n; i++) { for (int k = 1; k <= n; k++) for (int x = 1; x <= n; x++) for (int y = 1; y <= n; y++) f[i][x][y] = mmax(f[i][x][y], f[i - 1][x][k] + f[i - 1][k][y]); } bool flag = 0; int ttlen = 0; for (int i = len; i >= 0; i--) { for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) h[j][k] = g[j][k]; for (int j = 1; j <= n; j++) for (int l = 1; l <= n; l++) for (int k = 1; k <= n; k++) g[j][k] = mmax(g[j][k], h[j][l] + f[i][l][k]); bool find = 0; for (int j = 1; j <= n; j++) if (g[j][j] > 0) { find = 1; break; } if (find) { flag = 1; for (int j = 1; j <= n; j++) for (int l = 1; l <= n; l++) g[j][l] = h[j][l]; } else ttlen += 1 << i; } if (!flag) return 0 * puts("0"); printf("%d\n", ttlen + 1); }
#include <bits/stdc++.h> const int MAXN = 305; const int INF = -1000000000; int n, m; int map[MAXN][MAXN]; int a[MAXN][MAXN]; int b[MAXN][MAXN]; int d[MAXN][MAXN]; void mul(int a[][MAXN], int b[][MAXN]) { int i, j, k; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) d[i][j] = INF; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) for (k = 1; k <= n; k++) if (d[i][j] < a[i][k] + b[k][j]) d[i][j] = a[i][k] + b[k][j]; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) a[i][j] = d[i][j]; } bool check(int m) { int i, j; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) b[i][j] = map[i][j], a[i][j] = map[i][j]; m--; while (m) { if (m & 1) mul(b, a); mul(a, a); m >>= 1; } for (i = 1; i <= n; i++) if (b[i][i] > 0) return true; return false; } int main() { int i, j; scanf("%d%d", &n, &m); int x, y, c, cc; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) map[i][j] = INF; for (i = 1; i <= n; i++) map[i][i] = 0; for (i = 0; i < m; i++) { scanf("%d%d%d%d", &x, &y, &c, &cc); map[x][y] = c; map[y][x] = cc; } int l = 2, r = n, best = 0, mid; while (l <= r) { mid = (l + r) >> 1; if (check(mid)) { best = mid; r = mid - 1; } else l = mid + 1; } printf("%d\n", best); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 300 + 7, INF = 1e9; int n, m; int d[10][maxn][maxn]; int t[maxn][maxn]; int nd[maxn][maxn]; void work(int c[][maxn], int a[][maxn], int b[][maxn]) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { c[i][j] = -INF; } } for (int k = 1; k <= n; ++k) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { c[i][j] = max(c[i][j], a[i][k] + b[k][j]); } } } } bool is_ok(int a[][maxn]) { for (int i = 1; i <= n; ++i) { if (a[i][i] > 0) return true; } return false; } void print(int t[][maxn]) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cout << t[i][j] << ' '; } cout << endl; } cout << endl << endl; } int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (i == j) d[0][i][j] = 0; else d[0][i][j] = -INF; } } for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; cin >> d[0][u][v] >> d[0][v][u]; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { t[i][j] = d[0][i][j]; } } for (int i = 0; i < 9; ++i) { work(d[i + 1], d[i], d[i]); } if (!is_ok(d[9])) { cout << 0 << endl; return 0; } int ans = 1; for (int i = 8; i >= 0; --i) { work(nd, d[i], t); if (!is_ok(nd)) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { t[i][j] = nd[i][j]; } } ans += (1 << i); } } cout << ans + 1 << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; int rd() { int f = 1, val = 0; char ch = getchar(); while (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = getchar(); while (isdigit(ch)) val = val * 10 + ch - '0', ch = getchar(); return f * val; } const int N = 305; int f[11][N][N], now[N][N], ne[N][N]; signed main() { memset(f, 0xcf, sizeof f); memset(now, 0xcf, sizeof now); int n = rd(), m = rd(); for (int i = 1; i <= m; i++) { int u = rd(), v = rd(), w1 = rd(), w2 = rd(); f[0][u][v] = w1; f[0][v][u] = w2; } for (int i = 1; i <= n; i++) now[i][i] = f[0][i][i] = 0; for (int t = 1; t <= 10; t++) { for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { f[t][i][j] = max(f[t][i][j], f[t - 1][i][k] + f[t - 1][k][j]); } } } } int ans = 0; for (int t = 10; ~t; t--) { memset(ne, 0xcf, sizeof ne); for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { ne[i][j] = max(ne[i][j], now[i][k] + f[t][k][j]); } } } bool f = 0; for (int i = 1; i <= n; i++) { if (ne[i][i] > 0) f = 1; } if (f) continue; ans += (1 << t); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { now[i][j] = ne[i][j]; } } if (ans > n) return puts("0"), 0; } cout << ans + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; long long f[9][305][305]; long long g[305][305], t[305][305]; int main() { memset(f, 0xc0, sizeof f); memset(g, 0xc0, sizeof g); scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) f[0][i][i] = g[i][i] = 0; for (int i = 1; i <= m; ++i) { int x, y; long long z, zz; scanf("%d%d%lld%lld", &x, &y, &z, &zz); if (z + zz > 0) return puts("2"), 0; f[0][x][y] = z; f[0][y][x] = zz; } for (int i = 1; (1 << i) <= n; ++i) for (int u = 1; u <= n; ++u) for (int v = 1; v <= n; ++v) for (int j = 1; j <= n; ++j) f[i][u][v] = max(f[i][u][v], f[i - 1][u][j] + f[i - 1][j][v]); int num = 0; for (int i = 31 - __builtin_clz(n); ~i; --i) { int ok = 0; for (int u = 1; u <= n; ++u) { for (int v = 1; v <= n; ++v) if (g[u][v] + f[i][v][u] > 0) { ok = 1; break; } if (ok) break; } if (!ok) { memset(t, 0xc0, sizeof t); num += (1 << i); for (int u = 1; u <= n; ++u) for (int v = 1; v <= n; ++v) for (int j = 1; j <= n; ++j) t[u][v] = max(t[u][v], g[u][j] + f[i][j][v]); for (int u = 1; u <= n; ++u) for (int v = 1; v <= n; ++v) g[u][v] = t[u][v]; } } printf("%d", num < n ? num + 1 : 0); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 300, MAXK = 10; int n, m, x, y, z, w, Ans; int f[MAXK + 5][MAXN + 5][MAXN + 5]; int tmp[MAXN + 5][MAXN + 5], now[MAXN + 5][MAXN + 5]; void Init() { memset(now, 0xcf, sizeof(now)); memset(f, 0xcf, sizeof(f)); } int main() { scanf("%d %d", &n, &m); Init(); for (int i = 1; i <= m; i++) { scanf("%d %d %d %d", &x, &y, &z, &w); f[0][x][y] = z; f[0][y][x] = w; } for (int i = 1; i <= n; i++) now[i][i] = 0, f[0][i][i] = 0; for (int s = 1; s <= MAXK; s++) for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) f[s][i][j] = max(f[s][i][j], f[s - 1][i][k] + f[s - 1][k][j]); for (int s = MAXK; s >= 0; s--) { memset(tmp, 0xcf, sizeof(tmp)); for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) tmp[i][j] = max(tmp[i][j], now[i][k] + f[s][k][j]); bool f = 0; for (int i = 1; i <= n; i++) if (tmp[i][i] > 0) { f = 1; break; } if (f) continue; else { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) now[i][j] = tmp[i][j]; Ans += 1 << s; } } printf("%d\n", Ans > n ? 0 : Ans + 1); return 0; }
#include <bits/stdc++.h> using namespace std; int n, y, m, x, cnt[10], d; vector<vector<int> > a(301, vector<int>(301)); vector<vector<int> > mul(vector<vector<int> > a, vector<vector<int> > b) { vector<vector<int> > c(n + 1, vector<int>(n + 1)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) c[i][j] = -1e9; for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) c[i][j] = max(c[i][j], a[i][k] + b[k][j]); return c; } vector<vector<int> > pow(vector<vector<int> > a, int p) { if (p == 1) return a; if (p % 2) return mul(a, pow(a, p - 1)); vector<vector<int> > X = pow(a, p / 2); return mul(X, X); } bool check(int mo) { vector<vector<int> > d = pow(a, mo); for (int i = 1; i <= n; i++) if (d[i][i] > 0) return true; return false; } int bs(int l, int r) { int ans = 0; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) ans = mid, r = mid - 1; else l = mid + 1; } return ans; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) a[i][j] = -1e9; for (int i = 1; i <= n; i++) a[i][i] = 0; for (int i = 0; i < m; i++) { int w, c; scanf("%d%d%d%d", &x, &y, &w, &c); a[x][y] = w; a[y][x] = c; } printf("%d\n", bs(2, n)); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, f[35][305][305], dp[35][305][305], maxk, ans; void prepare() { for (int i = 1; i <= maxk; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) for (int l = 1; l <= n; l++) dp[i][k][l] = max(dp[i][k][l], dp[i - 1][k][j] + dp[i - 1][j][l]); } bool check(int x) { int now = 0; for (int i = 0; i <= maxk; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) f[i][j][k] = -0x3f3f3f3f; for (int i = 1; i <= n; i++) f[0][i][i] = 0; for (int i = 0; i <= maxk; i++) { if (!((x >> i) & 1)) continue; now++; for (int j = 1; j <= n; j++) f[now][j][j] = 0; for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) for (int l = 1; l <= n; l++) f[now][k][l] = max(f[now][k][l], f[now - 1][k][j] + dp[i][j][l]); } for (int i = 1; i <= n; i++) if (f[now][i][i] > 0) return 1; return 0; } int main() { int x, y, z, w; scanf("%d %d", &n, &m); maxk = log2(n); for (int l = 0; l <= maxk; l++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dp[l][i][j] = -0x3f3f3f3f; for (int l = 0; l <= maxk; l++) for (int i = 1; i <= n; i++) dp[l][i][i] = 0; for (int i = 1; i <= m; i++) { scanf("%d %d %d %d", &x, &y, &z, &w); dp[0][x][y] = z; dp[0][y][x] = w; } prepare(); ans = 0x3f3f3f3f; int l = 2, r = n + 1, mid; while (l <= r) { mid = (l + r) / 2; if (check(mid)) { r = mid - 1; ans = min(ans, mid); } else l = mid + 1; } if (l >= n + 1) printf("0\n"); else printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, INF = 1e9; const bool debug = 0; int n, m, a, b, c1, c2; struct matrix { vector<vector<int> > M; int n, m; matrix() {} matrix(int nn, int mm) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); } matrix(int nn, int mm, int z) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) M[i][j] = z; } vector<int>& operator[](int x) { return M[x]; } }; matrix operator*(matrix& a, matrix& b) { matrix c(a.n, b.m, -INF); for (int i = 0; i < a.n; ++i) for (int j = 0; j < b.m; ++j) for (int k = 0; k < a.m; ++k) if (a[i][k] > -INF && b[k][j] > -INF && c[i][j] < a[i][k] + b[k][j]) c[i][j] = a[i][k] + b[k][j]; return c; } matrix& operator*=(matrix& a, matrix& b) { a = a * b; return a; } bool h(matrix& a) { for (int i = 0; i < a.n; ++i) if (a[i][i] > 0) return true; return false; } ostream& operator<<(ostream& out, matrix& a) { for (int i = 0; i < a.n; ++i, out << endl) for (int j = 0; j < a.m; ++j) out << a[i][j] << " "; return out; } int main() { if (debug) freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; matrix g(n, n, -INF); for (int i = 0; i < n; ++i) g[i][i] = 0; for (int i = 0; i < m; ++i) { cin >> a >> b >> c1 >> c2; --a; --b; g[a][b] = max(g[a][b], c1); g[b][a] = max(g[b][a], c2); } int l = 1, r = n + 1; vector<matrix> stpd(10); stpd[0] = g; for (int i = 1; i < 10; ++i) stpd[i] = stpd[i - 1] * stpd[i - 1]; for (int k = 9; k >= 0; --k) { matrix pg = g * stpd[k]; if (!h(pg)) { l += (1 << k); g = pg; } } cout << (l + 1 > n ? 0 : l + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 320; int g[10][320][320], f[320][320], s[320][320]; int n, m; int main() { int x, y, z, w; scanf("%d%d", &n, &m); for (int i = 0; i <= 9; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) g[i][j][k] = -1e9; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) f[i][j] = -1e9; for (int i = 1; i <= n; i++) f[i][i] = g[0][i][i] = 0; for (int i = 1; i <= m; i++) scanf("%d%d%d%d", &x, &y, &z, &w), g[0][x][y] = z, g[0][y][x] = w; for (int l = 1; l <= 9; l++) for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) g[l][i][j] = max(g[l][i][j], g[l - 1][i][k] + g[l - 1][k][j]); int ans = 0; for (int l = 9; l >= 0; l--) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) s[i][j] = f[i][j]; for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) s[i][j] = max(f[i][k] + g[l][k][j], s[i][j]); bool flag = false; for (int i = 1; i <= n; i++) if (s[i][i] > 0) { flag = true; break; } if (flag) continue; ans += 1 << l; if (ans > n) { puts("0"); return 0; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) f[i][j] = s[i][j]; } printf("%d", ans + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = (1 << 29); inline int input() { int ret = 0; char c; bool neg = false; while (true) { c = getchar(); if (c == '-') { neg = true; continue; } if (c == ' ' || c == '\n') break; ret *= 10; ret += c - '0'; } return ret * (neg ? -1 : 1); } inline void resize(vector<vector<int> > &M, int n, int m) { M.resize(n); for (int i = 0; i < n; i++) M[i].resize(m); } inline vector<vector<int> > distanceProduct(vector<vector<int> > &a, vector<vector<int> > &b) { vector<vector<int> > ret; resize(ret, a.size(), a.size()); for (int i = 0; i < a.size(); i++) { for (int j = 0; j < a.size(); j++) { int mx = max(a[i][j], b[i][j]); for (int k = 0; k < a.size(); k++) mx = max(mx, a[i][k] + b[k][j]); ret[i][j] = mx; } } return ret; } inline vector<vector<int> > pow(vector<vector<int> > &x, int y) { if (y == 1) return x; vector<vector<int> > z = pow(x, y / 2); vector<vector<int> > ret = distanceProduct(z, z); if (y % 2) ret = distanceProduct(ret, x); return ret; } int n, m; vector<vector<int> > adj; inline bool can(vector<vector<int> > &a) { for (int i = 0; i < n; i++) if (a[i][i] > 0) return true; return false; } vector<vector<int> > ar[10]; int main() { n = input(); m = input(); resize(adj, n, n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) adj[i][j] = -inf; while (m--) { int i, j, a, b; i = input(); j = input(); a = input(); b = input(); i--, j--; adj[i][j] = a; adj[j][i] = b; } ar[0] = adj; for (int i = 1; i < 10; i++) ar[i] = distanceProduct(ar[i - 1], ar[i - 1]); vector<vector<int> > cur; resize(cur, n, n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cur[i][j] = -inf; int ans = 1; for (int i = 9; i >= 0; i--) { vector<vector<int> > prod = distanceProduct(cur, ar[i]); bool x = can(prod); if (!x) { ans += 1 << i; cur = prod; } } if (ans > n) puts("0"); else printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; const int MAXN = 305; const int INF = 0x3f3f3f3f; int n, m; int d[10][MAXN][MAXN], a[MAXN][MAXN], b[MAXN][MAXN]; void floyd(int a[MAXN][MAXN], int b[MAXN][MAXN], int c[MAXN][MAXN]) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) a[i][j] = INF; for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) a[i][j] = min(a[i][j], b[i][k] + c[k][j]); } bool isOk(int a[MAXN][MAXN]) { for (int i = 1; i <= n; i++) if (a[i][i] < 0) return true; return false; } int main() { scanf("%d%d", &n, &m); memset(d[0], INF, sizeof(d[0])); for (int i = 1; i <= n; i++) d[0][i][i] = 0; for (int i = 0; i < m; i++) { int u, v, a, b; scanf("%d%d%d%d", &u, &v, &a, &b); d[0][u][v] = -a; d[0][v][u] = -b; } int dig = 0; do { dig++; floyd(d[dig], d[dig - 1], d[dig - 1]); } while ((1 << dig) <= n); if (!isOk(d[dig])) printf("0\n"); else { int ans = 0; memset(a, INF, sizeof(a)); for (int i = 1; i <= n; i++) a[i][i] = 0; while (dig >= 0) { floyd(b, d[dig], a); if (!isOk(b)) { memcpy(a, b, sizeof(b)); ans += (1 << dig); } dig--; } printf("%d\n", ans + 1); } return 0; }
#include <bits/stdc++.h> using namespace std; const int sz = 301; int n, m; int A[sz][sz]; void copy(int C[sz][sz], int D[sz][sz]) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) D[i][j] = C[i][j]; } void vur(int C[sz][sz], int A[sz][sz], int B[sz][sz]) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { C[i][j] = 1000000000; for (int k = 1; k <= n; k++) C[i][j] = min(C[i][j], A[i][k] + B[k][j]); } } void calc(int M[sz][sz], int k) { int temp[sz][sz]; if (k == 1) { copy(A, M); } else if (k % 2) { calc(temp, k - 1); vur(M, temp, A); } else { calc(temp, k / 2); vur(M, temp, temp); } } bool f(int k) { int M[sz][sz]; calc(M, k); for (int i = 1; i <= n; i++) if (M[i][i] < 0) return true; return false; } int main() { int a, b, l, r, mid; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) A[i][j] = 1000000000; A[i][i] = 0; } for (int i = 1; i <= m; i++) { scanf("%d%d", &a, &b); scanf("%d%d", &A[a][b], &A[b][a]); A[a][b] *= (-1); A[b][a] *= (-1); } l = 1; r = n; while (l <= r) { mid = (l + r) / 2; if (f(mid)) r = mid - 1; else l = mid + 1; } int res = r + 1; if (res > n) res = 0; printf("%d\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline T1 max(T1 a, T2 b) { return a < b ? b : a; } template <typename T1, typename T2> inline T1 min(T1 a, T2 b) { return a < b ? a : b; } const char lf = '\n'; namespace ae86 { const int bufl = 1 << 15; char buf[bufl], *s = buf, *t = buf; inline int fetch() { if (s == t) { t = (s = buf) + fread(buf, 1, bufl, stdin); if (s == t) return EOF; } return *s++; } inline int ty() { int a = 0; int b = 1, c = fetch(); while (!isdigit(c)) b ^= c == '-', c = fetch(); while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); return b ? a : -a; } } // namespace ae86 using ae86::ty; const int _ = 307, lglg = 9; const long long linf = 0x3f3f3f3f3f3f3f3f; int n, m; long long xdis[lglg + 1][_][_] = {0}; void make(long long z[_][_], long long a[_][_], long long b[_][_]) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { long long temp = -linf; for (int k = 1; k <= n; k++) temp = max(temp, a[i][k] + b[k][j]); z[i][j] = temp; } } long long now[_][_], temp[_][_]; int main() { ios::sync_with_stdio(0), cout.tie(nullptr); n = ty(), m = ty(); memset(xdis, -64, sizeof(xdis)); for (int i = 1; i <= n; i++) xdis[0][i][i] = 0; for (int i = 1, a, b; i <= m; i++) a = ty(), b = ty(), xdis[0][a][b] = ty(), xdis[0][b][a] = ty(); for (int i = 1; i <= lglg; i++) make(xdis[i], xdis[i - 1], xdis[i - 1]); memset(now, -64, sizeof(now)); for (int i = 1; i <= n; i++) now[i][i] = 0; long long ans = 0; for (int i = lglg; i >= 0; i--) { make(temp, now, xdis[i]); int got = 0; for (int j = 1; j <= n; j++) if (temp[j][j] > 0) { got = j; break; } if (!got) ans += 1 << i, memcpy(now, temp, sizeof(now)); } if (ans > n) ans = -1; cout << ans + 1 << lf; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e8; int mtx[10][300][300]; int cur[2][300][300]; int n, m, ans; void mult(int a[300][300], int b[300][300], int c[300][300]) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { c[i][j] = -INF; } c[i][i] = 0; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { c[i][j] = max(c[i][j], a[i][k] + b[k][j]); } } } } void init() { ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { mtx[0][i][j] = -INF; cur[0][i][j] = -INF; } mtx[0][i][i] = 0; cur[0][i][i] = 0; } } void solve() { scanf("%d %d", &n, &m); init(); for (int i = 1, l, r; i <= m; ++i) { scanf("%d %d", &l, &r); --l, --r; scanf("%d %d", &mtx[0][l][r], &mtx[0][r][l]); } for (int i = 1; i < 10; ++i) { mult(mtx[i - 1], mtx[i - 1], mtx[i]); } int now = 0; for (int pw = 9; pw >= 0; --pw) { bool ok = 1; mult(cur[now], mtx[pw], cur[now ^ 1]); now ^= 1; for (int i = 0; i < n; ++i) { if (cur[now][i][i] > 0) { ok = 0; } } if (ok) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cur[now ^ 1][i][j] = cur[now][i][j]; } } ans += (1 << pw); } now ^= 1; } if (++ans > n) { ans = 0; } printf("%d\n", ans); } int main() { int tt = 1; while (tt--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, INF = 1e9; const bool debug = 0; int n, m, a, b, c1, c2; struct matrix { vector<vector<int> > M; int n, m; matrix() {} matrix(int nn, int mm) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); } matrix(int nn, int mm, int z) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) M[i][j] = z; } vector<int>& operator[](int x) { return M[x]; } const vector<int>& operator[](int x) const { return M[x]; } }; matrix operator*(const matrix& a, const matrix& b) { matrix c(a.n, b.m, -INF); for (int i = 0; i < a.n; ++i) for (int j = 0; j < b.m; ++j) for (int k = 0; k < a.m; ++k) if (a[i][k] > -INF && b[k][j] > -INF && c[i][j] < a[i][k] + b[k][j]) c[i][j] = a[i][k] + b[k][j]; return c; } bool h(const matrix& a) { for (int i = 0; i < a.n; ++i) if (a[i][i] > 0) return true; return false; } int main() { if (debug) freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; matrix g(n, n, -INF); for (int i = 0; i < n; ++i) g[i][i] = 0; for (int i = 0; i < m; ++i) { cin >> a >> b >> c1 >> c2; --a; --b; g[a][b] = max(g[a][b], c1); g[b][a] = max(g[b][a], c2); } int l = 1, r = n + 1; vector<matrix> stpd(10); stpd[0] = g; for (int i = 1; i < 10; ++i) stpd[i] = stpd[i - 1] * stpd[i - 1]; for (int k = 9; k >= 0; --k) { matrix pg = g * stpd[k]; if (!h(pg)) { l += (1 << k); g = pg; } } cout << (l + 1 > n ? 0 : l + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 305; long long dist[10][N][N], I[2][N][N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (i != j) dist[0][i][j] = INT_MIN; I[0][i][j] = dist[0][i][j]; } for (int i = 1; i <= m; i++) { int x, y, c1, c2; cin >> x >> y >> c1 >> c2; dist[0][x][y] = c1; dist[0][y][x] = c2; } for (int c = 1; c < 10; c++) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dist[c][i][j] = dist[c - 1][i][j]; for (int i = 1; i <= n; i++) for (int k = 1; k <= n; k++) for (int j = 1; j <= n; j++) dist[c][i][j] = max(dist[c][i][j], dist[c - 1][i][k] + dist[c - 1][k][j]); } bool ok = 0; for (int i = 1; i <= n; i++) if (dist[9][i][i] > 0) { ok = 1; break; } if (!ok) { cout << 0; return 0; } int ans = 1; for (int bit = 9; bit >= 0; bit--) { ok = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) I[1][i][j] = I[0][i][j]; for (int i = 1; i <= n; i++) for (int k = 1; k <= n; k++) for (int j = 1; j <= n; j++) I[1][i][j] = max(I[1][i][j], I[0][i][k] + dist[bit][k][j]); for (int i = 1; i <= n; i++) if (I[1][i][i] > 0) { ok = 1; break; } if (!ok) { ans += (1 << bit); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) I[0][i][j] = I[1][i][j]; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int n; struct Graph { int a[300][300]; int *operator[](int x) { return a[x]; } void operator*=(Graph &b) { static Graph temp; int i, j, k, *p; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { p = &temp[i][j]; *p = -0x3f3f3f3f; for (k = 0; k < n; k++) { *p = max(*p, a[i][k] + b[k][j]); } } } *this = temp; } bool check() { for (int i = 0; i < n; i++) if (a[i][i] > 0) return 1; return 0; } } T[20], In, now, temp; int main() { int m, i, j, k, x, y, a, b, ans; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) for (j = 0; j < n; j++) if (i != j) In[i][j] = -0x3f3f3f3f; T[0] = In; for (i = 1; i <= m; i++) { scanf("%d%d%d%d", &x, &y, &a, &b); x--, y--; T[0][x][y] = max(T[0][x][y], a), T[0][y][x] = max(T[0][y][x], b); } for (i = 1; (1 << i) <= n; i++) { T[i] = T[i - 1]; T[i] *= T[i - 1]; } for (k = i--, now = In, ans = 0; i >= 0; i--) { temp = now; temp *= T[i]; if (!temp.check()) ans |= 1 << i, now = temp; } if (ans == (1 << k) - 1) puts("0"); else printf("%d\n", ans + 1); fclose(stdin); fclose(stdout); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, a, b, x, y, ans, F, d[10][310][310], f[310][310], g[310][310]; void C(int a[310][310], int b[310][310], int c[310][310]) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) a[i][j] = min(a[i][j], b[i][k] + c[k][j]); } int main() { cin >> n >> m; memset(d, 63, sizeof d); memset(f, 63, sizeof f); for (int i = 1; i <= n; i++) d[0][i][i] = f[i][i] = 0; for (int i = 1; i <= m; i++) cin >> a >> b >> x >> y, d[0][a][b] = -x, d[0][b][a] = -y; for (int i = 1; i <= 9; i++) C(d[i], d[i - 1], d[i - 1]); for (int i = 9; i >= 0; i--) { memset(g, 63, sizeof g); C(g, f, d[i]), F = 0; for (int j = 1; j <= n; j++) if (g[j][j] < 0) F = 1; if (!F) memcpy(f, g, sizeof g), ans += (1 << i); } cout << (ans > n * 2 ? 0 : ans + 1); }
#include <bits/stdc++.h> using namespace std; 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; } typedef int mat[333][333]; mat d[10], f[10]; int ans, n, m, x, y, z, zz; inline void mul(mat &c, const mat &a, const mat &b) { for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) c[i][j] = max(c[i][j], a[i][k] + b[k][j]); } int main() { R(n), R(m); memset(d, 0xcf, sizeof(d)); memset(f, 0xcf, sizeof(f)); for (int i = 1; i <= m; i++) R(x), R(y), R(z), R(zz), d[0][x][y] = z, d[0][y][x] = zz; for (int j = 0; j < (int)10; j++) for (int i = 1; i <= n; i++) d[j][i][i] = f[j][i][i] = 0; for (int i = 0; i < (int)8; i++) mul(d[i + 1], d[i], d[i]); for (int i = 8, k = 9; ~i; i--) { mul(f[i], f[k], d[i]); bool F = 1; for (int j = 1; j <= n; j++) if (f[i][j][j] > 0) { F = 0; break; } if (F) ans += 1 << i, k = i; } printf("%d\n", (ans >= n) ? 0 : ans + 1); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, h[310][310], ans; int f[9][310][310], g[310][310]; int getint() { int w = 0; bool q = 0; char c = getchar(); while ((c > '9' || c < '0') && c != '-') c = getchar(); if (c == '-') c = getchar(), q = 1; while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar(); return q ? -w : w; } bool pd() { for (int i = 1; i <= n; i++) if (g[i][i] < 0) return 1; return 0; } int main() { n = getint(), m = getint(); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) f[0][i][j] = h[i][j] = (1 << 29); for (int i = 1; i <= n; i++) f[0][i][i] = h[i][i] = 0; for (int i = 1; i <= m; i++) { int u = getint(), v = getint(); f[0][u][v] = min(f[0][u][v], -getint()); f[0][v][u] = min(f[0][v][u], -getint()); } for (int i = 1; i < 9; i++) { for (int u = 1; u <= n; u++) for (int v = 1; v <= n; v++) f[i][u][v] = f[i - 1][u][v]; for (int u = 1; u <= n; u++) for (int v = 1; v <= n; v++) for (int k = 1; k <= n; k++) f[i][u][k] = min(f[i][u][k], f[i - 1][u][v] + f[i - 1][v][k]); } for (int i = 8; i >= 0; i--) { for (int u = 1; u <= n; u++) for (int v = 1; v <= n; v++) g[u][v] = h[u][v]; for (int u = 1; u <= n; u++) for (int v = 1; v <= n; v++) for (int k = 1; k <= n; k++) { g[u][k] = min(g[u][k], h[u][v] + f[i][v][k]); } if (!pd()) { ans += 1 << i; for (int u = 1; u <= n; u++) for (int v = 1; v <= n; v++) h[u][v] = g[u][v]; } } printf("%d", ans > n ? 0 : ans + 1); return 0; }
#include <bits/stdc++.h> using namespace std; int N; vector<vector<int> > mul(vector<vector<int> > &A, vector<vector<int> > &B) { vector<vector<int> > res(N, vector<int>(N, (1 << 29))); for (int(i) = 0; (i) < (N); (i)++) for (int(j) = 0; (j) < (N); (j)++) for (int(k) = 0; (k) < (N); (k)++) res[i][k] = min(res[i][k], A[i][j] + B[j][k]); return res; } int M, A, B, C, D; vector<vector<int> > F[9]; int main() { scanf("%d%d", &N, &M); F[0] = vector<vector<int> >(N, vector<int>(N, (1 << 29))); for (int(i) = 0; (i) < (N); (i)++) F[0][i][i] = 0; while (M--) { scanf("%d%d%d%d", &A, &B, &C, &D); A--, B--; F[0][A][B] = -C, F[0][B][A] = -D; } for (int(i) = (1); (i) < (9); (i)++) F[i] = mul(F[i - 1], F[i - 1]); vector<vector<int> > T(N, vector<int>(N, (1 << 29))); for (int(i) = 0; (i) < (N); (i)++) T[i][i] = 0; int res = 0; for (int c = 8; c >= 0; c--) { vector<vector<int> > tmp = mul(T, F[c]); bool ok = false; for (int(i) = 0; (i) < (N); (i)++) if (tmp[i][i] < 0) ok = true; if (!ok) { T = tmp; res += (1 << c); } } if (res > N) puts("0"); else printf("%d\n", ++res); }
#include <bits/stdc++.h> using namespace std; typedef int mat[305][305]; mat mn[25], dis, res; int n, m; inline void upd(mat x, mat a, mat b) { for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) x[i][j] = min(x[i][j], a[i][k] + b[k][j]); } } } int main() { scanf("%d%d", &n, &m); memset(dis, 0x3f, sizeof(dis)); memset(mn, 0x3f, sizeof(mn)); for (int i = 0; i < n; i++) mn[0][i][i] = dis[i][i] = 0; for (int i = 0; i < m; i++) { int u, v, x, y; scanf("%d%d%d%d", &u, &v, &x, &y); mn[0][--u][--v] = -x; mn[0][v][u] = -y; } for (int i = 1; i < 20; i++) upd(mn[i], mn[i - 1], mn[i - 1]); int ans = 0; for (int i = 19; i >= 0; i--) { memset(res, 0x3f, sizeof(res)); upd(res, dis, mn[i]); bool f = false; for (int j = 0; j < n; j++) f |= res[j][j] < 0; if (!f) { ans |= 1 << i; memcpy(dis, res, sizeof(res)); } } printf("%d\n", ans > n << 1 ? 0 : ans + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 305; int n, m; int mat[9][N][N]; int tmp[N][N]; int ret[N][N]; void checkmax(int &t, int x) { t = x > t ? x : t; } int main() { memset(mat, 200, sizeof(mat)); scanf("%d%d", &n, &m); for (int x, y, i = 0; i < m; i++) { scanf("%d%d", &x, &y); scanf("%d%d", &mat[0][x - 1][y - 1], &mat[0][y - 1][x - 1]); } for (int i = 0; i < n; i++) mat[0][i][i] = 0; for (int q = 1; q < 9; q++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) checkmax(mat[q][i][j], mat[q - 1][i][k] + mat[q - 1][k][j]); int ans = 0; memcpy(ret, mat[0], sizeof(ret)); for (int q = 8; q >= 0; q--) { memset(tmp, 200, sizeof(tmp)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) checkmax(tmp[i][j], ret[i][k] + mat[q][k][j]); int tag = 0; for (int i = 0; i < n; i++) if (tmp[i][i] > 0) { tag = 1; break; } if (!tag) { memcpy(ret, tmp, sizeof(tmp)); ans += 1 << q; } } if (ans + 2 > n) puts("0"); else printf("%d\n", ans + 2); }
#include <bits/stdc++.h> using namespace std; int n, m; struct M { int a[301][301]; M() { memset(a, 0, sizeof(a)); } }; M mul(M& a, M& b) { M ret; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { ret.a[i][j] = -1000000000; for (int k = 0; k < n; ++k) ret.a[i][j] = max(ret.a[i][j], a.a[i][k] + b.a[k][j]); } return ret; } M a[10]; M cur, nw; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) a[0].a[i][j] = -1000000000; for (int i = 0; i < n; ++i) a[0].a[i][i] = 0; cur = a[0]; for (int i = 0; i < m; ++i) { int x, y, c, d; scanf("%d%d%d%d", &x, &y, &c, &d); --x; --y; a[0].a[x][y] = c; a[0].a[y][x] = d; } for (int z = 0; z < 9; ++z) a[z + 1] = mul(a[z], a[z]); int ans = 1; for (int z = 9; z >= 0; --z) { nw = mul(cur, a[z]); bool awesome = false; for (int i = 0; i < n; ++i) if (nw.a[i][i] > 0) { awesome = true; break; } if (!awesome) { ans += (1 << z); cur = nw; } } if (ans <= n) printf("%d\n", ans); else printf("0\n"); }
#include <bits/stdc++.h> using namespace std; int dis[319][319]; int f[10][319][319]; int dp[10][319][319]; int n; void calc(int A[319][319], int B[319][319], int C[319][319]) { for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) for (int k = 1; k <= n; ++k) A[i][j] = max(A[i][j], B[i][k] + C[k][j]); } int main() { int m; scanf("%d%d", &n, &m); int u, v, ci, cj; memset(dis, 0xcf, sizeof(dis)); memset(dp, 0xcf, sizeof(dp)); memset(f, 0xcf, sizeof(f)); for (int i = 1; i <= n; ++i) dis[i][i] = f[9][i][i] = 0; for (int i = 0; i < m; ++i) { scanf("%d%d%d%d", &u, &v, &ci, &cj); dis[u][v] = max(dis[u][v], ci); dis[v][u] = max(dis[v][u], cj); } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) dp[0][i][j] = dis[i][j]; for (int i = 1; i < 9; ++i) { calc(dp[i], dp[i - 1], dp[i - 1]); } int ans = 0; int k = 9; int s = 0; for (int i = 8; i >= 0; --i) { calc(f[i], f[k], dp[i]); int j; for (j = 1; j <= n; ++j) if (f[i][j][j] > 0) break; if (j > n) ans += (1 << i), k = i; } if (ans > n) ans = -1; ans++; printf("%d\n", ans); }
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("O2") using namespace std; const int N = 2e5 + 5, INF = 1e9; const bool debug = 0; int n, m, a, b, c1, c2; struct matrix { vector<vector<int> > M; int n, m; matrix() {} matrix(int nn, int mm) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); } matrix(int nn, int mm, int z) { n = nn; m = mm; M.resize(n); for (int i = 0; i < n; ++i) M[i].resize(m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) M[i][j] = z; } vector<int>& operator[](int x) { return M[x]; } const vector<int>& operator[](int x) const { return M[x]; } }; matrix operator*(const matrix& a, const matrix& b) { matrix c(a.n, b.m, -INF); for (int i = 0; i < a.n; ++i) for (int j = 0; j < b.m; ++j) for (int k = 0; k < a.m; ++k) if (a[i][k] > -INF && b[k][j] > -INF && c[i][j] < a[i][k] + b[k][j]) c[i][j] = a[i][k] + b[k][j]; return c; } bool h(const matrix& a) { for (int i = 0; i < a.n; ++i) if (a[i][i] > 0) return true; return false; } int main() { if (debug) freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; matrix g(n, n, -INF); for (int i = 0; i < n; ++i) g[i][i] = 0; for (int i = 0; i < m; ++i) { cin >> a >> b >> c1 >> c2; --a; --b; g[a][b] = max(g[a][b], c1); g[b][a] = max(g[b][a], c2); } int l = 1, r = n + 1; vector<matrix> stpd(10); stpd[0] = g; for (int i = 1; i < 10; ++i) stpd[i] = stpd[i - 1] * stpd[i - 1]; for (int k = 9; k >= 0; --k) { matrix pg = g * stpd[k]; if (!h(pg)) { l += (1 << k); g = pg; } } cout << (l + 1 > n ? 0 : l + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 310, inf = 1 << 30; int n, m, f[25][maxn][maxn], ans[2][maxn][maxn]; bool check(int x) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) ans[0][i][j] = -inf; ans[0][i][i] = 0; } int now = 0; for (int t = 0; t <= 20; t++) { if (!((1 << t) & x)) continue; now ^= 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) ans[now][i][j] = -inf; ans[now][i][i] = 0; } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) ans[now][i][j] = max(ans[now][i][j], ans[now ^ 1][i][k] + f[t][j][k]); } } } for (int i = 1; i <= n; i++) if (ans[now][i][i] > 0) return true; return false; } int main() { scanf("%d%d", &n, &m); for (int t = 0; t <= 20; t++) { for (int i = 0; i < maxn; i++) { for (int j = 0; j < maxn; j++) f[t][i][j] = -inf; f[t][i][i] = 0; } } for (int i = 1, x, y; i <= m; i++) { scanf("%d%d", &x, &y); scanf("%d%d", &f[0][x][y], &f[0][y][x]); } for (int t = 1; t <= 20; t++) { for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { if (f[t - 1][i][k] == -inf) continue; for (int j = 1; j <= n; j++) f[t][i][j] = max(f[t][i][j], f[t - 1][i][k] + f[t - 1][k][j]); } } } int l = 2, r = n + 1, ans = 0; while (l <= r) { int mid = l + r >> 1; if (check(mid)) ans = mid, r = mid - 1; else l = mid + 1; } if (ans >= n + 1) ans = 0; printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, dp[12][305][305], h[2][305][305], ans = 1; int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) dp[0][i][j] = 0; else dp[0][i][j] = -0x3f3f3f3f; h[0][i][j] = dp[0][i][j]; } } for (int i = 0; i < m; i++) { int u, v, c1, c2; cin >> u >> v >> c1 >> c2; u--; v--; dp[0][u][v] = c1; dp[0][v][u] = c2; } for (int l = 1; l < 10; l++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dp[l][i][j] = dp[l - 1][i][j]; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dp[l][i][j] = max(dp[l][i][j], dp[l - 1][i][k] + dp[l - 1][k][j]); } } } } bool flag = false; for (int i = 0; i < n; i++) { if (dp[9][i][i] > 0) { flag = true; break; } } if (!flag) { cout << 0 << endl; return 0; } for (int l = 9; l >= 0; l--) { flag = false; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { h[1][i][j] = h[0][i][j]; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { h[1][i][j] = max(h[1][i][j], h[0][i][k] + dp[l][k][j]); } } } for (int i = 0; i < n; i++) { if (h[1][i][i] > 0) { flag = true; } } if (!flag) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { h[0][i][j] = h[1][i][j]; } } ans += (1 << l); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 305; const int maxint = 1 << 30; struct matrix { int data[maxn][maxn]; }; int g[maxn][maxn]; int n, m; matrix operator*(const matrix &a, const matrix &b) { matrix c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.data[i][j] = -maxint; for (int k = 1; k <= n; k++) c.data[i][j] = max(c.data[i][j], a.data[i][k] + b.data[k][j]); } } return c; } bool judge(int mid) { matrix a; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) a.data[i][j] = g[i][j]; matrix ans; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans.data[i][j] = (i == j) ? 0 : -maxint; while (mid > 0) { if (mid & 1) ans = ans * a; mid /= 2; a = a * a; } for (int i = 1; i <= n; i++) if (ans.data[i][i] > 0) return true; return false; } int main() { int a, b, c, d; while (scanf("%d%d", &n, &m) != EOF) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) g[i][j] = (i == j) ? 0 : -maxint; while (m--) { scanf("%d%d%d%d", &a, &b, &c, &d); g[a][b] = c; g[b][a] = d; } int l = 2, r = n + 5, ans = 0; while (l <= r) { int mid = (l + r) / 2; if (judge(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int f[18][310][310]; int tmp[310][310], M[310][310]; void init() { int i, j, k, l; scanf("%d%d", &n, &m); memset(f, -63, sizeof(f)); for (k = 1; k <= m; k++) { int x, y; scanf("%d%d%d%d", &i, &j, &x, &y); f[0][i][j] = x; f[0][j][i] = y; } for (l = 1; l <= 16; l++) { for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { for (k = 1; k <= n; k++) { f[l][i][j] = ((f[l][i][j]) > (f[l - 1][i][k] + f[l - 1][k][j]) ? (f[l][i][j]) : (f[l - 1][i][k] + f[l - 1][k][j])); } f[l][i][j] = ((f[l][i][j]) > (f[l - 1][i][j]) ? (f[l][i][j]) : (f[l - 1][i][j])); } } } } bool check() { int i, j, k; for (i = 1; i <= n; i++) if (tmp[i][i] > 0) return 1; return 0; } int solve() { int i, j, k, l; int ans = 0; memset(M, -63, sizeof(M)); for (l = 16; l >= 0; l--) { while (1) { if (ans) { memset(tmp, -63, sizeof(tmp)); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { for (k = 1; k <= n; k++) { tmp[i][j] = ((tmp[i][j]) > (M[i][k] + f[l][k][j]) ? (tmp[i][j]) : (M[i][k] + f[l][k][j])); } tmp[i][j] = ((tmp[i][j]) > (f[l][i][j]) ? (tmp[i][j]) : (f[l][i][j])); } } } else { memcpy(tmp, f[l], sizeof(f[l])); } if (!check()) { ans += (1 << l); memcpy(M, tmp, sizeof(tmp)); if (ans > m) return 0; } else break; } } return ans + 1; } int main() { init(); cout << solve() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 350; int n, m; int dis[MAXN][MAXN][25]; int ans[MAXN][MAXN]; int now[MAXN][MAXN]; int INF = 1 << 30; int read() { int f = 1, x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x *= 10; x += ch - '0'; ch = getchar(); } return x * f; } int main() { memset(now, 0xcf, sizeof(now)); memset(dis, 0xcf, sizeof(dis)); n = read(); m = read(); for (int i = 1; i <= m; i++) { int x, y, z, w; x = read(); y = read(); z = read(); w = read(); dis[x][y][0] = z; dis[y][x][0] = w; } for (int i = 1; i <= n; i++) { now[i][i] = 0; dis[i][i][0] = 0; } for (int s = 1; s <= 10; s++) { for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dis[i][j][s] = max(dis[i][j][s], dis[i][k][s - 1] + dis[k][j][s - 1]); } } } } int res = 0; for (int s = 10; s >= 0; s--) { memset(ans, 0xcf, sizeof(ans)); for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { ans[i][j] = max(ans[i][j], now[i][k] + dis[k][j][s]); } } } bool f = 0; for (int i = 1; i <= n; i++) { if (ans[i][i] > 0) { f = 1; break; } } if (f) continue; else { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { now[i][j] = ans[i][j]; } } res += (1 << s); } } cout << (res >= n ? 0 : res + 1) << endl; return 0; }
# include <bits/stdc++.h> using namespace std; pair<int, int> a[200010]; int calc(int l, int r, int x, int y) { if (x == l && y == r) { return 0; } int t = ((x + y) % 2) ? 0 : 1; if(x - l == y - r){ return t * (x - l); } else { if((l + r) % 2 == 0){ return (x - l - y + r) / 2 ; } else { return (x - l - y + r) / 2 + + (x - l - y + r) % 2; } } } void solve() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i].first; for (int i = 1; i <= n; i++) cin >> a[i].second; sort(a + 1, a + 1 + n); int l = 1, r = 1; int ans = 0; for (int i = 1; i <= n; i++) { ans += calc(l, r, a[i].first, a[i].second); l = a[i].first; r = a[i].second; } cout << ans<<'\n'; } int main() { std::ios::sync_with_stdio(false); // cin.tie(0); // cout.tie(0); int t; cin >> t; for (int tt = 1; tt <= t; tt++) { solve(); } }// // Created by sWX952464 on 3/26/2021. // ///13 2 10 50 1 28 37 32 30 46 19 47 33 41 24 34 27 42 49 18 9 48 23 35 31 8 7 12 6 5 3 22 43 36 11 40 26 4 44 17 39 38 15 14 25 16 29 20 21 45
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 200010 using namespace std; namespace ywy{ inline int get(){ int n=0;char c;while((c=getchar())||23333)if(c>='0'&&c<='9')break; n=c-'0';while((c=getchar())||23333){ if(c>='0'&&c<='9')n=n*10+c-'0';else return n; } } struct node{ int r,c; friend bool operator <(const node &a,const node &b){ return a.r<b.r; } }memchi[N]; int solve_0(int r,int c){ if(r==c) return(r-1); return (r-c)/2; } int solve_1(int r,int c){ return (r-c+1)/2; } void ywymain(){ int t=get(); memchi[0].r=memchi[0].c=1; while(t){ t--; int n=get(),ans=0; for(int i=1;i<=n;i++) memchi[i].r=get(); for(int i=1;i<=n;i++) memchi[i].c=get(); sort(memchi+1,memchi+1+n); for(int i=(memchi[1].r==1&&memchi[1].c==1);i<n;i++){ if((memchi[i].r+memchi[i].c)&1) ans+=solve_1(memchi[i+1].r-memchi[i].r+1,memchi[i+1].c-memchi[i].c+1); else ans+=solve_0(memchi[i+1].r-memchi[i].r+1,memchi[i+1].c-memchi[i].c+1); } printf("%d\n",ans); } } } int main(){ ywy::ywymain(); return 0; }
#include <cstring> #include <cassert> #include <utility> #include <iostream> #include <cstdio> #include <iomanip> #include <bitset> #include <chrono> #include <cstdlib> #include <functional> #include <tuple> #include <climits> #include <limits> #include <deque> #include <list> #include <array> #include <stack> #include <queue> #include <random> #include <complex> #include <string> #include <vector> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <algorithm> #define init ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); #define setpr(x) cout << setprecision((x)) #define PI atan(1)*4 #define fi first #define se second #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define mk(a , b) { min((a) , (b)) , max((a) , (b)) } #define flush cout.flush() #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define endc "\n" #define meme(x) memset((x), 0, sizeof((x))) #define memo(x) memset((x),-1, sizeof((x))) using namespace std; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<ld,ld> pld; // #define int ll #define MOD 998244353 ll egcd(ll a, ll b, ll &x, ll &y){ if (a == 0LL){ x = 0LL; y = 1LL; return b; } ll xx, yy; ll val = egcd(b % a, a, xx, yy); x = yy - (b / a) * xx; y = xx; return val; } ll gcd(ll a, ll b){ while(true){ if(a>b) swap(a,b); if (a == 0) return b; ll p = b % a, q = a; a = p; b = q; } } ll powerMod(ll x,ll y){ ll res = 1LL; x %= MOD; while(y > 0){ if(y & 1)res = (res * x) % MOD; y = y >> 1; x = (x * x) % MOD; } return res % MOD; } // ========== //\\ //|| ||====//|| // || // \\ || || // || // || //====\\ || || // || // || // \\ || || // || // ========== // \\ ======== ||//====|| // code void solve() { int n,i,j,k,l,p; cin >> n; vector<pii> a(n); for(auto &i : a) cin >> i.fi; for(auto &i : a) cin >> i.se; sort(all(a)); if(a[0].fi == 1) { a.erase(a.begin()); n--; } if(n == 0) { cout << 0 << endc; return; } debug(a); int ans = 0; int lr, lc; lr = lc = 1; for(auto i : a) { if(i.fi == i.se) { ans += i.se - lr; } else { if(lr == 1) { lr = 2; if(lr == i.fi) continue; } if(lc == i.se) { if((lc + lr) % 2) { p = i.fi - lr; ans += (p + 1) / 2; } else { p = i.fi - lr; ans += p / 2; } } else { // lc < i.se if((lc + lr) % 2) { lr = lr + i.se - lc; lc = i.se; p = i.fi - lr; ans += (p + 1) / 2; } else { if(i.fi - lr == i.se - lc) ans += i.se - lc; else { lr++; lr = lr + i.se - lc; lc = i.se; p = i.fi - lr; ans += (p + 1) / 2; } } } } lr = i.fi; lc = i.se; debug(ans); } cout << ans << endc; } int32_t main(){ init; int t = 1; cin >> t; for(int tt = 1; tt <= t; tt++) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define all(x) x.begin(),x.end() #define pll pair <long long,long long> #define ss second #define ff first #define inf (ll)2e18 #define mod 1000000007 //#define mod 998244353 #define ld long double #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); #define endl "\n" const ll N=17 ; int main() { fast ; ll t ; cin>>t ; while(t--) { ll n ; cin>>n ; ll r[n] ; for(ll i=0;i<n;i++) cin>>r[i] ; vector<pll>v ; for(ll i=0;i<n;i++) { ll c ; cin>>c ; v.push_back({r[i],c}) ; } sort(all(v)) ; ll curx=1,cury=1 ; ll ans=0 ; for(ll i=0;i<n;i++) { ll id1=abs(curx-cury) ; ll id2=abs(v[i].ff-v[i].ss) ; ll temp=(id2/2-id1/2) ; ans+=temp ; if(temp) { cury=v[i].ss ; curx=cury+2*(id2/2) ; } if(id2%2==0) { ans+=v[i].ff-curx ; } curx=v[i].ff ; cury=v[i].ss ; } cout<<ans<<endl ; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; pair<ll,ll>p[200020]; int main(void){ ll t,n,i,ans,x,y; cin>>t; while(t--){ ans=0; scanf("%lld",&n); n++; p[1]=make_pair(1ll,1ll); for(i=2;i<=n;i++) scanf("%lld",&p[i].first); for(i=2;i<=n;i++) scanf("%lld",&p[i].second); sort(p+1,p+n+1); for(i=2;i<=n;i++){ x=p[i-1].first-p[i-1].second; y=p[i].first-p[i].second; if(x==y&&x%2==0){ ans+=p[i].first-p[i-1].first; continue; } x=x/2,y=y/2; ans=ans+y-x; } printf("%lld\n",ans); } }
#include <bits/stdc++.h> #define ll long long int #define pb push_back #define umap unordered_map #define mod 1000000007ll #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define MN(a,b,c) min(a,min(b,c)) #define MX(a,b,c) max(a,max(b,c)) #define pr1 pair<ll,ll> #define F first #define S second #define mP make_pair #define f(i,n) for(ll i=0;i<n;i++) #define f1(i,x,y) for(ll i=x;i<=y;i++) #define f2(i,x,y) for(ll i=x;i>=y;i--) #define yes cout<<"Yes"<<"\n" #define no cout<<"No"<<"\n" #define modsum(a,b) ((a%mod)+(b%mod))%mod #define modpro(a,b) ((a%mod)*(b%mod))%mod #define moddif(a,b) ((a%mod)-(b%mod)+mod)%mod #define modsumt(a,b,c) modsum(a,modsum(b,c)) //__builtin_popcount(x) //__builtin_parity(x) =(number of set bits)%2 //__builtin_clz(x) to count the number of leading zeroes //__builtin_ctz(x) to count the number of trailing zeroes //__gcd(a,b) using namespace std; ll modularExponentiation(ll x,ll n,ll M) { ll result=1; while(n>0) { if(n % 2 ==1) result=modpro(result,x); x=modpro(x,x); n=n/2; } return result; } ll binaryExponentiation(ll x,ll n) { ll result=1; while(n>0) { if(n % 2 ==1) result=result * x; x=x*x; n=n/2; } return result; } ll pow1(ll x,ll y) { ll z=1; while(y--){z=z*x;} return z; } bool isprime(ll n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Returns n^(-1) mod p ll modInverse(ll n, ll p) { return modularExponentiation(n, p - 2, p); } // Returns nCr % p using Fermat's little // theorem. unsigned long long nCrModPFermat(unsigned long long n, ll r, ll p) { // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r unsigned long long fac[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } bool compare(pair<ll,ll> a, pair<ll,ll> b) { if(a.F==b.F) return a.S>b.S; else return a.F>b.F; } ll check(ll x,ll y) { ll z=1; ll ans=0; while(z<x){ans++;z*=y;} return ans; } ll countbits(ll n) { ll x=0; while(n>0) { x++; n=(n&(n-1)); } return x; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t,n; cin>>t; while(t--) { cin>>n; vector <pair<ll,ll>> a(n); f(i,n) cin>>a[i].F; f(i,n) cin>>a[i].S; sort(all(a)); ll cost=0; f(i,n) { if(i==0) { ll d1=0; ll d2=a[i].F-a[i].S; ll num=d2-d1+1; ll steps=num-1; if(d1%2) { cost+=(num/2); } else { cost+=((num-1)/2); } if(d2==0 || ((d2%2)==0 && d1==d2)) { cost+=(a[i].F-1-steps); } } else { ll d1=a[i-1].F-a[i-1].S; ll d2=a[i].F-a[i].S; ll num=d2-d1+1; ll steps=num-1; if(d1%2) { cost+=(num/2); } else { cost+=((num-1)/2); } if(d2==0 || ((d2%2)==0 && d1==d2)) { cost+=(a[i].F-a[i-1].F-steps); } } // cout<<cost<<"\n"; } cout<<cost<<"\n"; } }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define ff first #define ss second #define mp make_pair #define IN insert #define ALL(a) a.begin(),a.end() typedef long long int ll; ll solve(pair<ll, ll> p1, pair<ll, ll> p2) { if (p1.ff - p1.ss == p2.ff - p2.ss) { // cout << p1.ff << " " << p1.ss << " " << " " << p2.ff << " " << p2.ss << endl; if ((p1.ff + p1.ss) % 2)return 0; else return p2.ff - p1.ff; } p2.ff = p2.ff - (p1.ff - 1); p2.ss = p2.ss - (p1.ss - 1); if ((p1.ff + p1.ss) % 2 == 0) { return (p2.ff - p2.ss) / 2; } else { return (p2.ff - p2.ss + 1 ) / 2; } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output56.txt", "w", stdout); #endif ll t; cin >> t; while (t--) { ll n; cin >> n; vector<pair<ll, ll>> v(n); for (int i = 0; i < n; i++) { ll a; cin >> a; v[i].ff = a; } for (int i = 0; i < n; i++) { ll a; cin >> a; v[i].ss = a; } v.pb({1, 1}); sort(ALL(v)); ll ans = 0; for (int i = 1; i < v.size(); i++) { ans += solve(v[i - 1], v[i]); // cout << ans << endl; } cout << ans << endl; } }
#include<bits/stdc++.h> // 56 70 "Baklol, Take it easy" using namespace std; using ll = long long int; using ld = long double; using ull = unsigned long long int; #define rep(x, k, n) for(int32_t x = (k); x <= (n); ++x) #define rept(x, k, n) for(int32_t x = (k); x < (n); ++x) #define repr(x, k, n) for(int32_t x = (k); x >= (n); --x) #define pb push_back #define F first #define S second #define siz(x) ((int)(x).size()) #define o2(x) ((x) * (x)) #define all(x) (x).begin(), (x).end() #define clr(x, k) memset(x, k, sizeof(x)) // 0, -1 #define printv(v, k, n) rep(i, k, n) cout << v[i] << " \n"[i==n]; #define print2dv(V,k,n,m) rep(j, k, n) printv(V[j], k, m); #define report(x) cout << ((x) ? "Yes" : "No") << '\n' #define setbits(x) __builtin_popcountll(x) #define int long long // "be carefull" typedef vector<int> vi; typedef pair<int, int> pii; template <typename Arg1> void debug_out(const char* name, Arg1&& arg1){ cerr << name << " = " << arg1 << " ]" << "\n"; } template <typename Arg1, typename... Args> void debug_out(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " = " << arg1 << ","; debug_out(comma+1, args...); } #ifdef LOCAL #define deb(...) cerr << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__) #define debv(v, k, n) rep(i, k, n) cerr << v[i] << " \n"[i==n]; #define deb2dv(V, k, n, m) rep(j, k, n) debv(V[j], k, m); #define line rep(i, 1, 50) cerr << " " << "-\n"[i==50]; #else #define deb(...) #define debv(v, k, n) #define deb2dv(V, k, n, m) #define line #endif constexpr long long inf = 2e9, ninf = -2e9; constexpr long double eps = 1e-9; constexpr int mod = 1e9 + 7; // 998244353; constexpr int N = 3e5 + 5, M = 2e5 + 5; /*/------------------------------ CODE BEGINS ------------------------------/*/ int n; pii v[N]; // r, c void DONT_ASSUME_BLINDLY() { cin >> n; rep(i, 1, n) cin >> v[i].F; rep(i, 1, n) cin >> v[i].S; sort(v+1, v+n+1); int res = 0; v[0] = {1, 1}; for(int i = 1; i <= n; i++){ auto [r, c] = v[i-1]; auto [x, y] = v[i]; if((r + c) % 2 == 0){ if(x - r == y - c){ res += x - r; continue; } r++; } r += (y - c); res += (x - r + 1) / 2; } cout << res << '\n'; } /*/------------------------------- CODE ENDS -------------------------------/*/ int32_t main() { cin.tie(0)->sync_with_stdio(0); // cout << setprecision(12) << fixed; int tc = 1; cin >> tc; for(int i = 1; i <= tc; i++) { DONT_ASSUME_BLINDLY(); #ifdef LOCAL auto brute_force = [&]()->void{ }; // brute_force(); // break; #endif } cerr << "[time:" << 1.0*clock()/CLOCKS_PER_SEC << "s] "; return 0; } /* -> Understood the problem?, misread? -> edge cases, n = 1 ? -> binary search ?, brute_force ? -> time complexity - 1 sec : 4e8 will work but risky. -> space complexity - 256 mb : 6e7(int), 3e7(ll), 2e8(bool, char), will work. -> entire input - multiple testcases. -> mod : sort ?, mint, remove #define int. -> Iterative > Recursive. -> clear - global variables. -> builtin function - add ll. -> Move on - if completely_stuck > 30 minute. -> Submit - right file. */ /* -> Don't assume blindly. -> Focus on problem. -> Do consider worst case time complexity. -> If solution is barely passing then do submit again after doing optimization. -> Be carefull in nested loop. Iterating variable name should be different for both loop. -> Think simple first. -> Don't be stupid. -> Be comfortable and in your control. */
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; const int maxn = 2e5+55; struct node { int a, b; }; node no[maxn]; int main() { int t; cin >> t; while (t --) { int n; cin >> n; for (int i = 0; i < n; ++ i) { cin >> no[i].a; } for (int i = 0; i < n; ++ i) { cin >> no[i].b; } sort(no, no+n, [](node x, node y){ return x.a < y.a; }); int x = 1, y = 1, ans = 0; for (int i = 0; i < n; ++ i) { if ((x-y) / 2 == (no[i].a-no[i].b) / 2) { if ((no[i].a-no[i].b) % 2 == 0) { ans += (no[i].a - x); } } else { ans += abs((x-y) / 2 - (no[i].a-no[i].b) / 2); } // cout << x << " " << y << " " << ans << endl; x = no[i].a, y = no[i].b; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize ("-Ofast") #pragma GCC optimize ("unroll-loops") #define TASK "task" using namespace std; void openFiles() { #ifdef THEMIS freopen(TASK ".inp", "r", stdin); freopen(TASK ".out", "w", stdout); #endif } const long long MOD = 1e9 + 7; const int maxN = 2e6 + 5; int fact[maxN]; void factorial(int n) { fact[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = 1ll * fact[i - 1] * i % MOD; } } int Pow(int b, int e = MOD - 2) { if (e == 0) return 1; int x = Pow(b, e / 2); if (e % 2 == 0) return 1ll * x * x % MOD; return (1ll * x * x % MOD) * b % MOD; } int catalan(int n) { int res = 1ll * fact[2 * n] * Pow(fact[n]) % MOD; res = 1ll * res * Pow(fact[n + 1]) % MOD; return res; } void BracketSequencesI() { /// cses int n; cin >> n; if (n % 2 == 1) { cout << 0; return; } n /= 2; factorial(2 * n + 1); cout << catalan(n); } void BracketSequencesII() { /// cses int n; cin >> n; string s; cin >> s; if (n % 2 == 1) { cout << 0; return; } factorial(2 * n + 1); int k = 0; for (char i : s) { if (i == '(') k++; else k--; if (k < 0) break; if (k > n / 2) { k = -1; break; } } if (k < 0) { cout << 0; return; } } typedef pair<int, int> i2; void solveF_TriangularPaths() { int t; cin >> t; while (t--) { int n; cin >> n; vector<i2> arr(n, i2(0, 0)); for (int i = 0; i < n; ++i) cin >> arr[i].first; for (int i = 0; i < n; ++i) cin >> arr[i].second; sort(arr.begin(), arr.end(), [] (i2 a, i2 b) { if (a.second - a.first == b.second - b.first) return a.first < b.first; return a.first - a.second < b.first - b.second; }); int curr = 0, x = 1, y = 1; long long res = 0; for (auto it : arr) { int xx = it.first; int yy = it.second; int cnt = (xx - yy) / 2 - (x - y) / 2; res += cnt; int tmp = (xx - yy) - (x - y); if ((xx - yy) % 2 == 0 && (xx - yy) == (x - y)) res += yy - y; x = xx; y = yy; } cout << res << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); openFiles(); // BracketSequencesI(); // BracketSequencesII(); solveF_TriangularPaths(); }
#include <bits/stdc++.h> #define FOR(i, j, k) for(int i = j; i < k; i ++) #define FORE(i, j, k) for(int i = j; i <= k; i ++) #define FORD(i, j, k) for(int i = j; i >= k; i --) #define mp make_pair #define f first #define s second //#define int long long using namespace std; const int MOD=1e9+7; const int N = 200005; int T, n; pair < int, int > a[N]; long long res = 0; long long calc(int u, int v, int x, int y) { if(u - v == x - y) { if((u + v) % 2 == 0) return x - u; else return 0; } else { x = x - u + 1; y = y - v + 1; if((u + v) % 2 == 0) return (x - y)/2; else return (x - y)/2 + ((x + y) % 2); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #define task"" // freopen(task".inp","r",stdin); // freopen(task".out","w",stdout); cin >> T; while(T --) { res = 0; cin >> n; FORE(i, 1, n) cin >> a[i].f; FORE(i, 1, n) cin >> a[i].s; sort(a + 1, a + n + 1); int u = 1, v = 1; FORE(i, 1, n) { res += calc(u, v, a[i].f, a[i].s); u = a[i].f; v = a[i].s; } cout << res << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e6+7; const int inf=2e9; const ll inff=1e18; const ll mod=1e9+7; #define pii pair<int,int> #define mkp make_pair #define F first #define S second #define pb push_back #define sz(v) ((int)(v).size()) #define all(v) (v).begin(),(v).end() //#define int ll int a[maxn],b[maxn]; int32_t main(){ ios::sync_with_stdio(0); cin.tie(0); int _; cin>>_; while (_--) { int n; cin>>n; for (int i=1;i<=n;i++) cin>>a[i]; for (int i=1;i<=n;i++) cin>>b[i]; vector<pii> v; v.pb({1,1}); for (int i=1;i<=n;i++) v.pb({a[i],b[i]}); sort(all(v)); ll ans=0; for (int i=1;i<=n;i++) { if ((v[i-1].F+v[i-1].S)&1) { ans+=(v[i].F-v[i-1].F-(v[i].S-v[i-1].S)+1)/2; } else { if (v[i].F-v[i-1].F == v[i].S-v[i-1].S) ans+=(v[i].F-v[i-1].F); else ans+=(v[i].F-v[i-1].F-(v[i].S-v[i-1].S))/2; } } cout<<ans<<'\n'; } }
/* -> Written by <- ----------- |K_A_Z_A_M_A| |___________| | ___ | | (^_^) | | /( | )\ | |____|_|____| H O A N G */ #include <bits/stdc++.h> #define Task "running" #define F first #define S second #define pb push_back #define bit(x, i) ((x >> (i)) & 1) #define inf 1e9 + 7 #define INF 1e18 + 7 #define ll long long #define pii pair <int, int> #define debug(x) cerr << #x << " is " << x << "\n"; using namespace std; const int MOD = 1e9 + 7; const int maxn = 1e5 + 5; const int base = 3e5 + 1; int n; pair <int, int> a[200005]; int CalDis(int r1, int c1, int r2, int c2) { if (r1 - c1 == r2 - c2) { if ((r1 + c1) % 2 == 0) return r2 - r1; return 0; } int newr = r2 - c2 + c1; int newc = c1; int x = newr - r1; if ((newr + newc) % 2) return x / 2; return (x + 1) / 2; } void Solve() { cin >> n; for (int i = 1; i <= n; ++ i) { cin >> a[i].F; } for (int i = 1; i <= n; ++ i) { cin >> a[i].S; } sort(a + 1, a + 1 + n); int r1 = 1, c1 = 1; ll res = 0; for (int i = 1; i <= n; ++ i) { int r2 = a[i].F; int c2 = a[i].S; res += CalDis(r1, c1, r2, c2); c1 = c2; r1 = r2; } cout << res << "\n"; } int main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); if (fopen(Task".inp", "r")){ freopen(Task".inp", "r", stdin); freopen(Task".out", "w", stdout); } // Prepare(); int test_case = 1; cin >> test_case; while (test_case --){ Solve(); } return 0; }
#include<bits/stdc++.h> #define inf 1e18 #define endl "\n" #define mp make_pair #define pb push_back #define loop(i,x,y) for(int i = x; i < y ; i++ ) #define all(x) (x).begin(),(x).end() #define in(n) int n; cin>>n; #define inarr(arr,n) vector<int>arr(n); for(int i = 0; i < n ; i++){cin>>arr[i];} #define maploop(x) for(auto itr = x.begin(); itr != x.end();itr++) #define int long long using namespace std; #ifndef ONLINE_JUDGE #define debug(x) cerr << #x<<" "; _print(x); cerr << endl; #else #define debug(x); #endif void _print(int t) {cerr << t;} void _print(string t) {cerr << t;} void _print(char t) {cerr << t;} void _print(double t) {cerr << t;} template <class T, class V> void _print(pair <T, V> p); template <class T> void _print(vector <T> v); template <class T> void _print(set <T> v); template <class T, class V> void _print(map <T, V> v); template <class T> void _print(multiset <T> v); template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";} template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ const int N = 10001; const int mod = 1e9 + 7; int sum = 0; void carona(int r1, int c1, int r2, int c2) { //cout << r1 << " " << c1 << " " << r2 << " " << c2 << endl; if (r1 - c1 == r2 - c2) { if ((r1 + c1 ) % 2 ) return; else sum += r2 - r1; } else { int k = r2 - r1 + 1; int k2 = c2 - c1 + 1; if ((r1 + c1) % 2 == 0) sum += (k - k2) / 2; else sum += (k - k2 + 1) / 2; } } void Main() { in(n) vector<int>r; vector<int>c; int a, b; loop(i, 0, n) { cin >> a; r.pb(a); } loop(i, 0, n) { cin >> b; c.pb(b); } vector<pair<int, int>>fin; loop(i, 0, n) { fin.pb({r[i], c[i]}); } sum = 0; sort(fin.begin(), fin.end()); int prev = 1; int prevc = 1; loop(i, 0, n) { carona(prev, prevc, fin[i].first, fin[i].second); prevc = fin[i].second; prev = fin[i].first; } cout << sum << endl; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) { Main(); } }
#include<bits/stdc++.h> using namespace std; typedef long long lol; typedef unsigned long long ulol; #define ci cin>> #define co cout<< #define el <<'\n' #define te(i) int i; cin>>i #define ten te(n) #define tem te(m) #define tek te(k) #define tex te(x) #define tey te(y) #define tez te(z) #define pb push_back #define ub upper_bound #define lb lower_bound #define bs binary_search #define all(x) x.begin(), x.end() #define fo(i,a,b,p) for (int i=a; i<b; i+=p) #define foi(n) fo(i,0,n,1) #define foj(n) fo(j,0,n,1) #define foin foi(n) #define foim foi(m) #define fojn foj(n) #define fojm foj(m) #define ff first #define ss second #define ex return 0 #define cnu continue int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int _tatakae_ = 1; ci _tatakae_; while(_tatakae_--){ ten; vector<pair<int, int>> a(n); foin ci a[i].ff; foin ci a[i].ss; sort(all(a)); lol ans=0; int k=0; int c=1; int x=1, y=1; foin{ if(x-y == a[i].ff-a[i].ss){ if((x-y)%2) {x=a[i].ff, y=a[i].ss; cnu;} ans+=a[i].ff-x; x=a[i].ff, y=a[i].ss; } else{ int k=a[i].ff-a[i].ss; k-=(x-y); if((x-y)%2) k++; ans+=k/2; x=a[i].ff, y=a[i].ss; } //co ans el; } co ans el ; } ex; }
#include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while(t--){ int n; cin>>n; int rs[n]; int cs[n]; map<int,int> m; for(int i=0;i<n;i++){ cin>>rs[i]; } for(int i=0;i<n;i++){ cin>>cs[i]; } for(int i=0;i<n;i++){ m.insert(pair<int,int>(rs[i],cs[i])); } m.insert(pair<int,int>(1,1)); map<int,int>::reverse_iterator it; map<int,int>::reverse_iterator jt; int cost =0; for(it= m.rbegin(),jt = ++m.rbegin();jt!=m.rend();it++,jt++){ int dy = it->first - jt->first; int dx = it->second - jt->second; if(dx==dy){ //are they on the track or not if((it->first+it->second)%2==0){cost+=dx;} else{ //we dont do anything here they are on a track } } else if(dx==0){ //we can combinte some cases (1 and 3 but we wont) //for debugging if(dy%2==0){cost+=dy/2;} else if((it->first+it->second)%2==0){cost+=dy/2+1;} else{cost+=dy/2;} } else{ //this means that it is inbetween // we know that there is at least one track between those int diff = dy-dx; if(diff%2==0){cost+=diff/2;} else if((it->first+it->second)%2==0){cost+=diff/2+1;} else{cost+=diff/2;} } } cout<<cost<<endl; } return 0; }
#include <bits/stdc++.h> #define lli int #define loop(i,x,n) for(lli i=x;i<n;i++) #define primeloop(i,x,n) for(lli i=x;i*i<=n;i++) #define loopr(i,x,n) for(lli i=n-1;i>=x;i--) #define st set<lli> #define ve vector<lli> #define um unordered_map<lli,lli> #define vpl vector<pair<lli,lli>> #define llu long long unsigned #define ms multiset<lli> #define ums unordered_map<string,lli> #define pb push_back #define time ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.setf(ios::fixed); cout.setf(ios::showpoint); #define test lli t; cin>>t; while(t--) #define mod 1000000007 using namespace std; int main() { time test { lli n; cin>>n; set<pair<lli,lli>> ele; lli r[n]; loop(i,0,n) cin>>r[i]; loop(i,0,n) { lli c; cin>>c; ele.insert({r[i],c}); } lli ans=0; lli currx=1,curry=1,nxtx,nxty; for(auto it : ele) { nxtx=it.first; nxty=it.second; // cout<<currx<<" "<<curry<<" "<<nxtx<<" "<<nxty<<endl; if((nxtx-nxty) == (currx-curry)) ans+=((currx+curry)%2)?0LL:(nxtx-currx); else { lli fothisx = nxtx - currx; lli fothisy = nxty - curry; lli diff = (fothisx - fothisy)/2; ans+=((currx+curry)%2==0)?(diff):(diff+((fothisx - fothisy)%2)); } // cout<<ans<<endl; currx=nxtx; curry=nxty; } cout<<ans<<endl; } return 0; }
#include <bits/stdc++.h> #define pb push_back #define fi first #define se second using namespace std; typedef long long LL; typedef pair<LL, LL> ii; LL N; ii A[200100]; void main1() { cin >> N; for (LL i = 0; i < N; i++) { cin >> A[i].fi; } for (LL i = 0; i < N; i++) { cin >> A[i].se; } A[N] = {1, 1}; N++; sort(A, A + N, [&](ii a, ii b) { return a.fi + a.se < b.fi + b.se; }); LL ans = 0; for (LL i = 1; i < N; i++) { ii a = A[i - 1]; ii b = A[i]; LL l1 = a.fi - a.se; LL l2 = b.fi - b.se; if (l1 == l2 && l1 % 2 == 0) { ans += b.fi - a.fi; continue; } l1 /= 2; l2 /= 2; ans += l2 - l1; } cout << ans << endl; } LL T2; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T2; while (T2--) { main1(); } return 0; }
#include <iostream> #include <vector> #include <set> #include <algorithm> #include <cmath> #include <iomanip> #include <string> #include <map> #include <set> #include <cassert> #include <ciso646> #include <climits> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <array> #include <bitset> #include <deque> #include <forward_list> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> #include <algorithm> #include <complex> #include <functional> #include <initializer_list> #include <iterator> #include <limits> #include <locale> #include <numeric> #include <regex> #include <string> #include <utility> #include <fstream> #include <iostream> #include <sstream> #include <iomanip> #include <random> #include <memory> #include <chrono> using namespace std; #define pb push_back #define mp make_pair #define ff first #define ss second using ll = long long; #define int long long #define endl '\n' void fast_stream() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } ll inf = 1e18; void solve () { ll n; cin >> n; vector<pair<ll, ll>> s(n); for (int i = 0; i < n; i++) { cin >> s[i].first; } for (int i = 0; i < n; i++) { cin >> s[i].second; } sort(s.begin(), s.end()); if (s[0].first != 1 || s[0].second != 1) { reverse(s.begin(), s.end()); s.pb(mp(1, 1)); reverse(s.begin(), s.end()); } ll ans = 0; for (int i = 1; i < s.size(); i++) { //cout << s[i].first << " " << s[i].second << " "; ll sum = s[i - 1].first - s[i - 1].second, sum1 = s[i].first - s[i].second; //cout << sum << " " << sum1 << " "; if ((s[i - 1].first + s[i - 1].second) % 2 == 0) { ll nee = sum1 - sum; ll kol = s[i].first - s[i - 1].first; if (nee == 0) { ans += kol; } else { ans += nee / 2; } } else { ll nee = sum1 - sum; ll kol = s[i].first - s[i - 1].first; ans += (nee + 1) / 2; } //cout << ans << endl; } cout << ans << endl; } signed main() { fast_stream(); ll t = 1; cin >> t; while (t--) { solve(); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <list> #include <map> #include <iostream> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <bitset> #include <assert.h> #define ll long long #define ull unsigned long long #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define PI 3.1415926535898 #define pb push_back #define F first #define S second #define lson i << 1 #define rson i << 1 | 1 #define mem(x, y) memset(x, y, sizeof(x)) #define vec vector #define fin(x) freopen(x, "r", stdin) #define fout(x) freopen(x, "w", stdout) #define fcl fclose(stdin), fclose(stdout) #define ioss ios::sync_with_stdio(false), cout.tie(NULL) #define pii pair<int, int> #define all(x) x.begin(), x.end() #define endl '\n'; #define eps 1e-7 const int MAX = 2e5 + 7; const int MOD = 1e9 + 7; using namespace std; int t; int n; pair<ll,ll> p[MAX]; int main() { ioss; cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; i++) cin >> p[i].F; for (int i = 1; i <= n; i++) cin >> p[i].S; sort(p + 1, p + n + 1); ll prer = 1, prec = 1; ll ans = 0; for (int i = 1; i <= n; i++) { ll pred = prer - prec, d = p[i].F - p[i].S; if (pred == d && pred % 2 == 0) ans += p[i].S - prec; prer = p[i].F, prec = p[i].S; } ans += (p[n].F - p[n].S) / 2; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> #define pb push_back #define ll long long #define ull unsigned long long #define mp make_pair #define si short int #define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define pill pair<ll,ll> #define f first #define s second #define pilc pair<ll,char> #define all(a) (a).begin(),(a).end() #define rep(s,e,step) for(int i = (s); i < (e) ; i += step) #define vrep(s,e,step) for(int j = (s); j < (e) ; j += step) #define ex exit(0) #define sz(a) (a).size() #define triple pair<pill, ll> #define pinode pair<node*, node*> #define quadra pair<pill, pill> #define ld long double using namespace std; const ll N = 3e5 + 11; const ll M = 1E6; const ll big = 1e16; const ll hsh2 = 1964325029 ; const long long mod = 1e9 + 7; const long double EPS = 1e-15; const ll block = 1e7; const ll shift = 2e3; ll n, t; pill z[N]; int main() { speed; cin >> t; while(t--) { cin >> n; for(int i = 1; i <= n; i++) cin >> z[i].f; for(int i = 1; i <= n; i++) cin >> z[i].s; sort(z + 1, z + n + 1); ll a = 1, b = 1; ll ans = 0; for(int i = 1; i <= n; i++) { ll dlt = z[i].f - a; ll dlt2 = z[i].s - b, dlt3; ll cur = (a + b) % 2; if(cur) { dlt -= dlt2; ans += (dlt + 1)/2; } else { if(dlt == dlt2) { ans += dlt; } else { dlt--; dlt -= dlt2; ans += (dlt + 1) / 2; } } a = z[i].f, b = z[i].s; } cout << ans << '\n'; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pi = pair<int,int>; using pll=pair<ll,ll>; using vi=vector<int>; using vpi=vector<pi>; using vll=vector<ll>; using vpll=vector<pll>; #define pb push_back #define mp make_pair #define rep(i,a,b) for(int i=int(a);i<int(b);i++) #define ff first #define ss second #define sz(v) ((int)(v).size()) #define all(v) (v).begin(),(v).end() int t,n,a,ans1,b; pair<int,int> A[200001]; pair<int,int> p; int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>t; while(t--) { cin>>n; for(int i=0;i<n;i++) { cin>>a; A[i].first=a; } for(int i=0;i<n;i++) { cin>>a; A[i].second=a; } sort(A, A+n); ans1=0; a=0; p.first=1; p.second=1; for(int i=0;i<n;i++) { b=(A[i].first - A[i].second)/2; assert(b>=a); if(a==b) { if((!((p.first-p.second)%2))&&(!((A[i].first-A[i].second)%2))) { ans1+=(A[i].first-p.first); } } /*if(a!=b) { if(!((A[i].first-A[i].second)%2)) { ans1+=(A[i].second-p.second); } }*/ ans1+=(b-a); a=b; p=A[i]; } cout<<ans1<<"\n"; } }
#include <bits/stdc++.h> using namespace std; #define int ll using ll = long long; using db = long double; using pi = pair<int, int>; using mi = map<int, int>; #define fi first #define se second #define vt vector #define sz size() #define mp make_pair #define eb emplace_back #define endl '\n' #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, l, r) for (int i = l; i >= r; i--) void ok(){cout << "YES" << endl; exit(0);} void no(){cout << "NO" << endl; exit(0);} const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; ll qpow(ll a,ll b,ll MOD=mod){for(ll ans=1;;a=a*a%MOD,b>>=1){if(b&1)ans=ans*a%MOD;if(!b)return ans;}} ll inv(ll a,ll MOD=mod){return qpow(a,MOD-2,MOD);}//Ҫ��MODΪ���� ll exgcd(ll a,ll b,ll &x,ll &y){if(b==0){x=1,y=0;return a;}ll ret=exgcd(b,a%b,y,x);y-=a/b*x;return ret;} ll getInv(int a,int mod){ll x,y;ll d=exgcd(a,mod,x,y);return d==1?(x%mod+mod)%mod:-1;}//��a��mod�µ���Ԫ����������Ԫ����-1����Ҫ��MODΪ���� using vi = vt<int>; const int N = 4e5 + 5; int M = 1e9 + 7; inline int add(int x, int y) { return x + y >= M ? x + y - M : x + y; } inline int mul(int x, int y) { return x * y % M; } inline int sub(int x, int y) { return x - y < 0 ? x - y + M : x - y; } using G = vt<vi>; #define mid (l+r>>1) #define lc p<<1 #define rc lc|1 #define lson lc,l,mid #define rson rc,mid+1,r int dp[101][101]; int even(int x, int y, int nowx, int nowy){ return (nowx - x + y- nowy + 1)/2; } int odd(int x, int y, int nowx, int nowy){ if(nowx - x + y == nowy){ return nowx - x; }else{ return (nowx - x + y - nowy)/2; } } signed main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t; cin >> t; while(t--){ int n; cin >> n; vector<pair<int, int>>p(n); rep(i, 0, n - 1) cin >> p[i].fi; rep(i, 0, n - 1) cin >> p[i].se; sort(all(p)); int ans = 0; int x = 1, y = 1; for(int i = 0; i < n; i++){ if((x + y) % 2 == 0){ ans += odd(x, y, p[i].fi, p[i].se); }else{ ans += even(x, y, p[i].fi, p[i].se); } x = p[i].fi; y = p[i].se; } cout << ans << endl; } // dp[1][1] = 0; //// for(int i = 0; i <= 3; i++) dp[3][i] = LONG_MAX - 1000; //// dp[3][2] = 0; // ������1��ʼ�� ż����0��ʼ // for(int i = 2; i <= 100; i++){ // for(int j = 1; j <= i; j++){ // int x = i - 1; // int y = j - 1; // dp[i][j] = LONG_MAX; // if(j < i){ // if((x + j) % 2 == 0){ // dp[i][j] = dp[x][j]; // }else{ // dp[i][j] = dp[x][j] + 1; // } // } // if(j > 1){ // if((x + y) % 2 == 0){ // dp[i][j] = min(dp[x][y] + 1, dp[i][j]); // }else{ // dp[i][j] = min(dp[x][y], dp[i][j]); // } // } // cout << dp[i][j] << " \n"[j == i]; // } // } // nowx - x + y }
#include<bits/stdc++.h> #define int long long #define pii pair<int,int> #define X first #define Y second using namespace std; int32_t main(){ //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t; cin>>t; while(t--){ int n; cin>>n; vector<pii> v(n); for(auto& x: v) cin>>x.X; for(auto& x: v) cin>>x.Y; v.push_back({1LL, 1LL}); sort(v.begin(), v.end()); int res = 0, max_tire = 0; for(int i{1}; i <= n; ++i){ int tire1 = v[i - 1].X - v[i - 1].Y; int tire2 = v[i].X - v[i].Y; if(tire2 == tire1 && tire1 % 2 == 0){ res += v[i].X - v[i - 1].X; } max_tire = max(max_tire, tire2); } res += max_tire / 2; cout<<res<<endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { int t; cin>>t; while(t--) { int n; cin>>n; vector<int> a(n),b(n),ord(n); for(int i=0;i<n;++i) cin>>a[i]; for(int i=0;i<n;++i) cin>>b[i]; iota(ord.begin(),ord.end(),0); sort(ord.begin(),ord.end(),[&](int i,int j){return a[i]<a[j];}); int ans=0,x=1,y=1; for(int i:ord) { int nx=a[i],ny=b[i]; if(x==nx && ny==y); else { assert(nx>x); assert(nx-x>=ny-y); int cnt1 = (nx-x)-(ny-y), cnt2 = nx-x-cnt1; if(cnt1) { if((x+y)%2) --cnt1,++ans; ans+=cnt1/2; } else { if((x+y)%2==0) ans+=cnt2; } } x=nx,y=ny; } cout<<ans<<endl; } }
#pragma GCC optimize(2) #include <bits/stdc++.h> #define all(n) (n).begin(), (n).end() #define se second #define fi first #define pb emplace_back #define mp make_pair #define sqr(n) ((n)*(n)) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) #define precision(a) setiosflags(ios::fixed) << setprecision(a) #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<long long> VL; typedef double db; template <typename T> inline char read(T& x) { x = 0; T fg = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') fg = -1; ch = getchar(); } while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ '0'), ch = getchar(); x = fg * x; return ch; } template <typename T, typename... Args> inline void read(T& x, Args &... args) { read(x), read(args...); } template <typename T> inline void write(T x) { int len = 0; char c[40]; if (x < 0) putchar('-'), x = -x; do { ++len; c[len] = x % 10 + '0'; } while (x /= 10); for (int i = len; i >= 1; i--) putchar(c[i]); } template <typename T, typename... Args> inline void write(T& x, Args &... args) { write(x), write(args...); } template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; } template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; } template<class T> void clear(T& a) { T().swap(a); } const int N = 2e5 + 5; int n, m, _, k, cas; PII a[N]; int main() { IOS; a[0] = { 1, 1 }; for (cin >> _; _; --_) { cin >> n; rep(i, 1, n) cin >> a[i].fi; rep(i, 1, n) cin >> a[i].se; sort(a + 1, a + 1 + n); ll ans = 0; rep(i, 1, n) { ll x = a[i].se - a[i - 1].se, y = a[i].fi - a[i - 1].fi - x; //cout << x << ' ' << y << '\n'; if (a[i - 1].fi - a[i - 1].se & 1) ans += y + 1 >> 1; else ans += (y >> 1) + (y ? 0 : x); } cout << ans << '\n'; } return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--){ int n; cin >> n; vector<pair<int,int>> a(n); for(int i = 0; i < n; i++) cin >> a[i].first; for(int i = 0; i < n; i++) cin >> a[i].second; sort(a.begin(), a.end()); pair<int,int> prev = {1, 1}; int cost = 0; for(int i = 0; i < n; i++){ pair<int,int> cur = a[i]; int down, right; right = cur.second - prev.second; down = cur.first - prev.first - right; if(right == 0 && down == 0) continue; if(down == 0){ if((prev.first + prev.second) % 2 == 0) cost += right; } else{ cost += (down / 2); if(down & 1){ if((prev.first + prev.second) & 1) cost++; } } prev = cur; // cout << cur.first << ' ' << cur.second << ' ' << cost << ' '; } cout << cost << '\n'; } } /* 3 1 4 2 1 3 1 1,1 2,1 4,3 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 1,1 2,2 10,4 5 3 even - odd (0) odd - odd (0) even - even(1) odd - even (1) even ....... odd 2, 6 even - even odd - even / even - odd (6) odd - odd / even - even (2) */
//#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; void debug_out() { cerr << endl; } template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << "[" << H << "]"; debug_out(T...); } #ifdef dddxxz #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define SZ(s) ((int)s.size()) #define all(x) (x).begin(), (x).end() #define revall(x) (x).rbegin(), (x).rend() clock_t startTime; double getCurrentTime() { return (double) (clock() - startTime) / CLOCKS_PER_SEC; } typedef long long ll; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const double eps = 0.00001; const int MOD = 1e9 + 7; const int INF = 1000000101; const long long LLINF = 1223372000000000555; const int N = 1e6 + 3e2; const int M = 5555; int r[N], c[N]; void solve(int TC) { int n; cin >> n; r[0] = c[0] = 1; for (int i = 1; i <= n; i++) cin >> r[i]; for (int i = 1; i <= n; i++) cin >> c[i]; vector<int> perm; for (int i = 0; i <= n; i++) perm.push_back(i); sort(all(perm), [](int i, int j){ return r[i] < r[j]; }); ll ans = 0; int j = 0; for (int i : perm){ if (i == 0) continue; int k = 0; if (r[i] - c[i] == r[j] - c[j]){ if ((r[i] + c[i]) % 2 == 0) k = r[i] - r[j]; } else { int x = r[i] - c[i], y = r[j] - c[j]; k = (abs(x - y) + y % 2) / 2; } ans += k; // cout << k << ' '; j = i; } cout << ans << endl; } int main() { startTime = clock(); cin.tie(nullptr); cout.tie(nullptr); ios_base::sync_with_stdio(false); bool llololcal = false; #ifdef dddxxz freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); llololcal = true; #endif int TC = 1; cin >> TC; for (int test = 1; test <= TC; test++) { debug(test); solve(test); } if (llololcal) cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int line; cin >> line; while (line > 0) { int n = 0; cin >> n; int result = 0; vector<pair<int, int>> nodes(n); for (int i = 0; i < n; ++i) { cin >> nodes[i].first; } for (int i = 0; i < n; ++i) { cin >> nodes[i].second; } sort(nodes.begin(), nodes.end()); int x1 = 1, y1 = 1; for (int i = 0; i < n; ++i) { int x2 = nodes[i].first - x1; int y2 = nodes[i].second - y1; if ((x1 + y1) % 2 == 0) { if (x2 == y2) { result += x2; } else if (x2 != (y2 + 1)) { result += (x2 - y2) / 2; } } else { if (x2 != y2) { result += (x2 - y2 + 1) / 2; } } x1 = nodes[i].first; y1 = nodes[i].second; } cout << result << endl; --line; } return 0; }
#include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); typedef long long ll; const int N = 2e5+5; int n, m, k, t, mx; pair<int, int> p[N]; string s; int main() { IO; cin >> t; while(t--){ cin >> n; k = 0; for (int i = 0; i < n; ++i) cin >> p[i].first; for (int i = 0; i < n; ++i) cin >> p[i].second; sort(p, p+n); if(p[0].first == p[0].second) k = max(p[0].first - 1, k); else k = max(k, (p[0].first-p[0].second)/2); for(int i = 1; i < n; ++i){ int l = p[i].first-p[i].second, r = p[i-1].first-p[i-1].second; if(l == r && (p[i-1].first + p[i-1].second) % 2 == 0){ k += p[i].first - p[i-1].first; continue; } if((p[i-1].first + p[i-1].second) % 2 == 0) ++p[i-1].first; l = p[i].first-p[i].second, r = p[i-1].first-p[i-1].second; k += (l-r+1)/2; } cout << k << endl; } } /* 1 8 1 3 4 8 10 13 14 15 1 1 2 5 6 6 6 6 * */
#include <bits/stdc++.h> using namespace std; #define vi vector<int> #define vll vector<ll> #define vii vector<pair<int, int>> #define pii pair<int, int> #define pll pair<ll, ll> #define loop(_) for (int __ = 0; __ < (_); ++__) #define pb push_back #define f first #define s second #define sz(_) ((int)_.size()) #define all(_) _.begin(), _.end() #define lb lower_bound #define ub upper_bound using ll = long long; using ld = long double; const int N = 1e5 + 7; const ll mod = 1e9 + 7; int n; int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("in.in", "r", stdin); #endif int t; cin >> t; while (t--) { cin >> n; vii a(n); for (int i = 0; i < n; ++i) cin >> a[i].f; for (int i = 0; i < n; ++i) cin >> a[i].s; sort(all(a)); ll ans = 0; int curx = 1, cury = 1; for (auto u : a) { if (u.f == curx && u.s == cury) { continue; } bool f1 = (u.f - curx) & 1; bool f2 = (u.s - cury) & 1; bool f3 = (curx + cury) % 2 == 0; int difx = u.f - curx; int dify = u.s - cury; int mn = min(difx, dify); if (difx == dify) { if ((curx + cury) % 2 == 0) { ans += difx; } } else { curx += mn; cury += mn; int dif = u.f - curx; ans += dif / 2; if (dif & 1 && (curx + cury) % 2 == 1) ++ans; } curx = u.f; cury = u.s; } cout << ans << "\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; const int N=2e5+7; struct node{int r,c;}a[N]; int n,ans; bool cmp(node a,node b){return a.r<b.r;} int main() { int T;scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i].r); for(int i=1;i<=n;i++)scanf("%d",&a[i].c); sort(a+1,a+n+1,cmp); ans=0; int nr=1,nc=1; for(int i=1;i<=n;i++) if(a[i].r!=nr) { int tp=(nr+nc&1); if(!tp) { if(a[i].r-nr==a[i].c-nc)ans+=a[i].r-nr; else ans+=(a[i].r-nr-a[i].c+nc)/2; } else ans+=(a[i].r-nr-a[i].c+nc+1)/2; nr=a[i].r,nc=a[i].c; } printf("%d\n",ans); } }
#include <bits/stdc++.h> using namespace std; #define fix(f,n) std::fixed<<std::setprecision(n)<<f #define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); typedef long long int ll; typedef unsigned long long int ull; #define vi vector<int> #define pii pair<int,int> #define vii vector<pii> #define max(a,b) ((a>b)?a:b) #define min(a,b) ((a>b)?b:a) #define max3(a,b,c) ((a>b)?(a>c)?a:c:(b>c)?b:c) #define min3(a,b,c) ((a<b)?(a<c)?a:c:(b<c)?b:c) #define REP(i,a,n) for(ll i=a;i<n;i++) #define pb push_back #define mp make_pair #define mod 998244353 #define MAX 1000000001 int main(){ fast; int test=1; cin >> test; while(test--){ int n; cin >> n; ll ans=0; ll lx=1,ly=1; ll a[n]; for(int i=0 ; i<n ; i++){ cin >> a[i]; } vector<pair<ll,ll>> v(n); for(int i=0 ; i<n ; i++){ ll x; cin >> x; v[i]={a[i],x}; } sort(v.begin(),v.end()); for(int i=0 ; i<n ; i++){ ll x=v[i].first,y=v[i].second; if(x==1 && y==1){ continue; } if((lx+ly)%2){ ans+=(x-lx-y+ly+1)/2; }else{ if((x-lx)==(y-ly)){ ans+=(x-lx); }else{ ans+=(x-lx-y+ly)/2; } } lx=x; ly=y; //cout << ans << endl; } cout << ans << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define all(x) x.begin(), x.end() #define sortall(x) sort(all(x)) #define reverseall(x) reverse(all(x)) #define to_upper(x) transform(x.begin(), x.end(), x.begin(), ::toupper) #define to_lower(x) transform(x.begin(), x.end(), x.begin(), ::tolower) #define sz(x) (int)(x).size() #define rep(i, a, b) for(int i = a; i < (b); ++i) #define FOR(i,n) for(int i=0;i<n;i++) #define FORS(i,s,n) for(int i=s;i<n;i++) #define send {ios_base::sync_with_stdio(false);} #define help {cin.tie(NULL); cout.tie(NULL);} #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "= " << x << ", " << #y << "= " << y << endl #define clr(x) memset(x, 0, sizeof(x)) #define clrv(x, y) memset(x, y, sizeof(x)) #define gcd __gcd #define PI 3.1415926535897932384626 typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef priority_queue<int, vi, greater<int>> minpq; typedef priority_queue<ll, vi, greater<ll>> minpql; // DEBUG void __print(int x) {cout << x;} void __print(long x) {cout << x;} void __print(long long x) {cout << x;} void __print(unsigned x) {cout << x;} void __print(unsigned long x) {cout << x;} void __print(unsigned long long x) {cout << x;} void __print(float x) {cout << x;} void __print(double x) {cout << x;} void __print(long double x) {cout << x;} void __print(char x) {cout << '\'' << x << '\'';} void __print(const char *x) {cout << '\"' << x << '\"';} void __print(const string &x) {cout << '\"' << x << '\"';} void __print(bool x) {cout << (x ? "true" : "false");} template<typename T> void __print(const vector<T> &x) {cout << "{";for(int u69=0;u69<x.size();u69++){__print(x[u69]);cout << (u69+1==x.size()?"":",");};cout << "}";} template<typename T, typename V> void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << "}";} template<typename T> void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";} void _print() {cout << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cout << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif // const int mod = 1000000007; const double eps = 1e-14; void YES(bool t = 1) { const string YESNO[2] = {"NO", "YES"};cout << YESNO[t] << endl; } void Yes(bool t = 1) { const string YesNo[2] = {"No", "Yes"};cout << YesNo[t] << endl; } void yes(bool t = 1) { const string yesno[2] = {"no", "yes"};cout << yesno[t] << endl; } inline bool isPowerOfTwo(ll x) { return x && (!(x&(x-1))); } struct custom_hash { //Custom Hash_func for unordered_map /*OR DO mp.max_load_factor(0.25); mp.reserve(1024); */ static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; ll mpow(int b, int e, int modu=0) { ll result = 1,base = b, exp = e; if(modu) base %= mod; while (exp > 0) { if (exp & 1){ result = ((ll)result * base) ; if(modu) result %= mod; } base = ((ll)base * base); if(modu) base %= mod; exp >>= 1; } return result; } int64_t bpow(int b, int e){int64_t result = 1,base = b, exp = e;while(exp > 0){if(exp & 1){result = ((int64_t)result * base);}base = ((int64_t)base * base);exp >>= 1;}return result;} bool isPerfectSquare(long double x){long double sr = sqrt(x); return ((sr - floor(sr)) == 0);} bool isIn(string &s2, string &s1){if (s1.find(s2) != string::npos) return true;return false;} bool isSorted(vi &arr){for(int i=0;i<(int)arr.size()-1;i++) if(arr[i] > arr[i+1]) return false;return true;} /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */ int TEST_CASES = 1; void submain(){ int n; cin >> n; vl arr(n), brr(n); FOR(i,n){ cin >> arr[i]; } FOR(i,n){ cin >> brr[i]; } vector<pair<ll,ll>> crr(n); FOR(i,n){ crr[i] = {arr[i], brr[i]}; } sortall(crr); ll ans = 0; pair<ll,ll> curr = {1,1}; FOR(i,n){ ll gap = crr[i].first - crr[i].second; ll mygap = curr.first - curr.second; // debug(gap, mygap); if(gap == mygap){ if((gap&1) == 0){ ans += crr[i].first-curr.first; } }else{ if(mygap&1){ ans += ceil(double(crr[i].first-(curr.first+(crr[i].second-curr.second)))/2.0); }else{ ans += floor(double(crr[i].first-(curr.first+(crr[i].second-curr.second)))/2.0); } } // debug(crr[i], ans); curr = crr[i]; } cout << ans << '\n'; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif send help if(TEST_CASES){ int t; cin >> t; while(t--){ submain(); } }else{ submain(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define lli long long int #define ulli unsigned long long int #define Pi 3.14159265358979323846264338327950288419716939937510 #define print(x) cout<<endl<<"At line "<<x<<endl; int main(){ ios :: sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); //ifstream cin; ofstream cout; cin.open("input.txt"); cout.open("output.txt"); int T = 1; cin>>T; int cases = 0; while(T--){ cases += 1; int n; cin>>n; vector < pair <lli, lli> > points(n+1); for(int i=0; i<n; i++){ cin>>points[i].first; } for(int i=0; i<n; i++){ cin>>points[i].second; } points[n] = {1LL, 1LL}; sort(points.begin(), points.end()); lli result = 0; for(int i=0; i<n; i++){ lli d1 = points[i].first - points[i].second; lli d2 = points[i+1].first - points[i+1].second; lli d3 = d2 - d1; lli d = 0; if(d1&1){ d = (d3 + 1)/2; } else{ if(d1 == d2){ d = points[i+1].first-points[i].first; } else{ d = d3/2; } } result += d; } cout<<result<<endl; } }
#include<bits/stdc++.h> using namespace std; #define int long long signed main(){ int t,n;cin>>t; while(t--){ cin>>n; int r[n+1],c[n+1]; r[0]=1;c[0]=1; for(int i=1;i<=n;i++){cin>>r[i];} for(int i=1;i<=n;i++){cin>>c[i];} sort(r,r+n+1);sort(c,c+n+1); int cost=0,dr,dc; for(int i=0;i<n;i++){ dr=r[i+1]-r[i]; dc=c[i+1]-c[i]; if((r[i]+c[i])%2){ cost+=(dr-dc+1)/2; } else if(dr==dc)cost+=dr; else cost+=(dr-dc)/2; } cout<<cost<<'\n'; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; ll gcd(ll a, ll b) { for (; b; a %= b, swap(a, b)); return a; } int n; pll a[200001]; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i].first; for (int i = 0; i < n; i++) cin >> a[i].second; sort(a, a + n); ll cx = 1, cy = 1; ll ans = 0; for (int i = 0; i < n; i++) { ll nx = a[i].first, ny = a[i].second; ll cur = cx - cy; ll nxt = nx - ny; if (cur % 2 == 0) { if (cur == nxt) ans += ny - cy; else ans += (nxt - cur) / 2; } else { ans += (nxt - cur + 1) / 2; } cx = nx, cy = ny; } cout << ans << '\n'; } }
#include "bits/stdc++.h" using namespace std; #define IOS {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);} #define pb push_back #define ll long long #define ld long double #define all(c) (c).begin(),(c).end() #define sz(c) (int)(c.size()) #define mp make_pair #define mt make_tuple #define get(x,c) get<x>(c) #define trav(a,x) for(auto a=x.begin();a!=x.end();a++) #define rep(i,n) for(int i=0;i<n;i++) #define FOR(i,a,b) for(int i=a;i<=b;i++) #define FORR(i,b,a) for(int i=b;i>=a;i--) #define lb lower_bound #define ub upper_bound #define pll pair<ll,ll> #define vll vector<ll> #define vpll vector<pll> #define f first #define s second inline ll gcd(ll a,ll b) {if(b==0)return a;a%=b;return gcd(b,a);} inline ll max(ll a,ll b) {return((a>b)?a:b);} inline ll min(ll a,ll b) {return((a>b)?b:a);} ll power(ll x,ll ex); ll powermod(ll x,ll ex,ll md); const ll inf = 1e18 + 9; const ll mod = 1e9 + 7; const ld PI = acos(-1); const ld eps = 1e-9; const ll N = 1e5+11; ll calc(pll p1 , pll p2){ ll x1 = p1.f , y1 = p1.s; ll x2 = p2.f , y2 = p2.s; ll ans = 0; ll dl = abs(y1 - x1); ll dr = abs(y2 - x2); if(dl%2 == 0){ if(dl == dr) ans += abs(x1 - x2); else ans += abs(dl - dr)/2; } else ans += (abs(dl - dr) + 1)/2; return ans; } void solve(){ ll n; cin >> n; ll a[n] , b[n]; rep(i , n) cin >> a[i]; rep(i , n) cin >> b[i]; vpll v; rep(i , n) v.pb(mp(a[i] , b[i])); sort(all(v)); ll ans = 0; pll pt = mp(1 , 1); rep(i , n){ ans += calc(pt , v[i]); pt = v[i]; } cout << ans << "\n"; } int main(){ IOS; cout<<fixed<<setprecision(20); ll NTC=1; cin>>NTC; #ifdef PREPROCESS preprocess(); #endif #ifdef SIEVE sieve(); #endif #ifdef NCR factorial(); #endif ll PTC=0; while((PTC++)<NTC){ // cout<<"Case #"<<PTC<<":"<<' '; solve(); //cout<<"\n"; } //cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n"; } ll power(ll x,ll y){if(y==0) return 1;ll a=power(x,y/2);if(y%2==0) return a*a;else return x*a*a;} ll powermod(ll x,ll ex,ll md){ll ans=1ll;while(ex>0){if(ex&1ll) ans=(ans*x)%md; ex>>=1ll;x=(x*x)%md;}return ans;}
#include <bits/stdc++.h> #define int long long using pii=std::pair<int,int>; using namespace std; const int maxn = 2e5 + 5; int t, n, r[maxn], c[maxn]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> t; for(int cases = 0; cases < t; cases++) { cin >> n; for(int i = 0; i < n; i++) cin >> r[i]; for(int i = 0; i < n; i++) cin >> c[i]; vector<pii> order; order.push_back({1, 1}); for(int i = 0; i < n; i++) order.push_back({r[i], c[i]}); sort(order.begin(), order.end()); int ans = 0; for(int i = 1; i < order.size(); i++) { assert(order[i].first - order[i].second >= order[i - 1].first - order[i - 1].second); int down = (order[i].first - order[i - 1].first); int left = (order[i - 1].second + down - order[i].second); int curcost = 0; if(left == 0 && (order[i].first + order[i].second) % 2 == 0) curcost += down; if((order[i].first + order[i].second) % 2 == 1) curcost += left / 2; else curcost += (left + 1) / 2; ans += curcost; } cout << ans << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e5 + 10; typedef pair<int, int> pii; struct node{ int Max, Min; }; vector<int> a; int n; int b[maxn]; void solve(){ scanf("%d", &n); map<int, bool> mp; map<int, node> d; a.clear(); a.push_back(0); mp[0] = true; d[0] = {1, 1}; for(int i = 1; i <= n; i++){ scanf("%d", &b[i]); } int val; for(int i = 1; i <= n; i++){ scanf("%d", &val); if(!mp[b[i] - val]){ mp[b[i] - val] = true; a.push_back(b[i] - val); d[b[i] - val] = (node){b[i], b[i]}; } else{ int dis = b[i] - val; d[dis].Max = max(d[dis].Max, b[i]); d[dis].Min = min(d[dis].Min, b[i]); } } n = a.size(); sort(a.begin(), a.end()); ll ans = 0; int last = 0; for(int i = 0; i < n; i++){ if(!(a[i] & 1)){ ans += d[a[i]].Max - d[a[i]].Min; } int tmp = (a[i] & 1) ? a[i] - 1 : a[i]; ans += (tmp - last) / 2; //printf("tmp = %d last = %d\n", tmp, last); last = tmp; } printf("%lld\n", ans); return ; } int main(){ int T; scanf("%d", &T); while(T--){ solve(); } return 0; }
#include<bits/stdc++.h> #define first fi #define second se #define m_p make_pair #define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) using namespace std; typedef long long ll; typedef unsigned int uint; const double E_num = 0.57721566490153286060651209; const int maxn = 2e5 + 10; const int N = 5e5 + 10; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; int t; int n; struct node { int x,y; }a[maxn]; bool cmp(node a,node b) { return a.x < b.x; } int main() { IOS; cin >> t; while(t--) { cin >> n; for(int i = 1;i <= n; ++i) cin >> a[i].x; for(int i = 1;i <= n; ++i) cin >> a[i].y; sort(a + 1,a + 1 + n,cmp); ll ans= 0; int xx = 1; int yy = 1; //cout << ans << endl; for(int i = 1;i <= n; ++i) { if((xx + yy) % 2 == 0) { if(a[i].x - a[i].y == xx - yy) ans += a[i].x - xx; else if(((a[i].x - a[i].y) - (xx - yy))&1) ans += ((a[i].x - a[i].y) - (xx - yy) - 1) / 2; else ans += ((a[i].x - a[i].y) - (xx - yy)) / 2; } else { if(a[i].x - a[i].y == xx - yy) ans += 0; else if(((a[i].x - a[i].y) - (xx - yy))&1) ans += ((a[i].x - a[i].y) - (xx - yy) + 1) / 2; else ans += ((a[i].x - a[i].y) - (xx - yy)) / 2; } xx = a[i].x; yy = a[i].y; } cout << ans << endl; } return 0; }
#include<bits/stdc++.h> #define fi first #define se second #define pb push_back #define pf push_front #define popb pop_back #define popf pop_front #define ins insert #define pq priority_queue #define minele min_element #define maxele max_element #define lb lower_bound //first pos >= val #define ub upper_bound // first pos > val #define cnt_bit __builtin_popcount #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " //#pragma GCC optimize("Ofast") //#pragma GCC target("avx,avx2,fma") using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1}; int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1}; int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll oo = 1e18; const ll maxN = 1e6; /* Author : Le Ngoc Bao Anh, 10A5, LQD High School for Gifted Student */ void maximize(int &a, int b) { a = max(a, b); } void minimize(int &a, int b) { a = min(a, b); } struct Data { ll r, c; } a[maxN]; void solve() { int n; ll ans = 0; cin >> n; for(int i = 1; i <= n; i++) cin >> a[i].r; for(int i = 1; i <= n; i++) cin >> a[i].c; sort(a + 1, a + 1 + n, [&](Data a_, Data b_) {return a_.r < b_.r;}); int r = 1, c = 1; for(int i = 1; i <= n; i++) { ll needr = a[i].r - r, needc = a[i].c - c; //needr >= needc if(!needr && !needc) continue; //after 1 move, r always increase, r + c is always odd if((r + c) % 2 == 0) { if(needr == needc) { ans += needr; r = a[i].r, c = a[i].c; continue; } r++; needr--; ans += (needr - needc + 1) / 2; } else { ans += (needr - needc + 1) / 2; } r = a[i].r, c = a[i].c; } cout << ans << endl; } /* 6 3 4 7 10 11 12 3 4 6 9 9 10 */ int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("input.inp", "r", stdin); ll tc, ddd = 0; cin >> tc; while(tc--) { //ddd++; //cout << "Case #" << ddd << ": "; solve(); } }
#include<bits/stdc++.h> using namespace std; void __print(int x){cerr << x;} void __print(long x){cerr << x;} void __print(long long x){cerr << x;} void __print(unsigned x){cerr << x;} void __print(unsigned long x){cerr << x;} void __print(unsigned long long x){cerr << x;} void __print(float x){cerr << x;} void __print(double x){cerr << x;} void __print(long double x){cerr << x;} void __print(char x){cerr << '\'' << x << '\'';} void __print(const char *x){cerr << '\"' << x << '\"';} void __print(const string &x){cerr << '\"' << x << '\"';} void __print(bool x){cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x){cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x){int f = 0; cerr << '{'; for(auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print(){cerr << "]\n";} template<typename T, typename... V> void _print(T t, V... v){__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif typedef long long ll; typedef long double ld; typedef pair<int,int>pii; typedef pair<ll, ll>pll; const int M = 2e5 + 3; const int MOD = 1e9 + 7; void solve(){ int n; cin >> n; vector<pii>v; int rr[n], cc[n]; for(int i=0; i<n; i++) cin >> rr[i]; for(int i=0; i<n; i++){ cin >> cc[i]; if(rr[i] != 1) v.push_back({rr[i], rr[i] - cc[i]}); } v.push_back({1, 0}); sort(v.begin(), v.end()); int ans = 0; for(int i=0; i+1<v.size(); i++){ int r1 = v[i].first, x1 = v[i].second; int r2 = v[i+1].first, x2 = v[i+1].second; assert(x2 >= x1); ans += (x2 - x1 + x1 % 2) / 2; r1 += x2 - x1; if(x1 == x2 and x2 % 2 == 0) ans += r2 - r1; } cout << ans << "\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tc; cin >> tc; for(int i=1; i<=tc; i++){ solve(); } }
#include <bits/stdc++.h> #define PB push_back #define MP make_pair using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++ i) #define rep1(i, n) for (int i = 1; i < (int)(n); ++ i) #define foreach(itr, c) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++ itr) typedef long long LL; typedef pair<int, int> pii; const int N = 2e5 + 5; int T, n; pii a[N]; LL solve(pii s, pii t){ int sr = s.first, sc = s.second; int tr = t.first, tc = t.second; if (sr - sc == tr - tc){ if ((sr + sc) & 1) return 0; else return tr - sr; } tr -= tc - sc; tc = sc; int dist = tr - sr; if (dist & 1){ if ((sr + sc) & 1) return dist / 2 + 1; else return dist / 2; } return dist / 2; } int main(){ scanf("%d", &T); while (T --){ scanf("%d", &n); rep(i, n) scanf("%d", &a[i].first); rep(i, n) scanf("%d", &a[i].second); sort(a, a + n); pii cur(1, 1); LL ret = 0; rep(i, n){ ret += solve(cur, a[i]); cur = a[i]; } printf("%lld\n", ret); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; vector<pair<int, int>> p(n); for (int i = 0; i < n; i++) { cin >> p[i].first; } for (int i = 0; i < n; i++) { cin >> p[i].second; } sort(p.begin(), p.end()); function<int(int, int, int, int)> dis = [&dis](int x1, int y1, int x2, int y2) -> int { if (x1 == x2 && y1 == y2) { return 0; } if ((x1 + y1) % 2 == 0) { if (x1 - x2 == y1 - y2) { return x2 - x1; } return dis(x1 + 1, y1, x2, y2); } int d = x2 - (x1 + y2 - y1); if (d % 2 == 0) { return d / 2; } return (d + 1) / 2; }; int ans = 0; int x = 1, y = 1; for (auto [f, s] : p) { ans += dis(x, y, f, s); // cout << ans << ' '; x = f; y = s; } cout << ans << '\n'; } }
// 🅺🅴🅴🅿 🅸🆃 🆂🅸🅼🅿🅻🅴 #include <bits/stdc++.h> using namespace std; #define endl "\n" #define Br cout<<endl #define int long long #define fe first #define se second #define double long double #define all(v) v.begin(),v.end() #define allr(v) v.rbegin(),v.rend() #define PT(x) cout << (x) << endl #define last(x) prev(x.end()) #define PP(x) cout << (x) << " " #define PS(x) for(auto it: x)cout << it <<" "; cout << endl; #define PTT(x) cout << (#x) << " is " << (x) << endl #define lets_go ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; void PV(vi v) { for (int i = 0 ; i < (int)v.size() ; i++) cout << v[i] << " " ; cout << endl;} void PVV(vii v) {for (int i = 0 ; i < (int)v.size() ; i++) cout << v[i].first << " " << v[i].second << endl;} void PA(int v[], int n, int x = 0) { for (int i = x ; i < n + x; i++)cout << v[i] << ' ' ; cout << endl;} void IN(int a[], int n, int x = 0) {for (int i = x; i < n + x; i++)cin >> a[i];} inline void op() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } // <================================================================================================================================= // <================================================================================================================================= const int M = 1e9 + 7; const int IM = 1e18 + 37; const int N = 2e5 + 10; ii p[N]; int m(int x, int y, int a, int b) { int ans = 0; int d = (x - y) - (a - b); ans += d / 2 + (d % 2 ? (a + b) % 2 : 0); a += d; if(d > x || d < 0) return IM; if ((a - b) % 2 == 0) { ans += (x - a); } // PV({ans,d}); return ans; } signed main() { lets_go op(); int t; cin >> t; p[0] = {1,1}; while (t--) { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> p[i].fe; for (int i = 1; i <= n; i++) cin >> p[i].se; sort(p + 1, p + n + 1); // for (int i = 1; i <= n; i++) // cout << p[i].fe << " " << p[i].se << endl; // Br; int ans = 0; for (int i = 1; i <= n; i++) { int x, y, a, b, tm = 0; tie(a, b) = p[i - 1]; tie(x, y) = p[i]; tm = m(x, y, a, b); if (x != a + 1) { tm = min(tm, m(x - 1, y, a, b) + (x + y - 1) % 2); } ans += tm; // PV({i, x, y, tm}); } PT(ans); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+10; int t,n; struct node { int x,y; }a[maxn]; bool cmp(node x,node y) {return x.x<y.x;} int main() { ios::sync_with_stdio(false);cin.tie(0); cin>>t; while (t--) { cin>>n; for (int i=1;i<=n;i++) cin>>a[i].x; for (int i=1;i<=n;i++) cin>>a[i].y; sort(a+1,a+n+1,cmp); int ans=0; a[0].x=1;a[0].y=1; int nx=1,ny=1; for (int i=0;i<n;i++) { if (a[i+1].y-a[i].y==a[i+1].x-a[i].x) { if (!((a[i].x+a[i].y)&1)) ans+=a[i+1].x-a[i].x; nx=a[i+1].x;ny=a[i+1].y; continue; } if (!((a[i].x+a[i].y)&1)) nx++; nx+=(a[i+1].y-a[i].y);ny=a[i+1].y; if (!((a[i+1].x-nx)&1)) { ans+=(a[i+1].x-nx)/2; nx=a[i+1].x;ny=a[i+1].y; continue; } else { if ((nx+ny)&1) ans++; ans+=(a[i+1].x-nx)/2; nx=a[i+1].x;ny=a[i+1].y; continue; } } cout<<ans<<endl; } return 0; }
#include<bits/stdc++.h> using namespace std; const int N = 4e5 + 6; typedef long long ll; struct node{int l,r;bool operator<(const node & y)const{ return l < y.l; }}a[N]; int main(){ int t;scanf("%d",&t); while(t--){ int n; scanf("%d",&n); for(int i = 1;i <= n;i++){ int x;scanf("%d",&x); a[i].l = x; }for(int i = 1;i <= n;i++){ int x;scanf("%d",&x); a[i].r = x; } sort(a+1,a+1+n); int sx = 1,sy = 1;ll ans = 0; for(int i = 1;i <= n;i++){ if(a[i].l == sx && a[i].r == sy) continue; int cx = a[i].l - sx,cy = a[i].r - sy; if(cx == cy){ if((sx+sy) % 2 == 0) ans += cx; }else{ if(cy == 0) { if((sx+sy) % 2 == 0) ans += cx/2; else ans += (cx + 1)/2; } else if(cx - cy == 1){ if((sx+sy) % 2 != 0) ans ++; }else{ int now; if((sx + sy) % 2 == 0){ now = sx + cy + 1; }else{ now = sx+cy; } ans += (a[i].l - now + 1)/2; } } sx = a[i].l,sy = a[i].r; //printf("%d %d %d\n",sx,sy,ans); } cout<<ans<<endl; } return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <array> #include <unordered_set> #include <unordered_map> #include <set> #include <map> #include <math.h> #include <cassert> using namespace std; typedef long long ll; #define f first #define s second const int NMX = 2e5+5; pair<int, int> a[NMX]; void task() { int n; cin >> n; for(int i = 0; i < n; i++) cin >> a[i].s; for(int i = 0; i < n; i++) cin >> a[i].f; sort(a, a + n); ll cost[2] = {0, 0}; int lastRow = 1, lastCol = 1; for(int i = 0; i < n; i++) { int r = a[i].s, c = a[i].f; //cout << "{" << r << ", " << c << "}\n"; if(r == c) { cost[1] = 1LL * r - 1; continue; } int curRow = r - c + 1; if(curRow <= lastRow) { cost[0] += 1LL * (curRow % 2) * (c - lastCol); } else { cost[0] += 1LL * (curRow - lastRow + (curRow % 2)) / 2; lastRow = curRow + (curRow % 2); } lastCol = c; //cout << lastRow << " " << cost[0] << "\n"; } cout << cost[0] + cost[1] << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--) { task(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define END '\n' #define int long long #define pb push_back #define pii pair<int, int> #define ff first #define ss second #define bug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define loop(i, a, b) for(int i = (a); i < (b); i++) #define loopb(i, b, a) for(int i = (b); i > (a); --i) const int mod = 1e9+7; const int mod1 = 998244353; const int inf = 1e18; const int nax = 200005; struct pt { int y, x; }a[nax]; void solve() { int n; cin>>n; for(int i = 1; i <= n; i++) cin>>a[i].y; for(int i = 1; i <= n; i++) cin>>a[i].x; a[0].y = 1, a[0].x = 1; int ans = 0; sort(a, a + n + 1, [](const pt A, const pt B) { return A.y < B.y; }); for(int i = 1; i <= n; i++) { int diff_2 = a[i].y - a[i].x; int diff_1 = a[i - 1].y - a[i - 1].x; int diff = diff_2 - diff_1; int now = 0; if(diff == 0) { if(diff_2 % 2 == 0) now += a[i].y - a[i - 1].y; } else { if(diff_2 % 2) now += diff/2; else { diff--; now += 1 + diff/2; } } ans += now; } cout<<ans<<END; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); freopen("error.txt","w",stderr); #endif int t = 1; cin>>t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--) #define rrep(i,n) RREP(i,(n)-1,0) #define all(v) v.begin(), v.end() #define endk '\n' const int inf = 1e9+7; const ll longinf = 1LL<<60; const ll mod = 1e9+7; const ld eps = 1e-10; template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;} template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;} void solve() { int n; cin >> n; vector<ll> R(n); rep(i, n) cin >> R[i]; vector<ll> C(n); rep(i, n) cin >> C[i]; map<ll, set<int>> cnt; cnt[0].insert(1); rep(i, n) cnt[R[i]-C[i]].insert(R[i]); ll ans = cnt.rbegin()->first / 2; for(auto [d, st]: cnt) if(d % 2 == 0) ans += *st.rbegin() - *st.begin(); cout << ans << endk; } int main() { cin.tie(0); ios::sync_with_stdio(false); int T; cin >> T; while(T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define gap " " #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define pb push_back #define mp make_pair #define ff first #define ss second #define ok cout<<"ok"<<endl #define DBG(a) cerr<< "Line "<<__LINE__ <<" : "<< #a <<" = "<<(a)<<endl #define fastio {ios_base::sync_with_stdio(false);cin.tie(NULL);} typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; const int N=200009; int X[N],Y[N]; int main() { int t,n,i; scanf("%d",&t); while(t--){ vector<pii> vec; scanf("%d",&n); for(i=1; i<=n; i++) scanf("%d",&X[i]); for(i=1; i<=n; i++) scanf("%d",&Y[i]); for(i=1; i<=n; i++){ vec.pb({X[i],Y[i]}); } sort(vec.begin(),vec.end()); int mncost=0; pii pre,cur; pre.ff=1; pre.ss=1; for(i=0; i<n; i++){ cur=vec[i]; if(cur.ff-cur.ss == pre.ff-pre.ss){ int dif=cur.ff-pre.ff; if((pre.ff+pre.ss)%2==0) mncost+=dif; } else { if((pre.ff+pre.ss)%2==0) pre.ff++; int dif=cur.ss-pre.ss; pre.ff+=dif; pre.ss=cur.ss; dif=cur.ff-pre.ff; mncost+=(dif/2); if(dif%2==1 && (pre.ff+pre.ss)%2==1) mncost++; } pre=cur; } printf("%d\n",mncost); } return 0; }
#include<bits/stdc++.h> using namespace std; #define MOD 1000000007 #define ll unsigned long long #define mp make_pair #define ff first #define ss second #define pb push_back #define INF 1e18 #define pr 998244353 #define endl "\n" #define pi 3.14159265 int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin>>t; while(t--) { ll n; cin>>n; vector <pair<ll,ll>> v(n); for(int i=0;i<n;i++)cin>>v[i].ff; for(int i=0;i<n;i++)cin>>v[i].ss; sort(v.begin(),v.end()); ll ans=0; ll r=1,c=1; for(int i=0;i<n;i++) { ll rnext=v[i].ff; ll cnect=v[i].ss; if((r+c)%2==0) { ll diffx=rnext-r; ll diffy=cnect-c; if(diffx==diffy) { ans+=diffx; } else { diffx-=1; ll x=min(diffx,diffy); diffx-=x; diffy-=x; ans+=(diffx+1)/2; } } else { ll diffx=rnext-r; ll diffy=cnect-c; ll x=min(diffx,diffy); diffx-=x; diffy-=x; ans+=(diffx+1)/2; } r=rnext; c=cnect; } cout<<ans<<endl; } }
#include<bits/stdc++.h> #pragma GCC optimize(2) #define ll long long #define rep(i,a,n) for(int i=a;i<=n;i++) #define per(i,n,a) for(int i=n;i>=a;i--) #define endl '\n' #define eps 0.000000001 #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) #define IO ios::sync_with_stdio(false);cin.tie(0); using namespace std; const int INF=0x3f3f3f3f; const ll inf=0x3f3f3f3f3f3f3f3f; const int mod=1e9+7; const int maxn=2e5+5; int a[maxn],b[maxn]; pair<int,int> p[maxn]; bool cmp(pair<int,int> a,pair<int,int> b){ return a.first<b.first; } int main(){ int T;scanf("%d",&T); while(T--){ map<int,int> mp; int n;scanf("%d",&n); rep(i,1,n) scanf("%d",&a[i]); rep(i,1,n) scanf("%d",&b[i]); rep(i,1,n) p[i].first=a[i],p[i].second=b[i]; sort(p+1,p+n+1,cmp); int prex=1,prey=1,precha=0; ll sum=0; rep(i,1,n){ int cha=p[i].first-p[i].second; if(cha%2==0){ if(cha==precha){ sum+=p[i].first-prex; }else{ sum+=(cha/2-precha/2); } }else{ if(cha!=precha){ sum+=(cha/2-precha/2); }else{ sum+=0; } } prex=p[i].first; prey=p[i].second; precha=prex-prey; } cout<<sum<<endl; } }
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while (t--) { ll n; cin>>n; ll r[n]; vector<pair<ll, ll>> path(n+1); path[0] = {1, 1}; for (int i = 0; i < n; i++) { cin>>r[i]; } for (int i = 1; i <= n; i++) { ll c; cin>>c; path[i] = {r[i-1], c}; } sort(path.begin(), path.end()); ll cost = 0; for (int i = 1; i < path.size(); i++) { ll left = (path[i].first - path[i].second) - (path[i-1].first - path[i-1].second); ll free = path[i-1].first+path[i-1].second; if (free%2 == 0 && left == 0) { cost += path[i].first - path[i-1].first; } else if (free%2 == 1 && left == 0) { cost += 0; } else if (free%2 == 0) { cost += left/2; } else { cost += (left+1)/2; } } cout<<cost<<"\n"; } }
// ,;;;;;;, // ,;;;'""`;;\ // ,;;;/ .'`',;\ // ,;;;;/ | \|_ // /;;;;; \ / .\ // ,;;;;;;| '. \/_/ // /;;;;;;;| \ // _,.---._ /;;;;;;;;| ; _.---.,_ // .;;/ `.;;;;;;;;;| ;' \;;, // .;;;/ `;;;;;;;;;.._ .' \;;;. // /;;;;| _;-"` `"-;_ |;;;;\ // |;;;;;|.---. .' __.-"```"-.__ '. .---.|;;;;;| // |;;;;;| `\/ .'/__\ /__\'. \/` |;;;;;| // |;;;;;| |_/ // \ // \ \_| |;;;;;| // |;;;;;| |/ |/ || || \| \| |;;;;;| // \;;;;| __ || _ .-.\| |/.-. _ || __ |;;;;/ // \jgs| / _\|/ = /_o_\ /_o_\ = \|/_ \ |;;;/ // \;;/ |`.- ` ` ` ` -.`| \;;/ // _|;' \ | _ _ _ _ | / ';|_ // / .\ \_ ( '--'( )'--' ) _// /. \ // \/_/ \_/| /_ | | _\ |\_/ \_\/ // | /|\ \ / //|\ | // | | \'._'-'_.'/ | | // | ; '-.```.-' ; | // | \ ``` / | // __ ; '.-"""""-.' ; __ // /\ \_ __..--\ `-----' /--..__ _/ /\ // \_'/\`''---''`..;;;;.'.__, ,__.',;;;;..`''---''`/\'_/ // '-.__'';;;;;;;;;;;,,'._ _.',,;;;;;;;;;;;''__.-' // ``''--; ;;;;;;;;..`"`..;;;;;;;; ;--''`` _ // .-. /,;;;;;;;';;;;;;;;;';;;;;;;,\ _.-' `\ // .' /_ /,;;;;;;'/| ;;;;;;; |\';;;;;;,\ `\ '-'| // / ) /,;;;;;',' | ;;;;;;; | ',';;;;;,\ \ .'-./ // `'-..-' /,;;;;',' | ;;;;;;; | ',';;;;,\ `"` // | ;;;',' | ;;;;;;; | , ', ;;;'| // _\__.-' .-. ; ;;;;;;; ; |'-. '-.__/_ // / .\ ( ) \';;;;;'/ | | /. \ // \/_/ (` `) \';;;'/ '-._| \_\/ // '-/ \-' '._.' ` // """ /.`\ // \|_/ #include<bits/stdc++.h> #define pb push_back #define mk make_pair #define ll long long #define ss second #define ff first #define pll pair<ll,ll> #define vll vector<ll> #define mll map<ll,ll> #define mod 1000000007 #define sp " " #define w(x) ll x; cin>>x; while(x--) #define ps(x,y) fixed<<setprecision(y)<<x; #define fo(i, j, k, in) for (ll i=j ; i<k ; i+=in) #define re(i, j) fo(i, 0, j, 1) #define pi 3.1415926535897932384626433832795 #define all(cont) cont.begin(), cont.end() #define countbit(x) __builtin_popcount(x) #define mod 1000000007 #define lo lower_bound #define de(n) ll n;cin>>n; #define def(a,n) ll n;cin>>n;ll a[n];re(i,n){cin>>a[i];} #define defi(a,n,k) ll n;cin>>n; ll k;cin>>k;ll a[n];re(i,n){cin>>a[i];} #define deb(x) cout<<#x<<"="<<x<<endl; #define tr(it,a) for(auto it=a.begin();it!=a.end();it++) #define nl cout<<endl; #define precision(x) cout << fixed << setprecision(x); #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) ((a)*(b)) / gcd((a),(b)) #define endl "\n" using namespace std; //KnightMareVoid int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin>>t; while(t--){ int n; cin>>n; vector<pll> a(n); for(int i=0;i<n;i++){ int s; cin>>s; a[i].first=s; } for(int i=0;i<n;i++){ int s; cin>>s; a[i].second=s; } ll c=0; sort(all(a)); int x=1,y=1; for(int i=0;i<n;i++){ if(x==a[i].first && y==a[i].second)continue; int r=a[i].second-y; int l=a[i].first-x-r; if((x+y)%2==0){ if(l>0){ l--; if(l>0){ c+=(l/2); if(l%2==1){ c++; } } } else{ c+=r; } } else{ if(l>0){ c+=(l/2); if(l%2==1)c++; } } //cout<<c<<endl; x=a[i].first ; y=a[i].second; } cout<<c<<endl; } return 0; }
#include <iostream> #include <vector> #include <cstring> #include <algorithm> #include <cmath> #define forn(i, n) for(int i = 0; i < int(n); ++i) using namespace std; using ll = long long; void solve(){ int n; cin >> n; vector<pair<int ,int>> points(n); for (int i = 0; i < n; ++i) { cin >> points[i].first; } for (int i = 0; i < n; ++i) { cin >> points[i].second; } sort(points.begin(), points.end()); if (points[0] != make_pair(1, 1)){ points.insert(points.begin(), make_pair(1, 1)); n++; } long long ans = 0; for (int i = 0; i < n - 1; ++i) { pair<int, int> a = points[i]; pair <int, int> b = points[i + 1]; if (a.first - a.second == b.first - b.second){ if ((a.first - a.second) % 2 == 0){ ans += b.first - a.first; } } else{ ans += (b.first - b.second) / 2 - (a.first - a.second) / 2; } } cout << ans << '\n'; } int main(){ ios::sync_with_stdio(false); int T; cin >> T; while (T--){ solve(); } return 0; }