From 249911b083b98ebfb09e2d0ccee51c550a956996 Mon Sep 17 00:00:00 2001 From: KryptoCrash Date: Sun, 16 Jan 2022 19:08:21 -0500 Subject: [PATCH] holy --- 121a.cpp | 27 +++ 121b.cpp | 52 +++++ 121c.cpp | 59 ++++++ 121d.cpp | 39 ++++ 121e.cpp | 0 759a.cpp | 48 +++++ 759b.cpp | 38 ++++ 759c.cpp | 57 ++++++ 759cbad.cpp | 57 ++++++ 759d.cpp | 84 ++++++++ 759f.cpp | 82 ++++++++ 766a.cpp | 50 +++++ 766b.cpp | 41 ++++ 766c.cpp | 72 +++++++ 766d.cpp | 44 ++++ 766e.cpp | 99 +++++++++ aidanlol.cpp | 44 ++++ avghigh.cpp | 67 +++++++ banq.cpp | 77 +++++++ batik.cpp | 44 ++++ bforthing.cpp | 52 +++++ boring2seg.cpp | 311 ++++++++++++++++++++++++++++ boringseg.cpp | 80 ++++++++ changebrac.cpp | 48 +++++ chasesubway.cpp | 84 ++++++++ crazyrobot.cpp | 35 ++++ deca.cpp | 109 ++++++++++ decb.cpp | 109 ++++++++++ decc.cpp | 42 ++++ dforthing.cpp | 75 +++++++ dom.cpp | 56 ++++++ equi.cpp | 67 +++++++ equiseg.cpp | 221 ++++++++++++++++++++ exactchange.cpp | 57 ++++++ extext.cpp | 67 +++++++ firsta.cpp | 28 +++ firstb.cpp | 51 +++++ firstc.cpp | 93 +++++++++ firstd.cpp | 112 +++++++++++ garland.cpp | 76 +++++++ gcdweird.cpp | 51 +++++ gcdweirdeas.cpp | 58 ++++++ gen.cpp | 19 +- goat.cpp | 98 +++++++++ guesstheperm.cpp | 57 ++++++ intpro.cpp | 49 +++++ mathtest.cpp | 78 ++++++++ mincov.cpp | 61 ++++++ minesweep.cpp | 101 ++++++++++ minor.cpp | 90 +++++++++ optins.cpp | 200 ++++++++++++++++++ optinsbetter.cpp | 173 ++++++++++++++++ optinsbetterseg.cpp | 389 ++++++++++++++++++++++++++++++++++++ permshift.cpp | 64 ++++++ repnum.cpp | 54 +++++ shuffler.cpp | 70 +++++++ staircases.cpp | 115 +++++++++++ strongbuild.cpp | 77 +++++++ sumofminxor.cpp | 27 +++ telepainting.cpp | 76 +++++++ telepaintingbad.cpp | 118 +++++++++++ thinglol.cpp | 92 +++++++++ trainmat.cpp | 89 +++++++++ whatisthisproblemcalled.cpp | 32 +++ 64 files changed, 5089 insertions(+), 3 deletions(-) create mode 100644 121a.cpp create mode 100644 121b.cpp create mode 100644 121c.cpp create mode 100644 121d.cpp create mode 100644 121e.cpp create mode 100644 759a.cpp create mode 100644 759b.cpp create mode 100644 759c.cpp create mode 100644 759cbad.cpp create mode 100644 759d.cpp create mode 100644 759f.cpp create mode 100644 766a.cpp create mode 100644 766b.cpp create mode 100644 766c.cpp create mode 100644 766d.cpp create mode 100644 766e.cpp create mode 100644 aidanlol.cpp create mode 100644 avghigh.cpp create mode 100644 banq.cpp create mode 100644 batik.cpp create mode 100644 bforthing.cpp create mode 100644 boring2seg.cpp create mode 100644 boringseg.cpp create mode 100644 changebrac.cpp create mode 100644 chasesubway.cpp create mode 100644 crazyrobot.cpp create mode 100644 deca.cpp create mode 100644 decb.cpp create mode 100644 decc.cpp create mode 100644 dforthing.cpp create mode 100644 dom.cpp create mode 100644 equi.cpp create mode 100644 equiseg.cpp create mode 100644 exactchange.cpp create mode 100644 extext.cpp create mode 100644 firsta.cpp create mode 100644 firstb.cpp create mode 100644 firstc.cpp create mode 100644 firstd.cpp create mode 100644 garland.cpp create mode 100644 gcdweird.cpp create mode 100644 gcdweirdeas.cpp create mode 100644 goat.cpp create mode 100644 guesstheperm.cpp create mode 100644 intpro.cpp create mode 100644 mathtest.cpp create mode 100644 mincov.cpp create mode 100644 minesweep.cpp create mode 100644 minor.cpp create mode 100644 optins.cpp create mode 100644 optinsbetter.cpp create mode 100644 optinsbetterseg.cpp create mode 100644 permshift.cpp create mode 100644 repnum.cpp create mode 100644 shuffler.cpp create mode 100644 staircases.cpp create mode 100644 strongbuild.cpp create mode 100644 sumofminxor.cpp create mode 100644 telepainting.cpp create mode 100644 telepaintingbad.cpp create mode 100644 thinglol.cpp create mode 100644 trainmat.cpp create mode 100644 whatisthisproblemcalled.cpp diff --git a/121a.cpp b/121a.cpp new file mode 100644 index 0000000..330ecfc --- /dev/null +++ b/121a.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + string s; + cin >> s; + sort(s.begin(), s.end()); + + cout << s << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/121b.cpp b/121b.cpp new file mode 100644 index 0000000..0244199 --- /dev/null +++ b/121b.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + string s; + cin >> s; + + ll n = s.length(); + + bool works = false; + + for (int i = n - 1; i >= 1; i--) { + ll x = s[i] - '0'; + ll y = s[i - 1] - '0'; + if (x + y > 9) { + s[i - 1] = '0' + ((x + y) / 10); + + s[i] = '0' + ((x + y) % 10); + works = true; + break; + } + } + + if(!works) { + string t; + t += '0' + ((s[0] - '0') + (s[1] - '0')); + for (int i = 2; i < n; i++) { + t += s[i]; + } + + s = t; + } + + cout << s << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/121c.cpp b/121c.cpp new file mode 100644 index 0000000..bd5bb53 --- /dev/null +++ b/121c.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vpi d; + + vi k(n); + for(ll i = 0; i < n; i++) { + cin >> k[i]; + d.push_back({k[i], 1}); + } + + vi h(n); + for(ll i = 0; i < n; i++) { + cin >> h[i]; + } + + + for (ll i = 0; i < n; i++) { + d.push_back({k[i] - h[i] + 1, -1}); + } + + sort(d.begin(), d.end()); + + ll ans = 0; + ll depth = 0; + ll st = 1; + for (ll i = 0; i <= d.size(); i++) { + if(depth == 0) { + ll sz = (i > 0 ? d[i - 1].first : 0) - st + 1; + ans += (sz * (sz + 1)) / 2; + if(i != d.size()) st = d[i].first; + } + + if (i != d.size()) depth += d[i].second; + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/121d.cpp b/121d.cpp new file mode 100644 index 0000000..8e693f3 --- /dev/null +++ b/121d.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vi a(n); + map mp; + for (int i = 0; i < n; i++) { + cin >> a[i]; + mp[a[i]]++; + } + + sort(a.begin(), a.end()); + + for (int i = 0; i < 19; i++) { + for (int j = 0; j < 19; j++) { + + } + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/121e.cpp b/121e.cpp new file mode 100644 index 0000000..e69de29 diff --git a/759a.cpp b/759a.cpp new file mode 100644 index 0000000..df378b2 --- /dev/null +++ b/759a.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vi a(n); + for(ll i = 0; i < n; i++) { + cin >> a[i]; + } + + ll ans = 1; + bool died = false; + + if (a[0]) ans++; + + for (ll i = 1; i < n; i++) { + if (!a[i] && !a[i - 1]) died = true; + else if (a[i] && a[i - 1]) ans += 5; + else if (a[i] && !a[i - 1]) ans += 1; + + } + + if(died) { + cout << -1 << endl; + return; + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/759b.cpp b/759b.cpp new file mode 100644 index 0000000..f240bd1 --- /dev/null +++ b/759b.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + } + + ll rmx = 0; + + ll cnt = 0; + for (ll i = n - 1; i >= 0; i--) { + if (a[i] > rmx) cnt++, rmx = a[i]; + } + + cout << cnt - 1 << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/759c.cpp b/759c.cpp new file mode 100644 index 0000000..5e23b61 --- /dev/null +++ b/759c.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n, k; + cin >> n >> k; + + vi a; + vi b; + for (ll i = 0; i < n; i++) { + ll c; + cin >> c; + if (c == 0) continue; + if (c > 0) a.push_back(c); + if (c < 0) b.push_back(-c); + } + + sort(a.begin(), a.end(), greater()); + sort(b.begin(), b.end(), greater()); + vi trips; + + for (ll i = 0; i < a.size(); i += k) { + trips.push_back(a[i]); + } + + for (ll i = 0; i < b.size(); i += k) { + trips.push_back(b[i]); + } + + ll ans = 0; + sort(trips.begin(), trips.end(), greater()); + + for(ll i : trips) { + ans += i * 2; + } + + if(trips.size() > 0) ans -= trips[0]; + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/759cbad.cpp b/759cbad.cpp new file mode 100644 index 0000000..0594782 --- /dev/null +++ b/759cbad.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n, k; + cin >> n >> k; + + vi a; + vi b; + for (ll i = 0; i < n; i++) { + ll c; + cin >> c; + if (c == 0) continue; + if (c > 0) a.push_back(c); + if (c < 0) b.push_back(-c); + } + + sort(a.begin(), a.end(), greater()); + sort(b.begin(), b.end(), greater()); + vi trips; + + for (ll i = 0; i < a.size(); i += k) { + trips.push_back(a[i]); + } + + for (ll i = 0; i < b.size(); i += k) { + trips.push_back(b[i]); + } + + ll ans = 0; + sort(trips.begin(), trips.end(), greater()); + + for (ll i : trips) { + ans += i * 2; + } + + ans -= trips[0]; + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/759d.cpp b/759d.cpp new file mode 100644 index 0000000..4ef9e90 --- /dev/null +++ b/759d.cpp @@ -0,0 +1,84 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll MX = 5e5 + 8; + +template +struct Seg { // comb(ID,b) = b + + const T ID = 0; + T comb(T a, T b) { return a + b; } + + ll n; + vector seg; + + void init(ll _n) { + n = _n; + seg.assign(2 * n, ID); + } + + void pull(ll p) { seg[p] = comb(seg[2 * p], seg[2 * p + 1]); } + + void upd(ll p, T val) { // set val at position p + + seg[p += n] = val; + for (p /= 2; p; p /= 2) pull(p); + } + + T query(ll l, ll r) { // sum on llerval [l, r] + + T ra = ID, rb = ID; + + for (l += n, r += n + 1; l < r; l /= 2, r /= 2) { + if (l & 1) ra = comb(ra, seg[l++]); + + if (r & 1) rb = comb(seg[--r], rb); + } + + return comb(ra, rb); + } +}; + +void solve() { + ll n; + cin >> n; + + Seg st; + st.init(n + 8); + + vi a(n); + for(ll i = 0; i < n; i++) { + cin >> a[i]; + } + + ll inv = 0; + for (ll i = 0; i < n; i++) { + inv += st.query(a[i] + 1, n + 5); + st.upd(a[i], st.query(a[i], a[i]) + 1); + } + + sort(a.begin(), a.end()); + if(adjacent_find(a.begin(), a.end()) != a.end()) { + cout << "YES" << endl; + return; + } + + cout << (inv % 2 ? "NO" : "YES") << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/759f.cpp b/759f.cpp new file mode 100644 index 0000000..83c39f4 --- /dev/null +++ b/759f.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +template +struct Seg { // comb(ID,b) = b + + const T ID = 0; + T comb(T a, T b) { return (a + b) % mod; } + + ll n; + vector seg; + + void init(ll _n) { + n = _n; + seg.assign(2 * n, ID); + } + + void pull(ll p) { seg[p] = comb(seg[2 * p], seg[2 * p + 1]); } + + void upd(ll p, T val) { // set val at position p + + seg[p += n] = val; + for (p /= 2; p; p /= 2) pull(p); + } + + T query(ll l, ll r) { // sum on llerval [l, r] + + T ra = ID, rb = ID; + + for (l += n, r += n + 1; l < r; l /= 2, r /= 2) { + if (l & 1) ra = comb(ra, seg[l++]); + + if (r & 1) rb = comb(seg[--r], rb); + } + + return comb(ra, rb); + } +}; + +const ll mod = 998244353; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + Seg st; + Seg st2; + st.init(1e9 + 8); + st2.init(1e9 + 8); + + vi a(n); + for(int i = 0; i < n; i++) { + cin >> a[i]; + } + + for (int i = 0; i < n - 1; i++) { + ll x = st.query(1, a[i]) % mod; + ll y = st.query(1, min(a[i], a[i + 1])) % mod; + + ll c = a[i + 1]; + + ll d = min(a[i + 1], a[i]); + st2.upd(1, c, (c * x) % mod - y); + st2.upd(c + 1, d, ((d - c) * x) % mod); + + st = st2; + st2.init(1e9 + 8); + } + + cout << st.query(1, a[n - 1]) % mod << endl; + + return 0; +} \ No newline at end of file diff --git a/766a.cpp b/766a.cpp new file mode 100644 index 0000000..25e1bee --- /dev/null +++ b/766a.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n, m; + cin >> n >> m; + + ll r, c; + cin >> r >> c; + r--, c--; + + vector grid(n, vi(m)); + for (ll i = 0; i < n; i++) { + for(ll j = 0; j < m; j++) { + char cg; + cin >> cg; + grid[i][j] = cg == 'B'; + } + } + + ll ans = 3; + for (ll i = 0; i < n; i++) { + for(ll j = 0; j < m; j++) { + if (i == r && j == c && grid[i][j]) ans = min(ans, 0LL); + else if((i == r || j == c ) && grid[i][j]) + ans = min(ans, 1LL); + else if(grid[i][j]) + ans = min(ans, 2LL); + } + } + + cout << (ans == 3 ? -1 : ans) << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/766b.cpp b/766b.cpp new file mode 100644 index 0000000..fb655b2 --- /dev/null +++ b/766b.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n, m; + cin >> n >> m; + + vi dist(n * m); + ll idx = 0; + for (ll i = 0; i < n; i++) { + for (ll j = 0; j < m; j++) { + ll mx = 0; + mx = max(mx, abs(i - 0) + abs(j - 0)); + mx = max(mx, abs(i - (n-1)) + abs(j - (m-1))); + mx = max(mx, abs(i - 0) + abs(j - (m-1))); + mx = max(mx, abs(i - (n-1)) + abs(j - 0)); + dist[idx++] = mx; + } + } + + sort(dist.begin(), dist.end()); + for(ll i : dist) cout << i << " "; + cout << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/766c.cpp b/766c.cpp new file mode 100644 index 0000000..200240d --- /dev/null +++ b/766c.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vector> ed(n); + vi deg(n); + for (ll i = 0; i < n - 1; i++) { + ll u, v; + cin >> u >> v; + u--, v--; + deg[u]++; + deg[v]++; + ed[u].insert({v, i}); + ed[v].insert({u, i}); + } + + bool works = true; + ll lst = 0; + for (ll i = 0; i < n; i++) { + if (deg[i] > 2) works = false; + if (deg[i] == 1) lst = i; + } + + if(!works) { + cout << -1 << endl; + return; + } + + vb vis(n); + vi ans(n - 1); + + function dfs = [&](ll v, ll l, ll par) { + if(vis[v]) return; + vis[v] = true; + + for(auto ch : ed[v]) { + ll val = ch.first; + ll idx = ch.second; + if (val == par) continue; + ans[idx] = l; + dfs(val, (l == 3 ? 2 : 3), v); + } + }; + + dfs(lst, 2, -1); + + for(ll i : ans) { + cout << i << " "; + } + + cout << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/766d.cpp b/766d.cpp new file mode 100644 index 0000000..3d18043 --- /dev/null +++ b/766d.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vi a(n); + vi vis(1e6+8); + ll mx = 0; + ll ans = 0; + for (ll i = 0; i < n; i++) { + cin >> a[i]; + mx = max(mx, a[i]); + vis[a[i]] = true; + } + + for (ll g = 1; g <= mx; g++) { + ll cur = 0; + + for (ll m = g; m <= mx; m += g) { + if(vis[m]) { + cur = __gcd(cur, m); + if(cur == g) { + ans++; + break; + } + } + } + } + + cout << ans - n << endl; + + return 0; +} \ No newline at end of file diff --git a/766e.cpp b/766e.cpp new file mode 100644 index 0000000..0ca1a7d --- /dev/null +++ b/766e.cpp @@ -0,0 +1,99 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n, m, k; + cin >> n >> m >> k; + + vi cost(n); + for (ll i = 0; i < n; i++) { + cin >> cost[i]; + } + + map> ed; + vector> lad(n); + + vi b(n); + for (ll i = 0; i < k; i++) { + ll x, y, z, w, c; + + cin >> x >> y >> z >> w >> c; + x--, y--, z--, w--; + ed[{x, y}] = {z, w, -c}; + lad[x].insert({y, 1}); + lad[z].insert({w, 0}); + } + + lad[n - 1].insert({m - 1, 0}); + + priority_queue> pq; + pq.push({0, 0, 0, 0}); + vector>> vis(n, vector>(m, array{0, 0})); + vis[0][0][0] = true; + + ll ans = 1e9; + bool pos = 0; + while (!pq.empty()) { + array top = pq.top(); + pq.pop(); + ll x = top[1]; + ll y = top[2]; + ll dist = -top[0]; + ll tp = top[3]; + if (x == n - 1 && y == m - 1) { + ans = dist; + pos = 1; + break; + } + + auto it = lad[x].find({y, tp}); + if (it != lad[x].begin()) { + it--; + if (!vis[x][(*it).first][(*it).second]) { + pq.push({-(dist + abs(y - (*it).first) * cost[x]), x, (*it).first, (*it).second}); + vis[x][(*it).first][(*it).second] = true; + } + + it++; + } + if (it != lad[x].end()) { + it++; + if (it != lad[x].end()) { + if (!vis[x][(*it).first][(*it).second]) { + pq.push({-(dist + abs(y - (*it).first) * cost[x]), x, (*it).first, (*it).second}); + vis[x][(*it).first][(*it).second] = true; + } + } + it--; + } + + if (tp == 1) { + if (!vis[ed[{x, y}][0]][ed[{x, y}][1]][0LL]) { + pq.push({-(dist + ed[{x, y}][2]), ed[{x, y}][0], ed[{x, y}][1], 0LL}); + vis[ed[{x, y}][0]][ed[{x, y}][1]][0LL] = true; + } + } + } + + if (!pos) + cout << "NO ESCAPE" << endl; + else + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/aidanlol.cpp b/aidanlol.cpp new file mode 100644 index 0000000..f702ed7 --- /dev/null +++ b/aidanlol.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vector> a(n + 1, vector(n + 1, 1)); + for(ll i = 0; i < n; i++) { + for(ll j = 0; j < n; j++) { + char c; + cin >> c; + a[i][j] = c == '1'; + } + } + + bool works = true; + for (ll i = 0; i < n; i++) { + for(ll j = 0; j < n; j++) { + if(a[i][j]) { + if (!(a[i + 1][j] || a[i][j + 1])) works = false; + } + } + } + + cout << (works ? "YES" : "NO") << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/avghigh.cpp b/avghigh.cpp new file mode 100644 index 0000000..f4c4db3 --- /dev/null +++ b/avghigh.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vi a(n); + for(ll i = 0; i < n; i++) { + cin >> a[i]; + } + + ll x; + cin >> x; + + for (ll i = 0; i < n; i++) { + a[i] -= x; + } + + ll l, r; + l = r = 0; + + ll sum = 0; + ll ans = 0; + + while(l < n) { + bool found = false; + while (r - l <= 3) { + if (sum < 0 && r - l > 1) { + ans++; + found = true; + break; + } + + if (r - l < 3 && r < n) sum += a[r++]; + else + break; + } + + if(found) { + l = r; + sum = 0; + continue; + } + + sum -= a[l++]; + } + + cout << n - ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/banq.cpp b/banq.cpp new file mode 100644 index 0000000..1824c55 --- /dev/null +++ b/banq.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + map>> lin; + vi x(n); + vi y(n); + for (ll i = 0; i < n; i++) { + ll a, b, m; + cin >> a >> b >> m; + x[i] = a; + y[i] = b; + ll left = a + b - m; + ll mina = max(0LL, a - m); + ll maxa = a - max(0LL, -(b - m)); + + lin[left].push({-mina, 1, i}); + lin[left].push({-maxa, -1, i}); + } + + vb vis(n); + vpi ans(n); + ll tot = 0; + + for(auto l : lin) { + priority_queue> ev = l.second; + ll left = l.first; + stack waiting; + + while(!ev.empty()) { + array ar = ev.top(); + ev.pop(); + + ll v = -ar[0]; + ll tp = ar[1]; + ll idx = ar[2]; + + if (tp == 1) waiting.push(idx); + else { + if (vis[idx]) continue; + tot++; + while(!waiting.empty()) { + ll i = waiting.top(); + vis[i] = true; + ans[i] = {x[i] - v, y[i] - (left - v)}; + waiting.pop(); + } + } + } + } + + cout << tot << endl; + + for(pi i : ans) { + cout << i.first << " " << i.second << endl; + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/batik.cpp b/batik.cpp new file mode 100644 index 0000000..d9d0e90 --- /dev/null +++ b/batik.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll mod = 1e9 + 7; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, m, k, r, c; + cin >> n >> m >> k >> r >> c; + + ll x1, y1, x2, y2; + cin >> x1 >> y1 >> x2 >> y2; + + function binpow = [&](ll x, ll y) { + assert(y >= 0); + + x %= mod; + + ll res = 1; + + while (y > 0) { + if (y % 2 == 1) res = res * x % mod; + x = x * x % mod; + y /= 2; + } + + return res; + }; + + ll space = n*m; + if(!(x1 == x2 && y1 == y2)) space -= r * c; + + cout << binpow(k, space) << endl; + + return 0; +} \ No newline at end of file diff --git a/bforthing.cpp b/bforthing.cpp new file mode 100644 index 0000000..e0cdc68 --- /dev/null +++ b/bforthing.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, k; + cin >> n >> k; + + vector s(n); + map cnt; + for (ll i = 0; i < n; i++) { + string chr; + cin >> chr; + s[i] = chr; + cnt[s[i]]++; + } + + ll ans = 0; + for (ll i = 0; i < n; i++) { + for (ll j = i; j < n; j++) { + string s1 = s[i]; + string s2 = s[j]; + string con; + for (ll z = 0; z < k; z++) { + if (s1[z] == s2[z]) con += s1[z]; + else { + if (s1[z] == 'S' && s2[z] == 'T') con += 'E'; + if (s1[z] == 'T' && s2[z] == 'S') con += 'E'; + if (s1[z] == 'T' && s2[z] == 'E') con += 'S'; + if (s1[z] == 'E' && s2[z] == 'T') con += 'S'; + if (s1[z] == 'E' && s2[z] == 'S') con += 'T'; + if (s1[z] == 'S' && s2[z] == 'E') con += 'T'; + } + } + if(cnt.find(con) != cnt.end()) ans += max(0LL, cnt[con] - (s1 == con) - (s2 == con)); + } + } + + assert(ans % 3 == 0); + + cout << ans / 3 << endl; + + return 0; +} \ No newline at end of file diff --git a/boring2seg.cpp b/boring2seg.cpp new file mode 100644 index 0000000..9b86f21 --- /dev/null +++ b/boring2seg.cpp @@ -0,0 +1,311 @@ +#include + +#include + +#ifdef _MSC_VER +#include +#endif + +namespace atcoder { + +namespace internal { + +// @param n `0 <= n` +// @return minimum non-negative `x` s.t. `n <= 2**x` +int ceil_pow2(int n) { + int x = 0; + while ((1U << x) < (unsigned int)(n)) x++; + return x; +} + +// @param n `1 <= n` +// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` +int bsf(unsigned int n) { +#ifdef _MSC_VER + unsigned long index; + _BitScanForward(&index, n); + return index; +#else + return __builtin_ctz(n); +#endif +} + +} // namespace internal + +} // namespace atcoder + +#include +#include +#include +namespace atcoder { + +template +struct lazy_segtree { + public: + lazy_segtree() : lazy_segtree(0) {} + lazy_segtree(int n) : lazy_segtree(std::vector(n, e())) {} + lazy_segtree(const std::vector& v) : _n(int(v.size())) { + log = internal::ceil_pow2(_n); + size = 1 << log; + d = std::vector(2 * size, e()); + lz = std::vector(size, id()); + for (int i = 0; i < _n; i++) d[size + i] = v[i]; + for (int i = size - 1; i >= 1; i--) { + update(i); + } + } + + void set(int p, S x) { + assert(0 <= p && p < _n); + p += size; + for (int i = log; i >= 1; i--) push(p >> i); + d[p] = x; + for (int i = 1; i <= log; i++) update(p >> i); + } + + S get(int p) { + assert(0 <= p && p < _n); + p += size; + for (int i = log; i >= 1; i--) push(p >> i); + return d[p]; + } + + S prod(int l, int r) { + assert(0 <= l && l <= r && r <= _n); + if (l == r) return e(); + + l += size; + r += size; + + for (int i = log; i >= 1; i--) { + if (((l >> i) << i) != l) push(l >> i); + if (((r >> i) << i) != r) push(r >> i); + } + + S sml = e(), smr = e(); + while (l < r) { + if (l & 1) sml = op(sml, d[l++]); + if (r & 1) smr = op(d[--r], smr); + l >>= 1; + r >>= 1; + } + + return op(sml, smr); + } + + S all_prod() { return d[1]; } + + void apply(int p, F f) { + assert(0 <= p && p < _n); + p += size; + for (int i = log; i >= 1; i--) push(p >> i); + d[p] = mapping(f, d[p]); + for (int i = 1; i <= log; i++) update(p >> i); + } + void apply(int l, int r, F f) { + assert(0 <= l && l <= r && r <= _n); + if (l == r) return; + + l += size; + r += size; + + for (int i = log; i >= 1; i--) { + if (((l >> i) << i) != l) push(l >> i); + if (((r >> i) << i) != r) push((r - 1) >> i); + } + + { + int l2 = l, r2 = r; + while (l < r) { + if (l & 1) all_apply(l++, f); + if (r & 1) all_apply(--r, f); + l >>= 1; + r >>= 1; + } + l = l2; + r = r2; + } + + for (int i = 1; i <= log; i++) { + if (((l >> i) << i) != l) update(l >> i); + if (((r >> i) << i) != r) update((r - 1) >> i); + } + } + + template int max_right(int l) { + return max_right(l, [](S x) { return g(x); }); + } + template int max_right(int l, G g) { + assert(0 <= l && l <= _n); + assert(g(e())); + if (l == _n) return _n; + l += size; + for (int i = log; i >= 1; i--) push(l >> i); + S sm = e(); + do { + while (l % 2 == 0) l >>= 1; + if (!g(op(sm, d[l]))) { + while (l < size) { + push(l); + l = (2 * l); + if (g(op(sm, d[l]))) { + sm = op(sm, d[l]); + l++; + } + } + return l - size; + } + sm = op(sm, d[l]); + l++; + } while ((l & -l) != l); + return _n; + } + + template int min_left(int r) { + return min_left(r, [](S x) { return g(x); }); + } + template int min_left(int r, G g) { + assert(0 <= r && r <= _n); + assert(g(e())); + if (r == 0) return 0; + r += size; + for (int i = log; i >= 1; i--) push((r - 1) >> i); + S sm = e(); + do { + r--; + while (r > 1 && (r % 2)) r >>= 1; + if (!g(op(d[r], sm))) { + while (r < size) { + push(r); + r = (2 * r + 1); + if (g(op(d[r], sm))) { + sm = op(d[r], sm); + r--; + } + } + return r + 1 - size; + } + sm = op(d[r], sm); + } while ((r & -r) != r); + return 0; + } + + private: + int _n, size, log; + std::vector d; + std::vector lz; + + void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } + void all_apply(int k, F f) { + d[k] = mapping(f, d[k]); + if (k < size) lz[k] = composition(f, lz[k]); + } + void push(int k) { + all_apply(2 * k, lz[k]); + all_apply(2 * k + 1, lz[k]); + lz[k] = id(); + } +}; + +} // namespace atcoder + + +using namespace atcoder; +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +ll min_v(ll a, ll b) { + return min(a, b); +} + +ll e() { + return 1e9; +} + +ll mapping(ll f, ll x) { + return f + x; +} + +ll composition(ll f, ll g) { + return f + g; +} + +ll id() { + return 0; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, m; + cin >> n >> m; + + vector> a(n); + for(ll i = 0; i < n; i++) { + cin >> a[i][1] >> a[i][2] >> a[i][0]; + } + + sort(a.begin(), a.end()); + + ll mn = 1e9; + vector weird(m); + + lazy_segtree st(weird); + + function conn = [&]() { + // get min on segtree and check if its > 0 + return st.prod(0, m - 1) > 0; + }; + + function add = [&](ll l, ll r) { + // add on [l, r] + st.apply(l - 1, r - 1, 1); + }; + + function rem = [&](ll l, ll r) { + // sub on [l, r] + st.apply(l - 1, r - 1, -1); + }; + + ll r = 0; + for (ll l = 0; l < n; l++) { + while(!conn() && r < n) { + add(a[r][1], a[r][2]); + //for(int i = 0; i < m; i++) { + // cout << st.get(i) << endl; + //} + //cout << endl; + //cout << "M " << st.prod(0, m - 1) << endl; + //cout << endl; + r++; + } + + if (conn()) { + mn = min(mn, a[r - 1][0] - a[l][0]); + //cout << "DF " << l << " " << r - 1 << endl; + } + rem(a[l][1], a[l][2]); + //cout << "EW " << mn << " " << r-1 << " " << l << endl; + //for (int i = 0; i < m; i++) { + //cout << st.get(i) << endl; + //} + //cout << endl; + } + + cout << mn << endl; + + return 0; +} + diff --git a/boringseg.cpp b/boringseg.cpp new file mode 100644 index 0000000..0ffb27a --- /dev/null +++ b/boringseg.cpp @@ -0,0 +1,80 @@ +#include +#include + +using namespace atcoder; +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +ll min_v(ll a, ll b) { + return min(a, b); +} + +ll e() { + return 0; +} + +ll mapping(ll f, ll x) { + return f + x; +} + +ll composition(ll f, ll g) { + return f + g; +} + +ll id() { + return 0; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, m; + cin >> n >> m; + + vector> a(n); + for(ll i = 0; i < n; i++) { + cin >> a[i][1] >> a[i][2] >> a[i][0]; + } + + sort(a.begin(), a.end()); + + ll r = 0; + ll mn = 1e9; + + lazy_segtree st; + + function conn = [&]() { + // get min on segtree and check if its > 0 + return st.prod(0, m) > 0; + }; + + function add = [&](ll l, ll r) { + // add on [l, r] + st.apply(l, r, 1); + }; + + function rem = [&](ll l, ll r) { + // sub on [l, r] + st.apply(l, r, -1); + }; + + for (ll l = 0; l < n; l++) { + while(!conn() && r < n) { + add(a[r][1], a[r][2]); + + r++; + } + + if (conn()) mn = min(mn, a[r][0] - a[l][0]); + rem(a[l][1], a[l][2]); + l++; + } + + return 0; +} \ No newline at end of file diff --git a/changebrac.cpp b/changebrac.cpp new file mode 100644 index 0000000..0ec8c74 --- /dev/null +++ b/changebrac.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + string s; + cin >> s; + + n = s.length(); + + vector> p1(n + 1); + for (int i = 0; i < n; i++) { + p1[i + 1][0] = p1[i][0]; + p1[i + 1][1] = p1[i][1]; + if (s[i] == '[' || s[i] == ']') { + p1[i + 1][i % 2]++; + } + } + + ll q; + cin >> q; + for (int i = 0; i < q; i++) { + int l, r; + cin >> l >> r; + + ll ev = p1[r][0] - p1[l - 1][0]; + ll odd = p1[r][1] - p1[l - 1][1]; + + cout << abs(ev - odd) << endl; + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/chasesubway.cpp b/chasesubway.cpp new file mode 100644 index 0000000..80309b2 --- /dev/null +++ b/chasesubway.cpp @@ -0,0 +1,84 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const int maxn = 32767+8; +const int INF = 1e9; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vector> ed(maxn); + + for (int i = 0; i < n; i++) { + int k; + cin >> k; + + int last = -1; + + for (int j = 0; j < k; j++) { + int x; + cin >> x; + + if (last != -1) { + ed[x].insert(last); + ed[last].insert(x); + } + + last = x; + } + } + + int m; + cin >> m; + + vi path(m); + for (int i = 0; i < m; i++) { + cin >> path[i]; + } + + int a = path[0]; + int b = path[m - 1]; + + vector> dist(maxn, {INF, INF}); + + for (int i = 0; i < 2; i++) { + queue q; + q.push(i ? b : a); + + vi vis(maxn); + vis[q.front()] = true; + dist[q.front()][i] = 0; + + while (!q.empty()) { + ll top = q.front(); + //cout << top << endl; + q.pop(); + + for (int c : ed[top]) { + if (vis[c]) continue; + vis[c] = true; + q.push(c); + + dist[c][i] = min(dist[c][i], dist[top][i] + 1); + } + } + } + + for (int v = 1; v < maxn; v++) { + //if(dist[v][1] != INF) cout << v << " " << dist[v][0] << " " << dist[v][1] << endl; + + if ((dist[v][1] != INF) && ((m - 1) + dist[v][1]) == dist[v][0]) cout << v << endl; + } + + return 0; +} \ No newline at end of file diff --git a/crazyrobot.cpp b/crazyrobot.cpp new file mode 100644 index 0000000..6fbf568 --- /dev/null +++ b/crazyrobot.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n, m; + cin >> n >> m; + pi lab; + vector grid(n, vb(m)); + for (int i = 0; i < n; i++) { + for(int j = 0; j < m; j++) { + char c; + if (c == '#') continue; + grid[i][j] = true; + if (c == 'L') lab = {i, j}; + } + } + +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/deca.cpp b/deca.cpp new file mode 100644 index 0000000..9ed0bba --- /dev/null +++ b/deca.cpp @@ -0,0 +1,109 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll INF = 1e9+8; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, m, k; + cin >> k >> m >> n; + + vpi patch(k); + + for (ll i = 0; i < k; i++) { + ll a, b; + cin >> a >> b; + patch[i] = {a, b}; + } + + vi bad(m); + for (ll i = 0; i < m; i++) { + cin >> bad[i]; + } + + vpi ans(m + 1); + + ll j = 0; + for (ll i = 0; i < m; i++) { + vector p; + while(j < k && patch[j].first < bad[i]) { + p.push_back(j); + j++; + } + + ll sum = 0; + for (ll v : p) sum += patch[v].second; + + ans[i].second = sum; + + if(i == 0) { + ans[i].first = sum; + + continue; + } + + priority_queue> ev; + + ll l = bad[i - 1]; + ll r = bad[i]; + for (ll v : p) { + ll pos = patch[v].first; + ll taste = patch[v].second; + + ll startpos = pos - (r - pos) + 1; + ll endpos = pos + (pos - l) - 1; + + ev.push({-startpos, 1, taste}); + ev.push({-endpos, -1, taste}); + + + } + + ll best = 0; + ll cursum = 0; + while (!ev.empty()) { + array top = ev.top(); + ev.pop(); + + cursum += top[1] * top[2]; + + best = max(best, cursum); + } + + ans[i].first = best; + } + + while (j < k) { + ans[m].first += patch[j].second; + ans[m].second += patch[j++].second; + } + + vector dp(m + 2, vi(n + 1)); + for (ll i = 0; i <= m; i++) { + for (ll h = 0; h <= n; h++) { + if (h > 1) dp[i + 1][h - 2] = max(dp[i + 1][h - 2], dp[i][h] + ans[i].second); + if (h > 0) dp[i + 1][h - 1] = max(dp[i + 1][h - 1], dp[i][h] + ans[i].first); + + + } + } + + ll ansb = 0; + for (ll i = 0; i <= m + 1; i++) { + for (ll h = 0; h <= n; h++) { + ansb = max(ansb, dp[i][h]); + } + } + + cout << ansb << endl; + + return 0; +} \ No newline at end of file diff --git a/decb.cpp b/decb.cpp new file mode 100644 index 0000000..f124f45 --- /dev/null +++ b/decb.cpp @@ -0,0 +1,109 @@ +#include +using namespace std; + +using ll = long long; +using vi = vector; +using vb = vector; + +const ll INF = 1e9; + +void solve() { + ll n, m; + cin >> n >> m; + + vector> ed(n); + + for (ll i = 0; i < m; i++) { + ll a, b; + cin >> a >> b; + a--, b--; + + ed[a].insert(b); + ed[b].insert(a); + } + + vb vis(n); + + vi comp(n); + ll cur_comp = 0; + function dfs = [&](ll v) { + if (vis[v]) return; + + comp[v] = cur_comp; + vis[v] = true; + + for (ll ch : ed[v]) { + dfs(ch); + } + }; + + for (ll i = 0; i < n; i++) { + if (vis[i]) continue; + dfs(i); + cur_comp++; + } + + set set1; + set setn; + + if (comp[0] == comp[n - 1]) { + cout << 0 << endl; + return; + } + + for (ll i = 0; i < n; i++) { + if (comp[0] == comp[i]) set1.insert(i); + if (comp[n - 1] == comp[i]) setn.insert(i); + } + + vi min1(n, INF); + vi minn(n, INF); + + for (ll i = 0; i < n; i++) { + auto it = set1.lower_bound(i); + if (it == set1.end()) continue; + ll val = *it - i; + min1[comp[i]] = min(min1[comp[i]], val * val); + } + + for (ll i = 0; i < n; i++) { + auto it = set1.lower_bound(i); + if (it == set1.begin()) continue; + --it; + ll val = *it - i; + min1[comp[i]] = min(min1[comp[i]], val * val); + } + + for (ll i = 0; i < n; i++) { + auto it = setn.lower_bound(i); + if (it == setn.end()) continue; + ll val = *it - i; + minn[comp[i]] = min(minn[comp[i]], val * val); + } + + for (ll i = 0; i < n; i++) { + auto it = setn.lower_bound(i); + if (it == setn.begin()) continue; + --it; + ll val = *it - i; + minn[comp[i]] = min(minn[comp[i]], val * val); + } + + ll ans = INF; + for (ll i = 0; i < n; i++) { + ans = min(ans, minn[i] + min1[i]); + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/decc.cpp b/decc.cpp new file mode 100644 index 0000000..5f9c34c --- /dev/null +++ b/decc.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +using ll = long long; +using vi = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, m; + cin >> n >> m; + + vi cnta(m + 1); + vi cntb(m + 1); + + for (ll i = 0; i < n; i++) { + ll a, b; + cin >> a >> b; + cnta[a]++; + cntb[b]++; + } + + vi cnt2a(2 * m + 1); + vi cnt2b(2 * m + 1); + + for (ll i = 0; i < m + 1; i++) { + for (ll j = 0; j < m + 1; j++) { + cnt2a[i + j] += cnta[i] * cnta[j]; + cnt2b[i + j] += cntb[i] * cntb[j]; + } + } + + ll sum = 0; + for (ll i = 0; i < 2 * m + 1; i++) { + sum += cnt2a[i]; + cout << sum << endl; + sum -= cnt2b[i]; + } + + return 0; +} \ No newline at end of file diff --git a/dforthing.cpp b/dforthing.cpp new file mode 100644 index 0000000..9151e0e --- /dev/null +++ b/dforthing.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vi p(n + 1); + vi c(n + 1); + ll root; + for (ll i = 1; i <= n; i++) { + cin >> p[i] >> c[i]; + if (p[i] == 0) root = i; + } + + vector> ed(n + 1); + + for (ll i = 1; i <= n; i++) { + ed[p[i]].insert(i); + } + + bool works = true; + + function(ll)> dfs = [&](ll v) { + vector cur; + for (ll ch : ed[v]) { + vector chv = dfs(ch); + cur.insert(cur.begin(), chv.begin(), chv.end()); + } + + if (c[v] > cur.size()) { + works = false; + return vi{}; + } else { + cur.insert(cur.begin() + c[v], v); + //cout << "CUR: "; + //for (int i : cur) cout << i << " "; + //cout << endl; + //cout << endl; + return cur; + } + }; + + vi last = dfs(root); + + if(!works) { + cout << "NO" << endl; + } + else { + cout << "YES" << endl; + + vi ans(n); + + for (int i = 0; i < last.size(); i++) { + //cout << last[i] << endl; + ans[last[i] - 1] = i + 1; + } + + for(int i : ans) { + cout << i << " "; + } + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/dom.cpp b/dom.cpp new file mode 100644 index 0000000..d6c98b8 --- /dev/null +++ b/dom.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n, m, k; + cin >> m >> n >> k; + + bool works = false; + while (true) { + if(k % 2 == 0) { + if(m % 2 == 0 && n % 2 == 0) { + works = true; + break; + } + if(m % 2) { + if (k < n / 2) break; + m--; + k = k - (n / 2); + } + if(n % 2) { + swap(m, n); + k = (n * m) / 2 - k; + } + } else { + if (n % 2 == 0 && m % 2 == 0) break; + if(m % 2) { + if (k < n / 2) break; + m--; + k = k - (n / 2); + } + if(n % 2) { + swap(m, n); + k = (n * m) / 2 - k; + } + } + } + + cout << (works ? "YES" : "NO") << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/equi.cpp b/equi.cpp new file mode 100644 index 0000000..2d39e60 --- /dev/null +++ b/equi.cpp @@ -0,0 +1,67 @@ +#include + +#include +using namespace atcoder; +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +ll mapping(ll x, ll y) { + return min(x, y); +} + +ll mapping2(ll x, ll y) { + return max(x, y); +} + +ll e() { + return 0; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, q; + cin >> n >> q; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + } + + for (ll i = 0; i < n; i++) { + ll b; + cin >> b; + a[i] -= b; + } + + vi psum(n + 1); + + for (ll i = 1; i <= n; i++) { + psum[i] = psum[i - 1] + a[i]; + } + + segtree st(psum); + segtree st2(psum); + + for (ll i = 0; i < q; i++) { + ll l, r; + cin >> l >> r; + + ll mn = st.prod(l, r) - psum[l - 1]; + ll mx = st2.prod(l, r) - psum[l - 1]; + + ll sum = psum[r] - psum[l - 1]; + if (sum == 0 || mx > 0) cout << -1 << endl; + else { + cout << -mn << endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/equiseg.cpp b/equiseg.cpp new file mode 100644 index 0000000..1c06802 --- /dev/null +++ b/equiseg.cpp @@ -0,0 +1,221 @@ +#include + +#include + +#ifdef _MSC_VER +#include +#endif + +namespace atcoder { + +namespace internal { + +// @param n `0 <= n` +// @return minimum non-negative `x` s.t. `n <= 2**x` +int ceil_pow2(int n) { + int x = 0; + while ((1U << x) < (unsigned int)(n)) x++; + return x; +} + +// @param n `1 <= n` +// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` +int bsf(unsigned int n) { +#ifdef _MSC_VER + unsigned long index; + _BitScanForward(&index, n); + return index; +#else + return __builtin_ctz(n); +#endif +} + +} // namespace internal + +} // namespace atcoder + +#include +#include + +namespace atcoder { + +template +struct segtree { + public: + segtree() : segtree(0) {} + segtree(int n) : segtree(std::vector(n, e())) {} + segtree(const std::vector& v) : _n(int(v.size())) { + log = internal::ceil_pow2(_n); + size = 1 << log; + d = std::vector(2 * size, e()); + for (int i = 0; i < _n; i++) d[size + i] = v[i]; + for (int i = size - 1; i >= 1; i--) { + update(i); + } + } + + void set(int p, S x) { + assert(0 <= p && p < _n); + p += size; + d[p] = x; + for (int i = 1; i <= log; i++) update(p >> i); + } + + S get(int p) { + assert(0 <= p && p < _n); + return d[p + size]; + } + + S prod(int l, int r) { + assert(0 <= l && l <= r && r <= _n); + S sml = e(), smr = e(); + l += size; + r += size; + + while (l < r) { + if (l & 1) sml = op(sml, d[l++]); + if (r & 1) smr = op(d[--r], smr); + l >>= 1; + r >>= 1; + } + return op(sml, smr); + } + + S all_prod() { return d[1]; } + + template + int max_right(int l) { + return max_right(l, [](S x) { return f(x); }); + } + template + int max_right(int l, F f) { + assert(0 <= l && l <= _n); + assert(f(e())); + if (l == _n) return _n; + l += size; + S sm = e(); + do { + while (l % 2 == 0) l >>= 1; + if (!f(op(sm, d[l]))) { + while (l < size) { + l = (2 * l); + if (f(op(sm, d[l]))) { + sm = op(sm, d[l]); + l++; + } + } + return l - size; + } + sm = op(sm, d[l]); + l++; + } while ((l & -l) != l); + return _n; + } + + template + int min_left(int r) { + return min_left(r, [](S x) { return f(x); }); + } + template + int min_left(int r, F f) { + assert(0 <= r && r <= _n); + assert(f(e())); + if (r == 0) return 0; + r += size; + S sm = e(); + do { + r--; + while (r > 1 && (r % 2)) r >>= 1; + if (!f(op(d[r], sm))) { + while (r < size) { + r = (2 * r + 1); + if (f(op(d[r], sm))) { + sm = op(d[r], sm); + r--; + } + } + return r + 1 - size; + } + sm = op(d[r], sm); + } while ((r & -r) != r); + return 0; + } + + private: + int _n, size, log; + std::vector d; + + void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } +}; + +} // namespace atcoder + +using namespace atcoder; +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +ll mapping(ll x, ll y) { + return min(x, y); +} + +ll mapping2(ll x, ll y) { + return max(x, y); +} + +ll e() { + return 1e14; +} + +ll e2() { + return -1e14; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, q; + cin >> n >> q; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + } + + for (ll i = 0; i < n; i++) { + ll b; + cin >> b; + a[i] -= b; + } + + vi psum(n + 1); + + for (ll i = 1; i <= n; i++) { + psum[i] = psum[i - 1] + a[i - 1]; + } + + segtree st(psum); + segtree st2(psum); + + for (ll i = 0; i < q; i++) { + ll l, r; + cin >> l >> r; + + ll mn = st.prod(l, r+1) - psum[l - 1]; + ll mx = st2.prod(l, r+1) - psum[l - 1]; + + ll sum = psum[r] - psum[l - 1]; + if (sum != 0 || mx > 0) + cout << -1 << endl; + else { + cout << -mn << endl; + } + } + + return 0; +} diff --git a/exactchange.cpp b/exactchange.cpp new file mode 100644 index 0000000..b214220 --- /dev/null +++ b/exactchange.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll INF = 1e9; + +void solve() { + ll n; + cin >> n; + + vi a(n); + ll mx = 0; + for (ll i = 0; i < n; i++) { + cin >> a[i]; + mx = max(mx, a[i] / 3); + } + + ll best = INF; + + for (ll i = 0; i < 3; i++) { + for (ll j = 0; j < 3; j++) { + for (ll k = max(0LL, mx - 4); k <= mx; k++) { + ll cur_tot = i + j + k; + bool works = true; + for (ll l = 0; l < n; l++) { + bool mightwork = false; + for (ll x = 0; x <= i; x++) { + for (ll y = 0; y <= j; y++) { + ll v = (a[l] - x - 2 * y); + if (v >= 0 && v % 3 == 0 && v / 3 <= k) mightwork = true; + } + } + if (!mightwork) works = false; + } + if (works) best = min(best, cur_tot); + } + } + } + + cout << best << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/extext.cpp b/extext.cpp new file mode 100644 index 0000000..f54c885 --- /dev/null +++ b/extext.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll mod = 998244353; + +void solve() { + ll n; + cin >> n; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + } + + vpi f; + ll ans = 0; + ll num_break = 0; + for (ll i = n - 1; i >= 0; i--) { + vpi f2; + ll bk = 0; + + for (pi it : f) { + ll v = it.first; + ll cnt = it.second; + + ll x = ceil((double)a[i] / v); + ll first = floor((double)a[i] / x); + + if (first != bk) f2.push_back({first, 0}); + bk = first; + f2.back().second += cnt; + + num_break += (cnt % mod) * (x - 1); + num_break %= mod; + } + + reverse(f2.begin(), f2.end()); + ll first = a[i]; + + if (f2.size() == 0 || first != f2.back().first) f2.push_back({first, 0}); + f2.back().second++; + + ans += num_break; + ans %= mod; + + f = f2; + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/firsta.cpp b/firsta.cpp new file mode 100644 index 0000000..abd543d --- /dev/null +++ b/firsta.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll a, b, c; + cin >> a >> b >> c; + ll sum = a + b + c; + ll ops = sum / 9; + + cout << ((sum % 9 == 0 && min(a, min(b, c)) >= ops) ? "YES" : "NO") << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/firstb.cpp b/firstb.cpp new file mode 100644 index 0000000..901d1d3 --- /dev/null +++ b/firstb.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vi a(n); + for(ll i = 0; i < n; i++) { + cin >> a[i]; + } + + vi b(n); + vi c(n); + + ll bsum = 0; + ll csum = 0; + + for (ll i = 0; i < n; i++) { + if (i % 2) b[i] = a[i], c[i] = 1; + else + b[i] = 1, c[i] = a[i]; + + bsum += abs(b[i] - a[i]); + csum += abs(c[i] - a[i]); + } + + if(csum < bsum) { + for (ll i : c) cout << i << " "; + } else { + for (ll i : b) cout << i << " "; + } + cout << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/firstc.cpp b/firstc.cpp new file mode 100644 index 0000000..6f319f4 --- /dev/null +++ b/firstc.cpp @@ -0,0 +1,93 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vpi a(n); + for(ll i = 0; i < n; i++) { + cin >> a[i].first >> a[i].second; + } + + sort(a.begin(), a.end()); + + ll goal = 0; + ll goalpt = 0; + ll last = 0; + ll lasttime = 0; + + ll ans = 0; + + for (ll i = 0; i < n; i++) { + // figure out where we are + ll curtime = a[i].first; + ll cur = last + goal * (curtime - lasttime); + + //cout << "TIME " << curtime << " " << cur << endl; + + // figure out if we reached goal + bool reached = false; + if (goal == -1) { + if(cur <= goalpt) { + reached = true; + } + } else if(goal == 1) { + if (cur >= goalpt) reached = true; + } else if (goal == 0) + reached = true; + + if (reached) cur = goalpt; + //cout << "REACHED " << reached << endl; + + if(i > 0) { + ll lastgoal = a[i - 1].second; + if (lastgoal <= max(cur, last) && lastgoal >= min(cur, last)) { + ans++; + //cout << i - 1 << endl; + } + } + + last = cur; + if (reached) { + goal = (a[i].second - last) / abs(a[i].second - last); + goalpt = a[i].second; + if (i == n - 1) { + ans++; + //cout << n - 1 << endl; + } + } else if(i == n - 1) { + if (a[i].second <= max(cur, goalpt) && a[i].second >= min(cur, goalpt)) { + ans++; + //cout << i << endl; + } + } + + lasttime = curtime; + } + + + + cout << ans << endl; + //cout << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) { + //cout << "CASE " << t << endl; + solve(); + } + + return 0; +} \ No newline at end of file diff --git a/firstd.cpp b/firstd.cpp new file mode 100644 index 0000000..6c8c9bc --- /dev/null +++ b/firstd.cpp @@ -0,0 +1,112 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + vi b(n); + vb vis(2 * n + 1); + for (ll i = 0; i < n; i++) { + cin >> b[i]; + vis[b[i]] = true; + } + + vi c(n); + ll lst = 0; + for (ll i = 1; i <= 2 * n; i++) { + if (!vis[i]) c[lst++] = i; + } + + vector> pos(n + 1); + for(ll j = 0; j < 2; j++) { + vi num(n); + ll lsti = n - 1; + ll summ = 0; + for (ll i = 2 * n; i > 0; i--) { + if (!vis[i]) summ++; + else num[lsti--] = summ; + } + + vi maxk(n); + for (ll i = 0; i < n; i++) { + maxk[i] = num[i] + i; + //cout << num[i] << " " << maxk[i] << endl; + } + + pos[0][j] = true; + ll sweepmin = 1e9; + for (ll i = 0; i < n; i++) { + ll k = i + 1; + sweepmin = min(sweepmin, maxk[i]); + if (k > sweepmin) break; + pos[k][j] = true; + } + + vis.clear(); + vis.resize(2 * n + 1); + for (ll i = 0; i < n; i++) { + b[i] = 2*n - b[i] + 1; + vis[b[i]] = true; + } + c.clear(); + c.resize(n); + + lst = 0; + for (ll i = 1; i <= 2 * n; i++) { + if (!vis[i]) c[lst++] = i; + } + //cout << endl; + } + + /* + + vi num2(n); + ll lsti2 = 0; + ll summ2 = 0; + for (ll i = 1; i <= 2 * n; i++) { + if (!vis[i]) summ2++; + else num2[lsti2++] = summ2; + } + + vi maxk2(n); + for (ll i = 0; i < n; i++) { + maxk2[i] = num2[i] + (n - i); + } + + vi pos2(n + 1); + pos2[n] = true; + ll sweepmin2 = 1e9; + for (ll i = n - 1; i >= 0; i--) { + ll k = i - 1; + sweepmin2 = min(sweepmin2, num2[i]); + if (k > sweepmin2) break; + pos2[k] = true; + } + */ + + ll ans = 0; + for (ll i = 0; i <= n; i++) { + //cout << pos[i][0] << " " << pos[n - i][1] << endl; + if (pos[i][0] && pos[n - i][1]) ans++; + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/garland.cpp b/garland.cpp new file mode 100644 index 0000000..8b977da --- /dev/null +++ b/garland.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll INF = 1e9; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vi a(n + 1); + vb exi(n); + for (int i = 0; i < n; i++) { + cin >> a[i + 1]; + a[i + 1]--; + if (a[i + 1] == -1) continue; + exi[a[i + 1]] = true; + a[i + 1] %= 2; + } + + int num_0 = 0; + int num_1 = 0; + + for(int i = 0; i < n; i++) { + if(!exi[i]) { + if(i % 2) { + num_1++; + } else + num_0++; + } + } + + // index last elem in prefix, num of 1 in prefix USED, parity of last elem in prefix + vector>>> dp(n+1, vector>>(n, vector>(n, array{INF, INF}))); + + dp[0][0][0][0] = 0; + dp[0][0][0][1] = 0; + + for(int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + for (int m = 0; m < n; m++) { + for (int k = 0; k < 2; k++) { + if (dp[i][j][m][k] == INF) continue; + if (a[i + 1] == -1) { + // next thing unset + + // set to 1 + if (j < num_1) dp[i + 1][j+1][m][1] = min(dp[i + 1][j+1][m][1], dp[i][j][m][k] + (k ^ 1)); + + // set to 0 + if (m < num_0) dp[i + 1][j][m+1][0] = min(dp[i + 1][j][m+1][0], dp[i][j][m][k] + (k ^ 0)); + + } else if (a[i + 1] == 0) { + // next thing has parity of 0 + dp[i + 1][j][m][0] = min(dp[i + 1][j][m][0], dp[i][j][m][k] + (k ^ 0)); + } else if (a[i + 1] == 1) { + // next thing has parity of 1 + dp[i + 1][j][m][1] = min(dp[i + 1][j][m][1], dp[i][j][m][k] + (k ^ 1)); + } + } + } + } + } + + cout << min(dp[n][num_1][num_0][0], dp[n][num_1][num_0][1]) << endl; + + return 0; +} \ No newline at end of file diff --git a/gcdweird.cpp b/gcdweird.cpp new file mode 100644 index 0000000..d751a95 --- /dev/null +++ b/gcdweird.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + ll ma = 0; + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + ma = max(ma, a[i]); + } + ma += 8; + ma *= 2; + + vi th(ma); + for (ll i = 0; i < n; i++) { + th[a[i]]++; + } + + vi cnt(ma); + + for (ll x = 1; x < ma; x++) { + for (ll m = 1; m * x < ma; m++) { + cnt[x] += th[m * x]; + } + } + + vi dp(ma); + ll best = 0; + for (ll x = 1; x < ma; x++) { + for (ll m = 2; m * x < ma; m++) { + dp[m * x] = max(dp[m * x], dp[x] + x * (cnt[x] - cnt[m * x])); + best = max(best, dp[m * x] + cnt[m * x]); + } + } + + cout << best << endl; + + return 0; +} \ No newline at end of file diff --git a/gcdweirdeas.cpp b/gcdweirdeas.cpp new file mode 100644 index 0000000..928736b --- /dev/null +++ b/gcdweirdeas.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const int ma = 20000002; +ll cnt[ma + 8]; +long long dp[ma + 8]; +ll primes[ma + 8]; +ll vis[ma + 8]; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + } + + for (ll i = 0; i < n; i++) { + cnt[a[i]]++; + } + + ll lst = 0; + // find p-fact of the numbers ans use it to upd primes freq + for (long long i = 2; i <= ma; i++) { + if (vis[i] || i * i > ma) continue; + primes[lst++] = i; + for (ll j = i * i; j <= ma; j += i) { + vis[j] = true; + } + } + + for (int i = 1; i <= ma; ++i) + for (int j = 2 * i; j <= ma; j += i) + cnt[i] += cnt[j]; + + long long best = 0; + for (ll x = 1; x <= ma; x++) + for (ll v = 0; (v < lst) && (x * primes[v] <= ma); v++) + dp[primes[v] * x] = max(dp[primes[v] * x], dp[x] + (long long)x * (cnt[x] - cnt[primes[v] * x])); + + for (ll x = 1; x <= ma; x++) + best = max(best, dp[x] + x * cnt[x]); + + cout << best << endl; + + return 0; +} \ No newline at end of file diff --git a/gen.cpp b/gen.cpp index c68fd93..afc599d 100644 --- a/gen.cpp +++ b/gen.cpp @@ -6,7 +6,20 @@ int rnd(int a, int b) { int main() { srand(time(NULL)); - int a = rnd(1, 10); - int b = rnd(1, 10); - cout << a << " " << b << endl; + int n = rnd(1, 10); + + cout << n << endl; + + int lst = 2; + set ys; + for (int i = 0; i < n; i++) { + int x = rnd(lst, 1e3 + lst); + + int y = rnd(1, x - 1); + while (ys.find(y) != ys.end()) y = rnd(1, x - 1); + + lst = x + 1; + + cout << x << " " << y << " " << rnd(0, 1) << endl; + } } \ No newline at end of file diff --git a/goat.cpp b/goat.cpp new file mode 100644 index 0000000..c485744 --- /dev/null +++ b/goat.cpp @@ -0,0 +1,98 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +struct DSU { + vi par; + vi sz; + ll tot; + void init(ll n) { + for (ll i = 0; i < n; i++) { + par.push_back(i); + sz.push_back(1); + tot = 0; + } + } + ll get_rep(ll node) { + if (par[node] == node) return node; + return get_rep(par[node]); + } + ll get_sz(ll node) { + return sz[get_rep(node)]; + } + bool check_con(ll a, ll b) { + return get_rep(a) == get_rep(b); + } + void unite(ll a, ll b) { + ll x = get_rep(a); + ll y = get_rep(b); + if (x == y) return; + if (sz[x] < sz[y]) { + par[x] = y; + tot -= sz[x] / 2; + tot -= sz[y] / 2; + sz[y] += sz[x]; + tot += sz[y] / 2; + } else { + par[y] = x; + tot -= sz[x] / 2; + tot -= sz[y] / 2; + sz[x] += sz[y]; + tot += sz[x] / 2; + } + } +}; + +void solve() { + ll n; + cin >> n; + + vi a(n); + for (int i = 0; i < n; i++) { + cin >> a[i]; + } + + sort(a.begin(), a.end()); + + priority_queue q; + DSU dsu; + dsu.init(n); + + ll best = n; + for (int i = n - 1; i >= 0; i--) { + ll ans = i; + + while (!q.empty()) { + if (q.top().first <= a[i]) break; + pi top = q.top(); + + q.pop(); + + ll v = top.second; + dsu.unite(v, v + 1); + } + + if(i > 0) q.push({2 * a[i - 1] - a[i], i-1}); + + ans += dsu.tot; + best = min(best, ans); + } + + cout << best << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/guesstheperm.cpp b/guesstheperm.cpp new file mode 100644 index 0000000..5a7e40a --- /dev/null +++ b/guesstheperm.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n; + cin >> n; + + function query = [&](int l, int r) { + int ans = 0; + cout << "? " << l << " " << r << endl; + cin >> ans; + return ans; + }; + + int mx = query(1, n); + + int lo = 1; + int hi = n; + while (lo <= hi) { + int mid = (lo + hi) >> 1; + if (query(mid, n) < mx) + hi = mid - 1; + else + lo = mid + 1; + } + + ll i = hi; + + assert(i != n); + + ll dif = query(i, n) - query(i + 1, n); + + ll j = i + 1 + dif; + + dif = query(j, n) - query(j + 1, n); + + ll k = j + dif; + + cout << "! " << i << " " << j << " " << k << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/intpro.cpp b/intpro.cpp new file mode 100644 index 0000000..d1475d7 --- /dev/null +++ b/intpro.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + ll last = 0; + + ll sum = 0; + + function q = [&](ll c) { + cout << "+ " << c << endl; + sum += c; + ll res; + cin >> res; + return res; + }; + + ll lo = 0; + ll ans = 0; + for (ll bit = 9; bit >= 0; bit--) { + if ((1 << bit) >= n) continue; + ll val = (1 << bit) + lo; + if (val >= n) val -= n, last--; + + ll res = q(val); + lo = 0; + if (res > last) { + lo = n - (1 << bit); + last++; + } else { + ans += (1 << bit); + } + } + + cout << "! " << n - (ans + 1) + sum << endl; + + return 0; +} \ No newline at end of file diff --git a/mathtest.cpp b/mathtest.cpp new file mode 100644 index 0000000..d37d17c --- /dev/null +++ b/mathtest.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +using ll = int; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n, m; + cin >> n >> m; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + } + + vector s(n, vi(m)); + for (ll i = 0; i < n; i++) { + for (ll j = 0; j < m; j++) { + char c; + cin >> c; + s[i][j] = c - '0'; + } + } + + ll best = -1; + vi bestp(m); + for (ll msk = 0; msk < 1 << n; msk++) { + ll ans = 0; + + for (ll i = 0; i < n; i++) { + ans += a[i] * (msk & (1 << i) ? -1 : 1); + } + + vector coeff(22); + for (ll j = 0; j < m; j++) { + ll cnt = 0; + for (ll i = 0; i < n; i++) { + cnt += s[i][j] * (msk & (1 << i) ? 1 : -1); + } + coeff[cnt + 10].push_back(j); + } + + vi p(m); + ll curr = 1; + for (ll i = 0; i < 22; i++) { + for(ll j : coeff[i]) { + ans += (i - 10) * curr; + p[j] = curr; + + curr++; + } + } + + if (ans > best) { + best = ans; + bestp = p; + } + } + + for (ll i : bestp) { + cout << i << " "; + } + cout << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/mincov.cpp b/mincov.cpp new file mode 100644 index 0000000..74a0f2c --- /dev/null +++ b/mincov.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const int INF = 1e9; + +void solve() { + ll n; + cin >> n; + + vi a(n); + for(int i = 0; i < n; i++) { + cin >> a[i]; + } + + vector dp(n + 1, vi(2e3 + 1, INF)); + + dp[0][0] = 0; + + for (int i = 0; i < n; i++) { + for (int j = 0; j <= 2e3; j++) { + if(dp[i][j] == INF) continue; + + ll l = j; + ll r = dp[i][j]; + + // move left + ll newl = l - a[i]; + newl = max(newl, 0LL); + dp[i + 1][newl] = min(dp[i + 1][newl], r + a[i]); + + // move right + ll newr = r - a[i]; + newr = max(newr, 0LL); + if (l + a[i] <= 2e3) dp[i + 1][l + a[i]] = min(dp[i + 1][l + a[i]], newr); + } + } + + ll best = INF; + for (int i = 0; i <= 2e3; i++) { + best = min(best, dp[n][i] + i); + } + + cout << best << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while(t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/minesweep.cpp b/minesweep.cpp new file mode 100644 index 0000000..a32c775 --- /dev/null +++ b/minesweep.cpp @@ -0,0 +1,101 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +void solve() { + ll n, k; + cin >> n >> k; + + vector> p(n); + map> mp1; + map> mp2; + for (ll i = 0; i < n; i++) { + ll x, y, t; + cin >> x >> y >> t; + p[i][0] = x; + p[i][1] = y; + p[i][2] = t; + mp1[x][y] = i; + mp2[y][x] = i; + } + + vb vis(n); + ll cc = 0; + vi comp(n); + vi compmin(n, 1e9); + + function dfs = [&](ll v) { + if (vis[v]) return; + vis[v] = true; + comp[v] = cc; + compmin[cc] = min(compmin[cc], p[v][2]); + + auto it = mp1[p[v][0]]; + auto it2 = it.find(p[v][1]); + if (it2 != it.begin()) { + it2--; + if (p[v][1] - p[(*it2).second][1] <= k) { + dfs((*it2).second); + } + it2++; + } + + it2++; + if (it2 != it.end() && p[(*it2).second][1] - p[v][1] <= k) { + dfs((*it2).second); + } + + it = mp2[p[v][1]]; + it2 = it.find(p[v][0]); + if (it2 != it.begin()) { + it2--; + if (p[v][0] - p[(*it2).second][0] <= k) { + dfs((*it2).second); + } + it2++; + } + it2++; + if (it2 != it.end() && p[(*it2).second][0] - p[v][0] <= k) { + dfs((*it2).second); + } + }; + + for (ll i = 0; i < n; i++) { + if (vis[i]) continue; + + dfs(i); + cc++; + } + + vi path; + for (ll i : compmin) { + if (cc == 0) break; + cc--; + path.push_back(i); + } + + sort(path.begin(), path.end()); + + ll ans = 0; + for (ll i = 0; i < path.size(); i++) { + ans = max(ans, min((ll)path.size() - 1 - i, path[i])); + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/minor.cpp b/minor.cpp new file mode 100644 index 0000000..6719d38 --- /dev/null +++ b/minor.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +using ll = int; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +struct DSU { + vi par; + vi sz; + void init(ll n) { + par.clear(); + sz.clear(); + for (ll i = 0; i < n; i++) { + par.push_back(i); + sz.push_back(1); + } + } + ll get_rep(ll node) { + if (par[node] == node) return node; + return get_rep(par[node]); + } + bool check_con(ll a, ll b) { + return get_rep(a) == get_rep(b); + } + void unite(ll a, ll b) { + ll x = get_rep(a); + ll y = get_rep(b); + if (x == y) return; + if (sz[x] < sz[y]) { + par[x] = y; + sz[y] += sz[x]; + } else { + par[y] = x; + sz[x] += sz[y]; + } + } +}; + +void solve() { + ll n, m; + cin >> n >> m; + + vector> ed(m); + vb bad(m); + for (ll i = 0; i < m; i++) { + ll u, v, w; + cin >> u >> v >> w; + u--, v--; + ed[i] = {u, v, w}; + } + + DSU dsu; + + ll ans = 0; + + for (ll bit = 30; bit >= 0; bit--) { + dsu.init(n); + + for (ll i = 0; i < m; i++) { + if (bad[i]) continue; + ll w = ed[i][2] & (1 << bit); + + if (w == 0) dsu.unite(ed[i][0], ed[i][1]); + } + + if (dsu.sz[dsu.get_rep(0)] == n) { + for (ll i = 0; i < m; i++) { + ll w = ed[i][2] & (1 << bit); + if (w != 0) bad[i] = true; + } + } else + ans += (1 << bit); + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/optins.cpp b/optins.cpp new file mode 100644 index 0000000..a576c54 --- /dev/null +++ b/optins.cpp @@ -0,0 +1,200 @@ +/* +TODO: Fix case where the best place to put something is before all values in ai + +(might work to change how the psum and blocks work) +*/ + +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +struct FenwickTree { + vector bit; // binary indexed tree + ll n; + + FenwickTree(ll n) { + this->n = n; + bit.assign(n, 0); + } + + FenwickTree(vector a) : FenwickTree(a.size()) { + for (size_t i = 0; i < a.size(); i++) + add(i, a[i]); + } + + ll sum(ll r) { + ll ret = 0; + for (; r >= 0; r = (r & (r + 1)) - 1) + ret += bit[r]; + return ret; + } + + ll sum(ll l, ll r) { + return sum(r) - sum(l - 1); + } + + void add(ll idx, ll delta) { + for (; idx < n; idx = idx | (idx + 1)) + bit[idx] += delta; + } +}; + +void solve() { + ll n, m; + cin >> n >> m; + + priority_queue> ev; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + ev.push({-a[i], i, 1}); + } + + vi b(m); + for (ll i = 0; i < m; i++) { + cin >> b[i]; + } + + sort(b.begin(), b.end()); + + vi swap(n, 1); + + vi path(m); + + vi psum(n + 1); + + for (ll i = 0; i <= n; i++) { + psum[i] = i; + } + + ll sq = sqrt(n + 1) + 1; + + // min for block, min index for block, sum of total block updates + vector> block(sq); + + for(ll i = 0; i < sq; i++) { + block[i] = {(ll)1e9, -1, 0}; + } + + for (ll i = 0; i < sq; i++) { + for (ll j = i * sq; j < min(n + 1, (i+1)*sq); j++) { + if (psum[j] <= block[i][0]) { + block[i][0] = psum[j]; + block[i][1] = j; + } + } + } + + function add_suf = [&](ll l, ll d) { + // add in block + ll cur_block = l / sq; + ll cur_idx = l % sq; + + for (ll i = cur_block * sq + cur_idx; i < min(n + 1, (cur_block + 1) * sq); i++) { + psum[i] += d; + if (psum[i] <= block[cur_block][0]) { + block[cur_block][0] = psum[i]; + block[cur_block][1] = i; + } + } + + // add over blocks + for (ll i = cur_block + 1; i < sq; i++) { + block[i][2] += d; + } + }; + + function min_idx = [&]() { + ll mn = 1e9; + ll mn_idx = -1; + for (ll i = 0; i < sq; i++) { + ll mn_block = block[i][0] + block[i][2]; + + if (mn_block <= mn) mn = mn_block, mn_idx = block[i][1]; + } + + assert(mn_idx != -1); + + return mn_idx; + }; + + for (ll c = 0; c < m; c++) { + while (!ev.empty()) { + array top = ev.top(); + ll v = -top[0]; + ll i = top[1]; + ll tp = top[2]; + if (v > b[c]) break; + ev.pop(); + ll dif; + if (v == b[c] && tp == 1) dif = -swap[i], swap[i] = 0, ev.push({-(v + 1), i, 0}); + if (v < b[c] || (v == b[c] && tp == 0)) dif = -1 - swap[i], swap[i] = -1; + + add_suf(i+1, dif); + } + + path[c] = min_idx(); + } + + // construct optimal array + vpi opt; + + for (ll i = 0; i < n; i++) { + opt.push_back({i, -1}); + } + + for (ll i = 0; i < m; i++) { + opt.push_back({path[i]-1, i}); + } + + sort(opt.begin(), opt.end()); + + vpi actual_opt(opt.size()); + for (ll i = 0; i < opt.size(); i++) { + pi v = opt[i]; + actual_opt[i] = {v.second == -1 ? a[v.first] : b[v.second], i}; + } + + sort(actual_opt.begin(), actual_opt.end()); + + map coord; + + for(ll i = 0; i < opt.size(); i++) { + ll v = actual_opt[i].first; + + coord[v] = i; + } + + sort(actual_opt.begin(), actual_opt.end(), [](pi x, pi y) { return x.second < y.second; }); + + // calc inversions on optimal array + FenwickTree ft(opt.size() + 8); + + ll ans = 0; + for (pi i : actual_opt) { + ll v = coord[i.first]; + // find all things > v + ans += ft.sum(v + 1, opt.size()+2); + // add v to the thing + ft.add(v, 1); + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/optinsbetter.cpp b/optinsbetter.cpp new file mode 100644 index 0000000..69b062a --- /dev/null +++ b/optinsbetter.cpp @@ -0,0 +1,173 @@ +/* +TODO: Fix case where the best place to put something is before all values in ai + +(might work to change how the psum and blocks work) +*/ + +#include +#include +using namespace std; +using namespace atcoder; + +using ll = int; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +struct FenwickTree { + vector bit; // binary indexed tree + ll n; + + FenwickTree(ll n) { + this->n = n; + bit.assign(n, 0); + } + + FenwickTree(vector a) : FenwickTree(a.size()) { + for (size_t i = 0; i < a.size(); i++) + add(i, a[i]); + } + + ll sum(ll r) { + ll ret = 0; + for (; r >= 0; r = (r & (r + 1)) - 1) + ret += bit[r]; + return ret; + } + + ll sum(ll l, ll r) { + return sum(r) - sum(l - 1); + } + + void add(ll idx, ll delta) { + for (; idx < n; idx = idx | (idx + 1)) + bit[idx] += delta; + } +}; + +pi mn(pi a, pi b) { + if (a.first == b.first) return a.second > b.second ? a : b; + return a.first < b.first ? a : b; +} + +pi e() { + return {1e9, -1}; +} + +pi mapping(ll d, pi x) { + return {x.first + d, x.second}; +} + +ll composition(ll f, ll g) { + return f + g; +} + +ll id() { + return 0; +} + +void solve() { + ll n, m; + cin >> n >> m; + + priority_queue> ev; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + ev.push({-a[i], i, 1}); + } + + vi b(m); + for (ll i = 0; i < m; i++) { + cin >> b[i]; + } + + sort(b.begin(), b.end()); + + vi swap(n, 1); + + vi path(m); + + vpi psum(n + 1); + + for (ll i = 0; i <= n; i++) { + psum[i] = {i, i}; + } + + lazy_segtree seg(psum); + + for (ll c = 0; c < m; c++) { + while (!ev.empty()) { + array top = ev.top(); + ll v = -top[0]; + ll i = top[1]; + ll tp = top[2]; + if (v > b[c]) break; + ev.pop(); + ll dif; + if (v == b[c] && tp == 1) dif = -swap[i], swap[i] = 0, ev.push({-(v + 1), i, 0}); + if (v < b[c] || (v == b[c] && tp == 0)) dif = -1 - swap[i], swap[i] = -1; + seg.apply(i + 1, n + 1, dif); + } + + path[c] = seg.all_prod().second; + } + + // construct optimal array + vpi opt; + + for (ll i = 0; i < n; i++) { + opt.push_back({i, -1}); + } + + for (ll i = 0; i < m; i++) { + opt.push_back({path[i]-1, i}); + } + + sort(opt.begin(), opt.end()); + + vpi actual_opt(opt.size()); + for (ll i = 0; i < opt.size(); i++) { + pi v = opt[i]; + actual_opt[i] = {v.second == -1 ? a[v.first] : b[v.second], i}; + } + + sort(actual_opt.begin(), actual_opt.end()); + + map coord; + + for(ll i = 0; i < opt.size(); i++) { + ll v = actual_opt[i].first; + + coord[v] = i; + } + + sort(actual_opt.begin(), actual_opt.end(), [](pi x, pi y) { return x.second < y.second; }); + + // calc inversions on optimal array + FenwickTree ft(opt.size() + 8); + + ll ans = 0; + for (pi i : actual_opt) { + ll v = coord[i.first]; + // find all things > v + ans += ft.sum(v + 1, opt.size()+2); + // add v to the thing + ft.add(v, 1); + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/optinsbetterseg.cpp b/optinsbetterseg.cpp new file mode 100644 index 0000000..5a1257d --- /dev/null +++ b/optinsbetterseg.cpp @@ -0,0 +1,389 @@ +/* +TODO: Fix case where the best place to put something is before all values in ai + +(might work to change how the psum and blocks work) +*/ + +#include + +#include + +#ifdef _MSC_VER +#include +#endif + +namespace atcoder { + +namespace internal { + +// @param n `0 <= n` +// @return minimum non-negative `x` s.t. `n <= 2**x` +int ceil_pow2(int n) { + int x = 0; + while ((1U << x) < (unsigned int)(n)) x++; + return x; +} + +// @param n `1 <= n` +// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` +int bsf(unsigned int n) { +#ifdef _MSC_VER + unsigned long index; + _BitScanForward(&index, n); + return index; +#else + return __builtin_ctz(n); +#endif +} + +} // namespace internal + +} // namespace atcoder + +#include +#include +#include +namespace atcoder { + +template +struct lazy_segtree { + public: + lazy_segtree() : lazy_segtree(0) {} + lazy_segtree(int n) : lazy_segtree(std::vector(n, e())) {} + lazy_segtree(const std::vector& v) : _n(int(v.size())) { + log = internal::ceil_pow2(_n); + size = 1 << log; + d = std::vector(2 * size, e()); + lz = std::vector(size, id()); + for (int i = 0; i < _n; i++) d[size + i] = v[i]; + for (int i = size - 1; i >= 1; i--) { + update(i); + } + } + + void set(int p, S x) { + assert(0 <= p && p < _n); + p += size; + for (int i = log; i >= 1; i--) push(p >> i); + d[p] = x; + for (int i = 1; i <= log; i++) update(p >> i); + } + + S get(int p) { + assert(0 <= p && p < _n); + p += size; + for (int i = log; i >= 1; i--) push(p >> i); + return d[p]; + } + + S prod(int l, int r) { + assert(0 <= l && l <= r && r <= _n); + if (l == r) return e(); + + l += size; + r += size; + + for (int i = log; i >= 1; i--) { + if (((l >> i) << i) != l) push(l >> i); + if (((r >> i) << i) != r) push(r >> i); + } + + S sml = e(), smr = e(); + while (l < r) { + if (l & 1) sml = op(sml, d[l++]); + if (r & 1) smr = op(d[--r], smr); + l >>= 1; + r >>= 1; + } + + return op(sml, smr); + } + + S all_prod() { return d[1]; } + + void apply(int p, F f) { + assert(0 <= p && p < _n); + p += size; + for (int i = log; i >= 1; i--) push(p >> i); + d[p] = mapping(f, d[p]); + for (int i = 1; i <= log; i++) update(p >> i); + } + void apply(int l, int r, F f) { + assert(0 <= l && l <= r && r <= _n); + if (l == r) return; + + l += size; + r += size; + + for (int i = log; i >= 1; i--) { + if (((l >> i) << i) != l) push(l >> i); + if (((r >> i) << i) != r) push((r - 1) >> i); + } + + { + int l2 = l, r2 = r; + while (l < r) { + if (l & 1) all_apply(l++, f); + if (r & 1) all_apply(--r, f); + l >>= 1; + r >>= 1; + } + l = l2; + r = r2; + } + + for (int i = 1; i <= log; i++) { + if (((l >> i) << i) != l) update(l >> i); + if (((r >> i) << i) != r) update((r - 1) >> i); + } + } + + template int max_right(int l) { + return max_right(l, [](S x) { return g(x); }); + } + template int max_right(int l, G g) { + assert(0 <= l && l <= _n); + assert(g(e())); + if (l == _n) return _n; + l += size; + for (int i = log; i >= 1; i--) push(l >> i); + S sm = e(); + do { + while (l % 2 == 0) l >>= 1; + if (!g(op(sm, d[l]))) { + while (l < size) { + push(l); + l = (2 * l); + if (g(op(sm, d[l]))) { + sm = op(sm, d[l]); + l++; + } + } + return l - size; + } + sm = op(sm, d[l]); + l++; + } while ((l & -l) != l); + return _n; + } + + template int min_left(int r) { + return min_left(r, [](S x) { return g(x); }); + } + template int min_left(int r, G g) { + assert(0 <= r && r <= _n); + assert(g(e())); + if (r == 0) return 0; + r += size; + for (int i = log; i >= 1; i--) push((r - 1) >> i); + S sm = e(); + do { + r--; + while (r > 1 && (r % 2)) r >>= 1; + if (!g(op(d[r], sm))) { + while (r < size) { + push(r); + r = (2 * r + 1); + if (g(op(d[r], sm))) { + sm = op(d[r], sm); + r--; + } + } + return r + 1 - size; + } + sm = op(d[r], sm); + } while ((r & -r) != r); + return 0; + } + + private: + int _n, size, log; + std::vector d; + std::vector lz; + + void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } + void all_apply(int k, F f) { + d[k] = mapping(f, d[k]); + if (k < size) lz[k] = composition(f, lz[k]); + } + void push(int k) { + all_apply(2 * k, lz[k]); + all_apply(2 * k + 1, lz[k]); + lz[k] = id(); + } +}; + +} // namespace atcoder + +using namespace std; +using namespace atcoder; + +using ll = int; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +struct FenwickTree { + vector bit; // binary indexed tree + ll n; + + FenwickTree(ll n) { + this->n = n; + bit.assign(n, 0); + } + + FenwickTree(vector a) : FenwickTree(a.size()) { + for (size_t i = 0; i < a.size(); i++) + add(i, a[i]); + } + + ll sum(ll r) { + ll ret = 0; + for (; r >= 0; r = (r & (r + 1)) - 1) + ret += bit[r]; + return ret; + } + + ll sum(ll l, ll r) { + return sum(r) - sum(l - 1); + } + + void add(ll idx, ll delta) { + for (; idx < n; idx = idx | (idx + 1)) + bit[idx] += delta; + } +}; + +pi mn(pi a, pi b) { + if (a.first == b.first) return a.second > b.second ? a : b; + return a.first < b.first ? a : b; +} + +pi e() { + return {1e9, -1}; +} + +pi mapping(ll d, pi x) { + return {x.first + d, x.second}; +} + +ll composition(ll f, ll g) { + return f + g; +} + +ll id() { + return 0; +} + +void solve() { + ll n, m; + cin >> n >> m; + + priority_queue> ev; + + vi a(n); + for (ll i = 0; i < n; i++) { + cin >> a[i]; + ev.push({-a[i], i, 1}); + } + + vi b(m); + for (ll i = 0; i < m; i++) { + cin >> b[i]; + } + + sort(b.begin(), b.end()); + + vi swap(n, 1); + + vi path(m); + + vpi psum(n + 1); + + for (ll i = 0; i <= n; i++) { + psum[i] = {i, i}; + } + + lazy_segtree seg(psum); + + for (ll c = 0; c < m; c++) { + while (!ev.empty()) { + array top = ev.top(); + ll v = -top[0]; + ll i = top[1]; + ll tp = top[2]; + if (v > b[c]) break; + ev.pop(); + ll dif; + if (v == b[c] && tp == 1) dif = -swap[i], swap[i] = 0, ev.push({-(v + 1), i, 0}); + if (v < b[c] || (v == b[c] && tp == 0)) dif = -1 - swap[i], swap[i] = -1; + seg.apply(i + 1, n + 1, dif); + } + + path[c] = seg.all_prod().second; + } + + // construct optimal array + vpi opt; + + for (ll i = 0; i < n; i++) { + opt.push_back({i, -1}); + } + + for (ll i = 0; i < m; i++) { + opt.push_back({path[i]-1, i}); + } + + sort(opt.begin(), opt.end()); + + vpi actual_opt(opt.size()); + for (ll i = 0; i < opt.size(); i++) { + pi v = opt[i]; + actual_opt[i] = {v.second == -1 ? a[v.first] : b[v.second], i}; + } + + sort(actual_opt.begin(), actual_opt.end()); + + map coord; + + for(ll i = 0; i < opt.size(); i++) { + ll v = actual_opt[i].first; + + coord[v] = i; + } + + sort(actual_opt.begin(), actual_opt.end(), [](pi x, pi y) { return x.second < y.second; }); + + // calc inversions on optimal array + FenwickTree ft(opt.size() + 8); + + long long ans = 0; + for (pi i : actual_opt) { + ll v = coord[i.first]; + // find all things > v + ans += ft.sum(v + 1, opt.size()+2); + // add v to the thing + ft.add(v, 1); + } + + cout << ans << endl; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll t; + cin >> t; + while (t--) solve(); + + return 0; +} + diff --git a/permshift.cpp b/permshift.cpp new file mode 100644 index 0000000..307b8e7 --- /dev/null +++ b/permshift.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +using ll = long long; +using vi = vector; + +void solve() { + ll n, m; + cin >> n >> m; + + vi p(n); + for (ll i = 0; i < n; i++) { + cin >> p[i]; + p[i]--; + } + + vi cnt(n); + + for (ll i = 0; i < n; i++) { + ll nw = (i - p[i]) % n; + if (nw < 0) nw += n; + cnt[nw]++; + } + + ll cc = 0; + ll k = 0; + vi vis(n); + function dfs = [&](ll i) { + if (vis[i]) return; + vis[i] = true; + + ll e = (p[i] + k) % n; + if (e < 0) e += n; + dfs(e); + }; + + vi path; + for (; k < n; k++) { + //cout << cnt[k] << endl; + if (cnt[k] >= n / 3) { + vis.clear(); + vis.resize(n); + cc = 0; + for (ll i = 0; i < n; i++) { + if (!vis[i]) cc++, dfs(i); + } + //cout << k << " " << cc << endl; + if (n - cc <= m) path.push_back(k); + } + } + + cout << path.size() << " "; + for (ll i : path) cout << i << " "; + cout << endl; +} + +int main() { + ll t; + cin >> t; + + while (t--) solve(); + + return 0; +} \ No newline at end of file diff --git a/repnum.cpp b/repnum.cpp new file mode 100644 index 0000000..933f145 --- /dev/null +++ b/repnum.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vector> qu(n); + for (int i = 0; i < n; i++) { + int t; + int x, y; + x = y = 0; + cin >> t; + if (t == 1) { + cin >> x; + } else { + cin >> x >> y; + } + qu[i] = {t, x, y}; + } + + vi path; + vi look(5e5 + 8); + for (int i = 0; i < 5e5 + 8; i++) { + look[i] = i; + } + for (int i = n - 1; i >= 0; i--) { + int t = qu[i][0]; + int x = qu[i][1]; + int y = qu[i][2]; + + if(t==1) { + path.push_back(look[x]); + } else { + look[x] = look[y]; + } + } + + reverse(path.begin(), path.end()); + for (int i : path) { + cout << i << " "; + } + + return 0; +} \ No newline at end of file diff --git a/shuffler.cpp b/shuffler.cpp new file mode 100644 index 0000000..d386348 --- /dev/null +++ b/shuffler.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +const ll maxn = 5008; +ll C[maxn + 1][maxn + 1]; + +const ll mod = 998244353; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + C[0][0] = 1; + for (ll n = 1; n <= maxn; ++n) { + C[n][0] = C[n][n] = 1; + for (ll k = 1; k < n; ++k) { + C[n][k] = C[n - 1][k - 1] + C[n - 1][k]; + C[n][k] %= mod; + } + } + + ll n, k; + cin >> n >> k; + vb s(n); + + ll tot_k = 0; + for (ll i = 0; i < n; i++) { + char c; + cin >> c; + s[i] = c - '0'; + tot_k += s[i]; + } + + vi psum(n + 1); + + for (ll i = 1; i <= n; i++) { + psum[i] = psum[i - 1] + s[i - 1]; + } + + ll ans = 1; + + if (tot_k < k) { + cout << ans << endl; + return 0; + } + + for (ll i = 0; i < n; i++) { + for (ll j = i + 1; j < n; j++) { + ll num_k = psum[j + 1] - psum[i]; + if (num_k > k) continue; + num_k -= 2 - (s[i] + s[j]); + + ll sz = j - i - 1; + + if (sz < 0 || num_k < 0 || sz < num_k) continue; + ans += C[sz][num_k]; + ans %= mod; + } + } + + cout << ans << endl; + + return 0; +} \ No newline at end of file diff --git a/staircases.cpp b/staircases.cpp new file mode 100644 index 0000000..13c1308 --- /dev/null +++ b/staircases.cpp @@ -0,0 +1,115 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, m, q; + cin >> n >> m >> q; + + function in = [&](ll x, ll y) { + if (x >= 0 && x < n && y >= 0 && y < m) return true; + return false; + }; + + vector c(n + m + 2, vi(3)); + + for (ll i = 0; i <= n + m + 1; i += 1) + for (ll j = 0; j <= 2; j += 1) + c[i][j] = j ? (i ? (c[i - 1][j - 1] + c[i - 1][j]) : 0) : (i ? 1 : 0); + + ll cnt = 0; + // first precomp the amount of staircases + // this can be done rather inefficiently + for (ll i = 1; i < m; i++) { + ll x = 0; + ll y = i; + + ll len = 0; + + ll d = 0; + while (in(x, y)) { + if (d == 0) + x++; + else + y++; + len++; + + d = !d; + } + + cnt += c[len + 2][2]; + } + + for (ll i = 1; i < n; i++) { + ll x = i; + ll y = 0; + + ll len = 0; + + ll d = 1; + while (in(x, y)) { + if (d == 0) + x++; + else + y++; + len++; + d = !d; + } + + cnt += c[len + 2][2]; + } + cnt += n * m; + // now do queries fast because not much is affected per query + vector grid(n, vb(m)); + for (ll i = 0; i < q; i++) { + ll x, y; + cin >> x >> y; + x--, y--; + ll new_val = !grid[x][y]; + grid[x][y] = false; + ll change = 0; + array cur = {x, y}; + for (ll d = 0; d < 2; d++) { + ll cur_d = d; + ll cnt1, cnt2; + cnt1 = cnt2 = 0; + cur = {x, y}; + while (in(cur[0], cur[1]) && !grid[cur[0]][cur[1]]) { + cnt1++; + cur_d = !cur_d; + cur[cur_d]--; + } + cur_d = !d; + cur = {x, y}; + + while (in(cur[0], cur[1]) && !grid[cur[0]][cur[1]]) { + cnt2++; + cur_d = !cur_d; + cur[cur_d]++; + } + + change += cnt1 * cnt2 - 1; + } + + change += 1; + + grid[x][y] = new_val; + + if (new_val) { + cnt -= change; + } else + cnt += change; + + cout << cnt << endl; + } + + return 0; +} \ No newline at end of file diff --git a/strongbuild.cpp b/strongbuild.cpp new file mode 100644 index 0000000..8d39cb9 --- /dev/null +++ b/strongbuild.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vector a(n + 1, vi(2e5 + 8)); + vi bestpath; + bestpath.push_back(0); + for (int i = 1; i <= n; i++) { + int c; + cin >> c; + bestpath.push_back(c); + for (int j = 1; j <= c; j++) { + cin >> a[i][j]; + } + } + + ll m; + cin >> m; + + set> bb; + for (ll i = 0; i < m; i++) { + vector cur; + cur.push_back(0); + for (int j = 1; j <= n; j++) { + int blah; + cin >> blah; + + cur.push_back(blah); + } + + bb.insert(cur); + } + + ll best = 0; + set> gg; + for (vi b : bb) { + for (int j = 1; j <= n; j++) { + if(b[j] != 1) { + b[j]--; + if (bb.find(b) == bb.end()) gg.insert(b); + b[j]++; + } + } + } + + vi besti; + for (vi g : gg) { + ll sum = 0; + for (int i = 1; i <= n; i++) { + sum += a[i][g[i]]; + } + if(sum > best) { + best = sum; + besti = g; + } + } + + if (bb.find(bestpath) == bb.end()) besti = bestpath; + + for(int i : besti) { + if(i != 0) cout << i << " "; + } + + return 0; +} \ No newline at end of file diff --git a/sumofminxor.cpp b/sumofminxor.cpp new file mode 100644 index 0000000..d0df93d --- /dev/null +++ b/sumofminxor.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n; + cin >> n; + + vi a(n); + vi b(n); + for(int i = 0; i < n; i++) { + cin >> a[i]; + cin >> b[i]; + } + + sort + + return 0; +} \ No newline at end of file diff --git a/telepainting.cpp b/telepainting.cpp new file mode 100644 index 0000000..1319622 --- /dev/null +++ b/telepainting.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + +using ll = long long; +using vi = vector; +using vb = vector; +using vpi = vector>; + +const ll mod = 998244353; + +int main() { + ll n; + cin >> n; + + vi x(n + 2); + vi y(n + 1); + vi nxt(n + 1); + vi init(n + 1); + + vector> a(n); + + for (ll i = 0; i < n; i++) { + cin >> a[i][0] >> a[i][1] >> a[i][2]; + } + + sort(a.begin(), a.end()); + + for (ll i = 0; i < n; i++) { + x[i + 1] = a[i][0]; + y[i + 1] = a[i][1]; + init[i + 1] = a[i][2]; + } + + x[n + 1] = x[n]; + + for (ll i = n; i > 0; i--) { + nxt[i] = lower_bound(x.begin(), x.end(), y[i]) - x.begin(); + } + + //for (ll i = 1; i <= n; i++) cout << nxt[i] << " "; + //cout << endl; + + vi sum(n + 1); + vi psum(n + 1); + + for (ll i = 1; i <= n; i++) { + ll sucdist = x[nxt[i]] - y[i]; + + sum[i] = psum[i - 1] - psum[nxt[i] - 1] + sucdist + x[i + 1] - x[i]; + + sum[i] %= mod; + + //cout << "TH " << sum[i] << " " << endl; + + psum[i] = psum[i - 1] + sum[i]; + psum[i] %= mod; + } + + ll ans = x[1] + 1; + for (ll i = 1; i <= n; i++) { + if (init[i]) + ans += sum[i]; + else + ans += x[i + 1] - x[i]; + ans %= mod; + // cout << ans << endl; + } + + ans %= mod; + + if(ans < 0) ans += mod; + + cout << ans << endl; + + return 0; +} \ No newline at end of file diff --git a/telepaintingbad.cpp b/telepaintingbad.cpp new file mode 100644 index 0000000..6c132ad --- /dev/null +++ b/telepaintingbad.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ld long double +#define ll long long +#define ull unsigned long long +#define inf 1e9 + +using namespace std; + +void solve() { + ll mod = 998244353; + int n; + cin >> n; + vector s(n); + vector> arr(n); + for (int i = 0; i < n; i++) { + cin >> arr[i].first; + cin >> arr[i].second; + cin >> s[i]; + } + arr.emplace_back(arr[n - 1].first + 1, 0); + vector ans(n); + vector pre(n); + ans[0] = arr[1].first - arr[0].second; + pre[0] = ans[0]; + set> visited; + visited.emplace(arr[0].first, 0); + // for(int i=0;i(arr[i].second, 0)); + // for(auto a:visited){ + // cout<second; + // cout<first<<" "<second<> t; + for (int c = 0; c < t; c++) { + // cout<<"Case #"< +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, m; + cin >> n >> m; + + vector> ed(n); + for (ll i = 0; i < m; i++) { + ll a, b; + cin >> a >> b; + a--, b--; + + ed[a].insert(b); + ed[b].insert(a); + } + + bool imp = false; + vi col(n); + for (int i = 0; i < n; i++) { + if (i == 0 || (ed[i].find(0) == ed[i].end())) col[i] = 1; + } + + ll lst = -1; + for (int j = 0; j < n; j++) { + if (col[j] != 1) lst = j; + } + + if (lst == -1) imp = true; + + for (int i = 0; i < n; i++) { + if (i == lst || (ed[i].find(lst) == ed[i].end())) col[i] = 2; + } + + ll lst2 = -1; + for (int j = 0; j < n; j++) { + if (col[j] != 1 && col[j] != 2) lst2 = j; + } + + if(lst2 == -1) imp = true; + + for (int i = 0; i < n; i++) { + if (i == lst2 || (ed[i].find(lst2) == ed[i].end())) col[i] = 3; + } + + ll c1, c2, c3; + c1 = c2 = c3 = 0; + + for (int i = 0; i < n; i++) { + if (col[i] == 0) imp = true; + if (col[i] == 1) c1++; + if (col[i] == 2) c2++; + if (col[i] == 3) c3++; + } + + for (int i = 0; i < n; i++) { + ll a, b, c; + a = b = c = 0; + for(int ch : ed[i]) { + if (col[ch] == 1) a++; + if (col[ch] == 2) b++; + if (col[ch] == 3) c++; + } + if (col[i] == 1) { + if (a > 0 || b != c2 || c != c3) imp = true; + } + if (col[i] == 2) { + if (b > 0 || a != c1 || c != c3) imp = true; + } + if (col[i] == 3) { + if (c > 0 || a != c1 || b != c2) imp = true; + } + } + + if(imp) { + cout << -1 << endl; + } else { + for (int i = 0; i < n; i++) cout << col[i] << " "; + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/trainmat.cpp b/trainmat.cpp new file mode 100644 index 0000000..03e1336 --- /dev/null +++ b/trainmat.cpp @@ -0,0 +1,89 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ll n, m; + cin >> n >> m; + + vi a(n); + vi b(n); + for (ll i = 0; i < n; i++) { + scanf("%d%d", &a[i], &b[i]); + } + + ll ans = 0; + ll sq = (ll)sqrt(m); + vector upd(sq, vi(sq)); + vi rsum(sq); + vi psum(m); + vi st(n); + for (ll i = 0; i < m; i++) { + ll op, k; + scanf("%d%d", &op, &k); + k--; + ll x = a[k]; + ll y = b[k]; + + ll im = i % (x + y); + + if (op == 1) { + ans += psum[i]; + + for (ll j = 1; j < sq; j++) { + rsum[j] += upd[j][i % j]; + ans += rsum[j]; + } + st[k] = i; + if (x + y >= sq) { + for (ll j = i + x; j < m; j += x + y) { + psum[j]++; + if (j + y < m) psum[j + y]--; + } + } else { + upd[x + y][(i + x) % (x + y)]++; + upd[x + y][im]--; + } + } else { + ll v = st[k]; + if (x + y >= sq) { + bool works = false; + for (ll j = v + x; j < m; j += x + y) { + psum[j]--; + if (j + y < m) psum[j + y]++; + if (i >= j && i < j + y) { + works = true; + } + } + if (works) ans--; + } else { + ll spt = (v + x) % (x + y); + ll ept = v % (x + y); + if (spt < ept && im >= spt && im < ept) { + ans--; + } + if (ept > spt && (im >= spt || im < ept)) { + ans--; + } + + upd[x + y][spt]--; + upd[x + y][ept]++; + } + ans += psum[i]; + + for (ll j = 1; j < sq; j++) { + rsum[j] += upd[j][i % j]; + ans += rsum[j]; + } + } + + printf("%d\n", ans); + } + + return 0; +} \ No newline at end of file diff --git a/whatisthisproblemcalled.cpp b/whatisthisproblemcalled.cpp new file mode 100644 index 0000000..5720479 --- /dev/null +++ b/whatisthisproblemcalled.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +using ll = long long; +using pi = pair; +using vi = vector; +using vpi = vector; +using vb = vector; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + ll n, q; + cin >> n >> q; + + vi a(n); + for(int i = 0; i < n; i++) { + char c; + cin >> c; + + a[i] = c == '(' ? 1 : -1; + } + + vi psum(n + 1); + + for(int i = 0; i < n; i++) { + psum[i + 1] = psum[i] + a[i]; + } + + return 0; +}