func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int main() { int pkt; int start = 0; int lr = 0; cin >> pkt; if (pkt < 0) pkt = pkt / -1; for (int a = 1; start != pkt; a++) { start = start + a; lr++; if (start > pkt) { if ((start - pkt) % 2 == 1) ; else break; } } cout << lr << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const double h = 1e-6; const int MAX_ = 200005; inline long long int mul(long long int a, long long int b) { return (a * 1ll * b) % 1000000007; } inline long long int sub(long long int a, long long int b) { long long int c = a - b; if (c < 0) c += 1000000007; return c; } inline long long int add(long long int a, long long int b) { long long int c = a + b; if (c > 1000000007) c -= 1000000007; return c; } long long int sqr(long long int x) { return x * x; } long long int ncr[3050][3050]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); for (int i = 0; i < 3050; i++) { for (int j = 0; j <= i; j++) { if (i == 0 || j == 0) ncr[i][j] = 1; else ncr[i][j] = add(ncr[i - 1][j - 1], ncr[i - 1][j]); } } int k; cin >> k; long long int val; long long int ans = 1; int count = 0; for (long long int i = 0; i < k; i++) { cin >> val; ans = mul(ans, ncr[count + val - 1][val - 1]); ans %= 1000000007; count += val; } cout << ans; return 0; }
#include <bits/stdc++.h> char s[100 + 5], ans[100 + 5][250 + 5]; int main() { int n, i, j, p, l, r; scanf( %d%s , &n, s); for (i = l = r = 0; i<n; ++i, r = l> r ? l : r) l += s[i] == [ ? 2 : -2; for (i = p = 0; i < n; ++i, ++p) { ans[l][p] = ans[r][p] = + ; for (j = l + 1; j < r; ++j) ans[j][p] = | ; if (s[i] == [ ) ans[l][p + 1] = ans[r][p + 1] = - ; if (s[i] == ] ) ans[l][p - 1] = ans[r][p - 1] = - ; if (s[i] == [ && s[i + 1] == [ ) ++l, --r; if (s[i] == [ && s[i + 1] == ] ) p += 3; if (s[i] == ] && s[i + 1] == ] ) --l, ++r; } for (i = 0; i <= r; ++i, puts( )) for (j = 0; j < p; ++j) putchar(ans[i][j] ? ans[i][j] : ); }
#include <bits/stdc++.h> using namespace std; long n, m, dx, dy, qx, qy, ps[1 << 20]; long t, ans[1 << 20]; long a, b; long bst, bp; int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> dx >> dy; qx = qy = 0; for (int i = 0; i < n; i++) { ps[qx] = qy; qy += dy; qx += dx; qx %= n; qy %= n; } for (int i = 1; i <= m; i++) { cin >> a >> b; t = b - ps[a] + n; t %= n; ans[t]++; } bst = -1; for (int i = 0; i < n; i++) if (ans[i] > bst) bst = ans[i], bp = i; cout << 0 << << bp << endl; cin.get(); cin.get(); return 0; }
#include <bits/stdc++.h> using namespace std; long long p = 1000000007ll; int main(void) { long long n; cin >> n; vector<pair<long long, long long> > loops; long long tmp = 0; for (long long i = (0); i < (n); ++i) { string com; cin >> com; if (com == for ) { long long x; cin >> x; loops.emplace_back(x, tmp); tmp = 0; } else if (com == end ) { tmp = tmp * loops.back().first + loops.back().second; if (tmp >= (1ll << 32)) { cout << OVERFLOW!!! << endl; return 0; } loops.pop_back(); } else { ++tmp; } } if (tmp >= (1ll << 32)) { cout << OVERFLOW!!! << endl; return 0; } cout << tmp << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long lar = 1e16; const long long nlar = -1e16; const long long INF = LLONG_MAX; const long long NINF = LLONG_MIN; const long long mod = 998244353; const long long N = 1e6 + 100; struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; void google(long long t) { cout << Case # << t << : ; } long double sqrtBinarySearch(long long num, long long p) { long long left = 0, right = num; long long mid; long double res; while (left <= right) { mid = (left + right) / 2; if (mid * mid == num) { res = mid; break; } if (mid * mid < num) { left = mid + 1; res = mid; } else { right = mid - 1; } } long double incr = 0.1; for (long long i = 0; i < p; i++) { while (res * res <= num) { res += incr; } res -= incr; incr /= 10; } return res; } long long modInverse(long long a, long long m) { long long m0 = m; long long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } long long add(long long x, long long y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; return x; } long long mul(long long x, long long y) { return (x * 1ll * y) % mod; } long long binpow(long long x, long long y) { long long ans = 1; while (y > 0) { if (y % 2 == 1) ans = mul(ans, x); x = mul(x, x); y /= 2; } return ans; } long long divmod(long long i, long long pp) { return ((i * (modInverse(pp, mod))) % mod); } long long divide(long long x, long long y) { return mul(x, binpow(y, mod - 2)); } string to_binary(long long num) { string res = ; while (num) { res += ((num & (1LL)) + 0 ); num /= 2; } reverse(res.begin(), res.end()); return res; } long long to_decimal(string bi) { long long dec = 0; long long si = bi.size(); long long upd = 1; for (long long i = si - 1; i >= 0; i--) { dec += (upd * ((long long)(bi[i] - 0 ))); upd *= 2; } return dec; } vector<bool> SieveOfEratosthenes(long long n) { vector<bool> prime(n + 1, true); for (long long p = 2; p * p <= n; p++) { if (prime[p] == true) { for (long long i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } long long digitsum(long long x) { long long ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } long long nCr(long long n, long long r) { if (n == r) return 1; else if (r == 0) return 1; else return ((nCr(n - 1, r - 1) + nCr(n - 1, r)) % mod); } long long sqroot(long long number) { long long x = sqrt(number); while (x * x < number) x++; while (x * x > number) x--; return x; } long long cbroot(long long number) { long long x = cbrt(number); while (x * x * x < number) x++; while (x * x * x > number) x--; return x; } long long gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; return gcd(b % a, a); } long long power(long long a, long long b) { if (b == 0) return 1; if (b & 1) return (a * power(a, b - 1)); long long temp = power(a, b / 2); return (temp * temp); } long long powermod(long long a, long long b) { if (b == 0) return 1; if (b & 1) return (a * powermod(a, b - 1)) % mod; long long temp = powermod(a, b / 2) % mod; return (temp * temp) % mod; } vector<long long> factor(long long x) { vector<long long> fact; for (long long i = 1; i * i <= x; i++) { if (x % i == 0) { fact.push_back(i); if (i * i != x) { fact.push_back(x / i); } } } return fact; } long long fact[N]; void Initial_Factorial() { fact[0] = 1; for (long long i = 1; i < N; i++) { fact[i] = (fact[i - 1] * i); fact[i] %= mod; } } long long binpow(long long val, long long deg, long long mod) { if (!deg) return 1 % mod; if (deg & 1) return binpow(val, deg - 1, mod) * val % mod; long long res = binpow(val, deg >> 1, mod); return (res * res) % mod; } long long nCr_Binomial(long long n, long long i) { long long res = fact[n]; long long div = fact[n - i] * fact[i]; div %= mod; div = binpow(div, mod - 2, mod); return (res * div) % mod; } vector<long long> adj[10]; long long number_of_nodes = 10; vector<long long> visited(number_of_nodes, 0); void dfs(long long node) { visited[node] = 1; for (long long i = 0; i < adj[node].size(); ++i) { if (visited[adj[node][i]] == 0) dfs(adj[node][i]); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); Initial_Factorial(); long long n; cin >> n; if (n == 1) { cout << 1 << n ; return 0; } long long ans = n; ans *= fact[n]; ans %= mod; long long curr = 1; long long sum = 1; long long st = n - 1; while (st > 1) { curr *= st; curr %= mod; sum += curr; sum %= mod; st--; } sum *= n; sum %= mod; cout << (ans + mod - sum) % mod << n ; cerr << ntime taken : << (long double)clock() / CLOCKS_PER_SEC << secs << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int nmax = 5e5 + 42; struct info { int u, v, cost; }; info inp[nmax]; pair<int, int> my[nmax]; int n, k, m; int parent[nmax]; int root(int node) { if (parent[node] == node) return node; parent[node] = root(parent[node]); return parent[node]; } long long outp; set<int> seen[nmax]; vector<int> adj[nmax]; set<pair<int, int> > my_edge; void solve(int node, int par) { int biggest = -1; for (auto k : adj[node]) if (k != par) { solve(k, node); if (biggest == -1) biggest = k; else if (seen[biggest].size() < seen[k].size()) biggest = k; } if (biggest != -1) { swap(seen[biggest], seen[node]); for (auto w : seen[biggest]) if (seen[node].count(w)) seen[node].erase(w); else seen[node].insert(w); } for (auto k : adj[node]) if (k != par && k != biggest) { for (auto w : seen[k]) if (seen[node].count(w)) seen[node].erase(w); else seen[node].insert(w); } if (my_edge.count({node, par}) || my_edge.count({par, node})) { if (seen[node].size() == 0) { printf( -1 n ); exit(0); } outp += inp[*seen[node].begin()].cost; } } int main() { scanf( %i%i%i , &n, &k, &m); for (int i = 1; i <= n; i++) parent[i] = i; for (int i = 1; i <= k; i++) { scanf( %i%i , &my[i].first, &my[i].second); adj[my[i].first].push_back(my[i].second); adj[my[i].second].push_back(my[i].first); parent[root(my[i].first)] = root(my[i].second); my_edge.insert({my[i].first, my[i].second}); } for (int i = 1; i <= m; i++) { scanf( %i%i%i , &inp[i].u, &inp[i].v, &inp[i].cost); if (root(inp[i].u) != root(inp[i].v)) { parent[root(inp[i].u)] = root(inp[i].v); adj[inp[i].u].push_back(inp[i].v); adj[inp[i].v].push_back(inp[i].u); } else { seen[inp[i].u].insert(i); seen[inp[i].v].insert(i); } } solve(1, 0); printf( %lld n , outp); return 0; }
#include <bits/stdc++.h> using namespace std; using ld = long double; using ll = long long; using ull = unsigned long long; const ll mod = 1004535809, mod_g = 3; const int N = int(1e5 + 10); template <typename T> inline T read() { T x = 0, f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * f; } template <typename T> inline T print(T x) { if (x < 0) { putchar( - ); x = -x; } if (x > 9) print(x / 10); putchar(x % 10 + 0 ); } const ld pi = acos(-1.0); struct Complex { ld r, i; Complex(ld _r = 0, ld _i = 0) : r(_r), i(_i) {} Complex operator+(const Complex &b) const { return Complex(r + b.r, i + b.i); } Complex operator-(const Complex &b) const { return Complex(r - b.r, i - b.i); } Complex operator*(const Complex &b) const { return Complex(r * b.r - i * b.i, r * b.i + i * b.r); } Complex operator/(const Complex &b) const { return Complex((r * b.r + i * b.i) / (b.r * b.r + b.i * b.i), (i * b.r - r * b.i) / (b.r * b.r + b.i * b.i)); } }; int R[int(1e6 + 10000)]; Complex x[int(1e6 + 10000)], y[int(1e6 + 10000)]; void get_R(int lim) { for (int i = 0; i < lim; i++) { R[i] = (i & 1) * (lim >> 1) + (R[i >> 1] >> 1); } } void FFT(Complex *f, int lim, int rev) { for (int i = 0; i < lim; i++) { if (i < R[i]) swap(f[i], f[R[i]]); } for (int mid = 1; mid < lim; mid <<= 1) { Complex wn = Complex(cos(pi / mid), rev * sin(pi / mid)); for (int len = mid << 1, cur = 0; cur < lim; cur += len) { Complex w = Complex(1, 0); for (int k = 0; k < mid; k++, w = w * wn) { Complex x = f[cur + k], y = w * f[cur + mid + k]; f[cur + k] = x + y, f[cur + mid + k] = x - y; } } } if (rev == -1) { for (int i = 0; i < lim; i++) { f[i].r /= lim; } } } ll quick_pow(ll ans, ll p, ll res = 1) { for (; p; p >>= 1, ans = ans * ans % mod) if (p & 1) res = res * ans % mod; return res % mod; } ll ans[200010]; inline void solve() { int n, X; cin >> n >> X; for (int i = 1; i <= n; ++i) { int xx; cin >> xx; ans[i] = ans[i - 1] + (xx < X); x[ans[i]].r++; y[200000 - ans[i]].r++; x[i].i = y[i].i = 0; } x[0].r++, y[200000].r++; int lim = 1; while (lim <= 400000) lim <<= 1; get_R(lim); FFT(x, lim, 1); FFT(y, lim, 1); for (int i = 0; i <= lim - 1; ++i) x[i] = x[i] * y[i]; FFT(x, lim, -1); cout << (ll(x[200000].r + 0.5) - n - 1) / 2; for (int i = 1; i <= n; ++i) cout << << ll(x[200000 + i].r + 0.5); cout << n ; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long int MOD = (long long int)1e9 + 7; long long int powmod(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; b = b >> 1; } return res % MOD; } void solve(long long int testnum) { long long int n, k; cin >> n >> k; string s; cin >> s; string t; cin >> t; long long int ans = 0; long long int tmpmn = 0; long long int tmpmx = 0; long long int check = 0; for (long long int i = 0; i < n; i++) { if (s[i] == b ) { tmpmn++; } if (t[i] == b ) { tmpmx++; } long long int ort = tmpmx - tmpmn + 1; if (ort >= k) { check = 1; } if (check == 0) { ans += min(k, ort); } else { ans += k; } if (tmpmn == tmpmx) { tmpmn = 0; tmpmx = 0; } if (check == 0) { tmpmn *= 2; tmpmx *= 2; } } cout << ans << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; for (long long int i = 1; i <= t; i++) { solve(i); } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 10000; const int MAXK = 20; int V[MAXK + 3][MAXN + 3], Ans[MAXK + 3][MAXN + 3]; int main() { V[0][1] = 0; for (int j = 2; j <= MAXN; j++) { V[0][j] = 1; } for (int i = 1; i <= MAXK; i++) { int k = 1; for (int j = 1; j <= MAXN; j++) { if (k < MAXN && V[i - 1][k] < V[i - 1][k + 1] && V[i - 1][j] + V[i - 1][k] < j - 1) k++; V[i][j] = V[i - 1][j] + V[i - 1][k]; Ans[i][j] = k; } } int N, K; while (~scanf( %d%d , &N, &K)) { for (int i = 1; i <= K; i++) { for (int j = N; j > 1; j--) printf( %d , N - Ans[i][j] + 1); printf( %d n , N - Ans[i][1] + 1); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; void solve() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (auto i : a) { cout << i << ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int maxN = 1e5 + 10; long long int n, i, a[maxN], mx, cnt, nxt; stack<pair<long long int, long long int> > st; int main() { cin >> n; for (long long int i = 0; i < n; i++) cin >> a[i]; for (long long int i = n - 1; i >= 0; i--) { while (!st.empty()) { if (a[i] < st.top().first) break; cnt++; cnt = max(cnt, st.top().second); st.pop(); } mx = max(mx, cnt); st.push(make_pair(a[i], cnt)); cnt = 0; } cout << mx << endl; }
#include <bits/stdc++.h> using namespace std; int n, m, a, b, c, d, x, y, p, q, t, ans[400009], res, mi, T, lca[22][400009], sub[400009]; vector<int> v[400009]; int ma[2]; void dfs(int id, int par) { lca[1][id] = par; lca[0][id] = id; for (int i = 0; i < v[id].size(); i++) { int u = v[id][i]; if (u == par) continue; dfs(u, id); sub[id] += sub[u]; } sub[id]++; } int centroid(int id, int par) { int f = 0, s = 0; for (int i = 0; i < v[id].size(); i++) { int u = v[id][i]; if (u == par) continue; if (sub[u] > T) return centroid(u, id); else s += sub[u]; } if (f == 0 && n - 1 - s <= T) return id; } int lim; void init_lca() { lim = log(n) / log(2) + 1; for (int i = 2; i <= lim; i++) { for (int j = 1; j <= n; j++) { lca[i][j] = lca[1][lca[i - 1][lca[i - 1][j]]]; } } } void calc(int id, int par, int branch = 0) { for (int i = 0; i < v[id].size(); i++) { int u = v[id][i]; if (u == par) continue; int branch2 = (branch == 0 ? u : branch); if (branch2 != ma[0]) { if (n - sub[ma[0]] - sub[u] <= T) ans[u] = 1; } else { if (n - sub[ma[1]] - sub[u] <= T) ans[u] = 1; } if (n - sub[branch2] <= T) ans[u] = 1; calc(u, id, branch2); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i < n; i++) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } if (n <= 4) { for (int i = 1; i <= n; i++) cout << 1 << ; return 0; } dfs(1, 0); T = n / 2; c = centroid(1, 0); memset(sub, 0, sizeof(sub)); dfs(c, 0); ans[c] = 1; for (int i = 0; i < v[c].size(); i++) { int u = v[c][i]; if (sub[u] >= sub[ma[0]]) { ma[1] = ma[0]; ma[0] = u; } else if (sub[u] > sub[ma[1]]) ma[1] = u; } calc(c, 0); for (int i = 1; i <= n; i++) cout << ans[i] << ; return 0; }
#include <bits/stdc++.h> template <class T1, class T2> inline bool cmin(T1 &a, const T2 &b) { return b < a ? (a = b, true) : false; } template <class T1, class T2> inline bool cmax(T1 &a, const T2 &b) { return a < b ? (a = b, true) : false; } template <class Type> Type read() { Type a; bool b; unsigned char c; while (c = getchar() - 48, (c > 9) & (c != 253)) ; for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c) ; return b ? -a : a; } int (*rd)() = read<int>; const int N = 2e5; std::vector<int> b[N], g[N]; int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } std::pair<int, int> exgcd(int x, int y) { if (y == 0) return {1, 0}; int a, b; std::tie(b, a) = exgcd(y, x % y); return {a, b - x / y * a}; } void gg() { puts( -1 ); exit(0); } long long solve(int n, int m, const std::vector<int> &b, const std::vector<int> &g) { long long ans = -1; std::vector<std::pair<int, int>> pos; int inv = exgcd(n, m).second % n; if (inv < 0) inv += n; for (int i : b) pos.emplace_back((long long)i * inv % n, 0); for (int i : g) pos.emplace_back((long long)i * inv % n, 1); std::sort(pos.begin(), pos.end()); pos.resize(std::unique(pos.begin(), pos.end(), [](std::pair<int, int> p, std::pair<int, int> q) { return p.first == q.first; }) - pos.begin()); int i = pos.back().first; for (auto p : pos) { if ((p.first - 1 + n) % n != i) cmax(ans, (long long)(p.first - i - 1 + n) % n * m + (long long)i * m % n); if (p.second) cmax(ans, (long long)i * m % n); i = p.first; } return ans; } int main() { int n, m, d; long long ans = 0; std::vector<int> glob, glog; n = rd(), m = rd(), d = gcd(n, m); glob.resize(rd()); for (int &i : glob) i = rd(); glog.resize(rd()); for (int &i : glog) i = rd(); if (glob.size() + glog.size() < d) gg(); for (int i : glob) b[i % d].push_back(i / d); for (int i : glog) g[i % d].push_back(i / d); n /= d, m /= d; for (int i = 0; i < d; ++i) { if (b[i].empty() && g[i].empty()) gg(); cmax(ans, std::max(solve(n, m, b[i], g[i]), solve(m, n, g[i], b[i])) * d + i); } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int N; pair<pair<int, int>, int> arr[2050]; int vis[2050]; char s[2050]; double angle(pair<int, int> x, pair<int, int> y, pair<int, int> z) { pair<int, int> xy = {y.first - x.first, y.second - x.second}; pair<int, int> xz = {y.first - z.first, y.second - z.second}; double angyx = 1.0 * xy.first * xz.first + 1.0 * xy.second * xz.second; double angyz = 1.0 * xy.first * xz.second - 1.0 * xy.second * xz.first; return atan2(angyz, angyx); } int main() { scanf( %d , &N); for (int i = 0; i < N; i++) { int x, y; scanf( %d%d , &x, &y); arr[i] = {{x, y}, i + 1}; } scanf( %s , s); sort(arr, arr + N, [](const pair<pair<int, int>, int> &a, const pair<pair<int, int>, int> &b) { if (a.first.second == b.first.second) return a.first.first < b.first.first; return a.first.second < b.first.second; }); pair<int, int> tmp1 = {arr[0].first.first - 1, arr[0].first.second}; pair<int, int> tmp2 = arr[0].first; vis[0] = 1; pair<int, int> a = {-1, -1}; pair<int, int> b = {1, -1}; pair<int, int> c = {2, -1}; printf( %d , arr[0].second); for (int i = 0; i < N - 2; i++) { double max = -1234567.0, min = 1234567.0; int maxi, mini; for (int j = 0; j < N; j++) { if (vis[j] == 1) continue; double ang = angle(tmp1, tmp2, arr[j].first); if (i == 0) { } if (i == 0 && arr[j].first.second == arr[0].first.second && arr[j].first.first > arr[0].first.first) { min = -1234, mini = j; continue; } if (i == 0 && arr[j].first.second == arr[0].first.second && arr[j].first.first < arr[0].first.first) { max = 1234, maxi = j; continue; } if (ang > max) max = ang, maxi = j; if (ang < min) min = ang, mini = j; } if (s[i] == R ) { tmp1 = tmp2; tmp2 = arr[maxi].first; vis[maxi] = 1; printf( %d , arr[maxi].second); } else { tmp1 = tmp2; tmp2 = arr[mini].first; vis[mini] = 1; printf( %d , arr[mini].second); } } for (int i = 0; i < N; i++) { if (vis[i] == 0) { printf( %d n , arr[i].second); } } }
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << { << p.first << , << p.second << } ; return os; } const int N = 3e5 + 5; const int oo = 1e9 + 7; vector<pair<int, pair<int, int> > > g[N]; int f[N]; int ans[N]; int x[N]; int y[N]; bool vis[N]; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int w; cin >> x[i] >> y[i] >> w; g[x[i]].push_back({i, {y[i], w}}); g[y[i]].push_back({i, {x[i], w}}); } for (int i = 2; i <= n - 1; i++) { for (auto p : g[i]) { f[i] += p.second.second; } assert(f[i] % 2 == 0); f[i] /= 2; } queue<int> q; q.push(1); vis[1] = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (auto p : g[u]) { int i = p.first; int v = p.second.first; int w = p.second.second; if (vis[v]) continue; if (x[i] == u) ans[i] = 0; else ans[i] = 1; if (v == n) continue; f[v] -= w; if (f[v] == 0) { vis[v] = 1; q.push(v); } } } for (int i = 1; i <= m; i++) cout << ans[i] << n ; }
#include <bits/stdc++.h> using namespace std; int n; string queue_[6] = { , Sheldon , Leonard , Penny , Rajesh , Howard }; int main() { cin >> n; double count = 1; bool used = false; while (count * 5 < n) { n -= count * 5; count *= 2; used = true; } int x = ceil(n / count); cout << queue_[x]; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; const int logn = 19; vector<int> adj[maxn]; int idcnt, l[maxn], r[maxn], dep[maxn], anc[maxn][logn]; void dfs1(int u, int up) { l[u] = ++idcnt; dep[u] = dep[up] + 1; anc[u][0] = up; for (int i = 1; i < logn; i++) { if (dep[u] <= (1 << i)) break; anc[u][i] = anc[anc[u][i - 1]][i - 1]; } for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == up) continue; dfs1(v, u); } r[u] = idcnt; } int qlca(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int i = logn - 1; i >= 0; i--) if (dep[v] > (1 << i) && dep[anc[v][i]] >= dep[u]) v = anc[v][i]; if (u == v) return u; for (int i = logn - 1; i >= 0; i--) if (dep[u] > (1 << i) && anc[u][i] != anc[v][i]) { u = anc[u][i]; v = anc[v][i]; } return anc[u][0]; } vector<int> bus[maxn]; int nex[maxn][logn]; void dfs2(int u, int up) { for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == up) continue; dfs2(v, u); if (dep[nex[v][0]] < dep[nex[u][0]]) nex[u][0] = nex[v][0]; } } int n, st[maxn]; int lowbit(int x) { return x & (-x); } void add(int i, int x) { for (; i <= n; i += lowbit(i)) st[i] += x; } int qsum(int i) { int res = 0; for (; i > 0; i -= lowbit(i)) res += st[i]; return res; } struct node { int id, v; node(int id = 0, int v = 0) : id(id), v(v) {} }; vector<node> ql[maxn]; int ans[maxn], cmp[maxn]; void dfs3(int u, int up) { for (int i = 0; i < ql[u].size(); i++) { node cur = ql[u][i]; int id = cur.id, v = cur.v; cmp[id] = qsum(r[v]) - qsum(l[v] - 1); } for (int i = 0; i < bus[u].size(); i++) { int v = bus[u][i]; add(l[v], 1); } for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == up) continue; dfs3(v, u); } for (int i = 0; i < ql[u].size(); i++) { node cur = ql[u][i]; int id = cur.id, v = cur.v; if (qsum(r[v]) - qsum(l[v] - 1) != cmp[id]) ans[id]--; } } int main() { scanf( %d , &n); for (int i = 2; i <= n; i++) { int v; scanf( %d , &v); adj[i].push_back(v); adj[v].push_back(i); } dfs1(1, 1); int m; scanf( %d , &m); for (int i = 1; i <= n; i++) nex[i][0] = i; for (int i = 1; i <= m; i++) { int a, b; scanf( %d%d , &a, &b); bus[a].push_back(b); bus[b].push_back(a); int lca = qlca(a, b); if (dep[lca] < dep[nex[a][0]]) nex[a][0] = lca; if (dep[lca] < dep[nex[b][0]]) nex[b][0] = lca; } dfs2(1, 1); for (int i = 1; i < logn; i++) for (int j = 1; j <= n; j++) nex[j][i] = nex[nex[j][i - 1]][i - 1]; int q; scanf( %d , &q); for (int i = 1; i <= q; i++) { int u, v; scanf( %d%d , &u, &v); if (u > v) swap(u, v); int lca = qlca(u, v); for (int j = logn - 1; j >= 0; j--) if (dep[nex[v][j]] > dep[lca]) { v = nex[v][j]; ans[i] += (1 << j); } if (u == lca) { if (dep[nex[v][0]] > dep[lca]) { ans[i] = -1; continue; } if (dep[v] > dep[lca]) ans[i]++; } else { for (int j = logn - 1; j >= 0; j--) if (dep[nex[u][j]] > dep[lca]) { u = nex[u][j]; ans[i] += (1 << j); } if (dep[nex[u][0]] > dep[lca] || dep[nex[v][0]] > dep[lca]) { ans[i] = -1; continue; } ans[i] += 2; if (u > v) swap(u, v); ql[u].push_back(node(i, v)); } } dfs3(1, 1); for (int i = 1; i <= q; i++) printf( %d n , ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; long long k, res, a[1111], n; set<long long> s; int main() { cin >> n >> k; for (long long i = 1; i <= sqrt(n); i++) { if (n % i == 0) s.insert(i), s.insert(n / i); } for (long long i : s) { res++; if (res == k) { cout << i; return 0; } } cout << -1; }
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; const long long xx = 2e7; const long long MOD = 1e9 + 7; const long long inf = 1e18; long long isprime(long long n) { if (n == 2) return 1; if (n % 2 == 0) return 0; for (long long i = 3; i * i <= n; i += 2) { if (n % i == 0) return 0; } return 1; } signed main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; if (isprime(n)) { cout << 1 << n ; } else if (n % 2 == 0 || isprime(n - 2)) { cout << 2 << n ; } else cout << 3 << n ; }
#include <bits/stdc++.h> using namespace std; long long N = 0, M, K; long long MOD = 998244353; string S, S1; struct st { long long x, i; }; bool operator<(const st& a, const st& b) { if (a.x == b.x) return a.i < b.i; return a.x < b.x; } long long gcd(long long a, long long b) { while (b > 0) { a %= b; swap(a, b); } return a; } long long fact(long long x) { long long x1 = 1; while (x >= 1) { x1 *= x; x--; } return x1; } void prime(long long n, vector<long long>& pr) { vector<long long> lp(n + 90); for (long long i = 2; i <= n; ++i) { if (!lp[i]) { pr.push_back(i); lp[i] = i; } for (long long j = 0; j < pr.size() && pr[j] <= lp[i] && i * pr[j] <= n; ++j) { lp[i * pr[j]] = pr[j]; } } } void dfs(long long x, vector<long long>& b, vector<long long>& w, vector<vector<long long>>& v) { w[x] = 1; b.push_back(x); for (long long i : v[x]) { if (!w[i]) dfs(i, b, w, v); } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s, s1; long long n, k = 0, m; long long x = 0, y = 0, ans = 0; cin >> s >> n; vector<long long> a; for (long long i = 0; i < n; i++) { s1 = ; cin >> x >> y >> k; m = y - x + 1; k = k % m; for (long long j = y - k; j <= y - 1; j++) s1 += s[j]; s.erase(y - k, k); s.insert(x - 1, s1); } cout << s << n ; cin >> N; return 0; }
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; long long fastpo(long long a, long long x) { if (x == 0) return 1ll; if (x == 1) return a; long long y = fastpo(a, x / 2); if (x % 2 == 0) { return y * y; } return y * y * a; } long long inv(long long a, long long M) { return fastpo(a, M - 2); } long long gcdExtended(long long a, long long b, long long *x, long long *y) { if (a == 0) { *x = 0, *y = 1; return b; } long long x1, y1; long long gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } long long modInverse(long long a, long long m) { long long x, y; long long g = gcdExtended(a, m, &x, &y); if (g != 1) return -1; else { long long res = (x % m + m) % m; return res; } } long long gcd(long long a, long long b) { if (a == 0) { return b; } return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } long long N = 2e6 + 5; vector<long long> dp(N, 0), ci(N, 0), ai(N, 0), bi(N, 0), ans(N, 0); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 1; i < N; i++) { if (i == 1) { dp[i] = 1; ci[i] = 0; ai[i] = 0; bi[i] = 1; ans[i] = 0; } else if (i == 2) { dp[i] = 2; ci[i] = 0; ai[i] = 1; bi[i] = 1; ans[i] = 0; } else { ci[i - 1] = dp[i - 3]; ai[i - 1] = dp[i - 2] - dp[i - 3]; bi[i - 1] = dp[i - 1] - dp[i - 2]; dp[i] = ((ci[i - 1] + ai[i - 1] * 3 + bi[i - 1] * 2) % M + M) % M; ans[i] = (ans[i - 3] + bi[i - 2]) % M; } } int t; cin >> t; while (t--) { long long n; cin >> n; long long res = ((ans[n] * 4) % M + M) % M; cout << res << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void test() { int t; cin >> t; while (t--) { int i = 0; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int i = 0; int n; cin >> n; map<int, int> m; for (int i = 0; i < n; i++) { int x; cin >> x; m[x]++; } int g = 0; for (auto i : m) { if (i.first != 0) g++; } cout << g; return 0; }
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = a; i >= b; i--) using namespace std; typedef unsigned long long ull; typedef pair <int, int> pii; typedef long long ll; template <typename T> inline void read(T &f) { f = 0; T fu = 1; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) { fu = -1; } c = getchar(); } while (c >= 0 && c <= 9 ) { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> void print(T x) { if (x < 0) putchar( - ), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } template <typename T> void print(T x, char t) { print(x); putchar(t); } const int N = 3e5 + 5; struct seg_t { int mx[N << 2]; void build(int u, int l, int r) { mx[u] = 0; if (l == r) return; int mid = (l + r) >> 1; build(u << 1, l, mid); build(u << 1 | 1, mid + 1, r); } void modify(int u, int l, int r, int x, int y) { mx[u] = max(mx[u], y); if (l == r) return; int mid = (l + r) >> 1; if (mid >= x) modify(u << 1, l, mid, x, y); else modify(u << 1 | 1, mid + 1, r, x, y); } int query(int u, int L, int R, int l, int r) { if (l <= L && R <= r) return mx[u]; int mid = (L + R) >> 1, ans = 0; if (mid >= l) ans = max(ans, query(u << 1, L, mid, l, r)); if (mid + 1 <= r) ans = max(ans, query(u << 1 | 1, mid + 1, R, l, r)); return ans; } } tr, rmq; set <int> pos; set <int> :: iterator it; int a[N], cando[N], lpos[N], pre[N], ans[N]; int T, n; int main() { read(T); while (T--) { read(n); rmq.build(1, 1, n); for (int i = 1; i <= n; i++) read(a[i]), rmq.modify(1, 1, n, i, min(n, i + a[i])); tr.build(1, 1, n); for (int i = n; i >= 1; i--) { int r = min(i + a[i], n); if (i + 1 <= r) tr.modify(1, 1, n, i, tr.query(1, 1, n, i + 1, r)); else tr.modify(1, 1, n, i, i); } for (int i = 1; i <= n + 1; i++) cando[i] = 0; cando[1] = 1; pos.clear(); for (int i = 2; i <= n; i++) { if (cando[i - 1]) pos.insert(i - 1); it = pos.lower_bound(i - a[i]); if (it != pos.end()) { pre[i] = lpos[*it]; int r = i - 1; if (lpos[*it] != *it) r = rmq.query(1, 1, n, *it, i - 1); else if (*it + 1 <= i - 1) r = rmq.query(1, 1, n, *it + 1, i - 1); if (r >= i + 1) r = tr.query(1, 1, n, i + 1, r); if (!cando[r + 1]) { cando[r + 1] = 1; lpos[r + 1] = i; } if (r == n) break; } } if (cando[n + 1]) { printf( YES n ); for (int i = 1; i <= n; i++) ans[i] = 0; int now = lpos[n + 1]; while (now) { ans[now] = 1; now = pre[now]; } for (int i = 1; i <= n; i++) { if (ans[i]) putchar( L ); else putchar( R ); } putchar( n ); } else { printf( NO n ); } } return 0; }
#include <bits/stdc++.h> using namespace std; long long int M, tp, xx, lll, cc, ndl = 0, N, dl, nf, nef, T; struct bl { long long int type; long long int l, c, x; }; bl t; vector<bl> B; vector<long long int> C; map<long long int, long long int> MP; ifstream in( input.txt ); long long int find_bl(long long int n) { if (MP.count(n) == 0) { long long int num = (lower_bound(C.begin(), C.end(), n) - C.begin()); t = B[num]; if (t.type == 1) { T = t.x; MP[n] = T; return T; } else { nf = n - C[num - 1]; nef = (nf % t.l == 0) ? t.l : (nf % t.l); T = find_bl(nef); MP[n] = T; return T; } } else return MP[n]; } int main() { cin >> M; for (int i = 1; i <= M; i++) { cin >> tp; t.type = tp; if (tp == 1) { cin >> xx; t.x = xx; B.push_back(t); C.push_back(ndl + 1); ndl++; } else { cin >> lll >> cc; t.l = lll; t.c = cc; B.push_back(t); C.push_back(ndl + (lll * cc)); ndl += lll * cc; } } cin >> N; for (int i = 1; i <= N; i++) { cin >> xx; cout << find_bl(xx) << ; } }
#include <bits/stdc++.h> using namespace std; const int M = 102, T = 1000 * 1000 * 1000 + 7; int a[2600][M]; bool mrk[2600][M]; string s; int ff(long long sum, int n) { if (sum < 0) return 0; if (mrk[sum][n]) return a[sum][n]; if (sum > 25 && n == 1) return 0; if (sum <= 25 && n == 1) return 1; long long temp = 0; for (int i = 0; i < 26; i++) temp += ff(sum - i, n - 1), temp %= T; a[sum][n] = temp; mrk[sum][n] = true; return temp; } int main() { int t; cin >> t; while (t--) { string s; cin >> s; long long sum = 0; for (int i = 0; i < s.length(); i++) sum += s[i] - a ; cout << (ff(sum, s.length()) + T - 1) % T << endl; } return 0; ; }
#include <bits/stdc++.h> using namespace std; const int maxv = 10001; map<string, int> tidx; map<int, int> fp[maxv]; struct Edge { int to, next; } edge[maxv * 2]; int m, idx, head[maxv], ednum, deg[maxv]; char tmp[22]; bool mp[maxv][maxv]; void addedge(int u, int v) { edge[ednum] = Edge{v, head[u]}; head[u] = ednum++; edge[ednum] = Edge{u, head[v]}; head[v] = ednum++; } void cal(int u) { int i, j, vi, vj; deg[u] = 1; for (i = head[u]; i != -1; i = edge[i].next) { deg[u]++; for (j = edge[i].next; j != -1; j = edge[j].next) { vi = edge[i].to; vj = edge[j].to; if (!mp[vi][vj] && !mp[vj][vi]) fp[vi][vj] += 1, fp[vj][vi] += 1; } } } int main() { int a, b, i, maxv, num; string u, v; memset(head, -1, sizeof(head)); map<string, int>::iterator it; map<int, int>::iterator jt; idx = ednum = 0; scanf( %d , &m); for (i = 0; i < m; i++) { scanf( %s , tmp); u = tmp; scanf( %s , tmp); v = tmp; if (tidx.find(u) == tidx.end()) tidx[u] = a = idx++; else a = tidx[u]; if (tidx.find(v) == tidx.end()) tidx[v] = b = idx++; else b = tidx[v]; mp[a][b] = mp[b][a] = true; addedge(a, b); } printf( %d n , idx); for (i = 0; i < idx; i++) cal(i); for (it = tidx.begin(); it != tidx.end(); it++) { i = it->second; maxv = 0, num = idx - fp[i].size() - deg[i]; for (jt = fp[i].begin(); jt != fp[i].end(); jt++) { if (jt->second > maxv) { maxv = jt->second; num = 1; } else if (jt->second == maxv) num++; } printf( %s %d n , it->first.c_str(), num); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, f, k, m; int n[101][2]; cin >> m; for (int i = 0; i < m; i++) { cin >> a >> b >> c >> d >> k; if (a % c == 0) e = a / c; else e = a / c + 1; if (b % d == 0) f = b / d; else f = b / d + 1; if ((e + f) <= k) { n[i][0] = e; n[i][1] = f; } else { n[i][0] = -1; } } for (int i = 0; i < m; i++) { if (n[i][0] != -1) cout << n[i][0] << << n[i][1] << endl; else cout << -1 << endl; } }
#include <bits/stdc++.h> using namespace std; long long n, x, y; string s; int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> n >> x >> y >> s; long long zeros = 0; int last = -1; for (auto c : s) { int b = (c - 0 ); if (b != last && !b) zeros++; last = b; } if (zeros == 0) { cout << 0 << endl; return 0; } long long ans1 = x * (zeros - 1) + y; long long ans2 = y * (zeros - 1) + y; cout << min(ans1, ans2) << endl; }
#include <bits/stdc++.h> using namespace std; template <class T> T sq(T x) { return x * x; } const long long inf = 2147383647; const double pi = 2 * acos(0.0); const double eps = 1e-9; const long long maxint = 2147483647; const long long minint = -2147483648; int n, m; long long arr[100010], brr[100010]; long long dp1[100010], dp2[100010]; void input() { int i; for (i = 0; i < n; i++) { scanf( %I64d , &arr[i]); } for (i = 0; i < m; i++) { scanf( %I64d , &brr[i]); } sort(arr, arr + n); sort(brr, brr + m); } void dpcalc() { dp1[0] = 0; int i; for (i = 1; i < n; i++) { dp1[i] = dp1[i - 1]; dp1[i] += i * (arr[i] - arr[i - 1]); } dp2[m - 1] = 0; for (i = m - 2; i >= 0; i--) { dp2[i] = dp2[i + 1]; int e = m - i - 1; dp2[i] += e * (brr[i + 1] - brr[i]); } } long long findCost(long long x) { long long res = 0; long long pos = upper_bound(arr, arr + n, x) - arr; if (pos > 0) { pos--; res += dp1[pos]; res += (pos + 1) * (x - arr[pos]); } pos = lower_bound(brr, brr + m, x) - brr; if (pos < m) { res += dp2[pos]; res += (m - pos) * (brr[pos] - x); } return res; } int main() { scanf( %d %d , &n, &m); int i, j; input(); dpcalc(); long long low = 1, high = 1000000000; long long res = -1; while (high - low > 5) { long long m1 = (2 * low + high) / 3; long long m2 = (low + 2 * high) / 3; long long u = findCost(m1); long long v = findCost(m2); if (res == -1) res = u; res = min(res, u); res = min(res, v); if (u > v) { low = m1 + 1; } else { high = m2 - 1; } } while (low <= high) { res = min(res, findCost(low)); low++; } printf( %I64d n , res); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5000 + 100; const int M = 30; int n; string s; string t; int mat[M][M][N]; int ones[N]; int ans; long double javab; set<char> g; bool mark[30]; int chand[30]; bool oomade[30]; int main() { cin >> s; n = s.size(); for (int i = 0; i < n; i++) { g.insert(s[i]); chand[s[i] - a ]++; oomade[s[i] - a ] = 1; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n + i; j++) { int y = j % n; int f = (int)s[i] - (int) a ; int l = (int)s[y] - (int) a ; mat[f][l][j - i]++; } } for (int i = 0; i < 26; i++) { if (oomade[i] == 1) { for (int j = 0; j < n; j++) { int tmp = 0; for (int k = 0; k < 26; k++) { if (mat[i][k][j] == 1) { tmp++; } } ones[i] = max(ones[i], tmp); } ans += ones[i]; } } javab = (long double)ans / (n); cout << fixed << setprecision(10) << javab; }
#include <bits/stdc++.h> using namespace std; int main() { int months[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int n, m; cin >> n >> m; vector<int> ans; string s; int idx = 0; while (getline(cin, s)) { int month = (s[5] - 0 ) * 10 + (s[6] - 0 ); int day = (s[8] - 0 ) * 10 + (s[9] - 0 ); int hour = (s[11] - 0 ) * 10 + (s[12] - 0 ); int mnt = (s[14] - 0 ) * 10 + (s[15] - 0 ); int sec = (s[17] - 0 ) * 10 + (s[18] - 0 ); int totalSec = sec + mnt * 60 + hour * 3600 + day * 24 * 3600; for (int i = 0; i < month - 1; i++) { totalSec += months[i] * 24 * 3600; } while (idx < ans.size() and ans[idx] + n <= totalSec) { idx++; } ans.push_back(totalSec); if (ans.size() - idx >= m) { for (int i = 0; i < 19; i++) { printf( %c , s[i]); } puts( ); return 0; } } puts( -1 ); return 0; }
#include <bits/stdc++.h> using namespace std; int fir[5000]; int sec[5000]; int init(int x, int y) { printf( ? %d %d n , x, y); fflush(stdout); scanf( %d , &x); return x; } void print(int sch, const vector<int> &ans) { printf( ! n ); fflush(stdout); printf( %d n , sch); fflush(stdout); for (int i = 0; i < ans.size(); i++) printf( %d , ans[i]); fflush(stdout); } int main() { int n; scanf( %d , &n); for (int i = 0; i < n; i++) fir[i] = init(0, i); for (int i = 0; i < n; i++) sec[i] = init(i, 0); vector<int> ans; int sch = 0; for (int i = 0; i < n; i++) { bool t = true; int col[5000], p[5000], b[5000]; b[0] = i; memset(col, 0, sizeof col); for (int j = 0; j < n; j++) { int x = sec[j] ^ b[0]; if (x >= n || col[x]) { t = false; break; } p[j] = x; col[x]++; } if (t) { memset(col, 0, sizeof col); col[i]++; for (int j = 1; j < n; j++) { int x = fir[j] ^ p[0]; if (x >= n || col[x]) { t = false; break; } b[j] = x; col[x]++; } } if (t) { for (int j = 0; j < n; j++) if (j != b[p[j]]) t = false; } if (t) { if (!sch) { for (int j = 0; j < n; j++) ans.push_back(p[j]); } sch++; } } print(sch, ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, m, ans = 999999999999999999, mn = 123456789000LL; cin >> n >> m; long long x = n; vector<pair<long long, long long> > v; long long cnt = 0; for (long long i = 2; i <= sqrt(m); i++) { if (m % i == 0) { cnt = 0; while (m % i == 0) { cnt++; m = m / i; } v.push_back({i, cnt}); } } if (m > 1) { mn = 1; v.push_back({m, 1}); } for (int i = 0; i < v.size(); i++) { long long y = x; long long temp = 0; while (y) { temp += y / v[i].first; y = y / v[i].first; } temp = temp / v[i].second; ans = min(temp, ans); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int min1 = a; int min2 = b / 2; int min3 = c / 4; int res = min(min(min1, min2), min3); cout << res * 7; return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n,i,j=2,x,m,d,k; cin >> n; if(n%2==1) {cout << NO << endl;return;} while(j<1000000009 && j<=n) { if(n==j) { // cout << loop << endl; cout << YES << endl; return; } //cout << n << << j << endl; j*=2; } n=n/2; j=sqrt(n); if(j*j < n){ if(n%2==1) { cout << NO << endl;return; } n/=2; j=sqrt(n); if(j*j < n) { cout << NO << endl;return; } else { { cout << YES << endl;return; } } } //if(j%2==1) {cout << NO << endl;return;} cout << YES << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t=1; cin >> t; while(t--) solve(); }
#include <bits/stdc++.h> using namespace std; long long n, k; vector<long long> v; long long dp[1001][2001][4]; long long rec(long long i, long long mask, long long comp) { if (i == n && comp == k) return 1; else if (i == n) return 0; else if (comp > k) return 0; if (dp[i][comp][mask] != -1) return dp[i][comp][mask]; if (i == 0) { return dp[i][comp][mask] = (rec(i + 1, 0, comp + 1) + rec(i + 1, 1, comp + 2) + rec(i + 1, 2, comp + 2) + rec(i + 1, 3, comp + 1)) % 998244353; } if (mask == 0) { return dp[i][comp][mask] = (rec(i + 1, 0, comp) + rec(i + 1, 3, comp + 1) + rec(i + 1, 1, comp + 1) + rec(i + 1, 2, comp + 1)) % 998244353; } else if (mask == 1) { return dp[i][comp][mask] = (rec(i + 1, 0, comp) + rec(i + 1, 1, comp) + rec(i + 1, 2, comp + 2) + rec(i + 1, 3, comp)) % 998244353; } else if (mask == 2) { return dp[i][comp][mask] = (rec(i + 1, 0, comp) + rec(i + 1, 1, comp + 2) + rec(i + 1, 2, comp) + rec(i + 1, 3, comp)) % 998244353; } else if (mask == 3) { return dp[i][comp][mask] = (rec(i + 1, 0, comp + 1) + rec(i + 1, 1, comp + 1) + rec(i + 1, 2, comp + 1) + rec(i + 1, 3, comp)) % 998244353; } } void solve() { memset(dp, -1, sizeof(dp)); cin >> n >> k; cout << rec(0, 0, 0); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long q = 1; while (q--) { v.clear(); solve(); } }
#include <bits/stdc++.h> using namespace std; long long g, x, y, z; int n, m; int nn, mm; deque<long long> q; long long a[3010][3010]; long long b[3010][3010]; long long c[3010][3010]; int main() { scanf( %d%d%d%d , &nn, &mm, &n, &m); scanf( %lld%lld%lld%lld , &g, &x, &y, &z); for (int i = 1; i <= nn; i++) { for (int j = 1; j <= mm; j++) { a[i][j] = g; g = g * x + y; g %= z; } } for (int i = 1; i <= nn; i++) { for (int j = mm; j >= 1; j--) { while (!q.empty() && a[i][q.back()] >= a[i][j]) { q.pop_back(); } q.push_back(j); while (!q.empty() && q.front() - q.back() + 1 > m) { q.pop_front(); } if (j <= mm - m + 1) b[i][j] = a[i][q.front()]; } while (!q.empty()) q.pop_front(); } for (int i = 1; i <= mm - m + 1; i++) { for (int j = nn; j >= 1; j--) { while (!q.empty() && b[q.back()][i] >= b[j][i]) { q.pop_back(); } q.push_back(j); while (!q.empty() && q.front() - q.back() + 1 > n) { q.pop_front(); } if (j <= nn - n + 1) c[j][i] = b[q.front()][i]; } while (!q.empty()) q.pop_front(); } long long ans = 0; for (int i = 1; i <= nn - n + 1; i++) { for (int j = 1; j <= mm - m + 1; j++) { ans += c[i][j]; } } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long _pow(long long a, long long b, long long m) { if (b == 0) return 1; long long t = _pow(a, b / 2, m); t = (t * t) % m; if (b & 1) return (t * a) % m; else return t; } int main(int argc, char *argv[]) { int n; cin >> n; vector<string> v; int cnt = 0; bool zero = false; string ex = 1 ; for (int i = 0; i < n; i++) { string x; cin >> x; v.push_back(x); bool f = true; if (x[0] == 0 and x.size() == 1) zero = true; else if (x[0] == 1 ) { int cn = 0; int j; for (j = 1; j < x.size(); j++) if (x[j] != 0 ) { ex = x; break; } else cn++; if (j == x.size()) cnt += cn; } else ex = x; } if (zero) cout << 0 << endl; else { cout << ex; for (int i = 0; i < cnt; i++) cout << 0; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int mod = 1e9 + 7; const int N = 1e5 + 10; inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline int mul(int x, int y) { x = (1LL * x * y) % mod; return x; } int main() { string s; cin >> s; string a = abcdefghijklmnopqrstuvwxyz ; long long k = 0; for (int i = 0; i <= s.size() - 1; ++i) { if (s[i] <= a[k]) { s[i] = a[k]; k++; } } if (k == 26) { cout << s; } else { cout << -1; } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; vector<pair<int, int>> a; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; a.push_back({x, i}); } sort(a.begin(), a.end(), greater<pair<int, int>>()); int numRow = a[0].first + 1; vector<vector<int>> ans(numRow, vector<int>(n, 0)); for (int i = 0; i < a[0].first; i++) ans[i][a[0].second] = 1; int row = 0; for (int i = 1; i < n; i++) { auto [block, col] = a[i]; for (int i = 1; i <= block; i++) ans[(row + i) % numRow][col] = 1; if (row < numRow - 2) row++; } if (!accumulate(ans[numRow - 1].begin(), ans[numRow - 1].end(), 0)) numRow--; cout << numRow << endl; for (int i = 0; i < numRow; i++) { for (auto x : ans[i]) cout << x; cout << endl; } }
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) #pragma GCC target( sse4 ) using namespace std; int q; unsigned int a[200002]; unsigned int b[200002]; unsigned int w; int main() { scanf( %d , &q); for (int i = 0; i < q; ++i) { int t, l, r, k; scanf( %d%d%d , &t, &l, &r); --l, --r; if (t == 1) { scanf( %d , &k); if (k > 0) { w = k; for (int i = l; i < r; ++i) if (a[i]) { a[i] = min(a[i], w); } else { a[i] = w; } } else { w = -k; for (int i = l; i < r; ++i) if (b[i]) { b[i] = min(b[i], w); } else { b[i] = w; } } } else { long long ans = 0; for (int i = l; i < r; ++i) if (a[i] > 0 && b[i] > 0) ans += a[i] + b[i]; printf( %lld n , ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 0 , ch = getchar(); return x; } struct Question { int x, l, r, dat; inline Question(int tmp1 = 0, int tmp2 = 0, int tmp3 = 0, int tmp4 = 0) { x = tmp1, l = tmp2, r = tmp3, dat = tmp4; } inline int operator<(const Question &a) const { return x < a.x; } } opt[100010]; int k; inline int lowbit(int l, int r) { int res = 0; --l; for (int i = 1; i <= k; i <<= 1) res |= (((r / i - l / i) - (((i << 1) <= k) ? (r / (i << 1) - l / (i << 1)) : (0))) & 1) * i; return res; } struct SegmentTree { int tot = 0, rot = 0; int L[100010 * 30], R[100010 * 30], tag[100010 * 30], val[100010 * 30], low[100010 * 30]; inline void upd(int k) { if (tag[k]) val[k] = low[k]; else val[k] = val[L[k]] ^ val[R[k]]; } inline void Mod(int &k, int l, int r, int x, int y, int dat) { if (y < l || r < x) return; if (!k) k = ++tot, low[k] = lowbit(l, r); if (x <= l && r <= y) return tag[k] += dat, upd(k); Mod(L[k], l, ((l + r) >> 1), x, y, dat), Mod(R[k], ((l + r) >> 1) + 1, r, x, y, dat), upd(k); } } T; int main() { int n = read(), m = read(), ans = 0; k = read(); int lim = 1, cnt = 0; for (; lim < k; lim <<= 1) ; while (m--) { int xl = read(), yl = read(); int xr = read(), yr = read(); opt[++cnt] = Question(xl, yl, yr, 1); opt[++cnt] = Question(xr + 1, yl, yr, -1); } sort(opt + 1, opt + cnt + 1); for (int i = 1; i <= cnt; i++) { if (opt[i].x != opt[i - 1].x) { int x = lowbit(opt[i - 1].x, opt[i].x - 1), y = T.val[T.rot]; int a = 0, b = 0, sum = 0; for (int j = lim; j; j >>= 1) { sum = -a * b, a += (x & j) > 0, b += (y & j) > 0, sum += a * b; if (sum & 1) ans ^= j; } } T.Mod(T.rot, 1, n, opt[i].l, opt[i].r, opt[i].dat); } if (ans) puts( Hamed ); else puts( Malek ); return 0; }
#include <bits/stdc++.h> using namespace std; bool isgood(const vector<vector<char> > &graph, const vector<char> &choice) { int n = (int)graph.size() / 2; assert((int)graph[0].size() == 2 * n); assert((int)choice.size() == n); for (int i = 0; i < n; i++) { bool ok = false; int lbound = 2 * i; int rbound = 2 * i + 1; if (choice[i] != -1) lbound = rbound = 2 * i + choice[i]; for (int who = lbound; who <= rbound; who++) { bool cur = true; for (int j = 0; j < n; j++) { if (choice[j] != -1 && graph[who][2 * j + 1 - choice[j]]) cur = false; if (graph[who][2 * j] && graph[who][2 * j + 1]) cur = false; } ok |= cur; } if (!ok) return false; } return true; } void solve(string alph) { int n, m; cin >> n >> m; vector<vector<char> > graph(2 * n, vector<char>(2 * n)); int pos1, pos2; char t1, t2; for (int i = 0; i < m; i++) { scanf( %d %c %d %c , &pos1, &t1, &pos2, &t2); int x1 = 2 * (pos1 - 1) + (t1 == V ? 0 : 1); int x2 = 2 * (pos2 - 1) + (t2 == V ? 0 : 1); graph[x1][x2] = 1; graph[x2 ^ 1][x1 ^ 1] = 1; } string word; cin >> word; assert((int)word.size() == n); for (int i = 0; i < 2 * n; i++) graph[i][i] = 1; for (int k = 0; k < 2 * n; k++) for (int i = 0; i < 2 * n; i++) for (int j = 0; j < 2 * n; j++) graph[i][j] |= (graph[i][k] & graph[k][j]); for (int i = 0; i < 2 * n; i++) if (graph[i][i ^ 1] && graph[i ^ 1][i]) { puts( -1 ); return; } vector<char> choice(n, -1); for (int i = 0; i < n; i++) choice[i] = (alph[word[i] - a ] == V ? 0 : 1); vector<int> next_vowel(alph.size() + 1, -1), next_cons(alph.size() + 1, -1); for (int i = 0; i < (int)alph.size(); i++) { for (int j = i; j < (int)alph.size(); j++) { if (alph[j] == V && next_vowel[i] == -1) next_vowel[i] = j; if (alph[j] == C && next_cons[i] == -1) next_cons[i] = j; } } if (isgood(graph, choice)) { puts(word.c_str()); return; } int lcp; for (lcp = n - 1; lcp > 0; lcp--) { int num = word[lcp] - a + 1; if (next_vowel[num] != -1) { choice[lcp] = 0; if (isgood(graph, choice)) break; } if (next_cons[num] != -1) { choice[lcp] = 1; if (isgood(graph, choice)) break; } choice[lcp] = -1; } choice[lcp] = -1; string ans = word.substr(0, lcp); for (int i = lcp; i < n; i++) { char start = (i == lcp ? word[i] + 1 : a ); bool was_vowel = false; bool was_cons = false; bool ok = false; for (char ch = start; ch < a + (int)alph.size(); ch++) { if (!was_vowel && alph[ch - a ] == V ) { was_vowel = true; choice[i] = 0; ok |= isgood(graph, choice); if (ok) { ans += ch; break; } } if (!was_cons && alph[ch - a ] == C ) { was_cons = true; choice[i] = 1; ok |= isgood(graph, choice); if (ok) { ans += ch; break; } } } if (!ok) { puts( -1 ); return; } } puts(ans.c_str()); } int main() { string n; while (cin >> n) solve(n); }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, s, a, b, c, d; cin >> n >> s; d = s; for (int i = 0; i < n; i++) { cin >> a >> b; c = a + b; d = max(c, d); } cout << d; }
#include <bits/stdc++.h> using namespace std; int N, K; long long Time; int Free; multiset<long long> Working; int main() { scanf( %d%d , &N, &K); Free = K; Time = 0; long long S, M; for (int i = 0; i < N; ++i) { scanf( %I64d%I64d , &S, &M); S = max(S, Time); if (Free > 0) { --Free; printf( %I64d n , S + M); Working.insert(S + M); Time = S; } else { Time = *Working.begin(); Time = max(Time, S); printf( %I64d n , Time + M); Working.erase(Working.begin()); Working.insert(Time + M); } while ((not Working.empty()) and ((*Working.begin()) <= Time)) { ++Free; Working.erase(Working.begin()); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 2e5 + 1; vector<long long int> a[N]; vector<long long int> order, pos(N); bool topsort(long long int n) { order.clear(); queue<long long int> q; vector<long long int> indeg(n + 1, 0); for (long long int i = 1; i <= n; i++) for (auto &it : a[i]) indeg[it]++; for (long long int i = 1; i <= n; i++) if (!indeg[i]) q.push(i); long long int idx = 0; while (!q.empty()) { int v = q.front(); q.pop(); order.push_back(v); idx++; pos[v] = idx; for (auto &u : a[v]) { indeg[u]--; if (!indeg[u]) q.push(u); } } return (order.size() == n); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; vector<long long int> a1(n + 1), b(n + 1), c; for (long long int i = 1; i <= n; i++) cin >> a1[i]; for (long long int i = 1; i <= n; i++) { cin >> b[i]; if (b[i] != -1) a[i].push_back(b[i]); } topsort(n); vector<long long int> ans, tmp; long long int sum1 = 0, k = 0; for (long long int i = 0; i < order.size(); i++) { long long int pos = order[i]; sum1 += a1[pos]; if (b[pos] != -1 && a1[pos] < 0) tmp.push_back(pos); else { if (b[pos] != -1) a1[b[pos]] += a1[pos]; ans.push_back(pos); } } reverse(tmp.begin(), tmp.end()); for (long long int i = 0; i < tmp.size(); i++) ans.push_back(tmp[i]); cout << sum1 << n ; for (long long int i = 0; i < n; i++) cout << ans[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; long double optN(long long m, int k) { long double ret = 1; m *= k; long long nowC = k; for (int i = 1; i <= k; ++i) { if (m < nowC * i) { ret += m / i; return ret; } ret += nowC; m -= nowC * i; if (m / (i + 1.) / nowC <= (k - i) / (i + 1.)) return ret + m / (i + 1); nowC *= k - i; nowC /= (i + 1); } return ret; }; int main() { int CASE; cin >> CASE; for (int TCASE = 1; TCASE <= CASE; ++TCASE) { long long n, m; cin >> n >> m; long long l = 0, r = (0x3f3f3f3f); while (l < r) { long long mid = l + r >> 1; if (optN(m, mid) >= n) r = mid; else l = mid + 1; } cout << l << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const int INF = 1 << 28; const long long INFL = 1LL << 62; int toi(string x) { int z; istringstream iss(x); iss >> z; return z; } string str[] = { YES , NO }; int modulo(int m, int n) { return m >= 0 ? m % n : (n - abs(m % n)) % n; } using namespace std; int arr[101] = {0}, n, m; int main() { cin >> n >> m; for (int i = 1, a, b, c; i <= m; i++) { cin >> a >> b >> c; arr[a] -= c; arr[b] += c; } int sum = 0; for (int i = 1; i <= n; i++) if (arr[i] > 0) sum += arr[i]; cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, b[100005], a[200005]; void solve() { cin >> n; for (long long i = 1; i < n / 2 + 1; i++) cin >> b[i]; a[1] = 0; a[n] = b[1]; for (long long i = 2; i < n / 2 + 1; i++) { a[i] = max(a[i - 1], b[i] - a[n - i + 2]); a[n - i + 1] = b[i] - a[i]; } for (long long i = 1; i < n + 1; i++) cout << a[i] << ; cout << n ; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, x1, x2, y1, y2, i, sum = 0; cin >> n; for (i = 0; i < n; i++) { cin >> x1 >> y1 >> x2 >> y2; sum += ((x2 - x1) + 1) * ((y2 - y1) + 1); } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; const int Inf = (int)1e9; const long long InfL = (long long)1e18; const int MaxN = (int)2e5 + 5; vector<int> adj[MaxN], ans; int n, a, deg[MaxN], dead[MaxN]; void dfs2(int x, int p) { dead[x] = true; ans.push_back(x); for (auto u : adj[x]) --deg[u]; for (auto u : adj[x]) if (u != p && !dead[u]) dfs2(u, x); } void dfs1(int x, int p) { for (auto u : adj[x]) if (u != p) dfs1(u, x); if (!(deg[x] & 1)) dfs2(x, p); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) { cin >> a; if (!a) continue; adj[i].push_back(a); deg[i]++; adj[a].push_back(i); deg[a]++; } dfs1(1, 0); if (ans.size() == n) { cout << YES << n ; for (int i = 0; i < n; i++) cout << ans[i] << n ; } else { cout << NO << n ; } return 0; }
#include <bits/stdc++.h> int mod = 1000000007; const int M = INT_MAX; using namespace std; int powb(int x, int y) { int res = 1; x = x % M; while (y > 0) { if (y % 2) res = (res * x) % M; y /= 2; x = (x * x) % M; } return res; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int solve() { int n, t, s = 0, x, s1 = 0, i, j; cin >> n >> t; vector<int> v(2 * n); for (i = 0; i < n; i++) { cin >> x; s1 += x; v[i] = x; v[i + n] = x; } if (s1 <= t) return n; int max = 0; x = 0; for (i = 0; i < 2 * n; i++) { s += v[i]; if (s > t) { s -= v[i]; break; } x++; if (x > max) max = x; } for (i = 1; i < n; i++) { s -= v[i - 1]; x--; for (j = i + x; j < n; j++) { s += v[j]; if (s > t) { s -= v[j]; break; } x++; if (x > max) max = x; } if (j == n - 1) break; } return max; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << solve(); return 0; }
#include <bits/stdc++.h> using namespace std; void read(int &val) { int x = 0; int bz = 1; char c; for (c = getchar(); (c < 0 || c > 9 ) && c != - ; c = getchar()) ; if (c == - ) { bz = -1; c = getchar(); } for (; c >= 0 && c <= 9 ; c = getchar()) x = x * 10 + c - 48; val = x * bz; } const int INF = 0x3f3f3f3f; const int maxn = 1e6 + 100; const int mod = 998244353; long long a[maxn], b[maxn]; int n, k; long long sum[maxn], tim[maxn]; struct node { int id; bool operator<(const node bb) const { return tim[id] > tim[bb.id]; } }; bool check(long long mid) { priority_queue<node> q; for (int i = 1; i <= n; i++) { tim[i] = a[i] / b[i]; sum[i] = a[i]; q.push(node{i}); } for (int i = 0; i <= k - 1; i++) { node top = q.top(); q.pop(); int xx = top.id; if (tim[xx] < i) return false; if (tim[xx] >= k) return true; sum[xx] += mid; tim[xx] = sum[xx] / b[xx]; if (tim[xx] < i) return false; q.push(node{xx}); } return true; } int main() { read(n); read(k); for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); for (int i = 1; i <= n; i++) scanf( %lld , &b[i]); long long ans = -1, l = 0, r = 1e13; while (l <= r) { long long mid = l + r >> 1; if (check(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, L, start, endP; vector<vector<long long> > edges; vector<vector<long long> > neighbors; vector<vector<long long> > paths; long long weights[1000][1000] = {0}; long long dijk() { vector<long long> distances(n, LLONG_MAX); distances[start] = 0; vector<bool> visited(n, false); priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long> > > pq; pq.push({0, start}); while (!pq.empty()) { long long currentWeight = pq.top().first; long long currentNode = pq.top().second; pq.pop(); if (visited[currentNode]) continue; visited[currentNode] = true; if (currentNode == endP) break; for (long long i = 0; i < neighbors[currentNode].size(); i++) { long long currNeighbor = neighbors[currentNode][i]; long long currDist = weights[currentNode][currNeighbor]; if (currDist == -1) continue; if (currentWeight + currDist < distances[currNeighbor]) distances[currNeighbor] = currentWeight + currDist; if (!visited[currNeighbor]) pq.push({distances[currNeighbor], currNeighbor}); } } return distances[endP]; } long long dijk2() { vector<long long> distances(n, LLONG_MAX); distances[start] = 0; vector<bool> visited(n, false); priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long> > > pq; pq.push({0, start}); paths[start].push_back(start); while (!pq.empty()) { long long currentWeight = pq.top().first; long long currentNode = pq.top().second; pq.pop(); if (visited[currentNode]) continue; visited[currentNode] = true; if (currentNode == endP) break; for (long long i = 0; i < neighbors[currentNode].size(); i++) { long long currNeighbor = neighbors[currentNode][i]; long long currDist = weights[currentNode][currNeighbor]; if (currDist == -1) currDist = 1; if (currentWeight + currDist < distances[currNeighbor]) { distances[currNeighbor] = currentWeight + currDist; paths[currNeighbor] = paths[currentNode]; paths[currNeighbor].push_back(currNeighbor); } if (!visited[currNeighbor]) pq.push({distances[currNeighbor], currNeighbor}); } } return distances[endP]; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> L >> start >> endP; edges.resize(m); neighbors.resize(n); paths.resize(n); long long from, to, weight; for (long long i = 0; i < m; i++) { cin >> from >> to >> weight; if (weight == 0) weight = -1; weights[from][to] = weight; weights[to][from] = weight; edges[i] = {from, to, weight}; neighbors[from].push_back(to); neighbors[to].push_back(from); } long long currMin = dijk(); if (currMin < L) { cout << NO << endl; return 0; } long long newMin = dijk2(); if (newMin > L) { cout << NO << endl; return 0; } for (long long i = 1; i < paths[endP].size(); i++) { long long prev = paths[endP][i - 1]; long long curr = paths[endP][i]; if (weights[prev][curr] == -1) { weights[prev][curr] = -2; weights[curr][prev] = -2; } } for (long long i = 0; i < edges.size(); i++) { long long from = edges[i][0]; long long to = edges[i][1]; long long weight = weights[from][to]; if (weight == -1) { weights[from][to] = 999999999999; weights[to][from] = 999999999999; } } for (long long i = 1; i < paths[endP].size(); i++) { long long prev = paths[endP][i - 1]; long long curr = paths[endP][i]; if (weights[prev][curr] == -2) { weights[prev][curr] = -1; weights[curr][prev] = -1; } } while (newMin != L) { long long diff = L - newMin; for (long long i = 1; i < paths[endP].size(); i++) { long long prev = paths[endP][i - 1]; long long curr = paths[endP][i]; if (weights[prev][curr] == -1) { weights[prev][curr] = diff + 1; weights[curr][prev] = diff + 1; break; } } paths.clear(); paths.resize(n); newMin = dijk2(); } for (long long i = 1; i < paths[endP].size(); i++) { long long prev = paths[endP][i - 1]; long long curr = paths[endP][i]; if (weights[prev][curr] == -1) { weights[prev][curr] = 1; weights[curr][prev] = 1; } } cout << YES << endl; for (long long i = 0; i < edges.size(); i++) { long long from = edges[i][0]; long long to = edges[i][1]; long long weight = weights[from][to]; if (weight == -1) weight = 999999999999; cout << from << << to << << weight << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int socks[n + 1]; memset(socks, 0, sizeof(socks)); int maxi = -1; int brc = 0; for (int i = 1; i <= 2 * n; i++) { int x; cin >> x; if (socks[x] == 0) { socks[x]++; brc++; } else { socks[x]++; brc--; } if (maxi < brc) { maxi = brc; } } cout << maxi; return 0; }
#include <bits/stdc++.h> using namespace std; void Freopen() { freopen( title .in , r , stdin); freopen( title .out , w , stdout); } int read() { int g = 0, f = 1; char ch = getchar(); while (ch < 0 || 9 < ch) { if (ch == - ) f = -1; ch = getchar(); } while ( 0 <= ch && ch <= 9 ) { g = g * 10 + ch - 0 ; ch = getchar(); } return g * f; } const int N = 1e5 + 5; const int INF = 1e9 + 1; struct Seg { int tag[N << 2], mx[N << 2], cx[N << 2], size[N << 2], ok[N << 2]; long long su[N << 2]; void updata(int v, int w) { su[v] -= 1ll * ok[v] * size[v] * mx[v]; tag[v] = min(tag[v], w); mx[v] = min(mx[v], w); su[v] += 1ll * (ok[v] = 1) * size[v] * mx[v]; } void down(int v) { if (!(tag[v] ^ INF)) return void(); updata(v << 1, tag[v]), updata(v << 1 | 1, tag[v]), tag[v] = INF; } void up(int v) { ok[v] = ok[v << 1] & ok[v << 1 | 1]; su[v] = su[v << 1] + su[v << 1 | 1]; mx[v] = max(mx[v << 1], mx[v << 1 | 1]); cx[v] = max(cx[v << 1], cx[v << 1 | 1]); if (mx[v << 1] > mx[v << 1 | 1]) cx[v] = max(cx[v], mx[v << 1 | 1]); if (mx[v << 1] < mx[v << 1 | 1]) cx[v] = max(cx[v], mx[v << 1]); size[v] = size[v << 1] * (mx[v << 1] == mx[v]) + size[v << 1 | 1] * (mx[v << 1 | 1] == mx[v]); } void build(int v, int l, int r) { mx[v] = INF, size[v] = r - l + 1, tag[v] = INF, cx[v] = -INF; if (l == r) return void(); int mid = l + r >> 1; build(v << 1, l, mid), build(v << 1 | 1, mid + 1, r); } void change(int v, int l, int r, int ql, int qr, int w) { if (ql <= l && r <= qr) { if (mx[v] <= w) return void(); else if (w >= cx[v]) return updata(v, w), void(); } int mid = l + r >> 1; down(v); if (ql <= mid) change(v << 1, l, mid, ql, qr, w); if (qr > mid) change(v << 1 | 1, mid + 1, r, ql, qr, w); up(v); } } seg[2]; long long query(int v, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) { if (seg[0].ok[v] && seg[1].ok[v]) return seg[0].su[v] + seg[1].su[v]; else if (l == r || !seg[0].su[v] || !seg[1].su[v]) return 0; } int mid = l + r >> 1; seg[0].down(v), seg[1].down(v); if (qr <= mid) return query(v << 1, l, mid, ql, qr); if (ql > mid) return query(v << 1 | 1, mid + 1, r, ql, qr); return query(v << 1, l, mid, ql, qr) + query(v << 1 | 1, mid + 1, r, ql, qr); } signed main() { int n = 1e5; seg[0].build(1, 1, n), seg[1].build(1, 1, n); for (int T = read(); T--;) { int opt = read(), l = read(), r = read() - 1, x; if (opt == 1) x = read(), seg[x > 0].change(1, 1, n, l, r, abs(x)); else cout << query(1, 1, n, l, r) << n ; } return signed(); }
#include <bits/stdc++.h> using namespace std; int prime[1000000]; bool status[1000100]; int pos; void sieve() { int i, j; for (i = 0; i < 1000100; i++) { status[i] = false; } int sq = sqrt(1000100); for (i = 4; i <= 1000100; i += 2) { status[i] = true; } for (i = 3; i <= sq; i += 2) { if (status[i] == false) { for (j = 2 * i; j <= 1000100; j += i) { status[j] = true; } } } status[1] = true; pos = 1; for (int z = 1; z <= 1000100; z++) { if (status[z] == false) { prime[pos++] = z; } } } int main() { sieve(); long long n; while (cin >> n) { long long ans = 1; for (int i = 1; i < pos; i++) { if (n == 1) break; bool f = false; while (n % prime[i] == 0) { f = true; n /= prime[i]; } if (f) ans *= prime[i]; } if (n > 1) ans *= n; cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << ; cerr << endl; } const int MAX_N = 100000; vector<int> primes; bool isPrime[MAX_N]; void MakePrimes() { for (int i = 0; i < (int)(MAX_N); ++i) isPrime[i] = true; isPrime[0] = isPrime[1] = false; for (int i = 2; i < MAX_N; i++) { if (isPrime[i]) { for (int j = i + i; j < MAX_N; j += i) isPrime[j] = false; } } for (int i = 0; i < (int)(MAX_N); ++i) if (isPrime[i]) primes.push_back(i); } bool a(int n) { if (n < MAX_N) return isPrime[n]; for (int i = 0; primes[i] * primes[i] <= n; i++) if (n % primes[i] == 0) return false; return true; } const int MILLION = 1000000; int h[] = { 0, 39176, 74417, 108284, 141503, 174194, 206333, 238261, 269760, 301101, 332181, 363080, 393993, 424569, 454974, 485275, 515414, 545501, 575503, 605357, 635171, 664771, 694490, 723896, 753318, 782754, 812028, 841364, 870514, 899686, 928780, 957871, 986746, 1015619, 1044521, 1073174, 1101903, 1130652, 1159339, 1188093, 1216688, 1245272, 1273689, 1302081, 1330570, 1358925, 1387240, 1415626, 1443939, 1472195, 1500453, 1528622, 1556702, 1584772, 1612824, 1640857, 1668892, 1696863, 1724880, 1752772, 1780671, 1808647, 1836360, 1864207, 1892075, 1919906, 1947727, 1975510, 2003304, 2031020, 2058719, 2086330, 2113990, 2141713, 2169247, 2196835, 2224515, 2252025, 2279483, 2306931, 2334374, 2361872, 2389450, 2416904, 2444352, 2471679, 2499026, 2526330, 2553802, 2581128, 2608394, 2635613, 2662840, 2690185, 2717374, 2744682, 2771910, 2799109, 2826276, 2853362, 2880505, 2907606, 2934703, 2961881, 2988902, 3016009, 3043022, 3069997, 3097083, 3124226, 3151359, 3178276, 3205350, 3232326, 3259118, 3286023, 3312951, 3339778, 3366670, 3393551, 3420476, 3447272, 3474095, 3500974, 3527908, 3554739, 3581527, 3608315, 3635172, 3662084, 3688865, 3715571, 3742387, 3769101, 3795810, 3822594, 3849184, 3875855, 3902460, 3929085, 3955921, 3982460, 4009128, 4035734, 4062451, 4089090, 4115722, 4142364, 4168923, 4195422, 4221985, 4248402, 4274957, 4301295, 4327912, 4354389, 4380845, 4407487, 4433902, 4460241, 4486724, 4513194, 4539593, 4566061, 4592654, 4619006, 4645360, 4671705, 4698103, 4724481, 4750950, 4777296, 4803668, 4830058, 4856492, 4882798, 4909157, 4935488, 4961878, 4988226, 5014695, 5040863, 5067205, 5093333, 5119591, 5145981, 5172232, 5198500, 5224741, 5250964, 5277359, 5303300, 5329410, 5355703, 5381929, 5408176, 5434359, 5460458, 5486492, 5512631, 5538821, 5564989, 5591257, 5617364, 5643587, 5669724, 5695725, 5721870, 5747922, 5773921, 5800089, 5826127, 5852352, 5878520, 5904577, 5930672, 5956845, 5982939, 6008887, 6034853, 6060852, 6086846, 6112891, 6139005, 6164976, 6191134, 6217047, 6243137, 6268950, 6294880, 6320783, 6346739, 6372759, 6398760, 6424585, 6450524, 6476474, 6502519, 6528444, 6554452, 6580260, 6606273, 6632211, 6657923, 6683806, 6709795, 6735695, 6761489, 6787249, 6813192, 6838975, 6864928, 6890595, 6916351, 6942266, 6968229, 6994053, 7019911, 7045744, 7071649, 7097378, 7123329, 7149099, 7175070, 7200837, 7226696, 7252624, 7278251, 7304018, 7329881, 7355504, 7381276, 7406982, 7432639, 7458445, 7484264, 7509988, 7535700, 7561435, 7587022, 7612748, 7638354, 7664134, 7689731, 7715474, 7741178, 7766793, 7792385, 7818155, 7843890, 7869478, 7895233, 7920913, 7946432, 7972124, 7997861, 8023413, 8049029, 8074668, 8100189, 8125719, }; int hard(int n) { int k = 0; while (k * MILLION <= n) { k++; } k--; int ans = h[k]; if (k == 0) { if (2 <= n) if (a(2)) ++ans; for (int i = 5; i <= n; i += 4) { if (a(i)) ++ans; } return ans; } else { for (int i = k * MILLION + 1; i <= n; i += 4) { if (a(i)) ++ans; } return ans; } } int main() { MakePrimes(); int L, R; cin >> L >> R; cout << hard(R) - hard(L - 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; void solve() { int n, k; cin >> n >> k; string s; cin >> s; vector<pair<int, int>> res; string s2; int n2 = n - 2 * (k - 1); for (int i = 0; i < 2 * (k - 1); ++i) { if (i % 2) { s2.push_back( ) ); } else { s2.push_back( ( ); } } for (int i = 0; i < n2; ++i) { if (i < n2 / 2) { s2.push_back( ( ); } else { s2.push_back( ) ); } } for (int i = 0; i < n; ++i) { if (s[i] != s2[i]) { for (int j = i + 1; j < n; ++j) { if (s[j] == s2[i]) { reverse(s.begin() + i, s.begin() + j + 1); res.push_back({i, j}); break; } } } } cout << res.size() << n ; for (const auto &p : res) { cout << p.first + 1 << << p.second + 1 << n ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long i, j, k, l, n, m, a, b, u; cin >> n; vector<long long> v(n); map<long long, long long> mp; long long ans = 0LL; priority_queue<pair<long long, long long>> pq; for (i = 0; i < n; i++) { cin >> v[i]; mp[v[i]]++; } unordered_set<long long> st; for (auto it : mp) { pq.push({it.second, it.first}); } map<long long, long long> mp2; while (!pq.empty()) { auto p = pq.top(); pq.pop(); if (p.first == 0) continue; if (st.find(p.first) == st.end()) { st.insert(p.first); ans += p.first; } else { pq.push({p.first - 1, p.second}); } } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> int main() { int m, n; scanf( %d %d , &n, &m); if (n == 1) { if (m == 2) { printf( 0 n ); printf( 1 1 n ); printf( 1 2 n ); printf( 1 1 n ); return 0; } else { printf( 1 n ); printf( 1 %d 1 1 n , m); for (int i = 1; i <= m; ++i) printf( 1 %d n , i); printf( 1 1 n ); } } else if (m == 1) { if (n == 2) { printf( 0 n ); printf( 1 1 n ); printf( 2 1 n ); printf( 1 1 n ); return 0; } else { printf( 1 n ); printf( %d 1 1 1 n , n); for (int i = 1; i <= n; ++i) printf( %d 1 n , i); printf( 1 1 n ); } } else if (n % 2 == 0) { printf( 0 n ); for (int i = 1; i <= n; ++i) printf( %d 1 n , i); bool direita = true; for (int lin = n; lin >= 1; --lin) { if (direita) { for (int j = 2; j <= m; ++j) printf( %d %d n , lin, j); } else { for (int j = m; j > 1; --j) printf( %d %d n , lin, j); } direita = !direita; } printf( 1 1 n ); } else if (m % 2 == 0) { printf( 0 n ); for (int j = 1; j <= m; ++j) printf( 1 %d n , j); bool baixo = true; for (int col = m; col >= 1; --col) { if (baixo) { for (int i = 2; i <= n; ++i) printf( %d %d n , i, col); } else { for (int i = n; i > 1; --i) printf( %d %d n , i, col); } baixo = !baixo; } printf( 1 1 n ); } else { printf( 1 n ); printf( %d %d 1 1 n , n, m); bool direita = true; for (int i = 1; i <= n; ++i) { if (direita) { for (int j = 1; j <= m; ++j) printf( %d %d n , i, j); } else { for (int j = m; j >= 1; --j) printf( %d %d n , i, j); } direita = !direita; } printf( 1 1 n ); } }
#include<bits/stdc++.h> #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL) ;cout.tie(NULL) using namespace std; #define ll long long int #define pb push_back #define endn n #define mp make_pair #define ss second #define ff first #define deb(x) cout << #x << << x << endl #define deb1(x,y) cout<<#x<< <<x<< , <<#y<< <<y<<endl #define deb2(x,y,z) cout<<#x<< <<x<< , <<#y<< <<y<< , <<#z<< <<z<<endl const long long INF = 1e18; int small = INT_MIN; int large = INT_MAX; void solve() { int n, k; cin >> n >> k; int a[n]; int i = 2, j = n; a[0] = 1; int t = 1; while (k-- && t < n - 1) { a[t] = j; t++; j--; a[t] = i; t++; i++; } if (k != -1) { cout << -1; } else { while (i <= j) { a[t] = i; t++; i++; } for (int x : a) { cout << x << ; } } cout << endn; } int main() { fast_cin(); ll T = 1; cin >> T; while (T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0; char s = getchar(); bool w = 0; while (s < 0 || s > 9 ) { w |= s == - ; s = getchar(); } while (s >= 0 && s <= 9 ) { x = (x << 3) + (x << 1) + (s ^ 48); s = getchar(); } return w ? -x : x; } const long long inf = 1e18; long long n, m, sx, sy, tim; struct edge { long long ne, to, dis; } e[2100], E[2100000]; struct node { long long t, c; } a[2100]; long long head[2100], head1[2100000], tot, num, dis[1100]; bool vis[1100]; priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long> > > q; inline void add(long long fr, long long to, long long dis) { e[++tot].ne = head[fr]; e[tot].to = to; e[tot].dis = dis; head[fr] = tot; } inline void Add(long long fr, long long to, long long dis) { E[++num].ne = head1[fr]; E[num].to = to; E[num].dis = dis; head1[fr] = num; } inline void Dij1(long long s) { for (long long i = 1; i <= n; i++) { dis[i] = inf; vis[i] = 0; } dis[s] = 0; q.push(make_pair(dis[s], s)); while (!q.empty()) { long long u = q.top().second; q.pop(); if (vis[u]) continue; vis[u] = 1; for (long long i = head[u]; i; i = e[i].ne) { long long v = e[i].to; if (dis[v] > dis[u] + e[i].dis && dis[u] + e[i].dis <= a[s].t) { dis[v] = dis[u] + e[i].dis; Add(s, v, a[s].c); if (!vis[v]) q.push(make_pair(dis[v], v)); } } } } inline void Dij2(long long s) { for (long long i = 1; i <= n; i++) { dis[i] = inf; vis[i] = 0; } dis[s] = 0; q.push(make_pair(dis[s], s)); while (!q.empty()) { long long u = q.top().second; q.pop(); if (vis[u]) continue; vis[u] = 1; for (long long i = head1[u]; i; i = E[i].ne) { long long v = E[i].to; if (dis[v] > dis[u] + E[i].dis) { dis[v] = dis[u] + E[i].dis; if (!vis[v]) q.push(make_pair(dis[v], v)); } } } } signed main() { n = read(); m = read(); sx = read(); sy = read(); for (long long i = 1; i <= m; i++) { long long u, v, w; u = read(); v = read(); w = read(); add(u, v, w); add(v, u, w); } for (long long i = 1; i <= n; i++) a[i].t = read(), a[i].c = read(); for (long long i = 1; i <= n; i++) Dij1(i); Dij2(sx); if (dis[sy] != inf) printf( %I64d n , dis[sy]); else printf( -1 n ); return 0; }
#include <bits/stdc++.h> using namespace std; bool prime[10000007]; void SieveOfEratosthenes() { memset(prime, true, sizeof(prime)); for (long long int p = 2; p * p <= 10000007; p++) { if (prime[p] == true) { for (long long int i = p * p; i <= 10000007; i += p) prime[i] = false; } } } const long long int N = 300500; long long int fact[N]; long long int invFact[N]; long long int fast_pow(long long int a, long long int p, long long int m) { long long int res = 1; while (p) { if (p % 2 == 0) { a = (a * a) % m; p /= 2; } else { res = (res * a) % m; p--; } } return res; } void Call_For_nCr() { fact[0] = invFact[0] = 1; for (long long int i = 1; i < N; i++) { fact[i] = (fact[i - 1] * i) % 1000000007; invFact[i] = fast_pow(fact[i], 1000000007 - 2, 1000000007); } } long long int nCR(long long int n, long long int k, long long int m) { if (k > n) { return 0; } return fact[n] * invFact[k] % m * invFact[n - k] % m; } long long int sumOfDigit(long long int n, long long int b) { long long int unitDigit, sum = 0; while (n > 0) { unitDigit = n % b; sum += unitDigit; n = n / b; } return sum; } long long int factorial(long long int n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } bool compare(pair<long long int, long long int> a, pair<long long int, long long int> b) { if (a.first < b.first) return 1; if (b.first < a.first) return 0; if (a.second > b.second) return 1; return 0; } void solve() { string s; cin >> s; long long int ans = 0, one = 0, ok = 0, zero = 0; for (long long int i = 0; i < s.length(); i++) { if (s[i] == 0 ) { zero = 1; ok = 1; if (one && zero) ans = 1; } if (s[i] == 1 ) { if (zero) one = 1; } } if (!ok) { cout << 0 << n ; return; } if (ans) cout << 2 << n ; else cout << 1 << n ; } void init_code() { ios_base::sync_with_stdio(false); cin.tie(NULL); } signed main() { init_code(); long long int t = 1; cin >> t; long long int i = 1; while (t--) { solve(); i++; } return 0; }
#include <bits/stdc++.h> using namespace std; inline bool is_palindrome(const string& s) { return std::equal(s.begin(), s.end(), s.rbegin()); } const long long MOD = 1000000007; const long long INF = 1e9 + 5; const double eps = 1e-7; const double PI = acos(-1.0); inline void debug_vi(vector<int> a) { for (long long i = (long long)(0); i < (long long)(a.size()); i++) cout << a[i] << ; } inline void debug_vll(vector<long long> a) { for (long long i = (long long)(0); i < (long long)(a.size()); i++) cout << a[i] << ; } const int N = 1e5 + 5; int arr[N]; pair<int, int> xpos, ypos; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, l, x, y, j; cin >> n >> l >> x >> y; xpos = {-1, -1}; ypos = xpos; for (long long i = (long long)(0); i < (long long)(n); i++) { cin >> arr[i]; } j = 0; for (long long i = (long long)(0); i < (long long)(n); i++) { while (j < n && arr[j] - arr[i] < x) j++; if (j < n && arr[j] - arr[i] == x) { xpos = {i, j}; break; } } j = 0; for (long long i = (long long)(0); i < (long long)(n); i++) { while (j < n && arr[j] - arr[i] < y) j++; if (j < n && arr[j] - arr[i] == y) { ypos = {i, j}; break; } } if (xpos.first != -1 && ypos.first != -1) { cout << 0; } else if (xpos.first != -1 || ypos.first != -1) { cout << 1 << n ; if (xpos.first != -1) { cout << y; } else { cout << x; } } else { j = 0; for (long long i = (long long)(0); i < (long long)(n); i++) { while (j < n && arr[j] - arr[i] < x + y) j++; if (j < n && arr[j] - arr[i] == x + y) { cout << 1 << n ; cout << arr[i] + x << n ; return 0; } } j = 0; for (long long i = (long long)(0); i < (long long)(n); i++) { while (j < n && arr[j] - arr[i] < y - x) j++; if (j < n && arr[j] - arr[i] == y - x) { if (arr[i] - x >= 0) { cout << 1 << n ; cout << arr[i] - x; return 0; } else if (arr[j] + x <= l) { cout << 1 << n ; cout << arr[j] + x; return 0; } } } cout << 2 << n ; cout << x << << y; } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void in(T &x) { x = 0; bool f = 0; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) f = 1; c = getchar(); } while ( 0 <= c && c <= 9 ) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } if (f) x = -x; } char tc[1000005]; int a[1000005], b[1000005]; int n, m; int sum[1000005][3][3]; signed main() { scanf( %s , tc + 1); n = strlen(tc + 1); for (register int i = 1; i <= n; ++i) a[i] = (tc[i] == R ? 0 : (tc[i] == G ? 1 : 2)); scanf( %s , tc + 1); m = strlen(tc + 1); for (register int i = 1; i <= m; ++i) b[i] = (tc[i] == R ? 0 : (tc[i] == G ? 1 : 2)); for (register int i = 2; i <= m; ++i) { for (register int j = 0; j < 3; ++j) for (register int k = 0; k < 3; ++k) sum[i][j][k] = sum[i - 1][j][k]; ++sum[i][b[i - 1]][b[i]]; } int l = 1, r = 0; long long ans = 0; for (register int i = 1; i <= n; ++i) { ++r; while (r < m && b[r] != a[i]) ++r; if (r > m) r = m; if (i > 1 && b[l] == a[i - 1]) ++l; if (l > m) break; ans += r - l + 1; if (i > 1 && a[i] != a[i - 1]) ans -= sum[r][a[i]][a[i - 1]] - sum[l - 1][a[i]][a[i - 1]]; } printf( %lld , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAX = 100001; long long n, k, l[MAX], inf = 1000000000000; multiset<long long> kek; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> l[i + 1]; } long long a; cin >> a; long long ans = 0; kek.insert(inf); for (int i = 1; i <= n; i++) { long long b; cin >> b; kek.insert(b); while (k < l[i]) { if (*(kek.begin()) == inf) { cout << -1; return 0; } long long take = *(kek.begin()); kek.erase(kek.find(take)); k += a; ans += take; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], b[1000000] = {0}; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n / 2; i++) { if (a[i] - i - 1 >= 0) b[a[i] - i - 1]++; if (a[n - 1 - i] - i - 1 >= 0) b[a[n - 1 - i] - i - 1]++; } if (n % 2 && a[n / 2] - (n / 2 + 1) >= 0) { b[a[n / 2] - (n / 2 + 1)]++; } int m = -1; for (int i = 0; i < 1000000; i++) m = m > b[i] ? m : b[i]; cout << n - m; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, a, b; int main() { scanf( %lld %lld %lld %lld , &n, &m, &a, &b); long long int mm = n % m; unsigned long long ans = min((m - mm) * a, mm * b); printf( %llu n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(20); auto solve = [&]() -> string { long long int n, k, a, b; cin >> n >> k >> a >> b; long long int remG = a; long long int remB = b; string ans; vector<long long int> rem({remG, remB}); vector<long long int> lab({ G , B }); long long int cur = (remG > remB) ? 0 : 1; while (true) { if (rem[cur] == 0) { return NO ; } else if (rem[cur] < rem[1 - cur]) { ans += lab[cur]; rem[cur] -= 1; } else { long long int t = min(rem[cur], k); for (long long int i = 0; i < t; i++) ans += lab[cur]; rem[cur] -= t; } if (rem[0] == 0 && rem[1] == 0) return ans; cur = 1 - cur; } }; cout << solve() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int bit[2][100011], head[100011], tail[100011], name[100011], dist[100011], n, z, D; vector<int> adj[100011]; int query(int x, int* bit) { int r = 0; while (x) { r += bit[x]; x -= x & -x; } return r; } void update(int x, int v, int* bit) { while (x < 100011) { bit[x] += v; x += x & -x; } } void dfs(int p, int h, int d) { if (name[p]) return; D = max(D, d); name[p] = z++; head[p] = h; dist[p] = d; for (int i = 0; i < adj[p].size(); ++i) dfs(adj[p][i], h, d + 1); } int main() { int q, t, v, x, d; scanf( %d%d , &n, &q); for (int i = 0; i < n - 1; ++i) { int a, b; scanf( %d%d , &a, &b); adj[a].push_back(b); adj[b].push_back(a); } z = 2; name[1] = head[1] = 1; for (int i = 0; i < adj[1].size(); ++i) { int u = adj[1][i]; dfs(u, z, 1); tail[name[u]] = z - 1; } ++D; while (q--) { scanf( %d , &t); if (t == 0) { scanf( %d%d%d , &v, &x, &d); int g = d - dist[v]; if (g >= 0) update(max(1, D - g), x, bit[0]); if (g >= 0) { int cob = head[v] + g; if (cob <= tail[head[v]]) { update(cob, x, bit[1]); update(min(tail[head[v]] + 1, name[v] + d + 1), -x, bit[1]); } } else { update(name[v] - d, x, bit[1]); update(min(tail[head[v]] + 1, name[v] + d + 1), -x, bit[1]); } } else { scanf( %d , &v); int resa = query(D - dist[v], bit[0]); int resb = query(name[v], bit[1]); printf( %d n , resa + resb); } } }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, j, k, p, t, a = 0, b = 0; cin >> n; vector<long long int> vec; for (i = 0; i < n; i++) { cin >> p; if (p % 2 == 0) a++; else b++; vec.push_back(p); } if (a == 0 || b == 0) { for (i = 0; i < n; i++) cout << vec[i] << ; } else { sort(vec.begin(), vec.end()); for (i = 0; i < n; i++) cout << vec[i] << ; } }
#include <bits/stdc++.h> int main() { int n, p, q, p1[150], q1[150], pq[300], i, j, k, l, m; scanf( %d , &n); scanf( %d , &p); for (i = 0; i < p; i++) { scanf( %d , &p1[i]); } scanf( %d , &q); for (j = 0; j < q; j++) { scanf( %d , &q1[j]); } if ((p == 0) && (q == 0)) { printf( Oh, my keyboard! n ); return 0; } else { for (k = 0; k < (p + q); k++) { if (k < p) { pq[k] = p1[k]; } else { pq[k] = q1[k - p]; } } for (l = 1; l <= n; l++) { for (m = 0; m < p + q; m++) { if (pq[m] == l) { break; } else if (m == (p + q - 1)) { printf( Oh, my keyboard! n ); return 0; } } } printf( I become the guy. n ); } return 0; }
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-9; long double _A[19][100009], _b[19], _c[100009]; struct LPSolver { int m, n; vector<int> B, N; vector<vector<long double> > D; LPSolver(const vector<vector<long double> > &A, const vector<long double> &b, const vector<long double> &c) : m(b.size()), n(c.size()), N(n + 1), B(m), D(m + 2, vector<long double>(n + 2)) { for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) D[i][j] = A[i][j]; for (int i = 0; i < m; i++) { B[i] = n + i; D[i][n] = -1; D[i][n + 1] = b[i]; } for (int j = 0; j < n; j++) { N[j] = j; D[m][j] = -c[j]; } N[n] = -1; D[m + 1][n] = 1; } void Pivot(int r, int s) { double inv = 1.0 / D[r][s]; for (int i = 0; i < m + 2; i++) if (i != r) for (int j = 0; j < n + 2; j++) if (j != s) D[i][j] -= D[r][j] * D[i][s] * inv; for (int j = 0; j < n + 2; j++) if (j != s) D[r][j] *= inv; for (int i = 0; i < m + 2; i++) if (i != r) D[i][s] *= -inv; D[r][s] = inv; swap(B[r], N[s]); } bool Simplex(int phase) { int x = phase == 1 ? m + 1 : m; while (true) { int s = -1; for (int j = 0; j <= n; j++) { if (phase == 2 && N[j] == -1) continue; if (s == -1 || D[x][j] < D[x][s] || D[x][j] == D[x][s] && N[j] < N[s]) s = j; } if (D[x][s] > -EPS) return true; int r = -1; for (int i = 0; i < m; i++) { if (D[i][s] < EPS) continue; if (r == -1 || D[i][n + 1] / D[i][s] < D[r][n + 1] / D[r][s] || (D[i][n + 1] / D[i][s]) == (D[r][n + 1] / D[r][s]) && B[i] < B[r]) r = i; } if (r == -1) return false; Pivot(r, s); } } long double Solve(vector<long double> &x) { int r = 0; for (int i = 1; i < m; i++) if (D[i][n + 1] < D[r][n + 1]) r = i; if (D[r][n + 1] < -EPS) { Pivot(r, n); if (!Simplex(1) || D[m + 1][n + 1] < -EPS) return -numeric_limits<long double>::infinity(); for (int i = 0; i < m; i++) if (B[i] == -1) { int s = -1; for (int j = 0; j <= n; j++) if (s == -1 || D[i][j] < D[i][s] || D[i][j] == D[i][s] && N[j] < N[s]) s = j; Pivot(i, s); } } if (!Simplex(2)) return numeric_limits<long double>::infinity(); x = vector<long double>(n); for (int i = 0; i < m; i++) if (B[i] < n) x[B[i]] = D[i][n + 1]; return D[m][n + 1]; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, i; long double p, q; cin >> n >> p >> q; m = 2; for (i = 0; i < n; i++) { cin >> _A[0][i] >> _A[1][i]; _A[0][i] = -_A[0][i]; _A[1][i] = -_A[1][i]; _c[i] = -1; } _b[0] = -p; _b[1] = -q; vector<vector<long double> > A(m); vector<long double> b(_b, _b + m); vector<long double> c(_c, _c + n); for (i = 0; i < m; i++) A[i] = vector<long double>(_A[i], _A[i] + n); LPSolver solver(A, b, c); vector<long double> x; long double value = solver.Solve(x); cout << fixed << setprecision(10) << (-value) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long a[1111], b[1111]; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) cin >> b[i]; long long ans = 0; for (int i = 1; i <= n; ++i) { long long cur1 = 0, cur2 = 0; for (int j = i; j <= n; ++j) { cur1 = cur1 | a[j]; cur2 = cur2 | b[j]; ans = max(ans, cur1 + cur2); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 10; const long long M = 3e5 + 1; const long long oo = 1e9; long long n, minn, ans; long long bit[N], deg[N]; pair<long long, long long> nex[N]; vector<long long> vi[N]; void upd(long long pos, long long val) { for (long long i = pos; i < N; i += (i & -i)) bit[i] += val; } long long get(long long pos) { long long res = 0; for (long long i = pos; i >= 1; i -= (i & -i)) res += bit[i]; return res; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> minn; for (long long i = 1; i <= n; ++i) { long long x, y; cin >> x >> y; nex[i] = pair<long long, long long>(x, y); vi[x].push_back(i); vi[y].push_back(i); deg[x]++; deg[y]++; } for (long long i = 1; i <= n; ++i) upd(M - vi[i].size(), 1); for (long long i = 1; i <= n; ++i) { long long cur = vi[i].size(); for (long long v : vi[i]) { long long die = (nex[v].first == i) ? nex[v].second : nex[v].first; upd(M - deg[die], -1); deg[die]--; upd(M - deg[die], 1); } upd(M - deg[i], -1); long long rem = max(0ll, minn - cur); ans += get(M - rem); upd(M - deg[i], 1); for (long long v : vi[i]) { long long die = (nex[v].first == i) ? nex[v].second : nex[v].first; upd(M - deg[die], -1); deg[die]++; upd(M - deg[die], 1); } } cout << ans / 2; }
#include <bits/stdc++.h> using namespace std; long long n, k, c[3000001], s; set<pair<long long, long long> > d, t; set<pair<long long, long long> >::iterator p; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> c[i]; for (int i = 1; i <= k; i++) d.insert({c[i], i}); for (int i = k + 1; i <= k + n; i++) { if (i <= n) d.insert({c[i], i}); p = d.end(); p--; long long r = (*p).first; long long l = (*p).second; s += ((i - l) * r); d.erase(p); t.insert({l, i}); } cout << s << endl; for (auto e : t) cout << e.second << ; return 0; }
#include <bits/stdc++.h> using namespace std; long long sqr(long long x) { return x * x; } int n, m, x, y; int main() { cin >> x >> y >> n; long double ans = 1e9, p = 0; for (int i = 0; i < n; ++i) { int x1, y1, v; cin >> x1 >> y1 >> v; p = sqrt(sqr(x1 - x) + sqr(y1 - y)) / v; ans = min(ans, p); } cout << fixed << setprecision(10) << ans; }
#include <bits/stdc++.h> using namespace std; const int maxn = 20; long long n, m, x, y, ans, low, dp[maxn][1 << maxn]; bool f[maxn][maxn]; long long lowbit(long long x) { long long ret = 0; while (x) { if (x & 1) return ret; else ret++; x /= 2; } } int main() { scanf( %lld%lld , &n, &m); for (long long i = 1; i <= m; i++) scanf( %d%d , &x, &y), x--, y--, f[x][y] = f[y][x] = true; for (long long i = 0; i < n; i++) dp[i][1 << i] = 1; for (long long i = 1; i < (1 << n); i++) { long long minn = lowbit(i); for (long long j = 0; j < n; j++) if ((i & (1 << j)) && dp[j][i]) for (long long k = minn; k < n; k++) if (f[j][k]) { if ((!(i & (1 << k)))) dp[k][i | (1 << k)] += dp[j][i]; else if (k == minn) ans += dp[j][i]; } } printf( %lld n , (ans - m) / 2); return 0; }
#include <bits/stdc++.h> using namespace std; int q[2005], a[2005], cum[2005]; int cnt = 1, n; set<int> use; set<pair<int, int> > s; set<pair<int, int> >::iterator it; map<int, vector<int> > mp; long long diff(int dif) { return (dif * 1LL * (dif + 1)) / 2; } int main() { scanf( %d , &n); int cur = 0; for (int i = 0; i < n; i++) { int ti; scanf( %d , &ti); int ok = 1, ni = ti; while (ni > 0) { if (!(ni % 10 == 4 || ni % 10 == 7)) { ok = 0; break; } ni /= 10; } if (ok == 1) { if (cur > 0) { q[cnt] = cur; a[cnt] = 0; cnt++; cur = 0; } mp[ti].push_back(cnt); q[cnt] = 1; a[cnt] = ti; cnt++; } else cur++; } if (cur > 0) { q[cnt] = cur; a[cnt] = 0; cnt++; } cum[0] = 0; for (int i = 1; i < cnt; i++) { cum[i] = cum[i - 1] + q[i]; } long long ret = 0; for (int i = 1; i < cnt; i++) { if (q[i] >= 2) { for (int j = 1; j < q[i]; j++) { ret += j * 1LL * (j + 1) / 2 * (q[i] - j); } for (int j = 1; j < q[i]; j++) { ret += j * 1LL * (cum[cnt - 1] - cum[i]) * 1LL * (q[i] - j); ret += (q[i] - j) * 1LL * (cum[i - 1]) * 1LL * j; } } s.clear(); use.clear(); long long sum = diff(cum[i - 1]); if (i > 1) s.insert(make_pair(1, i - 1)); for (int j = i; j < cnt; j++) { if (a[j] == 0) { if (i != j) { ret += sum * 1LL * q[j] * 1LL * q[i]; } else { ret += sum * 1LL * q[i] * 1LL * (q[i] + 1) / 2; } } else { if (use.find(a[j]) == use.end()) { int len = mp[a[j]].size(); for (int k = 0; k < len; k++) { int pos = mp[a[j]][k]; if (pos >= i) break; it = s.lower_bound(make_pair(pos + 1, 0)); if (it != s.begin()) { it--; if ((*it).second >= pos) { int pa = (*it).second, pb = (*it).first; sum -= diff(cum[pa] - cum[pb - 1]); s.erase(*it); if (pa > pos) { s.insert(make_pair(pos + 1, pa)); sum += diff(cum[pa] - cum[pos]); } if (pb < pos) { s.insert(make_pair(pb, pos - 1)); sum += diff(cum[pos - 1] - cum[pb - 1]); } } } } use.insert(a[j]); } ret += sum * 1LL * q[i]; } if (i != j) { if (!s.empty()) { it = s.end(); it--; if ((*it).second == i - 1) { int pos = cum[i - 1] - cum[(*it).first - 1]; ret += q[j] * 1LL * pos * 1LL * q[i] * 1LL * (q[i] - 1) / 2; } } } } } printf( %I64d n , ret); }
#include <bits/stdc++.h> using namespace std; const int maxN = 101000 * 8; const int G = 3; const int Mod = 998244353; int n; long long m; int Fc[maxN], Ifc[maxN]; int In[maxN], A[maxN], B[maxN], C[maxN], R[maxN]; int QPow(int x, int cnt); void NTT(int *P, int N, int opt); int main() { Fc[0] = Ifc[0] = 1; for (int i = 1; i < maxN; i++) Fc[i] = 1ll * Fc[i - 1] * i % Mod; Ifc[maxN - 1] = QPow(Fc[maxN - 1], Mod - 2); for (int i = maxN - 2; i >= 1; i--) Ifc[i] = 1ll * Ifc[i + 1] * (i + 1) % Mod; scanf( %d%lld , &n, &m); int N = 1; while (N <= n + n) N <<= 1; m = m % (Mod - 1); for (int i = 0; i <= n; i++) scanf( %d , &In[i]), B[i] = 1ll * Fc[i] * In[i] % Mod; for (int i = 0; i <= n; i++) C[i] = Ifc[n - i]; NTT(B, N, 1); NTT(C, N, 1); for (int i = 0; i < N; i++) B[i] = 1ll * B[i] * C[i] % Mod; NTT(B, N, -1); for (int i = 0; i <= n; i++) A[i] = 1ll * B[i + n] * Ifc[i] % Mod; for (int i = 0; i <= n; i++) A[i] = 1ll * A[i] * QPow(QPow(i + 1, Mod - 2), m) % Mod * Fc[i] % Mod; memset(C, 0, sizeof(C)); for (int i = 0; i <= n; i++) C[i] = (i & 1) ? Mod - Ifc[i] : Ifc[i]; reverse(&C[0], &C[n + 1]); NTT(A, N, 1); NTT(C, N, 1); for (int i = 0; i < N; i++) A[i] = 1ll * A[i] * C[i] % Mod; NTT(A, N, -1); for (int i = 0; i <= n; i++) printf( %I64d , 1ll * A[i + n] * Ifc[i] % Mod); printf( n ); return 0; } int QPow(int x, int cnt) { int ret = 1; while (cnt) { if (cnt & 1) ret = 1ll * ret * x % Mod; cnt >>= 1; x = 1ll * x * x % Mod; } return ret; } void NTT(int *P, int N, int opt) { int l = -1, _n = N; while (_n) ++l, _n >>= 1; for (int i = 0; i < N; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (l - 1)); for (int i = 0; i < N; i++) if (i < R[i]) swap(P[i], P[R[i]]); for (int i = 1; i < N; i <<= 1) { int dw = QPow(G, (Mod - 1) / (i << 1)); if (opt == -1) dw = QPow(dw, Mod - 2); for (int j = 0; j < N; j += (i << 1)) for (int k = 0, w = 1; k < i; k++, w = 1ll * w * dw % Mod) { int X = P[j + k], Y = 1ll * P[j + k + i] * w % Mod; P[j + k] = (X + Y) % Mod; P[j + k + i] = (X - Y + Mod) % Mod; } } if (opt == -1) { int inv = QPow(N, Mod - 2); for (int i = 0; i < N; i++) P[i] = 1ll * P[i] * inv % Mod; } return; }
#include <bits/stdc++.h> using namespace std; long long mi = 2e9; long long ma = -2 * 1e9; const int N = 1e5 + 5; int main() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long na, nb, ka, kb; cin >> na >> nb >> ka >> kb; long long a[na], b[nb]; for (int i = 0; i < na; ++i) cin >> a[i]; for (int i = 0; i < nb; ++i) cin >> b[i]; sort(a, a + na); sort(b, b + nb); long long x = nb - kb; if (a[ka - 1] < b[nb - 1] && b[x] > a[ka - 1]) cout << YES ; else cout << NO ; }
#include <bits/stdc++.h> const long long Inf = 1ll << 50; const long long N = 200010; using namespace std; long long cl[2 * N], b, c, d, i, j, k, l, n, ans, a[N], y, kk; int main() { scanf( %I64d%I64d , &n, &k); for (i = n + 1; i > 0; i--) scanf( %I64d , &a[i]); memset(cl, 0, sizeof(cl)); cl[0] = a[0] = 0; for (i = 1; i <= n + 1; i++) { cl[i] = (cl[i - 1] + a[i - 1]) << 1ll; if ((cl[i] > Inf) || (cl[i] < -Inf)) break; } y = 0; ans = 0; i--; for (j = n + 1; j > i; j--) { if ((y & 1) == 0) y >>= 1ll; else break; y += a[j]; } if (i == j) { for (kk = i; kk > 0; kk--) { if ((y % 2) == 0) y /= 2ll; else break; if (cl[kk] + y - y == cl[kk] && abs(cl[kk] + y) <= k && (!(kk == 1 && (cl[kk] + y == 0)))) ans++; y += a[kk]; } } printf( %I64d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long arr[300000]; long long temp[300000]; int main() { int q; cin >> q; while (q--) { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; temp[i] = arr[i]; } int st = 0; int endIdx1 = -1; for (int i = 0; i < n; i++) { if (arr[i] >= st) { st++; endIdx1 = i; } else { break; } } if (endIdx1 == n - 1) { cout << Yes << endl; continue; } st = 0; int endIdx = n; for (int i = n - 1; i >= 0; i--) { if (arr[i] >= st) { st++; endIdx = i; } else { break; } } if (endIdx == 0) { cout << Yes << endl; continue; } if (endIdx1 >= endIdx) { cout << Yes << endl; } else { cout << No << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; long long k = 0, m = 1; while (t > 0) { if (t % 10 == 4) { k = k + m; } else { k = k + 2 * m; } m = m * 2; t = t / 10; } cout << k; }
#include <bits/stdc++.h> using namespace std; int marked[1000006]; int f(int i, int j) { if (j == 1) { return marked[i]; } return f(marked[i], j - 1); } vector<int> v; int main() { int n, a, b; scanf( %d %d %d , &n, &a, &b); int cut = -1; for (int i = 0; i <= n; i++) { if (i % a == 0 && (n - i) % b == 0) { cut = i; break; } } if (cut == -1) { printf( -1 n ); return 0; } int cnt = 0; for (int i = 1; i <= cut; i++) { cnt++; v.push_back(i); if (cnt == a) { if ((int)v.size() >= 2) { rotate(v.begin(), v.begin() + 1, v.end()); } for (int j = 0; j < v.size(); j++) { printf( %d , v[j]); } cnt = 0; v.clear(); } } cnt = 0; for (int i = cut + 1; i <= n; i++) { cnt++; v.push_back(i); if (cnt == b) { if ((int)v.size() >= 2) { rotate(v.begin(), v.begin() + 1, v.end()); } for (int j = 0; j < v.size(); j++) { printf( %d , v[j]); } cnt = 0; v.clear(); } } }
#include <bits/stdc++.h> using namespace std; void vin(vector<long long int> &v, long long int t) { for (long long int i = 0; i < t; i++) cin >> v[i]; } void vout(vector<long long int> &v, long long int t) { for (long long int i = 0; i < t; i++) cout << v[i] << ; cout << endl; } void ingraph(vector<vector<long long int>> &graph, long long int m) { long long int x, y; for (long long int i = 0; i < m; i++) { cin >> x >> y; x--, y--; graph[x].push_back(y); graph[y].push_back(x); } } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return a * b / gcd(a, b); } void solve() { long long int n, m; cin >> n >> m; vector<vector<long long int>> a(n, vector<long long int>(m)), dp(n, vector<long long int>(m)); for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < m; j++) cin >> a[i][j]; } long long int ans = (long long int)1e18; for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < m; j++) { long long int b = a[i][j] - i - j; if (a[0][0] < b) continue; vector<vector<long long int>> dp( n, vector<long long int>(m, (long long int)1e18)); dp[0][0] = a[0][0] - b; for (long long int p = 0; p < n; p++) { for (long long int q = 0; q < m; q++) { if (a[p][q] < (b + p + q)) continue; if (p > 0) dp[p][q] = min(dp[p][q], dp[p - 1][q] + a[p][q] - (b + q + p)); if (q > 0) dp[p][q] = min(dp[p][q], dp[p][q - 1] + a[p][q] - (b + q + p)); } } ans = min(ans, dp[n - 1][m - 1]); } } cout << ans << n ; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; struct SCC { int N; vector<bool> used; vector<int> vs; vector<int> cmp; void dfs(int v, const vector<int> G[]) { used[v] = true; for (auto u : G[v]) { if (!used[u]) dfs(u, G); } vs.push_back(v); } void rdfs(int v, int k, const vector<int> rG[]) { used[v] = true; cmp[v] = k; for (auto u : rG[v]) { if (!used[u]) rdfs(u, k, rG); } } void calculate(int Nin, const vector<int> G[], const vector<int> rG[]) { N = Nin; used.resize(N); fill(used.begin(), used.end(), false); for (int v = 0; v < N; v++) { if (!used[v]) dfs(v, G); } fill(used.begin(), used.end(), false); cmp.resize(N); int k = 0; for (int i = vs.size() - 1; i >= 0; i--) { if (!used[vs[i]]) rdfs(vs[i], k++, rG); } } int dump_graph(const vector<int> G[], vector<int> edges2[]) { for (int i = 0; i < N; i++) { for (auto j : G[i]) { if (cmp[i] != cmp[j]) { edges2[cmp[i]].push_back(cmp[j]); } } } return *max_element(cmp.begin(), cmp.end()) + 1; } }; vector<int> edges[5000000], edges2[5000000]; int main() { int N, M, D; cin >> N >> M >> D; for (int i = 0; i < M; i++) { int a, b; scanf( %d %d , &a, &b); a--; b--; for (int d = 0; d < D; d++) { int s = a * D + d, t = b * D + (d + 1) % D; edges[s].push_back(t); edges2[t].push_back(s); } } SCC scc; scc.calculate(D * N, edges, edges2); for (int i = 0; i < N * D; i++) edges2[i].clear(); int N2 = scc.dump_graph(edges, edges2); vector<int> num(N * D); char s[50]; for (int i = 0; i < N; i++) { scanf( %s , s); set<int> st; for (int d = 0; d < D; d++) { if (s[d] == 0 ) continue; int c = scc.cmp[i * D + d]; if (!st.count(c)) { num[c]++; st.insert(c); } } } vector<int> indeg(N2); for (int i = 0; i < N2; i++) for (int j : edges2[i]) indeg[j]++; vector<int> dp(N2); queue<int> que; for (int i = 0; i < N2; i++) { dp[i] = -1e9; if (indeg[i] == 0) que.push(i); } int st = scc.cmp[0]; dp[st] = num[st]; while (que.size()) { int i = que.front(); que.pop(); for (int j : edges2[i]) { dp[j] = max(dp[j], dp[i] + num[j]); indeg[j]--; if (indeg[j] == 0) que.push(j); } } int ans = *max_element(dp.begin(), dp.end()); cout << ans << endl; return 0; }
#include <bits/stdc++.h> int main() { int n(0); scanf( %d n , &n); std::string number; getline(std::cin, number); if (number.size() <= 3) { std::cout << number << std::endl; } else { int index(0); if (number.size() % 2) { std::cout << number.substr(0, 3); index = 3; } else { std::cout << number.substr(0, 2); index = 2; } while (index < number.size()) { std::cout << - ; std::cout << number.substr(index, 2); index += 2; } std::cout << std::endl; } return 0; }
#include <bits/stdc++.h> using namespace std; struct E { int ad, id, ne; } e[100010 * 4]; int n, m, q, u[100010], v[100010], w[100010], h1[100010], h2[100010], de[100010], fa[100010], f[100010], p0[100010], p1[100010]; bool o[100010]; void add(int* he, int x, int y, int d) { static int t = 0; ++t, e[t].ne = he[x], he[x] = t, e[t].ad = y, e[t].id = d; } int ff(int* f, int x) { if (f[x] == x) return x; else return f[x] = ff(f, f[x]); } void ff1(int x) { p0[x] = p1[x] = x; for (int p = h1[x]; p; p = e[p].ne) { int y = e[p].ad; if (!de[y]) { fa[y] = x, de[y] = de[x] + 1, ff1(y), p1[y] = x; if (ff(p0, x) == ff(p0, y)) o[x] |= o[y]; } else if (de[y] + 1 < de[x]) { int z = ff(p0, x); while (de[z] > de[y] + 1) p0[z] = fa[z], z = ff(p0, z); if ((de[x] - de[y]) % 2 == 0) o[x] = 1; } } for (int p = h2[x]; p; p = e[p].ne) if (de[e[p].ad]) w[e[p].id] = ff(p1, e[p].ad); } void ff2(int x) { if (o[ff(p0, x)]) f[x]++; for (int i = h1[x]; i; i = e[i].ne) { int y = e[i].ad; if (de[y] == de[x] + 1) { if (ff(p0, x) == ff(p0, y)) o[y] |= o[x]; f[y] = f[x], ff2(y); } } } bool chk(int u, int v, int w) { if (ff(p1, u) != ff(p1, v)) return 0; if ((de[u] - de[v]) % 2) return 1; return f[u] + f[v] - f[w] * 2 > 0; } int main() { scanf( %d%d , &n, &m); for (int i = 1, x, y; i <= m; i++) scanf( %d%d , &x, &y), add(h1, x, y, i), add(h1, y, x, i); scanf( %d , &q); for (int i = 1; i <= q; i++) scanf( %d%d , u + i, v + i), add(h2, u[i], v[i], i), add(h2, v[i], u[i], i); for (int i = 1; i <= n; i++) if (!de[i]) de[i] = 1, ff1(i), ff2(i); for (int i = 1; i <= q; i++) puts(chk(u[i], v[i], w[i]) ? Yes : No ); }
#include <bits/stdc++.h> #pragma GCC optimize( -O3 , unroll-loops ) using namespace std; template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; } const long long MOD = 1e9 + 7; const long long MAX = 200010; const double pi = acos(-1); vector<long long> G[200010]; long long dp[200020]; long long dp_init(long long n, long long pre) { if (dp[n] != 0) return dp[n]; long long res = 1; for (auto nex : G[n]) { if (nex == pre) continue; res += dp_init(nex, n); } dp[n] = res; return res; } long long ans = 0; long long N; bool used[200020]; void calc(long long n, long long res) { used[n] = true; for (auto nex : G[n]) { if (used[nex]) continue; long long tmp = res + N - 2 * dp[nex]; chmax(ans, tmp); used[nex] = true; calc(nex, tmp); } } void solve() { cin >> N; for (long long i = 0; i < N - 1; i++) { long long u, v; cin >> u >> v; u--, v--; G[u].push_back(v); G[v].push_back(u); } long long root = 0; dp_init(0, 0); long long tmp = 0; for (long long i = 0; i < N; i++) tmp += dp[i]; ans = tmp; calc(0, tmp); cout << ans << n ; ; } signed main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }
#include <bits/stdc++.h> using namespace std; int xd[] = {0, 1, 0, -1}; int yd[] = {1, 0, -1, 0}; int xx[] = {0, 1, 0, -1, 1, -1, 1, -1}; int yy[] = {1, 0, -1, 0, 1, 1, -1, -1}; int kx[] = {-2, -1, -2, -1, 1, 2, 2, 1}; int ky[] = {1, 2, -1, -2, -2, -1, 1, 2}; bool islucky(int x) { int four = 0, seven = 0; while (x) { if (x % 10 != 7 && x % 10 != 4) return false; if (x % 10 == 7) seven++; else four++; x = x / 10; } return four == seven; } bool is_prime(long long x) { if (x == 1) return false; for (int i = 2; i * i <= x; i++) if (x % i == 0) return false; return true; } long long C(int k, int l) { if (l == k) return 1; if (l * 2 > k) l = k - l; long long res = 1, temp = l; for (int i = k - l + 1; i <= k; i++) { res *= i / temp; temp--; } return res; } long long sumOfDigit(int x) { int res = 0; while (x) { res += x % 10; x /= 10; } return res; } uint64_t bigPow(uint64_t base, uint64_t p) { uint64_t temp = 1; while (p--) { temp *= base; } return temp; } vector<int> soso; bool prime[10000001] = {0}; void SieveOfEratosthenes(int n) { soso.push_back(2); for (int p = 3; p * p <= n; p += 2) { if (prime[p] == false) { for (int i = p * 2; i <= n; i += p) prime[i] = true; } } for (int i = 3; i <= n; i += 2) if (!prime[i]) soso.push_back(i); } vector<vector<int> > AdjList; bool visited[100005]; void dfs(int s) { if (visited[s]) return; visited[s] = 1; for (int i = 0; i < AdjList[s].size(); i++) dfs(AdjList[s][i]); return; } long long gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { char grid[8][8]; for (int i = 0; i < 8; i++) cin >> grid[i]; int w = 0, b = 0; for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) { if (grid[i][j] != . ) { char c = grid[i][j]; if (c == q ) b += 9; if (c == Q ) w += 9; if (c == r ) b += 5; if (c == R ) w += 5; if (c == b || c == n ) b += 3; if (c == B || c == N ) w += 3; if (c == p ) b += 1; if (c == P ) w += 1; } } if (b == w) cout << Draw ; else if (b > w) cout << Black ; else cout << White ; }
#include <bits/stdc++.h> const long long N = 100100; const long long MAX = 1000000100; using namespace std; long long a[N] = {0}; int main() { long long n; while (scanf( %lld , &n) != EOF) { map<long long, long long> num; for (long long i = 0; i < n; i++) { scanf( %lld , &a[i]); num[a[i]]++; } sort(a, a + n); long long min1 = MAX + 1, min2 = MAX + 2, min3 = MAX + 3; for (long long i = 0; i < n; i++) { if (a[i] < min1) { min1 = a[i]; } else if (a[i] > min1 && a[i] < min2) { min2 = a[i]; } else if (a[i] > min2 && a[i] < min3) { min3 = a[i]; break; } } if (num[min1] >= 3) { long long sum = 1; for (long long i = 3; i >= 1; i--) sum *= num[min1]--; cout << sum / 6 << endl; } else if (num[min1] == 2) { cout << num[min2] << endl; } else { if (num[min2] >= 2) { long long sum = 1; for (long long i = 2; i >= 1; i--) sum *= num[min2]--; cout << sum / 2 << endl; } else { cout << num[min3] << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int m = 0, b = 0; int main() { int n; cin >> n; int Arrpi[n]; for (int j = 0; j < n; ++j) { cin >> Arrpi[j]; } int A[n]; for (int l = 0; l < n; ++l) { m = Arrpi[l] - 1; b = l + 1; A[m] = b; } for (int y = 0; y < n; ++y) { cout << A[y] << ; } return 0; }
#include <bits/stdc++.h> using namespace std; char getc() { char c = getchar(); while ((c < A || c > Z ) && (c < a || c > z ) && (c < 0 || c > 9 )) c = getchar(); return c; } int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); } int read() { int x = 0, f = 1; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) f = -1; c = getchar(); } while (c >= 0 && c <= 9 ) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n, k, a[5010], f[5010][5010], ans; int ksm(int a, int k) { int s = 1; for (; k; k >>= 1, a = 1ll * a * a % 1000000007) if (k & 1) s = 1ll * s * a % 1000000007; return s; } int inv(int a) { return ksm(a, 1000000007 - 2); } void inc(int &x, int y) { x += y; if (x >= 1000000007) x -= 1000000007; } signed main() { const char LL[] = %lld n ; n = read(), k = read(); for (int i = 1; i <= n; i++) a[i] = read(); f[0][0] = 1; for (int i = 1; i <= n; i++) { f[i][0] = 1ll * f[i - 1][0] * a[i] % 1000000007; for (int j = 1; j <= i; j++) f[i][j] = (1ll * f[i - 1][j] * a[i] - f[i - 1][j - 1] + 1000000007) % 1000000007; } int s = 1; for (int i = 0; i <= min(k, n); i++) { inc(ans, 1ll * ksm(n, k - i) * f[n][i] % 1000000007 * s % 1000000007); s = 1ll * s * (k - i) % 1000000007; } ans = 1ll * ans * inv(ksm(n, k)) % 1000000007; ans = (f[n][0] - ans + 1000000007) % 1000000007; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline T read() { register T sum = 0; register char cc = getchar(); int sym = 1; while (cc != - && (cc > 9 || cc < 0 )) cc = getchar(); if (cc == - ) sym = -1, cc = getchar(); sum = sum * 10 + cc - 0 ; cc = getchar(); while (cc >= 0 && cc <= 9 ) sum = sum * 10 + cc - 0 , cc = getchar(); return sym * sum; } template <typename T> inline T read(T& a) { a = read<T>(); return a; } template <typename T, typename... Others> inline void read(T& a, Others&... b) { a = read(a); read(b...); } int n, ans = 0x7fffffff, b[200010], prim[1000010]; long long a[200010]; set<long long> s; void init() { for (int i = 2; i <= 1000000; i++) { if (!prim[i]) prim[++prim[0]] = i; for (int j = 1; j <= prim[0] && i * prim[j] <= 1000000; j++) { prim[i * prim[j]] = true; if (i % prim[j]) break; } } } void div(long long x) { for (int i = 1; i <= prim[0]; i++) { if (x % prim[i]) continue; s.insert(prim[i]); while (x % prim[i] == 0) x /= prim[i]; } if (x != 1) s.insert(x); } int solve(long long p) { long long res = 0; for (int i = 1; i <= n; i++) { long long d = a[i] < p ? p - a[i] : min(a[i] % p, p - a[i] % p); res = min(1ll * n, res + d); } return res; } int main() { srand(time(NULL)); init(); read(n); for (int i = 1; i <= n; i++) { read(a[i]); b[i] = i; } random_shuffle(b + 1, b + 1 + n); for (int i = 1; i <= 100 && i <= n; i++) { long long t = a[b[i]]; div(t); div(t + 1); if (t > 1) div(t - 1); } for (auto i : s) ans = min(ans, solve(i)); printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, c, q, i; cin >> q; for (i = 1; i <= q; i++) { cin >> n; c = 0; while (1) { if (n % 2 == 0) { n = n / 2; c++; } else if (n % 3 == 0) { n = (2 * n) / 3; c++; } else if (n % 5 == 0) { n = (4 * n) / 5; c++; } else break; } if (n == 1) cout << c << endl; else cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1505; const int mo = 998244353; int n, m, a[N][N]; int fa[N], sz[N], S[N]; int f[N][N], tmp[N]; pair<int, int> e[N * N]; int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); } void merge(int x, int y) { memset(tmp, 0, sizeof(tmp)); for (int i = (int)(1); i <= (int)(sz[x]); i++) for (int j = (int)(1); j <= (int)(sz[y]); j++) tmp[i + j] = (tmp[i + j] + 1ll * f[x][i] * f[y][j]) % mo; memcpy(f[x], tmp, sizeof(f[x])); sz[x] += sz[y]; S[x] += S[y]; fa[y] = x; } bool cmp(pair<int, int> x, pair<int, int> y) { return a[x.first][x.second] < a[y.first][y.second]; } int main() { scanf( %d , &n); for (int i = (int)(1); i <= (int)(n); i++) for (int j = (int)(1); j <= (int)(n); j++) scanf( %d , &a[i][j]); for (int i = (int)(1); i <= (int)(n); i++) for (int j = (int)(i + 1); j <= (int)(n); j++) e[++m] = pair<int, int>(i, j); sort(e + 1, e + m + 1, cmp); for (int i = (int)(1); i <= (int)(n); i++) fa[i] = i, sz[i] = 1, S[i] = 0, f[i][1] = 1; for (int i = (int)(1); i <= (int)(m); i++) { int x = get(e[i].first); int y = get(e[i].second); if (x != y) merge(x, y); ++S[x]; if (S[x] == sz[x] * (sz[x] - 1) / 2) f[x][1] = (f[x][1] + 1) % mo; } for (int i = (int)(1); i <= (int)(n); i++) cout << f[get(1)][i] << ; }
#include <bits/stdc++.h> using namespace std; string to_string(string s) { return + s + ; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? true : false ); } template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename A> string to_string(A v) { bool first = true; string res = { ; for (const auto &x : v) { if (!first) { res += , ; } first = false; res += to_string(x); } res += } ; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << << to_string(H); debug_out(T...); } class segtree { public: struct node { long long mn = 0; long long add = 0; void apply(int l, int r, long long v) { mn += v; add += v; } }; node unite(const node &a, const node &b) const { node res; res.mn = min(a.mn, b.mn); return res; } inline void push(int x, int l, int r) { int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); if (tree[x].add != 0) { tree[x + 1].apply(l, y, tree[x].add); tree[z].apply(y + 1, r, tree[x].add); tree[x].add = 0; } } inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); } int n; vector<node> tree; void build(int x, int l, int r) { if (l == r) { return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); build(x + 1, l, y); build(z, y + 1, r); pull(x, z); } template <typename M> void build(int x, int l, int r, const vector<M> &v) { if (l == r) { tree[x].apply(l, r, v[l]); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); build(x + 1, l, y, v); build(z, y + 1, r, v); pull(x, z); } node get(int x, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) { return tree[x]; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); push(x, l, r); node res{}; if (rr <= y) { res = get(x + 1, l, y, ll, rr); } else { if (ll > y) { res = get(z, y + 1, r, ll, rr); } else { res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); } } pull(x, z); return res; } template <typename... M> void modify(int x, int l, int r, int ll, int rr, const M &...v) { if (ll <= l && r <= rr) { tree[x].apply(l, r, v...); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); push(x, l, r); if (ll <= y) { modify(x + 1, l, y, ll, rr, v...); } if (rr > y) { modify(z, y + 1, r, ll, rr, v...); } pull(x, z); } int find_first_knowingly(int x, int l, int r, const function<bool(const node &)> &f) { if (l == r) { return l; } push(x, l, r); int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); int res; if (f(tree[x + 1])) { res = find_first_knowingly(x + 1, l, y, f); } else { res = find_first_knowingly(z, y + 1, r, f); } pull(x, z); return res; } int find_first(int x, int l, int r, int ll, int rr, const function<bool(const node &)> &f) { if (ll <= l && r <= rr) { if (!f(tree[x])) { return -1; } return find_first_knowingly(x, l, r, f); } push(x, l, r); int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); int res = -1; if (ll <= y) { res = find_first(x + 1, l, y, ll, rr, f); } if (rr > y && res == -1) { res = find_first(z, y + 1, r, ll, rr, f); } pull(x, z); return res; } int find_last_knowingly(int x, int l, int r, const function<bool(const node &)> &f) { if (l == r) { return l; } push(x, l, r); int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); int res; if (f(tree[z])) { res = find_last_knowingly(z, y + 1, r, f); } else { res = find_last_knowingly(x + 1, l, y, f); } pull(x, z); return res; } int find_last(int x, int l, int r, int ll, int rr, const function<bool(const node &)> &f) { if (ll <= l && r <= rr) { if (!f(tree[x])) { return -1; } return find_last_knowingly(x, l, r, f); } push(x, l, r); int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); int res = -1; if (rr > y) { res = find_last(z, y + 1, r, ll, rr, f); } if (ll <= y && res == -1) { res = find_last(x + 1, l, y, ll, rr, f); } pull(x, z); return res; } segtree(int _n) : n(_n) { assert(n > 0); tree.resize(2 * n - 1); build(0, 0, n - 1); } template <typename M> segtree(const vector<M> &v) { n = v.size(); assert(n > 0); tree.resize(2 * n - 1); build(0, 0, n - 1, v); } node get(int ll, int rr) { assert(0 <= ll && ll <= rr && rr <= n - 1); return get(0, 0, n - 1, ll, rr); } node get(int p) { assert(0 <= p && p <= n - 1); return get(0, 0, n - 1, p, p); } template <typename... M> void modify(int ll, int rr, const M &...v) { assert(0 <= ll && ll <= rr && rr <= n - 1); modify(0, 0, n - 1, ll, rr, v...); } int find_first(int ll, int rr, const function<bool(const node &)> &f) { assert(0 <= ll && ll <= rr && rr <= n - 1); return find_first(0, 0, n - 1, ll, rr, f); } int find_last(int ll, int rr, const function<bool(const node &)> &f) { assert(0 <= ll && ll <= rr && rr <= n - 1); return find_last(0, 0, n - 1, ll, rr, f); } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, tt; cin >> n >> tt; vector<int> p(n); vector<int> w(n); for (int i = 1; i < n; i++) { cin >> p[i] >> w[i]; p[i]--; } vector<vector<int>> qs(n); vector<int> L(tt), R(tt), v(tt); vector<long long> res(tt, -1); for (int i = 0; i < tt; i++) { cin >> v[i] >> L[i] >> R[i]; v[i]--; L[i]--; R[i]--; qs[v[i]].push_back(i); } vector<long long> dst(n); for (int i = 1; i < n; i++) { dst[i] = dst[p[i]] + w[i]; } vector<int> is_leaf(n, 1); for (int i = 1; i < n; i++) { is_leaf[p[i]] = 0; } const long long inf = (long long)1e18; for (int i = 0; i < n; i++) { if (!is_leaf[i]) { dst[i] = inf; } } segtree st(dst); vector<int> stk; vector<int> to(n, n - 1); for (int i = 0; i < n; i++) { while (!stk.empty() && stk.back() != p[i]) { int u = stk.back(); to[u] = i - 1; stk.pop_back(); } stk.push_back(i); } 42; stk.clear(); for (int i = 0; i < n; i++) { while (!stk.empty() && stk.back() != p[i]) { int u = stk.back(); st.modify(0, n - 1, -w[u]); st.modify(u, to[u], 2 * w[u]); stk.pop_back(); } stk.push_back(i); st.modify(0, n - 1, w[i]); st.modify(i, to[i], -2 * w[i]); for (int id : qs[i]) { res[id] = st.get(L[id], R[id]).mn; } } for (int i = 0; i < tt; i++) { cout << res[i] << n ; } return 0; }