solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int n;
int l[10][10], h[10][10], a[10][10];
int u[110], v[110];
int ans1, ans2, m;
inline void Read() {
int x, y;
scanf("%d%d", &x, &y);
scanf("%d%d%d", &l[x][y], &h[x][y], &a[x][y]);
}
inline void Init() {
ans1 = INT_MAX;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++) {
u[++m] = i, v[m] = j;
}
}
int LOK[110], now;
int num[110];
inline void Find(int x) {
if (x > m) {
if (n > 2 && LOK[n - 1] != 0) return;
if (LOK[1] < ans1 || ans1 == LOK[1] && ans2 < now) {
ans1 = LOK[1];
ans2 = now;
}
return;
}
int U = u[x], V = v[x];
if (U > 1) {
if (LOK[U] > 0) return;
if (LOK[1] > ans1) return;
if (U > 2) {
if (LOK[U - 1] != 0) return;
}
}
for (int i = l[U][V]; i <= h[U][V]; i++) {
if (i) now += i * i + a[U][V];
LOK[U] += i;
LOK[V] -= i;
Find(x + 1);
if (i) now -= i * i + a[U][V];
LOK[U] -= i;
LOK[V] += i;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n * (n - 1) / 2; i++) Read();
Init();
Find(1);
if (ans1 > 1e6)
printf("-1 -1\n");
else
printf("%d %d\n", ans1, ans2);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 10;
const int r = 1 << 18;
int treemin[2 * r];
int tmax[2 * r];
int tadd[2 * r];
int t0[maxN];
int t[maxN];
int N, shift0;
int last_idx;
int get_min_pos(int a, int b) {
a += r;
b += r;
int res = 0;
while (a < b) {
if (a & 1) {
if (t[res] > t[treemin[a]]) res = treemin[a];
a /= 2;
a++;
} else
a /= 2;
if (b & 1)
b /= 2;
else {
if (t[res] > t[treemin[b]]) res = treemin[b];
b /= 2;
b--;
}
}
if (a == b) {
if (t[res] > t[treemin[a]]) res = treemin[a];
}
return res;
}
void add(int za, int zb, int val, int ta = 0, int tb = r - 1, int x = 1) {
if (za <= ta && tb <= zb) {
tadd[x] += val;
tmax[x] += val;
return;
}
int avg = (ta + tb) / 2;
if (za <= avg) add(za, zb, val, ta, avg, 2 * x);
if (avg < zb) add(za, zb, val, avg + 1, tb, 2 * x + 1);
tmax[x] = tadd[x] + max(tmax[2 * x], tmax[2 * x + 1]);
}
int get_max(int za, int zb, int ta = 0, int tb = r - 1, int x = 1) {
if (za <= ta && tb <= zb) return tmax[x];
int avg = (ta + tb) / 2, res = -1e9;
if (za <= avg) res = max(res, get_max(za, zb, ta, avg, 2 * x));
if (avg < zb) res = max(res, get_max(za, zb, avg + 1, tb, 2 * x + 1));
return res + tadd[x];
}
int b0[maxN];
int b1[maxN];
void create_dep(int a, int b, int dp) {
int best = get_min_pos(a, b);
b0[best] = a;
b1[best] = b;
tmax[best + r] = dp;
if (a != best) create_dep(a, best - 1, dp + 1);
if (b != best) create_dep(best + 1, b, dp + 1);
}
void init() {
last_idx = N;
for (int i = 1; i <= N; ++i) {
treemin[i + r] = i;
}
for (int i = r - 1; i > 0; --i) {
treemin[i] = treemin[2 * i + 1];
if (t[treemin[i]] > t[treemin[2 * i]]) treemin[i] = treemin[2 * i];
}
create_dep(1, N, 1);
for (int i = r - 1; i > 0; --i) {
tmax[i] = max(tmax[2 * i], tmax[2 * i + 1]);
}
}
void move1() {
int idx = last_idx--;
if (b0[idx] != idx) add(b0[idx], idx - 1, -1);
int dep0 = get_max(idx, idx);
int dep1 = 2;
if (b1[idx] != N) dep1 = get_max(b1[idx] + 1, b1[idx] + 1) + 1;
add(idx, idx, dep1 - dep0);
if (b1[idx] != idx) add(idx + 1, b1[idx], 1);
}
void print_deps() {
for (int i = 0; i <= N; ++i) {
printf("%d ", get_max(i, i));
}
puts("");
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%d", t0 + i);
if (t0[i] == 1) shift0 = N - i;
}
for (int i = 0; i < N; ++i) t[1 + (i + shift0) % N] = t0[i];
t[0] = 1e9;
init();
int bestshift = 0, bestdep = tmax[1];
for (int i = 0; i < N - 1; ++i) {
move1();
if (tmax[1] < bestdep) {
bestdep = tmax[1];
bestshift = i + 1;
}
}
printf("%d %d\n", bestdep, (N - (bestshift + shift0) % N) % N);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int cards;
cin >> cards;
int p1, p2;
cin >> p1;
vector<int> cards1;
vector<int> cards2;
int num;
for (int i = 0; i < p1; i++) {
cin >> num;
cards1.push_back(num);
}
cin >> p2;
for (int i = 0; i < p2; i++) {
cin >> num;
cards2.push_back(num);
}
int fans = 0;
while (cards1.size() > 0 && cards2.size() > 0) {
if (cards1[0] > cards2[0]) {
cards1.push_back(cards2[0]);
cards1.push_back(cards1[0]);
} else {
cards2.push_back(cards1[0]);
cards2.push_back(cards2[0]);
}
fans++;
cards1.erase(cards1.begin());
cards2.erase(cards2.begin());
if (fans == 1000) {
cout << "-1";
return 0;
}
}
if (cards1.size() > 0) {
cout << fans << " "
<< "1";
} else {
cout << fans << " "
<< "2";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char ar[200005];
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
int n, m;
scanf("%d %d %s", &n, &m, ar);
vector<int> mis(n + 2, 0), ans(26, 0);
for (int i = 0; i < m; i++) {
int a;
scanf("%d", &a);
mis[1]++;
mis[a + 1]--;
}
mis[1]++;
for (int i = 1; i <= n; i++) {
mis[i] += mis[i - 1];
ans[ar[i - 1] - 'a'] += mis[i];
}
for (int i = 0; i < 26; i++) {
printf("%d%c", ans[i], " \n"[i + 1 == 26]);
}
}
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int inf = 0x3f3f3f3f;
const long long linf = 1234567890123456789;
const long long mod = 998244353;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int poss = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < (1 << n); j++) {
if (j & (1 << i)) continue;
for (int k = 0; k < (1 << n); k++) {
if ((j & k) != k) continue;
int sum = 0;
for (int l = 0; l < n; l++) {
if (j & (1 << l)) {
if (k & (1 << l)) {
sum -= a[l];
} else {
sum += a[l];
}
}
}
if (sum == a[i]) poss = 1;
}
}
}
cout << (poss ? "YES" : "NO") << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string p;
cin >> p;
int f = 0, i;
for (i = 0; i < p.length(); i++) {
if (p[i] == 'H' || p[i] == 'Q' || p[i] == '9') {
cout << "YES" << endl;
f = 1;
break;
}
}
if (f == 0) cout << "NO" << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string solve() {
long long n, k;
cin >> n >> k;
string s;
cin >> s;
int ss = s.size();
string ans(ss, '1');
vector<int> zeros;
for (int i = 0; i < ss; i++) {
if (s[i] == '0') zeros.push_back(i);
}
for (int i = 0; i < (int)zeros.size(); i++) {
long long dist = zeros[i] - i;
long long mini = min(dist, k);
ans[zeros[i] - mini] = '0';
k -= mini;
}
return ans;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int t;
cin >> t;
while (t--) cout << solve() << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a + ((a - 1) / (b - 1));
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long v[2][2], k;
long long cnt[2000], a[15], cnt1;
long long min(long long a, long long b) { return a > b ? b : a; }
void dfs(int i, int t) {
if (i == t) {
long long sum = 0;
for (int j = 0; j < t; j++) {
sum = (sum * 10) + a[j];
}
if (sum) cnt[cnt1++] = sum;
return;
} else {
a[i] = 4;
dfs(i + 1, t);
a[i] = 7;
dfs(i + 1, t);
}
}
int find(int t, int num) {
if (v[t][1] < cnt[num]) {
return 0;
} else {
if (v[t][0] > cnt[num]) {
if (num + 1 < cnt1) {
if (cnt[num + 1] < v[t][0])
return 0;
else {
if (v[t][1] >= cnt[num + 1]) {
return cnt[num + 1] - v[t][0];
} else {
return v[t][1] + 1 - v[t][0];
}
}
} else {
return v[t][1] - v[t][0] + 1;
}
} else {
if (num + 1 < cnt1) {
if (v[t][1] >= cnt[num + 1]) {
return cnt[num + 1] - cnt[num];
} else {
return v[t][1] + 1 - cnt[num];
}
} else {
return v[t][1] - cnt[num] + 1;
}
}
}
}
double work() {
int i = 0;
while (v[0][0] > cnt[i]) {
i++;
}
double sum = 0;
long long p1 = v[0][0];
long long d = (v[1][1] - v[1][0] + 1) * (v[0][1] - v[0][0] + 1);
for (; i <= cnt1 - k && v[0][1] >= p1; i++) {
long long tmp = (min(cnt[i], v[0][1]) - p1 + 1) * find(1, i + k - 1);
sum += double(tmp) / d;
p1 = cnt[i] + 1;
}
i = 0;
while (v[1][0] > cnt[i]) {
i++;
}
p1 = v[1][0];
for (; i <= cnt1 - k && v[1][1] >= p1; i++) {
long long tmp = (min(cnt[i], v[1][1]) - p1 + 1) * find(0, i + k - 1);
sum += double(tmp) / d;
p1 = cnt[i] + 1;
}
if (k == 1) {
for (int i = 0; i < cnt1; i++) {
if (cnt[i] >= v[0][0] && cnt[i] <= v[0][1] && cnt[i] >= v[1][0] &&
cnt[i] <= v[1][1]) {
sum -= 1.0 / d;
}
}
}
return sum;
}
int main() {
cnt1 = 0;
while (scanf("%lld%lld%lld%lld%lld", &v[0][0], &v[0][1], &v[1][0], &v[1][1],
&k) != EOF) {
for (int i = 1; i < 10; i++) {
dfs(0, i);
}
sort(cnt, cnt + cnt1);
printf("%.9lf\n", work());
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long c[500010], S[500010];
int main() {
int N, K, fir, se, st, en, x;
long long val;
scanf("%d%d", &N, &K);
for (int i = (1); i < (N + 1); i++) scanf("%I64d", c + i);
c[0] = 0;
sort(c, c + N + 1);
S[0] = 0;
for (int i = (1); i < (N + 1); i++) S[i] = S[i - 1] + c[i];
fir = N + 1;
se = -1;
for (int i = (1); i < (N + 1); i++) {
val = c[i] * i - S[i];
if (val > K) break;
x = c[i] + (K - val + i - 1) / i;
fir = i;
if ((K - val) % i == 0)
st = x;
else
st = x - 1;
}
for (int i = N; i > 0; i--) {
val = S[N] - S[i - 1] - c[i] * (N - i + 1);
if (val > K) break;
x = c[i] - (K - val + N - i) / (N - i + 1);
se = i;
if ((K - val) % (N - i + 1) == 0)
en = x;
else
en = x + 1;
}
if (fir >= se || (fir == se - 1 && en <= st)) {
if (S[N] % N == 0)
puts("0");
else
puts("1");
} else
printf("%d\n", en - st);
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
int n, m, i;
scanf("%d%d", &n, &m);
if (n < m)
printf("-1\n");
else if (m == 1) {
if (n == 1)
printf("a");
else
printf("-1");
printf("\n");
} else {
for (i = 1; i < n + 3 - m; i++) {
if (i % 2 == 1)
printf("a");
else
printf("b");
}
for (i = 2; i < m; i++) printf("%c", 'a' + i);
printf("\n");
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
int n;
struct node {
long long int sum, lazy;
node() {
sum = 0;
lazy = 0;
}
node(long long int val) {
sum = val;
lazy = 0;
}
};
node temp, broken;
node merge(node l, node r) {
temp.sum = l.sum + r.sum;
temp.lazy = 0;
return temp;
}
struct segment_tree {
node tr[4 * 400005];
void push(int l, int r, int idx) {
if (tr[idx].lazy) {
tr[idx].sum += (r - l + 1) * tr[idx].lazy;
if (l != r) {
tr[2 * idx + 1].lazy += tr[idx].lazy;
tr[2 * idx + 2].lazy += tr[idx].lazy;
}
tr[idx].lazy = 0;
}
}
void init(int l = 1, int r = n, int idx = 1) {
if (l == r) {
tr[idx] = node(0);
return;
}
int mid = (l + r) >> 1;
init(l, mid, 2 * idx + 1);
init(mid + 1, r, 2 * idx + 2);
tr[idx] = merge(tr[2 * idx + 1], tr[2 * idx + 2]);
}
void update(int qL, int qR, int val, int l = 1, int r = n, int idx = 1) {
push(l, r, idx);
if (qL > r || l > qR) return;
if (qL <= l && r <= qR) {
tr[idx].lazy += val;
push(l, r, idx);
return;
}
int mid = (l + r) >> 1;
update(qL, qR, val, l, mid, 2 * idx + 1);
update(qL, qR, val, mid + 1, r, 2 * idx + 2);
tr[idx] = merge(tr[2 * idx + 1], tr[2 * idx + 2]);
}
node query(int qL, int qR, int l = 1, int r = n, int idx = 1) {
push(l, r, idx);
if (l > qR || r < qL) return broken;
if (qL <= l && r <= qR) return tr[idx];
int mid = (l + r) >> 1;
return merge(query(qL, qR, l, mid, 2 * idx + 1),
query(qL, qR, mid + 1, r, 2 * idx + 2));
}
};
segment_tree st;
long long int a[400005];
int ht[400005];
vector<int> vt[400005];
vector<pair<int, int> > qt[400005];
void dfs(int v, int p) {
for (auto q : qt[v]) {
st.update(ht[v], min(n, ht[v] + q.first), q.second);
}
a[v] = st.query(ht[v], ht[v]).sum;
for (auto g : vt[v]) {
if (g == p) continue;
ht[g] = ht[v] + 1;
dfs(g, v);
}
for (auto q : qt[v]) {
st.update(ht[v], min(n, ht[v] + q.first), -q.second);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int m, u, v, i, d, x;
cin >> n;
for (i = 1; i < n; ++i) {
cin >> u >> v;
vt[u].push_back(v);
vt[v].push_back(u);
}
cin >> m;
while (m--) {
cin >> v >> d >> x;
qt[v].push_back({d, x});
}
st.init();
ht[1] = 1;
dfs(1, 0);
for (i = 1; i <= n; ++i) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 105, M = 10005, mod = 998244353;
int f[M][N], n, a[N], tot, ans, C[N][N], s;
bool in[N];
int main() {
scanf("%d", &n);
f[0][0] = 1;
for (register int i = 1; i <= n; i++) {
scanf("%d", &a[i]), tot += a[i];
if (!in[a[i]]) s++, in[a[i]] = 1;
}
for (register int i = 1; i <= n; i++)
for (register int j = tot; j >= a[i]; j--)
for (register int k = 1; k <= i; k++)
(f[j][k] += f[j - a[i]][k - 1]) %= mod;
sort(a + 1, a + n + 1);
for (register int i = 0; i <= n; i++)
for (register int j = 0; j <= i; j++)
if (!j)
C[i][j] = 1;
else
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
for (register int i = 1, j; i <= n; i = j + 1) {
j = i;
while (j < n && a[j + 1] == a[i]) j++;
for (register int k = 1; k <= j - i + 1; k++)
if (f[k * a[i]][k] == C[j - i + 1][k]) ans = max(ans, k);
if (s == 2 && f[(j - i + 1) * a[i]][j - i + 1] == 1) ans = n;
}
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long int ll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
int n=a+b;
string s;
cin >> s;
bool k=false;
for (int i=0; i<(n + 1)/2; i++) {
if (s[i]!='?' && s[n-i-1]!='?') { //1
if (s[i]!=s[n-i-1])
k=true;
} else if (s[i]!='?' && s[n-i-1]=='?') { //2
s[n-i-1]=s[i];
} else if (s[i]=='?' && s[n-i-1]!='?') { //3
s[i]=s[n-i-1];
}
}
for (int i = 0; i < n; i++)
if (s[i] == '1')
b--;
else if (s[i] == '0')
a--;
for (int i=0; i<(n + 1)/2; i++) { //4
if (s[i]=='?') {
if (i==n/2 && n%2) {
if (a) {
s[i]='0';
a--;
} else if (b) {
s[i]='1';
b--;
}
} else {
if (a>=2) {
s[i]='0';
s[n-i-1]='0';
a-=2;
} else if (b>=2) {
s[i]='1';
s[n-i-1]='1';
b-=2;
}
}
}
}
if (a==0 && b==0 && !k)
cout << s;
else
cout << -1;
cout << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
int a[N], b[N];
int tree1[4 * N], tree2[4 * N];
int fa[N], fb[N], fsum[N];
int n;
int resPerm[N];
vector<int> initialPermutation;
void update(int tree[], int node, int l, int r, int ind, int val) {
if (l == r) {
tree[node] += val;
} else {
int lnode = node << 1;
int rnode = lnode + 1;
int mid = (l + r) >> 1;
if (ind <= mid)
update(tree, lnode, l, mid, ind, val);
else
update(tree, rnode, mid + 1, r, ind, val);
tree[node] = tree[lnode] + tree[rnode];
}
}
int query(int tree[], int node, int l, int r, int l2, int r2) {
if (l2 > r2) return 0;
if (r < l2 || l > r2) return 0;
if (l >= l2 && r <= r2) return tree[node];
int lnode = node << 1;
int rnode = lnode + 1;
int mid = (l + r) >> 1;
int s1 = query(tree, lnode, l, mid, l2, r2);
int s2 = query(tree, rnode, mid + 1, r, l2, r2);
return s1 + s2;
}
void getFactoradicNumber(int permutation[], int res[], int tree[], int len) {
for (int i = 0; i < len; i++) {
int x = permutation[i];
update(tree, 1, 1, n, x, -1);
res[i] = query(tree, 1, 1, n, 1, x - 1);
}
}
void addTwoFactoradicNumbers(int x[], int y[], int res[], int len) {
int carry = 0;
int base = 1;
for (int i = n - 1; i >= 0; i--) {
res[i] = x[i] + y[i] + carry;
carry = res[i] / base;
res[i] %= base;
base++;
}
}
void convertFactoradicNumberToPermutation(int factoradic[], int res[]) {
initialPermutation.clear();
for (int i = 1; i <= n; i++) initialPermutation.push_back(i);
for (int i = 0; i < n; i++) {
res[i] = initialPermutation[factoradic[i]];
initialPermutation.erase(initialPermutation.begin() + factoradic[i]);
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i]++;
update(tree1, 1, 1, n, a[i], 1);
}
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
b[i]++;
update(tree2, 1, 1, n, b[i], 1);
}
getFactoradicNumber(a, fa, tree1, n);
getFactoradicNumber(b, fb, tree2, n);
addTwoFactoradicNumbers(fa, fb, fsum, n);
convertFactoradicNumberToPermutation(fsum, resPerm);
for (int i = 0; i < n; i++) printf("%d ", resPerm[i] - 1);
printf("\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const int MAXN = 100005;
struct node {
long long sum;
long long lazy;
};
node seg_tree[650005];
void build_tree(int l, int r, int i) {
if (l == r) {
seg_tree[i].sum = 0;
seg_tree[i].lazy = 0;
return;
}
int mid = (l + r) / 2;
build_tree(l, mid, 2 * i);
build_tree(mid + 1, r, 2 * i + 1);
}
void update(int l, int r, int x, int y, int val, int i) {
if (seg_tree[i].lazy) {
seg_tree[i].sum += seg_tree[i].lazy * (r - l + 1);
if (l != r) {
seg_tree[2 * i].lazy += seg_tree[i].lazy;
seg_tree[2 * i + 1].lazy += seg_tree[i].lazy;
}
seg_tree[i].lazy = 0;
}
if (l > y || r < x) return;
if (x <= l && r <= y) {
seg_tree[i].sum += val * (r - l + 1);
if (l != r) {
seg_tree[2 * i].lazy += val;
seg_tree[2 * i + 1].lazy += val;
}
return;
}
int mid = (l + r) / 2;
update(l, mid, x, y, val, 2 * i);
update(mid + 1, r, x, y, val, 2 * i + 1);
seg_tree[i].sum = seg_tree[2 * i].sum + seg_tree[2 * i + 1].sum;
}
long long query(int l, int r, int x, int y, int i) {
if (seg_tree[i].lazy) {
seg_tree[i].sum += seg_tree[i].lazy * (r - l + 1);
if (l != r) {
seg_tree[2 * i].lazy += seg_tree[i].lazy;
seg_tree[2 * i + 1].lazy += seg_tree[i].lazy;
}
seg_tree[i].lazy = 0;
}
if (l > x || r < x) return 0;
if (x <= l && r <= y) {
return seg_tree[i].sum;
}
int mid = (l + r) / 2;
long long p = query(l, mid, x, y, 2 * i);
long long q = query(mid + 1, r, x, y, 2 * i + 1);
return p + q;
}
int main() {
int t, n, i, j, k, N, type, a, x, m;
long long sum;
scanf("%d", &N);
n = 1;
m = N + 5;
build_tree(1, m, 1);
while (N--) {
scanf("%d", &type);
if (type == 1) {
scanf("%d %d", &a, &x);
update(1, m, 1, a, x, 1);
} else if (type == 2) {
n++;
scanf("%d", &k);
update(1, m, n, n, k, 1);
} else if (n >= 2) {
k = query(1, m, n, n, 1);
update(1, m, n, n, -k, 1);
n--;
}
sum = query(1, m, 1, m, 1);
printf("%lf\n", (double)sum * 1.0 / n);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, grundy[100005], aux, j;
long long prev = 1;
for (long long i = 1; i <= 60; i++) {
for (j = prev; j <= prev + i; j++) {
grundy[j] = i;
}
prev = j;
}
cin >> n;
long long w = 0;
for (long long i = 0; i < n; i++) {
cin >> aux;
w ^= grundy[aux];
}
if (w)
cout << "NO" << '\n';
else
cout << "YES" << '\n';
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll,ll> pll;
typedef pair<int, int> pii;
#define N 100010
#define M 200010
#define endl "\n"
#define mod 1000000007
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(NULL);
int t;
cin >> t;
while(t--){
int n, k;
string a, b;
cin >> n >> k;
cin >> a >> b;
int cnt1[26], cnt2[26];
memset(cnt1, 0, sizeof(cnt1));
memset(cnt2, 0, sizeof(cnt2));
for(char c : a){
cnt1[c - 'a']++;
}
for(char c : b){
cnt2[c - 'a']++;
}
bool flag = true;
for(int i = 0; i < 26; i++){
if(cnt1[i] < cnt2[i]){
flag = false;
}
cnt1[i] -= cnt2[i];
if(cnt1[i] % k || (i == 25 && cnt1[25])){
flag = false;
}
if(i < 25){
cnt1[i + 1] += cnt1[i];
}
}
if(flag){
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > art_graph_adj[1000000 + 9];
long long color[1000000 + 9];
long long art_ar[1000000 + 9];
long long art_low[1000000 + 9];
long long art_app[1000000 + 9];
long long art_parent[1000000 + 9];
long long articulation_edge_list[1000000 + 9];
long long art_counter;
long long art_root_degree;
long long art_source;
long long NODE_VAL[1000000 + 9], LEFT_NODE[1000000 + 9],
RIGHT_NODE[1000000 + 9], new_color[1000000 + 9], TTTT[1000000 + 9],
EEEE[1000000 + 9];
long long art_color[1000000 + 9];
vector<long long> new_adj_list[1000000 + 9];
long long YY, MAIN_ANS_ANS, CURR_POS;
void init_articulation(int n, int m) {
int i;
for (i = 0; i <= n + 5; i++) {
art_graph_adj[i].clear();
art_color[i] = 0;
art_parent[i] = 0;
}
}
void find_articulation(int v) {
int i, w;
art_color[v] = 1;
art_counter++;
art_ar[v] = art_low[v] = art_counter;
for (i = 0; i < art_graph_adj[v].size(); i++) {
w = art_graph_adj[v][i].first;
if (!art_color[w]) {
art_parent[w] = v;
find_articulation(w);
art_low[v] = min(art_low[v], art_low[w]);
if (art_low[w] > art_ar[v]) {
art_app[v]++;
articulation_edge_list[art_graph_adj[v][i].second] = 1;
}
} else if (w != art_parent[v]) {
art_low[v] = min(art_low[v], art_ar[w]);
}
}
}
void DFS(long long curr) {
long long i, j, k, l, siz;
color[curr] = YY;
TTTT[YY] += NODE_VAL[curr];
EEEE[YY] += 1;
siz = art_graph_adj[curr].size();
for (i = 0; i < siz; i++) {
j = art_graph_adj[curr][i].first;
k = art_graph_adj[curr][i].second;
if (articulation_edge_list[k]) continue;
if (color[j] == 0) DFS(j);
}
}
pair<pair<long long, long long>, long long> ANOTHER_DFS(long long curr) {
long long i, j, k, l, siz, ans, temp;
pair<pair<long long, long long>, long long> RET_TUPLE, TEMP_TUPLE;
new_color[curr] = 1;
siz = new_adj_list[curr].size();
RET_TUPLE.second = (EEEE[curr] > 1);
RET_TUPLE.first.first = 0;
RET_TUPLE.first.second = 0;
for (i = 0; i < siz; i++) {
j = new_adj_list[curr][i];
if (new_color[j] == 0) {
TEMP_TUPLE = ANOTHER_DFS(j);
RET_TUPLE.second |= TEMP_TUPLE.second;
RET_TUPLE.first.first += TEMP_TUPLE.first.first;
RET_TUPLE.first.second =
max(RET_TUPLE.first.second, TEMP_TUPLE.first.second);
}
}
if (RET_TUPLE.second == 0)
RET_TUPLE.first.second += TTTT[curr];
else
RET_TUPLE.first.first += TTTT[curr];
return RET_TUPLE;
}
int main() {
long long i, j, k, l, n, m, curr_pos, ans;
cin >> n >> m;
init_articulation(n, m);
for (i = 1; i <= n; i++) scanf("%lld", &NODE_VAL[i]);
for (i = 1; i <= m; i++) {
scanf("%lld%lld", &j, &k);
art_graph_adj[j].push_back(make_pair(k, i));
art_graph_adj[k].push_back(make_pair(j, i));
LEFT_NODE[i] = j;
RIGHT_NODE[i] = k;
}
cin >> curr_pos;
for (i = 1; i <= n; i++) {
if (color[i] == 0) {
art_counter = 0;
art_source = i;
art_root_degree = art_graph_adj[art_source].size();
find_articulation(art_source);
if (art_root_degree > 1) {
art_app[art_source] = 1;
}
}
}
memset(color, 0, sizeof(color));
YY = 0;
for (i = 1; i <= n; i++) {
if (color[i] == 0) {
YY++;
DFS(i);
}
}
for (i = 1; i <= m; i++) {
if (color[LEFT_NODE[i]] == color[RIGHT_NODE[i]]) continue;
new_adj_list[color[LEFT_NODE[i]]].push_back(color[RIGHT_NODE[i]]);
new_adj_list[color[RIGHT_NODE[i]]].push_back(color[LEFT_NODE[i]]);
}
CURR_POS = color[curr_pos];
pair<pair<long long, long long>, long long> ANS_TUPLE = ANOTHER_DFS(CURR_POS);
cout << ANS_TUPLE.first.first + ANS_TUPLE.first.second << endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e2 + 20;
const int mod = 998244353;
map<long long, long long> mp;
long long sq2(long long a) {
long long l = 0, r = 2e9;
while (r - l > 1) {
long long m = (l + r) / 2;
if (m * m >= a)
r = m;
else
l = m;
}
return r;
}
long long sq3(long long a) {
long long l = 0, r = 2e6;
while (r - l > 1) {
long long m = (l + r) / 2;
if (m * m * m >= a)
r = m;
else
l = m;
}
return r;
}
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<long long> shits;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
long long S2 = sq2(x);
long long S3 = sq3(x);
if (S2 * S2 == x) {
if (sq2(S2) * sq2(S2) == S2)
mp[sq2(S2)] += 4;
else
mp[S2] += 2;
} else {
if (S3 * S3 * S3 == x)
mp[S3] += 3;
else
shits.push_back(x);
}
}
vector<long long> wt;
for (auto x : mp) wt.push_back(x.first);
for (int i = 0; i < (int)shits.size(); i++)
for (int j = i + 1; j < (int)shits.size(); j++) {
long long x = shits[i], y = shits[j];
if (x != y && gcd(x, y) != 1) wt.push_back(gcd(x, y));
}
vector<long long> tmp;
for (auto x : shits) {
bool f = 0;
for (auto p : wt) {
if (x % p == 0) {
mp[p]++;
mp[x / p]++;
f = 1;
break;
}
}
if (!f) tmp.push_back(x);
}
long long res = 1;
for (auto x : mp) res = res * (x.second + 1) % mod;
mp.clear();
for (auto x : tmp) mp[x]++;
for (auto x : mp) res = res * (x.second + 1) * (x.second + 1) % mod;
cout << res << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
unsigned short table[500][500];
unsigned short sol[500];
int main() {
long long t;
long long a, b;
cin >> a >> b;
set<int> ss;
for (long long i = 0; i < a; ++i) {
for (long long j = 0; j < b; ++j) {
int temp;
cin >> temp;
table[i][j] = temp;
}
}
bool found = false;
unsigned short xr_orig = 0;
unsigned short xr = 0;
for (long long i = 0; i < a; ++i) {
xr_orig = xr_orig ^ table[i][0];
sol[i] = 0;
}
xr = xr_orig;
for (long long i = 0; i < a; ++i) {
if (xr) {
found = true;
break;
}
unsigned short xr2 = xr_orig ^ table[i][0];
for (long long j = 1; j < b; ++j) {
unsigned short xr3 = xr2 ^ table[i][j];
sol[i] = j;
if (xr3) {
found = true;
break;
}
}
if (!found) {
sol[i] = 0;
} else {
break;
};
}
if (found) {
cout << "TAK" << endl;
for (long long i = 0; i < a; ++i) {
cout << sol[i] + 1 << " ";
}
} else {
cout << "NIE" << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int op[100005];
int X[100005];
int C[100005];
int chk[2][5005];
int mat[5005][5005];
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < k; i++) {
scanf("%d%d%d", &op[i], &X[i], &C[i]);
}
for (int i = k - 1; i >= 0; i--) {
if (op[i] == 1) {
if (!chk[0][X[i]]) {
chk[0][X[i]] = 1;
for (int j = 1; j <= m; j++) {
if (mat[X[i]][j]) continue;
mat[X[i]][j] = C[i];
}
}
} else {
if (!chk[1][X[i]]) {
chk[1][X[i]] = 1;
for (int j = 1; j <= n; j++) {
if (mat[j][X[i]]) continue;
mat[j][X[i]] = C[i];
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
printf("%d ", mat[i][j]);
}
puts("");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<int> g[N];
vector<int> a[N];
int dfn[N], low[N];
int tot = 0;
bool valid[N];
set<int> S;
void dfs(int u) {
tot++;
dfn[u] = tot;
low[u] = tot;
S.insert(u);
if (u != (*S.begin())) valid[u] = 0;
for (int v : g[u]) {
if (!dfn[v])
dfs(v), low[u] = min(low[u], low[v]);
else if (S.count(v))
low[u] = min(low[u], dfn[v]);
}
if (low[u] < dfn[u]) {
cout << "-1";
exit(0);
}
S.erase(u);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int x;
cin >> x;
int y;
cin >> y;
g[x].emplace_back(y);
a[y].emplace_back(x);
}
fill(valid + 1, valid + 1 + n, 1);
for (int i = 1; i <= n; ++i)
if (!dfn[i]) dfs(i);
for (int i = 1; i <= n; ++i) {
g[i].swap(a[i]);
dfn[i] = 0;
low[i] = 0;
}
for (int i = 1; i <= n; ++i)
if (!dfn[i]) dfs(i);
cout << accumulate(valid + 1, valid + 1 + n, (int)0) << "\n";
for (int i = 1; i <= n; ++i) cout << (valid[i] ? "A" : "E");
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>> adj;
int u1, u2, max_d;
vector<int> d;
void bfs(int root) {
d.assign(n, INT_MAX);
d[root] = 0;
max_d = 0;
queue<int> q;
q.push(root);
while (!q.empty()) {
int cur = q.front();
q.pop();
if (d[cur] > max_d) {
max_d = d[cur];
u2 = cur;
}
for (auto nxt : adj[cur])
if (d[nxt] == INT_MAX) {
d[nxt] = d[cur] + 1;
q.push(nxt);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
adj.resize(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u -= 1;
v -= 1;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < n; i++)
if (adj[i].size() == 1) u1 = i;
bfs(u1);
bfs(u2);
cout << max_d << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int MAXN = 500006;
const int UP = 10000007;
pair<int, int> G[UP];
int A[MAXN];
int Z[UP];
vector<int> dziel(int a) {
vector<int> E;
while (a > 1) {
int p = G[a].first;
while (a % p == 0) a /= p;
E.push_back(p);
}
return E;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < (n); ++i) scanf("%d", &A[i]);
G[1] = pair<int, int>(1, -1);
for (int i = 2; i < UP; ++i) {
int p = G[i].first;
if (p == 0) {
G[i].first = p = i;
if (1LL * p * p < UP)
for (int j = p * p; j < UP; j += i) G[j].first = i;
}
if ((i / p) % p) G[i].second = -G[i / p].second;
}
for (int i = 0; i < (n); ++i) {
vector<int> P = dziel(A[i]);
for (int ma = 0; ma < (1 << P.size()); ++ma) {
int d = 1;
for (int j = 0; j < (P.size()); ++j)
if (ma & (1 << j)) d *= P[j];
Z[d] = (Z[d] * 2 + 1) % mod;
}
}
long long all = 0;
for (int i = 0; i < (UP); ++i)
if (i > 1) all += G[i].second * Z[i] + mod;
all %= mod;
;
long long ret = 0;
for (int i = 0; i < (n); ++i) {
vector<int> P = dziel(A[i]);
long long tu = all;
for (int ma = 0; ma < (1 << P.size()); ++ma)
if (ma) {
int d = 1;
for (int j = 0; j < (P.size()); ++j)
if (ma & (1 << j)) d *= P[j];
tu += mod - Z[d] * G[d].second;
}
ret += tu;
;
}
printf("%lld\n", ret % mod);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int a[222222];
int b[222222];
int p[222222];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
--a[i];
}
for (int i = 0; i < n; ++i) {
scanf("%d", &b[i]);
--b[i];
p[b[i]] = i;
}
for (int i = 0; i < n; i++) {
b[i] = p[a[i]];
}
for (int i = 0; i + 1 < n; i++) {
if (b[i + 1] < b[i]) {
cout << n - 1 - i;
return 0;
}
}
cout << 0;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
template <typename first, typename second>
ostream& operator<<(ostream& os, const pair<first, second>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os << "{";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ",";
os << *it;
}
return os << "]";
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << *it;
}
return os << "]";
}
template <typename first, typename second>
ostream& operator<<(ostream& os, const map<first, second>& v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin()) os << ", ";
os << it->first << " = " << it->second;
}
return os << "]";
}
void faltu() { cerr << '\n'; }
template <typename T>
void faltu(T a[], int n) {
for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
cerr << '\n';
}
template <typename T, typename... hello>
void faltu(T arg, const hello&... rest) {
cerr << arg << ' ';
faltu(rest...);
}
const int mx = 1e5 + 5;
int n;
int q[mx];
set<int> st;
vector<int> store;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
store.clear();
st.clear();
cin >> n;
for (int i = 1; i <= n; ++i) st.insert(i);
for (int i = 1; i <= n; ++i) cin >> q[i];
int cur = 0;
for (int i = 1; i <= n; ++i) {
if (q[i] > cur) {
store.push_back(q[i]);
cur = q[i];
st.erase(q[i]);
} else {
if (*st.begin() < cur) {
store.push_back(*st.begin());
st.erase(*st.begin());
} else {
break;
}
}
}
if ((int)store.size() != n)
cout << -1 << '\n';
else {
for (auto x : store) cout << x << " ";
cout << '\n';
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (a < b) swap(a, b);
while (b) {
int t = a % b;
a = b;
b = t;
}
return a;
}
int main() {
int n, m;
while (cin >> n >> m) {
int A[n + 1];
int B[m + 1];
for (int i = 0; i <= n; i++) cin >> A[i];
for (int i = 0; i <= m; i++) cin >> B[i];
if (n > m) {
bool minus = (A[0] < 0) ^ (B[0] < 0);
if (minus) cout << "-";
cout << "Infinity" << endl;
} else if (n < m) {
cout << 0 << "/" << 1 << endl;
} else {
if (B[0] == 0) {
if (A[0] < 0) cout << "-";
cout << "Infinity" << endl;
} else {
bool minus = (A[0] < 0) ^ (B[0] < 0);
if (A[0] < 0) A[0] = -A[0];
if (B[0] < 0) B[0] = -B[0];
int r = gcd(A[0], B[0]);
A[0] /= r;
B[0] /= r;
if (minus) cout << "-";
cout << A[0] << '/' << B[0] << endl;
}
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
int n;
int G[MAXN][MAXN];
char s[MAXN][MAXN];
int ans[MAXN][2], cnt;
void solve() {
cnt = 0;
for (int i = 1; i <= n; ++i) {
scanf("%s", s[i] + 1);
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (s[i][j] == '.') {
ans[cnt][0] = i;
ans[cnt][1] = j;
++cnt;
break;
}
}
}
if (cnt < n) {
cnt = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (s[j][i] == '.') {
ans[cnt][0] = j;
ans[cnt][1] = i;
++cnt;
break;
}
}
}
}
if (cnt < n)
printf("-1\n");
else {
for (int i = 0; i < n; ++i) {
printf("%d %d\n", ans[i][0], ans[i][1]);
}
}
}
int main() {
while (~scanf("%d", &n)) solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long mod = 998244353;
const long long modu = 1000000007;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long n, m;
cin >> n >> m;
vector<long long> w(n);
for (long long i = 0; i < n; i++) cin >> w[i];
vector<map<long long, long long>> con(n);
vector<long long> b(n, 0LL);
vector<vector<long long>> bt(n, vector<long long>());
long long x, y;
for (long long i = 0; i < m; i++) {
cin >> x >> y;
--x, --y;
con[x][y]++, con[y][x]++;
bt[x].push_back(i), bt[y].push_back(i);
w[x]--;
w[y]--;
}
priority_queue<tuple<long long, long long>> pq;
for (long long i = 0; i < n; i++) pq.push(make_tuple(w[i], i));
vector<long long> sol;
while (!pq.empty()) {
long long d, u;
tie(d, u) = pq.top();
pq.pop();
if (b[u]) continue;
for (auto val : bt[u]) sol.push_back(val);
b[u] = 1;
if (w[u] < 0) {
cout << "DEAD" << '\n';
return 0;
}
for (auto val : con[u]) {
if (b[val.first]) continue;
w[val.first] += val.second;
pq.push(make_tuple(w[val.first], val.first));
}
}
cout << "ALIVE" << '\n';
vector<long long> seen(m, 0LL), ans;
for (auto val : sol) {
if (!seen[val]) {
seen[val] = 1LL;
ans.push_back(val + 1LL);
}
}
reverse(ans.begin(), ans.end());
for (auto v : ans) cout << v << " ";
cout << '\n';
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll MOD = 998244353;
const int INF = (ll)1e9 + 7;
const ll INFLL = (ll)1e18;
template <class t>
using vvector = vector<vector<t>>;
template <class t>
using vvvector = vector<vector<vector<t>>>;
template <class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template <class t, class u>
bool chmax(t &a, u b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class t, class u>
bool chmin(t &a, u b) {
if (a > b) {
a = b;
return true;
}
return false;
}
namespace templates {
ll modpow(ll x, ll b) {
ll res = 1;
while (b) {
if (b & 1) res = res * x % MOD;
x = x * x % MOD;
b >>= 1;
}
return res;
}
ll modinv(ll x) { return modpow(x, MOD - 2); }
bool was_output = false;
template <class t>
void output(t a) {
if (was_output) cout << " ";
cout << a;
was_output = true;
}
void outendl() {
was_output = false;
cout << endl;
}
ll in() {
ll res;
scanf("%lld", &res);
return res;
}
template <class t>
istream &operator>>(istream &is, vector<t> &x) {
for (auto &i : x) is >> i;
return is;
}
template <class t, class u>
istream &operator>>(istream &is, pair<t, u> &x) {
is >> x.first >> x.second;
return is;
}
template <class t>
t in() {
t res;
cin >> res;
return res;
}
template <class t>
void out(t x) {
cout << x;
}
template <class t>
vector<t> sorted(
vector<t> line,
function<bool(t, t)> comp = [](t a, t b) { return a < b; }) {
sort(line.begin(), line.end(), comp);
return line;
}
template <class t>
vector<t> reversed(vector<t> line) {
reverse(line.begin(), line.end());
return line;
}
} // namespace templates
using namespace templates;
ll func() {
ll n = in();
ll m = in();
ll sx = in();
ll sy = in();
ll fx = in();
ll fy = in();
vector<pll> teleport(m);
for (auto &i : (teleport)) {
i = in<pii>();
}
vvector<pll> edges(m + 2);
function<void(function<int(pll x)> f)> connect =
(function<void(function<int(pll x)> f)>)[&](function<int(pll x)> f)
->void {
vector<pll> line;
for (int i = (m) * (strlen("") != 0); i < (int)(strlen("") ?: (m)); ++i) {
line.emplace_back(f(teleport[i]), i);
}
sort((line).begin(), (line).end());
for (int i = (m - 1) * (strlen("") != 0); i < (int)(strlen("") ?: (m - 1));
++i) {
int a = line[i].second;
int b = line[i + 1].second;
ll dis = min(abs(teleport[a].first - teleport[b].first),
abs(teleport[a].second - teleport[b].second));
edges[a].emplace_back(b, dis);
edges[b].emplace_back(a, dis);
}
};
connect((function<int(pll x)>)[&](pll x)->int { return x.first; });
connect((function<int(pll x)>)[&](pll x)->int { return x.second; });
for (int i = (m) * (strlen("") != 0); i < (int)(strlen("") ?: (m)); ++i) {
ll dis = min(abs(teleport[i].first - sx), abs(teleport[i].second - sy));
edges[m].emplace_back(i, dis);
edges[i].emplace_back(m, dis);
}
for (int i = (m) * (strlen("") != 0); i < (int)(strlen("") ?: (m)); ++i) {
ll dis = abs(teleport[i].first - fx) + abs(teleport[i].second - fy);
edges[i].emplace_back(m + 1, dis);
}
edges[m].emplace_back(m + 1, abs(sx - fx) + abs(sy - fy));
vector<ll> fast(m + 2, INFLL);
fast[m] = 0;
priority_queuer<pll> pq;
pq.emplace(0, m);
while (pq.size()) {
pll d = pq.top();
pq.pop();
if (fast[d.second] != d.first) continue;
for (auto &edge : (edges[d.second])) {
ll to = d.first + edge.second;
if (chmin(fast[edge.first], to)) {
pq.emplace(to, edge.first);
}
}
}
return fast[m + 1];
}
int main() {
cout << func() << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
long long mod_exp(long long x, long long y, long long mm) {
if (y == 0)
return (1);
else if (y % 2 == 0)
return (mod_exp((x * x) % mm, y / 2, mm));
else
return ((x * mod_exp((x * x) % mm, (y - 1) / 2, mm)) % mm);
}
bool isPowerOfTwo(long long n) {
if (n == 0) return false;
return (ceil(log2(n)) == floor(log2(n)));
}
void solve() {
long long a, b;
cin >> a >> b;
cout << min((a ^ b), a + b) << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
int n, t, f[21][4][21], ans;
int main() {
scanf("%d %d", &n, &t);
f[0][0][0] = 1;
f[0][1][0] = 1;
f[0][2][0] = 1;
for (int i = 0; i <= n; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 2 * t; k++) {
for (int l = 0; l < 4; l++) {
if (i == 1 && j > l || i > 1 && j != l) {
f[i][j][k + (j < l != k % 2)] += f[i - 1][l][k];
}
}
}
}
}
ans = 0;
for (int i = 0; i < 4; i++) {
ans += f[n - 1][i][t * 2 - 1];
}
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n + 1);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (n <= 6 || m == 0) {
cout << m << endl;
return 0;
}
int sum = 0;
for (int i = 1; i < 7; i++) {
for (int j = i + 1; j <= 7; j++) {
vector<bool> vis(8, false);
int cnt = 0, u = 0, v = 0;
for (; u < adj[i].size(); u++) vis[adj[i][u]] = true;
for (; v < adj[j].size(); v++)
if (!vis[adj[j][v]]) vis[adj[j][v]] = true;
for (int k = 1; k <= 7; k++)
if (vis[k]) cnt++;
sum = max(sum, m - u - v + cnt);
}
}
cout << sum << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int val;
node *l, *r;
node() {
val = 0;
l = NULL;
r = NULL;
}
};
node *segr, *segc;
void update(node *cur, int l, int r, int x, int y, int val) {
if (l > r || l > y || r < x) return;
if (x <= l && r <= y) {
cur->val = max(cur->val, val);
return;
}
int mid = (l + r) >> 1;
if (cur->l == NULL) cur->l = new node();
if (cur->r == NULL) cur->r = new node();
update(cur->l, l, mid, x, y, val);
update(cur->r, mid + 1, r, x, y, val);
}
int query(node *cur, int l, int r, int idx) {
if (cur == NULL || l > idx || l > r || r < idx) return 0;
if (l == r && l == idx) {
return cur->val;
}
int mid = (l + r) >> 1;
return max(
{cur->val, query(cur->l, l, mid, idx), query(cur->r, mid + 1, r, idx)});
}
const int maxn = (int)(1e9);
int main() {
segr = new node();
segc = new node();
int n, q;
scanf("%d %d", &n, &q);
for (int i = 0; i < q; i++) {
int u, v;
char c;
scanf("%d %d %c", &u, &v, &c);
if (c == 'U') {
int ma = query(segc, 1, maxn, u);
printf("%d\n", v - ma);
update(segr, 1, maxn, ma, v, u);
update(segc, 1, maxn, u, u, v);
} else {
int ma = query(segr, 1, maxn, v);
printf("%d\n", u - ma);
update(segc, 1, maxn, ma, u, v);
update(segr, 1, maxn, v, v, u);
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int mx, ans;
long long res = 1LL;
const long long md = 1e9 + 7;
int f[256];
int main() {
scanf("%d", &n);
cin >> s;
for (int i = 0; i < n; i++) {
f[s[i]]++;
if (f[s[i]] > mx) {
mx = f[s[i]];
ans = 1;
} else if (f[s[i]] == mx)
ans++;
}
for (int i = 0; i < n; i++) res = res * (long long)ans % md;
printf("%lld", res);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ULL = unsigned long long;
using UL = unsigned;
using LL = long long;
struct RAQ {
vector<UL> node;
UL N;
UL n;
void init(UL n) {
N = 1;
while (N < n) N <<= 1;
this->n = n;
node.assign(N * 2 - 1, 0);
}
UL query(UL a, UL b, UL l = 0, UL r = -1, UL i = 0) {
if (r == -1) r = N;
if (b <= l || r <= a) return -1;
if (a <= l && r <= b) return node[i];
return (query(a, b, l, (l + r) / 2, i * 2 + 1) &
query(a, b, (l + r) / 2, r, i * 2 + 2)) |
node[i];
}
void update(UL a, UL b, UL v, UL l = 0, UL r = -1, UL i = 0) {
if (r == -1) r = N;
if (b <= l || r <= a) return;
if (a <= l && r <= b) {
node[i] |= v;
return;
}
update(a, b, v | node[i], l, (l + r) / 2, i * 2 + 1);
update(a, b, v | node[i], (l + r) / 2, r, i * 2 + 2);
node[i] |= node[i * 2 + 1] & node[i * 2 + 2];
}
};
struct Problem {
void Solve() {
UL N, M;
cin >> N >> M;
RAQ Q;
Q.init(N);
vector<pair<pair<UL, UL>, UL>> C(M);
for (UL i = 0; i < (M); i++) {
UL l, r, a;
cin >> l >> r >> a;
l--;
C[i] = {{l, r}, a};
Q.update(l, r, a);
}
bool ok = true;
for (UL i = 0; i < (M); i++) {
UL buf = Q.query(C[i].first.first, C[i].first.second);
if (buf != C[i].second) ok = false;
}
if (!ok) {
cout << "NO" << endl;
return;
}
cout << "YES" << endl;
for (UL i = 0; i < (N); i++) {
if (i) cout << " ";
cout << Q.query(i, i + 1);
}
cout << endl;
}
Problem();
};
int main() {
unique_ptr<Problem> p(new Problem());
p->Solve();
return 0;
}
Problem::Problem() { cout << fixed << setprecision(10); }
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename C>
void MA(C& a, C b) {
if (a < b) a = b;
}
template <typename C>
void MI(C& a, C b) {
if (a > b) a = b;
}
int n, r, a[107];
int t, ans;
int main() {
scanf("%d%d", &n, &r);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int l = 1; l < 107; l++) {
if (l < r) continue;
t = 0;
for (int i = 0; i < n; i++) t += a[i] / l;
MA(ans, t * l);
}
printf("%d\n", ans);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct T {
int x;
int y;
};
long long fib[10009], k;
int main() {
int n;
scanf("%d%I64d", &n, &k);
fib[1] = 1;
for (int i = 2; i <= n; i++) fib[i] = fib[i - 1] + fib[i - 2];
for (int i = 1; i <= n; i++) {
if (fib[n - i + 1] < k) {
printf("%d %d ", i + 1, i);
k -= fib[n - i + 1];
i++;
} else
printf("%d ", i);
}
puts("");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct query {
int l, r, id;
query() : l(0), r(0), id(0){};
query(int l, int r, int id) : l(l), r(r), id(id){};
};
int n, t, a[202020], num[1000001], bb[202020], ll, rr;
vector<query> qq[400];
long long ans[202020], cur = 0;
bool cmp(query q1, query q2) {
if (q1.r < q2.r) return true;
return false;
}
void inc_r() {
++rr;
cur += (long long)a[rr] * (2 * num[a[rr]] + 1);
num[a[rr]]++;
}
void dec_l() {
--ll;
cur += (long long)a[ll] * (2 * num[a[ll]] + 1);
num[a[ll]]++;
}
void inc_l() {
cur += (long long)a[ll] * (-2 * num[a[ll]] + 1);
num[a[ll]]--;
++ll;
}
int main() {
for (int it = 0; it < 400; it++) {
int ll = it * 500 + 1;
int rr = (it + 1) * 500;
for (int i = ll; i <= rr; i++) bb[i] = it;
}
scanf("%d%d", &n, &t);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= t; i++) {
int l, r;
scanf("%d%d", &l, &r);
qq[bb[l]].push_back(query(l, r, i));
}
for (int it = 0; it < 400; it++) {
if (qq[it].size() == 0) continue;
sort(qq[it].begin(), qq[it].end(), cmp);
ll = it * 500 + 1;
rr = it * 500;
inc_r();
for (int i = 0; i < qq[it].size(); i++) {
while (rr < qq[it][i].r) inc_r();
while (ll < qq[it][i].l) inc_l();
while (ll > qq[it][i].l) dec_l();
ans[qq[it][i].id] = cur;
}
cur = 0;
for (int i = ll; i <= rr; i++) num[a[i]] = 0;
}
for (int i = 1; i <= t; i++) printf("%I64d\n", ans[i]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, tot, dep, now;
char s[11];
int head[510000], nex[510000], to[510000];
int val[510000], v[(1 << 23) + 10];
int size[510000], son[510000], deep[510000], ans[510000];
void add(int x, int y) {
tot++;
nex[tot] = head[x];
head[x] = tot;
to[tot] = y;
}
void dfs(int x, int y) {
size[x] = 1;
deep[x] = deep[y] + 1;
val[x] ^= val[y];
for (int i = head[x]; i; i = nex[i]) {
dfs(to[i], x);
size[x] += size[to[i]];
son[x] = size[to[i]] > size[son[x]] ? to[i] : son[x];
}
}
void opt1(int x) {
if (v[val[x]]) now = max(now, v[val[x]] + deep[x] - dep * 2);
for (int i = 'a', t; i <= 'v'; i++)
if (t = v[val[x] ^ (1 << i - 'a')]) now = max(now, t + deep[x] - dep * 2);
}
void opt2(int x) { v[val[x]] = max(v[val[x]], deep[x]); }
void upd(int x) {
opt1(x);
for (int i = head[x]; i; i = nex[i]) upd(to[i]);
}
void ins(int x) {
opt2(x);
for (int i = head[x]; i; i = nex[i]) ins(to[i]);
}
void del(int x) {
v[val[x]] = 0;
for (int i = head[x]; i; i = nex[i]) del(to[i]);
}
void dfs1(int x, int tp) {
for (int i = head[x]; i; i = nex[i])
if (to[i] != son[x]) dfs1(to[i], 0);
if (son[x]) dfs1(son[x], 1);
dep = deep[x];
opt1(x);
opt2(x);
for (int i = head[x]; i; i = nex[i])
if (to[i] != son[x]) upd(to[i]), ins(to[i]);
ans[x] = now;
if (!tp) {
now = 0;
del(x);
}
}
void dfs2(int x) {
for (int i = head[x]; i; i = nex[i]) {
dfs2(to[i]);
ans[x] = max(ans[x], ans[to[i]]);
}
}
int main() {
scanf("%d", &n);
for (int i = 2, x; i <= n; i++) {
scanf("%d%s", &x, s + 1);
val[i] = 1 << (s[1] - 'a');
add(x, i);
}
dfs(1, 0);
dfs1(1, 0);
dfs2(1);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-9;
int t;
bool arr[61][61];
int n, m;
bool nosoln() {
for (int i = 0; i < (int)(n); ++i)
for (int j = 0; j < (int)(m); ++j)
if (arr[i][j]) return false;
return true;
}
bool soln0() {
for (int i = 0; i < (int)(n); ++i)
for (int j = 0; j < (int)(m); ++j)
if (!arr[i][j]) return false;
return true;
}
void copy(bool from[61][61], bool to[61][61]) {
for (int i = 0; i < (int)(n); ++i)
for (int j = 0; j < (int)(m); ++j) to[i][j] = from[i][j];
}
bool soln1() {
bool all = true;
for (int i = 0; i < (int)(n); ++i) {
if (!arr[i][0]) all = false;
}
if (all) return true;
all = true;
for (int i = 0; i < (int)(n); ++i) {
if (!arr[i][m - 1]) all = false;
}
if (all) return true;
all = true;
for (int j = 0; j < (int)(m); ++j) {
if (!arr[0][j]) all = false;
}
if (all) return true;
all = true;
for (int j = 0; j < (int)(m); ++j) {
if (!arr[n - 1][j]) all = false;
}
if (all) return true;
return false;
}
bool soln2() {
if (arr[0][0] || arr[n - 1][m - 1] || arr[n - 1][0] || arr[0][m - 1])
return true;
for (int i = 1; i < (int)(n - 1); i++) {
bool all = true;
for (int j = 0; j < (int)(m); ++j) {
if (!arr[i][j]) all = false;
}
if (all) return true;
}
for (int j = 1; j < (int)(m - 1); j++) {
bool all = true;
for (int i = 0; i < (int)(n); ++i) {
if (!arr[i][j]) all = false;
}
if (all) return true;
}
return false;
}
bool soln3() {
for (int i = 0; i < (int)(n); ++i) {
if (arr[i][0] || arr[i][m - 1]) return true;
}
for (int j = 0; j < (int)(m); ++j) {
if (arr[0][j] || arr[n - 1][j]) return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> t;
while (t--) {
cin >> n >> m;
char c;
for (int i = 0; i < (int)(n); ++i)
for (int j = 0; j < (int)(m); ++j) {
cin >> c;
arr[i][j] = c == 'A';
}
if (nosoln())
cout << "MORTAL" << endl;
else if (soln0())
cout << "0" << endl;
else if (soln1())
cout << "1" << endl;
else if (soln2())
cout << "2" << endl;
else if (soln3())
cout << "3" << endl;
else
cout << "4" << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll mod = 1e9 + 7;
const double eps = 1e-9;
int n, m;
char g[1100][1100];
bool used[1100][1100];
bool whitecol[1100];
bool whiterow[1100];
void dfs(int i, int j) {
if (!used[i][j] && g[i][j] == '#') {
used[i][j] = true;
if (i - 1 >= 0) dfs(i - 1, j);
if (j + 1 < m) dfs(i, j + 1);
if (i + 1 < n) dfs(i + 1, j);
if (j - 1 >= 0) dfs(i, j - 1);
}
}
void solve() {
cin >> n >> m;
for (int i = 0; i < max(n, m); i++) {
whitecol[i] = true;
whiterow[i] = true;
}
for (int i = 0; i < n; i++) cin >> g[i];
for (int i = 0; i < n; i++) {
int l = 1e9, r = -1e9, cnt = 0;
for (int j = 0; j < m; j++) {
if (g[i][j] == '#') {
whiterow[i] = false;
whitecol[j] = false;
cnt++;
l = min(l, j);
r = j;
}
}
if (cnt && cnt != r - l + 1) {
cout << "-1\n";
return;
}
}
for (int j = 0; j < m; j++) {
int u = 1e9, d = -1e9, cnt = 0;
for (int i = 0; i < n; i++) {
if (g[i][j] == '#') {
cnt++;
u = min(i, u);
d = i;
}
}
if (cnt && cnt != d - u + 1) {
cout << "-1\n";
return;
}
}
for (int i = 0; i < n; i++) {
if (whiterow[i]) {
bool chk = false;
for (int j = 0; j < m; j++) {
if (whitecol[j]) chk = true;
}
if (!chk) {
cout << "-1\n";
return;
}
}
}
for (int j = 0; j < m; j++) {
if (whitecol[j]) {
bool chk = false;
for (int i = 0; i < n; i++) {
if (whiterow[i]) chk = true;
}
if (!chk) {
cout << "-1\n";
return;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] == '#' && !used[i][j]) {
ans++;
dfs(i, j);
}
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct T {
int num = 0;
int k = 0;
bool vis[27] = {0};
} str[110];
int n;
int f(int i, int j) {
int sum = 0;
for (int h = 0; h < n; h++) {
if (str[h].k == 2 && str[h].vis[i] && str[h].vis[j]) sum += str[h].num;
if (str[h].k == 1 && (str[h].vis[i] || str[h].vis[j])) sum += str[h].num;
}
return sum;
}
int main() {
int j, i;
char s;
scanf("%d", &n);
getchar();
for (i = 0; i < n; i++) {
while (scanf("%c", &s) && s != 10) {
if (str[i].vis[s - 'a'] == 0) {
str[i].vis[s - 'a'] = 1;
str[i].k++;
}
str[i].num++;
}
}
int m = 0, ans = 0;
for (i = 0; i < 25; i++) {
for (j = i + 1; j <= 25; j++) {
m = f(i, j);
ans = max(ans, m);
}
}
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7;
const long long mod = 998244353;
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
int n, l, k;
cin >> n >> l >> k;
string s;
cin >> s;
sort(s.begin(), s.end());
s = "?" + s;
vector<string> ans(n + 1);
int last;
vector<int> freq(26, 0);
for (int i = 1, j = 1; i <= n * l; i++, j++) {
if (j < k) {
ans[j].push_back(s[i]);
freq[s[i] - 'a']++;
} else {
ans[k].push_back(s[i]);
if (ans[k].size() == l) {
last = i + 1;
break;
}
int cnt = freq[s[i] - 'a'];
j = k - cnt - 1;
for (int x = 0; x < 26; x++) freq[x] = 0;
}
}
for (int i = last, j = 1; i <= n * l; i++) {
while ((j <= n) && ((ans[j].length() == l) || (j == k))) j++;
ans[j].push_back(s[i]);
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < l; j++) cout << ans[i][j];
cout << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10, size = 1 << 20, mod = 998244353, inf = 2e9;
const long long INF = 1e15;
template <class o>
void qr(o& x) {
char c = getchar();
x = 0;
int f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
x *= f;
}
template <class o>
void qw(o x) {
if (x / 10) qw(x / 10);
putchar(x % 10 + '0');
}
template <class o>
void pr1(o x) {
if (x < 0) x = -x, putchar('-');
qw(x);
putchar(' ');
}
template <class o>
void pr2(o x) {
if (x < 0) x = -x, putchar('-');
qw(x);
putchar('\n');
}
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long power(long long a, long long b = mod - 2, long long p = mod) {
long long c = 1;
while (b) {
if (b & 1) c = c * a % p;
b /= 2;
a = a * a % p;
}
return c;
}
template <class o>
void cmax(o& x, o y) {
if (x < y) x = y;
}
void cmax(int& x, int y) {
x = x - y >> 31 ? y : x;
;
}
template <class o>
void cmin(o& x, o y) {
if (x > y) x = y;
}
void cmin(int& x, int y) { x = x - y >> 31 ? x : y; }
template <typename t1, typename t2>
void ad(t1& x, t2 y) {
x += y;
if (x >= mod) x -= mod;
}
template <typename t1, typename t2>
void dl(t1& x, t2 y) {
x -= y;
if (x < 0) x += mod;
}
int n, m;
int mx[N << 2];
void change(int x, int l, int r, int pos, int d) {
if (l == r) {
mx[x] = d;
return;
}
if (pos <= ((l + r) >> 1))
change((x << 1), l, ((l + r) >> 1), pos, d);
else
change((x << 1 | 1), ((l + r) >> 1) + 1, r, pos, d);
cmax(mx[x] = mx[(x << 1)], mx[(x << 1 | 1)]);
}
int ask(int x, int l, int r, int L, int R) {
if (L <= l && r <= R) return mx[x];
int s = 0;
if (L <= ((l + r) >> 1)) cmax(s, ask((x << 1), l, ((l + r) >> 1), L, R));
if (((l + r) >> 1) < R)
cmax(s, ask((x << 1 | 1), ((l + r) >> 1) + 1, r, L, R));
return s;
}
int trie[N][27], tot = 1, pos[N];
vector<int> name[N];
void insert(char* s, int now) {
int p = 1;
for (int i = 1; s[i]; i++) {
int c = s[i] - 'a';
if (!trie[p][c]) trie[p][c] = ++tot;
p = trie[p][c];
}
name[p].push_back(now);
pos[now] = p;
}
int fail[N];
void add(int x, int y);
void bfs() {
static int q[N], l, r;
q[l = r = 1] = 1;
for (int i = 0; i <= 25; i++) trie[0][i] = 1;
while (l <= r) {
int p = q[l++], x, y;
for (int c = 0; c <= 25; c++) {
x = trie[p][c];
if (!x) {
trie[p][c] = trie[fail[p]][c];
continue;
}
y = fail[p];
if (p == 1)
fail[x] = 1;
else
fail[x] = trie[fail[p]][c];
q[++r] = x;
add(fail[x], x);
}
}
}
int fa[N], dep[N], son[N], sz[N], top[N], dfn[N], id;
vector<int> e[N];
void add(int x, int y) { e[x].push_back(y); }
void dfs1(int x) {
sz[x] = ((int)name[x].size());
son[x] = 0;
for (int y : e[x]) {
fa[y] = x;
dep[y] = dep[x] + 1;
dfs1(y);
sz[x] += sz[y];
if (sz[y] > sz[son[x]]) son[x] = y;
}
}
int st[N], ed[N];
void dfs2(int x, int tp) {
top[x] = tp;
st[x] = id + 1;
for (int y : name[x]) dfn[y] = ++id;
ed[x] = id;
if (son[x]) dfs2(son[x], tp);
for (int y : e[x])
if (y ^ son[x]) dfs2(y, y);
}
int Max(int x) {
int ans = -1;
while (x) {
if (st[top[x]] <= ed[x]) cmax(ans, ask(1, 1, n, st[top[x]], ed[x]));
x = fa[top[x]];
}
return ans;
}
void solve() {
qr(n);
qr(m);
static char s[N];
for (int i = 1; i <= n; i++) {
scanf("%s", s + 1);
insert(s, i);
}
bfs();
dfs1(1);
dfs2(1, 1);
while (m--) {
int op, i, x;
qr(op);
if (op == 1) {
qr(i);
qr(x);
change(1, 1, n, dfn[i], x);
} else {
scanf("%s", s + 1);
int ans = -1;
for (int i = 1, p = 1; s[i]; i++) {
int c = s[i] - 'a';
p = trie[p][c];
cmax(ans, Max(p));
}
pr2(ans);
}
}
}
int main() {
int T = 1;
while (T--) solve();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 2005;
int64_t dp[N][N];
int64_t powD[N];
int a[N][N];
int n;
struct fen {
int a[N] = {};
void add(int i, int v) {
++i;
while (i < N) {
a[i] += v;
i += i & (-i);
}
}
int get(int r) {
int ans = 0;
while (r > 0) {
ans += a[r];
r -= r & (-r);
}
return ans;
}
bool exist(int i) { return get(i + 1) - get(i); }
};
void calc_dp() {
dp[0][0] = 1;
for (int i = (1); i < (N); ++i) dp[i][i] = dp[i - 1][i - 1] * i % mod;
for (int j = (1); j < (N); ++j)
dp[j - 1][j] = dp[j - 1][j - 1] * (j - 1) % mod;
for (int i = (2); i < (N); ++i)
for (int j = (i); j < (N); ++j)
dp[j - i][j] =
(dp[j - i][j - 1] * (j - i) + dp[j - i + 1][j - 1] * (i - 1)) % mod;
powD[0] = 1;
for (int i = (1); i < (N); ++i) powD[i] = powD[i - 1] * dp[0][n] % mod;
}
int64_t cnt_first() {
int64_t ans = 0;
fen ghabli;
for (int i = (0); i < (n); ++i) ghabli.add(i, 1);
for (int i = (0); i < (n); ++i) {
ans += dp[n - 1 - i][n - 1 - i] * ghabli.get(a[0][i]);
ans %= mod;
ghabli.add(a[0][i], -1);
}
return ans * powD[n - 1] % mod;
}
int64_t cnt(int i) {
fen azad, zendan;
for (int i = (0); i < (n); ++i) zendan.add(i, 1);
int64_t ans = 0;
for (int j = (0); j < (n); ++j) {
ans += azad.get(a[i][j]) *
dp[azad.get(N) - 1 + zendan.exist(a[i - 1][j])][n - 1 - j] % mod;
ans += (zendan.get(a[i][j]) -
(a[i - 1][j] < a[i][j] && zendan.exist(a[i - 1][j]))) *
dp[azad.get(N) + zendan.exist(a[i - 1][j])][n - 1 - j] % mod;
ans %= mod;
if (azad.exist(a[i][j])) azad.add(a[i][j], -1);
if (zendan.exist(a[i][j])) zendan.add(a[i][j], -1);
if (zendan.exist(a[i - 1][j]))
zendan.add(a[i - 1][j], -1), azad.add(a[i - 1][j], 1);
}
return ans * powD[n - 1 - i] % mod;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
;
cin >> n;
for (int i = (0); i < (n); ++i)
for (int j = (0); j < (n); ++j) cin >> a[i][j], a[i][j]--;
calc_dp();
int64_t ans = 0;
ans += cnt_first();
for (int i = (1); i < (n); ++i) ans = (ans + cnt(i)) % mod;
cout << ans << '\n';
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int N, M;
map<int, int> mp;
int ans[100100];
int main() {
ios::sync_with_stdio(0);
cin >> N >> M;
for (int i = 1, f; i <= N; i++) {
cin >> f;
if (mp.count(f))
mp[f] = -1;
else
mp[f] = i;
}
bool amb = 0;
for (int i = 0, b; i < M; i++) {
cin >> b;
if (!mp.count(b)) {
cout << "Impossible\n";
return 0;
}
if (mp[b] == -1)
amb = 1;
else
ans[i] = mp[b];
}
if (amb)
cout << "Ambiguity\n";
else {
cout << "Possible\n";
for (int i = 0; i < M; i++) cout << ans[i] << " ";
cout << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
int smallest(int x, int y, int z) {
int c = 0;
while (x && y && z) {
x--;
y--;
z--;
c++;
}
return c;
}
long long fast_exp(long long base, long long exp) {
long long res = 1;
while (exp > 0) {
if (exp % 2 == 1) res = (res * base) % mod;
base = (base * base) % mod;
exp /= 2;
}
return res % mod;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
struct comparator {
bool operator()(int i, int j) { return i > j; }
};
int main() {
long long i, j, k;
long long arr[200005] = {0};
long long n;
long long maxi = 1000000000;
long long ans;
cin >> n >> k;
long long ind = k;
for (i = 1; i <= n; i++) {
scanf("%lld", &arr[i]);
if (i >= 1) arr[i] += arr[i - 1];
if (i >= k) {
ans = arr[i] - arr[i - k];
if (ans < maxi) {
maxi = ans;
ind = i;
}
}
}
printf("%lld\n", ind - k + 1);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long aa[200005];
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
int flag = 0;
int zuo, you;
for (int i = 1; i <= n; i++) {
cin >> aa[i];
}
for (int i = 2; i <= n; i++) {
if (abs(aa[i] - aa[i - 1]) >= 2) {
flag = 1;
zuo = i - 1;
you = i;
break;
}
}
if (flag == 1) {
cout << "YES" << endl;
cout << zuo << ' ' << you << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, k;
cin >> n >> k;
long long l1, r1;
long long l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
long long intersection = (min(r1, r2) - max(l1, l2)) * n;
long long ans = 0;
if (intersection < 0) {
long long amnt_1_op = (max(r1, r2) - min(l1, l2));
int full_cost_one = -1 * intersection / n;
if (0) {
ans += full_cost_one + amnt_1_op + (k - amnt_1_op) * 2;
cout << ans << endl;
return;
} else {
if (k <= amnt_1_op * n) {
int cnt = k / amnt_1_op;
ans += (full_cost_one + amnt_1_op) * cnt;
k -= amnt_1_op * cnt;
if (ans != 0)
ans = min(ans + full_cost_one + k, ans + 2 * k);
else
ans = ans + full_cost_one + k;
} else {
ans += (full_cost_one + amnt_1_op) * n;
k -= amnt_1_op * n;
ans += 2 * k;
}
}
cout << ans << endl;
} else {
k -= intersection;
if (k <= 0) {
cout << ans << endl;
return;
}
long long amnt_1_op =
(max(r1, r2) - min(l1, l2) - (min(r1, r2) - max(l1, l2))) * n;
if (k <= amnt_1_op) {
ans = k;
} else {
k -= amnt_1_op;
ans += amnt_1_op;
long long amnt_2_op = 2 * k;
ans += amnt_2_op;
}
cout << ans << endl;
}
return;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<int> v(N);
for (int c = 0; c < N; c++) {
cin >> v[c];
}
if (v[N - 1] == 0) {
for (int c = 0; c < N + 1; c++) {
cout << c + 1 << " ";
}
cout << "\n";
continue;
}
if (v[0] == 1) {
cout << N + 1 << " ";
for (int c = 0; c < N; c++) {
cout << c + 1 << " ";
}
cout << "\n";
continue;
}
int lastBeforeEnd = N - 1;
while (lastBeforeEnd >= 0) {
if (v[lastBeforeEnd] == 0) break;
lastBeforeEnd--;
}
for (int c = 0; c < N; c++) {
cout << c + 1 << " ";
if (lastBeforeEnd == c) {
cout << N + 1 << " ";
}
}
cout << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long solve(long long n) {
long long ans = 0;
vector<long long> v;
for (int i = 0; i < 42; i++) {
v.push_back(n / (1ll << i));
}
for (int i = 0; i < v.size() - 1; i++) {
ans += (1ll << i) * (v[i] - v[i + 1]);
}
return ans;
}
int main(int argc, char const *argv[]) {
long long n;
cin >> n;
cout << solve(n - 1) << endl;
}
| 5 |
#include<bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define f first
#define s second
#define int long long
#define pb push_back
#define ld long double
const int N = 3e5 + 10, inf = 1e18 + 10;
int a[N], n, b[N], k;
bool ok(int num){
for(int i = 0; i < n; ++i){
if(a[i] >= num)
b[i] = 1;
else
b[i] = -1;
if(i >= 1)
b[i] += b[i - 1];
}
int minval = 0;
for(int i = k - 1; i < n; ++i){
if(i >= k)
minval = min(minval, b[i - k]);
if(b[i] - minval >= 1)
return true;
}
return false;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 0; i < n; ++i)
cin >> a[i];
int l = 1, r = n + 1;
while(r - l > 1){
int m = (l + r) / 2;
if(ok(m))
l = m;
else
r = m;
}
cout << l;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAXB = 1000000;
int ans, n;
string s;
int chisl(int l1, int l2) {
int t = 0;
for (int k = l1; k < l2; k++) {
t = t * 10 + (int)(s[k] - '0');
if (t > MAXB) return MAXB + 1;
}
return t;
}
int razd(int l, int r) {
if (s[0] == '0' && l != 1) return -1;
if (s[l] == '0' && r != l + 1) return -1;
if (s[r] == '0' && r != n - 1) return -1;
int t1 = chisl(0, l);
int t2 = chisl(l, r);
int t3 = chisl(r, n);
if ((t1 <= MAXB) && (t2 <= MAXB) && (t3 <= MAXB)) {
return t1 + t2 + t3;
} else
return -1;
}
int main() {
cin >> s;
n = s.size();
ans = -1;
for (int i = 1; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ans = max(ans, razd(i, j));
}
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 17;
const int M = 1e9 + 7;
int n, t;
pair<int, int> p[N];
int dp[1 << N][5];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> t;
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
p[i].second--;
}
int ans = 0;
dp[0][3] = 1;
for (int i = 0; i < (1 << n); i++) {
for (int last = 0; last < 4; last++) {
for (int j = 0; j < n; j++) {
if (p[j].second != last && ((i & (1 << j)) == 0)) {
dp[i ^ (1 << j)][p[j].second] += dp[i][last];
dp[i ^ (1 << j)][p[j].second] %= M;
}
}
int sum = 0;
for (int k = 0; k < n; k++) {
if (i & (1 << k)) {
sum += p[k].first;
}
}
if (sum == t) {
ans += dp[i][last];
ans %= M;
}
}
}
cout << (ans % M + M) % M << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long Maxn = 3005;
long long ax[Maxn], ay[Maxn], nx, ny, n, px[Maxn], py[Maxn], vis[Maxn][Maxn],
ans;
inline long long find_x(long long x) {
return lower_bound(ax + 1, ax + ax[0] + 1, x) - ax;
}
inline long long find_y(long long x) {
return lower_bound(ay + 1, ay + ay[0] + 1, x) - ay;
}
void dfs(long long x, long long y) {
if (vis[x][y] || x < 1 || x > ax[0] + 1 || y < 1 || y > ay[0] + 1) return;
vis[x][y] = 2;
dfs(x - 1, y), dfs(x, y - 1), dfs(x + 1, y), dfs(x, y + 1);
}
signed main() {
scanf("%lld", &n);
ax[++ax[0]] = -1, ax[++ax[0]] = 0, ax[++ax[0]] = 1;
ay[++ay[0]] = -1, ay[++ay[0]] = 0, ay[++ay[0]] = 1;
for (long long i = 1; i <= n; i++) {
char op[5];
long long x;
scanf("%s", op);
scanf("%lld", &x);
if (op[0] == 'R') ny += x;
if (op[0] == 'U') nx += x;
if (op[0] == 'L') ny -= x;
if (op[0] == 'D') nx -= x;
px[i] = nx, py[i] = ny;
ax[++ax[0]] = nx - 1, ax[++ax[0]] = nx, ax[++ax[0]] = nx + 1;
ay[++ay[0]] = ny - 1, ay[++ay[0]] = ny, ay[++ay[0]] = ny + 1;
}
sort(ax + 1, ax + ax[0] + 1);
sort(ay + 1, ay + ay[0] + 1);
ax[0] = unique(ax + 1, ax + ax[0] + 1) - (ax + 1);
ay[0] = unique(ay + 1, ay + ay[0] + 1) - (ay + 1);
for (long long i = 1; i <= n; i++) {
long long p1 = find_x(px[i - 1]), q1 = find_y(py[i - 1]),
p2 = find_x(px[i]), q2 = find_y(py[i]);
if (p1 == p2)
for (long long j = min(q1, q2); j <= max(q1, q2); j++) vis[p1][j] = 1;
if (q1 == q2)
for (long long j = min(p1, p2); j <= max(p1, p2); j++) vis[j][q1] = 1;
}
dfs(1, 1);
for (long long i = 1; i <= ax[0]; i++) {
for (long long j = 1; j <= ay[0]; j++)
if (vis[i][j] != 2) ans += (ax[i] - ax[i - 1]) * (ay[j] - ay[j - 1]);
}
printf("%lld", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse3,sse4,popcnt,avx,mmx,abm,tune=native")
using namespace std;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
long long n, k;
cin >> n >> k;
vector<bool> dp(k);
dp[0] = true;
long long sum = 0;
for (long long i = 0; i < n; i++) {
long long a, b;
cin >> a >> b;
sum += a + b;
vector<long long> v = {a};
for (long long j = 0; j <= k; j++) {
long long na = a - j, nb = b - (k - j);
if (na >= 0 && nb >= 0) v.push_back(na);
}
vector<bool> tmp(k);
for (long long j = 0; j < k; j++) {
if (dp[j])
for (long long x : v) tmp[(j + x) % k] = true;
}
dp = tmp;
}
long long ans = 0;
for (long long i = 0; i < k; i++) {
if (!dp[i]) continue;
ans = max(ans, (sum - i - ((sum - i) % k)) / k);
}
cout << ans << "\n";
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using lld = long double;
using ulli = unsigned long long int;
using pll = pair<lli, lli>;
using ttt = pair<lli, pll>;
using vttt = vector<ttt>;
using vll = vector<pll>;
using vl = vector<lli>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using cd = complex<lld>;
const double PI = acos(-1);
const int lim = 20;
int up[100005][lim];
int timer = 0;
int tin[100005], tout[100005];
lli depth[100005];
int counter;
vi dlow, issp, prefsp;
void lcadfs(int v, int p, int dep, vvi &adj) {
tin[v] = ++timer;
depth[v] = dep;
prefsp[v] = prefsp[p] + issp[v];
up[v][0] = p;
for (int i = 1; i < lim; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
for (auto &u : adj[v]) {
if (u != p) lcadfs(u, v, dep + 1, adj);
}
tout[v] = ++timer;
}
bool is_ancestor(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
if (is_ancestor(u, v)) return u;
if (is_ancestor(v, u)) return v;
for (int i = lim - 1; i >= 0; --i) {
if (!is_ancestor(up[u][i], v)) u = up[u][i];
}
return up[u][0];
}
void tarjanSCC(int v, int p, vvi &adj) {
dlow[v] = counter++;
for (auto &x : adj[v]) {
if (dlow[x] == 0) tarjanSCC(x, v, adj);
if (x != p) dlow[v] = min(dlow[v], dlow[x]);
}
}
void dfs(int v, int p, vvi &adj) {
prefsp[v] = prefsp[p] + issp[v];
for (auto &x : adj[v]) {
if (x != p) dfs(x, v, adj);
}
}
void storeSCC(int num, vvi &adj) {
int n = adj.size();
counter = 1;
dlow = prefsp = issp = vi(n, 0);
tarjanSCC(1, 0, adj);
vvi cadj(n);
for (int i = 1; i < num + 1; i++) {
auto cur = &cadj[dlow[i]];
for (auto &x : adj[i])
if (dlow[x] != dlow[i]) (*cur).push_back(dlow[x]);
}
for (auto &x : dlow) issp[x]++;
for (auto &x : issp)
if (x > 1)
x = 1;
else
x = 0;
for (int i = 0; i < counter + 2; i++) {
sort(cadj[i].begin(), cadj[i].end());
cadj[i].erase(unique(cadj[i].begin(), cadj[i].end()), cadj[i].end());
}
lcadfs(dlow[1], dlow[1], 0, cadj);
}
inline lli mpow(lli a, lli b, lli m = 1000000007) {
lli ans = 1;
b = ((b % 1000000007) + 1000000007) % 1000000007;
while (b) {
if (b & 1) ans = (ans * a) % m;
a = (a * a) % m;
b /= 2;
}
return ans;
}
int tejas_919(int kkkk) {
lli n, m, k, q, u, v, temp = 0, ans = 0;
cin >> n >> m;
vvi adj(n + 5);
for (int i = 0; i < m; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
storeSCC(n, adj);
cin >> k;
while (k--) {
cin >> u >> v;
u = dlow[u];
v = dlow[v];
temp = lca(u, v);
{}
ans = prefsp[u] + prefsp[v] - (2LL * prefsp[temp]) + issp[temp];
ans = mpow(2, ans);
cout << ans << '\n';
}
return 0;
}
signed main() {
if (!0) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
cout << fixed << setprecision(10);
int t = 1;
for (int i = 0; i < t; i++) {
tejas_919(i + 1);
}
{};
if (0) system("pause");
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> graph[200005];
bool visited[200005];
long long mod = 1000000007;
long long dfs(long long i) {
visited[i] = true;
for (long long j = 0; j < graph[i].size(); j++) {
if (!visited[graph[i][j]]) {
dfs(graph[i][j]);
}
}
}
vector<string> s;
long long n, m;
bool check(string t) {
for (long long i = 0; i < n; i++) {
long long cnt = 0;
for (long long j = 0; j < m; j++) {
if (t[j] != s[i][j]) cnt++;
}
if (cnt > 1) return false;
}
return true;
}
void solve() {
cin >> n >> m;
s.clear();
for (long long i = 0; i < n; i++) {
string temp;
cin >> temp;
s.push_back(temp);
}
string ans = s[0];
if (check(ans)) {
cout << ans << endl;
return;
} else {
bool flag = false;
for (long long i = 0; i < m; i++) {
string str = s[0];
for (char c = 'a'; c <= 'z'; c++) {
str[i] = c;
if (check(str)) {
cout << str << endl;
flag = true;
}
}
if (flag) break;
}
if (flag == false) cout << -1 << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
int main() {
int n, k, fst0 = -1, fst1 = -1, last0, last1;
char s[100001];
int prefOf1[100000];
scanf("%i%i\n%s", &n, &k, s);
if (s[0] - '0')
fst1 = 0;
else
fst0 = 0;
prefOf1[0] = s[0] - '0';
for (int i = 1; i < n; i++) {
prefOf1[i] = prefOf1[i - 1] + s[i] - '0';
if (s[i] - '0') {
if (fst1 == -1) fst1 = i;
last1 = i;
} else {
if (fst0 == -1) fst0 = i;
last0 = i;
}
}
for (int i = 0; i <= n - k; i++) {
if (prefOf1[n - 1] - (prefOf1[i + k - 1] - (i > 0) * (prefOf1[i - 1])) ==
n - k ||
prefOf1[n - 1] == (prefOf1[i + k - 1] - (i > 0) * (prefOf1[i - 1]))) {
printf("tokitsukaze");
return 0;
}
}
if (k == 1 || k * 2 < n) {
printf("once again");
return 0;
}
for (int i = 1; i < n - k - 1; i++) {
if (s[i] != s[i - 1] || s[n - i] != s[n - i - 1]) {
printf("once again");
return 0;
}
}
printf("quailty");
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n;
const double eps = 1e-6;
inline double dcmp(double x) {
if (x < -eps) return -1;
if (x > eps) return 1;
return 0;
}
const int maxn = 105;
struct point {
double x, y;
point() {}
point(double _x, double _y) { x = _x, y = _y; }
friend bool operator==(point a, point b) { return a.x == b.x && a.y == b.y; }
} P[maxn];
inline double sqr(double x) { return x * x; }
inline double dist(point A, point B) {
return sqrt(sqr(A.x - B.x) + sqr(A.y - B.y));
}
point empty = point(-1e9, -1e9);
point calc_circle(point A, point B, point C) {
double d1 = sqr(A.x) + sqr(A.y) - sqr(B.x) - sqr(B.y);
double d2 = sqr(B.x) + sqr(B.y) - sqr(C.x) - sqr(C.y);
double dx1 = A.x - B.x, dx2 = B.x - C.x;
double dy1 = A.y - B.y, dy2 = B.y - C.y;
double tmp = (dx2 * dy1 - dx1 * dy2) * 2.0;
if (dcmp(tmp) == 0) return empty;
return point((d2 * dy1 - d1 * dy2) / tmp, (d1 * dx2 - d2 * dx1) / tmp);
}
int main() {
scanf("%d", &n);
for (int i = 1, x, y; i <= n; i++) {
scanf("%d%d", &x, &y);
P[i] = point(x, y);
}
double ans = -1;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
for (int k = j + 1; k <= n; k++) {
point A = P[i], B = P[j], C = P[k];
double len[3] = {dist(A, B), dist(B, C), dist(A, C)};
sort(len, len + 3);
point p = calc_circle(A, B, C);
if (p == empty) continue;
int flag = dcmp(sqr(len[2]) - sqr(len[1]) - sqr(len[0]));
if (flag == 1) continue;
if (flag == 0) {
if (dist(A, B) == len[2]) swap(A, C);
if (dist(A, C) == len[2]) swap(A, B);
point D = point(B.x + C.x - A.x, B.y + C.y - A.y);
for (int l = 1; l <= n; l++)
if (P[l] == D) flag = 1;
}
if (flag) {
double dis = 1e9;
for (int l = 1; l <= n; l++) dis = min(dis, dist(p, P[l]));
if (dcmp(dis - len[2] / 2) >= 0) ans = max(ans, dis);
}
}
if (ans == -1) puts("-1"), exit(0);
cout << fixed << setprecision(6) << ans << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
long long n, q;
cin >> n >> q;
vector<long long> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
if (8 * q / n > 20) {
cout << 0;
return 0;
}
sort((v).begin(), (v).end());
q = 1 << (8 * q / n);
vector<long long> d(n);
d[0] = 1;
for (int i = 1; i < n; ++i) {
d[i] = d[i - 1];
d[i] += (v[i] != v[i - 1]);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
int x = lower_bound((d).begin(), (d).end(), q + d[i]) - d.begin();
ans = max(ans, x - i);
}
cout << n - ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int ans;
void solve(int l, int r, int pl, int pr, int dep) {
if (l > r || pl > pr) return;
if (l >= pl && r <= pr) {
ans = ((ans) > (r - l + 1) ? (ans) : (r - l + 1));
return;
}
if (pl >= l && pr <= r) {
ans = ((ans) > (pr - pl + 1) ? (ans) : (pr - pl + 1));
return;
}
if (l > pl) {
swap(l, pl), swap(r, pr);
}
int w = (1 << dep);
if (pl > w) return solve(l, r, pl - w, pr - w, dep);
if (l > w) return solve(l - w, r - w, pl, pr, dep);
if (pr < w && r < w) return solve(l, r, pl, pr, dep - 1);
ans = ((ans) > (((pr) < (r) ? (pr) : (r)) - ((l) > (pl) ? (l) : (pl)) + 1)
? (ans)
: (((pr) < (r) ? (pr) : (r)) - ((l) > (pl) ? (l) : (pl)) + 1));
solve(l, r, pl, w - 1, dep);
solve(l, r, w + 1, pr, dep);
}
int main() {
ans = 0;
int l, r, pl, pr;
scanf("%d%d%d%d", &l, &r, &pl, &pr);
solve(l, r, pl, pr, 30);
printf("%d\n", ans);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
mt19937 gen(time(NULL));
const long double eps = 1e-9;
const int inf = 1e9;
const int mod = 1e9 + 7;
const long long infinity = 2 * 1e18;
int n, m;
int w[100500];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> w[i];
int sum = 0;
for (int i = 0; i < n; i++) sum += w[i];
double x = sum / (double)n;
if ((int)round(x) == m)
cout << 0;
else {
int calc = 0;
while (true) {
sum += m;
calc++;
x = sum / (double)(n + calc);
if (x >= m - 0.5) break;
}
cout << calc << '\n';
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long O = 2e9;
const double E = 1e-9;
const double pi = 3.1415926536;
int DX[] = {1, 0, -1, 0};
int DY[] = {0, 1, 0, -1};
int main() {
int n;
cin >> n;
long long x1, x2;
cin >> x1 >> x2;
vector<pair<long long, long long> > arr;
for (int i = 0; i < n; i++) {
long long k, b;
cin >> k >> b;
long long y1 = k * x1 + b;
long long y2 = k * x2 + b;
arr.push_back({y1, y2});
}
sort(arr.begin(), arr.end());
for (int i = 0; i < n - 1; i++)
if (arr[i].first < arr[i + 1].first && arr[i].second > arr[i + 1].second) {
cout << "YES\n";
return 0;
}
cout << "NO\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char arr[300001];
int main(void) {
int N;
scanf("%d", &N);
scanf("%s", arr);
int i;
char _max = 0;
int idx = -1;
for (i = 0; i < N; i++) {
if (_max < arr[i]) {
_max = arr[i];
idx = i;
}
if (idx != -1 && _max > arr[i]) {
printf("YES\n");
printf("%d %d\n", idx + 1, i + 1);
return 0;
}
}
printf("NO\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> v(n);
for (auto &x : v) {
cin >> x;
if (x % 2 == 0) x--;
}
for (auto data : v) cout << data << ' ';
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, a, b;
cin >> n >> m >> a >> b;
int ans = 0;
int i = 0;
int rides = 0;
while (rides < n) {
if (rides + m <= n) {
ans += min(b, m * a);
} else {
ans += min(b, (n - rides) * a);
}
rides += m;
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int mod1(long long int x) {
return (((x % 1000000007) + 1000000007) % 1000000007);
}
long long int mul(long long int a, long long int b) {
return mod1(mod1(a) * mod1(b));
}
void solve();
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
void solve() {
long long int n;
cin >> n;
vector<long long int> a(n);
for (auto& i : a) cin >> i;
vector<vector<long long int>> ans;
for (long long int i = 0; i < n; i++) {
long long int max_ind = max_element(a.begin(), a.end()) - a.begin();
if (max_ind != n - i - 1) {
ans.push_back({max_ind + 1, n - i, 1});
}
a.erase(a.begin() + max_ind);
}
cout << ans.size() << "\n";
for (auto i : ans) cout << i[0] << " " << i[1] << " " << i[2] << "\n";
}
void solve1() {
long long int n;
cin >> n;
vector<long long int> a(n), b(n);
for (auto& i : a) cin >> i;
b = a;
sort(b.begin(), b.end());
if (a == b) {
cout << "0\n";
return;
}
vector<long long int> d;
vector<pair<long long int, long long int>> p;
long long int k = 0;
for (long long int i = 0; i < n; i++) {
vector<long long int>::iterator it = max_element(a.begin(), a.end() - i);
if (a[i] == b[i]) continue;
k++;
p.push_back({(it - a.begin() + 1), n - i});
long long int dif = (it - a.begin()) - i;
d.push_back(dif);
long long int temp = (*it);
for (long long int j = (it - a.begin()); j < (n - i - 1); j--) {
a[j] = a[j + 1];
}
a[n - i] = temp;
}
cout << k << "\n";
if (k) {
for (int i = 0; i < p.size(); i++)
cout << p[i].first << " " << p[i].second << " 1\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x = 0, y = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int v;
cin >> v;
if (v < 0)
y += v;
else
x += v;
}
cout << x - y;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long powr(long long m, long long n) {
if (m == 1) return 1;
if (n == 0) return 1;
if (n == 1) return m % 1000000007;
if (n % 2) {
long long te = powr(m, n / 2);
return (((te * te) % 1000000007) * (m % 1000000007)) % 1000000007;
} else {
long long te = powr(m, n / 2);
return (te * te) % 1000000007;
}
}
vector<int> v;
int cnt[15];
long long ans;
long long fact[20];
void gen(int k) {
if (k == 10) {
int totall = 0;
for (int i = 0; i < v.size(); i++) {
totall += v[i];
}
if (!cnt[0]) {
long long tmp = fact[totall];
for (int j = 0; j < v.size(); j++) tmp /= fact[v[j]];
ans += tmp;
}
for (int i = 1; i <= cnt[0]; i++) {
int total = i + totall;
long long tmp = fact[total];
for (int j = 0; j < v.size(); j++) tmp /= fact[v[j]];
tmp /= fact[i];
long long tmp2 = 0LL;
tmp2 = fact[total - 1];
for (int j = 0; j < v.size(); j++) tmp2 /= fact[v[j]];
tmp2 /= fact[i - 1];
ans += (tmp - tmp2);
}
} else {
if (cnt[k] == 0) gen(k + 1);
for (int i = 1; i <= cnt[k]; i++) {
v.push_back(i);
gen(k + 1);
v.pop_back();
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
int cs = 0;
while (tc--) {
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
cnt[s[i] - '0']++;
}
fact[0] = 1;
for (long long i = 1; i < 20; i++) fact[i] = i * fact[i - 1];
gen(1);
cout << ans;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9, MOD = 1e9 + 7;
const int n_ = 1e5 + 1000;
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
long long power(long long a, long long n) {
long long p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
}
n >>= 1;
a *= a;
}
return p;
}
long long power(long long a, long long n, long long mod) {
long long p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
p %= mod;
}
n >>= 1;
a *= a;
a %= mod;
}
return p % mod;
}
int main() {
ios_base::sync_with_stdio(false);
long long n, x, y, a;
scanf("%lld %lld %lld", &n, &x, &y);
a = y - n + 1;
if (a > 0 && a * a + n - 1 >= x) {
for (int(i) = 0; (i) < (n - 1); (i)++) puts("1");
printf("%lld\n", a);
} else {
puts("-1");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
inline int add(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
inline int sub(int a, int b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
inline int mul(int a, int b) { return (int)((long long)a * b % MOD); }
inline int binpow(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1) res = mul(res, a);
a = mul(a, a);
b /= 2;
}
return res;
}
inline int inv(int a) { return binpow(a, MOD - 2); }
int prime[1000005];
void find_prime() {
for (long long i = 0; i < 1000005 - 1; i++) prime[i] = 1;
for (int i = 2; i * i < 1000005; i++) {
if (prime[i])
for (int j = i * i; j < 1000005; j += i) {
prime[j] = 0;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
find_prime();
int n;
cin >> n;
int ans = 0;
for (long long i = n; i < 2 * n; i++) {
if (prime[i]) {
ans = i;
break;
}
}
cout << ans << "\n";
for (long long i = 1; i < n + 1; i++) {
cout << i << " " << i % n + 1 << "\n";
ans--;
}
for (long long i = 1; i < n + 1; i++) {
if (ans == 0) break;
cout << i << " " << (i + n / 2 - 1) % n + 1 << "\n";
ans--;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long int mxN = 150000;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long int n, x;
cin >> n >> x;
long long int a[n];
long long int sum = 0;
long long int cnt = 0;
bool ok = false;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
if (a[i] != x)
ok = true;
else
cnt++;
}
if (!ok) {
cout << "0" << endl;
} else if (sum % n == 0 and sum / n == x) {
cout << "1" << endl;
} else if (cnt >= 1) {
cout << "1" << endl;
} else
cout << "2" << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[2001];
int depth(int p) {
if (a[p] == -1) return 1;
return depth(a[p]) + 1;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int best = 0;
for (int i = 1; i <= n; i++) {
int val = depth(i);
if (val > best) best = val;
}
cout << best << endl;
return 0;
}
| 0 |
#include <stdio.h>
#include<iomanip>
#include<iostream>
#include<iostream>
#include <algorithm>
#include<string.h>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<bits/stdc++.h>
using namespace std;
#pragma warning(disable:4996)
#define inf 0x3f3f3f3f
#define PI 3.1415926535898
const double eps = 1e-7;
typedef long long LL;
#define ll long long
//LL GCD(LL a, LL b)
//{
// if (b == 0)
// return a;
// return GCD(b, a % b);
//}
//LL fac[N], inv[N];
//LL quick_pow(LL a, LL b)
//{
// LL ans = 1 % mod;
// while (b) {
// if (b & 1) ans = (ans * a) % mod;
// b >>= 1;
// a = (a * a) % mod;
// }
// return ans;
//}
//void init()
//{
// fac[0] = 1;
// inv[0] = 1;
// for (int i = 1; i <= N - 2; i++)
// {
// fac[i] = (fac[i - 1] * i) % mod;
// inv[i] = quick_pow(fac[i], mod - 2);
// }
//}
//LL C(LL a, LL b) //计算组合数C(a, b)
//{
// if (a < b || b < 0) return 0;
// return fac[a] * ((inv[a - b] * inv[b]) % mod) % mod;
//}
//LL n;
//LL a[N], c[N], d[N];
//void add(LL x, LL v)
//{
// LL i = x;
// while (x <= (n << 1))
// {
// c[x] += v;
// d[x] += v * (i - 1);
// x += (x & -x);
// }
//}
//LL sum(LL x)
//{
// LL res = 0, i = x;
// while (x > 0)
// {
// res += i * c[x] - d[x];
// x -= x & -x;
// }
// return res;
//}
//int prime[N];
//int vis[N];
//void get_prime()
//{
// for (int i = 2; i < N; i++)
// {
// if (!vis[i])
// {
// prime[++prime[0]] = i;
// }
// for (int j = 1; j <= prime[0] && i * prime[j] < N; j++)
// {
// vis[i * prime[j]] = 1;
// if (i % prime[j] == 0)
// break;
// }
// }
//}
//struct no {
// LL first;
// int id;
// no() {}
// no(LL first, int id) :first(first), id(id) {}
// bool operator<(const no& a)const
// {
// return first < a.first;//da顶堆
// }
//};
LL mod = 1e9 + 7;
const LL N = 3e5 + 10;
LL n, m, t;
LL a[N], b[N], c[N], aa[3];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
/*srand((int)time(0));*/
//freopen("C:\\Users\\Lenovo\\Desktop\\test\\in.txt", "r", stdin);
//freopen("C:\\Users\\Lenovo\\Desktop\\test\\out.txt", "w", stdout);
int n1, n2, n3;
cin >> n1 >> n2 >> n3;
LL sum1 = 0, sum2 = 0, sum3 = 0;
LL m1 = 1e17, m2 = 1e17, m3 = 1e17;
for (int i = 0; i < n1; i++)
{
cin >> a[i];
m1 = min(a[i], m1);
sum1 += a[i];
}
for (int i = 0; i < n2; i++)
{
cin >> b[i];
m2 = min(b[i], m2);
sum2 += b[i];
}
for (int i = 0; i < n3; i++)
{
cin >> c[i];
m3 = min(c[i], m3);
sum3 += c[i];
}
LL all = sum1 + sum2 + sum3;
LL ans=-1e17;
ans = max(ans, sum1 + sum2 - sum3);
ans = max(ans, sum1 + sum3 - sum2);
ans = max(ans, sum2 + sum3 - sum1);
ans = max(ans, all - 2 * (m1 + m2));
ans = max(ans, all - 2 * (m1 + m3));
ans = max(ans, all - 2 * (m3 + m2));
cout << ans << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 250005, M = 1 << 17, D = 18;
int P;
int power(int a, int t) {
int r = 1;
while (t) {
if (t & 1) r = (long long)r * a % P;
a = (long long)a * a % P;
t >>= 1;
}
return r;
}
int L, K, fac[N], n, m, bit[20], ifac[N], bitcnt[N];
long long suma[D][M], sumc[D][M];
unsigned long long dp[D][M], tmp[D][M];
int main() {
scanf("%d%d%d", &L, &K, &P);
fac[0] = 1;
for (int i = 1; i <= L; i++) {
fac[i] = (long long)fac[i - 1] * i % P;
bitcnt[i] = bitcnt[i >> 1] + (i & 1);
}
long long all = power(K, L);
if (L & 1) printf("%lld\n", all), exit(0);
ifac[L] = power(fac[L], P - 2);
for (int i = L; i >= 1; i--) ifac[i - 1] = (long long)ifac[i] * i % P;
for (int i = 1; (1 << i) <= L; i++)
if (L >> i & 1) bit[n++] = 1 << i;
m = 1 << n;
for (int i = 0; i < m; i++) {
int s = 0;
for (int j = 0; j < n; j++)
if (i >> j & 1) s += bit[j];
suma[bitcnt[i]][i] = ifac[s];
}
for (int t = 0; t <= n; t++) {
int i, j, k, l;
long long* a = suma[t];
for (l = m; l >= 2; l >>= 1)
for (j = 0, i = l >> 1; j < m; j += l)
for (k = 0; k < i; k++) a[j + k + i] += a[j + k];
for (j = 0; j < m; j++) a[j] = a[j] % P;
}
for (int t = 0; t <= n; t++) memcpy(dp[t], suma[t], m << 3);
int cc = 0, revK = 0;
while (K) (revK <<= 1) |= K & 1, cc++, K >>= 1;
for (cc--, revK >>= 1; cc; cc--, revK >>= 1) {
for (int t = 0; t <= n; t++) memset(tmp[t], 0, m << 3);
for (int a = 0; a <= n; a++)
for (int b = 0; a + b <= n; b++) {
unsigned long long *f = tmp[a + b], *g = dp[a], *h = dp[b];
for (int j = 0; j < m; j++) f[j] += g[j] * h[j];
}
for (int t = 0; t <= n; t++)
for (int j = 0; j < m; j++) dp[t][j] = tmp[t][j] % P;
if (revK & 1) {
for (int t = 0; t <= n; t++) memset(tmp[t], 0, m << 3);
for (int a = 0; a <= n; a++)
for (int b = 0; a + b <= n; b++) {
unsigned long long *f = tmp[a + b], *g = dp[a];
long long* h = suma[b];
for (int j = 0; j < m; j++) f[j] += g[j] * h[j];
}
for (int t = 0; t <= n; t++)
for (int j = 0; j < m; j++) dp[t][j] = tmp[t][j] % P;
}
}
for (int t = 0; t <= n; t++) memcpy(sumc[t], dp[t], m << 3);
for (int t = 0; t <= n; t++) {
int i, j, k, l;
long long* a = sumc[t];
for (l = 2; l <= m; l <<= 1)
for (j = 0, i = l >> 1; j < m; j += l)
for (k = 0; k < i; k++) a[j + k + i] -= a[j + k];
for (j = 0; j < m; j++) a[j] = (a[j] % P + P) % P;
}
long long ans = (all - sumc[n][(1 << n) - 1] * fac[L] % P + P) % P;
printf("%lld\n", ans);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int a[111111];
set<int> ans;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
set<int> cur, nxt;
for (int i = 0; i < n; i++) {
nxt.insert(a[i]);
for (int j : cur) {
nxt.insert(j | a[i]);
}
cur.clear();
for (int j : nxt) {
ans.insert(j);
cur.insert(j);
}
nxt.clear();
}
printf("%d\n", (int)ans.size());
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, p, q, i, j, r, n;
cin >> a >> b;
p = a;
while (a >= b) {
q = a / b;
p += q;
r = a % b;
a = q + r;
}
cout << p << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e4 + 10;
const int MOD = 1e9 + 7;
struct node {
long long i, j, x;
};
int t, n;
long long a[MAX];
vector<node> cnt;
int main() {
ios::sync_with_stdio(false), cout.tie(NULL);
cin >> t;
while (t--) {
cin >> n;
cnt.clear();
long long sum = 0, fg = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
if (a[i] >= i) fg = 1;
}
if (sum % n || !fg) {
cout << -1 << endl;
continue;
}
long long tmp;
for (int i = 2; i <= n; i++) {
tmp = a[i] / i;
if (a[1] >= i - (a[i] % i)) {
cnt.push_back({1, i, i - (a[i] % i)});
cnt.push_back({i, 1, tmp + 1});
a[1] += a[i];
a[i] = 0;
} else if (tmp) {
cnt.push_back({i, 1, tmp});
a[1] += tmp * i;
a[i] -= tmp * i;
}
}
for (int i = 2; i <= n; i++) {
if (a[i] > sum / n) {
cnt.push_back({1, i, sum / n + i - a[i]});
cnt.push_back({i, 1, 1});
a[1] += a[i] - sum / n;
a[i] = sum / n;
}
}
for (int i = 2; i <= n; i++) {
if (a[i] < sum / n) {
cnt.push_back({1, i, sum / n - a[i]});
a[1] -= (sum / n - a[i]);
a[i] += (sum / n - a[i]);
}
}
cout << cnt.size() << endl;
for (auto i : cnt) cout << i.i << " " << i.j << " " << i.x << endl;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int NMax = 1000005;
int N;
int Father[NMax];
int Max[NMax], Max2[NMax];
int DP[NMax];
vector<int> G[NMax];
void DFS(int node, int father) {
Father[node] = father;
for (int i = 0; i < G[node].size(); i++) {
int neighb = G[node][i];
if (neighb == father) continue;
DFS(neighb, node);
}
}
void Read() {
scanf("%d", &N);
for (int i = 2; i <= N + 1; i++) scanf("%d", &Father[i]);
}
void Solve(int node) {
DP[node] = 1;
int last = node;
node = Father[node];
while (node != 0) {
if (Max[node] < DP[last]) {
Max2[node] = Max[node];
Max[node] = DP[last];
} else if (Max2[node] < DP[last])
Max2[node] = DP[last];
if (max(Max[node], Max2[node] + 1) <= DP[node]) break;
if (node != 1)
DP[node] = max(Max[node], Max2[node] + 1);
else
DP[node] = Max[node];
last = node;
node = Father[node];
}
printf("%d ", DP[1]);
}
int main() {
Read();
for (int i = 2; i <= N + 1; i++) Solve(i);
printf("\n");
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000007;
const long long N = 10000000;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(numeric_limits<double>::max_digits10);
long long q;
cin >> q;
while (q--) {
string s;
cin >> s;
string s1 = s;
long long i = 0, j = 1;
long long scrA = 0, scrB = 0, ans1 = 10, ans2 = 10;
while (i < 10) {
if (i % 2 == 0) {
if (s[i] == '1' || s[i] == '?') scrA++;
if (scrB + (5 - i / 2) < scrA) {
ans1 = i + 1;
break;
}
i++;
}
if (i % 2) {
if (s[i] == '1') scrB++;
if (scrB + (4 - i / 2) < scrA) {
ans1 = i + 1;
break;
}
i++;
}
}
i = 0;
scrA = 0;
scrB = 0;
while (i < 10) {
if (i % 2 == 0) {
if (s[i] == '1') scrA++;
if (scrA + (4 - i / 2) < scrB) {
ans2 = i + 1;
break;
}
i++;
}
if (i % 2) {
if (s[i] == '?' || s[i] == '1') scrB++;
if (scrA + (4 - i / 2) < scrB) {
ans2 = i + 1;
break;
}
i++;
}
}
cout << min(ans1, ans2) << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
const long long MOD = 1e9 + 7;
const long long MAX = 150010;
long long n, a[MAX], b[MAX], b_idx;
void print(bool u) {
if (u) {
printf("YES\n");
for (long long i = 0; i < n; i++) printf("%lld ", a[i]);
printf("\n");
return;
}
printf("NO\n");
}
int main() {
scanf("%lld", &n);
for (long long i = 0; i < n; i++) scanf("%lld", &b[i]);
for (long long i = 0; i < n; i++)
if (b[b_idx] < b[i]) b_idx = i;
if (b[b_idx] == 0) {
for (long long i = 0; i < n; i++) a[i] = 1;
print(true);
return 0;
}
for (long long i = 0, t = 0; i < n; i++)
if (b[b_idx] == b[i]) {
t++;
if (t == n) {
print(false);
return 0;
}
if (b[(i - 1 + n) % n] != b[i]) b_idx = i;
}
a[b_idx] = b[b_idx];
for (long long i = b_idx, j = 0; j < n - 1; i = (i - 1 + n) % n, j++) {
long long pi = (i - 1 + n) % n;
if (i == b_idx)
a[pi] = a[i] * 2 + b[pi];
else
a[pi] = a[i] + b[pi];
}
print(true);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, a, b, c, d;
cin >> n >> a >> b >> c >> d;
int x = 2 * b;
if (n * (a - b) >= (c - d) && n * (a - b) <= (c + d))
cout << "YES\n";
else if (n * (a - b) <= (c - d) && n * x >= ((c - d) - n * (a - b)))
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int y;
int x = 0;
int z = 0;
cin >> y;
char chr[y];
for (int i = 0; i < y; i++) {
cin >> chr[i];
if (chr[i] == 'A') {
x++;
} else if (chr[i] == 'D') {
z++;
}
}
if (x > z) {
cout << "Anton";
} else if (z > x) {
cout << "Danik";
} else if (z == x) {
cout << "Friendship";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<vector<char> > m(2, vector<char>(n));
for (int i = 0; i < n; i++) cin >> m[0][i];
for (int i = 0; i < n; i++) cin >> m[1][i];
vector<vector<bool> > v(2, vector<bool>(n, false));
bool ha_arribat = false;
stack<pair<int, int> > q;
stack<int> q2;
q.push(pair<int, int>(0, 0));
q2.push(0);
while (!ha_arribat && !q.empty()) {
pair<int, int> p = q.top();
q.pop();
int c = p.first;
int a = p.second;
int w = q2.top();
q2.pop();
if (a + k >= n) {
ha_arribat = true;
continue;
}
v[c][a] = true;
if (a - 1 >= w + 1) {
if (!v[c][a - 1] && m[c][a - 1] != 'X') {
q.push(pair<int, int>(c, a - 1));
q2.push(w + 1);
}
}
if (!v[c][a + 1] && m[c][a + 1] != 'X') {
q.push(pair<int, int>(c, a + 1));
q2.push(w + 1);
}
if (!v[1 - c][a + k] && m[1 - c][a + k] != 'X') {
q.push(pair<int, int>(1 - c, a + k));
q2.push(w + 1);
}
}
if (ha_arribat)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long fib[100005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, m;
cin >> n >> m;
fib[0] = 1;
fib[1] = 1;
for (long long i = 2; i < 100005; i++) {
fib[i] = (fib[i - 1] % 1000000007 + fib[i - 2] % 1000000007) % 1000000007;
}
cout << (2 * ((fib[n] - 1) % 1000000007 + fib[m] % 1000000007)) % 1000000007;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const double pi = acos(-1.0);
const double eps = 1e-9;
int resa, resb, res;
map<pair<int, int>, pair<int, int> > m;
int a[200100], b[200100], c[200100], n;
vector<string> vres, ures;
void calc(int v, int x, int y, int z, int mask) {
if (v > n / 2) {
pair<int, int> p = make_pair(x - y, x - z);
m[p] = make_pair(z, mask);
return;
}
calc(v + 1, a[v] + x, y + b[v], z, mask * 4 + 1);
calc(v + 1, a[v] + x, y, z + c[v], mask * 4 + 2);
calc(v + 1, x, y + b[v], z + c[v], mask * 4 + 3);
}
void rec(int v, int x, int y, int z, int mask) {
if (v > n) {
pair<int, int> p = make_pair(y - x, z - x);
if (m.count(p)) {
pair<int, int> q = m[p];
if (q.first + z > res) {
res = q.first + z;
resa = q.second;
resb = mask;
}
}
return;
}
rec(v + 1, a[v] + x, y + b[v], z, mask * 4 + 1);
rec(v + 1, a[v] + x, y, z + c[v], mask * 4 + 2);
rec(v + 1, x, y + b[v], z + c[v], mask * 4 + 3);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i] >> c[i];
}
if (n == 1) {
if (a[1] == 0 && b[1] == 0) {
cout << "LM";
return 0;
}
if (c[1] == 0 && b[1] == 0) {
cout << "MW";
return 0;
}
if (a[1] == 0 && c[1] == 0) {
cout << "LW";
return 0;
}
cout << "Impossible";
return 0;
}
res = -inf;
calc(1, 0, 0, 0, 0);
rec(n / 2 + 1, 0, 0, 0, 0);
if (res == -inf)
cout << "Impossible";
else {
while (resa) {
if (resa % 4 == 1) vres.push_back("LM");
if (resa % 4 == 2) vres.push_back("LW");
if (resa % 4 == 3) vres.push_back("MW");
resa /= 4;
}
reverse(vres.begin(), vres.end());
while (resb) {
if (resb % 4 == 1) ures.push_back("LM");
if (resb % 4 == 2) ures.push_back("LW");
if (resb % 4 == 3) ures.push_back("MW");
resb /= 4;
}
reverse(ures.begin(), ures.end());
for (int i = 0; i < vres.size(); i++) cout << vres[i] << endl;
for (int i = 0; i < ures.size(); i++) cout << ures[i] << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
vector<int> parent, ans;
int mx, n, k;
void print(int idx) {
int u = parent[idx];
if (u == -1) {
cout << "! ";
for (int i = 0; i < k; i++) cout << mx << " ";
cout << endl;
} else {
int sz = 0;
for (int i = 1; i <= n; i++) {
if (parent[i] == u) continue;
sz++;
}
cout << "? " << sz << " ";
for (int i = 1; i <= n; i++) {
if (parent[i] == u) continue;
cout << i << ' ';
}
cout << endl;
int res;
cin >> res;
cout << "! ";
for (int i = 0; i < k; i++) {
if (i == u)
cout << res << " ";
else
cout << mx << " ";
}
cout << endl;
}
string res;
cin >> res;
}
void check(int lo, int hi) {
int mid = (hi - lo) / 2 + lo;
cout << "? " << mid - lo + 1 << " ";
for (int i = lo; i <= mid; i++) cout << i << " ";
cout << endl;
int res;
cin >> res;
if (res == mx) {
if (lo == mid) {
print(lo);
} else {
check(lo, mid);
}
} else {
if (mid + 1 == hi) {
print(hi);
} else {
check(mid + 1, hi);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt;
cin >> tt;
while (tt--) {
cin >> n >> k;
adj.clear();
ans.assign(k, 0);
parent.assign(n + 1, -1);
for (int i = 0; i < k; i++) {
int sz;
cin >> sz;
adj.emplace_back();
for (int j = 0; j < sz; j++) {
int a;
cin >> a;
parent[a] = i;
adj.back().push_back(a);
}
}
cout << "? " << n << " ";
for (int i = 1; i <= n; i++) cout << i << " ";
cout << endl;
cin >> mx;
check(1, n);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long x, point = 0, StepNum = 0;
cin >> x;
for (StepNum = sqrt(abs(x)); StepNum < 2 * abs(x); StepNum++) {
point = (1 + StepNum) * StepNum / 2;
if (point == abs(x))
break;
else if (point > abs(x) && (point - abs(x)) % 2 == 0) {
break;
}
}
cout << StepNum << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n;
cin >> n;
map<long long, long long> m;
for (long long i = 2; i * i <= n; i++) {
while (n % i == 0) {
m[i]++;
n /= i;
}
}
if (n > 1) m[n]++;
long long s = 0, ans = 1, p = 0;
if (m.size() == 0) {
cout << 1 << '\n' << 0;
return;
}
for (auto i : m) {
s += i.second;
if (p < 2) {
if (i.second > 1)
ans = i.first * i.first, p = 2;
else
ans *= i.first, p++;
}
}
if (s > 2) {
cout << 1 << '\n' << ans << '\n';
return;
}
if (s == 2) {
cout << 2 << '\n';
return;
}
if (s == 1) {
cout << 1 << '\n' << 0;
return;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream& operator<<(ostream& out, const pair<A, B>& p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template <class A>
ostream& operator<<(ostream& out, const vector<A>& v) {
out << "[";
for (int i = 0; i < v.size(); ++i) {
if (i) out << ", ";
out << v[i];
}
return out << "]";
}
const int N = 1e5 + 6;
const string yes = "Yes", no = "No";
vector<vector<pair<int, int>>> adj(N);
vector<int> par(N);
vector<set<int>> sets(N);
vector<map<int, int>> adj1(N);
int parent(int v) {
if (par[v] == v)
return v;
else
return par[v] = parent(par[v]);
}
void merge(int from, int to) {
if (from == to) return;
from = parent(from);
to = parent(to);
if (sets[to].size() > sets[from].size()) {
sets[to].insert(sets[from].begin(), sets[from].end());
set<int>().swap(sets[from]);
} else {
sets[from].insert(sets[to].begin(), sets[to].end());
set<int>().swap(sets[to]);
sets[from].swap(sets[to]);
}
}
void dsu(int u, int v) {
merge(parent(u), parent(v));
par[u] = parent(par[v]);
}
void add(int x, int y, int color) {
if (adj1[y][color]) {
dsu(parent(x), parent(adj1[y][color]));
}
if (adj1[x][color]) {
dsu(parent(y), parent(adj1[x][color]));
}
adj1[x][color] = y;
adj1[y][color] = x;
sets[parent(x)].insert(y);
sets[parent(y)].insert(x);
}
int main(int argc, char const* argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m, c, q;
cin >> n >> m >> c >> q;
iota(par.begin(), par.end(), 0);
for (int i = (0); i < (m); ++i) {
int x, y, z;
cin >> x >> y >> z;
add(x, y, z);
}
for (int i = (0); i < (q); ++i) {
char c;
int x, y, z;
cin >> c;
if (c == '+') {
cin >> x >> y >> z;
add(x, y, z);
} else {
cin >> x >> y;
if (parent(x) == parent(y)) {
cout << yes << endl;
} else if (sets[parent(x)].count(y) == 1) {
cout << yes << endl;
} else {
cout << no << endl;
}
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
string second;
int main() {
cin >> second;
int i = 0, n = second.size();
for (; i < n && second[i] == 'a'; i++)
;
if (i == n)
second[n - 1] = 'z';
else
for (; i < n && second[i] != 'a'; i++) second[i]--;
cout << second;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T GCD(T a, T b) {
return !a ? b : GCD(b % a, a);
}
template <typename T>
T LCM(T a, T b) {
return (a * b) / GCD(a, b);
}
template <typename T>
T fastPower(T base, T power) {
if (!power) return 1;
T ans = fastPower(base, power >> 1);
return power & 1 ? ans * ans * base : ans * ans;
}
vector<vector<int>> graph;
vector<bool> vis;
int n, m;
bool flag = true;
void DFS(int node) {
if (vis[node]) return;
vis[node] = true;
for (auto here : graph[node]) DFS(here);
}
int main() {
ios_base::sync_with_stdio();
cin.tie(0);
cout.tie(0);
scanf("%d%d", &n, &m);
graph.resize(n + m + 5);
vis.resize(n + m + 5);
for (int i = 1; i <= n; ++i) {
int k;
scanf("%d", &k);
flag &= k == 0;
while (k--) {
int u;
scanf("%d", &u);
graph[i].push_back(u + n);
graph[u + n].push_back(i);
}
}
int ans = -1;
if (flag) return printf("%d", n), 0;
for (int i = 1; i <= n; ++i)
if (not vis[i]) {
++ans;
DFS(i);
}
printf("%d", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[105];
int c1, c2;
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
if (a[i] > k) break;
c1++;
}
if (c1 == n) {
cout << n;
return 0;
}
for (int i = n; i >= 1; i--) {
if (a[i] > k) break;
c2++;
}
cout << c1 + c2;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vpdd = vector<pdd>;
using vvpdd = vector<vpdd>;
template <typename T>
void ckmin(T& a, const T& b) {
a = min(a, b);
}
template <typename T>
void ckmax(T& a, const T& b) {
a = max(a, b);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
namespace __input {
template <class T1, class T2>
void re(pair<T1, T2>& p);
template <class T>
void re(vector<T>& a);
template <class T, size_t SZ>
void re(array<T, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(double& x) {
string t;
re(t);
x = stod(t);
}
template <class Arg, class... Args>
void re(Arg& first, Args&... rest) {
re(first);
re(rest...);
}
template <class T1, class T2>
void re(pair<T1, T2>& p) {
re(p.first, p.second);
}
template <class T>
void re(vector<T>& a) {
for (int i = 0; i < (int((a).size())); i++) re(a[i]);
}
template <class T, size_t SZ>
void re(array<T, SZ>& a) {
for (int i = 0; i < (SZ); i++) re(a[i]);
}
} // namespace __input
using namespace __input;
namespace __output {
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T, size_t SZ>
void pr(const array<T, SZ>& x);
template <class T>
void pr(const vector<T>& x);
template <class T>
void pr(const set<T>& x);
template <class T1, class T2>
void pr(const map<T1, T2>& x);
template <class T>
void pr(const T& x) {
cout << x;
}
template <class Arg, class... Args>
void pr(const Arg& first, const Args&... rest) {
pr(first);
pr(rest...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
pr("{", x.first, ", ", x.second, "}");
}
template <class T, bool pretty = true>
void prContain(const T& x) {
if (pretty) pr("{");
bool fst = 1;
for (const auto& a : x) pr(!fst ? pretty ? ", " : " " : "", a), fst = 0;
if (pretty) pr("}");
}
template <class T>
void pc(const T& x) {
prContain<T, false>(x);
pr("\n");
}
template <class T, size_t SZ>
void pr(const array<T, SZ>& x) {
prContain(x);
}
template <class T>
void pr(const vector<T>& x) {
prContain(x);
}
template <class T>
void pr(const set<T>& x) {
prContain(x);
}
template <class T1, class T2>
void pr(const map<T1, T2>& x) {
prContain(x);
}
void ps() { pr("\n"); }
template <class Arg>
void ps(const Arg& first) {
pr(first);
ps();
}
template <class Arg, class... Args>
void ps(const Arg& first, const Args&... rest) {
pr(first, " ");
ps(rest...);
}
} // namespace __output
using namespace __output;
namespace __algorithm {
template <typename T>
void dedup(vector<T>& v) {
sort((v).begin(), (v).end());
v.erase(unique((v).begin(), (v).end()), v.end());
}
template <typename T>
typename vector<T>::iterator find(vector<T>& v, const T& x) {
auto it = lower_bound((v).begin(), (v).end(), x);
return it != v.end() && *it == x ? it : v.end();
}
template <typename T>
size_t index(vector<T>& v, const T& x) {
auto it = find(v, x);
assert(it != v.end() && *it == x);
return it - v.begin();
}
template <typename C, typename T>
vector<T> prefixes(const C& v, T zero) {
vector<T> res(int((v).size()) + 1, zero);
for (int i = 0; i < (int((v).size())); i++) res[i + 1] = res[i] + v[i];
return res;
}
template <typename C, typename T>
vector<T> suffixes(const C& v, T zero) {
vector<T> res(int((v).size()) + 1, zero);
for (int i = (int((v).size())) - 1; i >= 0; i--) res[i] = v[i] + res[i + 1];
return res;
}
} // namespace __algorithm
using namespace __algorithm;
struct monostate {
friend istream& operator>>(istream& is,
const __attribute__((unused)) monostate& ms) {
return is;
}
friend ostream& operator<<(ostream& os,
const __attribute__((unused)) monostate& ms) {
return os;
}
} ms;
template <typename W = monostate>
struct wedge {
int u, v, i;
W w;
wedge<W>(int _u = -1, int _v = -1, int _i = -1) : u(_u), v(_v), i(_i) {}
int operator[](int loc) const { return u ^ v ^ loc; }
friend void re(wedge& e) {
re(e.u, e.v, e.w);
--e.u, --e.v;
}
friend void pr(const wedge& e) { pr(e.u, "<-", e.w, "->", e.v); }
};
namespace __io {
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void setIO(string second = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.precision(15);
if (int((second).size())) {
setIn(second + ".in"), setOut(second + ".out");
}
}
} // namespace __io
using namespace __io;
template <typename F>
struct dinic {
static const F inf = numeric_limits<F>::max();
int V;
vvi adj;
vi dest;
vector<F> cap;
dinic(int V = 0) : V(V) { adj.resize(V); }
void __arc(int u, int v, F c) {
adj[u].push_back(dest.size());
dest.push_back(v);
cap.push_back(c);
}
void arc(int u, int v, F c) {
__arc(u, v, c);
__arc(v, u, F(0));
}
bool bfs(int second, int t, vi& level, vector<F>& flow) const {
level = vi(V, -1);
level[second] = 0;
for (queue<int> q({second}); !q.empty(); q.pop()) {
int u = q.front();
for (int e : adj[u]) {
if (level[dest[e]] == -1 && flow[e] < cap[e]) {
level[dest[e]] = level[u] + 1;
q.push(dest[e]);
}
}
}
return level[t] != -1;
}
F augment(int second, int t, vi& level, vector<F>& flow, vi& inx,
F cur) const {
if (second == t) return cur;
for (int e; inx[second] < adj[second].size(); inx[second]++) {
e = adj[second][inx[second]];
if (level[dest[e]] != level[second] + 1) continue;
if (flow[e] == cap[e]) continue;
F incr =
augment(dest[e], t, level, flow, inx, min(cur, cap[e] - flow[e]));
if (incr > F(0)) {
flow[e] += incr;
flow[e ^ 1] -= incr;
return incr;
}
}
return F(0);
}
tuple<F, vector<F>> max_flow(int second, int t) const {
assert(second != t);
F res(0);
vector<F> flow(cap.size());
for (vi level; bfs(second, t, level, flow);) {
for (vi inx(V, 0); F incr = augment(second, t, level, flow, inx, inf);)
res += incr;
}
return make_tuple(res, flow);
}
};
int main() {
setIO();
int N, M;
re(N, M);
vi allX, allY;
struct rect {
int x1, y1, x2, y2;
};
vector<rect> rc(M);
for (int i = 0; i < (M); i++) {
auto& r = rc[i];
re(r.x1, r.y1, r.x2, r.y2);
allX.push_back(r.x1);
allX.push_back(++r.x2);
allY.push_back(r.y1);
allY.push_back(++r.y2);
}
dedup(allX);
dedup(allY);
const int SRC = 0, XL = 1, YL = XL + int((allX).size()),
SNK = YL + int((allY).size());
dinic<ll> g(SNK + 1);
for (int i = 0; i < (int((allX).size()) - 1); i++) {
g.arc(SRC, XL + i, allX[i + 1] - allX[i]);
}
for (int i = 0; i < (int((allY).size()) - 1); i++) {
g.arc(YL + i, SNK, allY[i + 1] - allY[i]);
}
vector<vb> conn(int((allX).size()), vb(int((allY).size()), false));
for (int ri = 0; ri < (M); ri++) {
auto& r = rc[ri];
for (int i = 0; i < (int((allX).size()) - 1); i++) {
if (r.x1 <= allX[i] && r.x2 >= allX[i + 1]) {
for (int j = 0; j < (int((allY).size()) - 1); j++) {
if (r.y1 <= allY[j] && r.y2 >= allY[j + 1]) {
if (!conn[i][j]) {
conn[i][j] = true;
g.arc(XL + i, YL + j, LLONG_MAX / 8);
}
}
}
}
}
}
ps(get<0>(g.max_flow(SRC, SNK)));
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, ans = 0, sz[100005];
long long a[100005];
vector<pair<int, long long> > g[100005];
void dfs(int u, bool up, long long sum, long long minSum) {
bool era = up | (sum - minSum > a[u]);
sz[u] = 1;
for (int i = 0; i <= int(g[u].size()) - 1; i++) {
int v = g[u][i].first;
long long w = g[u][i].second;
long long newSum = sum + w;
dfs(v, era, newSum, min(minSum, newSum));
sz[u] += sz[v];
}
if (era && !up) ans += sz[u];
}
void setup() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int u;
long long w;
for (int i = 2; i <= n; i++) {
cin >> u >> w;
g[u].push_back(make_pair(i, w));
}
dfs(1, false, 0, 0);
cout << ans;
}
void xuly() {}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
setup();
xuly();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << endl;
err(++it, args...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
long long d, n;
cin >> d >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
long long cl = a[0] + 1, ans = 0;
for (long long i = 1; i < n; i++) {
ans += d - a[i - 1];
}
cout << ans << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = acos(-1);
const int knightDir[8][2] = {{-2, -1}, {-2, 1}, {-1, 2}, {1, 2},
{2, -1}, {2, 1}, {-1, -2}, {1, -2}};
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const long long MOD = 1000000000 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
t = 1;
while (t--) {
string a, b;
cin >> a >> b;
int aa[30] = {0}, bb[30] = {0};
int lena = a.length();
int lenb = b.length();
for (int i = 0; i < lena; i++) aa[a[i] - 'a']++;
for (int i = 0; i < lenb; i++) bb[b[i] - 'a']++;
bool d = true, au = false;
for (int i = 0; i < 30; i++) {
if (bb[i] > aa[i]) {
d = false;
break;
}
}
int i, j;
for (i = 0, j = 0; j < lena;) {
if (b[i] != a[j])
j++;
else
i++, j++;
}
if (i == lenb) au = true;
if (!d) {
cout << "need tree\n";
} else if (lena == lenb) {
cout << "array\n";
} else if (au)
cout << "automaton\n";
else
cout << "both\n";
}
}
| 3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.