solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
long long exp(long long n, long long r, long long md) {
if (r == 0) {
return 1;
}
long long ans = exp(n, r / 2, md);
ans *= ans;
ans %= md;
if (r & 1) {
ans *= n;
ans %= md;
}
return ans % md;
}
long long choose(long long n, long long r, long long md) {
long long ans = 1;
for (int i = n - r + 1; i < n + 1; ++i) {
ans *= i;
ans %= md;
}
for (int i = 1; i < r + 1; ++i) {
ans *= exp(i, md - 2, md);
ans %= md;
}
return ans % md;
}
int mat[107][107] = {0};
int main() {
int n, m, q, l = 0;
cin >> n >> m >> q;
vector<pair<int, int>> pr;
for (int i = 0; i < q; ++i) {
int p;
cin >> p;
int d, x, y, v;
if (p > 2) {
cin >> x >> y >> v;
mat[x][y] = v;
} else {
cin >> d;
pr.push_back(make_pair(p, d));
if (p == 1) {
int temp = mat[d][1];
for (int i = 1; i < m; ++i) {
mat[d][i] = mat[d][i + 1];
}
mat[d][m] = temp;
} else {
int temp = mat[1][d];
for (int i = 1; i < n; ++i) {
mat[i][d] = mat[i + 1][d];
}
mat[n][d] = temp;
}
}
}
for (int i = pr.size() - 1; i >= 0; --i) {
if (pr[i].first == 1) {
int temp = mat[pr[i].second][m];
for (int j = m; j > 1; --j) {
mat[pr[i].second][j] = mat[pr[i].second][j - 1];
}
mat[pr[i].second][1] = temp;
} else {
int temp = mat[n][pr[i].second];
for (int j = n; j > 1; --j) {
mat[j][pr[i].second] = mat[j - 1][pr[i].second];
}
mat[1][pr[i].second] = temp;
}
}
for (int i = 1; i < n + 1; ++i) {
for (int j = 1; j < m + 1; ++j) {
cout << mat[i][j] << " ";
}
cout << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int dcmp(double a, double b) {
return ((fabs(a - b) < (1e-9)) ? 0 : (a < b) ? -1 : +1);
}
double crossP(const complex<double> &v1, const complex<double> &v2) {
return (conj(v1) * v2).imag();
}
double dotP(const complex<double> &v1, const complex<double> &v2) {
return (conj(v1) * v2).real();
}
double lengthSqr(const complex<double> &p) { return dotP(p, p); }
double length(const complex<double> &p) { return hypot(p.real(), p.imag()); }
double fixAngle(double a) { return ((a > 1) ? 1 : (a < -1) ? -1 : a); }
bool same(const complex<double> &p1, const complex<double> &p2) {
return ((dcmp(p1.real(), p2.real()) == 0 && dcmp(p1.imag(), p2.imag()) == 0)
? 1
: 0);
}
double getAngle_A_abc(double a, double b, double c) {
return acos(fixAngle((b * b + c * c - a * a) / (2 * b * c)));
}
const int N = 2 * 60 + 9, sep = 60;
map<double, bitset<N>> m;
vector<int> yls, yrs;
int f() {
int mx = 0;
for (const auto &kb1 : m)
for (const auto &kb2 : m)
mx = max(mx, (int)(kb1.second | kb2.second).count());
return mx;
}
int main() {
int n1, n2;
scanf("%d %d", &n1, &n2);
yls.resize(n1);
for (__typeof(n1) i = 0; i < n1; ++i) scanf("%d", &yls[i]);
yrs.resize(n2);
for (__typeof(n2) i = 0; i < n2; ++i) scanf("%d", &yrs[i]);
for (__typeof(n1) i = 0; i < n1; ++i)
for (__typeof(n2) j = 0; j < n2; ++j) {
double ym = (yls[i] + yrs[j]) / 2.;
m[ym][i] = 1;
m[ym][sep + j] = 1;
}
printf("%d\n", f());
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
long long arr[100003];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", &arr[i]);
if (n == 1 || n == 2)
printf("0\n");
else {
long long res = INF;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
long long a = arr[0] + i;
long long b = (arr[1] + j) - (arr[0] + i);
long long cnt = abs(i) + abs(j);
for (int k = 2; k < n; k++) {
if (abs(a + (k * b) - arr[k]) > 1)
cnt = INF;
else
cnt += abs(a + (k * b) - arr[k]);
}
res = min(res, cnt);
}
}
if (res <= 1e9)
printf("%lld\n", res);
else
printf("-1\n");
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int c = 1, vis[1000005], in[1000005], out[1000005], val[1000005],
sum = 0;
vector<long long int> v[1000005];
void dfs(long long int src) {
vis[src] = 1;
in[src] = c++;
for (long long int i = 0; i < v[src].size(); i++) {
if (vis[v[src][i]] != 1) {
dfs(v[src][i]);
val[src] += val[v[src][i]];
}
}
out[src] = c;
}
long long int flag1 = 0;
long long int flag2 = 0;
long long int ans1;
long long int par = -1;
long long int ans2;
void dfs_1(long long int src) {
vis[src] = 1;
if (flag1 == 1 && in[src] > in[par] && out[src] <= out[par] &&
val[src] == sum / 3) {
flag2 = 1;
ans1 = src;
ans2 = par;
}
if (val[src] == 2 * (sum / 3)) {
flag1 = 1;
par = src;
}
for (long long int i = 0; i < v[src].size(); i++) {
if (vis[v[src][i]] != 1) {
dfs_1(v[src][i]);
}
}
}
void dfs_2(long long int src) {
vis[src] = 1;
if (flag1 == 1 && !(in[src] > in[par] && out[src] <= out[par]) &&
val[src] == sum / 3) {
flag2 = 1;
ans1 = src;
ans2 = par;
}
if (val[src] == sum / 3) {
flag1 = 1;
par = src;
}
for (long long int i = 0; i < v[src].size(); i++) {
if (vis[v[src][i]] != 1) {
dfs_2(v[src][i]);
}
}
}
int main() {
long long int n, i, x, root;
scanf("%lld", &n);
for (i = 1; i <= n; i++) {
scanf("%lld %lld", &x, &val[i]);
sum += val[i];
if (x != 0) {
v[x].push_back(i);
v[i].push_back(x);
} else {
root = i;
}
}
if (sum % 3 != 0 || sum == 0) {
printf("-1\n");
return 0;
}
for (i = 1; i <= n; i++) {
vis[i] = 0;
}
dfs(root);
for (i = 1; i <= n; i++) {
vis[i] = 0;
}
dfs_1(root);
if (flag2 == 1) {
printf("%lld %lld\n", ans1, ans2);
return 0;
}
flag1 = 0;
flag2 = 0;
ans1;
ans2;
for (i = 1; i <= n; i++) {
vis[i] = 0;
}
dfs_2(root);
if (flag2 == 1) {
printf("%lld %lld\n", ans1, ans2);
return 0;
} else {
printf("-1\n");
return 0;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 11, inf = 1111111111;
int ans;
string s;
set<string> q;
string pow1(string s) {
string res = s;
res[0] = s[3];
res[1] = s[0];
res[2] = s[1];
res[3] = s[2];
return res;
}
string pow2(string s) {
string res = s;
res[3] = s[4];
res[5] = s[3];
res[1] = s[5];
res[4] = s[1];
return res;
}
string pow3(string s) {
string res = s;
res[5] = s[2];
res[2] = s[4];
res[4] = s[0];
res[0] = s[5];
return res;
}
int main() {
cin >> s;
sort(s.begin(), s.end());
do {
q.insert(s);
} while (next_permutation(s.begin(), s.end()));
while (!q.empty()) {
++ans;
string a = *q.begin();
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
q.erase(a);
a = pow1(a);
}
a = pow2(a);
}
a = pow3(a);
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long n, m;
cin >> n >> m;
long long x[n + m], t[n + m];
for (int i = 0; i < n + m; i++) cin >> x[i];
for (int i = 0; i < n + m; i++) cin >> t[i];
vector<long long> v;
map<long long, long long> mp;
for (int i = 0; i < n + m; i++)
if (t[i] == 1) {
v.push_back(x[i]);
mp[x[i]] = i;
}
long long freq[200010] = {0};
for (int i = 0; i < n + m; i++) {
if (t[i] == 0) {
auto it1 = lower_bound(v.begin(), v.end(), x[i]);
it1--;
auto it2 = upper_bound(v.begin(), v.end(), x[i]);
if (fabs(*it1 - x[i]) <= fabs(*it2 - x[i])) {
freq[mp[*it1]]++;
} else {
freq[mp[*it2]]++;
}
}
}
for (int i = 0; i < n + m; i++) {
if (t[i] == 1) {
cout << freq[i] << " ";
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
long long h[MAXN], th[MAXN];
int nxt[MAXN];
struct data {
int len, loc;
data() {}
data(int _len, int _loc) : len(_len), loc(_loc) {}
data operator+(const data &t) const { return len >= t.len ? *this : t; }
};
struct node {
int l, r, m;
data v;
} s[MAXN << 2];
void push_up(int n) { s[n].v = s[n << 1].v + s[n << 1 | 1].v; }
void build(int l, int r, int n) {
int m = (l + r) >> 1;
s[n].l = l;
s[n].r = r;
s[n].m = m;
if (r - l == 1) {
s[n].v = data(0, 0);
return;
}
build(l, m, n << 1);
build(m, r, n << 1 | 1);
push_up(n);
}
void update(int p, data v, int n) {
if (s[n].l == p && s[n].r == p + 1) {
s[n].v = s[n].v + v;
return;
}
if (p < s[n].m)
update(p, v, n << 1);
else
update(p, v, n << 1 | 1);
push_up(n);
}
data query(int l, int r, int n) {
if (r <= l) return data(0, 0);
if (s[n].l == l && s[n].r == r) return s[n].v;
if (r <= s[n].m) return query(l, r, n << 1);
if (l >= s[n].m) return query(l, r, n << 1 | 1);
return query(l, s[n].m, n << 1) + query(s[n].m, r, n << 1 | 1);
}
int main() {
int n;
long long d;
scanf("%d%I64d", &n, &d);
for (int i = 1; i <= n; i++) scanf("%I64d", &h[i]);
memcpy(th, h + 1, n * sizeof(long long));
sort(th, th + n);
int tot = unique(th, th + n) - th;
build(1, tot + 1, 1);
int res = 0, st = 0;
for (int i = n; i >= 1; i--) {
int l = upper_bound(th, th + tot, h[i] - d) - th;
int r = lower_bound(th, th + tot, h[i] + d) - th + 1;
data t = query(1, l + 1, 1) + query(r, tot + 1, 1);
nxt[i] = t.loc;
if (res < t.len + 1) {
res = t.len + 1;
st = i;
}
int p = lower_bound(th, th + tot, h[i]) - th + 1;
update(p, data(t.len + 1, i), 1);
}
printf("%d\n", res);
for (int i = st; i; i = nxt[i]) printf("%d ", i);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
template <class T>
struct Matrix {
vector<vector<T> > A;
Matrix() {}
Matrix(size_t n, size_t m) : A(n, vector<T>(m, 0)) {}
Matrix(size_t n) : A(n, vector<T>(n, 0)){};
size_t height() const { return (A.size()); }
size_t width() const { return (A[0].size()); }
inline const vector<T> &operator[](int k) const { return (A.at(k)); }
inline vector<T> &operator[](int k) { return (A.at(k)); }
static Matrix I(size_t n) {
Matrix mat(n);
for (int i = 0; i < n; i++) mat[i][i] = 1;
return (mat);
}
Matrix &operator+=(const Matrix &B) {
size_t n = height(), m = width();
assert(n == B.height() && m == B.width());
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) (*this)[i][j] += B[i][j];
return (*this);
}
Matrix &operator-=(const Matrix &B) {
size_t n = height(), m = width();
assert(n == B.height() && m == B.width());
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) (*this)[i][j] -= B[i][j];
return (*this);
}
Matrix &operator*=(const Matrix &B) {
size_t n = height(), m = B.width(), p = width();
assert(p == B.height());
vector<vector<T> > C(n, vector<T>(m, 0));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
for (int k = 0; k < p; k++)
C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);
A.swap(C);
return (*this);
}
Matrix &operator^=(long long k) {
Matrix B = Matrix::I(height());
while (k > 0) {
if (k & 1) B *= *this;
*this *= *this;
k >>= 1LL;
}
A.swap(B.A);
return (*this);
}
Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); }
Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); }
Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); }
Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); }
friend ostream &operator<<(ostream &os, Matrix &p) {
size_t n = p.height(), m = p.width();
for (int i = 0; i < n; i++) {
os << "[";
for (int j = 0; j < m; j++) {
os << p[i][j] << (j + 1 == m ? "]\n" : ",");
}
}
return (os);
}
T determinant() {
Matrix B(*this);
assert(width() == height());
T ret = 1;
for (int i = 0; i < width(); i++) {
int idx = -1;
for (int j = i; j < width(); j++) {
if (B[j][i] != 0) idx = j;
}
if (idx == -1) return (0);
if (i != idx) {
ret *= -1;
swap(B[i], B[idx]);
}
ret *= B[i][i];
T vv = B[i][i];
for (int j = 0; j < width(); j++) {
B[i][j] /= vv;
}
for (int j = i + 1; j < width(); j++) {
T a = B[j][i];
for (int k = 0; k < width(); k++) {
B[j][k] -= B[i][k] * a;
}
}
}
return (ret);
}
};
template <int mod>
struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return ModInt(u);
}
friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
const int mod = 1e9 + 7;
using modint = ModInt<mod>;
int main() {
int64 N, M;
cin >> N >> M;
map<int64, vector<pair<int, int> > > add;
for (int i = 0; i < N; i++) {
int64 A, L, R;
cin >> A >> L >> R;
--A;
add[L - 1].emplace_back(A, 1);
add[R].emplace_back(A, -1);
}
add[1].emplace_back(-1, -1);
add[M].emplace_back(-1, -1);
vector<modint> sum(3, 0);
Matrix<modint> mat(3, 3);
vector<int> inc(3, 0);
int64 pv = -1;
for (auto &p : add) {
for (auto &v : p.second) {
if (~v.first) inc[v.first] += v.second;
}
Matrix<modint> mat2(3, 3);
for (int i = 0; i < 3; i++) {
for (int j = -1; j <= 1; j++) {
int k = i + j;
if (k < 0 || k >= 3) continue;
if (inc[k] == 0) mat2[i][k] = 1;
}
}
if (~pv) {
mat ^= p.first - pv;
vector<modint> sum2(3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum2[j] += sum[i] * mat[i][j];
}
}
sum.swap(sum2);
} else {
sum[1] = 1;
}
mat = mat2;
pv = p.first;
}
cout << sum[1] << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool mem[100100][26];
bool vis[100100][26];
bool dp(int k, int i) {
if (k < 0) return 0;
if (i == 26) {
return (k == 0);
}
if (vis[k][i]) return mem[k][i];
vis[k][i] = 1;
for (int s = 1; s * (s - 1) / 2 <= k; s++)
if (dp(k - s * (s - 1) / 2, i + 1)) return mem[k][i] = 1;
return mem[k][i] = 0;
}
void go(int k, int i) {
if (i == 26) return;
for (int s = 1; s * (s - 1) / 2 <= k; s++)
if (dp(k - s * (s - 1) / 2, i + 1)) {
for (int j = 0; j < s; j++) printf("%c", (char)(i + 'a'));
go(k - s * (s - 1) / 2, i + 1);
return;
}
}
int main() {
int k;
scanf("%d", &k);
dp(k, 0);
go(k, 0);
printf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
int amnt1 = 0;
int tie = a;
int jack = d;
int x = min(d, a);
tie -= x;
jack -= x;
amnt1 += x * e;
int y = min(min(b, jack), c);
amnt1 += f * y;
int amnt2 = 0;
int scar = b;
int vest = c;
jack = d;
x = min(min(scar, jack), vest);
amnt2 += f * x;
jack -= x;
scar -= x;
vest -= x;
y = min(a, jack);
amnt2 += e * y;
cout << max(amnt1, amnt2) << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
bool sortinrev(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
return (a.first > b.first);
}
long long int A[1000000];
long long int B[1000000];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
vector<long long int> v;
long long int n;
cin >> n;
for (long long int i = 2; i <= n; i++) {
bool check = true;
for (long long int j = 2; (j * j) <= i; j++) {
if (i % j == 0) {
check = false;
break;
}
}
long long int val = i;
while (check && val <= n) {
v.push_back(val);
val *= i;
}
}
cout << v.size() << endl;
if (v.size() > 0)
for (long long int i = 0; i < v.size(); i++) cout << v[i] << " ";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, t, a, b, da, db, pa, pb;
int flag = 0;
cin >> x >> t >> a >> b >> da >> db;
if (x == 0)
flag = 1;
else {
for (int i = 0; i < t; i++) {
for (int j = 0; j < t; j++) {
pa = a - da * (i);
pb = b - db * (j);
if (pa + pb == x || pa == x || pb == x) {
flag = 1;
break;
}
}
if (flag) break;
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
vector<int> app[maxn];
set<int> cnt;
int main() {
int n, q, x, type, num;
while (~scanf("%d%d", &n, &q)) {
num = 0;
cnt.clear();
int last = 0;
for (int i = 0; i < q; i++) {
scanf("%d%d", &type, &x);
if (type == 1) {
app[x].push_back(++num);
cnt.insert(num);
} else if (type == 2) {
for (int i = 0; i < app[x].size(); i++) cnt.erase(app[x][i]);
app[x].clear();
} else {
for (int i = last; i <= x; i++) {
cnt.erase(i);
}
last = max(x, last);
}
printf("%d\n", cnt.size());
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
long long mulmod(long long a, long long b, long long MOD) {
if (b == 0) return 0;
long long res = mulmod(a, b >> 1, MOD);
res += res;
res %= MOD;
return (b & 1) ? (a + res) % MOD : res;
}
int main() {
ios::sync_with_stdio(0);
long long t1, t2, x1, x2, t0;
cin >> t1 >> t2 >> x1 >> x2 >> t0;
long long ans1 = 3000000000000ll, ans2 = 1, ii = 0, jj = 0;
if (t1 == t0) {
if (t2 == t0) {
cout << x1 << ' ' << x2 << endl;
return 0;
} else {
cout << x1 << ' ' << 0 << endl;
return 0;
}
}
if (t2 == t0) {
cout << 0 << ' ' << x2 << endl;
return 0;
}
if (t0 <= t1) {
ii = x1;
jj = 0;
ans1 = x1 * t1;
ans2 = x1;
}
if (t0 <= t2 &&
(ans1 == 3000000000000ll || (t2 < t1 || (t2 == t1 && x2 > x1)))) {
ii = 0;
jj = x2;
ans1 = x2 * t2;
ans2 = x2;
}
for (long long i = 0; i <= x1; i++) {
long long j = (i * (t0 - t1)) / (t2 - t0);
if ((i * (t0 - t1)) % (t2 - t0) != 0) {
j++;
}
if (i == 0 && j == 0) continue;
if (j < 0 || j > x2) {
continue;
}
if (ans1 * (i + j) > (i * t1 + j * t2) * ans2) {
ans1 = (i * t1 + j * t2);
ans2 = (i + j);
ii = i;
jj = j;
} else {
if (ans1 * (i + j) == (i * t1 + j * t2) * ans2 && ans2 < (i + j)) {
ans1 = (i * t1 + j * t2);
ans2 = (i + j);
ii = i;
jj = j;
}
}
}
cout << ii << ' ' << jj << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct node {
string s;
int id;
bool operator<(node const &x) const { return s > x.s; }
} a;
string str;
priority_queue<node> q;
int n, k;
string solve() {
while (!q.empty()) {
a = q.top();
q.pop();
k--;
if (k == 0) return a.s;
if (a.id < n) {
a.s = a.s + str[a.id];
a.id++;
q.push(a);
}
}
return "No such line.";
}
int main() {
int i;
cin >> str >> k;
n = str.size();
for (i = 0; str[i]; i++) {
a.s = str[i];
a.id = i + 1;
q.push(a);
}
cout << solve() << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
FILE* g_f_;
int g_s_;
template <class _T>
inline _T sqr(const _T& x) {
return x * x;
}
template <class _T>
inline string tostr(const _T& a) {
ostringstream os("");
os << a;
return os.str();
}
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-11;
int main() {
cout << setiosflags(ios::fixed) << setprecision(10);
int n, r1, r2;
(g_s_ = scanf("%d%d%d", &n, &r2, &r1));
if (r1 > r2)
puts("NO");
else {
if (r1 * 2 > r2) {
puts(n == 1 ? "YES" : "NO");
} else {
long double aa = asin(r1 * 1.0 / (r2 - r1)) * 2.0;
int k = (int)(2.0 * PI / aa + 1e-7);
puts(k >= n ? "YES" : "NO");
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct PT {
long long x, y;
PT() {}
PT(long long x, long long y) : x(x), y(y) {}
PT(const PT &p) : x(p.x), y(p.y) {}
};
vector<PT> ret(100007), p;
bool cmp(PT a, PT b) {
if (a.x == b.x) return a.y < b.y;
return a.x > b.x;
}
long long area(PT p, PT q, PT r) {
long long x[] = {p.x, q.x, r.x};
long long y[] = {p.y, q.y, r.y};
long long a = 0;
long long j = 2;
for (long long i = 0; i < 3; i++) {
a += (x[j] + x[i]) * (y[j] - y[i]);
j = i;
}
return llabs(a);
}
long long sign(PT p, PT q, PT r) {
long long val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
val = -val;
if (val < 0) return 1;
if (val > 0) return 2;
return 0;
}
void bord() {
long long k = 0, n = p.size();
sort(p.begin(), p.end(), cmp);
for (long long i = 0; i < n; i++) {
while (k >= 2 and sign(ret[k - 2], ret[k - 1], p[i]) != 2) k--;
ret[k++] = p[i];
}
ret.resize(k);
}
int main() {
int n;
cin >> n;
p.resize(n);
for (int i = 0; i < n; i++)
cin >> p[i].x >> p[i].y, p[i].y -= p[i].x * p[i].x;
bord();
int ans = ret.size() - 1;
if (ret.size() > 1 and ret[0].x == ret[1].x) ans--;
if (ret.size() > 2 and ret[ret.size() - 1].x == ret[ret.size() - 2].x) ans--;
cout << ans << endl;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n1, n2, k1, k2, s, S;
scanf("%d %d %d %d", &n1, &n2, &k1, &k2);
s = min(k1, k2);
if (n1 - s <= n2 - s)
puts("Second");
else
puts("First");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.precision(20);
int n;
cin >> n;
while (n--) {
string s, t;
cin >> s >> t;
bool ok = true;
if (t.size() < s.size()) ok = false;
int ns = s.size(), ms = t.size();
int pos = 0;
for (int i = 0; i < ns; i++) {
int cnt = 1;
char c = s[i];
while (i + 1 < ns and s[i + 1] == c) {
cnt++;
i++;
}
int cnt2 = 0;
while (pos < ms and t[pos] == c) {
cnt2++;
pos++;
}
if (cnt2 < cnt) ok = false;
}
if (pos < ms) ok = false;
if (ok)
cout << "YES\n";
else
cout << "NO\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, n1, n2, limiter = 1, fight = 0, temp;
cin >> n >> n1;
queue<int> p1, p2;
for (int i = 1; i <= n1; i++) {
cin >> temp;
p1.push(temp);
limiter *= i;
}
cin >> n2;
for (int i = 1; i <= n2; i++) {
cin >> temp;
p2.push(temp);
limiter *= (n1 + i);
}
limiter *= (n + 1);
while (1) {
if (fight > limiter) {
cout << -1;
break;
} else if (p1.empty()) {
cout << fight << " " << 2;
break;
} else if (p2.empty()) {
cout << fight << " " << 1;
break;
} else {
if (p1.front() > p2.front()) {
p1.push(p2.front());
p1.push(p1.front());
p1.pop();
p2.pop();
fight++;
} else {
p2.push(p1.front());
p2.push(p2.front());
p2.pop();
p1.pop();
fight++;
}
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
int id[N + N];
int a[N + N];
int where[N + N], color[N + N];
vector<int> E[N + N];
bool cmp(int i, int j) { return a[i] < a[j]; }
void dfs(int u, int c) {
if (color[u] == -1) {
color[u] = c;
} else {
return;
}
for (int v : E[u]) {
dfs(v, c ^ 1);
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < (int)(n); i++) {
scanf("%d%d", &a[i * 2], &a[i * 2 + 1]);
a[i * 2 + 1]++;
id[i * 2] = i * 2;
id[i * 2 + 1] = i * 2 + 1;
}
sort(id, id + n + n, cmp);
for (int i = 0; i < (int)(n + n); i++) {
where[id[i]] = i;
}
for (int i = 0; i < (int)(n); i++) {
E[i * 2].push_back(i * 2 + 1);
E[i * 2 + 1].push_back(i * 2);
E[where[i * 2]].push_back(where[i * 2 + 1]);
E[where[i * 2 + 1]].push_back(where[i * 2]);
}
memset(color, 0xff, sizeof(color));
for (int i = 0; i < (int)(n + n); i++) {
if (color[i] == -1) {
dfs(i, 0);
}
}
for (int i = 0; i < (int)(n); i++) {
printf("%d ", color[where[i * 2]]);
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int> > gauss(vector<int> &v) {
vector<pair<int, int> > log;
for (int i = 30, pos = 0; i >= 0; --i) {
int idx = -1;
for (int j = pos; j < n; ++j) {
if (v[j] & (1 << i)) {
idx = j;
break;
}
}
if (idx == -1) continue;
if (idx != pos) {
v[pos] ^= v[idx];
log.push_back(pair<int, int>(pos, idx));
}
for (int j = 0; j < n; ++j) {
if (j == pos) continue;
if (v[j] & (1 << i)) {
v[j] ^= v[pos];
log.push_back(pair<int, int>(j, pos));
}
}
++pos;
}
return log;
}
int highest_bit(int p) {
p |= (p >> 1);
p |= (p >> 2);
p |= (p >> 4);
p |= (p >> 8);
p |= (p >> 16);
return (p + 1) >> 1;
}
int main(void) {
cin >> n;
vector<int> x(n), y(n);
for (int i = 0; i < n; ++i) {
cin >> x[i];
}
for (int i = 0; i < n; ++i) {
cin >> y[i];
}
vector<int> orig_x = x, orig_y = y;
vector<pair<int, int> > x_log = gauss(x);
vector<pair<int, int> > y_log = gauss(y);
vector<pair<int, int> > merge_log;
for (int i = 0, j = 0; i < n; ++i) {
if (y[i] == 0) {
for (; j < n; ++j) {
if (x[j] != 0) {
merge_log.push_back(pair<int, int>(j, j));
x[j] ^= x[j];
}
}
break;
}
while (j < n && highest_bit(x[j]) > highest_bit(y[i])) {
merge_log.push_back(pair<int, int>(j, j));
x[j] ^= x[j];
++j;
}
if (j == n || highest_bit(x[j]) < highest_bit(y[i])) {
cout << -1 << endl;
return 0;
}
for (int k = j + 1; k < n && x[j] != y[i]; ++k) {
int b1 = highest_bit(x[k]);
int b2 = highest_bit(x[j] ^ y[i]);
if (b2 > b1) {
break;
}
if (b1 == b2) {
x[j] ^= x[k];
merge_log.push_back(pair<int, int>(j, k));
}
}
if (x[j] != y[i]) {
cout << -1 << endl;
return 0;
}
if (j != i) {
merge_log.push_back(pair<int, int>(j, i));
x[j] ^= x[i];
merge_log.push_back(pair<int, int>(i, j));
x[i] ^= x[j];
merge_log.push_back(pair<int, int>(j, j));
x[j] ^= x[j];
}
++j;
}
vector<pair<int, int> > result;
result.insert(result.end(), x_log.begin(), x_log.end());
result.insert(result.end(), merge_log.begin(), merge_log.end());
result.insert(result.end(), y_log.rbegin(), y_log.rend());
x = orig_x;
y = orig_y;
cout << result.size() << endl;
for (int i = 0; i < (int)result.size(); ++i) {
x[result[i].first] ^= x[result[i].second];
cout << result[i].first + 1 << " " << result[i].second + 1 << endl;
}
if (x != y) {
return 1;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
struct node {
int l, r, lo, wi;
double lose, win, ans;
} t[200005 << 2];
int a[200005], p[200005], b[200005];
int n, q, tt;
node get(int k) {
node ret;
ret.l = ret.r = k;
ret.ans = 1.0 * p[k] * a[k] / (a[k] + b[k]);
ret.ans = min(ret.ans, 1.0 * p[k] / 2);
if (a[k] >= b[k])
ret.win = 0;
else
ret.win = 1.0 * p[k] * (a[k] + 1) / (a[k] + 1 + b[k]) -
1.0 * p[k] * a[k] / (a[k] + b[k]);
if (a[k] <= 0)
ret.lose = 1e9;
else if (a[k] > b[k])
ret.lose = 0;
else
ret.lose = 1.0 * p[k] * a[k] / (a[k] + b[k]) -
1.0 * p[k] * (a[k] - 1) / (a[k] + b[k] - 1);
ret.lo = ret.wi = k;
return ret;
}
void update(int k) {
t[k].ans = t[k << 1].ans + t[k << 1 | 1].ans;
if (t[k << 1].win > t[k << 1 | 1].win) {
t[k].win = t[k << 1].win;
t[k].wi = t[k << 1].wi;
} else {
t[k].win = t[k << 1 | 1].win;
t[k].wi = t[k << 1 | 1].wi;
}
if (t[k << 1].lose < t[k << 1 | 1].lose) {
t[k].lose = t[k << 1].lose;
t[k].lo = t[k << 1].lo;
} else {
t[k].lose = t[k << 1 | 1].lose;
t[k].lo = t[k << 1 | 1].lo;
}
}
void build(int l, int r, int k) {
t[k].l = l;
t[k].r = r;
if (l == r) {
t[k] = get(l);
return;
}
int mid = (l + r) >> 1;
build(l, mid, k << 1);
build(mid + 1, r, k << 1 | 1);
update(k);
}
void change(int l, int k) {
if (t[k].l == l && t[k].r == l) {
t[k] = get(l);
return;
}
int mid = (t[k].l + t[k].r) >> 1;
if (mid >= l)
change(l, k << 1);
else
change(l, k << 1 | 1);
update(k);
}
int main() {
n = read();
tt = read();
q = read();
for (int i = 1; i <= n; i++) p[i] = read();
for (int i = 1; i <= n; i++) b[i] = read();
build(1, n, 1);
for (int i = 1; i <= tt; i++) {
int wi = t[1].wi;
a[wi]++;
change(wi, 1);
}
for (int i = 1; i <= q; i++) {
int opt = read(), x = read();
if (opt == 1)
b[x]++;
else
b[x]--;
change(x, 1);
while (t[1].win - t[1].lose > 1e-9) {
int wi = t[1].wi;
int lo = t[1].lo;
a[lo]--;
a[wi]++;
change(lo, 1);
change(wi, 1);
}
printf("%.9lf\n", t[1].ans);
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
vector<int> primes{
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107,
109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181,
191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263,
269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433,
439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613,
617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<int> y;
for (int i = 0; i < primes.size() && primes[i] <= n; ++i) {
int x = primes[i];
while (x <= n) {
y.push_back(x);
x *= primes[i];
}
}
cout << y.size() << endl;
for (int x : y) {
cout << x << " ";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-10;
const long long MOD = 1000000007ll;
const long long mod1 = 1000000009ll;
const long long mod2 = 1100000009ll;
int INF = (int)1e9 + 5;
long long INFINF = (long long)1e18;
int arr[100];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
if (n == 1) {
if (arr[0] == 0)
cout << "UP" << endl;
else if (arr[0] == 15)
cout << "DOWN" << endl;
else
cout << "-1" << endl;
return 0;
}
if (arr[n - 2] < arr[n - 1] && arr[n - 1] < 15) {
cout << "UP" << endl;
return 0;
}
if (arr[n - 2] < arr[n - 1] && arr[n - 1] == 15) {
cout << "DOWN" << endl;
return 0;
}
if (arr[n - 2] > arr[n - 1] && arr[n - 1] > 0) {
cout << "DOWN" << endl;
return 0;
}
if (arr[n - 2] > arr[n - 1] && arr[n - 1] == 0) {
cout << "UP" << endl;
return 0;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
pair<int, int> p[2 * N];
map<int, int> ma;
int cnt, s[2 * N];
void upd(int l, int r, int x) {
r++;
map<int, int>::iterator it;
it = ma.find(l);
if (it == ma.end()) {
cnt++;
p[cnt] = make_pair(cnt, 0);
s[cnt] = 1;
ma.insert(make_pair(l, cnt));
}
it = ma.find(r);
if (it == ma.end()) {
cnt++;
p[cnt] = make_pair(cnt, 0);
s[cnt] = 1;
ma.insert(make_pair(r, cnt));
}
l = ma[l];
r = ma[r];
int w1 = 0, w2 = 0;
while (l != p[l].first) {
w1 ^= p[l].second;
l = p[l].first;
}
while (r != p[r].first) {
w2 ^= p[r].second;
r = p[r].first;
}
if (l == r) {
return;
}
if (s[l] > s[r]) {
p[r] = make_pair(l, x ^ w1 ^ w2);
s[l] += s[r];
} else {
p[l] = make_pair(r, x ^ w1 ^ w2);
s[r] += s[l];
}
}
int get(int l, int r) {
r++;
map<int, int>::iterator it;
it = ma.find(l);
if (it == ma.end()) {
cnt++;
p[cnt] = make_pair(cnt, 0);
s[cnt] = 1;
ma.insert(make_pair(l, cnt));
}
it = ma.find(r);
if (it == ma.end()) {
cnt++;
p[cnt] = make_pair(cnt, 0);
s[cnt] = 1;
ma.insert(make_pair(r, cnt));
}
l = ma[l];
r = ma[r];
int w1 = 0, w2 = 0;
while (l != p[l].first) {
w1 ^= p[l].second;
l = p[l].first;
}
while (r != p[r].first) {
w2 ^= p[r].second;
r = p[r].first;
}
if (l != r) {
return -1;
}
return w1 ^ w2;
}
int main() {
ios_base::sync_with_stdio(0);
int q;
cin >> q;
int last = 0;
for (int i = 1; i <= q; i++) {
int t;
cin >> t;
if (t == 1) {
int l, r, x;
cin >> l >> r >> x;
l ^= last;
r ^= last;
x ^= last;
if (l > r) {
swap(l, r);
}
upd(l, r, x);
}
if (t == 2) {
int l, r;
cin >> l >> r;
l ^= last;
r ^= last;
if (l > r) {
swap(l, r);
}
int ans = get(l, r);
cout << ans << "\n";
last = ans;
if (ans == -1) {
last = 1;
}
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
constexpr int P = 1e9 + 7, N = 2e5 + 10;
inline void upd(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
struct SegmentTree {
struct Info {
int c[6];
Info() { std::fill(c, c + 6, 0); }
int operator[](int i) const { return c[i]; }
int &operator[](int i) { return c[i]; }
friend Info operator+(const Info &a, const Info &b) {
Info res;
for (int i = 0; i < 6; ++i) res[i] = (a[i] + b[i]) % P;
upd(res[3], 1ll * a[0] * b[1] % P);
upd(res[4], 1ll * a[1] * b[2] % P);
upd(res[5], 1ll * a[0] * b[4] % P);
upd(res[5], 1ll * a[3] * b[2] % P);
return res;
}
};
struct Node {
int lc, rc;
Info c;
Node() {
lc = rc = 0;
c = Info();
}
} t[N * 23];
int cnt;
int newNode() {
int u = ++cnt;
t[cnt] = Node();
return u;
}
SegmentTree() { cnt = 0; }
void pushup(int x) { t[x].c = t[t[x].lc].c + t[t[x].rc].c; }
void modify(int &x, int l, int r, int p, int vi, int vj, int vk) {
if (!x) x = newNode();
if (l == r) {
t[x].c[0] = vi;
upd(t[x].c[1], vj);
t[x].c[2] = vk;
return;
}
int mid = (l + r) >> 1;
if (p <= mid)
modify(t[x].lc, l, mid, p, vi, vj, vk);
else
modify(t[x].rc, mid + 1, r, p, vi, vj, vk);
pushup(x);
}
} st;
int fw[N];
void mdf(int x, int v = 1) {
for (; x < N; x += x & -x) upd(fw[x], v);
}
int qry(int x) {
int ans = 0;
for (; x; x -= x & -x) upd(ans, fw[x]);
return ans;
}
int rt[N], n, q, a[N], b[N], pre[N], suf[N];
int val[N], tot;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
val[i] = a[i];
}
std::sort(val + 1, val + n + 1);
tot = std::unique(val + 1, val + n + 1) - val - 1;
for (int i = 1; i <= n; ++i)
a[i] = std::lower_bound(val + 1, val + tot + 1, a[i]) - val;
std::fill(fw, fw + tot + 1, 0);
for (int i = 1; i <= n; ++i) {
pre[i] = qry(a[i]);
mdf(a[i]);
}
std::fill(fw, fw + tot + 1, 0);
for (int i = n; i >= 1; --i) {
suf[i] = qry(a[i]);
mdf(a[i]);
}
for (int i = 1; i <= n; ++i) {
st.modify(rt[a[i]], 1, n, i, pre[i], 1, suf[i]);
}
int ans = 0;
for (int i = 1; i <= tot; ++i) upd(ans, st.t[rt[i]].c[5]);
std::cin >> q;
while (q--) {
int opt, x;
std::cin >> opt >> x;
int vi, vj, vk;
if (opt == 1)
vi = 0, vj = P - 1, vk = 0;
else
vi = pre[x], vj = 1, vk = suf[x];
upd(ans, P - st.t[rt[a[x]]].c[5]);
st.modify(rt[a[x]], 1, n, x, vi, vj, vk);
upd(ans, st.t[rt[a[x]]].c[5]);
std::cout << ans << '\n';
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
int x = sqrt(n);
int ans = 2 * x;
if (x * x >= n) {
ans -= 2;
} else if (x * (x + 1) >= n) {
ans -= 1;
}
cout << ans << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long func2(long long x, long long y, long long t) {
if (t >= x + y - 2) return x * y;
long long ans = (t + 1) * (t + 2) / 2;
if (t >= x) ans -= (t - x + 1) * (t - x + 2) / 2;
if (t >= y) ans -= (t - y + 1) * (t - y + 2) / 2;
return ans;
}
long long func(long long N, long long x, long long y, long long t) {
long long ans = func2(x, y, t) + func2(x, N - y + 1, t) +
func2(N - x + 1, y, t) + func2(N - x + 1, N - y + 1, t);
ans -= 3;
ans -= min(x - 1, t);
ans -= min(N - x, t);
ans -= min(y - 1, t);
ans -= min(N - y, t);
return ans;
}
int main(void) {
long long N, x, y, c;
cin >> N >> x >> y >> c;
if (1 >= c) {
cout << 0 << endl;
return 0;
}
long long low = 0, high = (1ll << 33);
while (high - low > 1) {
long long mid = (high + low) / 2;
long long tmp = func(N, x, y, mid);
if (tmp >= c)
high = mid;
else
low = mid;
}
cout << high << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
vector<vector<char> > mat(9, vector<char>(9));
for (long long int i = 0; i < (9); i++) {
for (long long int j = 0; j < (9); j++) {
cin >> mat[i][j];
}
}
int x, y;
cin >> x >> y;
x--;
y--;
bool flag = 0;
int m = x % 3;
int n = y % 3;
int a = m * 3;
int b = n * 3;
for (int i = a; i < a + 3; i += 1) {
for (int j = b; j < b + 3; j += 1) {
if (mat[i][j] == '.') {
flag = 1;
mat[i][j] = '!';
}
}
}
if (!flag) {
for (long long int i = 0; i < (9); i++) {
for (long long int j = 0; j < (9); j++) {
if (mat[i][j] == '.') mat[i][j] = '!';
if (j % 3 == 2)
cout << mat[i][j] << " ";
else
cout << mat[i][j];
}
if (i % 3 == 2)
cout << "\n\n";
else
cout << "\n";
}
} else {
for (long long int i = 0; i < (9); i++) {
for (long long int j = 0; j < (9); j++) {
if (j % 3 == 2)
cout << mat[i][j] << " ";
else
cout << mat[i][j];
}
if (i % 3 == 2)
cout << "\n\n";
else
cout << "\n";
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n, m, min, step, t, x;
long long k, ans;
while (cin >> n >> m >> k) {
if (n % 2 == 0) {
for (i = 0; i < n; i++) cin >> x;
cout << 0 << endl;
continue;
}
if (n == 1) {
cin >> ans;
if (m * k < ans) ans = m * k;
cout << ans << endl;
continue;
}
step = m / ((n + 1) / 2);
cin >> min;
for (i = (n - 1) / 2; i > 0; i--) {
cin >> t >> x;
if (x < min) min = x;
}
if (step * k > min)
ans = min;
else
ans = step * k;
cout << ans << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, i, smx = 0, smi = 0, x, y;
cin >> n >> m;
long double ans = 0;
smx = (n - 1) * (n) / 2;
x = n / 2;
smi = (x) * (x + 1);
if (n % 2 == 0) {
smi -= n / 2;
}
while (m--) {
cin >> x >> y;
ans += n * x;
if (y >= 0) {
ans += smx * y;
} else
ans += smi * y;
}
std::cout << std::fixed;
std::cout << std::setprecision(18) << ans / n << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
vector<int> primes(1000001, -1);
vector<long long> facs(1e7);
long long binpow(long long x, long long p) {
if (p == 0) return 1;
if (p == 1) return x % M;
return ((binpow((x * x) % M, p / 2) % M) * (p % 2 ? x : 1)) % M;
}
long long getc(long long n, long long k) {
return (facs[n + k - 1] * binpow((facs[n - 1] * facs[k]) % M, M - 2)) % M;
}
int main() {
ios::sync_with_stdio(false);
primes[1] = 1;
for (int i = 2; i <= 1e6; ++i) {
if (primes[i] != -1) continue;
for (int j = i; j <= 1e6; j += i) primes[j] = i;
}
facs[0] = 1;
for (int i = 1; i < facs.size(); ++i) facs[i] = (facs[i - 1] * i) % M;
int q;
cin >> q;
while (q--) {
long long x, y;
cin >> x >> y;
long long prx = 1;
map<long long, long long> vm;
while (primes[x] != 1) {
vm[primes[x]]++;
x /= primes[x];
}
long long ans = 1;
for (auto it = vm.begin(); it != vm.end(); ++it)
ans = (ans * getc(y, it->second)) % M;
ans = (ans * binpow(2, y - 1)) % M;
cout << ans << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
inline long long isqrt(long long k) {
long long r = sqrt(k) + 1;
while (r * r > k) r--;
return r;
}
inline long long icbrt(long long k) {
long long r = cbrt(k) + 1;
while (r * r * r > k) r--;
return r;
}
inline long long mnz(long long& a, long long b) { return a = (a > b ? b : a); }
inline long long mxz(long long& a, long long b) { return a = (a < b ? b : a); }
inline string toString(long long n) {
stringstream ss;
ss << n;
return ss.str();
}
double const eps = 1e-6;
long long const Base = 1e9 + 7, oo = 1e17, MAXN = 1e6;
long long A[MAXN + 5];
long long Solves() {
long long n, m, k, cnt = 0, ans = 0, x, y, q, c, sum = 0, v, t;
cin >> n;
long long flag1 = 0, flag2 = 0;
for (long long i = 1; i <= n; i++) {
cin >> A[i];
if (abs(A[i]) % 2 == 0)
A[i] /= 2;
else {
if (A[i] > 0) {
if (flag1)
A[i] = A[i] / 2 + 1;
else
A[i] /= 2;
flag1 ^= 1;
} else {
if (flag2)
A[i] = A[i] / 2 - 1;
else
A[i] /= 2;
flag2 ^= 1;
}
}
cout << A[i] << endl;
}
return 0;
}
int main() {
if (fopen("locin.txt", "r")) freopen("locin.txt", "r", stdin);
long long JUDGE_ONLINE = 1;
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long test = 1;
for (long long T = 1; T <= test; T++) {
Solves();
}
if (!JUDGE_ONLINE)
cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int cntGems[60005];
int memo[31005][2005];
int n, d;
int dp(int i, int j) {
if (i > 30000) {
return 0;
} else if (memo[i][j - d + 1001] != -1) {
return memo[i][j - d + 1001];
} else {
int ans = max(cntGems[i + j] + dp(i + j, j),
cntGems[i + j + 1] + dp(i + j + 1, j + 1));
if (j > 1) {
ans = max(ans, cntGems[i + (j - 1)] + dp(i + (j - 1), j - 1));
}
return memo[i][j - d + 1001] = ans;
}
}
int main() {
scanf("%d%d", &n, &d);
memset(cntGems, 0, sizeof(cntGems));
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
cntGems[x]++;
}
memset(memo, -1, sizeof(memo));
printf("%d", cntGems[d] + dp(d, d));
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:200000000")
using namespace std;
const int INF = (int)1e9;
const long long INF64 = (long long)1e18;
const long double eps = 1e-9;
const long double pi = 3.14159265358979323846;
int n, m;
int t[200][200];
int sr[200];
int sc[200];
bool ansr[200];
bool ansc[200];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < int(n); ++i)
for (int j = 0; j < int(m); ++j) cin >> t[i][j];
for (int i = 0; i < int(n); ++i)
for (int j = 0; j < int(m); ++j) sr[i] += t[i][j], sc[j] += t[i][j];
while (true) {
int idx = -1;
for (int i = 0; i < int(n); ++i)
if (sr[i] < 0) {
idx = i;
break;
}
if (idx != -1) {
ansr[idx] ^= 1;
sr[idx] *= -1;
for (int j = 0; j < int(m); ++j) {
sc[j] -= t[idx][j];
t[idx][j] *= -1;
sc[j] += t[idx][j];
}
continue;
}
idx = -1;
for (int j = 0; j < int(m); ++j)
if (sc[j] < 0) {
idx = j;
break;
}
if (idx != -1) {
ansc[idx] ^= 1;
sc[idx] *= -1;
for (int i = 0; i < int(n); ++i) {
sr[i] -= t[i][idx];
t[i][idx] *= -1;
sr[i] += t[i][idx];
}
continue;
}
break;
}
vector<int> ans;
for (int i = 0; i < int(n); ++i)
if (ansr[i]) ans.push_back(i + 1);
cout << ans.size() << " ";
for (int i = 0; i < int(ans.size()); ++i) cout << ans[i] << " ";
cout << endl;
ans.clear();
for (int j = 0; j < int(m); ++j)
if (ansc[j]) ans.push_back(j + 1);
cout << ans.size() << " ";
for (int i = 0; i < int(ans.size()); ++i) cout << ans[i] << " ";
cout << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, c, q;
int fa[N];
vector<int> G[N], col[N];
set<int> s[N];
map<int, int> mp[N];
char op[4];
bool vis[N];
void addedge(int u, int v, int w) {
G[u].push_back(v);
col[u].push_back(w);
}
int find(int u) {
if (u == fa[u]) {
return fa[u];
}
return fa[u] = find(fa[u]);
}
void uniset(int u, int v) {
if (find(u) == find(v)) {
return;
}
if (s[find(u)].size() < s[find(v)].size()) {
swap(u, v);
}
while (!s[find(v)].empty()) {
s[find(u)].insert(*s[find(v)].begin());
s[find(v)].erase(s[find(v)].begin());
}
fa[find(v)] = find(u);
}
void dfs(int u) {
if (vis[u]) {
return;
}
vis[u] = 1;
for (int i = 0, v, w; i < (int)G[u].size(); i++) {
v = G[u][i];
w = col[u][i];
if (!mp[u][w]) {
mp[u][w] = v;
} else {
uniset(find(mp[u][w]), find(v));
}
}
}
int main() {
scanf("%d%d%d%d", &n, &m, &c, &q);
for (int i = 1; i <= n; i++) {
s[i].insert(i);
fa[i] = i;
}
for (int i = 1, u, v, w; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
s[u].insert(v);
s[v].insert(u);
}
for (int i = 1; i <= n; i++) {
dfs(i);
}
int u, v, w;
while (q--) {
scanf("%s", op);
if (op[0] == '+') {
scanf("%d%d%d", &u, &v, &w);
s[find(u)].insert(v);
s[find(v)].insert(u);
if (mp[u][w]) {
uniset(find(mp[u][w]), find(v));
} else {
mp[u][w] = v;
}
if (mp[v][w]) {
uniset(find(mp[v][w]), find(u));
} else {
mp[v][w] = u;
}
} else {
scanf("%d%d", &u, &v);
if (s[find(u)].count(v)) {
printf("Yes\n");
} else {
printf("No\n");
}
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, t;
cin >> s >> t;
int a;
int c = 0;
for (a = 0; a < t.length(); a++) {
if (t[a] == s[c]) c++;
if (c == s.length()) break;
}
int b;
c = s.length();
for (b = t.length() - 1; b >= 0; b--) {
if (t[b] == s[c - 1]) c--;
if (c == 0) break;
}
int answ;
if (b <= a)
answ = 0;
else
answ = b - a;
cout << answ;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
const int infi = 1e9 + 7;
const long long infl = 1e18;
inline int readChar();
inline void writeChar(int x);
inline void writeWord(const char *s);
inline void flush();
static const int buf_size = 4096;
inline int getChar() {
static char buf[buf_size];
static int len = 0, pos = 0;
if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin);
if (pos == len) return -1;
return buf[pos++];
}
inline int readChar() {
int c = getChar();
while (c >= 0 && c <= 32) c = getChar();
return c;
}
inline int readWord(char *bf) {
char *cur = bf;
*cur = getChar();
while (*cur <= 32) *cur = getChar();
while (*cur > 32) {
++cur;
*cur = getChar();
}
return cur - bf;
}
inline int readInt() {
int s = 1, c = readChar();
int x = 0;
if (c == '-') s = -1, c = getChar();
while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar();
return s == 1 ? x : -x;
}
static int write_pos = 0;
static char write_buf[buf_size];
inline void writeChar(int x) {
if (write_pos == buf_size)
fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
write_buf[write_pos++] = x;
}
inline void flush() {
if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
}
inline void writeInt(long long x) {
if (x < 0) writeChar('-'), x = -x;
char s[24];
int n = 0;
while (x || !n) s[n++] = '0' + x % 10, x /= 10;
while (n--) writeChar(s[n]);
}
inline void writeWord(const char *s) {
while (*s) writeChar(*s++);
}
string ar;
int main() {
ios_base::sync_with_stdio(0);
cin.sync_with_stdio(false);
cin.tie(0);
cin >> ar;
string arz;
int flag = 0;
int n = (int)ar.size();
for (int i = 0; i < n; ++i) {
if (ar[i] != 'a') {
if (!flag) {
ar[i]--;
flag = 1;
continue;
} else {
ar[i]--;
}
} else {
if (flag) break;
}
}
if (flag == 0) {
if (ar[n - 1] == 'a')
ar[n - 1] = 'z';
else
ar[n - 1]--;
}
cout << ar;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> pt[222222];
int bh[222222];
int n;
int i, u;
int ans, e, ne;
int ass[222222];
int ggg[222222];
inline bool cmp(const int &a, const int &b) { return pt[a] < pt[b]; }
int main() {
scanf("%d", &n);
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d%d", &pt[i].first, &pt[i].second);
bh[i] = i;
pt[i].second += pt[i].first;
}
sort(bh, bh + n, cmp);
ans = 0;
e = 0;
ne = 0;
for (i = 0; i < n; i++) {
u = bh[i];
if (pt[u].second <= ne) continue;
if (pt[u].first > e) {
ans++;
e = max(ne, pt[u].first);
}
ne = pt[u].second;
ass[ans] = u;
}
for (i = 1; i <= ans; i++) ggg[ass[i]] = 1;
printf("%d\n", n - ans);
for (i = 0; i < n; i++)
if (!ggg[i]) printf("%d ", i + 1);
printf("\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long rem[n], cap[n];
long long srem = 0;
for (long long i = 0; i < n; i++) {
cin >> rem[i];
srem += rem[i];
}
long long maxcap = LONG_LONG_MIN;
for (long long i = 0; i < n; i++) {
cin >> cap[i];
maxcap = max(maxcap, cap[i]);
}
sort(cap, cap + n);
long long temp = cap[n - 2];
long long sum = temp + maxcap;
if (sum < srem)
cout << "NO\n";
else
cout << "YES\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin, s);
int upperCount = 0, lowerCount = 0;
for (int i = 0; i < s.length(); i++) {
if (isupper(s[i])) {
upperCount++;
} else {
lowerCount++;
}
}
if (upperCount > lowerCount) {
transform(s.begin(), s.end(), s.begin(), ::toupper);
} else
transform(s.begin(), s.end(), s.begin(), ::tolower);
cout << s << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long SIZE = 1e6 + 50;
inline long long read() {
long long x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x;
}
long long n, m, ans, sum;
long long deletion[SIZE];
signed main() {
n = read();
m = read();
for (long long i = 1; i <= m; ++i) {
long long x = read(), y = read();
deletion[x]++;
deletion[y]++;
}
ans = n * (n - 1) / 2 * (n - 2) / 3;
for (long long i = 1; i <= n; ++i) {
sum += deletion[i] * (n - 1 - deletion[i]);
}
sum >>= 1;
ans -= sum;
printf("%lld\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
if (x < y) x = y;
}
template <typename T>
inline void write(T x) {
long long i = 20;
char buf[21];
buf[20] = '\n';
do {
buf[--i] = x % 10 + '0';
x /= 10;
} while (x);
do {
putchar(buf[i]);
} while (buf[i++] != '\n');
}
template <typename T>
inline T readInt() {
T n = 0, s = 1;
char p = getchar();
if (p == '-') s = -1;
while ((p < '0' || p > '9') && p != EOF && p != '-') p = getchar();
if (p == '-') s = -1, p = getchar();
while (p >= '0' && p <= '9') {
n = (n << 3) + (n << 1) + (p - '0');
p = getchar();
}
return n * s;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
if (b == 0) return a;
if (a == b) return a;
if (a > b)
return gcd(a % b, b);
else
return gcd(a, b % a);
}
long long exp(long long x, long long n) {
if (n == 0) return 1;
if (n == 1) return x % 1000000007;
if (n % 2 == 0)
return exp((x * x) % 1000000007, n / 2);
else
return exp(x * exp((x * x) % 1000000007, n / 2));
}
bool sortbysec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second < b.second);
}
void solve() {
string s;
cin >> s;
long long n = s.size();
long long c1 = 0, c2 = 0, c3 = 0;
for (long long i = 0; i < s.size(); i += 1) {
if (s[i] == 'A')
c1++;
else if (s[i] == 'B')
c2++;
else
c3++;
}
if (c1 <= c2) {
n -= 2 * c1;
c2 = c2 - c1;
if (c2 == c3) {
n -= 2 * c3;
}
}
if (n == 0)
cout << "YES\n";
else
cout << "NO\n";
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
cin >> t;
for (long long i = 1; i <= t; i++) {
solve();
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[1000000];
map<pair<int, int>, bool> memo;
bool dp(int idx, int rem) {
if (rem == 0) {
return true;
}
if (idx == n) {
return false;
}
auto ret = memo.insert(make_pair(make_pair(idx, rem), 0));
if (ret.second == false) {
return ret.first->second;
}
ret.first->second = dp(idx + 1, rem);
ret.first->second = max(ret.first->second, dp(idx + 1, (rem + arr[idx]) % m));
return ret.first->second;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> n >> m;
if (n > m) {
cout << "YES\n";
return 0;
}
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
for (int i = 0; i < n; ++i) {
if (dp(i + 1, arr[i] % m)) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void write(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
const long long N = 1000010, mod = 1e9 + 7;
char s[N];
long long n, a[N], A, B, xianA, xianB;
vector<long long> ans;
long long find(long long x) {
long long ans = 0;
for (long long i = (long long)(1); i <= (long long)(n); ++i)
if (a[i] == x) ans = i;
return ans;
}
int main() {
for (long long T = read(); T--;) {
n = read();
bool fl = 1;
ans.clear();
for (long long i = (long long)(1); i <= (long long)(n); ++i) {
a[i] = read();
fl &= (a[i] & 1) == (i & 1);
}
if (!fl)
puts("-1");
else {
for (long long i = n; i >= 3; i -= 2) {
long long p;
p = find(i);
if (p > 0 && p != 1) ans.push_back(p), reverse(a + 1, a + p + 1);
p = find(i - 1) - 1;
if (p > 0 && p != 1) ans.push_back(p), reverse(a + 1, a + p + 1);
p = find(i - 1) + 1;
if (p > 0 && p != 1) ans.push_back(p), reverse(a + 1, a + p + 1);
p = find(i);
if (p > 0 && p != 1) ans.push_back(p), reverse(a + 1, a + p + 1);
ans.push_back(i);
reverse(a + 1, a + i + 1);
}
cout << ans.size() << endl;
for (long long i = (long long)(0); i < (long long)(ans.size()); ++i)
write(ans[i]), putchar(' ');
puts("");
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; ++i) {
v[i] = i + 1;
}
for (int i = v.size() - 1; i > 0; i -= 2) {
swap(v[i], v[i - 1]);
}
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << ' ';
}
cout << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, dp[20][2];
string s[20];
int solve(int idx = n - 1, int left = 1) {
if (idx < k) return 0;
int& ret = dp[idx][left];
if (~ret) return ret;
int val = (m - 1) * !left;
int start = (m - 1) * left;
int inc = (left ? -1 : 1);
for (int i = start; i >= 0 && i < m; i += inc) {
if (s[idx][i] == '1') {
val = i;
break;
}
}
if (!left) {
val = m - val - 1;
}
if (idx == k) {
return ret = val;
}
return ret = min(solve(idx - 1, 1 - left) + m,
solve(idx - 1, left) + val * 2 + 1);
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
m += 2;
while (s[k] == string(m, '0')) {
k++;
}
memset(dp, -1, sizeof(dp));
cout << solve() << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 5010, INF = 1e9;
long long n;
vector<long long> g[N];
long long dist[3][N];
void bfs(long long from, long long* dist, long long nastya) {
for (long long i = 1; i <= n; i++) dist[i] = INF;
dist[from] = 0;
queue<long long> q;
q.push(from);
while (!q.empty()) {
long long v = q.front();
q.pop();
for (long long to : g[v])
if (dist[to] == INF && to != nastya) dist[to] = dist[v] + 1, q.push(to);
}
}
array<long long, 5> calc(long long a, long long b, long long c, long long v,
long long first, long long second, long long z) {
long long neighbours = -1, mxdist = -1, a1 = -1, b1 = -1, c1 = -1;
neighbours = (set<long long>{first, second, z}).size();
mxdist = -max({dist[first][a], dist[second][b], dist[z][c]});
if (mxdist == -INF)
return neighbours = -INF,
array<long long, 5>{neighbours, mxdist, a1, b1, c1};
for (long long to : g[a])
if (dist[first][to] < dist[first][a]) a1 = to;
for (long long to : g[b])
if (dist[second][to] < dist[second][b]) b1 = to;
for (long long to : g[c])
if (dist[z][to] < dist[z][c]) c1 = to;
return array<long long, 5>{neighbours, mxdist, a1, b1, c1};
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(time(0) ^ ((long long)(new char)));
long long m;
cin >> n >> m;
while (m--) {
long long v, u;
cin >> v >> u;
g[v].push_back(u);
g[u].push_back(v);
}
long long a = rand() % n + 1, b = rand() % n + 1, c = rand() % n + 1;
a = b = c = 1;
cout << a << " " << b << " " << c << endl;
long long v;
cin >> v;
for (long long _ = 0; _ < n; _++) {
if (a == v || b == v || c == v) exit(0);
if (count(g[a].begin(), g[a].end(), v))
cout << v << " " << b << " " << c << endl, exit(0);
if (count(g[b].begin(), g[b].end(), v))
cout << a << " " << v << " " << c << endl, exit(0);
if (count(g[c].begin(), g[c].end(), v))
cout << a << " " << b << " " << v << endl, exit(0);
for (long long t = 0; t < g[v].size(); t++) bfs(g[v][t], dist[t], v);
array<long long, 5> res{-INF, -INF, -INF, -INF, -INF};
for (long long first = 0; first < g[v].size(); first++)
for (long long second = 0; second < g[v].size(); second++)
for (long long z = 0; z < g[v].size(); z++)
res = max(res, calc(a, b, c, v, first, second, z));
a = res[2], b = res[3], c = res[4];
cout << a << " " << b << " " << c << endl;
if (a == v || b == v || c == v) exit(0);
cin >> v;
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n, m, g[1100][1100], S, T, s[1100], t[1100], dp[1100], cnt[1100], Ans[1100];
bool cut[1100][1100], vis[1100];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int dfs(int u, int cur) {
if (vis[u]) return dp[u];
vis[u] = 1;
int tmp = -1;
for (int v = 1; v <= n; ++v)
if (g[u][v] == 1 && g[u][t[cur]] == g[v][t[cur]] + 1)
tmp = max(tmp, dfs(v, cur));
if (tmp == -1) tmp = 1e9;
tmp = min(tmp, Ans[u]);
return dp[u] = tmp;
}
int main() {
n = read(), m = read(), S = read(), T = read();
memset(g, 0x3f, sizeof(g));
for (int i = 1; i <= n; ++i) g[i][i] = 0;
while (m--) {
int u = read(), v = read();
g[u][v] = 1;
}
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
m = read();
for (int k = 1; k <= m; ++k) {
s[k] = read(), t[k] = read();
if (g[s[k]][t[k]] == 0x3f3f3f3f) continue;
for (int i = 1; i <= n; ++i)
if (g[s[k]][i] + g[i][t[k]] == g[s[k]][t[k]]) cnt[g[s[k]][i]]++;
for (int i = 1; i <= n; ++i)
if (g[s[k]][i] + g[i][t[k]] == g[s[k]][t[k]]) {
if (cnt[g[s[k]][i]] == 1) cut[k][i] = 1;
cnt[g[s[k]][i]] = 0;
}
}
bool flag = 1;
memset(dp, 0x3f, sizeof(dp));
memset(Ans, 0x3f, sizeof(Ans));
Ans[T] = 0;
while (flag) {
flag = 0;
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
if (cut[i][j]) {
memset(vis, 0, sizeof(vis));
int tmp = dfs(j, i) + 1;
if (Ans[j] > tmp) {
flag = 1;
Ans[j] = tmp;
}
}
}
if (Ans[S] > 233) Ans[S] = -1;
printf("%d\n", Ans[S]);
return 0;
}
| 9 |
/*
the vast starry sky,
bright for those who chase the light.
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define mk make_pair
const int inf=(int)1e9;
const ll INF=(ll)5e18;
const int MOD=998244353;
int _abs(int x){return x<0 ? -x : x;}
int add(int x,int y){x+=y; return x>=MOD ? x-MOD : x;}
int sub(int x,int y){x-=y; return x<0 ? x+MOD : x;}
#define mul(x,y) (ll)(x)*(y)%MOD
void Add(int &x,int y){x+=y; if(x>=MOD) x-=MOD;}
void Sub(int &x,int y){x-=y; if(x<0) x+=MOD;}
void Mul(int &x,int y){x=mul(x,y);}
int qpow(int x,int y){int ret=1; while(y){if(y&1) ret=mul(ret,x); x=mul(x,x); y>>=1;} return ret;}
void checkmin(int &x,int y){if(x>y) x=y;}
void checkmax(int &x,int y){if(x<y) x=y;}
void checkmin(ll &x,ll y){if(x>y) x=y;}
void checkmax(ll &x,ll y){if(x<y) x=y;}
#define out(x) cerr<<#x<<'='<<x<<' '
#define outln(x) cerr<<#x<<'='<<x<<endl
#define sz(x) (int)(x).size()
inline int read(){
int x=0,f=1; char c=getchar();
while(c>'9'||c<'0'){if(c=='-') f=-1; c=getchar();}
while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
const int N=200005;
int n,k,a[N],dis[N],dp[N];
vector<int> v[N];
int vis[N],bl[N];
int rt,mi=inf,mxdep,sz[N],dep[N],Tmpmx;
namespace BIT{
int a[N];
void init(){for(int i=1;i<=mxdep;i++) a[i]=0;}
int lowbit(int x){return x&(-x);}
void update(int x,int val){
for(int i=x;i;i-=lowbit(i)) checkmax(a[i],val);
}
int query(int x){
int ret=0;
for(int i=x;i<=mxdep;i+=lowbit(i)) checkmax(ret,a[i]);
return ret;
}
}
void findroot(int u,int f,int S){
sz[u]=1; int mx=0;
for(auto &to : v[u]){
if(to==f||vis[to]) continue;
findroot(to,u,S); sz[u]+=sz[to];
checkmax(mx,sz[to]);
}
int tmp=max(mx,S-sz[u]);
if(tmp<mi) mi=tmp,rt=u;
}
void dfs1(int u,int f){
sz[u]=1; checkmax(mxdep,dep[u]);
if(dep[u]<dis[u]) checkmax(Tmpmx,dis[u]);
for(auto &to : v[u]){
if(to==f||vis[to]) continue;
dep[to]=dep[u]+1; dfs1(to,u);
sz[u]+=sz[to];
}
}
void dfs2(int u,int f){
checkmax(dp[u],BIT::query(dep[u]));
for(auto &to : v[u]) if(!vis[to]&&to!=f) dfs2(to,u);
}
void dfs3(int u,int f){
if(dis[u]-dep[u]-1>0) BIT::update(min(mxdep,dis[u]-dep[u]-1),dis[u]);
for(auto &to : v[u]) if(!vis[to]&&to!=f) dfs3(to,u);
}
void solve(int x,int S){
if(S==1) return;
mi=inf,mxdep=0; Tmpmx=0;
findroot(x,-1,S); dep[rt]=0; dfs1(rt,-1);
BIT::init(); checkmax(dp[rt],Tmpmx);
if(dis[rt]>1) BIT::update(min(mxdep,dis[rt]-1),dis[rt]);
for(int i=0;i<sz(v[rt]);i++){
int to=v[rt][i]; if(vis[to]) continue;
dfs2(to,rt); dfs3(to,rt);
}
BIT::init();
if(dis[rt]>1) BIT::update(min(mxdep,dis[rt]-1),dis[rt]);
for(int i=sz(v[rt])-1;i>=0;i--){
int to=v[rt][i]; if(vis[to]) continue;
dfs2(to,rt); dfs3(to,rt);
}
vis[rt]=1;
for(auto &to : v[rt]) if(!vis[to]) solve(to,sz[to]);
}
int main()
{
n=read();
for(int i=1;i<n;i++){
int x=read(),y=read();
v[x].push_back(y); v[y].push_back(x);
}
k=read(); for(int i=1;i<=k;i++) a[i]=read(),bl[a[i]]=1;
queue<int> q;
for(int i=1;i<=n;i++) if(bl[i]) vis[i]=1,q.push(i);
while(!q.empty()){
int u=q.front(); q.pop();
for(auto &to : v[u]) if(!vis[to])
vis[to]=1,dis[to]=dis[u]+1,q.push(to);
}
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++) dp[i]=dis[i];
solve(1,n);
for(int i=1;i<=n;i++){
if(bl[i]) printf("0 ");
else printf("%d ",dp[i]);
}
return 0;
} | 9 |
#include <bits/stdc++.h>
using namespace std;
const long long int M = 1000000007;
long long int ncr(int n, int r) {
if (r > n) return 0;
if (r > n - r) r = n - r;
long long int ans = 1;
int i;
for (i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}
long long int power(long long int x, long long int n, long long int m) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return (power(((x % m) * (x % m)) % m, n / 2, m)) % m;
else
return ((x % m) * (power(x, n - 1, m) % m)) % m;
}
int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int dy[] = {1, 0, -1, 1, 0, -1, 1, 0, -1};
long long int modInverse(long long int a, long long int m) {
long long int m0 = m;
long long int y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long int q = a / m;
long long int t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
string binary(long long int n, long long int k) {
if (n == 0) return "0";
string ans;
while (n > 0) {
if (n & 1)
ans.push_back('1');
else
ans.push_back('0');
n = n >> 1;
}
long long int l = ans.length();
for (int j = 1; j <= k - l; j++) ans.push_back('0');
reverse(ans.begin(), ans.end());
return ans;
}
bool is_prime(long long int n) {
if (n == 1) return 0;
for (long long int i = 2; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
long long int sum_digit(long long int n) {
long long int ans = 0;
while (n > 0) {
ans += (n % 10);
n /= 10;
}
return ans;
}
long long int binary_to_decimal(string str) {
long long int ans = 0, d = 1;
for (int i = str.length() - 1; i >= 0; i--) {
ans += d * (str[i] - 48);
d *= 2;
}
return ans;
}
double round(double var) {
double value = (long long int)(var * 100 + .5);
return (double)value / 100;
}
char num[10][10];
long long int f = 0;
void fun(long long int i, long long int j, long long int step) {
if (step > 7) {
f += 1;
return;
}
if (i == 0 && j == 7) {
f += 1;
return;
}
for (int i1 = 0; i1 < 9; i1++) {
long long int x = dx[i1] + i;
long long int y = dy[i1] + j;
if (x < 0 || y < 0 || x > 7 || y > 7) {
continue;
}
if (x - step - 1 >= 0 && num[x - step - 1][y] == 'S') {
continue;
}
if (x - step >= 0 && num[x - step][y] == 'S') {
continue;
}
fun(x, y, step + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cin >> num[i][j];
}
}
fun(7, 0, 0);
if (f == 0)
cout << "LOSE";
else
cout << "WIN";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
bool Max(T1& a, T2 b) {
return a < b ? a = b, 1 : 0;
}
template <typename T1, typename T2>
bool Min(T1& a, T2 b) {
return a > b ? a = b, 1 : 0;
}
const int N = 100002, M = 100, K = 1001;
long long f[2 * K][2 * K];
set<int> pos[N];
int nxt[N], pre[N];
int a[N];
int nb;
void upd(int x, int y, int v) {
int bx = x / M, by = y / M;
v *= (y - x);
for (int x = bx + nb; x; x >>= 1) {
for (int y = by + nb; y; y >>= 1) {
f[x][y] += v;
}
}
}
long long que2(int x, int x1, int x2) {
long long res = 0;
for (x1 += nb, x2 += nb + 1; x1 < x2; x1 >>= 1, x2 >>= 1) {
if (x1 & 1) res += f[x][x1++];
if (x2 & 1) res += f[x][--x2];
}
return res;
}
long long que(int x, int y) {
long long res = 0;
for (int x1 = x + nb, x2 = y + nb + 1; x1 < x2; x1 >>= 1, x2 >>= 1) {
if (x1 & 1) res += que2(x1++, x, y);
if (x2 & 1) res += que2(--x2, x, y);
}
return res;
}
void update(int i, int x) {
int y = a[i];
if (y) {
upd(i, nxt[i], -1);
upd(pre[i], i, -1);
upd(pre[i], nxt[i], 1);
nxt[pre[i]] = nxt[i];
pre[nxt[i]] = pre[i];
pos[y].erase(i);
}
a[i] = x;
auto it = pos[x].insert(i).first;
nxt[i] = *(++it);
--it, --it;
pre[i] = *it;
nxt[pre[i]] = pre[nxt[i]] = i;
upd(pre[i], nxt[i], -1);
upd(pre[i], i, 1);
upd(i, nxt[i], 1);
}
long long query(int x, int y) {
int bx = x / M, by = y / M;
long long ret = 0;
if (bx == by) {
for (int i = x; i <= (y); ++i)
if (nxt[i] <= y) ret += nxt[i] - i;
return ret;
}
int xed = (bx + 1) * M - 1;
int ybg = by * M;
ret = que(bx + 1, by - 1);
for (int i = x; i <= (xed); ++i)
if (nxt[i] <= y) ret += nxt[i] - i;
for (int i = ybg; i <= (y); ++i)
if (pre[i] > xed) ret += i - pre[i];
return ret;
}
void solve() {
int n, q, v;
cin >> n >> q;
nb = (n + M) / M;
for (int i = 1; i <= (N - 1); ++i) {
pos[i].insert(0);
pos[i].insert(n + 1);
}
for (int i = 1; i <= (n); ++i) {
cin >> v;
update(i, v);
}
while (q--) {
int t, x, y;
cin >> t >> x >> y;
if (t == 1) {
update(x, y);
} else {
cout << query(x, y) << '\n';
}
}
}
void init() {}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.setf(ios::fixed);
cout.precision(20);
init();
int TC = 1;
for (int TI = 1; TI <= (TC); ++TI) {
solve();
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
string s;
cin >> s;
vector<int> v(s.size());
for (int i = 0; i < s.size(); i++) {
if (s[i] == '0') {
v[i] = 0;
} else {
v[i] = 1;
}
}
vector<int> prefmin(s.size());
int prefcnt0 = 0;
int prefcnt1 = 0;
for (int i = 0; i < s.size(); i++) {
if (v[i] == 0) {
prefcnt0++;
} else {
prefcnt1++;
}
prefmin[i] = min(prefcnt0, prefcnt1);
}
int sufcnt0 = 0;
int sufcnt1 = 0;
vector<int> sufmin(s.size());
for (int i = s.size() - 1; i >= 0; i--) {
if (v[i] == 0) {
sufcnt0++;
} else {
sufcnt1++;
}
sufmin[i] = min(sufcnt0, sufcnt1);
}
int ans = min(prefcnt0, prefcnt1);
for (int i = 0; i < s.size() - 1; i++) {
ans = min(ans, prefmin[i] + sufmin[i + 1]);
}
cout << ans << "\n";
}
int main() {
int tt;
cin >> tt;
while (tt--) {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int read();
inline void write(int x);
const int M = 500016, MOD = 1000000007;
int save[M];
int main(void) {
int n = read(), m = read();
for (int i = 0; i < n; i++) putchar('4');
putchar('\n');
for (int i = 0; i < n - 1; i++) putchar('5');
putchar('6');
putchar('\n');
return 0;
}
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k;
double n, m;
cin >> n >> m;
printf("%.7f\n",
(n * m - 1 ? (1 / n + (n - 1) * (m - 1) / n / (n * m - 1)) : 1));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct dat {
long long scm, scl, tm, tl;
long double pro;
bool operator<(dat a) const {
return tl * 1e6 * (1 - pro) * a.pro < a.tl * 1e6 * (1 - a.pro) * pro;
}
} plm[1005];
pair<long double, long double> dp[1005][1561];
int main() {
int n, t;
cin >> n >> t;
for (int i = 0; i < n; i++) {
long long x1, x2, t1, t2;
long double p;
cin >> x1 >> x2 >> t1 >> t2 >> p;
plm[i].scm = x1;
plm[i].scl = x2;
plm[i].tm = t1;
plm[i].tl = t2;
plm[i].pro = 1 - p;
}
sort(plm, plm + n);
dp[0][0] = make_pair(0, 0);
for (int i = 0; i <= t; i++) dp[0][i] = make_pair(0, 0);
dp[0][(int)plm[0].tm] = make_pair(plm[0].scm * 1e6, -plm[0].tm * 1e6);
dp[0][(int)plm[0].tl + plm[0].tm] =
make_pair(plm[0].scm * 1e6 + plm[0].scl * 1e6 * plm[0].pro,
-plm[0].tm * 1e6 - plm[0].tl * 1e6 * plm[0].pro);
for (int i = 1; i < n; i++) {
for (int j = 0; j <= t; j++) {
dp[i][j] = dp[i - 1][j];
long double p = plm[i].pro;
long long x1 = plm[i].scm, x2 = plm[i].scl, t1 = plm[i].tm,
t2 = plm[i].tl;
if (t1 <= j) {
long double la1 = dp[i - 1][j - t1].first,
la2 = -dp[i - 1][j - t1].second;
dp[i][j] = max(dp[i][j], make_pair(la1 + x1 * 1e6, -la2 - t1 * 1e6));
}
if (t1 + t2 <= j) {
long double la1 = dp[i - 1][j - t1 - t2].first,
la2 = -dp[i - 1][j - t1 - t2].second;
dp[i][j] =
max(dp[i][j], make_pair(la1 + x1 * 1e6 + x2 * 1e6 * p,
-p * j * 1e6 - (1 - p) * (t1 * 1e6 + la2)));
}
}
}
pair<long double, long double> res = make_pair(0, 0);
for (int i = 0; i <= t; i++) {
res = max(res, dp[n - 1][i]);
}
cout << fixed << setprecision(15) << res.first / 1e6 << " "
<< -res.second / 1e6;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 7;
const int INF = (int)1e9 + 7;
const long long linf = (long long)1e18 + 1;
const int block = 350;
int cnt[block + 2][N];
deque<int> dq[block + 1];
void upd(int l, int r) {
int L = l / block, R = r / block;
int r_val = dq[R][r % block];
cnt[R][r_val]--;
dq[R].erase(dq[R].begin() + (r % block));
cnt[L][r_val]++;
dq[L].insert(dq[L].begin() + (l % block), r_val);
for (int i = L; i < R; ++i) {
int val = dq[i].back();
--cnt[i][val];
dq[i].pop_back();
++cnt[i + 1][val];
dq[i + 1].push_front(val);
}
}
int get(int l, int r, int k) {
int L = l / block, R = r / block;
int res = 0;
if (L == R) {
int p = 0;
for (auto to : dq[L]) {
if (l % block <= p && p <= r % block && to == k) ++res;
++p;
}
} else {
int p = 0;
for (auto to : dq[L]) {
if (l % block <= p && to == k) ++res;
++p;
}
p = 0;
for (auto to : dq[R]) {
if (p <= r % block && to == k) ++res;
++p;
}
for (int i = L + 1; i <= R - 1; ++i) res += cnt[i][k];
}
return res;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
dq[i / block].push_back(x);
cnt[i / block][x]++;
}
int q;
scanf("%d", &q);
int lastans = 0;
for (int i = 1; i <= q; ++i) {
int type, l, r;
scanf("%d %d %d", &type, &l, &r);
l = ((l + lastans - 1) % n), r = ((r + lastans - 1) % n);
if (l > r) swap(l, r);
if (type == 2) {
int k;
scanf("%d", &k);
k = ((k + lastans - 1) % n) + 1;
int cur = get(l, r, k);
printf("%d\n", cur);
lastans = cur;
} else
upd(l, r);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 2e6 + 5;
int n, m, a[N], cnt, num, all;
long long ans[N];
set<int> st1[N], st2[N];
set<int>::iterator it;
struct S {
int x, y, y1, y2, f, v, id;
} s[M], t[M];
struct BIT {
long long t[N];
inline int lowbit(int x) { return x & -x; }
inline void add(int x, int v) {
while (x <= n) t[x] += v, x += lowbit(x);
}
inline void add(int x, int y, int v) {
add(x, v);
if (y < n) add(y + 1, -v);
}
inline long long query(int x) {
long long tmp = 0;
while (x) tmp += t[x], x -= lowbit(x);
return tmp;
}
inline void clear(int x) {
while (x <= n) t[x] = 0, x += lowbit(x);
}
inline void clear(int x, int y) {
clear(x);
if (y < n) clear(y + 1);
}
} tt;
inline void add(int x, int y, int y1, int y2, int f, int v, int id) {
if (x >= 1 && x <= n && y1 >= 1 && y1 <= n && y2 >= 1 && y2 <= n && y1 <= y2)
s[++cnt] = (S){x, y, y1, y2, f, v, id};
}
inline void add2(int x, int y, int y1, int y2, int f, int v, int id) {
s[++cnt] = (S){x, y, y1, y2, f, v, id};
}
inline bool cmp(S a, S b) {
if (a.x != b.x) return a.x < b.x;
return a.f > b.f;
}
inline void solve(int l, int r) {
if (l == r) return;
int mid = (l + r) >> 1;
solve(l, mid);
all = 0;
for (int i = l; i <= mid; i++)
if (s[i].f == -1 || s[i].f == 1) t[++all] = s[i];
for (int i = mid + 1; i <= r; i++)
if (s[i].f == 0) t[++all] = s[i];
sort(t + 1, t + all + 1, cmp);
for (int i = 1; i <= all; i++) {
if (t[i].f == 1) tt.add(t[i].y1, t[i].y2, t[i].v);
if (t[i].f == -1) tt.add(t[i].y1, t[i].y2, -t[i].v);
if (t[i].f == 0) ans[t[i].id] += tt.query(t[i].y);
}
for (int i = 1; i <= all; i++) {
if (t[i].f == 1) tt.clear(t[i].y1, t[i].y2);
if (t[i].f == -1) tt.clear(t[i].y1, t[i].y2);
}
solve(mid + 1, r);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
int L = 0, R = n + 1, M = i;
it = st1[a[M]].upper_bound(-M);
if (it != st1[a[M]].end()) L = -(*it);
it = st2[a[M]].upper_bound(M);
if (it != st2[a[M]].end()) R = (*it);
if (R != n + 1) add(L + 1, 0, R, n, 1, R - M, 0);
if (R != n + 1) add(M, 0, R, n, -1, R - M, 0);
if (L != 0) add(1, 0, M, R - 1, 1, M - L, 0);
if (L != 0) add(L, 0, M, R - 1, -1, M - L, 0);
st1[a[i]].insert(-i), st2[a[i]].insert(i);
}
while (m--) {
int t, x, y;
scanf("%d%d%d", &t, &x, &y);
if (t == 1) {
int L = 0, R = n + 1, M = x;
it = st1[a[M]].upper_bound(-M);
if (it != st1[a[M]].end()) L = -(*it);
it = st2[a[M]].upper_bound(M);
if (it != st2[a[M]].end()) R = (*it);
if (R != n + 1) add(L + 1, 0, R, n, 1, -(R - M), 0);
if (R != n + 1) add(M, 0, R, n, -1, -(R - M), 0);
if (L != 0) add(1, 0, M, R - 1, 1, -(M - L), 0);
if (L != 0) add(L, 0, M, R - 1, -1, -(M - L), 0);
st1[a[M]].erase(-M);
st2[a[M]].erase(M);
a[M] = y;
st1[a[M]].insert(-M);
st2[a[M]].insert(M);
L = 0, R = n + 1, M = x;
it = st1[a[M]].upper_bound(-M);
if (it != st1[a[M]].end()) L = -(*it);
it = st2[a[M]].upper_bound(M);
if (it != st2[a[M]].end()) R = (*it);
if (R != n + 1) add(L + 1, 0, R, n, 1, R - M, 0);
if (R != n + 1) add(M, 0, R, n, -1, R - M, 0);
if (L != 0) add(1, 0, M, R - 1, 1, M - L, 0);
if (L != 0) add(L, 0, M, R - 1, -1, M - L, 0);
}
if (t == 2) add2(x, y, 0, 0, 0, 0, ++num);
}
solve(1, cnt);
for (int i = 1; i <= num; i++) printf("%I64d\n", ans[i]);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int NAX = 3e5 + 1;
const int INF = 1e9 + 1;
const long long UMAX = 2e18 + 1;
long long sm(long long n) {
int res = 0;
while (n > 0) {
res += n % 10;
n /= 10;
}
return res;
}
void solve() {
long long n;
cin >> n;
vector<long long> v(n);
vector<long long> ans;
for (int i = 0; i < n; ++i) cin >> v[i];
sort(v.begin(), v.end());
long long p1 = v[0] * v[1] * v[2] * v[3] * v[n - 1];
long long p2 = v[n - 1] * v[n - 2] * v[n - 3] * v[n - 4] * v[n - 5];
long long p3 = v[0] * v[1] * v[n - 1] * v[n - 2] * v[n - 3];
ans.push_back(p1);
ans.push_back(p2);
ans.push_back(p3);
cout << *max_element(ans.begin(), ans.end()) << '\n';
;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc = 1;
cin >> tc;
while (tc--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int r, x, y, xd, yd;
cin >> r >> x >> y >> xd >> yd;
int dx = fabs(xd - x);
int dy = fabs(yd - y);
cout << ceil(sqrt(pow(dx, 2) + pow(dy, 2)) / (2 * r)) << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, s1 = 0, s2 = 0, s3 = 0;
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
s1 += a;
}
for (int i = 0; i < n - 1; ++i) {
int a;
cin >> a;
s2 += a;
}
for (int i = 0; i < n - 2; ++i) {
int a;
cin >> a;
s3 += a;
}
cout << s1 - s2 << endl << s2 - s3 << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long int decnum, rem, quot;
int i = 1, j, temp;
cin >> decnum;
quot = decnum;
stringstream ss;
ss << std::hex << decnum;
string hexdecnum(ss.str());
int x = 0;
for (int p = 0; p < hexdecnum.length(); p++) {
if (hexdecnum[p] == '0')
x += 1;
else if (hexdecnum[p] == '4')
x += 1;
else if (hexdecnum[p] == '6')
x += 1;
else if (hexdecnum[p] == '8')
x += 2;
else if (hexdecnum[p] == '9')
x += 1;
else if (hexdecnum[p] == 'a')
x += 1;
else if (hexdecnum[p] == 'b')
x += 2;
else if (hexdecnum[p] == 'd')
x += 1;
}
cout << x << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, q;
int cnt[500001];
bool prime[500001];
vector<int> P[500001];
int a[500001];
bool f[500001];
long long Calc(vector<int> a) {
int n = a.size();
long long ans = 0;
for (int mask = 1; mask < 1 << n; mask++) {
int count = 0;
long long x = 1;
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
x *= a[i];
count++;
}
}
x = cnt[x];
if (count % 2) {
ans += x;
} else {
ans -= x;
}
}
return ans;
}
void update(int x, int val) {
vector<int> a = P[x];
int n = a.size();
for (int mask = 1; mask < 1 << n; mask++) {
int x = 1;
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) x *= a[i];
}
cnt[x] += val;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (int i = 2; i <= 5e5; i++) {
if (prime[i] == false) {
for (int j = i; j <= 5e5; j += i) {
prime[j] = true;
P[j].push_back(i);
}
}
}
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int C = 0;
long long ans = 0;
while (q--) {
int id;
cin >> id;
if (!f[id]) {
ans += Calc(P[a[id]]);
update(a[id], 1);
f[id] = true;
++C;
} else {
update(a[id], -1);
ans -= Calc(P[a[id]]);
f[id] = false;
--C;
}
cout << 1LL * C * (C - 1) / 2 - ans << "\n";
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long get() {
char c = getchar();
long long x = 0LL;
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') {
x *= 10LL;
x += (c - '0');
c = getchar();
}
return x;
}
map<string, long long> mp, f;
string s;
long long ans1 = 0, ans2 = 0;
int main() {
ios::sync_with_stdio(0);
while (cin >> s) {
long long ans = 0;
while (1) {
long long x;
for (int i = s.size() - 1; i >= 1; i--)
if (s[i] == '\\') {
x = i;
break;
}
if (x == 2) break;
s = s.substr(0, x);
long long y = f[s];
mp[s] += ans;
f[s]++;
ans1 = max(mp[s], ans1);
ans2 = max(ans2, f[s]);
if (!y) ans++;
}
}
cout << ans1 << " " << ans2;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1E9 + 7;
inline int Add(int a, int b) {
return (a + b >= MOD) ? (a + b - MOD) : (a + b);
}
inline int Sub(int a, int b) { return (a >= b) ? (a - b) : (a - b + MOD); }
inline int Mul(int a, int b) { return (long long)a * b % MOD; }
const int N = 200005, P = 2000005;
int n;
int p[P], mnp[P], mnpc[P], mnpp[P];
bool ntp[P];
void Sieve(int n) {
for (int i = 2; i <= n; ++i) {
if (!ntp[i]) {
p[++*p] = i;
mnp[i] = mnpp[i] = i;
mnpc[i] = 1;
}
for (int j = 1, t = 0; j <= *p && (t = p[j] * i) <= n; ++j) {
ntp[t] = 1;
if (i % p[j]) {
mnp[t] = mnpp[t] = p[j];
mnpc[t] = 1;
} else {
mnp[t] = mnp[i];
mnpc[t] = mnpc[i] + 1;
mnpp[t] = mnpp[i] * p[j];
break;
}
}
}
return;
}
int cnt[P], r[P], c[P], v[P];
int main() {
Sieve(P - 1);
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
cnt[x]++;
}
int tp = 0;
for (int i = P - 1; i; --i)
if (cnt[i]) {
if (!c[i]) {
r[i] = i;
c[i] = 1;
if (cnt[i] > 1) {
v[i] = 1;
for (int x = i - 1; x > 1; x /= mnpp[x])
if (r[mnp[x]] < mnpp[x]) {
r[mnp[x]] = mnpp[x];
c[mnp[x]] = 1;
} else if (r[mnp[x]] == mnpp[x])
c[mnp[x]]++;
}
if (cnt[i] > 2) {
v[i] = 0;
tp = 1;
}
} else {
v[i] = 1;
for (int x = i - 1; x > 1; x /= mnpp[x])
if (r[mnp[x]] < mnpp[x]) {
r[mnp[x]] = mnpp[x];
c[mnp[x]] = 1;
} else if (r[mnp[x]] == mnpp[x])
c[mnp[x]]++;
if (cnt[i] > 1) {
v[i] = 0;
tp = 1;
}
}
}
for (int i = 1; i < P && !tp; ++i)
if (v[i]) {
bool flag = 1;
for (int x = i - 1; x > 1 && flag; x /= mnpp[x])
if (r[mnp[x]] < mnpp[x] || (r[mnp[x]] == mnpp[x] && c[mnp[x]] == 1))
flag = 0;
if (flag) tp = 1;
}
int ret = 1;
for (int i = 1; i < P; ++i)
if (c[i]) ret = Mul(ret, r[i]);
ret = Add(ret, tp);
printf("%d\n", ret);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
long long gcd(long long a, long long b) {
if (a < b) {
return gcd(b, a);
}
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int n;
vector<long long> a;
long long p, q;
void input_data() {
ios_base::sync_with_stdio(false);
cin >> n;
a.resize(n);
p = 0LL;
q = (long long)n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a.begin(), a.end());
long long csum = 0LL;
for (int i = 0; i < n; ++i) {
p += 2LL * ((long long)i * a[i] - csum) + ((a[i] >= 0) ? a[i] : -a[i]);
csum += a[i];
}
long long g = gcd(p, q);
p /= g;
q /= g;
cout << p << " " << q << "\n";
}
int main() {
input_data();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, b;
cin >> n >> b;
vector<int> v;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
v.push_back(a);
}
int odd = 0;
int even = 0;
vector<int> cost;
int cuts = 0;
for (int i = 0; i < n; i++) {
if (v[i] % 2 == 0)
even++;
else
odd++;
if (even == odd && i + 1 < n) {
cost.push_back(abs(v[i] - v[i + 1]));
even = 0;
odd = 0;
}
}
sort(cost.begin(), cost.end());
int sumofcuts = 0;
vector<int>::iterator it = cost.begin();
while (sumofcuts <= b && it != cost.end()) {
sumofcuts += *it;
it++;
if (sumofcuts <= b) cuts++;
}
cout << cuts << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool sortmahstyle(const pair<int, int> &a, const pair<int, int> &b) {
if (a.first == b.first && a.second < b.second) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
while (t-- > 0) {
long long int r, b;
cin >> r >> b;
long long int vasya = min(r, b);
long long int petya = max(r, b) - 1;
cout << petya << " " << vasya << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a[2000][2000];
int x[2000], y[2000];
int main() {
cin >> n >> m;
cin >> k;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) cin >> a[i][j];
for (int i = 0; i < n; ++i) x[i] = i;
for (int j = 0; j < m; ++j) y[j] = j;
for (int i = 0; i < k; ++i) {
int f, s;
char c;
cin >> c >> f >> s;
--f;
--s;
if (c == 'c') {
swap(y[f], y[s]);
}
if (c == 'r') {
swap(x[f], x[s]);
}
if (c == 'g') {
printf("%d\n", a[x[f]][y[s]]);
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s, a, b;
cin >> n >> s;
vector<int> arr1(n), arr2(n), arr;
for (int x = 0; x < n; x++) cin >> arr1[x] >> arr2[x];
for (int x = 0; x < n - 1; x++) {
for (int y = x + 1; y < n; y++) {
if (arr2[x] > arr2[y]) {
swap(arr2[x], arr2[y]);
swap(arr1[x], arr1[y]);
}
}
}
bool check1 = 0, check2 = 0;
for (int x = 0; x < n; x++) {
if (s > arr1[x]) {
check1 = 1;
break;
} else if (s == arr1[x] && arr2[x] == 0) {
check2 = 1;
break;
}
}
if (check1 == 0 && check2 == 0) {
cout << -1;
return 0;
}
for (int x = 0; x < n; x++) {
if (s > arr1[x] && (100 - arr2[x]) != 100) {
cout << 100 - arr2[x];
return 0;
}
}
cout << "0";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a, b;
long long answer = 0;
int main(int argc, char* argv[]) {
scanf("%d", &n);
a.resize(n);
b.resize(n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n; i++) scanf("%d", &b[i]);
long long current_balance = 0;
for (int i = 0; i < n; i++) {
long long current_element = a[i] + current_balance;
answer += abs(b[i] - current_element);
current_balance += b[i] - current_element;
}
cout << answer << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int inf = int(2e9);
const int _inf = -inf;
const int mod = inf + 7;
vector<int> gr1[26];
vector<int> gr2[26];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
string s1, s2;
cin >> n >> s1 >> s2;
vector<int> q1, q2;
vector<int> t1, t2;
for (int i = 0; i < n; ++i) {
if (s1[i] == '?')
q1.push_back(i);
else
gr1[s1[i] - 'a'].push_back(i);
if (s2[i] == '?')
q2.push_back(i);
else
gr2[s2[i] - 'a'].push_back(i);
}
vector<pair<int, int> > res;
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < min(gr1[i].size(), gr2[i].size()); j++) {
res.push_back(make_pair(gr1[i][j], gr2[i][j]));
}
for (int j = min(gr1[i].size(), gr2[i].size()); j < gr1[i].size(); ++j) {
t1.push_back(gr1[i][j]);
}
for (int j = min(gr1[i].size(), gr2[i].size()); j < gr2[i].size(); ++j) {
t2.push_back(gr2[i][j]);
}
}
int f1 = q2.size() - min(t1.size(), q2.size());
int f2 = q1.size() - min(q1.size(), t2.size());
cout << res.size() + min(t1.size(), q2.size()) + min(q1.size(), t2.size()) +
min(f1, f2)
<< endl;
for (int i = 0; i < res.size(); ++i) {
cout << res[i].first + 1 << " " << res[i].second + 1 << endl;
}
for (int i = 0; i < min(t1.size(), q2.size()); i++) {
cout << t1[i] + 1 << " " << q2[i] + 1 << endl;
}
for (int i = 0; i < min(t2.size(), q1.size()); i++) {
cout << q1[i] + 1 << " " << t2[i] + 1 << endl;
}
int i1 = min(q1.size(), t2.size());
int i2 = min(t1.size(), q2.size());
while (i1 != q1.size() && i2 != q2.size()) {
cout << q1[i1] + 1 << " " << q2[i2] + 1 << endl;
i1++;
i2++;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
int out(int x) {
int l, r;
if (x > 0)
l = n - x, r = n;
else
l = 1 - x, r = 1;
cout << "?";
for (int i = 1; i <= n; i++) cout << " " << (i != n ? l : r);
cout << endl;
int ret;
cin >> ret;
return ret;
}
int a[105];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> n;
int now = n;
for (int i = 1; i < n; i++) {
int x = out(n - now + 1);
if (x == 0)
break;
else
a[x] = n - now + 1;
now--;
}
for (int i = 1; i < now; i++) a[out(i - now)] = i - now;
for (int i = 1; i <= n; i++) a[i] += now;
cout << "!";
for (int i = 1; i <= n; i++) cout << " " << a[i];
cout << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
long long x;
int a[110000], b[110000], c[110000], pos[110000], B[110000];
int n, d, tot;
int getNextX() {
x = (x * 37 + 10007) % 1000000007;
return x;
}
void init() {
for (int i = 0; i < n; i = i + 1) {
a[i] = i + 1;
}
for (int i = 0; i < n; i = i + 1) {
swap(a[i], a[getNextX() % (i + 1)]);
}
for (int i = 0; i < n; i = i + 1) {
if (i < d)
b[i] = 1;
else
b[i] = 0;
}
for (int i = 0; i < n; i = i + 1) {
swap(b[i], b[getNextX() % (i + 1)]);
}
}
int main() {
scanf("%d%d%I64d", &n, &d, &x);
init();
for (int i = 0; i <= n - 1; ++i) pos[a[i]] = i;
for (int i = 0; i < n; ++i)
if (b[i]) B[++tot] = i;
int cnt = n - 1;
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= tot; ++j) {
int x = pos[i] + B[j];
if (x > cnt) break;
if (!c[x]) c[x] = i;
}
while (cnt >= 0 && c[cnt]) cnt--;
if (cnt == -1) break;
}
for (int i = 0; i < n; ++i) printf("%d\n", c[i]);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, c[26], i, j, min_char, max_char;
char in[100005];
while (scanf("%d%d%s", &n, &k, in) != EOF) {
memset(c, 0, sizeof(int) * 26);
min_char = 26;
max_char = -1;
for (i = 0; i < strlen(in); i++) {
c[in[i] - 'a']++;
min_char = min(min_char, in[i] - 'a');
max_char = max(max_char, in[i] - 'a');
}
if (n < k) {
printf("%s", in);
for (i = n; i < k; i++) printf("%c", min_char + 'a');
printf("\n");
} else {
for (i = k - 1; i >= 0; i--) {
if (in[i] - 'a' == max_char) {
in[i] = min_char + 'a';
} else {
for (j = in[i] - 'a' + 1; j < 26; j++) {
if (c[j] > 0) break;
}
in[i] = j + 'a';
break;
}
}
in[k] = 0;
printf("%s\n", in);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct team {
string name;
int score, goaldiffs, totalgoals;
team() {
name = "";
score = 0;
goaldiffs = 0;
totalgoals = 0;
}
bool operator<(const team& other) const {
if (score < other.score)
return true;
else if (score == other.score) {
if (goaldiffs < other.goaldiffs)
return true;
else if (goaldiffs == other.goaldiffs) {
if (totalgoals < other.totalgoals)
return true;
else if (totalgoals == other.totalgoals) {
if (name > other.name)
return true;
else
return false;
} else
return false;
} else
return false;
} else
return false;
}
} teams[4];
struct solution {
int x, y;
solution(int a, int b) {
x = a;
y = b;
}
bool operator<(const solution& other) const {
if (x - y < other.x - other.y)
return true;
else if (x - y == other.x - other.y)
return (y < other.y);
else
return false;
}
};
string missing;
map<string, pair<int, int> > M;
map<string, int> points;
map<string, int> cnt;
vector<solution> Ans;
inline bool won(int i, int j) {
int cnt = 0;
for (map<string, pair<int, int> >::iterator it = M.begin(); it != M.end();
it++) {
string name = (*it).first;
int goalx = (*it).second.first, goaly = (*it).second.second;
if (name == "BERLAND") {
goalx += i;
goaly += j;
} else if (name == missing) {
goaly += i;
goalx += j;
}
teams[cnt].score = points[name];
teams[cnt].name = name;
teams[cnt].goaldiffs = goalx - goaly;
teams[cnt].totalgoals = goalx;
cnt++;
}
sort(teams, teams + 4);
if (teams[2].name == "BERLAND" || teams[3].name == "BERLAND")
return true;
else
return false;
}
int main() {
cin.sync_with_stdio(0);
for (int i = 0; i < 5; i++) {
string name1, name2, score;
cin >> name1 >> name2 >> score;
cnt[name1]++;
cnt[name2]++;
int scores_first = score[0] - '0', scores_second = score[2] - '0';
pair<int, int> pii = M[name1];
pii.first += scores_first;
pii.second += scores_second;
M[name1] = pii;
pii = M[name2];
pii.first += scores_second;
pii.second += scores_first;
M[name2] = pii;
if (scores_first > scores_second)
points[name1] += 3;
else if (scores_second > scores_first)
points[name2] += 3;
else if (scores_first == scores_second) {
points[name1]++;
points[name2]++;
}
}
for (map<string, int>::iterator it = cnt.begin(); it != cnt.end(); it++)
if ((*it).second == 2 && (*it).first != "BERLAND") missing = (*it).first;
points["BERLAND"] += 3;
Ans.reserve(5000);
for (int i = 1; i <= 1000; i++)
for (int j = i - 1; j >= 0; j--)
if (won(i, j)) Ans.push_back(solution(i, j));
if (Ans.empty())
cout << "IMPOSSIBLE" << endl;
else {
sort(Ans.begin(), Ans.end());
cout << Ans[0].x << ":" << Ans[0].y << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int inf = 1e9;
int a[maxn];
int par[maxn];
vector<pair<int, int>> edge_max[maxn], edge_min[maxn];
long long cur_path_cnt, last_path_cnt;
long long C(int a) { return 1ll * a * (a - 1) / 2; }
int get_par(int v) { return par[v] < 0 ? v : par[v] = get_par(par[v]); }
void merge(int v, int u) {
v = get_par(v), u = get_par(u);
if (par[v] < par[u]) {
swap(v, u);
}
cur_path_cnt -= par[v] == -1 ? 0 : C(-par[v]);
cur_path_cnt -= par[u] == -1 ? 0 : C(-par[u]);
par[u] += par[v];
par[v] = u;
cur_path_cnt += C(-par[u]);
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
cin >> n;
int maxa = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
maxa = max(maxa, a[i]);
}
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
edge_max[max(a[--x], a[--y])].push_back({x, y});
edge_min[min(a[x], a[y])].push_back({x, y});
}
long long ans = 0;
memset(par, -1, sizeof par);
last_path_cnt = 0;
for (int i = 1; i <= maxa; i++) {
cur_path_cnt = last_path_cnt;
for (auto e : edge_max[i]) {
merge(e.first, e.second);
}
ans += 1ll * i * (cur_path_cnt - last_path_cnt);
last_path_cnt = cur_path_cnt;
}
memset(par, -1, sizeof par);
last_path_cnt = 0;
for (int i = maxa; i > 0; i--) {
cur_path_cnt = last_path_cnt;
for (auto e : edge_min[i]) {
merge(e.first, e.second);
}
ans -= 1ll * i * (cur_path_cnt - last_path_cnt);
last_path_cnt = cur_path_cnt;
}
cout << ans << '\n';
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long double w;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> w >> m;
long double capacity = w * n / m;
bool ok = 1;
vector<pair<int, long double>> ans[m + 5];
int cnt = 0, idxg = 0, idxb = 1;
long double residue = capacity, cur = w;
while (ok && idxb <= n) {
long double val = min(cur, residue);
residue -= val;
cur -= val;
cnt++;
ans[idxg].push_back({idxb, val});
if (residue < 1e-6) residue = capacity, idxg++;
if (cur < 1e-6)
cur = w, cnt = 0, idxb++;
else if (cnt >= 2)
ok = 0;
}
if (!ok)
cout << "NO" << '\n';
else {
cout << "YES" << '\n';
cout << fixed << setprecision(6);
for (int i = 0; i < m; i++) {
for (auto& it : ans[i]) cout << it.first << ' ' << it.second << ' ';
cout << '\n';
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long max1 = (long long)1e9;
int main() {
long long i, j, k, n, m, ct = 0, t, ans = 0;
cin >> n;
string str;
cin >> str;
long long l = str.size() / 3, l0 = 0, l1 = 0, l2 = 0;
for (i = 0; i < str.size(); i++) {
if (str[i] == '0')
l0++;
else if (str[i] == '1')
l1++;
else
l2++;
}
if (l0 < l) {
i = 0;
while (l0 != l) {
if (str[i] == '1' && l1 > l) {
str[i] = '0';
l1--;
l0++;
} else if (str[i] == '2' && l2 > l) {
str[i] = '0';
l2--;
l0++;
}
i++;
}
}
if (l2 < l) {
i = n - 1;
while (l2 != l) {
if (str[i] == '1' && l1 > l) {
str[i] = '2';
l1--;
l2++;
} else if (str[i] == '0' && l0 > l) {
str[i] = '2';
l0--;
l2++;
}
i--;
}
}
if (l1 < l) {
i = 0;
while (l2 != l) {
if (str[i] == '2' && l2 > l) {
str[i] = '1';
l2--;
l1++;
}
i++;
}
i = n - 1;
while (l0 != l) {
if (str[i] == '0' && l0 > l) {
str[i] = '1';
l0--;
l1++;
}
i--;
}
}
for (i = 0; i < str.size(); i++) cout << str[i];
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, bmax, breg;
int t = 0;
int tdmg, total;
int j = 0;
struct node {
int p, d;
bool used;
} scroll[1000 + 10];
struct nodeans {
int id;
int t;
} ans[1000 + 10];
int main(int argc, char const *argv[]) {
scanf("%d%d%d", &n, &bmax, &breg);
for (int i = 0; i < n; i++) {
scanf("%d%d", &scroll[i].p, &scroll[i].d);
scroll[i].used = false;
}
total = bmax;
for (t = 0; t < 3000; t++) {
bmax -= tdmg;
bmax += breg;
bmax = min(bmax, total);
if (bmax <= 0) break;
int tmp = 0;
for (int i = 0; i < n; i++) {
if ((bmax * 1.0 / total) <= (scroll[i].p * 1.0 / 100) &&
scroll[i].used == false && scroll[i].d > tmp) {
tmp = scroll[i].d;
}
}
if (tmp > 0) {
for (int i = 0; i < n; i++) {
if ((bmax * 1.0 / total) <= (scroll[i].p * 1.0 / 100) &&
scroll[i].used == false && scroll[i].d == tmp) {
scroll[i].used = true;
tdmg += scroll[i].d;
ans[j].id = i + 1;
ans[j++].t = t;
break;
}
}
}
}
if (t == 3000) {
printf("NO\n");
return 0;
}
printf("YES\n");
printf("%d %d\n", t, j);
for (int i = 0; i < j; i++) {
printf("%d %d\n", ans[i].t, ans[i].id);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
long long n, m, k;
vector<pair<int, int> > v[N];
bool store[N];
void init() {
cin >> n >> m >> k;
int a, b, c;
for (int i = 0; i < m; ++i) {
cin >> a >> b >> c;
v[a].push_back(make_pair(b, c));
v[b].push_back(make_pair(a, c));
}
for (int i = 0; i < k; ++i) {
cin >> a;
store[a] = true;
}
}
void solve() {
long long ans = 1000000006;
for (int i = 1; i <= n; ++i) {
if (store[i]) continue;
for (int j = 0; j < v[i].size(); ++j)
if (store[v[i][j].first] && v[i][j].second < ans) {
ans = v[i][j].second;
}
}
if (ans == 1000000006)
cout << "-1" << endl;
else
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int __ = 1;
while (__--) {
init();
solve();
}
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2501;
const long long inf = 1e10;
template <class T, int... N>
struct Fenwick {
T v = 0;
void update(T v) { this->v ^= v; }
T query() { return v; }
};
template <class T, int N, int... M>
struct Fenwick<T, N, M...> {
Fenwick<T, M...> fenw[N + 1];
template <typename... Args>
void update(int i, Args... args) {
for (; i <= N; i += (i & -i)) fenw[i].update(args...);
}
template <typename... Args>
T query(int i, Args... args) {
T v = 0;
for (; i > 0; i -= (i & -i)) v ^= fenw[i].query(args...);
return v;
}
};
Fenwick<long long, N, N> mat;
map<int, map<int, long long>> val[N][N];
int n, m, q;
int main() {
cin.tie(0)->sync_with_stdio(0), cout.tie(0);
cin >> n >> m >> q;
int t, r1, r2, c1, c2;
while (q--) {
cin >> t >> r1 >> c1 >> r2 >> c2;
if (t == 1) {
long long k = 1 + rng() % inf;
mat.update(r1, c1, k);
mat.update(r1, c2 + 1, k);
mat.update(r2 + 1, c1, k);
mat.update(r2 + 1, c2 + 1, k);
val[r1][r2][c1][c2] = k;
} else if (t == 2) {
long long k = val[r1][r2][c1][c2];
mat.update(r1, c1, k);
mat.update(r1, c2 + 1, k);
mat.update(r2 + 1, c1, k);
mat.update(r2 + 1, c2 + 1, k);
} else {
if (mat.query(r1, c1) == mat.query(r2, c2))
cout << "Yes\n";
else
cout << "No\n";
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t, n, i, s, j, k, c, b, a;
cin >> t;
while (t--) {
s = 0;
cin >> n;
a = 6;
b = 10;
c = 14;
if (n < 31)
cout << "NO" << endl;
else {
s = n - 30;
if (s == 6 || s == 10 || s == 14) {
s = n - 31;
c = 15;
}
cout << "YES" << endl;
cout << a << " " << b << " " << c << " " << s << endl;
}
}
return 0;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int power(long long int x, long long int y, long long int m) {
if (y == 0) return 1;
long long int p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
long long int modInverse(long long int a, long long int m) {
return power(a, m - 2, m);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
vector<ll> A;
ll solve(vector<ll> v, int bit) {
if (bit == -1) {
return 0;
}
vector<ll> on, off;
for (ll x : v) {
if ((x & (1LL << bit)) > 0) {
on.push_back(x);
} else {
off.push_back(x);
}
}
if (on.size() == 0) {
return solve(off, bit - 1);
}
if (off.size() == 0) {
return solve(on, bit - 1);
}
return min(solve(on, bit - 1), solve(off, bit - 1)) + (1LL << bit);
}
int main() {
cin >> n;
A.resize(n);
for (int i = 0; i < n; i++) {
cin >> A[i];
}
cout << solve(A, 31) << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
int n;
long long int ra, rb, rc;
long long int d;
long long int a[1001000];
long long int cost[1001000][2];
long long int dp[1001000][2][2];
void setmin(long long int &lval, long long int rval) {
if (lval > rval) lval = rval;
}
int main() {
scanf("%d%lld%lld%lld%lld", &n, &ra, &rb, &rc, &d);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 0; i < n; i++) {
cost[i][0] = ra * a[i] + rc;
cost[i][1] = ra * (a[i] + 2);
if (cost[i][1] > rb + ra) cost[i][1] = rb + ra;
cost[i][0] += d;
cost[i][1] += 2 * d;
if (cost[i][1] > cost[i][0] + d) cost[i][1] = cost[i][0] + d;
if (cost[i][0] > cost[i][1] + d) cost[i][0] = cost[i][1] + d;
}
for (int i = 0; i <= n; i++) {
dp[i][0][0] = 1e18;
dp[i][0][1] = 1e18;
dp[i][1][0] = 1e18;
dp[i][1][1] = 1e18;
}
dp[0][0][0] = 0;
dp[0][0][1] = d;
for (int i = 0; i < n; i++) {
setmin(dp[i + 1][0][0], dp[i][0][0] + cost[i][0]);
setmin(dp[i + 1][0][0], dp[i][0][1] + cost[i][1]);
setmin(dp[i + 1][0][1], dp[i][0][0] + cost[i][1]);
setmin(dp[i + 1][0][1], dp[i + 1][0][0] + d);
setmin(dp[i + 1][0][0], dp[i + 1][0][1] + d);
setmin(dp[i + 1][1][1], dp[i + 1][0][0]);
setmin(dp[i + 1][1][1], dp[i + 1][0][1]);
setmin(dp[i + 1][1][0], dp[i + 1][0][0]);
setmin(dp[i + 1][1][0], dp[i][1][1] + cost[i][0]);
setmin(dp[i + 1][1][1], dp[i][1][1] + cost[i][1]);
setmin(dp[i + 1][1][1], dp[i + 1][1][0] + d);
setmin(dp[i + 1][1][0], dp[i + 1][1][1] + d);
}
printf("%lld", dp[n][1][0] - d);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
unsigned long long n, a[100001], x, res;
map<long long, int> used;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 31; j++) {
x = pow(2, j);
res += used[x - a[i]];
}
used[a[i]]++;
}
cout << res;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin, s);
int i = s.size();
for (i = i - 2; i >= 0; i--) {
if (s[i] == ' ')
continue;
else {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' || s[i] == 'y' || s[i] == 'A' || s[i] == 'E' ||
s[i] == 'I' || s[i] == 'O' || s[i] == 'U' || s[i] == 'Y') {
cout << "YES" << endl;
break;
} else {
cout << "NO" << endl;
break;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int n, x;
map<int, int> mp;
int main() {
cin >> n;
cin >> x;
mp[1]++;
for (int i = 2; i <= n; i++) {
cin >> x;
if (mp[x] > 0) {
mp[x]--;
mp[i]++;
} else {
mp[i]++;
}
}
int num = 0;
for (int i = 0; i <= n; i++) {
if (mp[i] > 0) {
num++;
}
}
cout << num << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, k, m;
struct tree {
int l;
int r;
long long val;
long long lazy;
} tree[4 * 400005];
void build(int root, int l, int r) {
tree[root].l = l;
tree[root].r = r;
if (l == r) {
tree[root].val = l - 400000;
tree[root].lazy = 0;
return;
}
int mid = (l + r) / 2;
build((root * 2), l, mid);
build((root * 2 + 1), mid + 1, r);
tree[root].val = max(tree[(root * 2)].val, tree[(root * 2 + 1)].val);
}
void pushdown(int root) {
tree[(root * 2)].lazy += tree[root].lazy;
tree[(root * 2 + 1)].lazy += tree[root].lazy;
tree[(root * 2)].val += tree[root].lazy;
tree[(root * 2 + 1)].val += tree[root].lazy;
tree[root].lazy = 0;
}
void update(int root, int l, int r, int val) {
if (l == tree[root].l && r == tree[root].r) {
tree[root].lazy += val;
tree[root].val += val;
return;
}
pushdown(root);
if (l <= ((tree[root].l + tree[root].r) / 2) &&
r > ((tree[root].l + tree[root].r) / 2)) {
update((root * 2), l, ((tree[root].l + tree[root].r) / 2), val);
update((root * 2 + 1), ((tree[root].l + tree[root].r) / 2) + 1, r, val);
} else if (r <= ((tree[root].l + tree[root].r) / 2))
update((root * 2), l, r, val);
else if (l > ((tree[root].l + tree[root].r) / 2))
update((root * 2 + 1), l, r, val);
tree[root].val = max(tree[(root * 2)].val, tree[(root * 2 + 1)].val);
}
long long quiry(int root, int l, int r) {
if (l == tree[root].l && r == tree[root].r) return tree[root].val;
pushdown(root);
if (l <= ((tree[root].l + tree[root].r) / 2) &&
r > ((tree[root].l + tree[root].r) / 2))
return max(
quiry((root * 2), l, ((tree[root].l + tree[root].r) / 2)),
quiry((root * 2 + 1), ((tree[root].l + tree[root].r) / 2) + 1, r));
else if (l > ((tree[root].l + tree[root].r) / 2))
return quiry((root * 2 + 1), l, r);
else if (r <= ((tree[root].l + tree[root].r) / 2))
return quiry((root * 2), l, r);
}
int cnt[400005];
unordered_map<long long, int> vis;
long long id(long long x, long long y) { return 1ll * x * n + y; }
int main() {
scanf("%d%d%d", &n, &k, &m);
build(1, 1, 400000);
priority_queue<int> q;
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
int pos = y + abs(x - k);
if (!vis.count(id(x, y))) {
vis[id(x, y)] = 1;
update(1, 1, pos, 1);
q.push(pos);
cnt[pos]++;
} else {
vis.erase(id(x, y));
update(1, 1, pos, -1);
cnt[pos]--;
while (q.size() && cnt[q.top()] == 0) q.pop();
}
if (q.empty()) {
cout << "0" << endl;
continue;
}
pos = q.top();
if (pos == 1) {
cout << max(0, cnt[pos] - n) << endl;
continue;
}
update(1, 1, pos, -cnt[pos]);
long long ans = max(0ll, quiry(1, 1, pos - 1) + (400000 - pos)) + cnt[pos] +
pos - 1 - n;
ans = max(ans, 0ll);
update(1, 1, pos, cnt[pos]);
cout << ans << endl;
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, k, a;
vector<int> arboles;
int main() {
while (cin >> n >> k) {
arboles.clear();
for (int i = 0; i < n; i++) {
cin >> a;
arboles.push_back(a);
}
int inicial = 0;
int iguales = 0;
for (int i = 1; i <= 1000; i++) {
int mintemp = 0;
for (int j = 0; j < n; j++) {
int num = i + (j * k);
if (num == arboles[j]) {
mintemp++;
}
}
if (mintemp > iguales) {
iguales = mintemp;
inicial = i;
}
}
int contador = 0;
for (int i = 0; i < n; i++) {
if (arboles[i] != inicial + (i * k)) {
contador++;
}
}
cout << contador << endl;
for (int i = 0; i < n; i++) {
int arbolactual = arboles[i];
int numeroactual = inicial + (i * k);
if (arbolactual > numeroactual) {
cout << "- " << i + 1 << " " << arbolactual - numeroactual << endl;
}
if (arbolactual < numeroactual) {
cout << "+ " << i + 1 << " " << numeroactual - arbolactual << endl;
}
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int NV = 1 << 20;
int A[NV * 2], B[NV * 2], C[NV * 2];
int N, M, Q;
multiset<int> L[202020], R[202020];
set<pair<int, int>> P;
void update(int entry) {
entry += NV;
if (L[entry - NV].empty()) {
A[entry] = 1 << 20;
} else {
A[entry] = *L[entry - NV].begin();
}
if (R[entry - NV].empty()) {
B[entry] = -1;
} else {
B[entry] = *prev(R[entry - NV].end());
}
C[entry] = A[entry] <= B[entry];
while (entry > 1) {
entry >>= 1;
C[entry] =
C[entry * 2] | C[entry * 2 + 1] | (A[entry * 2] <= B[entry * 2 + 1]);
A[entry] = min(A[entry * 2], A[entry * 2 + 1]);
B[entry] = max(B[entry * 2], B[entry * 2 + 1]);
}
}
void solve() {
int i, j, k, l, r, x, y;
string s;
cin >> N >> M >> Q;
for (i = 0; i < (2 * NV); i++) {
A[i] = 1 << 20;
B[i] = -1;
C[i] = 0;
}
int NF = 0;
while (Q--) {
cin >> y >> x;
y--;
x--;
int r = y % 2;
y /= 2;
x /= 2;
if (P.count({y, r * 1000000 + x})) {
P.erase({y, r * 1000000 + x});
if (r == 0) {
L[x].erase(L[x].find(y));
} else {
R[x].erase(R[x].find(y));
}
} else {
P.insert({y, r * 1000000 + x});
if (r == 0) {
L[x].insert(y);
} else {
R[x].insert(y);
}
}
update(x);
if (C[1]) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
}
int main(int argc, char** argv) {
string s;
int i;
if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long powmod(long long a, long long b, long long mod) {
long long sum = 1;
while (b) {
if (b & 1) {
sum = (sum * a) % mod;
b--;
}
b /= 2;
a = a * a % mod;
}
return sum;
}
const double Pi = acos(-1.0);
const double epsilon = Pi / 180.0;
const int maxn = 1e5 + 10;
int a[160000];
int main() {
int _;
scanf("%d", &_);
while (_--) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int minn = a[n];
int ans = 0;
for (int i = n - 1; i >= 1; i--) {
if (a[i] > minn)
ans++;
else
minn = a[i];
}
cout << ans << '\n';
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000, MAX_M = 1000;
int N, M;
int a[MAX_N][MAX_M], mxx[MAX_N], mxy[MAX_M], cmpx[MAX_N][MAX_M],
cmpy[MAX_N][MAX_M];
vector<int> cmp;
int main() {
scanf("%d %d", &N, &M);
for (int i = (0); i < (N); i++)
for (int j = (0); j < (M); j++) {
scanf("%d", &a[i][j]);
}
for (int i = (0); i < (N); i++) {
cmp.clear();
for (int j = (0); j < (M); j++) {
cmp.push_back(a[i][j]);
}
sort(cmp.begin(), cmp.end());
cmp.erase(unique(cmp.begin(), cmp.end()), cmp.end());
mxx[i] = (int)cmp.size();
for (int j = (0); j < (M); j++) {
cmpx[i][j] =
lower_bound(cmp.begin(), cmp.end(), a[i][j]) - cmp.begin() + 1;
}
}
for (int i = (0); i < (M); i++) {
cmp.clear();
for (int j = (0); j < (N); j++) {
cmp.push_back(a[j][i]);
}
sort(cmp.begin(), cmp.end());
cmp.erase(unique(cmp.begin(), cmp.end()), cmp.end());
mxy[i] = (int)cmp.size();
for (int j = (0); j < (N); j++) {
cmpy[j][i] =
lower_bound(cmp.begin(), cmp.end(), a[j][i]) - cmp.begin() + 1;
}
}
for (int i = (0); i < (N); i++) {
for (int j = (0); j < (M); j++) {
if (j) printf(" ");
int mx = max(cmpx[i][j], cmpy[i][j]);
printf("%d", mx + max(mxx[i] - cmpx[i][j], mxy[j] - cmpy[i][j]));
}
printf("\n");
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000009;
int N;
map<pair<int, int>, int> all;
pair<int, int> points[100001];
set<pair<int, pair<int, int>>> good;
int ans;
bool isstable(int x, int y, int bx, int by) {
if (!all.count({x, y})) return true;
return y == 0 ||
(make_pair(x - 1, y - 1) != make_pair(bx, by) &&
all.count({x - 1, y - 1})) ||
(make_pair(x, y - 1) != make_pair(bx, by) && all.count({x, y - 1})) ||
(make_pair(x + 1, y - 1) != make_pair(bx, by) &&
all.count({x + 1, y - 1}));
}
bool canremove(int x, int y) {
return all.count({x, y}) && isstable(x - 1, y + 1, x, y) &&
isstable(x, y + 1, x, y) && isstable(x + 1, y + 1, x, y);
}
void check(int x, int y) {
if (canremove(x, y)) good.insert({all[{x, y}], {x, y}});
}
void work(int x, int y) {
if (all.count({x, y}) && !canremove(x, y)) good.erase({all[{x, y}], {x, y}});
}
void recheck(int x, int y) {
if (all.count({x, y})) {
work(x - 1, y - 1);
work(x, y - 1);
work(x + 1, y - 1);
}
}
int main() {
scanf("%d", &N);
int a, b;
for (int i = 0; i < N; i++) {
scanf("%d%d", &a, &b);
points[i] = {a, b};
all[{a, b}] = i;
}
for (int i = 0; i < N; i++)
if (canremove(points[i].first, points[i].second))
good.insert({i, points[i]});
for (int i = 0; i < N; i++) {
int x, y;
if (i & 1) {
auto cur = *good.begin();
ans = (1LL * ans * N + cur.first) % MOD;
all.erase(cur.second);
x = cur.second.first;
y = cur.second.second;
good.erase(cur);
} else {
auto cur = *good.rbegin();
ans = (1LL * ans * N + cur.first) % MOD;
all.erase(cur.second);
x = cur.second.first;
y = cur.second.second;
good.erase(cur);
}
recheck(x - 1, y + 1);
recheck(x, y + 1);
recheck(x + 1, y + 1);
recheck(x - 1, y);
recheck(x, y);
recheck(x + 1, y);
check(x - 1, y - 1);
check(x, y - 1);
check(x + 1, y - 1);
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1000005];
int t[1000005];
map<int, int> last;
vector<pair<int, int> > v[1000005];
int ans[1000005];
int sz;
inline void update(int x, int y) {
for (int i = x; i <= n; i = (i | (i + 1))) t[i] ^= y;
}
inline int get(int x) {
int res = 0;
for (int i = x; i >= 0; i = (i & (i + 1)) - 1) res ^= t[i];
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
cin >> m;
for (int i = 1; i <= m; ++i) {
int x, y;
cin >> x >> y;
v[y].push_back(make_pair(x, i));
}
int x, y;
for (int i = 1; i <= n; ++i) {
x = last[a[i]];
if (x) update(x, a[i]);
last[a[i]] = i;
for (int j = 0; j < v[i].size(); ++j) {
x = v[i][j].first;
y = v[i][j].second;
ans[y] = get(i) ^ get(x - 1);
}
}
for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int modp(long long int a, long long int p, long long int n) {
long long int d[100];
long long int i, j, k, l;
if (n == 0) return 1;
for (i = 0; n >= 1; i++) {
d[i] = n % 2;
n /= 2;
}
l = i;
long long int e = 1;
if (d[0] == 1) e *= (a % p);
for (i = 1; i < l; i++) {
a *= a;
a = a % p;
if (d[i] == 1) {
e *= a;
e = e % p;
}
}
return e % p;
}
long long int modInverse(long long int n, long long int p) {
return modp(n, p, p - 2);
}
long long int p1 = 1e9 + 7, p2 = 998244353;
long long int l, r, mid, ans;
long long int n, i, j, k, g, m;
long long int x, y, n1, n2, h, z, c;
long long int aa = 1;
vector<long long int> v(1000001), parent(1000001);
void make_set(long long int x) { parent[x] = x; }
long long int find_set(long long int x) {
if (parent[x] == x) return x;
return parent[x] = find_set(parent[x]);
}
void union_set(long long int a, long long int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (b < a) swap(a, b);
parent[b] = a;
}
}
void solve() {
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> v[i];
}
for (i = 0; i < n; i++) {
make_set(i);
}
for (i = 0; i < m; i++) {
cin >> x >> y;
x--;
y--;
union_set(x, y);
}
vector<vector<long long int> > w(n), w2(n);
vector<long long int> ans(n);
for (i = 0; i < n; i++) {
parent[i] = find_set(i);
w[parent[i]].push_back(v[i]);
w2[parent[i]].push_back(i);
}
for (i = 0; i < n; i++) {
sort(w[i].begin(), w[i].end(), greater<long long int>());
l = w[i].size();
for (j = 0; j < l; j++) {
ans[w2[i][j]] = w[i][j];
}
}
for (i = 0; i < n; i++) cout << ans[i] << " ";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
long long int ans = 0;
for (int i = 0; i < t; i++) {
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
char s1[1000010], s2[1000010];
int dpa[1000010][30], dpb[1000010][30];
int gcd(int x, int y) {
if (x % y == 0) return y;
return gcd(y, x % y);
}
int main() {
int i, j, k, len1, len2;
long long d, dis;
while (scanf("%I64d %I64d", &n, &m) != EOF) {
scanf("%s %s", s1, s2);
len1 = strlen(s1);
len2 = strlen(s2);
int g = gcd(len1, len2);
memset(dpa, 0, sizeof(dpa));
memset(dpb, 0, sizeof(dpb));
for (i = 0; i < len1; i++) dpa[i % g][s1[i] - 'a']++;
for (i = 0; i < len2; i++) dpb[i % g][s2[i] - 'a']++;
d = 0;
for (i = 0; i < g; i++)
for (j = 0; j < 26; j++) {
d += (long long)dpa[i][j] * (long long)dpb[i][j];
}
dis = (long long)n * len1 - (long long)(n * g / len2 * d);
printf("%I64d\n", dis);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, k;
int a[100005], b[100005];
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n / k; ++i) scanf("%d", &a[i]);
for (int i = 0; i < n / k; ++i) scanf("%d", &b[i]);
int p = 1;
for (int i = 1; i < k; ++i) p *= 10;
long long acc = 1;
for (int i = 0; i < n / k; ++i) {
long long c;
if (b[i]) {
c = (p * 10 - 1) / a[i] + 1;
c -= ((b[i] + 1) * p - 1) / a[i] - (b[i] * p - 1) / a[i];
} else {
c = (p * 10 - 1) / a[i] - (p - 1) / a[i];
}
acc = (acc * c) % mod;
}
printf("%lld\n", acc);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int n = s.size();
int li = 0;
int ri = n - 1;
int count = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
li = i;
count += 1;
break;
}
}
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
ri = i;
count += 1;
break;
}
}
if (count == 0) {
cout << "0" << endl;
} else {
int ans = 0;
for (int i = li; i <= ri; i++) {
if (s[i] == '0') {
ans += 1;
}
}
cout << ans << endl;
}
}
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.