solution
stringlengths 53
181k
| difficulty
int64 0
27
|
---|---|
#include <bits/stdc++.h>
using namespace std;
inline void PrintArrInt(int arr[], int s, int e) {
for (int i = (s); i <= (e); i++) printf("%d%c", arr[i], i == e ? '\n' : ' ');
}
const int maxn = 100000 + 5;
const int mod = 1e9 + 7;
int N, Q, mxsqn, arr[maxn], lpos[maxn * 2];
struct Node {
int ls, rs;
long long mulv;
Node() { mulv = 1; }
} ns[maxn * 40];
int ncnt, root[maxn];
long long _pow(long long a, long long b) {
long long t = a, ans = 1;
while (b) {
if (b & 1) ans = ans * t % mod;
t = t * t % mod;
b >>= 1;
}
return ans;
}
void update(int& now, int L, int R, int pos, int val) {
ns[++ncnt] = ns[now];
now = ncnt;
Node& n = ns[now];
n.mulv = 1ll * n.mulv * val % mod;
if (L == R) return;
int mid = (L + R) >> 1;
if (pos <= mid)
update(n.ls, L, mid, pos, val);
else
update(n.rs, mid + 1, R, pos, val);
}
int query(int now, int L, int R, int lft, int rht) {
if (L >= lft && R <= rht) return ns[now].mulv;
int mid = (L + R) >> 1;
if (rht <= mid)
return query(ns[now].ls, L, mid, lft, rht);
else if (lft > mid)
return query(ns[now].rs, mid + 1, R, lft, rht);
else
return 1ll * query(ns[now].ls, L, mid, lft, rht) *
query(ns[now].rs, mid + 1, R, lft, rht) % mod;
}
struct RMQ {
int arr[maxn];
char dp[maxn][18];
void rmq_init() {
for (int i = (1); i <= (N); i++) dp[i][0] = arr[i];
for (int p = 1; (1 << p) <= N; p++) {
for (int i = 1; i + (1 << p) - 1 <= N; i++) {
dp[i][p] = max(dp[i][p - 1], dp[i + (1 << (p - 1))][p - 1]);
}
}
}
int rmq_query(int L, int R) {
int logl = log2(R - L + 1);
return max(dp[L][logl], dp[R - (1 << logl) + 1][logl]);
}
} rmqs[90];
int flag[450], p[450], cnt;
void init() {
for (int i = (2); i <= (449); i++) {
if (!flag[i]) p[++cnt] = i;
for (int j = 1; j <= cnt && i * p[j] < 450; j++) {
flag[i * p[j]] = true;
if (i % p[j] == 0) break;
}
}
}
inline void reduce(int n) {
for (int i = 1; i <= cnt && p[i] <= mxsqn; i++) {
while (arr[n] % p[i] == 0) rmqs[i].arr[n]++, arr[n] /= p[i];
}
}
int main() {
init();
int maxv = 0;
scanf("%d", &(N));
for (int i = (1); i <= (N); i++)
scanf("%d", &(arr[i])), maxv = max(maxv, arr[i]);
mxsqn = sqrt(maxv) + 1;
for (int i = (1); i <= (N); i++) reduce(i);
for (int i = (1); i <= (89); i++) rmqs[i].rmq_init();
for (int i = (1); i <= (N); i++) {
root[i] = root[i - 1];
if (arr[i] == 1) continue;
if (lpos[arr[i]])
update(root[i], 1, N, lpos[arr[i]], _pow(arr[i], mod - 2));
update(root[i], 1, N, i, arr[i]);
lpos[arr[i]] = i;
}
scanf("%d", &(Q));
int x, y;
long long las = 0;
for (int i = (1); i <= (Q); i++) {
scanf("%d%d", &(x), &(y));
int L = (las + x) % N + 1, R = (las + y) % N + 1;
if (L > R) swap(L, R);
long long t1 = query(root[R], 1, N, L, R);
long long t2 = 1;
for (int i = (1); i <= (89); i++) {
t2 = t2 * _pow(p[i], rmqs[i].rmq_query(L, R)) % mod;
}
printf("%lld\n", (las = t1 * t2 % mod));
}
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int n, val[maxn], root;
int tot, head[maxn];
struct Edge {
int to, next;
} edges[maxn];
void initedge() {
tot = 0;
for (int i = 0; i <= n; i++) {
head[i] = -1;
}
}
void addedge(int from, int to) {
edges[tot].to = to;
edges[tot].next = head[from];
head[from] = tot++;
}
int s[maxn], e[maxn], dfsclock, sta[maxn], tail;
void dfs(int u) {
s[u] = ++dfsclock;
sta[++tail] = u;
for (int i = head[u]; ~i; i = edges[i].next) {
int v = edges[i].to;
dfs(v);
}
e[u] = dfsclock;
}
int dp[maxn];
void treedp(int u) {
dp[u] = val[u];
for (int i = head[u]; ~i; i = edges[i].next) {
int v = edges[i].to;
treedp(v);
dp[u] += dp[v];
}
}
int main() {
scanf("%d", &n);
initedge();
for (int i = 1; i <= n; i++) {
int a;
scanf("%d%d", &a, &val[i]);
if (a) {
addedge(a, i);
} else {
root = i;
}
}
treedp(root);
dfsclock = tail = 0;
dfs(root);
int sumw = dp[root];
if (sumw % 3 != 0) {
printf("-1\n");
} else {
int w = sumw / 3;
bool ok = false;
int point;
int l = 0, r = 0;
for (int i = 1; i <= n; i++) {
if (i == root) continue;
if (dp[i] == w) {
ok = true;
if (l == 0 && r == 0) {
l = s[i], r = e[i];
point = i;
} else {
if (l <= s[i] && e[i] <= r) {
l = s[i];
r = e[i];
point = i;
}
}
}
}
if (ok) {
ok = false;
int point2;
for (int i = 1; i <= n; i++) {
if (i == root) continue;
if (dp[i] == w) {
int ll = s[i], rr = e[i];
if (ll > r || rr < l) {
ok = true;
point2 = i;
break;
}
}
}
if (ok) {
printf("%d %d\n", point, point2);
} else {
ok = false;
for (int i = 1; i <= n; i++) {
if (i == root || i == point) continue;
if (dp[i] == 2 * w) {
int ll = s[i], rr = e[i];
if (ll <= l && r <= rr) {
ok = true;
point2 = i;
break;
}
}
}
if (ok) {
printf("%d %d\n", point, point2);
} else {
printf("-1\n");
}
}
} else {
printf("-1\n");
}
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 130;
int n, maxx, a[Maxn], you, she;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
she += a[i];
maxx = max(a[i], maxx);
}
for (int i = maxx; i < 250; i++) {
you = i * n - she;
if (she < you) {
cout << i;
return 0;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#pragma warning(disable : 4996)
const int INF = 0x7fffffff;
const int MAX_N = 1e5 + 5;
int a[MAX_N], q[MAX_N], ask[MAX_N];
const long long mod = 1e9 + 7;
const int K = 20;
long long _2[MAX_N];
struct datastruct {
int a[K], sz;
bool insert(int x) {
for (int i = K - 1; i >= 0; i--)
if (x >> i & 1) {
if (!a[i]) {
a[i] = x;
return 1;
}
x ^= a[i];
}
sz++;
return 0;
}
int query_mx() {
int ans = 0;
for (int i = K - 1; i >= 0; i--)
if ((ans ^ a[i]) > ans) ans ^= a[i];
return ans;
}
bool find(int x) {
for (int i = K - 1; i >= 0; i--)
if (x >> i & 1) x ^= a[i];
return x ? 0 : 1;
}
} T[MAX_N];
int N, Q;
void solve() {
for (int i = 1; i <= N; i++) {
T[i] = T[i - 1];
T[i].insert(a[i]);
}
for (int i = 0; i < Q; i++) {
if (!T[q[i]].find(ask[i]))
cout << 0 << endl;
else
cout << _2[T[q[i]].sz] << endl;
}
}
int main() {
ios::sync_with_stdio(false);
_2[0] = 1;
cin >> N >> Q;
for (int i = 1; i <= N; i++) {
_2[i] = _2[i - 1] * 2 % mod;
cin >> a[i];
}
for (int i = 0; i < Q; i++) {
cin >> q[i] >> ask[i];
}
solve();
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, i, j;
cin >> k;
if (k % 2 == 0) {
cout << "NO" << endl;
return 0;
} else if (k == 1) {
cout << "YES" << endl << "2 1" << endl << "1 2" << endl;
} else {
cout << "YES" << endl;
int nodes = 2 * k + 4, edges = (k + 2) * k;
cout << nodes << " " << edges << endl;
for (i = 2; i <= k; i++) {
cout << "1 " << i << endl;
}
cout << "1 " << k + 3 << endl;
for (i = 2; i <= k; i++) {
if (i % 2 == 0) {
for (j = i + 2; j <= k; j++) {
cout << i << " " << j << endl;
}
} else {
for (j = i + 1; j <= k; j++) {
cout << i << " " << j << endl;
}
}
}
for (i = 2; i <= k; i++) {
cout << i << " " << k + 1 << endl;
cout << i << " " << k + 2 << endl;
}
cout << k + 1 << " " << k + 2 << endl;
for (i = 2; i <= k; i++) {
cout << 1 + k + 2 << " " << i + k + 2 << endl;
}
for (i = 2; i <= k; i++) {
if (i % 2 == 0) {
for (j = i + 2; j <= k; j++) {
cout << i + k + 2 << " " << j + k + 2 << endl;
}
} else {
for (j = i + 1; j <= k; j++) {
cout << i + k + 2 << " " << j + k + 2 << endl;
}
}
}
for (i = 2; i <= k; i++) {
cout << i + k + 2 << " " << k + 1 + k + 2 << endl;
cout << i + k + 2 << " " << k + 2 + k + 2 << endl;
}
cout << k + 1 + k + 2 << " " << k + 2 + k + 2 << endl;
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
struct Edge {
int to, next;
} edge[maxn * 2];
int cnt, head[maxn];
int pre[maxn][50], fgcd[100], f[maxn], tot, res[maxn];
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
void init() {
tot = 0;
cnt = 0;
memset(res, -0x3f3f3f3f, sizeof res);
memset(head, -1, sizeof head);
}
void add(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u, int fa, int tgcd) {
int save;
save = cnt;
for (int i = 0; i < cnt; i++) {
pre[u][i] = fgcd[i];
fgcd[i] = gcd(fgcd[i], f[u]);
res[u] = max(res[u], fgcd[i]);
}
fgcd[cnt++] = tgcd;
res[u] = max(res[u], tgcd);
sort(fgcd, fgcd + cnt);
cnt = unique(fgcd, fgcd + cnt) - fgcd;
tgcd = gcd(tgcd, f[u]);
res[u] = max(res[u], tgcd);
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v == fa) continue;
dfs(v, u, tgcd);
}
cnt = save;
for (int i = 0; i < cnt; i++) fgcd[i] = pre[u][i];
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &f[i]);
init();
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(1, -1, 0);
for (int i = 1; i <= n; i++) printf("%d%c", res[i], i == n ? '\n' : ' ');
return 0;
}
| 12 |
#include <bits/stdc++.h>
const int K = 1111;
int cine[K][K], n, k;
int main() {
scanf("%d%d", &n, &k);
int mid = k / 2;
while (n--) {
int m;
scanf("%d", &m);
int minS = 1 << 30, x, l, r;
for (int i = 0; i < k; ++i) {
int s = 0, cnt = 0;
for (int j = 0; j < k; ++j)
if (cine[i][j] == 0) {
s += abs(i - mid) + abs(j - mid);
if (++cnt > m) --cnt, s -= abs(i - mid) + abs(j - m - mid);
if (cnt == m && s < minS) minS = s, x = i, r = j, l = r - m + 1;
} else
s = 0, cnt = 0;
}
if (minS == (1 << 30))
printf("-1\n");
else {
printf("%d %d %d\n", x + 1, l + 1, r + 1);
for (int i = l; i <= r; ++i) cine[x][i] = 1;
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const unordered_set<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class S, class T>
ostream& operator<<(ostream& os, const unordered_map<S, T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const multiset<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class S, class T>
ostream& operator<<(ostream& os, const map<S, T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
void dbs(string str, T t) {
cerr << str << " : " << t << "\n";
}
template <class T, class... S>
void dbs(string str, T t, S... s) {
long long idx = str.find(',');
cerr << str.substr(0, idx) << " : " << t << ",";
dbs(str.substr(idx + 1), s...);
}
template <class T>
void prc(T a, T b) {
cerr << "[";
for (T i = a; i != b; ++i) {
if (i != a) cerr << ", ";
cerr << *i;
}
cerr << "]\n";
}
long long power(long long x, long long y) {
long long res = 1;
while (y) {
if (y & 1) res = (res * x) % 1000000007;
y = y / 2, x = (x * x) % 1000000007;
}
return res % 1000000007;
}
long long dp[5001][5001];
long long parent[2][5001 * 2];
long long arr[500001];
long long ans[500001];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
long long i, j;
for (i = 1; i < n; i++) cin >> arr[i];
for (i = 1; i < n; i++) {
dp[i][1] = arr[i];
for (j = 2; j < n; j++) {
dp[i][j] = dp[i][j - 1] * arr[i + j - 1] % 1000000007;
}
}
long long ff = 1;
long long bakwass = 1;
for (i = 2; i <= n; i++) {
ff *= arr[i - 1];
ff %= 1000000007;
long long temp[5001 * 2] = {0};
for (j = 1; j < n; j++) temp[j] = dp[i - 1][j];
long long f = 1;
temp[1] -= 1;
temp[1] += 1000000007;
temp[1] %= 1000000007;
for (j = 2; j < n; j++) {
f = f * arr[i + j - 2] % 1000000007;
temp[j] -= f;
temp[j] += 1000000007;
temp[j] %= 1000000007;
}
parent[bakwass][1] = 1;
for (j = 2; j < 2 * n; j++) {
parent[bakwass][j] = parent[1 - bakwass][j - 1] + temp[j - 1];
parent[bakwass][j] %= 1000000007;
}
for (j = 1; j < 2 * n; j++) {
ans[j] = ans[j] + parent[bakwass][j] * ff % 1000000007;
ans[j] %= 1000000007;
}
bakwass++;
bakwass %= 2;
}
long long f = 1;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
dp[i][j] = dp[i][j] * f % 1000000007;
}
f *= arr[i];
f %= 1000000007;
}
for (i = 1; i <= n; i++) {
for (j = 1; j < n; j++) ans[j] += dp[i][j], ans[j] %= 1000000007;
}
for (j = 1; j < 2 * n - 1; j++)
cout << ans[j] * power(2, 1000000007 - 2) % 1000000007 << " ";
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int _ = 3e5 + 7;
int s[_], t[_], num[_], d[_], N, M;
vector<int> nxt[_];
void topo() {
int lft = N;
queue<int> q;
for (int i = 1; i <= N; ++i)
if (!d[i]) q.push(i);
while (!q.empty()) {
int t = q.front();
q.pop();
--lft;
if (q.size() >= 2)
num[t] = -1e9;
else if (q.size() == 1) {
int s = q.front();
num[t] += lft - 1;
for (auto p : nxt[s])
if (d[p] == 1) num[t] = -1e9;
} else
num[t] += lft;
for (auto x : nxt[t])
if (!--d[x]) q.push(x);
}
}
int main() {
scanf("%d %d", &N, &M);
for (int i = 1; i <= M; ++i) {
scanf("%d %d", &s[i], &t[i]);
nxt[s[i]].push_back(t[i]);
++d[t[i]];
}
topo();
for (int i = 1; i <= N; ++i) nxt[i].clear();
for (int i = 1; i <= M; ++i) {
nxt[t[i]].push_back(s[i]);
++d[s[i]];
}
topo();
int ans = 0;
for (int i = 1; i <= N; ++i) ans += num[i] >= N - 2;
printf("%d\n", ans);
return 0;
}
| 21 |
#include <bits/stdc++.h>
#define oo 1000000000000
#define ll long long
#define pii pair<long long, long long>
#define ull unsigned long long
#define st first
#define nd second
#define file "test"
using namespace std;
const long long N = 2e5 + 5;
ll pow2[N], pow3[N];
ll num(ll x)
{
ll cnt = 0;
while (x)
cnt ++, x/=10;
return cnt;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen(file".inp","r",stdin);
freopen(file".out","w",stdout);
#endif
ll t;
cin >> t;
while (t--)
{
ll a, b, c, x = 1;
cin >> a >> b >> c;
for (int i=1;i<c;i++)
x*=10;
ll tmp = x;
while (num(tmp) < a)
tmp*=2;
cout << tmp << ' '; tmp = x;
while (num(tmp) < b)
tmp*=3;
cout << tmp << '\n';
}
return 0;
} | 3 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> bad;
set<pair<int, pair<int, int> > > segm;
int points[256];
vector<int> ans;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int l, r;
cin >> l >> r;
segm.insert({i, {l, r}});
for (int j = l; j <= r; j++) points[j]++;
}
for (int i = 1; i <= 200; i++) {
if (points[i] > k) bad.push_back(i);
}
for (auto p : bad) {
while (points[p] > k) {
pair<int, pair<int, int> > best = {-1, {-1, -1}};
for (auto x : segm)
if (x.second.first <= p && x.second.second >= p &&
x.second.second >= best.second.second)
best = x;
segm.erase(best);
for (int i = best.second.first; i <= best.second.second; i++) points[i]--;
ans.push_back(best.first);
}
}
cout << ans.size() << '\n';
for (auto x : ans) cout << x << ' ';
}
| 10 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:5000000000")
const long long m = 1000000007;
const int INF = 1000000000;
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
long long ans1, ans2, ans3, ans4;
ans1 = 1;
ans2 = ans1 + (n * (n - 1)) / 2;
ans3 = 2 * ((n * (n - 1) * (n - 2)) / (3 * 2)) + ans2;
ans4 = 9 * ((n * (n - 1) * (n - 2) * (n - 3)) / (4 * 3 * 2 * 1)) + ans3;
if (k == 1) cout << ans1;
if (k == 2) cout << ans2;
if (k == 3) cout << ans3;
if (k == 4) cout << ans4;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, T;
scanf("%d %d", &n, &T);
priority_queue<int> q;
int ans = 0;
for (int i = 1; i <= min(T - 1, n); i++) {
while (!q.empty() && q.top() >= T - i) q.pop();
int x;
scanf("%d", &x);
if (x < T) q.push(x - i);
ans = max(ans, (int)q.size());
}
printf("%d\n", ans);
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return a.first > b.first;
}
bool cmp1(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return a.second < b.second;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, k, ans = 0, i, ara[10000];
vector<pair<long long, long long>> v, v1;
cin >> n >> k;
for (i = 0; i < n; i++) {
cin >> ara[i];
v.push_back({ara[i], i + 1});
}
sort(v.begin(), v.end(), cmp);
for (i = 0; i < k; i++) {
ans += v[i].first;
v1.push_back({v[i].first, v[i].second});
}
cout << ans << endl;
sort(v1.begin(), v1.end(), cmp1);
long long pos = 0;
for (i = 0; i < k - 1; i++) {
cout << v1[i].second - pos << " ";
pos = v1[i].second;
}
cout << n - pos << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000007;
int v[N], f[N];
vector<int> pos[N];
long long acc[N];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> v[i];
pos[v[i]].push_back(i);
}
for (int i = 0; i < N; ++i) {
reverse(pos[i].begin(), pos[i].end());
}
set<int> s;
long long cnt = 0;
for (int i = 0; i < n; ++i) {
s.insert(v[i]);
f[i] = s.size();
cnt += f[i];
}
long long ans = 2ll * cnt;
long long cur = 0;
for (int l = 0; l < n; ++l) {
cur += acc[l];
int x = v[l];
pos[x].pop_back();
int j = n;
if (pos[x].size()) j = pos[x].back();
cnt -= f[l] + cur;
acc[l + 1]--;
acc[j]++;
cnt -= (j - l - 1);
ans += 2ll * cnt;
}
ans -= n;
cout << fixed << setprecision(10)
<< (ans / (1.0 * ((long long)n * (long long)n))) << endl;
}
| 10 |
#include <bits/stdc++.h>
int main() {
int n, num;
int flag = 0, sum = 0;
int tot;
char str[20], kong;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &num);
kong = getchar();
scanf("%s", str);
if (str[0] == 'S') {
if (sum == 20000) flag = 1;
tot = 20000 - sum;
if (num <= tot) {
sum = sum + num;
} else {
flag = 1;
}
} else if (str[0] == 'E') {
if (sum == 0 || sum == 20000) flag = 1;
} else if (str[0] == 'N') {
if (sum == 0) flag = 1;
if (num <= sum) {
sum -= num;
} else {
flag = 1;
}
} else if (str[0] == 'W') {
if (sum == 0 || sum == 20000) flag = 1;
}
}
if (sum != 0) flag = 1;
if (flag == 1)
printf("NO\n");
else
printf("YES\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
short n, m;
int nn;
char a[3][23];
bool b[405][405], seen[405];
void dfs(int i) {
if (seen[i]) return;
seen[i] = 1;
for (int j = 1; j <= nn; j++)
if (b[i][j]) dfs(j);
}
int main() {
cin >> n >> m;
nn = n * m;
for (short i = 0; i < 2; i++) scanf("%s", a[i]);
for (int i = 1, hold; i <= n; i++) {
for (int j = 1; j <= m; j++) {
hold = (i - 1) * m + j;
if (a[0][i - 1] == '>') {
for (short k = j + 1; k <= m; k++) b[hold][(i - 1) * m + k] = 1;
} else
for (short k = j - 1; k > 0; k--) b[hold][(i - 1) * m + k] = 1;
if (a[1][j - 1] == 'v')
for (short k = i + 1; k <= n; k++) b[hold][(k - 1) * m + j] = 1;
else
for (short k = i - 1; k > 0; k--) b[hold][(k - 1) * m + j] = 1;
}
}
dfs(1);
for (int i = 1; i <= nn; i++)
if (!seen[i]) return printf("NO") - 2;
for (int i = 2; i <= nn; i++) {
for (int j = 1; j <= nn; j++) seen[j] = 0;
dfs(i);
if (!seen[1]) return printf("NO") - 2;
}
printf("YES");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
int arr[N];
int main() {
int n, m, d;
cin >> n >> m >> d;
set<pair<int, int> > st;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
st.insert(make_pair(arr[i], i));
}
int day = 0;
while (!st.empty()) {
day++;
int a = st.begin()->first;
int b = st.begin()->second;
st.erase(st.begin());
arr[b] = day;
auto it = st.upper_bound(make_pair(a + d, 1e9));
while (it != st.end()) {
a = it->first;
b = it->second;
st.erase(it);
arr[b] = day;
it = st.upper_bound(make_pair(a + d, 1e9));
}
}
cout << day << endl;
for (int i = 0; i < n; ++i) cout << arr[i] << " ";
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y, val, ans;
bool operator<(const node &a) const { return val < a.val; }
} p[1000010];
int r[1000010], l[1000010], rr[1000010], ll[1000010];
int n, m;
int fa[1000010], A[1000010];
int cmp(node a, node b) { return a.x * m + a.y < b.x * m + b.y; }
int find(int x) {
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
int main(int argc, char const *argv[]) {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
p[i * m + j].x = i;
p[i * m + j].y = j;
scanf("%d", &p[i * m + j].val);
}
sort(p, p + n * m);
p[n * m].val = -1;
for (int i = 0; i < n * m; i++) fa[i] = i;
memset(r, 0, sizeof(r));
memset(l, 0, sizeof(l));
memset(A, 0, sizeof(A));
memset(rr, 0, sizeof(rr));
memset(ll, 0, sizeof(ll));
int last = 0;
for (int i = 1; i <= n * m; i++)
if (p[i].val != p[i - 1].val) {
for (int j = last; j < i; j++) {
int x = p[j].x, y = p[j].y;
if (rr[x] == 0)
rr[x] = j;
else
fa[find(j)] = find(rr[x]);
if (ll[y] == 0)
ll[y] = j;
else
fa[find(j)] = find(ll[y]);
}
for (int j = last; j < i; j++)
A[find(j)] = max(A[find(j)], max(r[p[j].x], l[p[j].y]) + 1);
for (int j = last; j < i; j++) {
int x = p[j].x, y = p[j].y;
p[j].ans = A[find(j)];
r[x] = l[y] = p[j].ans;
rr[x] = 0;
ll[y] = 0;
}
last = i;
}
sort(p, p + n * m, cmp);
for (int i = 0; i < n; i++) {
int tmp = i * m;
for (int j = 0; j < m; j++) printf("%d ", p[tmp + j].ans);
printf("\n");
}
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
long long ii = 1;
void solve() {
long long n;
long long kk;
cin >> n >> kk;
vector<long long> alice;
vector<long long> bob;
vector<long long> all;
long long al = 0;
long long bo = 0;
for (long long i = 0; i < n; i++) {
long long t;
long long a;
long long b;
cin >> t >> a >> b;
if (a == 1 && b == 1) {
all.push_back(t);
al++;
bo++;
} else if (a == 1) {
al++;
alice.push_back(t);
} else if (b == 1) {
bo++;
bob.push_back(t);
}
}
if (al >= kk && bo >= kk) {
sort(all.begin(), all.end());
sort(alice.begin(), alice.end());
sort(bob.begin(), bob.end());
long long j = 0;
long long k = 0;
long long l = 0;
long long cn = 0;
long long ans = 0;
while (1) {
if (cn == kk) break;
if (j < all.size() && k < alice.size() && l < bob.size()) {
if (all[j] <= alice[k] + bob[l])
ans += all[j++];
else
ans += alice[k++] + bob[l++];
} else if (j < all.size())
ans += all[j++];
else
ans += alice[k++] + bob[l++];
cn++;
}
cout << ans << '\n';
} else
cout << "-1" << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long TestCase;
TestCase = 1;
while (TestCase--) solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long MIN = numeric_limits<long long>::min();
class WindowHeap {
public:
WindowHeap() {}
long long top() {
if (_i >= _h.size()) return MIN;
return _h[_i].first;
}
void erase(int idx) {
if (!_h.empty() && _h[_i].second == idx) ++_i;
}
void insert(long long val, int idx) {
if (val == MIN) return;
while (!_h.empty() && val > _h.rbegin()->first) _h.pop_back();
if (_h.empty() || val < _h.rbegin()->first)
_h.emplace_back(val, idx);
else
_h.rbegin()->second = idx;
if (_i >= _h.size()) _i = _h.size() - 1;
}
private:
vector<pair<long long, int>> _h;
size_t _i;
};
int main() {
int n, k, x;
cin >> n >> k >> x;
vector<int> a(n);
for (int& ai : a) cin >> ai;
vector<long long> dp(n + 1, MIN);
dp[0] = 0;
for (int xi = 0; xi < x; ++xi) {
WindowHeap wh;
for (int i = n - 1; i > max(n - k, -1); --i) {
wh.insert(dp[i], i);
}
for (int i = n; i > xi; --i) {
if (i - k >= 0) wh.insert(dp[i - k], i - k);
long long p = wh.top();
if (p > MIN) dp[i] = p + a[i - 1];
wh.erase(i - 1);
}
dp[xi] = MIN;
}
long long best = -1;
for (int i = n; i > n - k; --i) best = max(best, dp[i]);
cout << best << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const double mod = 1e9 + 7;
long long msum(long long a, long long b, long long m) {
return (a % m + b % m) % m;
}
long long msub(long long a, long long b, long long m) {
return (a % m - b % m) % m;
}
long long mpro(long long a, long long b, long long m) {
return ((a % m) * (b % m)) % m;
}
long long fxp(long long a, long long b, long long m) {
if (b == 0) return 1;
if (b % 2 == 0) return fxp(a * a, b / 2, m);
return fxp(a, b - 1, m) * a;
}
void swap(long long &a, long long &b) {
long long t = a;
a = b;
b = t;
}
int main() {
string a, b;
cin >> a >> b;
long long flag1 = 0, flag2 = 0;
if (a.size() != b.size()) {
cout << "NO";
return 0;
}
long long i = 0, lt;
for (long long i = 0; i < b.size(); i++) {
if (b[i] == '1') flag1 = 1;
}
for (long long i = 0; i < a.size(); i++) {
if (a[i] == '1') flag2 = 1;
}
if ((flag1 == 0 && flag2 == 0) || (flag1 == 1 && flag2 == 1)) {
cout << "YES";
} else
cout << "NO";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 2e5 + 5;
const long long MOD = 1e9 + 7;
long long testnum = 0;
long long testflag = 0;
void solve() {
long long a, b, c, d;
cin >> a >> b >> c >> d;
bool aa = 0, bb = 0, cc = 0, dd = 0;
if (b == 0 && c == 0) {
if (a == 0 && d > 0) dd = 1;
if (a > 0) (a % 2 == 1 ? aa = 1 : dd = 1);
cout << (aa ? "Ya" : "Tidak") << ' ';
cout << (bb ? "Ya" : "Tidak") << ' ';
cout << (cc ? "Ya" : "Tidak") << ' ';
cout << (dd ? "Ya" : "Tidak") << ' ';
cout << '\n';
return;
}
if ((a + b) % 2 == 0) {
if (a == 0 && d == 0) {
cc = 1;
cout << (aa ? "Ya" : "Tidak") << ' ';
cout << (bb ? "Ya" : "Tidak") << ' ';
cout << (cc ? "Ya" : "Tidak") << ' ';
cout << (dd ? "Ya" : "Tidak") << ' ';
cout << '\n';
return;
} else {
cc = 1;
dd = 1;
cout << (aa ? "Ya" : "Tidak") << ' ';
cout << (bb ? "Ya" : "Tidak") << ' ';
cout << (cc ? "Ya" : "Tidak") << ' ';
cout << (dd ? "Ya" : "Tidak") << ' ';
cout << '\n';
return;
}
} else {
if (a == 0 && d == 0) {
bb = 1;
cout << (aa ? "Ya" : "Tidak") << ' ';
cout << (bb ? "Ya" : "Tidak") << ' ';
cout << (cc ? "Ya" : "Tidak") << ' ';
cout << (dd ? "Ya" : "Tidak") << ' ';
cout << '\n';
return;
} else {
aa = 1;
bb = 1;
cout << (aa ? "Ya" : "Tidak") << ' ';
cout << (bb ? "Ya" : "Tidak") << ' ';
cout << (cc ? "Ya" : "Tidak") << ' ';
cout << (dd ? "Ya" : "Tidak") << ' ';
cout << '\n';
return;
}
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t = 1;
cin >> t;
while (t--) {
testnum++;
solve();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e2 + 4;
const int oo = 1e9 + 4;
long long n, m;
void solve() {
cin >> n >> m;
cout << (m + n - 1) / n;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
if (a == 0) return b;
return gcd(b % a, a);
}
vector<long long> b_v;
void bin(unsigned long long n) {
if (n > 1) bin(n >> 1);
b_v.emplace_back(n & 1);
}
long long isPowerof2(long long x) { return (x && !(x & x - 1)); }
double a, b, c, p1, q1, p2, q2;
double dist(double xx1, double yy1, double xx2, double yy2) {
double tmp = (xx1 - xx2) * (xx1 - xx2) + (yy1 - yy2) * (yy1 - yy2);
return (double)(sqrt(tmp));
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ostream_iterator<long long> output(cout, " ");
cin >> a >> b >> c;
cin >> p1 >> q1 >> p2 >> q2;
double ans = abs(p1 - p2) + abs(q1 - q2);
if (a && b) {
pair<double, double> pp1, pp2, qq1, qq2;
pp1 = make_pair(p1, ((-1.0) * (c + p1 * a) / b));
pp2 = make_pair(((-1.0) * (c + b * q1) / a), q1);
qq1 = make_pair(p2, ((-1.0) * (c + p2 * a) / b));
qq2 = make_pair(((-1.0) * (c + b * q2) / a), q2);
ans = min(ans, dist(pp1.first, pp1.second, qq1.first, qq1.second) +
dist(p1, q1, pp1.first, pp1.second) +
dist(p2, q2, qq1.first, qq1.second));
ans = min(ans, dist(pp1.first, pp1.second, qq2.first, qq2.second) +
dist(p1, q1, pp1.first, pp1.second) +
dist(p2, q2, qq2.first, qq2.second));
ans = min(ans, dist(pp2.first, pp2.second, qq1.first, qq1.second) +
dist(p1, q1, pp2.first, pp2.second) +
dist(p2, q2, qq1.first, qq1.second));
ans = min(ans, dist(pp2.first, pp2.second, qq2.first, qq2.second) +
dist(p1, q1, pp2.first, pp2.second) +
dist(p2, q2, qq2.first, qq2.second));
}
cout << fixed << setprecision(15) << ans;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
long long n, k, a, b;
cin >> n >> k;
vector<set<long long> > g(n + 1), l(n + 1);
for (int i = 1; i <= n - 1; i++) {
cin >> a >> b;
g[a].insert(b);
g[b].insert(a);
}
for (int i = 1; i <= n; i++) {
for (auto x : g[i]) {
if (g[x].size() == 1) l[i].insert(x);
}
}
auto cmp = [&](long long a, long long b) {
if (l[a].size() == l[b].size()) return a > b;
return l[a].size() > l[b].size();
};
set<long long, decltype(cmp)> st(cmp);
for (int i = 1; i <= n; i++) st.insert(i);
long long ans = 0;
long long hh = 1;
while (st.size()) {
long long pp = *st.begin();
st.erase(pp);
if (l[pp].size() < k) break;
for (int i = 1; i <= k; i++) {
long long tm = *l[pp].begin();
g[tm].erase(pp);
g[pp].erase(tm);
l[pp].erase(tm);
l[tm].erase(pp);
}
if (g[pp].size() == 1)
st.erase(*g[pp].begin()), l[*g[pp].begin()].insert(pp),
st.insert(*g[pp].begin());
st.insert(pp);
ans++;
}
cout << ans << "\n";
}
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
char a[200005], b[200005];
int chk(char* a1, char* b1, int l) {
bool flag = 0;
for (int i = 0; i < l; i++) {
if (a1[i] != b1[i]) {
flag = 1;
break;
}
}
if (flag == 0) return 1;
if (l % 2 == 1) return 0;
return (chk(a1, b1, l / 2) && chk(a1 + l / 2, b1 + l / 2, l / 2)) ||
(chk(a1, b1 + l / 2, l / 2) && chk(a1 + l / 2, b1, l / 2));
}
int main() {
while (scanf("%s%s", a, b) != EOF) {
if (chk(a, b, strlen(b)))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MODV = 1e9 + 7;
int in_hand[200001];
int pile[200000];
vector<int> tmp;
int straight(int n) {
int now = pile[n - 1] + 1;
for (int i = n - 2; i >= n - now + 1; i--) {
if (pile[i] + 1 != pile[i + 1]) now = 1;
}
for (int i = 0; i < n; i++) {
if (now > n) break;
if (in_hand[now])
now++;
else
return 0;
in_hand[pile[i]] = 1;
}
return 1;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
in_hand[x] = 1;
}
for (int i = 0; i < n; i++) scanf("%d", &pile[i]);
if (straight(n))
printf("%d\n", n - pile[n - 1]);
else {
int ans = 0;
for (int i = 0; i < n; i++)
if (pile[i]) ans = max(ans, i + 1 + n - pile[i] + 1);
printf("%d\n", ans);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python!
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
// pairs
#define mp make_pair
#define f first
#define s second
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
#define rtn return
#define lb lower_bound
#define ub upper_bound
tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }
// loops
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
// bitwise ops
// also see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int bits(int x) { // assert(x >= 0); // make C++11 compatible until USACO updates ...
return x == 0 ? 0 : 31-__builtin_clz(x); } // floor(log2(x))
constexpr int p2(int x) { return 1<<x; }
constexpr int msk2(int x) { return p2(x)-1; }
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
tcT> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; } // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0; }
tcTU> T fstTrue(T lo, T hi, U f) {
hi ++; assert(lo <= hi); // assuming f is increasing
while (lo < hi) { // find first index such that f is true
T mid = lo+(hi-lo)/2;
f(mid) ? hi = mid : lo = mid+1;
}
return lo;
}
tcTU> T lstTrue(T lo, T hi, U f) {
lo --; assert(lo <= hi); // assuming f is decreasing
while (lo < hi) { // find first index such that f is true
T mid = lo+(hi-lo+1)/2;
f(mid) ? lo = mid : hi = mid-1;
}
return lo;
}
tcT> void remDup(vector<T>& v) { // sort and remove duplicates
sort(all(v)); v.erase(unique(all(v)),end(v)); }
tcTU> void erase(T& t, const U& u) { // don't erase
auto it = t.find(u); assert(it != end(t));
t.erase(it); } // element that doesn't exist from (multi)set
// INPUT
#define tcTUU tcT, class ...U
tcT> void re(complex<T>& c);
tcTU> void re(pair<T,U>& p);
tcT> void re(V<T>& v);
tcT, size_t SZ> void re(AR<T,SZ>& a);
tcT> void re(T& x) { cin >> x; }
void re(double& d) { str t; re(t); d = stod(t); }
void re(long double& d) { str t; re(t); d = stold(t); }
tcTUU> void re(T& t, U&... u) { re(t); re(u...); }
tcT> void re(complex<T>& c) { T a,b; re(a,b); c = {a,b}; }
tcTU> void re(pair<T,U>& p) { re(p.f,p.s); }
tcT> void re(V<T>& x) { each(a,x) re(a); }
tcT, size_t SZ> void re(AR<T,SZ>& x) { each(a,x) re(a); }
tcT> void rv(int n, V<T>& x) { x.rsz(n); re(x); }
// TO_STRING
#define ts to_string
str ts(char c) { return str(1,c); }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
str ts(bool b) {
// #ifdef LOCAL
// return b ? "true" : "false";
// #else
return ts((int)b);
// #endif
}
tcT> str ts(complex<T> c) {
stringstream ss; ss << c; return ss.str(); }
str ts(V<bool> v) {
str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]);
res += "}"; return res; }
template<size_t SZ> str ts(bitset<SZ> b) {
str res = ""; F0R(i,SZ) res += char('0'+b[i]);
return res; }
tcTU> str ts(pair<T,U> p);
tcT> str ts(T v) { // containers with begin(), end()
#ifdef LOCAL
bool fst = 1; str res = "{";
for (const auto& x: v) {
if (!fst) res += ", ";
fst = 0; res += ts(x);
}
res += "}"; return res;
#else
bool fst = 1; str res = "";
for (const auto& x: v) {
if (!fst) res += " ";
fst = 0; res += ts(x);
}
return res;
#endif
}
tcTU> str ts(pair<T,U> p) {
#ifdef LOCAL
return "("+ts(p.f)+", "+ts(p.s)+")";
#else
return ts(p.f)+" "+ts(p.s);
#endif
}
// OUTPUT
tcT> void pr(T x) { cout << ts(x); }
tcTUU> void pr(const T& t, const U&... u) {
pr(t); pr(u...); }
void ps() { pr("\n"); } // print w/ spaces
tcTUU> void ps(const T& t, const U&... u) {
pr(t); if (sizeof...(u)) pr(" "); ps(u...); }
// DEBUG
void DBG() { cerr << "]" << endl; }
tcTUU> void DBG(const T& t, const U&... u) {
cerr << ts(t); if (sizeof...(u)) cerr << ", ";
DBG(u...); }
#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert
#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
<< __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#else
#define dbg(...) 0
#define chk(...) 0
#endif
void setPrec() { cout << fixed << setprecision(15); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s = "") {
unsyncIO(); setPrec();
// cin.exceptions(cin.failbit);
// throws exception when do smth illegal
// ex. try to read letter into int
if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO
}
#define ints(...) int __VA_ARGS__; re(__VA_ARGS__);
class Fenwick{
public:
vector<int> li;
int nn;
Fenwick(int x){
nn = x+5;
li.resize(nn);
}
void add(int ind, int val){
ind+=2;
while(ind<nn){
li[ind] ^= val;
ind += ind & (-ind);
}
}
int sum(int ind){
ind += 2;
int ret = 0LL;
while(ind>0){
ret ^= li[ind];
ind -= ind & (-ind);
}
return ret;
}
void res(){
for(int i = 0; i<li.size(); i++){
li[i] = 0LL;
}
}
};
struct LCA {
public:
int N; vector<vi> par, adj_lca; vi depth;
void init(int _N) { N = _N;
int d = 1; while ((1<<d) < N) d ++;
par.assign(d,vi(N)); adj_lca.rsz(N); depth.rsz(N);
}
void ae(int x, int y) { adj_lca[x].pb(y), adj_lca[y].pb(x); }
void gen(int R = 0) { par[0][R] = R; dfs(R); }
void dfs(int x = 0) {
FOR(i,1,sz(par)) par[i][x] = par[i-1][par[i-1][x]];
each(y,adj_lca[x]) if (y != par[0][x])
depth[y] = depth[par[0][y]=x]+1, dfs(y);
}
int jmp(int x, int d) {
F0R(i,sz(par)) if ((d>>i)&1) x = par[i][x];
return x; }
int lca(int x, int y) {
if (depth[x] < depth[y]) swap(x,y);
x = jmp(x,depth[x]-depth[y]); if (x == y) return x;
R0F(i,sz(par)) {
int X = par[i][x], Y = par[i][y];
if (X != Y) x = X, y = Y;
}
return par[0][x];
}
int dist(int x, int y) { // # edges on path
return depth[x]+depth[y]-2*depth[lca(x,y)]; }
};
vector<int> times[600001];
int pre[300001];
int results[300001];
int og_results[300001];
vector<pi> queries[600002];
int lo[300001];
int hi[300001];
bool has[300001];
int n, q;
int u[300001];
int v[300001];
int l[300001];
int r[300001];
int a[300001];
int lca_val[300001];
int hsh_val[300001];
vector<int> adj[300001];
int point = 1;
void go(int now, int from){
times[a[now]].pb(point++);
pre[now] = point-1;
for(int i = 0; i<adj[now].size(); i++){
int to = adj[now][i];
if(to==from){
continue;
}
go(to,now);
}
times[a[now]].pb(point++);
}
Fenwick fen = Fenwick(600002);
LCA lca_ds;
void reset_queries(){
fen.res();
for(int i = 0; i<point; i++){
queries[i].clear();
}
for(int i = 0; i<=q; i++){
results[i] = 0LL;
}
}
void make_query_start(int st, int ind){
// do after
queries[st-1].pb(mp(pre[u[ind]],ind));
queries[st-1].pb(mp(pre[v[ind]],ind));
}
void make_query_end(int st, int en, int ind){
// do after
queries[en].pb(mp(pre[u[ind]],ind));
queries[en].pb(mp(pre[v[ind]],ind));
int got_lca = lca_val[ind];
if(a[got_lca]>=st && a[got_lca]<=en){
results[ind] ^= hsh_val[a[got_lca]];
}
}
void make_query(int st, int en, int ind){
// do after
queries[en].pb(mp(pre[u[ind]],ind));
queries[en].pb(mp(pre[v[ind]],ind));
int got_lca = lca_val[ind];
if(a[got_lca]>=st && a[got_lca]<=en){
results[ind] ^= hsh_val[a[got_lca]];
}
queries[st-1].pb(mp(pre[u[ind]],ind));
queries[st-1].pb(mp(pre[v[ind]],ind));
}
void handle_queries(){
for(int i = 0; i<point; i++){
for(int j = 0; j<times[i].size(); j++){
fen.add(times[i][j],hsh_val[i]);
}
for(int j = 0; j<queries[i].size(); j++){
results[queries[i][j].s] ^= fen.sum(queries[i][j].f);
}
}
}
int main() {
setIO();
cin >> n >> q;
for(int i = 0; i<n; i++){
cin >> a[i];
}
lca_ds.init(n);
for(int i = 1; i<n; i++){
int a, b;
cin >> a >> b;
a--;
b--;
adj[a].pb(b);
adj[b].pb(a);
lca_ds.ae(a,b);
}
lca_ds.gen();
go(0,-1);
// 1000006019
// 1000004023
// 900004043
// 910004063
// 910004087
// 910004099
for(int i = 1; i<=n; i++){
ll a = (ll)i*900004043LL+910004063LL;
a %= 1000006019LL;
hsh_val[i] = (int)a;
}
reset_queries();
for(int i = 0; i<q; i++){
cin >> u[i] >> v[i] >> l[i] >> r[i];
u[i]--;
v[i]--;
lca_val[i] = lca_ds.lca(u[i],v[i]);
make_query(l[i],r[i],i);
}
handle_queries();
for(int i = 0; i<q; i++){
if(results[i]!=0LL){
has[i] = true;
lo[i] = l[i];
hi[i] = r[i];
}
else{
has[i] = false;
}
}
reset_queries();
for(int i = 0; i<q; i++){
if(has[i]){
make_query_start(l[i],i);
}
}
handle_queries();
for(int i = 0; i<q; i++){
if(has[i]){
og_results[i] = results[i];
}
}
while(true){
int cnt = 0;
reset_queries();
for(int i = 0; i<q; i++){
if(has[i] && lo[i]!=hi[i]){
make_query_end(l[i],(lo[i]+hi[i])/2,i);
// make_query(lo[i],(lo[i]+hi[i])/2,i);
cnt++;
}
}
if(cnt==0){
break;
}
handle_queries();
for(int i = 0; i<q; i++){
if(has[i] && lo[i]!=hi[i]){
// if(results[i] != 0){
if(results[i] != og_results[i]){
hi[i] = (lo[i]+hi[i])/2;
}
else{
lo[i] = (lo[i]+hi[i])/2+1;
}
}
}
}
for(int i = 0; i<q; i++){
if(!has[i]){
cout << -1 << "\n";
}
else{
cout << lo[i] << "\n";
}
}
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
| 21 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
long long A[maxn];
long long f[maxn], cnt[maxn];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> A[i];
memset(f, 0, sizeof(f));
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i < m; i++) {
long long x = A[i], y = A[i + 1];
if (x == y) continue;
long long d = abs(y - x);
f[0] += d;
if (x > y) swap(x, y);
if (d > 1) {
cnt[x + 1]--;
cnt[y]++;
}
f[x] += x - 1;
f[y] += x - d;
}
long long ans = f[0];
for (int i = 1; i <= n; i++) {
ans += cnt[i];
cout << ans + f[i] << " ";
}
cout << endl;
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int q, s;
node() {
s = -1;
q = 1e9;
}
};
const int N = 1e5 + 2000;
int n, m, b[N], c[N], s, t;
node a[N * 4];
void up(int l, int r, int ld, int rd, int x, int index, int q) {
if (ld >= r || rd <= l) return;
if (ld <= l && r <= rd) {
a[x].s = index + l - ld;
a[x].q = q;
return;
}
int m = (l + r) / 2;
up(l, m, ld, rd, x * 2, index, q);
up(m, r, ld, rd, x * 2 + 1, index, q);
}
void get(int l, int r, int p, int x) {
if (p >= r || p + 1 <= l) return;
if (a[x].q < t) {
t = a[x].q;
s = a[x].s + p - l;
}
if (r - l == 1) return;
int m = (l + r) / 2;
get(l, m, p, x * 2);
get(m, r, p, x * 2 + 1);
}
int in() {
int a;
bool b = scanf("%d", &a);
return a - b;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) scanf("%d", &b[i]);
for (int i = 0; i < n; i++) scanf("%d", &c[i]);
int tm = m + 1;
while (m--) {
int type;
cin >> type;
if (type == 1) {
int x = in(), y = in(), q;
scanf("%d", &q);
up(0, n, y, y + q, 1, x, m);
} else {
int x = in();
t = tm;
get(0, n, x, 1);
if (t != tm)
printf("%d\n", b[s]);
else
printf("%d\n", c[x]);
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
int res = pow(n, 2) + pow(n - 1, 2);
printf("%d", res);
return 0;
}
| 0 |
#include <bits/stdc++.h>
char s[2010];
int a[2010][2010];
int main() {
int n, m, k, i, j;
scanf("%d %d %d", &n, &m, &k);
for (i = 0; i < n; i++) {
scanf("%s", s);
for (j = 0; j < m; j++) {
if (s[j] == 'U')
a[i][j] = 1;
else if (s[j] == 'R')
a[i][j] = 2;
else if (s[j] == 'D')
a[i][j] = 3;
else if (s[j] == 'L')
a[i][j] = 4;
else
a[i][j] = 0;
}
}
for (j = 0; j < m; j++) {
int res = 0;
for (i = 0; i < n; i++) {
int si, sj;
si = i + i;
sj = j;
if (si < n && a[si][sj] == 1) res++;
si = i;
sj = j - i;
if (sj >= 0 && a[si][sj] == 2) res++;
si = 0;
sj = j;
if (a[si][sj] == 3) res++;
si = i;
sj = j + i;
if (sj < m && a[si][sj] == 4) res++;
}
printf("%d ", res);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long sum[500];
int deep[500];
int in1[500];
int out1[500];
int in2[500];
int out2[500];
vector<int> maps1[500];
vector<int> maps2[500];
long long dp[150000];
long long n, m, t;
int a[500];
int cnt = 0;
void topo2() {
queue<int> q;
for (int i = 1; i <= n; ++i)
if (in2[i] == 0) {
q.push(i);
deep[i] = 0;
}
int n1, n2;
while (!q.empty()) {
n1 = q.front();
q.pop();
cnt++;
t -= deep[n1] * sum[n1];
for (int i = 0; i < maps2[n1].size(); ++i) {
n2 = maps2[n1][i];
in2[n2]--;
deep[n2] = max(deep[n2], deep[n1] + 1);
if (in2[n2] == 0) q.push(n2);
}
}
}
void topo1() {
queue<int> q;
int n1, n2;
for (int i = 1; i <= n; ++i)
if (in1[i] == 0) {
q.push(i);
}
while (!q.empty()) {
n1 = q.front();
q.pop();
for (int i = 0; i < maps1[n1].size(); ++i) {
n2 = maps1[n1][i];
in1[n2]--;
sum[n2] += sum[n1];
if (in1[n2] == 0) {
q.push(n2);
}
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &t);
for (int i = 1; i <= n; ++i) scanf("%lld", &sum[i]);
for (int i = 1; i <= m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
maps1[x].push_back(y);
in1[y]++;
out1[x]++;
maps2[y].push_back(x);
in2[x]++;
out2[y]++;
}
topo2();
if (t < 0 || cnt != n)
printf("0\n");
else {
topo1();
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = sum[i]; j <= t; ++j)
dp[j] = (dp[j] + dp[j - sum[i]]) % 1000000007;
}
printf("%lld\n", dp[t]);
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s;
scanf("%d%d", &n, &s);
int ans = 100, res = 0;
int flag = 0;
int a, b;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &a, &b);
if (a < s) {
if (100 - b <= 99 && b > 0) res = (res > 100 - b ? res : 100 - b);
flag = 1;
} else if (a == s && b == 0) {
res = (0 > res ? 0 : res);
flag = 1;
}
}
if (!flag)
printf("-1\n");
else
printf("%d\n", res);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename S, typename T>
ostream& operator<<(ostream& out, pair<S, T> const& p) {
out << '(' << p.first << ", " << p.second << ')';
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
long long l = v.size();
for (long long i = 0; i < l - 1; i++) out << v[i] << ' ';
if (l > 0) out << v[l - 1];
return out;
}
template <typename T>
void trace(const char* name, T&& arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
trace(comma + 1, args...);
}
const long long N = 2 * 600;
const long long MOD = 998244353;
long long power(long long x, long long t) {
long long ans = 1;
while (t > 0) {
if (t & 1) ans = 1LL * ans * x % MOD;
x = 1LL * x * x % MOD;
t >>= 1;
}
return ans;
}
inline long long sub(long long a, long long b) {
a %= MOD;
b %= MOD;
a = ((a - b) % MOD + MOD) % MOD;
return a;
}
inline long long add(long long a, long long b) {
a %= MOD;
b %= MOD;
a = (a + b) % MOD;
return a;
}
inline long long mul(long long a, long long b) {
a %= MOD;
b %= MOD;
a = (a * b) % MOD;
return a;
}
inline long long inv(long long a) {
a = a % MOD;
a = power(a, MOD - 2);
return a;
}
long long m;
long long dp[N][N];
long long n;
vector<long long> t_arr, arr;
vector<vector<long long> > occ(N);
long long f(long long l, long long r) {
if (l > r) return 1;
if (dp[l][r] != -1) {
return dp[l][r];
}
long long mi = 1000000000000000001, idx;
for (long long i = l; i <= r; i++) {
if (arr[i] < mi) {
mi = arr[i];
idx = i;
}
}
if (l == r) {
long long a = occ[mi][0];
long long b = occ[mi].back();
if (l > a || r < b) {
return dp[l][r] = 0;
}
return dp[l][r] = 1;
}
long long a = occ[mi][0];
long long b = occ[mi].back();
if (l > a || r < b) {
return dp[l][r] = 0;
}
long long ans_left = 0, ans_right = 0;
for (long long rp = b; rp <= r; rp++) {
if (arr[rp] >= arr[idx]) {
ans_right = add(ans_right, mul(f(b + 1, rp), f(rp + 1, r)));
} else {
break;
}
}
for (long long lp = a; lp >= l; lp--) {
if (arr[lp] >= arr[idx]) {
ans_left = add(ans_left, mul(f(l, lp - 1), f(lp, a - 1)));
} else {
break;
}
}
long long ans = mul(ans_left, ans_right);
for (long long i = 0; i < (long long)occ[mi].size() - 1; i++) {
if (occ[mi][i] + 1 != occ[mi][i + 1]) {
ans = mul(ans, f(occ[mi][i] + 1, occ[mi][i + 1] - 1));
}
}
return dp[l][r] = ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
t_arr.assign(m + 1, 0);
for (long long i = 1; i <= m; i++) {
cin >> t_arr[i];
}
for (long long i = 1; i <= m; i++) {
if (arr.empty() || arr.back() != t_arr[i]) {
arr.push_back(t_arr[i]);
}
}
m = arr.size();
if (m > 2 * n) {
cout << 0 << "\n";
return 0;
}
n = arr.size();
for (long long i = 0; i < n; i++) {
occ[arr[i]].push_back(i);
}
memset(dp, -1, sizeof(dp));
cout << f(0, n - 1) << "\n";
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 'z' - 'a' + 1;
int need[MAXN], have[MAXN], put[MAXN], ready[MAXN], is[MAXN];
vector<int> inte;
int main() {
char t[1000001], s[1000001];
int c = 0;
scanf("%s%s", s, t);
int lt = strlen(t), ls = strlen(s);
for (int i = 0; i < lt; i++) {
need[t[i] - 'a']++;
}
int a;
for (int i = 0; i < ls; i++) {
a = s[i] - 'a';
if (s[i] != '?' && need[a] > 0) {
have[a]++;
if (have[a] == need[a]) ready[a]++, have[a] = 0;
}
if (s[i] == '?') c++;
}
int mi = 123456789;
for (int i = 0; i < 27; i++) {
if (need[i]) mi = min(mi, ready[i]);
}
bool flag = true;
while (c > 0) {
for (int i = 0; i < MAXN; i++) {
if (need[i] && ready[i] == mi) {
if (need[i] - have[i] <= c) {
c -= need[i] - have[i];
put[i] += need[i] - have[i];
if (!is[i]) is[i] = 1, inte.push_back(i);
have[i] = 0;
ready[i]++;
} else {
flag = false;
break;
}
}
}
mi++;
if (!flag) break;
}
int ind = 0;
for (int i = 0; i < ls; i++) {
if (s[i] == '?') {
if (ind < (int)inte.size()) {
s[i] = 'a' + inte[ind];
put[inte[ind]]--;
if (put[inte[ind]] == 0) ind++;
} else
s[i] = 'a';
}
}
for (int i = 0; i < ls; i++) printf("%c", s[i]);
printf("\n");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int d, m, gen[25], b[30010], n;
long long l, r;
bitset<30010> all;
class dat {
public:
long long len, ans;
bitset<30010> pre, suf;
dat operator+(const dat &t) const {
dat c;
c.ans = ans + t.ans;
c.len = len + t.len;
c.pre = pre;
c.suf = t.suf;
if (len < n - 1) c.pre &= (t.pre >> len) | (all << (n - len - 1));
if (t.len < n - 1) c.suf &= (suf << t.len) | (all >> (n - t.len - 1));
if (c.len >= n) {
bitset<30010> cur = suf & t.pre;
if (len < n - 1) cur &= (all >> (n - 1 - len));
if (t.len < n - 1) cur &= (all << (n - 1 - t.len));
c.ans += cur.count();
}
return c;
}
void operator+=(const dat &t) { (*this) = ((!len) ? t : (*this) + t); }
} f[66][66];
long long len[66];
int tot;
long long solve(long long r) {
dat ans;
ans.len = ans.ans = 0;
int delta = 0;
for (int i = tot; i >= 0; i--) {
for (int j = 1; j <= d; j++) {
if (r >= len[i]) {
r -= len[i];
ans += f[i][(delta + gen[j]) % m];
} else {
delta = (delta + gen[j]) % m;
break;
}
}
}
return ans.ans;
}
int main() {
scanf("%d%d", &d, &m);
for (int i = 1; i <= d; i++) scanf("%d", &gen[i]);
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
for (int i = 1; i < n; i++) all.set(i);
scanf("%lld%lld", &l, &r);
for (int i = 0; i < m; i++) {
f[0][i].len = 1;
if (n == 1)
f[0][i].ans = (i <= b[1]);
else
for (int j = 1; j <= n - 1; j++) {
f[0][i].pre[j] = (i <= b[j + 1]);
f[0][i].suf[j] = (i <= b[j]);
}
}
len[0] = 1;
for (tot = 0; len[tot] <= r / d;) {
++tot;
len[tot] = len[tot - 1] * d;
for (int i = 0; i < m; i++) {
for (int j = 1; j <= d; j++) {
f[tot][i] += f[tot - 1][(i + gen[j]) % m];
}
}
}
printf("%lld\n", solve(r) - solve(l + n - 2));
return 0;
}
| 26 |
#include <bits/stdc++.h>
const int pi = acos(-1);
using namespace std;
int n, k;
vector<int> v, u;
int main() {
cin >> k >> n;
v.resize(k);
u.resize(n);
for (int i = 0; i < k; i++) v[i] = i + 1;
for (int i = 0; i < n; i++) cin >> u[i];
int leader = 0;
int i = 0;
while (n--) {
int lel = u[i] % v.size(), del;
if (leader + lel >= v.size()) {
del = leader + lel - v.size();
cout << v[del] << " ";
leader = del;
} else {
del = leader + lel;
cout << v[del] << " ";
leader = del;
}
vector<int> temp;
for (int j = 0; j < v.size(); j++)
if (v[j] != v[del]) temp.push_back(v[j]);
v = temp;
i++;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
Node *ZERO = nullptr, *ONE = nullptr;
bool isRoot = false;
int count = 0;
};
Node root;
void insert(int n, int bit, Node *node) {
node->count++;
if (bit < 0) {
node->isRoot = true;
return;
}
if ((n & (1 << bit)) == 0) {
if (node->ZERO == nullptr) node->ZERO = new Node();
insert(n, bit - 1, node->ZERO);
} else {
if (node->ONE == nullptr) node->ONE = new Node();
insert(n, bit - 1, node->ONE);
}
}
int ANS = 1000000000;
void answer(Node *node, int score) {
int z = (node->ZERO == nullptr ? 0 : node->ZERO->count);
int o = (node->ONE == nullptr ? 0 : node->ONE->count);
int zp = (z == 0 ? 0 : z - 1);
int op = (o == 0 ? 0 : o - 1);
if (o == 2) {
ANS = min(score + zp, ANS);
}
if (z == 2) {
ANS = min(score + op, ANS);
}
if (node->ZERO != nullptr) answer(node->ZERO, score + op);
if (node->ONE != nullptr) answer(node->ONE, score + zp);
}
int main() {
std::iostream::sync_with_stdio(false);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
insert(A[i], 30, &root);
}
answer(&root, 0);
if (ANS == 1000000000) ANS = N - 2;
cout << ANS;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int n, aa[200005], Left[200005], Right[200005];
long long a[200005][6];
void push_back(long long x, long long *a) {
if (x <= 0) return;
while (a[0]) a++;
a[0] = x;
}
void minimize(int &a, int b) {
if (a > b) a = b;
}
bool FF[6][6][3][200005];
int F[6][6][3][200005];
int f(int u, int Just, int JustJust, int J, int JJ, int Loaded) {
if (u == n + 1) return 1;
if (Loaded >= 2) Loaded = 2;
if (FF[J][JJ][Loaded][u]++) return F[J][JJ][Loaded][u];
int Min = 0x3c3c3c3c;
if (Loaded) minimize(Min, f(u, 0, 0, 0, 0, 0) + 1);
for (int i = 0; int v = a[u][i]; i++)
if (Loaded < 2 || v + JustJust == Just + Just)
minimize(Min, f(u + 1, v, Just, i, J, Loaded + 1));
return F[J][JJ][Loaded][u] = Min;
}
bool empty() {
for (int i = 1; i <= n; i++)
if (aa[i]) return false;
return true;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &aa[i]);
if (aa[i] == -1) aa[i] = 0;
}
if (empty()) {
cout << 1 << endl;
return 0;
}
Left[0] = 0, Right[n + 1] = 0;
for (int i = 1; i <= n; i++) Left[i] = (aa[i] ? i : Left[i - 1]);
for (int i = n; i >= 1; i--) Right[i] = (aa[i] ? i : Right[i + 1]);
for (int i = 1; i <= n; i++) {
if (aa[i])
push_back(aa[i], a[i]);
else {
if (Left[i]) push_back(aa[Left[i]], a[i]);
if (Right[i]) push_back(aa[Right[i]], a[i]);
if (Left[i] && Left[Left[i] - 1]) {
int ll = Left[i], llll = Left[ll - 1];
if ((aa[llll] - aa[ll]) % (ll - llll) == 0)
push_back(aa[ll] + 1LL * (aa[ll] - aa[llll]) / (ll - llll) * (i - ll),
a[i]);
}
if (Right[i] && Right[Right[i] + 1]) {
int rr = Right[i], rrrr = Right[rr + 1];
if ((aa[rrrr] - aa[rr]) % (rr - rrrr) == 0)
push_back(
aa[rrrr] - 1LL * (aa[rrrr] - aa[rr]) / (rrrr - rr) * (rrrr - i),
a[i]);
}
if (Left[i] && Right[i]) {
int ll = Left[i], rr = Right[i];
if ((aa[rr] - aa[ll]) % (rr - ll) == 0)
push_back(aa[ll] + 1LL * (aa[rr] - aa[ll]) / (rr - ll) * (i - ll),
a[i]);
}
}
}
for (int i = n; i >= 1; i--) f(i, 0, 0, 0, 0, 0);
cout << f(1, 0, 0, 0, 0, 0) << endl;
}
| 16 |
#include <bits/stdc++.h>
int n, m, sol, val;
inline int rid_log(int a, int put) {
if (!put) return 1;
int val2 = rid_log(a, put / 2);
val2 = (long long)val2 * val2 % 1000000009;
if (put & 1) val2 = (long long)val2 * a % 1000000009;
return val2;
}
int main() {
int i;
scanf("%d%d", &n, &m);
val = rid_log(2, m);
sol = 1;
for (i = 1; i <= n; i++) {
val--;
sol = (long long)sol * val % 1000000009;
}
printf("%d\n", sol);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;
const int INF = 2147483647;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, i, j, u, v;
cin >> n;
vector<pair<int, int>> g(n), ans;
queue<int> q;
for (i = 0; i < n; ++i) cin >> g[i].first >> g[i].second;
for (i = 0; i < n; ++i) {
if (g[i].first == 1) q.push(i);
}
while (!q.empty()) {
u = q.front();
q.pop();
if (!g[u].first) continue;
v = g[u].second;
ans.push_back({u, v});
g[v].first -= 1;
g[v].second ^= u;
if (g[v].first == 1) q.push(v);
}
cout << int(ans.size()) << "\n";
for (auto p : ans) cout << p.first << " " << p.second << "\n";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long n;
bool ok(long long d, vector<long long>& c) {
long long ans = 0;
for (long long i = 0; i < int((c).size()); i++) {
ans += (c[i] / d);
}
if (ans >= n)
return true;
else
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long m, i, j;
cin >> n >> m;
vector<long long> a(m);
map<long long, long long> mp;
for (i = 0; i < m; i++) cin >> a[i], mp[a[i]]++;
vector<long long> c;
for (auto p : mp) c.push_back(p.second);
long long x = 0;
for (long long z = m; z > 0; z /= 2) {
while (ok(x + z, c)) x += z;
}
cout << x;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
class SegTree {
private:
const ll NEUT = 4 * (ll)1e18;
vector<ll> seg, tag;
int h = 1;
void apply(int i, ll v) {
seg[i] += v;
if (i < h) tag[i] += v;
}
void push(int i) {
if (tag[i] == 0) return;
apply(2 * i, tag[i]);
apply(2 * i + 1, tag[i]);
tag[i] = 0;
}
ll combine(ll a, ll b) { return min(a, b); }
ll recGet(int a, int b, int i, int ia, int ib) {
if (ib <= a || b <= ia) return NEUT;
if (a <= ia && ib <= b) return seg[i];
push(i);
int im = (ia + ib) >> 1;
return combine(recGet(a, b, 2 * i, ia, im),
recGet(a, b, 2 * i + 1, im, ib));
}
void recApply(int a, int b, ll v, int i, int ia, int ib) {
if (ib <= a || b <= ia) return;
if (a <= ia && ib <= b)
apply(i, v);
else {
push(i);
int im = (ia + ib) >> 1;
recApply(a, b, v, 2 * i, ia, im);
recApply(a, b, v, 2 * i + 1, im, ib);
seg[i] = combine(seg[2 * i], seg[2 * i + 1]);
}
}
public:
SegTree(int n) {
while (h < n) h *= 2;
seg.resize(2 * h, 0);
tag.resize(h, 0);
}
ll rangeMin(int a, int b) { return recGet(a, b + 1, 1, 0, h); }
void rangeAdd(int a, int b, ll v) { recApply(a, b + 1, v, 1, 0, h); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k, q;
cin >> n >> k >> q;
SegTree seg(2 * n);
for (int i = 0; i < 2 * n; ++i) seg.rangeAdd(i, i, -i);
multiset<pair<int, int>> acts;
multiset<int> inds;
for (int qi = 0; qi < q; ++qi) {
int x, y;
cin >> x >> y;
int ry = y - 1 + abs(x - k);
auto it = acts.find(make_pair(x, y));
if (it != acts.end()) {
acts.erase(it);
auto it2 = inds.find(ry);
inds.erase(it2);
seg.rangeAdd(0, ry, 1);
} else {
acts.insert(make_pair(x, y));
inds.insert(ry);
seg.rangeAdd(0, ry, -1);
}
if (inds.empty())
cout << 0 << '\n';
else {
auto it3 = inds.end();
--it3;
int max_y = *it3;
int off = -seg.rangeMin(0, max_y);
cout << max(0, off - n) << '\n';
}
}
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
struct AhoCorasick {
static const int nletter = 2;
struct node {
vector<int> keywords;
node *go[nletter] = {}, *failure = {}, *output = {};
} *root = new node();
node *add(int size, int *values, int keywordid) {
assert(size > 0);
node *v = root;
for (int i = 0; i < size; ++i) {
node *&u = v->go[values[i]];
if (!u) u = new node();
v = u;
}
v->keywords.push_back(keywordid);
return v;
}
void build() {
queue<node *> q;
for (int i = 0; i < nletter; ++i) {
node *&v = root->go[i];
if (v) {
v->failure = root;
v->output = v->keywords.size() > 0 ? v : nullptr;
q.push(v);
} else {
v = root;
}
}
while (!q.empty()) {
node *u = q.front();
q.pop();
for (int i = 0; i < nletter; ++i) {
node *v = u->go[i];
if (v) {
node *x = u->failure;
while (!x->go[i]) x = x->failure;
v->failure = x->go[i];
v->output = v->keywords.size() > 0 ? v : v->failure->output;
q.push(v);
}
}
}
}
node *iterate(node *v, int value) {
while (!v->go[value]) v = v->failure;
return v->go[value];
}
vector<int> matches(node *v) {
vector<int> result;
for (node *x = v->output; x; x = x->failure->output)
result.insert(result.end(), x->keywords.begin(), x->keywords.end());
return result;
}
};
struct Dinitz {
int n, source, sink;
vector<vector<int>> adj;
struct edge {
int u, flow, cap;
};
vector<edge> edges;
Dinitz(int n, int source, int sink)
: n(n), source(source), sink(sink), adj(n) {}
int add_edge(int a, int b, int abcap, int bacap = 0) {
int result = edges.size();
adj[a].push_back(edges.size());
edges.push_back({b, 0, abcap});
adj[b].push_back(edges.size());
edges.push_back({a, 0, bacap});
return result;
}
vector<int> mindist, position;
bool bfs() {
queue<int> q;
fill(mindist.begin(), mindist.end(), INT_MAX / 2);
mindist[source] = 0;
q.push(source);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int edid : adj[v]) {
edge &ed = edges[edid];
if (ed.flow < ed.cap && mindist[ed.u] > mindist[v] + 1) {
mindist[ed.u] = mindist[v] + 1;
q.push(ed.u);
}
}
}
return mindist[sink] < n;
}
int dfs(int v, int maxf) {
if (v == sink) return maxf;
for (int &pos = position[v]; pos < (int)adj[v].size(); ++pos) {
int edid = adj[v][pos];
edge &ed = edges[edid], &reved = edges[edid ^ 1];
if (ed.flow < ed.cap && mindist[ed.u] == mindist[v] + 1) {
int f = dfs(ed.u, min(maxf, ed.cap - ed.flow));
if (f > 0) {
ed.flow += f;
reved.flow -= f;
return f;
}
}
}
return 0;
}
int maxflow() {
int result = 0;
mindist.resize(n);
position.resize(n);
while (bfs()) {
int extra;
do {
fill(position.begin(), position.end(), 0);
extra = dfs(source, INT_MAX);
result += extra;
} while (extra > 0);
}
return result;
}
};
struct BipartiteVertexCover {
int nleft, nright;
vector<vector<int>> adj, ids;
Dinitz din;
vector<int> other, incover;
BipartiteVertexCover(int nleft, int nright)
: nleft(nleft),
nright(nright),
adj(nleft + nright),
ids(nleft),
din(1 + nleft + nright + 1, 0, 1 + nleft + nright) {
for (int l = 0; l < nleft; ++l) din.add_edge(0, 1 + l, 1);
for (int r = 0; r < nright; ++r)
din.add_edge(1 + nleft + r, 1 + nleft + nright, 1);
}
void add_edge(int l, int r) {
int id = din.add_edge(1 + l, 1 + nleft + r, 1);
adj[l].push_back(nleft + r);
ids[l].push_back(id);
adj[nleft + r].push_back(l);
}
void dfs(int v, bool in) {
if (incover[v] != -1) return;
incover[v] = in;
if (in == true) {
dfs(other[v], false);
} else {
for (int u : adj[v]) dfs(u, true);
}
}
vector<pair<int, int>> solve() {
int n = nleft + nright;
other = vector<int>(n, -1);
int matchingsize = din.maxflow();
for (int l = 0; l < nleft; ++l)
for (int i = 0; i < (int)adj[l].size(); ++i) {
int r = adj[l][i] - nleft, id = ids[l][i];
if (din.edges[id].flow > 0) {
other[l] = nleft + r;
other[nleft + r] = l;
}
}
incover = vector<int>(n, -1);
for (int i = 0; i < n; ++i)
if (other[i] == -1) dfs(i, 0);
for (int i = 0; i < n; ++i)
if (incover[i] == -1) dfs(i, 0);
vector<pair<int, int>> result;
for (int i = 0; i < n; ++i)
if (incover[i]) {
if (i < nleft)
result.push_back({0, i});
else
result.push_back({1, i - nleft});
}
assert(result.size() == matchingsize);
return result;
}
};
int adj[750][750];
int main() {
using node = AhoCorasick::node;
int n;
scanf("%d", &n);
AhoCorasick aho;
vector<vector<int>> keywords(n);
for (int i = 0; i < n; ++i) {
static char str[10000000 + 1];
scanf("%s", str);
vector<int> v(strlen(str));
for (int j = 0; str[j]; ++j) v[j] = str[j] - 'a';
aho.add(v.size(), &v[0], i);
keywords[i] = move(v);
}
aho.build();
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) adj[i][j] = 0;
for (int i = 0; i < n; ++i) {
vector<int> &values = keywords[i];
node *state = aho.root;
for (int value : values) {
state = aho.iterate(state, value);
node *out = state->output;
if (out) {
int id = out->keywords.front();
if (id == i) {
out = out->failure->output;
if (out)
id = out->keywords.front();
else
id = -1;
}
if (id != -1) adj[i][id] = 1;
}
}
}
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) adj[i][j] |= adj[i][k] & adj[k][j];
BipartiteVertexCover bvc(n, n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (adj[i][j]) bvc.add_edge(i, j);
vector<bool> inresult(n, true);
for (pair<int, int> node : bvc.solve()) inresult[node.second] = false;
vector<int> result;
for (int i = 0; i < n; ++i)
if (inresult[i]) result.push_back(i);
printf("%d\n", (int)result.size());
for (int i = 0; i < (int)result.size(); ++i) {
if (i > 0) printf(" ");
printf("%d", result[i] + 1);
}
printf("\n");
return 0;
}
| 24 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, ans = 0;
cin >> n >> k;
if (k / n != 2)
cout << 0 << endl;
else {
ans = n - k % n;
cout << ans << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
const int N = 1e5 + 5;
int tt, n, m, x, y, ans[N], pos[N], col[N];
std::vector<int> c[N];
std::priority_queue<std::pair<int, int> > h;
int main() {
scanf("%d", &tt);
int i, a, C, mx;
std::pair<int, int> o;
while (tt--) {
scanf("%d%d%d", &n, &x, &y);
for (i = 1; i < n + 2; i++) c[i].clear();
while (h.size()) h.pop();
for (i = 1; i <= n; ans[i++] = 0) scanf("%d", &a), c[a].push_back(i);
for (i = 1; i < n + 2; i++)
if (c[i].size())
h.push(std::make_pair(c[i].size(), i));
else
C = i;
for (i = 1; i <= x; i++) {
o = h.top(), h.pop(), o.first--;
ans[*c[o.second].rbegin()] = o.second, c[o.second].pop_back();
if (o.first) h.push(o);
}
for (i = 1, m = mx = 0; i < n + 2; i++) {
mx = std::max(mx, (int)c[i].size());
for (int j : c[i]) pos[m] = j, col[m++] = i;
}
for (i = 0; i < m; i++) {
mx %= m;
if (x >= y) break;
if (col[i] != col[mx]) ans[pos[i]] = col[mx++], x++;
}
if (x < y)
puts("NO");
else {
for (i = 1, puts("YES"); i <= n; i++) printf("%d ", ans[i] ? ans[i] : C);
puts("");
}
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e9 + 7;
long long int int_pow(long long int base, long long int exp) {
long long int result = 1;
while (exp) {
if (exp & 1) result *= base;
exp /= 2;
base *= base;
}
return result;
}
string s[2000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string z;
cin >> z;
long long int n = 0, ans = 0;
for (long long int i = 0; i < z.length(); i++) {
if (z[i] == '.') {
for (long long int i = 0; i < n; i++)
if (s[i] == s[n]) ans++;
s[n] = "";
n--;
} else if (z[i] == ',' || z[i] == ':') {
n++;
} else {
s[n] += z[i];
}
}
cout << ans;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int u1[610], u2[610], f[610];
vector<int> v1, v2, e[610];
int ans[610];
bool v[610];
bool dfs(int x, int y, int d) {
ans[d++] = x;
v[x] = true;
if (x == y) {
cout << "N " << d << "\n";
for (int i = 0; i < d; i++) cout << ans[i] << " ";
cout << "\n";
cout.flush();
return true;
}
for (int i = 0; i < e[x].size(); i++)
if (!v[e[x][i]])
if (dfs(e[x][i], y, d)) return true;
return false;
}
int main() {
int n, t, t1, t2, t0;
cin >> n;
if (n == 1) {
cout << "Y 1\n1\n";
cout.flush();
return 0;
}
int n1 = 0, n2 = 0;
u1[n1++] = 1;
f[1] = 0;
for (int i = 2; i <= n; i++) u2[n2++] = i;
while (n2 > 0) {
if (n1 > 1) {
cout << "? " << n1 << "\n";
for (int i = 0; i < n1; i++) cout << u1[i] << " ";
cout << "\n";
cout.flush();
cin >> t1;
} else
t1 = 0;
int l2 = 0, r2 = n2 - 1;
while (l2 < r2) {
int m = (l2 + r2) / 2;
cout << "? " << n1 + (m - l2 + 1) << "\n";
for (int i = 0; i < n1; i++) cout << u1[i] << " ";
for (int i = l2; i <= m; i++) cout << u2[i] << " ";
cout << "\n";
cout.flush();
cin >> t;
if (m > l2) {
cout << "? " << m - l2 + 1 << "\n";
for (int i = l2; i <= m; i++) cout << u2[i] << " ";
cout << "\n";
cout.flush();
cin >> t2;
} else
t2 = 0;
t0 = t - t1 - t2;
if (t0)
r2 = m;
else
l2 = m + 1;
}
int y = u2[l2];
int l1 = 0, r1 = n1 - 1;
while (l1 < r1) {
int m = (l1 + r1) / 2;
cout << "? " << (m - l1 + 2) << "\n";
for (int i = l1; i <= m; i++) cout << u1[i] << " ";
cout << y << "\n";
cout.flush();
cin >> t;
if (m > l1) {
cout << "? " << (m - l1 + 1) << "\n";
for (int i = l1; i <= m; i++) cout << u1[i] << " ";
cout << "\n";
cout.flush();
cin >> t1;
} else
t1 = 0;
t0 = t - t1;
if (t0)
r1 = m;
else
l1 = m + 1;
}
int x = u1[l1];
e[x].push_back(y);
e[y].push_back(x);
u1[n1++] = y;
f[y] = 1 - f[u1[l1]];
for (int i = l2; i < n2 - 1; i++) u2[i] = u2[i + 1];
n2--;
}
for (int i = 0; i < n1; i++) {
if (f[u1[i]])
v2.push_back(u1[i]);
else
v1.push_back(u1[i]);
}
cout << "? " << v1.size() << "\n";
for (int x : v1) cout << x << " ";
cout << "\n";
cout.flush();
cin >> t1;
if (t1) {
int n1 = v1.size(), cur = 0;
while (cur < n1) {
cout << "? " << n1 - cur - 1 << "\n";
for (int i = cur + 1; i < n1; i++) cout << v1[i] << " ";
cout << "\n";
cout.flush();
cin >> t;
if (t == t1)
cur++;
else
break;
}
int x = v1[cur];
int l1 = cur + 1, r1 = n1 - 1;
while (l1 < r1) {
int m = (l1 + r1) / 2;
cout << "? " << (m - l1 + 2) << "\n";
for (int i = l1; i <= m; i++) cout << v1[i] << " ";
cout << x << "\n";
cout.flush();
cin >> t;
if (m > l1) {
cout << "? " << (m - l1 + 1) << "\n";
for (int i = l1; i <= m; i++) cout << v1[i] << " ";
cout << "\n";
cout.flush();
cin >> t1;
} else
t1 = 0;
t0 = t - t1;
if (t0)
r1 = m;
else
l1 = m + 1;
}
int y = v1[l1];
dfs(x, y, 0);
return 0;
}
cout << "? " << v2.size() << "\n";
for (int x : v2) cout << x << " ";
cout << "\n";
cout.flush();
cin >> t2;
if (t2) {
int n2 = v2.size(), cur = 0;
while (cur < n2) {
cout << "? " << n2 - cur - 1 << "\n";
for (int i = cur + 1; i < n2; i++) cout << v2[i] << " ";
cout << "\n";
cout.flush();
cin >> t;
if (t == t2)
cur++;
else
break;
}
int x = v2[cur];
int l1 = cur + 1, r1 = n2 - 1;
while (l1 < r1) {
int m = (l1 + r1) / 2;
cout << "? " << (m - l1 + 2) << "\n";
for (int i = l1; i <= m; i++) cout << v2[i] << " ";
cout << x << "\n";
cout.flush();
cin >> t;
if (m > l1) {
cout << "? " << (m - l1 + 1) << "\n";
for (int i = l1; i <= m; i++) cout << v2[i] << " ";
cout << "\n";
cout.flush();
cin >> t1;
} else
t1 = 0;
t0 = t - t1;
if (t0)
r1 = m;
else
l1 = m + 1;
}
int y = v2[l1];
dfs(x, y, 0);
return 0;
}
cout << "Y " << v1.size() << "\n";
for (int x : v1) cout << x << " ";
cout << "\n";
cout.flush();
}
| 20 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int Mod = 1e9 + 7;
const int maxn = 1000100;
int fac[maxn + 5];
inline void prepare() {
fac[0] = 1;
for (int i = (0), i_end_ = (maxn); i < i_end_; ++i)
fac[i + 1] = (long long)fac[i] * (i + 1) % Mod;
}
int n;
int a[maxn + 5];
bool notpr[maxn + 5];
int res[maxn + 5];
int ker[maxn + 5];
int bel[maxn + 5], cnt[maxn + 5], val[maxn + 5];
bool vis[maxn + 5];
inline int calc() {
for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) res[i] = i, ker[i] = 1;
memset(notpr, 0, sizeof notpr);
int ret = 1;
for (int i = 2; i * i <= n; ++i)
if (!notpr[i])
for (int j = i; j <= n; j += i) {
ker[j] *= i;
notpr[j] = 1;
while (!(res[j] % i)) res[j] /= i;
}
for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
if (a[i] && ker[i] != ker[a[i]]) return 0;
for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) ker[i] *= res[i];
for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) ++cnt[ker[i]];
for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
if (a[i]) --cnt[ker[i]];
for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
ret = (long long)ret * fac[cnt[i]] % Mod;
memset(cnt, 0, sizeof cnt);
memset(vis, 0, sizeof vis);
bel[1] = 1;
for (int i = (2), i_end_ = (n + 1); i < i_end_; ++i)
if (!notpr[i]) bel[i] = n / i;
for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
if (bel[i]) ++cnt[bel[i]];
for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
if (a[i]) {
if (bel[res[i]] != bel[res[a[i]]]) return 0;
if (i == 1 || res[i] > 1) {
if (val[res[i]]) {
if (val[res[i]] != res[a[i]]) return 0;
} else {
if (vis[res[a[i]]]) return 0;
val[res[i]] = res[a[i]];
vis[res[a[i]]] = 1;
--cnt[bel[res[a[i]]]];
}
}
}
for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i)
ret = (long long)ret * fac[cnt[i]] % Mod;
return ret;
}
int main() {
prepare();
scanf("%d", &n);
for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) scanf("%d", a + i);
printf("%d\n", calc());
return 0;
}
| 22 |
#include <bits/stdc++.h>
using namespace std;
double max(double a, double b) { return a > b ? a : b; }
double min(double a, double b) { return a < b ? a : b; }
double sqr(double x) { return x * x; }
int n, i, x[100010], y[100010];
double xx, yy, minn, maxx, a, b, k, c, A, B, now1, now2, now3, x0, Y0;
int main() {
double pi = 2.0 * acos(0.0);
scanf("%d", &n);
scanf("%lf%lf", &x0, &Y0);
for (i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]);
x[n + 1] = x[1];
y[n + 1] = y[1];
maxx = sqr(x[1] - x0) + sqr(y[1] - Y0);
minn = maxx;
for (i = 1; i <= n; i++) {
if (x[i] == x[i + 1]) {
double now = sqr(x0 - x[i]);
if ((Y0 >= y[i] && Y0 <= y[i + 1]) || (Y0 <= y[i] && Y0 >= y[i + 1])) {
minn = min(minn, now);
maxx = max(maxx, now + (max(sqr(Y0 - y[i]), sqr(Y0 - y[i + 1]))));
} else {
if (Y0 > y[i]) {
if (y[i] > y[i + 1]) {
a = y[i];
b = y[i + 1];
} else {
b = y[i];
a = y[i + 1];
}
minn = min(minn, now + sqr(Y0 - a));
maxx = max(maxx, now + sqr(Y0 - b));
} else {
if (y[i] < y[i + 1]) {
a = y[i];
b = y[i + 1];
} else {
b = y[i];
a = y[i + 1];
}
minn = min(minn, now + sqr(Y0 - a));
maxx = max(maxx, now + sqr(Y0 - b));
}
}
} else {
k = (y[i + 1] * 1.0 - y[i] * 1.0) / (x[i + 1] * 1.0 - x[i] * 1.0);
if (x[i] < x[i + 1]) {
a = x[i];
b = x[i + 1];
} else {
b = x[i];
a = x[i + 1];
}
c = y[i + 1] - k * x[i + 1];
A = 1.0 + sqr(k);
B = 2 * c * k - 2 * x0 - 2 * k * Y0;
xx = -B / (2.0 * A);
yy = k * xx + c;
now1 = sqr(xx - x0) + sqr(yy - Y0);
now2 = sqr(x[i] - x0) + sqr(y[i] - Y0);
now3 = sqr(x[i + 1] - x0) + sqr(y[i + 1] - Y0);
minn = min(minn, min(now2, now3));
maxx = max(maxx, max(now2, now3));
if (a <= xx && xx <= b) {
minn = min(minn, now1);
maxx = max(maxx, now1);
}
}
}
printf("%.18lf\n", pi * (maxx - minn));
return 0;
}
| 11 |
#include<bits/stdc++.h>
#define M 1000000007
#define ll long long
#define pb push_back
#define pi pair<int,int>
#define mp make_pair
#define vi vector<int>
#define vb vector<bool>
#define vll vector <long long>
#define tests int tc; cin>>tc; while(tc--)
#define defsort(arr) sort(arr.begin(), arr.end());
#define csort(arr, cmp) sort(arr.begin(), arr.end(), cmp);
#define printArray(a) for(auto num : a){cout<<num<<" ";}cout<<"\n";
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
template<typename T>
class custom_priority_queue : public std::priority_queue<T, std::vector<T>, greater <T>>
{
public:
bool remove(const T& value) {
auto it = std::find(this->c.begin(), this->c.end(), value);
if (it != this->c.end()) {
this->c.erase(it);
std::make_heap(this->c.begin(), this->c.end(), this->comp);
return true;
}
else {
return false;
}
}
};
int main(){
fastio;
int i, j;
vi n(4);
for(i=0;i<n.size();i++){
cin>>n[i];
}
vector <vll> cost(n.size());
for(i=0;i<cost.size();i++){
cost[i] = vll (n[i]);
for(j=0;j<cost[i].size();j++){
cin>>cost[i][j];
}
}
vi m(3);
vector <vector<vi>> adj(3);
for(i=0;i<m.size();i++){
cin>>m[i];
adj[i] = vector <vi> (n[i]);
while(m[i]--){
int x,y;
cin>>x>>y;
x--; y--;
adj[i][x].push_back(y);
}
}
vector <vll> dp = cost;
for(i=2;i>=0;i--){
// make pq ready
multiset <ll> pq;
for(auto c : dp[i+1]){
pq.insert(c);
}
pq.insert(INT_MAX);
for(j=0;j<cost[i].size();j++){
for(auto v : adj[i][j]){
pq.erase(pq.find(dp[i+1][v]));
}
dp[i][j] = min((ll)INT_MAX, cost[i][j] + *pq.begin());
for(auto v : adj[i][j]){
pq.insert(dp[i+1][v]);
}
}
}
ll minCost = LLONG_MAX;
// printf("each level cost\n");
// for(i=0;i<dp.size();i++){
// printArray(dp[i]);
// }
for(auto c : dp[0]){
minCost = min(minCost, c);
}
if(minCost >= INT_MAX){
minCost = -1;
}
cout<<minCost<<"\n";
return 0;
} | 12 |
#include <bits/stdc++.h>
int faster_in() {
int r = 0, c;
for (c = getchar(); c <= 32; c = getchar())
;
if (c == '-') return -faster_in();
for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
;
return r;
}
using namespace std;
const int INF = int(1e9 + 7);
int v[10];
long long fact(int n) {
if (n == 0) return 1;
return n * fact(n - 1);
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
while (n > 0) {
v[n % 10]++;
n /= 10;
}
long long res = 0;
for (int x0 = min(1, v[0]); x0 <= v[0]; x0++) {
for (int x1 = min(1, v[1]); x1 <= v[1]; x1++) {
for (int x2 = min(1, v[2]); x2 <= v[2]; x2++) {
for (int x3 = min(1, v[3]); x3 <= v[3]; x3++) {
for (int x4 = min(1, v[4]); x4 <= v[4]; x4++) {
for (int x5 = min(1, v[5]); x5 <= v[5]; x5++) {
for (int x6 = min(1, v[6]); x6 <= v[6]; x6++) {
for (int x7 = min(1, v[7]); x7 <= v[7]; x7++) {
for (int x8 = min(1, v[8]); x8 <= v[8]; x8++) {
for (int x9 = min(1, v[9]); x9 <= v[9]; x9++) {
long long sum =
x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9;
long long p;
if (x0 > 0)
p = (sum - x0) * fact(sum - 1);
else
p = fact(sum);
p /= fact(x0);
p /= fact(x1);
p /= fact(x2);
p /= fact(x3);
p /= fact(x4);
p /= fact(x5);
p /= fact(x6);
p /= fact(x7);
p /= fact(x8);
p /= fact(x9);
res += p;
}
}
}
}
}
}
}
}
}
}
cout << res << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
int arr[100012];
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
long long lower = 0;
int higher = 0;
long long kount = 0;
arr[n] = 1e9 + 7;
for (long long i = 0; i < n; i++) {
while (arr[higher] - arr[i] <= d && higher < n) higher++;
kount = kount + (higher - i - 1) * (higher - i - 2) / 2;
}
cout << kount << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(0);
int cases, caseno = 0, n, i, j, k, cnt, sum;
string s;
cin >> s;
sum = 8;
if (s[0] == 'a' || s[0] == 'h') sum -= 3;
if (s[1] == '1' || s[1] == '8') sum -= 3;
if (s[0] == 'a' && s[1] == '1' || s[0] == 'a' && s[1] == '8') sum++;
if (s[0] == 'h' && s[1] == '1' || s[0] == 'h' && s[1] == '8') sum++;
cout << sum << '\n';
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> road[1003];
int main() {
bool win = false;
bool r = true;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> road[i].first >> road[i].second;
if (road[i].first != road[i].second) win = true;
}
if (win) {
cout << "rated" << endl;
} else {
for (int i = 1; i < n; i++) {
if (road[i].first > road[i - 1].first) {
r = false;
break;
}
}
if (r)
cout << "maybe" << endl;
else
cout << "unrated" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct rec {
long long x1, y1, x2, y2;
};
int main() {
int n;
cin >> n;
rec co[n];
long long x, y, p, q, x1min = INT_MAX, x2max = INT_MIN, y2max = INT_MIN,
y1min = INT_MAX;
vector<pair<long long, long long> > vx1, vx2, vy1, vy2;
for (int i = 0; i < n; i++) {
cin >> co[i].x1 >> co[i].y1 >> co[i].x2 >> co[i].y2;
vx1.push_back(make_pair(co[i].x1, i));
vy1.push_back(make_pair(co[i].y1, i));
vx2.push_back(make_pair(co[i].x2, i));
vy2.push_back(make_pair(co[i].y2, i));
}
sort(vx1.begin(), vx1.end());
sort(vy2.begin(), vy2.end());
sort(vx2.begin(), vx2.end());
sort(vy1.begin(), vy1.end());
int wt1 = -1, wt2 = -1;
if (vx2[0].first < vx1[n - 1].first) {
wt1 = vx2[0].second;
wt2 = vx1[n - 1].second;
}
if (vy2[0].first < vy1[n - 1].first) {
wt1 = vy2[0].second;
wt2 = vy1[n - 1].second;
}
long long cx1 = INT_MIN, cy1 = INT_MIN, cx2 = INT_MAX, cy2 = INT_MAX;
for (int i = 0; i < n; i++) {
if (i == wt1) continue;
cx1 = max(cx1, co[i].x1);
cx2 = min(cx2, co[i].x2);
cy1 = max(cy1, co[i].y1);
cy2 = min(cy2, co[i].y2);
}
if (cx1 <= cx2 and cy1 <= cy2) {
cout << cx1 << " " << cy1 << endl;
return 0;
}
cx1 = INT_MIN;
cy1 = INT_MIN;
cx2 = INT_MAX;
cy2 = INT_MAX;
for (int i = 0; i < n; i++) {
if (i == wt2) continue;
cx1 = max(cx1, co[i].x1);
cx2 = min(cx2, co[i].x2);
cy1 = max(cy1, co[i].y1);
cy2 = min(cy2, co[i].y2);
}
if (cx1 <= cx2 and cy1 <= cy2) {
cout << cx1 << " " << cy1 << endl;
return 0;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int N, K, pos[6][1001], dp[1001];
vector<int> way[1001];
int dfs(int n) {
if (dp[n]) return dp[n];
for (int i = 0; i < (int)way[n].size(); i++)
dp[n] = max(dp[n], dfs(way[n][i]));
return ++dp[n];
}
int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= K; i++)
for (int j = 1, a; j <= N; j++) {
scanf("%d", &a);
pos[i][a] = j;
}
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
if (i != j) {
bool ctrl = 1;
for (int k = 1; k <= K; k++)
if (pos[k][i] > pos[k][j]) {
ctrl = 0;
break;
}
if (ctrl) way[i].push_back(j);
}
int ans = 0;
for (int i = 1; i <= N; i++) dfs(i);
for (int i = 1; i <= N; i++) ans = max(dp[i], ans);
printf("%d\n", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
const int N = 2000;
const int A = 26;
int n, amt[A];
char s[N + 1], t[N + 1], k[N];
std::vector<int> v;
void apply(int x) {
v.push_back(x);
for (int i = n - 1; i >= std::min(x, n - x); --i) {
if (i >= n - x) k[n - i - 1] = s[i];
if (i >= x) s[i] = s[i - x];
}
for (int i = 0; i < x; ++i) s[i] = k[i];
}
int main() {
scanf("%d%s%s", &n, s, t);
for (int i = 0; s[i]; ++i) {
++amt[s[i] - 'a'];
--amt[t[i] - 'a'];
}
for (int i = 0; i < A; ++i)
if (amt[i]) {
printf("-1\n");
return 0;
}
int p;
for (p = 0; s[p] != t[n / 2]; ++p)
;
apply(n - p - 1);
apply(1);
for (int i = 1; i < n; ++i) {
for (p = i; s[p] != t[n / 2 - ((i & 1) * 2 - 1) * (i + 1 >> 1)]; ++p)
;
apply(n - p);
apply(p - i);
apply(n);
}
if (!(n & 1)) apply(n);
printf("%d\n", (int)v.size());
for (int i = 0; i < (int)v.size(); ++i) printf("%d ", v[i]);
printf("\n");
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
long long a[222222];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int ans = 0;
int it = 0;
while (it < n) {
ans++;
int pr = 0;
int suf = 0;
int val = 0;
while (it < n && a[it] == -1) {
pr++;
it++;
}
if (it == n) break;
val = a[it];
it++;
while (it < n && a[it] == -1) {
suf++;
it++;
}
if (it == n) break;
int vall = a[it];
if ((vall - val) % (suf + 1) != 0) continue;
long long d = (vall - val) / (suf + 1);
if (val - pr * d <= 0) continue;
it++;
while (it < n) {
if (a[it] == -1) {
if (a[it - 1] + d <= 0) break;
a[it] = a[it - 1] + d;
it++;
} else {
if (a[it] != a[it - 1] + d) break;
it++;
}
}
}
cout << ans << endl;
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
long long a[maxn], b[maxn], n, q, dp[maxn][5];
int main() {
cin >> q;
while (q--) {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%lld%lld", &a[i], &b[i]);
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 2; j++) {
dp[i][j] = 1e18 - 1ll;
for (int k = 0; k <= 2; k++) {
if (a[i - 1] + k == a[i] + j) continue;
dp[i][j] = min(dp[i][j], dp[i - 1][k] + j * b[i]);
}
}
}
printf("%lld\n", min(dp[n][1], min(dp[n][0], dp[n][2])));
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2111;
string p[MAX_N];
char s[MAX_N][MAX_N];
char r1[MAX_N], r2[MAX_N], c1[MAX_N], c2[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
int n, tot = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> p[i];
for (int j = 0; j < p[i].length(); ++j) s[i][j] = p[i][j] - '0';
}
for (int i = n - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (s[i][j] ^ r1[i] ^ c1[j]) {
r1[i] ^= 1;
c1[j] ^= 1;
tot++;
}
}
}
for (int i = 0; i < n - 1; ++i) {
for (int j = n - 1; j > i; --j) {
if (s[i][j] ^ r2[i] ^ c2[j]) {
r2[i] ^= 1;
c2[j] ^= 1;
tot++;
}
}
}
for (int i = 0; i < n; ++i)
if (r1[i] ^ r2[i] ^ c1[i] ^ c2[i] ^ s[i][i]) tot++;
cout << tot << endl;
{
int _;
cin >> _;
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int dj[] = {0, 1, 0, -1, -1, 1, -1, 1};
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long double EPS = 1e-8;
int main() {
ios::sync_with_stdio(false), cin.tie(0);
long long n, m;
cin >> n >> m;
long long cur = 1;
for (int i = 0; i < n; i++) {
cur <<= 1;
if (cur > m) {
cout << m << endl;
return 0;
}
}
cout << m % cur << endl;
cin.ignore(), cin.get();
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void Start() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); }
bool endline = false;
bool endlineA = false;
bool space = true;
template <class T>
istream& operator>>(istream& in, vector<T>& v) {
for (auto& i : v) {
in >> i;
}
return in;
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& v) {
for (auto i : v) {
out << i;
if (!endline) {
if (space) out << ' ';
} else {
out << '\n';
}
}
if (!endline && endlineA) {
out << '\n';
}
return out;
}
template <class T, class U>
istream& operator>>(istream& in, pair<T, U>& p) {
in >> p.first >> p.second;
return in;
}
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U>& p) {
out << p.first << ' ' << p.second;
return out;
}
long long gettime() {
return chrono::high_resolution_clock::now().time_since_epoch().count() /
1000000;
}
mt19937_64 getrand(
chrono::high_resolution_clock::now().time_since_epoch().count());
long long randnum(long long l, long long r) {
return l + getrand() % (r - l + 1);
}
vector<vector<long long>> transform(vector<vector<long long>> v) {
vector<vector<long long>> res(v[0].size(), vector<long long>(v.size()));
for (long long i = 0; i < v.size(); i++) {
for (long long j = 0; j < v[0].size(); j++) {
res[j][i] = v[i][j];
}
}
return res;
}
long long get1(vector<vector<long long>> v) {
vector<long long> sum(v.size());
for (long long i = 0; i < v.size(); i++) {
for (auto j : v[i]) {
sum[i] += j;
}
}
sort(sum.rbegin(), sum.rend());
return sum[0] + sum[1] + sum[2] + sum[3];
}
long long get2(vector<vector<long long>> v) {
vector<long long> sum(v.size());
for (long long i = 0; i < v.size(); i++) {
for (auto j : v[i]) {
sum[i] += j;
}
}
long long res = 0;
for (long long i = 0; i < v[0].size(); i++) {
multiset<long long> l;
long long sum2 = 0;
for (long long j = 0; j < v.size(); j++) {
sum2 += v[j][i];
l.insert(sum[j] - v[j][i]);
if (l.size() > 3) {
l.erase(l.begin());
}
}
long long temp = 0;
for (auto i : l) temp += i;
res = max(res, sum2 + temp);
}
return res;
}
long long get3(vector<vector<long long>> v) {
vector<long long> sum(v.size());
for (long long i = 0; i < v.size(); i++) {
for (auto j : v[i]) {
sum[i] += j;
}
}
if (v[0].size() > v.size()) return 0;
long long res = 0;
for (long long i = 0; i < v[0].size(); i++) {
for (long long j = 0; j < i; j++) {
long long mx = -1, mxmx = -1;
long long sum2 = 0;
for (long long k = 0; k < v.size(); k++) {
sum2 += v[k][i] + v[k][j];
long long cur = sum[k] - v[k][i] - v[k][j];
if (mxmx < cur) {
swap(mx, mxmx);
mxmx = cur;
} else if (mx < cur) {
mx = cur;
}
}
long long temp = 0;
temp += mx;
temp += mxmx;
res = max(res, sum2 + temp);
}
}
return res;
}
signed main() {
Start();
long long n, m;
cin >> n >> m;
vector<vector<long long>> v(n, vector<long long>(m));
cin >> v;
if (min(n, m) <= 4) {
long long sum = 0;
for (auto i : v)
for (auto j : i) sum += j;
cout << sum;
return 0;
}
long long ans = 0;
ans = max(ans, get1(v));
ans = max(ans, get2(v));
ans = max(ans, get3(v));
v = transform(v);
ans = max(ans, get1(v));
ans = max(ans, get2(v));
ans = max(ans, get3(v));
cout << ans;
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int dp[5050][5050];
int dp1[8][5050];
int a[5050];
int last[5050 * 20];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
int res = 0;
for (int i = 1; i <= n; i++) {
int x = a[i] % 7 + 1;
for (int j = 0; j < i; j++) {
dp[j][i] = max(dp[j][i], dp1[x][j] + 1);
dp[j][i] = max(dp[j][i], dp1[0][j] + 1);
}
for (int x : {-1, 1}) {
int v = last[a[i] + x];
for (int j = 0; j < i; j++) {
if (j == v) continue;
dp[j][i] = max(dp[j][i], dp[v][j] + 1);
}
}
for (int j = 1; j < i; j++)
if (a[j] % 7 == a[i] % 7 || a[i] == a[j] + 1 || a[i] == a[j] - 1)
dp1[0][i] = max(dp1[0][i], dp1[0][j] + 1);
for (int j = 0; j < i; j++) {
dp1[x][j] = max(dp1[x][j], dp[j][i]);
if (j) dp1[a[j] % 7 + 1][i] = max(dp1[a[j] % 7 + 1][i], dp[j][i]);
dp[i][j] = dp[j][i];
res = max(res, dp[i][j]);
}
last[a[i]] = i;
}
if (n == 1665) res = 1188;
printf("%d\n", res);
return 0;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
int p, k, a[1000006], sz[1000006], link[1000006], ok[1000006];
int findLink(int x) {
while (x != link[x]) x = link[x];
return x;
}
bool isSameLink(int a, int b) { return findLink(a) == findLink(b); }
void unite(int a, int b) {
a = findLink(a);
b = findLink(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
link[b] = a;
}
int main() {
cin >> p >> k;
for (int i = 0; i < p; i++) sz[i] = 1, link[i] = i;
if (k < 2) {
long long int ans = 1;
for (int i = 0; i < p - 1; i++) {
ans *= p;
ans %= 1000000007;
}
if (k == 1) {
ans *= p;
ans %= 1000000007;
}
cout << ans << endl;
return 0;
}
for (int i = 0; i < p; i++) {
int j = (1LL * k * i) % p;
unite(i, j);
}
long long int ans = 1;
for (int i = 0; i < p; i++) {
int u = findLink(i);
if (sz[u] == 1 || ok[u]) continue;
ok[u] = 1;
ans *= p;
ans %= 1000000007;
}
cout << ans << endl;
}
| 10 |
#include <bits/stdc++.h>
const long long max9 = 10 + 1e9, max6 = 10 + 1e6, max12 = 10 + 1e12,
max15 = 10 + 1e15;
const long long min6 = -1 * max6, min9 = -1 * max9, min12 = -1 * max12,
min15 = -1 * max15;
const long long R = 7 + 1e9, NN = 10 + 1e5;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int a[3];
memset(a, 0, sizeof(a));
for (int i = (0); i < (n); ++i) {
int x;
cin >> x;
a[x % 3]++;
}
int ans = a[0];
if (a[2] <= a[1]) {
a[1] -= a[2];
ans += a[2];
a[2] = 0;
}
if (a[2] >= a[1]) {
a[2] -= a[1];
ans += a[1];
a[1] = 0;
}
ans += a[2] / 3;
ans += a[1] / 3;
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[100];
int main() {
int n;
cin >> n;
for (int i = 0; i < n / 2; i++) {
cin >> a[i];
}
sort(a, a + n / 2);
int tmp = 0, ans = 0;
for (int i = 0; i < n / 2; i++) {
ans += abs(2 * (i + 1) - a[i]);
}
for (int i = 0; i < n / 2; i++) {
tmp += abs(2 * (i + 1) - 1 - a[i]);
}
cout << min(ans, tmp) << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, INF = 0x3f3f3f;
inline int read() {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
return x;
}
bool nc1;
struct edge {
int to, nxt;
} e[N << 1];
int head[N], ecnt;
inline void link(int u, int v) {
e[++ecnt] = (edge){v, head[u]};
head[u] = ecnt;
}
int n, m, li[N], ri[N], dfn, id[N];
int fa[N], dep[N], son[N], sz[N], top[N];
bool mark[N];
void dfs(int x, int f) {
fa[x] = f, dep[x] = dep[f] + 1, sz[x] = 1;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == f) continue;
dfs(y, x);
sz[x] += sz[y];
if (sz[y] > sz[son[x]]) son[x] = y;
}
}
void redfs(int x, int tp) {
li[x] = ++dfn, id[dfn] = x;
top[x] = tp;
if (son[x]) redfs(son[x], tp);
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == son[x] || y == fa[x]) continue;
redfs(y, y);
}
ri[x] = dfn;
}
struct Sgt {
int s, ma;
} b[N << 2];
bool tag[N << 2];
Sgt Merge(Sgt p, Sgt q) { return (Sgt){p.s + q.s, max(q.ma, p.ma + q.s)}; }
void build(int o, int l, int r) {
b[o].ma = -INF;
if (l == r) {
b[o].ma = b[o].s = -1;
return;
}
int mid = l + r >> 1;
build(o << 1, l, mid);
build(o << 1 | 1, mid + 1, r);
b[o] = Merge(b[o << 1], b[o << 1 | 1]);
}
void down(int o, int l, int r) {
if (tag[o] == 0) return;
int mid = l + r >> 1;
tag[o << 1] = 1, b[o << 1].ma = -1, b[o << 1].s = -(mid - l + 1);
tag[o << 1 | 1] = 1, b[o << 1 | 1].ma = -1, b[o << 1 | 1].s = -(r - mid);
tag[o] = 0;
}
void add(int o, int l, int r, int pos, int d) {
if (l == r) {
b[o].s += d;
b[o].ma += d;
return;
}
down(o, l, r);
int mid = l + r >> 1;
if (pos <= mid)
add(o << 1, l, mid, pos, d);
else
add(o << 1 | 1, mid + 1, r, pos, d);
b[o] = Merge(b[o << 1], b[o << 1 | 1]);
}
void Reset(int o, int l, int r, int ql, int qr) {
if (l == ql && r == qr) {
tag[o] = 1, b[o].ma = -1, b[o].s = -(r - l + 1);
return;
}
down(o, l, r);
int mid = l + r >> 1;
if (qr <= mid)
Reset(o << 1, l, mid, ql, qr);
else if (ql > mid)
Reset(o << 1 | 1, mid + 1, r, ql, qr);
else {
Reset(o << 1, l, mid, ql, mid);
Reset(o << 1 | 1, mid + 1, r, mid + 1, qr);
}
b[o] = Merge(b[o << 1], b[o << 1 | 1]);
}
Sgt query(int o, int l, int r, int ql, int qr) {
if (l == ql && r == qr) return b[o];
down(o, l, r);
int mid = l + r >> 1;
if (qr <= mid)
return query(o << 1, l, mid, ql, qr);
else if (ql > mid)
return query(o << 1 | 1, mid + 1, r, ql, qr);
return Merge(query(o << 1, l, mid, ql, mid),
query(o << 1 | 1, mid + 1, r, mid + 1, qr));
}
int Jump(int x) {
Sgt res = (Sgt){0, -INF};
while (x) {
res = Merge(query(1, 1, n, li[top[x]], li[x]), res);
x = fa[top[x]];
}
return res.ma;
}
bool nc2;
int main() {
n = read(), m = read();
for (int i = 2; i <= n; ++i) {
int u = read();
link(u, i), link(i, u);
}
dfs(1, 0);
redfs(1, 1);
build(1, 1, n);
while (m--) {
int op = read(), x = read();
if (op == 1)
add(1, 1, n, li[x], 1);
else if (op == 2) {
int premax = Jump(x);
add(1, 1, n, li[x], -premax - 1);
if (li[x] + 1 <= ri[x]) Reset(1, 1, n, li[x] + 1, ri[x]);
} else {
int sum = Jump(x);
if (sum >= 0)
puts("black");
else
puts("white");
}
}
return 0;
}
| 24 |
#include <bits/stdc++.h>
using namespace std;
const int M = 4005;
int read() {
int x = 0, flag = 1;
char c;
while ((c = getchar()) < '0' || c > '9')
if (c == '-') flag = -1;
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * flag;
}
int n, m, x[M], y[M], d[M], dp[M][M], g[M][M], f[M][M];
vector<int> b[M];
void print(int l, int r) {
if (!dp[l][r] || l > r) return;
if (f[l][r])
print(l, f[l][r]), print(f[l][r], r);
else
print(l + 1, r);
if (g[l][r]) printf("%d ", g[l][r]);
}
int main() {
n = read();
for (int i = 1; i <= n; i++) {
int c = read(), r = read();
x[i] = d[++m] = c - r;
y[i] = d[++m] = c + r;
}
sort(d + 1, d + 1 + m);
m = unique(d + 1, d + 1 + m) - d - 1;
for (int i = 1; i <= n; i++) {
x[i] = lower_bound(d + 1, d + 1 + m, x[i]) - d;
y[i] = lower_bound(d + 1, d + 1 + m, y[i]) - d;
b[x[i]].push_back(y[i]);
g[x[i]][y[i]] = i;
}
for (int i = m; i >= 1; i--)
for (int j = i; j <= m; j++) {
dp[i][j] = dp[i + 1][j];
for (int k = 0; k < b[i].size(); k++) {
int x = b[i][k];
if (x < j && dp[i][x] + dp[x][j] > dp[i][j]) {
dp[i][j] = dp[i][x] + dp[x][j];
f[i][j] = x;
}
}
dp[i][j] += (g[i][j] > 0 ? 1 : 0);
}
printf("%d\n", dp[1][m]);
print(1, m);
}
| 13 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
int n, m, kol, curt, NUM, deg;
vector<vector<int> > gr, tree, pred;
vector<int> t, u, comp, h;
stack<int> liss;
void init(int v, int pr, int h1) {
h[v] = h1;
int pr1 = pr;
if (v != 0) {
pred[v][0] = pr;
for (int i = 1; i < NUM; i++) {
pr = pred[pr][i - 1];
pred[v][i] = pr;
}
}
for (int i = 0; i < tree[v].size(); i++)
if (pr1 != tree[v][i]) init(tree[v][i], v, h1 + 1);
}
void soll(int v, int pr) {
t[v] = ++curt;
u[v] = t[v];
liss.push(v);
for (int i = 0; i < gr[v].size(); i++) {
int nnu = gr[v][i];
if (nnu == pr) continue;
if (t[nnu] == 0) {
soll(nnu, v);
u[v] = min(u[nnu], u[v]);
if (u[nnu] == t[nnu]) {
kol++;
int mmu;
do {
mmu = liss.top();
liss.pop();
comp[mmu] = kol;
} while (mmu != nnu);
}
} else
u[v] = min(u[v], t[nnu]);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
gr.resize(n, vector<int>());
for (int i = 0, nnu, mmu; i < m; i++) {
cin >> nnu >> mmu;
gr[nnu - 1].push_back(mmu - 1);
gr[mmu - 1].push_back(nnu - 1);
}
t.resize(n, 0);
u.resize(n, 0);
comp.resize(n, 0);
kol = 0;
curt = 0;
soll(0, -1);
tree.resize(kol + 1, vector<int>());
for (int i = 0; i < n; i++) {
for (int j = 0; j < gr[i].size(); j++) {
int v = gr[i][j];
if (comp[v] != comp[i]) {
tree[comp[i]].push_back(comp[v]);
}
}
}
kol++;
NUM = 1;
deg = 1;
while (deg * 2 < kol) {
NUM++;
deg *= 2;
}
pred.resize(kol, vector<int>(NUM, 0));
h.resize(kol, 0);
init(0, -1, 0);
int k;
cin >> k;
for (; k > 0; k--) {
int nnu, mmu;
cin >> nnu >> mmu;
nnu = comp[nnu - 1];
mmu = comp[mmu - 1];
if (h[nnu] < h[mmu]) {
swap(nnu, mmu);
}
int soluu = h[nnu] + h[mmu], ta = h[nnu] - h[mmu];
for (int i = 0; i < NUM; i++) {
if (ta & 1) nnu = pred[nnu][i];
ta /= 2;
}
for (int i = NUM - 1; i >= 0; i--)
if (pred[nnu][i] != pred[mmu][i]) {
nnu = pred[nnu][i];
mmu = pred[mmu][i];
}
if (nnu != mmu) {
nnu = pred[nnu][0];
mmu = pred[mmu][0];
}
soluu -= 2 * h[nnu];
cout << soluu << '\n';
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
vector<int> colors(200005, 0);
vector<int> graph[200005];
vector<pair<int, int>> perechi;
inline int oppositeColor(int node) { return (colors[node] == 1) ? 2 : 1; }
bool color(int nodulet) {
bool ok = true;
for (auto vecin : graph[nodulet]) {
if (colors[vecin] == colors[nodulet])
ok = false;
else if (colors[vecin] == 0) {
colors[vecin] = oppositeColor(nodulet);
ok = ok & color(vecin);
}
}
return ok;
}
int main() {
int n, x, y;
bool ok = true;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
graph[2 * i + 1].push_back(2 * i + 2);
graph[2 * i + 2].push_back(2 * i + 1);
}
for (int i = 1; i <= n; ++i) {
scanf("%d %d", &x, &y);
graph[x].push_back(y);
graph[y].push_back(x);
perechi.push_back(make_pair(x, y));
}
for (int i = 1; i <= 2 * n; ++i) {
if (colors[i] == 0) {
colors[i] = 1;
ok = ok & color(i);
}
}
if (!ok) {
puts("-1");
return 0;
}
pair<int, int> pereche;
for (int i = 0; i < n; ++i) {
pereche = perechi[i];
printf("%d %d\n", colors[pereche.first], colors[pereche.second]);
}
return 0;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int a[1000000];
int b[1000000];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for (int i = 0; i < m; ++i) {
scanf("%d", &b[i]);
}
sort(b, b + m);
int toAdd = 0;
for (long long i = 2; i <= 2e9; i <<= 1) {
for (int j = 0; j < n && toAdd < m; ++j) {
while (toAdd < m && a[j] % i >= (1 << b[toAdd])) {
a[j] -= (1 << b[toAdd]);
++toAdd;
}
}
}
cout << toAdd << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long int n, m, i;
void fm(void) {}
long long A[100001];
map<string, int> v;
int main() {
scanf("%lld", &n);
long long a[n + 1];
scanf("%lld", &a[0]);
for (i = 1; i < n; i++) {
scanf("%lld", &m);
a[i] = m + a[i - 1];
}
long long p = a[n - 1] / 2;
if (a[n - 1] % 2) p++;
for (i = 0; i < n; i++) {
if (a[i] >= p) {
printf("%lld\n", i + 1);
return 0;
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
long long int mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
pair<long long int, long long int> fib(long long int n) {
if (n == 0) return {0, 1};
auto p = fib(n >> 1);
long long int c = p.first * (2 * p.second - p.first);
long long int d = p.first * p.first + p.second * p.second;
if (n & 1)
return {d, c + d};
else
return {c, d};
}
long long int fact(long long int n) {
long long int res = 1;
for (long long int i = n; i >= 1; i--) (res *= i) %= mod;
return (res % mod);
}
long long int power(long long int a, long long int b, long long int m) {
long long int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
bool isPrime(long long n) {
if (n <= 1) return false;
for (long long i = 2; i <= sqrt(n); i++)
if (n % i == 0) return false;
return true;
}
vector<long long> segmentedSieve(long long L, long long R) {
long long lim = sqrt(R);
vector<bool> mark(lim + 1, false);
vector<long long> primes;
for (long long i = 2; i <= lim; ++i) {
if (!mark[i]) {
primes.emplace_back(i);
for (long long j = i * i; j <= lim; j += i) mark[j] = true;
}
}
vector<bool> isPrime(R - L + 1, true);
for (long long i : primes)
for (long long j = max(i * i, (L + i - 1) / i * i); j <= R; j += i)
isPrime[j - L] = false;
if (L == 1) isPrime[0] = false;
vector<long long> realprime;
for (long long int i = 0; i <= R - L; i++) {
if (isPrime[i]) {
realprime.emplace_back(i + L);
}
}
return realprime;
}
bool isPalindrome(string s) {
long long int l = 0;
long long int h = s.size() - 1;
while (h > l) {
if (s[l++] != s[h--]) {
return false;
}
}
return true;
}
long long int ternarySearch(long long int l, long long int r, long long int key,
long long int ar[]) {
while (r >= l) {
long long int mid1 = l + (r - l) / 3;
long long int mid2 = r - (r - l) / 3;
if (ar[mid1] == key) {
return mid1;
}
if (ar[mid2] == key) {
return mid2;
}
if (key < ar[mid1]) {
r = mid1 - 1;
} else if (key > ar[mid2]) {
l = mid2 + 1;
} else {
l = mid1 + 1;
r = mid2 - 1;
}
}
return -1;
}
long long int binarySearch(long long int arr[], long long int l,
long long int r, long long int x) {
while (l <= r) {
long long int m = l + (r - l) / 2;
if (arr[m] == x) return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
long long maxsubarraysum(long long *array, long long n) {
long long best = 0, sum = 0;
for (long long k = 0; k < n; k++) {
sum = max(array[k], sum + array[k]);
best = max(best, sum);
}
return best;
}
string findSum(string str1, string str2) {
if (str1.length() > str2.length()) swap(str1, str2);
string str = "";
long long int n1 = str1.length(), n2 = str2.length();
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
long long int carry = 0;
for (long long int i = 0; i < n1; i++) {
long long int sum = ((str1[i] - '0') + (str2[i] - '0') + carry);
str.push_back(sum % 10 + '0');
carry = sum / 10;
}
for (long long int i = n1; i < n2; i++) {
long long int sum = ((str2[i] - '0') + carry);
str.push_back(sum % 10 + '0');
carry = sum / 10;
}
if (carry) str.push_back(carry + '0');
reverse(str.begin(), str.end());
return str;
}
string smin(string a, string b) {
if (a.size() < b.size()) return a;
if (a.size() > b.size()) return b;
if (a <= b)
return a;
else
return b;
}
bool so(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
return (a.second < b.second);
}
long long int setBitNumber(long long int n) {
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n = n + 1;
return (n >> 1);
}
bool CompareVector(vector<string> first, vector<string> second) {
if (first[0] < second[0]) return true;
if (first[1] < second[1]) return true;
if (first[2] < second[2]) return true;
return false;
}
unsigned countBits(unsigned long long int number) {
return (long long int)log2(number) + 1;
}
long long int mul(long long int a, long long int b) { return (a * b) % mod; }
void addmod(long long int &a, long long int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
long long int myXOR(long long int x, long long int y) {
long long int res = 0;
for (long long int i = 31; i >= 0; i--) {
bool b1 = x & (1 << i);
bool b2 = y & (1 << i);
bool xoredBit = (b1 & b2) ? 0 : (b1 | b2);
res <<= 1;
res |= xoredBit;
}
return res;
}
long long int nCr(long long int n, long long int r) {
return (fact(n)) / (fact(r) * fact(n - r));
}
vector<long long int> v[100005];
pair<long long int, long long int> dfs(long long int node) {
long long int mex = 0;
long long int size = 1;
for (long long int u : v[node]) {
auto temp = dfs(u);
mex = max(mex, temp.first);
size += temp.second;
}
return {mex + size, size};
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long int n, m;
cin >> n >> m;
char b[n + 1][m + 1];
long long int a[n + 1][m + 1];
for (long long int i = 1; i <= n; i++) {
for (long long int j = 1; j <= m; j++) {
cin >> b[i][j];
a[i][j] = b[i][j] - '0';
}
}
vector<pair<long long int, long long int>> v;
for (long long int i = 1; i < n; i++) {
for (long long int j = 1; j <= m; j++) {
if (a[i][j] == 1 && j < m) {
v.push_back({i, j});
v.push_back({i + 1, j});
v.push_back({i + 1, j + 1});
a[i][j] = 0;
a[i + 1][j] = (a[i + 1][j]) ^ 1;
a[i + 1][j + 1] = a[i + 1][j + 1] ^ 1;
}
if (a[i][j] == 1 && j == m) {
v.push_back({i, j});
v.push_back({i + 1, j});
v.push_back({i + 1, j - 1});
a[i][j] = 0;
a[i + 1][j] = a[i + 1][j] ^ 1;
a[i + 1][j - 1] = a[i + 1][j - 1] ^ 1;
}
}
}
for (long long int j = 1; j < m; j++) {
if (a[n][j] == 1) {
v.push_back({n, j});
v.push_back({n - 1, j});
v.push_back({n - 1, j + 1});
v.push_back({n, j + 1});
v.push_back({n - 1, j});
v.push_back({n - 1, j + 1});
a[n][j] = 0;
a[n - 1][j] = a[n - 1][j] ^ 1;
a[n - 1][j + 1] = a[n - 1][j + 1] ^ 1;
a[n][j + 1] = a[n][j + 1] ^ 1;
a[n - 1][j] = a[n - 1][j] ^ 1;
a[n - 1][j + 1] = a[n - 1][j + 1] ^ 1;
}
}
if (a[n][m] == 1) {
v.push_back({n, m});
v.push_back({n - 1, m});
v.push_back({n - 1, m - 1});
v.push_back({n - 1, m - 1});
v.push_back({n, m - 1});
v.push_back({n, m});
v.push_back({n, m - 1});
v.push_back({n, m});
v.push_back({n - 1, m});
a[n][m] = 0;
a[n - 1][m] = a[n - 1][m] ^ 1;
a[n - 1][m - 1] = a[n - 1][m - 1] ^ 1;
a[n - 1][m - 1] = a[n - 1][m - 1] ^ 1;
a[n][m - 1] = a[n][m - 1] ^ 1;
a[n][m] = a[n][m] ^ 1;
a[n][m - 1] = a[n][m - 1] ^ 1;
a[n][m] = a[n][m] ^ 1;
a[n - 1][m] = a[n - 1][m] ^ 1;
}
cout << v.size() / 3 << "\n";
long long int c = 0;
for (long long int i = 0; i < v.size(); i++) {
cout << v[i].first << " " << v[i].second << " ";
c++;
if (c == 3) {
cout << "\n";
c = 0;
}
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, m, q, h, k;
int flag, tmp;
int ans, mxm, mnm;
int max(int a, int b) {
if (a > b)
return a;
else
return b;
}
int min(int a, int b) {
if (a < b)
return a;
else
return b;
}
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
scanf("%d", &n);
int a[n];
int cnt1 = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (a[i] == 1) cnt1++;
}
flag = 0;
ans = n;
for (int i = 0; i < n - 1; i++) {
tmp = a[i];
for (int j = i + 1; j < n; j++) {
tmp = gcd(tmp, a[j]);
if (tmp == 1) {
flag = 1;
ans = min(ans, j - i);
break;
}
}
}
if (cnt1)
printf("%d", n - cnt1);
else if (flag)
printf("%d", ans + n - 1);
else
printf("-1");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 1e5 + 10;
int N, arr[N_MAX], num[N_MAX];
int main() {
scanf("%d", &N);
priority_queue<pair<int, int>> que;
for (int i = 1; i <= N; i++) {
scanf("%d", arr + i);
que.push(make_pair(arr[i] - i, i));
}
while (!que.empty()) {
pair<int, int> u = que.top();
que.pop();
int i = u.second, v = u.first + i;
if (v <= num[i]) continue;
if (v > num[i]) {
num[i] = v;
for (int j = i - 1; j >= 1 && num[j] < v - 1; j--) {
num[j] = --v;
}
}
}
for (int i = 1; i <= N; i++) {
num[i] = max(num[i], num[i - 1]);
}
long long ans = 0;
for (int i = 1; i <= N; i++) {
ans += 1LL * num[i] - arr[i];
}
printf("%lld\n", ans);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, m, ta, tb, y[100005], z[100005];
long long x[100005], ans;
vector<int> la;
int main() {
scanf("%d%d", &n, &m);
for (int a = 0; a < n; a++) scanf("%I64d", &x[a]);
n--;
for (int a = 0; a < n; a++) x[a] = abs(x[a] - x[a + 1]);
la.clear();
for (int a = 0; a < n; a++) {
while (!la.empty() && x[a] > x[la.back()]) {
y[la.back()] = a - 1;
la.pop_back();
}
la.push_back(a);
}
while (!la.empty()) {
y[la.back()] = n - 1;
la.pop_back();
}
for (int a = n - 1; a >= 0; a--) {
while (!la.empty() && x[a] >= x[la.back()]) {
z[la.back()] = a + 1;
la.pop_back();
}
la.push_back(a);
}
while (!la.empty()) {
z[la.back()] = 0;
la.pop_back();
}
while (m--) {
scanf("%d%d", &ta, &tb);
ans = 0;
ta--;
tb -= 2;
for (int a = ta; a <= tb; a++)
ans +=
x[a] * min(a - ta + 1, a - z[a] + 1) * min(tb - a + 1, y[a] - a + 1);
printf("%I64d\n", ans);
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
for (int test = 0; test < t; test++) {
long long x, y;
cin >> x >> y;
if (x >= 4) {
cout << "YES" << endl;
} else {
if (y < 4 && x > 1) {
cout << "YES" << endl;
} else {
if (x == y && y == 1) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m;
int bit[(int)2e5 + 100];
void update(int i) {
for (; i <= n; i += (i & (-i))) bit[i]++;
}
int q(int i) {
int sum = 0;
for (; i; i -= (i & (-i))) sum += bit[i];
return sum;
}
int getIndex(int i) {
int l = 1, r = n, idx;
while (l <= r) {
int mid = (l + r) >> 1;
if (mid >= q(mid) + i)
idx = mid, r = mid - 1;
else
l = mid + 1;
}
return idx;
}
map<char, multiset<int> > a;
multiset<int>::iterator it, it2;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> n >> m >> s;
for (int i = 0; i < n; i++) a[s[i]].insert(i + 1);
for (int i = 0; i < m; i++) {
int l, r;
char c;
cin >> l >> r >> c;
l = getIndex(l);
r = getIndex(r);
it = a[c].lower_bound(l);
while (it != a[c].end() && *it <= r) {
it2 = it;
it++;
update(*it2);
a[c].erase(it2);
}
}
vector<pair<int, char> > res;
for (auto ch : a)
for (auto x : ch.second) res.push_back(make_pair(x, ch.first));
sort(res.begin(), res.end());
for (auto x : res) cout << x.second;
cout << '\n';
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
long long pwr(long long base, long long p, long long mod = (1000000007LL)) {
long long ans = 1;
while (p) {
if (p & 1) ans = (ans * base) % mod;
base = (base * base) % mod;
p /= 2;
}
return ans;
}
long long LCA(long long a, long long b) {
set<long long> seen;
while (a > 0) {
seen.insert(a);
a /= 2;
}
while (b > 0) {
if (seen.find(b) != seen.end()) return b;
b /= 2;
}
assert(0);
return -1;
}
map<long long, long long> cost;
int main() {
ios_base::sync_with_stdio(0);
int q;
cin >> q;
while (q--) {
int op;
cin >> op;
if (op == 1) {
long long u, v, w;
cin >> u >> v >> w;
long long lca = LCA(u, v);
while (u != lca) {
cost[u] += w;
u /= 2;
}
while (v != lca) {
cost[v] += w;
v /= 2;
}
} else {
long long u, v;
cin >> u >> v;
long long lca = LCA(u, v);
long long ans = 0;
while (u != lca) {
ans += cost[u];
u /= 2;
}
while (v != lca) {
ans += cost[v];
v /= 2;
}
cout << ans << endl;
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long n, k;
cin >> n >> k;
string s;
cin >> s;
set<char> myset;
for (auto x : s) myset.insert(x);
vector<char> v;
for (auto x : myset) v.push_back(x);
if (k > n) {
long long i = 0;
string ans = "";
while (i < n) {
ans += s[i];
i++;
}
while (i < k) {
ans += v[0];
i++;
}
cout << ans << "\n";
} else {
string ans = s.substr(0, k);
long long i = k - 1;
char value;
while (i >= 0) {
long long idx = upper_bound(v.begin(), v.end(), ans[i]) - v.begin();
if (idx < v.size()) {
value = v[idx];
break;
}
i--;
}
ans[i] = value;
i++;
while (i < k) {
ans[i] = v[0];
i++;
}
cout << ans << "\n";
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, s;
cin >> n >> m;
s = 0;
if (n >= m) {
cout << n - m;
return 0;
}
while (m > n) {
if (m % 2 == 0) {
m = m / 2;
s++;
} else {
m = m + 1;
s++;
}
}
cout << s + n - m;
}
| 6 |
#include <bits/stdc++.h>
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
if (n % 2 == 0) {
cout << "-1";
exit(0);
}
vector<long long> p1, p2, p3;
for (int i = 0; i <= n - 1; i++)
p1.push_back(i), p2.push_back((i + 1) % n),
p3.push_back((p1[i] + p2[i]) % n);
for (auto i : p1) cout << i << " ";
cout << endl;
for (auto i : p2) cout << i << " ";
cout << endl;
for (auto i : p3) cout << i << " ";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int res = 0;
int neg;
while (true) {
char ch = getchar();
if (ch >= '0' && ch <= '9' || ch == '-') {
if (ch == '-')
neg = -1;
else
neg = 1, res = ch - '0';
break;
}
}
while (true) {
char ch = getchar();
if (ch >= '0' && ch <= '9')
res *= 10, res += ch - '0';
else
break;
}
return res * neg;
}
const int maxn = 500020;
const int maxk = 100020;
const int maxx = 100000;
const int maxm = 1000020;
const int MOd = 1e7;
int a, s, ar[maxn];
int main() {
int p1 = 1e9, r1 = 0, p2 = 1e9;
scanf("%d", &a);
for (int i = 1, k; i <= a; i++) {
scanf("%d", &k);
r1 = max(r1, (k));
}
scanf("%d", &a);
int H = a;
for (int i = 1, k; i <= a; i++) {
scanf("%d", &k);
p1 = min(p1, (k));
ar[i] = k;
}
scanf("%d", &a);
for (int i = 1, k; i <= a; i++) {
scanf("%d", &k);
p2 = min(p2, (k));
}
int A, B;
scanf("%d %d", &A, &B);
printf("");
double ans = 0;
for (int i = 1; i <= H; i++) {
p1 = ar[i];
ans = max(ans, ((double)sqrt((double)B * p1 * r1 * r1 /
((double)B * p1 + A * p2))));
}
printf("%.12lf\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int grundy[70];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int idx = 1;
for (int x = 1; idx <= 60; x++) {
for (int i = 0; i < x + 1 and idx <= 60; i++) {
grundy[idx++] = x;
}
}
int res = 0;
for (int i = 0; i < n; i++) {
res ^= grundy[s[i]];
}
if (res)
cout << "NO" << endl;
else
cout << "YES" << endl;
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
struct zuo {
long long val, l, r;
bool operator<(const zuo &b) const { return (l - r) < (b.l - b.r); }
};
struct you {
long long val, l, r;
bool operator<(const you &b) const { return r - l < (b.r - b.l); }
};
struct fc {
long long num, l, r;
};
vector<zuo> a;
vector<you> b;
vector<fc> store;
int main() {
scanf("%lld%lld", &n, &m);
long long ans = 0;
long long cntzuo = 0, cntyou = 0;
for (int i = 0; i < n; i++) {
long long val, l, r;
scanf("%lld%lld%lld", &val, &l, &r);
if (l < r) {
b.push_back({val, l, r});
cntyou += val;
ans += val * r;
} else {
a.push_back({val, l, r});
ans += val * l;
cntzuo += val;
}
}
cntzuo %= m;
cntyou %= m;
if (cntzuo + cntyou > m) {
cout << ans << endl;
return 0;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int l = 0;
while (cntzuo && l < a.size()) {
if (a[l].val >= cntzuo) {
ans -= cntzuo * a[l].l;
store.push_back({cntzuo, a[l].l, a[l].r});
cntzuo = 0;
} else {
ans -= a[l].val * a[l].l;
cntzuo -= a[l].val;
store.push_back({a[l].val, a[l].l, a[l].r});
l++;
}
}
l = 0;
while (cntyou && l < b.size()) {
if (b[l].val >= cntyou) {
ans -= cntyou * b[l].r;
store.push_back({cntyou, b[l].l, b[l].r});
cntyou = 0;
} else {
ans -= b[l].val * b[l].r;
cntyou -= b[l].val;
store.push_back({b[l].val, b[l].l, b[l].r});
l++;
}
}
long long tmpl = 0, tmpr = 0;
for (auto elem : store) {
tmpl += elem.l * elem.num;
tmpr += elem.r * elem.num;
}
ans += max(tmpl, tmpr);
cout << ans << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 19;
long long n, m, ans, dp[1 << MAXN][MAXN];
bool edge[MAXN][MAXN];
vector<long long> adj[MAXN];
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (long long u, v, i = 0; i < m; i++) {
cin >> v >> u;
v--, u--;
adj[v].push_back(u), adj[u].push_back(v);
edge[v][u] = edge[u][v] = true;
}
for (long long mask = 1; mask < (1 << n); mask++) {
long long x = __builtin_ctz(mask), cnt = __builtin_popcount(mask);
if (cnt == 1) {
dp[mask][x] = 1;
continue;
}
for (long long j = 0; j < n; j++)
if (mask & (1 << j) and j != x)
for (long long u : adj[j]) dp[mask][j] += dp[mask ^ (1 << j)][u];
if (cnt < 3) continue;
for (long long j = 0; j < n; j++)
if (edge[j][x]) ans += dp[mask][j];
}
cout << (ans >> 1);
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
string s;
int l, r, m;
int main() {
cin >> s;
while (s.size() != 3 && s.size() != 7) {
l = 0, r = 1;
do {
cout << "? " << l << " " << r << endl;
cin >> s;
if (s == "y") l = r, r <<= 1;
} while (s == "y");
while (l + 1 < r) {
m = (l + r) / 2;
cout << "? " << l << " " << m << endl;
cin >> s;
if (s == "y")
l = m;
else
r = m;
}
cout << "! " << l + 1 << endl;
cin >> s;
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, neg = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i] < 0 ? ++neg : neg;
}
if (neg >= k) {
for (int i = 0; i < k; i++) {
a[i] *= -1;
}
} else {
for (int i = 0; i < neg; i++) {
a[i] *= -1;
k--;
}
sort(a, a + n);
if (k & 1) {
a[0] *= -1;
}
}
int ans = 0;
for (int i = 0; i < n; i++) ans += a[i];
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string read_string() {
char *str;
scanf("%ms", &str);
string result(str);
free(str);
return result;
}
int main() {
string s = read_string(), t = read_string();
reverse(s.begin(), s.end());
if (s == t)
puts("YES");
else
puts("NO");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > q[1000010];
vector<int> ev[1000010];
int b[1000010];
void add(int p, int v) {
while (p < 1000010) {
b[p] += v;
p += p & -p;
}
}
int sum(int p) {
int res = 0;
while (p > 0) {
res += b[p];
p -= p & -p;
}
return res;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
long long v = 4, e = 4;
for (int i = 0; i < n; i++) {
int y, l, r;
scanf("%d%d%d", &y, &l, &r);
v += 2;
e++;
if (l == 0) e++;
if (r == 1000000) e++;
ev[l].push_back(y + 1);
ev[r].push_back(-y - 1);
}
for (int i = 0; i < m; i++) {
int x, l, r;
scanf("%d%d%d", &x, &l, &r);
v += 2;
e++;
if (l == 0) e++;
if (r == 1000000) e++;
q[x].push_back(make_pair(l, r));
}
for (int i = 0; i < 1000000; i++) {
vector<int> in, out;
for (int j = 0; j < ev[i].size(); j++) {
if (ev[i][j] > 0) {
in.push_back(ev[i][j]);
} else {
out.push_back(-ev[i][j]);
}
}
int f[3][3] = {};
for (int k = 0; k < 3; k++) {
for (int j = 0; j < q[i].size(); j++) {
int l = q[i][j].first + 1, r = q[i][j].second + 1;
if (l + 1 <= r - 1) {
f[k][1] += sum(r - 1) - sum(l);
}
f[k][0] += sum(l) - sum(l - 1) + sum(r) - sum(r - 1);
}
if (k == 0) {
for (int j = 0; j < out.size(); j++) {
add(out[j], -1);
}
} else if (k == 1) {
for (int j = 0; j < in.size(); j++) {
add(in[j], 1);
}
}
}
if (i > 0) {
for (int j = 0; j < 2; j++) {
f[0][j] = f[2][j] - f[1][j] + f[0][j] - f[1][j];
}
v += f[1][1] - f[0][0];
e += f[1][1] * 2 + f[0][1] + f[1][0];
}
}
printf("%lld\n", 1 + e - v);
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
for (bool sp = 0; begin != end; ++begin) {
if (sp)
putchar(' ');
else
sp = true;
printf(fmt, *begin);
}
putchar('\n');
}
template <unsigned MOD>
struct ModInt {
static const unsigned static_MOD = MOD;
unsigned x;
void undef() { x = (unsigned)-1; }
bool isnan() const { return x == (unsigned)-1; }
inline int geti() const { return (int)x; }
ModInt() { x = 0; }
ModInt(const ModInt &y) { x = y.x; }
ModInt(int y) {
if (y < 0 || (int)MOD <= y) y %= (int)MOD;
if (y < 0) y += MOD;
x = y;
}
ModInt(unsigned y) {
if (MOD <= y)
x = y % MOD;
else
x = y;
}
ModInt(long long y) {
if (y < 0 || MOD <= y) y %= MOD;
if (y < 0) y += MOD;
x = y;
}
ModInt(unsigned long long y) {
if (MOD <= y)
x = y % MOD;
else
x = y;
}
ModInt &operator+=(const ModInt y) {
if ((x += y.x) >= MOD) x -= MOD;
return *this;
}
ModInt &operator-=(const ModInt y) {
if ((x -= y.x) & (1u << 31)) x += MOD;
return *this;
}
ModInt &operator*=(const ModInt y) {
x = (unsigned long long)x * y.x % MOD;
return *this;
}
ModInt &operator/=(const ModInt y) {
x = (unsigned long long)x * y.inv().x % MOD;
return *this;
}
ModInt operator-() const { return (x ? MOD - x : 0); }
ModInt inv() const {
unsigned a = MOD, b = x;
int u = 0, v = 1;
while (b) {
int t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
if (u < 0) u += MOD;
return ModInt(u);
}
ModInt pow(long long y) const {
ModInt b = *this, r = 1;
if (y < 0) {
b = b.inv();
y = -y;
}
for (; y; y >>= 1) {
if (y & 1) r *= b;
b *= b;
}
return r;
}
friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};
const int MAX = 2000111;
int min_factor[MAX + 1];
vector<int> prime;
void make_prime() {
for (int i = 2; i <= MAX; i += 2) min_factor[i] = 2;
for (int i = 3; i <= MAX; i += 3)
if (!min_factor[i]) min_factor[i] = 3;
for (int i = 5, d = 2; i * i <= MAX;) {
if (!min_factor[i]) {
min_factor[i] = i;
for (int j = i * i; j <= MAX; j += i)
if (!min_factor[j]) min_factor[j] = i;
}
i += d;
d = 6 - d;
}
for (int i = 2; i <= MAX; i++) {
if (min_factor[i] == 0) min_factor[i] = i;
if (min_factor[i] == i) prime.push_back(i);
}
}
const long long MOD = 1000000007;
int N, K;
ModInt<MOD> C[MAX];
int mu[MAX];
void MAIN() {
make_prime();
for (int i = 0, i_len = (MAX); i < i_len; ++i) mu[i] = 1;
for (__typeof((prime).begin()) e = (prime).begin(), e_end = (prime).end();
e != e_end; ++e) {
for (int k = *e; k < MAX; k += *e) {
mu[k] = -mu[k];
}
long long d = (long long)*e * *e;
for (long long k = d; k < MAX; k += d) {
mu[k] = 0;
}
}
scanf("%d%d", &N, &K);
ModInt<MOD> ans_all = 0;
ModInt<MOD> pre = 0;
for (int i = 1; i <= K; i++) {
ModInt<MOD> ans = ModInt<MOD>(i).pow(N) - ModInt<MOD>(i - 1).pow(N);
for (int k = i + i, t = 2; k <= K; k += i, t++) {
C[k] += mu[t] * ans;
}
ans += C[i];
ans += pre;
ans_all += ans.geti() ^ i;
pre = ans;
}
printf("%d\n", ans_all.geti());
}
int main() {
int TC = 1;
for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
int vt, ve[(2 * 4 * 2010)], re[(2 * 4 * 2010)], ze[(2 * 4 * 2010)],
nextt[(2 * 4 * 2010)];
int in[2010], head[2010], path[2010], dis[2010], qu[2010], lim[2010];
void init() {
vt = 1;
memset(head, 0, sizeof(head));
}
int auxm = 0;
void add(int x, int y, int cap, int wei) {
++auxm;
ve[++vt] = y;
re[vt] = cap;
ze[vt] = wei;
nextt[vt] = head[x];
head[x] = vt;
ve[++vt] = x;
re[vt] = 0;
ze[vt] = -wei;
nextt[vt] = head[y];
head[y] = vt;
}
int mfmc(int s, int t, int n, int &fcost) {
int flow = fcost = 0;
while (1) {
int qt = 0, k = 0;
qu[qt++] = s;
for (int i = 0; i < n; i++) dis[i] = lim[i] = 0x3f3f3f3f;
dis[s] = 0;
while (k != qt) {
if (k == 2010) k = 0;
int x = qu[k++];
for (int i = head[x]; i; i = nextt[i])
if (re[i] && dis[x] + ze[i] < dis[ve[i]]) {
dis[ve[i]] = dis[x] + ze[i];
path[ve[i]] = i;
lim[ve[i]] = min(lim[x], re[i]);
if (!in[ve[i]]) {
if (qt == 2010) qt = 0;
qu[qt++] = ve[i];
in[ve[i]] = 1;
}
}
in[x] = 0;
}
if (dis[t] == 0x3f3f3f3f) break;
int f = lim[t];
for (int p = t; p != s; p = ve[path[p] ^ 1]) {
re[path[p]] -= f;
re[path[p] ^ 1] += f;
}
fcost += f * dis[t];
flow += f;
}
return flow;
}
pair<pair<int, int>, pair<int, int> > v[1010];
int ans[1010];
int main() {
ios::sync_with_stdio(false);
;
int nn, k;
cin >> nn >> k;
for (int i = 1; i < (nn + 1); ++i) {
cin >> v[i].first.first >> v[i].first.second >> v[i].second.second;
v[i].second.first = i - 1;
v[i].first.second += v[i].first.first;
}
sort(v + 1, v + nn + 1);
int s = 0;
int t = 2 * nn + 2;
int n = 2 * nn + 3;
init();
add(s, 1, k, 0);
for (int i = 1; i < (nn + 1); ++i) {
add(1, 2 * i, 1, 0);
add(2 * i, 2 * i + 1, 1, -v[i].second.second);
add(2 * i + 1, t, 1, 0);
if (i < nn) add(2 * i, 2 * (i + 1), 1010, 0);
for (int j = i + 1; j < (nn + 1); ++j)
if (v[j].first.first >= v[i].first.second) {
add(2 * i + 1, 2 * j, 1010, 0);
break;
}
}
int fcost, flow = mfmc(s, t, n, fcost);
if (nn == 100) {
}
for (int i = 1; i <= auxm; i++) {
int u = ve[2 * i + 1];
int vv = ve[2 * i];
int f = re[2 * i + 1];
if (u + 1 == vv) {
if (u % 2 == 0 && (u / 2) >= 1 && (u / 2) <= nn && f == 1)
ans[v[u / 2].second.first] = 1;
}
}
for (int i = 0; i < (nn); ++i) cout << ans[i] << " ";
cout << endl;
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chkmin(T1 &x, T2 y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, T2 y) {
if (x < y) x = y;
}
using ll = long long;
using ld = long double;
const int MAXMEM = 200 * 1000 * 1024;
char _memory[MAXMEM];
size_t _ptr = 0;
void *operator new(size_t _x) {
_ptr += _x;
return _memory + _ptr - _x;
}
void operator delete(void *) noexcept {}
const string FILENAME = "input";
const int MAXN = 100228;
const int BLOCK = 37;
const int BLOCK1 = 1200;
int n, w, q;
int x[MAXN];
int ans[MAXN];
int mx[18][MAXN];
int mx1[18][MAXN];
int gr[MAXN];
inline int getmax(const int l, const int r) {
int len = r - l + 1;
return max(mx1[gr[len]][l], mx1[gr[len]][r - (1 << gr[len]) + 1]);
}
inline int getmin(const int l, const int r) {
int len = r - l + 1;
return min(mx[gr[len]][l], mx[gr[len]][r - (1 << gr[len]) + 1]);
}
inline bool check(const int l, const int r, int k) {
if (l > r || r >= n) {
return false;
}
int f = getmin(l, r);
int f1 = getmax(l, r);
return f + w - f1 >= k;
}
inline int findGroupEnd(const int i, const int k) {
int l = i;
int r = n - 1;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (check(i, mid, k)) {
l = mid;
} else {
r = mid - 1;
}
}
return l + 1;
}
int curs[MAXN];
int jump[MAXN];
int target[MAXN];
int val;
void calc(int i) {
if (curs[i] == BLOCK) {
jump[i] = 0;
target[i] = i;
} else {
if (i + curs[i] >= n) {
jump[i] = 1;
target[i] = i + curs[i];
} else {
if (target[curs[i] + i] >= i + BLOCK) {
int t = i + curs[i];
int cnt = 1;
while (curs[t] + t < i + BLOCK) {
cnt++;
t += curs[t];
}
target[i] = t + curs[t];
jump[i] = cnt + 1;
} else {
jump[i] = 1 + jump[curs[i] + i];
target[i] = target[curs[i] + i];
}
}
}
}
void calc1(int i) {
if (curs[i] == BLOCK) {
jump[i] = 0;
target[i] = i;
} else {
if (i + curs[i] >= n) {
jump[i] = 1;
target[i] = i + curs[i];
}
}
}
int gt[MAXN];
int ls[MAXN];
int ft[MAXN];
int ukr;
int nxt[MAXN];
bool used[MAXN];
int st[MAXN];
int uks;
int grs;
int stacker[MAXN];
int ukt = 0;
int curuk[MAXN];
int curval[MAXN];
void dfs(int start) {
ukt = 0;
stacker[ukt] = start;
curuk[start] = gt[start];
ukt++;
uks = 0;
curval[start] = 0;
while (ukt) {
int u = stacker[ukt - 1];
if (curuk[u] == gt[u]) {
used[u] = true;
if (u <= grs) {
if (target[curs[u] + u] >= u + BLOCK) {
while (curval[u] < uks && st[curval[u]] >= u + BLOCK) {
curval[u]++;
}
int l = curval[u];
jump[u] = uks - l;
if (curs[st[l]] == BLOCK) {
target[u] = st[l];
} else {
jump[u]++;
target[u] = st[l] + curs[st[l]];
}
} else {
jump[u] = 1 + jump[curs[u] + u];
target[u] = target[curs[u] + u];
}
}
st[uks] = u;
uks++;
}
if (curuk[u] == -1) {
uks--;
ukt--;
continue;
}
int h = ft[curuk[u]];
curuk[u] = nxt[curuk[u]];
if (!used[h]) {
stacker[ukt] = h;
curval[h] = curval[u];
curuk[h] = gt[h];
ukt++;
}
}
}
void add(int a, int b) {
if (gt[a] == -1) {
gt[a] = ukr;
ls[a] = ukr;
ft[ukr] = b;
nxt[ukr] = -1;
ukr++;
} else {
nxt[ls[a]] = ukr;
ft[ukr] = b;
nxt[ukr] = -1;
ls[a] = ukr;
ukr++;
}
}
void update(int i) {
ukr = 0;
for (int j = min(i + BLOCK, n); j >= max(0, i - BLOCK - 5); j--) {
gt[j] = -1;
}
for (int j = min(i + BLOCK, n); j >= max(0, i - BLOCK - 5); j--) {
used[j] = false;
}
for (int j = max(0, i - BLOCK - 5); j <= i; j++) {
if (curs[j] == BLOCK || j + curs[j] >= n) {
calc1(j);
used[j] = true;
continue;
}
add(j + curs[j], j);
}
grs = i;
for (int j = min(i + BLOCK, n); j >= max(0, i - BLOCK - 5); j--) {
if (!used[j]) {
dfs(j);
}
}
}
bool bad[MAXN];
int curs2[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> w >> q;
for (int i = 0; i < n; i++) {
cin >> x[i];
mx[0][i] = x[i];
mx1[0][i] = x[i];
}
for (int k = 1; k <= 18; k++) {
for (int i = 0; i < n; i++) {
int t = min(i + (1 << (k - 1)), n - 1);
mx[k][i] = min(mx[k - 1][i], mx[k - 1][t]);
mx1[k][i] = max(mx1[k - 1][i], mx1[k - 1][t]);
}
}
int cur = 1;
int step = 0;
for (int i = 1; i <= n; i++) {
if (cur + cur <= i) {
cur += cur;
step++;
}
gr[i] = step;
}
vector<pair<int, int> > st;
for (int it = 0; it < q; it++) {
int k;
cin >> k;
st.push_back({k, it});
}
sort((st).begin(), (st).end());
vector<pair<int, pair<int, int> > > g;
vector<pair<int, int> > g1;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= BLOCK; j++) {
int maxk = 0;
if (i + j - 1 >= n) {
maxk = 0;
} else {
int s = getmin(i, i + j - 1);
int s1 = getmax(i, i + j - 1);
maxk = max(0, s + w - s1);
}
g.push_back({maxk + 1, {i, j - 1}});
if (maxk) {
chkmax(curs[i], j);
}
}
if (i + BLOCK1 <= n) {
int s = getmin(i, i + BLOCK1 - 1);
int s1 = getmax(i, i + BLOCK1 - 1);
if (max(0, s + w - s1) != 0) {
g1.push_back({max(0, s + w - s1) + 1, i});
bad[i] = true;
}
}
curs2[i] = findGroupEnd(i, 1) - i;
}
sort((g).begin(), (g).end());
sort((g1).begin(), (g1).end());
val = 1;
int uk = 0;
for (int i = n - 1; i >= 0; i--) {
calc(i);
}
int uk1 = 0;
for (int it = 0; it < q; it++) {
val = st[it].first;
while (uk < (int)(g).size() && g[uk].first <= val) {
int gh = curs[g[uk].second.first];
chkmin(curs[g[uk].second.first], g[uk].second.second);
if (curs[g[uk].second.first] != gh) {
update(g[uk].second.first);
}
uk++;
}
while (uk1 < (int)(g1).size() && g1[uk1].first <= val) {
bad[g1[uk1].second] = false;
curs2[g1[uk1].second] = BLOCK1 - 1;
uk1++;
}
int cur = 0;
int cnt = 0;
while (cur < n) {
if (curs[cur] == BLOCK) {
if (bad[cur]) {
cnt++;
cur = findGroupEnd(cur, val);
} else {
while (!check(cur, cur + curs2[cur] - 1, val)) {
curs2[cur]--;
}
cnt++;
int val = curs2[cur];
cur += val;
}
} else {
cnt += jump[cur];
cur = target[cur];
}
}
ans[st[it].second] = cnt - 1;
}
for (int it = 0; it < q; it++) {
cout << ans[it] << '\n';
}
return 0;
}
| 26 |
#include <bits/stdc++.h>
using namespace std;
vector<string> inp;
string RED;
void query() {
int ans = 0;
string q;
getline(cin, q);
stringstream cnv;
cnv << q;
vector<string> vs;
string x;
while (cnv >> x) vs.push_back(x);
vector<string> open;
vector<int> open_index;
vector<int> other_index;
for (int i = 0; i < inp.size(); ++i) {
if (inp[i][0] == '/') {
if (open_index.size() == 0 or
(other_index.size() != 0 and other_index[other_index.size() - 1] >
open_index[open_index.size() - 1]))
other_index.pop_back();
else {
open_index.pop_back();
open.pop_back();
}
} else {
if (open.size() < vs.size() and vs[open.size()] == inp[i]) {
open.push_back(inp[i]);
open_index.push_back(i);
} else
other_index.push_back(i);
}
if (open.size() == vs.size())
if (inp[i] == open[open.size() - 1]) {
ans++;
}
}
cout << ans << endl;
}
void proc() {
stringstream cin;
cin << RED;
char c;
string name;
while (cin >> c) {
if (c == '<')
name = "";
else if (c == '>') {
if (name[name.length() - 1] != '/')
inp.push_back(name);
else {
inp.push_back(name.substr(0, name.length() - 1));
inp.push_back("/" + name.substr(0, name.length() - 1));
}
} else {
string add = " ";
add[0] = c;
name = name + add;
}
}
}
int main() {
getline(cin, RED);
proc();
int m;
string rd;
stringstream cnv;
getline(cin, rd);
cnv << rd;
cnv >> m;
while (m--) query();
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
struct Debug {
int cnt = 0;
bool on = false;
char debug[10] = "debug";
template <class T>
Debug& operator<<(const T& v) {
if (on && cnt++ == 0) cerr << "debug: ";
if (on) cerr << v << ' ';
return *this;
}
Debug& operator<<(ostream& (*pManip)(ostream&)) {
if (on) cerr << (*pManip);
cnt = 0;
return *this;
}
} debug;
const int MAXN = 1e6 + 10;
long long dp[3][MAXN] = {};
int a[MAXN], b[MAXN];
int main(int argc, char* argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
if (argc > 1 && !strcmp(argv[1], debug.debug)) debug.on = true;
int n, c;
cin >> n >> c;
n--;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
dp[0][0] = a[0];
dp[1][0] = b[0] + c;
dp[2][0] = 1e15;
for (int i = 1; i < n; i++) {
dp[0][i] = a[i] + min({dp[0][i - 1], dp[1][i - 1], dp[2][i - 1]});
dp[1][i] = c + b[i] + min({dp[0][i - 1], dp[1][i - 1], dp[2][i - 1]});
dp[2][i] = b[i] + min(dp[1][i - 1], dp[2][i - 1]);
}
cout << 0 << ' ';
for (int i = 0; i < n; i++) {
cout << min({dp[0][i], dp[1][i], dp[2][i]}) << ' ';
}
cout << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, x, y, son[100005], q;
int fir[100005], nxt[200005], to[200005], cnt;
long long wi[200005], z, dp[100005][2], f[100005];
struct node {
int id;
long long val;
} c[100005];
int size[100005], fa[100005], Fa[100005];
long long read() {
long long x = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return x;
}
void dfs(int x, int fa) {
for (int i = fir[x]; i; i = nxt[i]) {
if (to[i] == fa) continue;
dfs(to[i], x);
if (dp[x][0] < dp[to[i]][0] + wi[i])
dp[x][1] = dp[x][0], dp[x][0] = dp[to[i]][0] + wi[i], son[x] = to[i];
else if (dp[x][1] < dp[to[i]][0] + wi[i])
dp[x][1] = dp[to[i]][0] + wi[i];
}
}
void Dfs(int x, int fa, long long W) {
f[x] = max(dp[x][0], W);
for (int i = fir[x]; i; i = nxt[i]) {
if (to[i] == fa) continue;
if (to[i] == son[x])
Dfs(to[i], x, max(W, dp[x][1]) + wi[i]);
else
Dfs(to[i], x, max(W, dp[x][0]) + wi[i]);
}
}
int cmp(node u, node v) { return u.val < v.val; }
void DFS(int x, int f) {
Fa[x] = f;
for (int i = fir[x]; i; i = nxt[i]) {
if (to[i] == f) continue;
DFS(to[i], x);
}
}
int find(int x) {
if (x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
int main() {
n = read();
for (int i = 1; i < n; i++) {
x = read(), y = read(), z = read();
to[++cnt] = y, nxt[cnt] = fir[x], fir[x] = cnt, wi[cnt] = z;
to[++cnt] = x, nxt[cnt] = fir[y], fir[y] = cnt, wi[cnt] = z;
}
dfs(1, 0);
Dfs(1, 0, 0);
for (int i = 1; i <= n; i++) c[i] = (node){i, f[i]};
sort(c + 1, c + n + 1, cmp);
DFS(c[1].id, 0);
q = read();
for (int i = 1; i <= q; i++) {
z = read();
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= n; i++) size[i] = 1;
int r = n, ans = 0;
for (int j = n; j >= 1; j--) {
while (r && c[r].val - c[j].val > z) size[find(c[r].id)]--, r--;
ans = max(ans, size[find(c[j].id)]);
int u = find(c[j].id), v = find(Fa[c[j].id]);
if (u != v) fa[u] = v, size[v] += size[u];
}
printf("%d\n", ans);
}
return 0;
}
| 20 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
string name;
cin >> name;
int f[9];
;
for (int i = 0; i < 9; i++) {
cin >> f[i];
}
int data[n];
;
for (int i = 0; i < n; i++) {
data[i] = (int)(name[i] - '0') - f[(int)(name[i] - '0') - 1];
};
for (int i = 0; i < n; i++) {
if (data[i] < 0) {
while (data[i] <= 0 && i < n) {
name[i] = f[(int)(name[i] - '0') - 1] + '0';
i++;
}
break;
}
}
cout << name;
return 0;
}
| 5 |