solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; inline void PrintArrInt(int arr[], int s, int e) { for (int i = (s); i <= (e); i++) printf("%d%c", arr[i], i == e ? '\n' : ' '); } const int maxn = 100000 + 5; const int mod = 1e9 + 7; int N, Q, mxsqn, arr[maxn], lpos[maxn * 2]; struct Node { int ls, rs; long long mul...
19
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int n, val[maxn], root; int tot, head[maxn]; struct Edge { int to, next; } edges[maxn]; void initedge() { tot = 0; for (int i = 0; i <= n; i++) { head[i] = -1; } } void addedge(int from, int to) { edges[tot].to = to; edges[tot].nex...
12
#include <bits/stdc++.h> using namespace std; const int Maxn = 130; int n, maxx, a[Maxn], you, she; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; she += a[i]; maxx = max(a[i], maxx); } for (int i = maxx; i < 250; i++) { you = i * n - she; if (she < you) { cout << i;...
0
#include <bits/stdc++.h> using namespace std; #pragma warning(disable : 4996) const int INF = 0x7fffffff; const int MAX_N = 1e5 + 5; int a[MAX_N], q[MAX_N], ask[MAX_N]; const long long mod = 1e9 + 7; const int K = 20; long long _2[MAX_N]; struct datastruct { int a[K], sz; bool insert(int x) { for (int i = K - 1...
16
#include <bits/stdc++.h> using namespace std; int main() { int k, i, j; cin >> k; if (k % 2 == 0) { cout << "NO" << endl; return 0; } else if (k == 1) { cout << "YES" << endl << "2 1" << endl << "1 2" << endl; } else { cout << "YES" << endl; int nodes = 2 * k + 4, edges = (k + 2) * k; ...
11
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; struct Edge { int to, next; } edge[maxn * 2]; int cnt, head[maxn]; int pre[maxn][50], fgcd[100], f[maxn], tot, res[maxn]; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } void init() { tot = 0; cnt = 0; ...
12
#include <bits/stdc++.h> const int K = 1111; int cine[K][K], n, k; int main() { scanf("%d%d", &n, &k); int mid = k / 2; while (n--) { int m; scanf("%d", &m); int minS = 1 << 30, x, l, r; for (int i = 0; i < k; ++i) { int s = 0, cnt = 0; for (int j = 0; j < k; ++j) if (cine[i][j...
7
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") template <class S, class T> ostream& operator<<(ostream& os, const pair<S, T>& p) { return os << "(" << p.first << ", " << p.se...
17
#include <bits/stdc++.h> using namespace std; const int _ = 3e5 + 7; int s[_], t[_], num[_], d[_], N, M; vector<int> nxt[_]; void topo() { int lft = N; queue<int> q; for (int i = 1; i <= N; ++i) if (!d[i]) q.push(i); while (!q.empty()) { int t = q.front(); q.pop(); --lft; if (q.size() >= 2) ...
21
#include <bits/stdc++.h> #define oo 1000000000000 #define ll long long #define pii pair<long long, long long> #define ull unsigned long long #define st first #define nd second #define file "test" using namespace std; const long long N = 2e5 + 5; ll pow2[N], pow3[N]; ll num(ll x) { ll cnt = 0; while (x) cnt ++, x/=...
3
#include <bits/stdc++.h> using namespace std; int n, k; vector<int> bad; set<pair<int, pair<int, int> > > segm; int points[256]; vector<int> ans; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { int l, r; cin >> l >> r; segm.insert({i, {l, r}}); for (int j = l; j <= r; j++) points[j]++; ...
10
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:5000000000") const long long m = 1000000007; const int INF = 1000000000; using namespace std; int main() { long long n, k; cin >> n >> k; long long ans1, ans2, ans3, ans4; ans1 = 1; ans2 = ans1 + (n * (n - 1)) / 2; ans3 = 2 * ((n * (n - 1) * (n - 2)) ...
8
#include <bits/stdc++.h> using namespace std; int main() { int n, T; scanf("%d %d", &n, &T); priority_queue<int> q; int ans = 0; for (int i = 1; i <= min(T - 1, n); i++) { while (!q.empty() && q.top() >= T - i) q.pop(); int x; scanf("%d", &x); if (x < T) q.push(x - i); ans = max(ans, (int)...
14
#include <bits/stdc++.h> using namespace std; bool cmp(const pair<long long, long long>& a, const pair<long long, long long>& b) { return a.first > b.first; } bool cmp1(const pair<long long, long long>& a, const pair<long long, long long>& b) { return a.second < b.second; } int main() { ios_bas...
4
#include <bits/stdc++.h> using namespace std; const int N = 1000007; int v[N], f[N]; vector<int> pos[N]; long long acc[N]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> v[i]; pos[v[i]].push_back(i); } for (int i = 0; i < N; ++i) { reverse(pos[i].begin(), pos[i].end()); } s...
10
#include <bits/stdc++.h> int main() { int n, num; int flag = 0, sum = 0; int tot; char str[20], kong; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &num); kong = getchar(); scanf("%s", str); if (str[0] == 'S') { if (sum == 20000) flag = 1; tot = 20000 - sum; i...
5
#include <bits/stdc++.h> using namespace std; short n, m; int nn; char a[3][23]; bool b[405][405], seen[405]; void dfs(int i) { if (seen[i]) return; seen[i] = 1; for (int j = 1; j <= nn; j++) if (b[i][j]) dfs(j); } int main() { cin >> n >> m; nn = n * m; for (short i = 0; i < 2; i++) scanf("%s", a[i]); ...
6
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 50; int arr[N]; int main() { int n, m, d; cin >> n >> m >> d; set<pair<int, int> > st; for (int i = 0; i < n; ++i) { cin >> arr[i]; st.insert(make_pair(arr[i], i)); } int day = 0; while (!st.empty()) { day++; int a = st.begi...
8
#include <bits/stdc++.h> using namespace std; struct node { int x, y, val, ans; bool operator<(const node &a) const { return val < a.val; } } p[1000010]; int r[1000010], l[1000010], rr[1000010], ll[1000010]; int n, m; int fa[1000010], A[1000010]; int cmp(node a, node b) { return a.x * m + a.y < b.x * m + b.y; } int...
14
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; long long ii = 1; void solve() { long long n; long long kk; cin >> n >> kk; vector<long long> alice; vector<long long> bob; vector<long long> all; long long al = 0; long long bo = 0; for (long long i = 0; i < n; i++) { long lon...
8
#include <bits/stdc++.h> using namespace std; const long long MIN = numeric_limits<long long>::min(); class WindowHeap { public: WindowHeap() {} long long top() { if (_i >= _h.size()) return MIN; return _h[_i].first; } void erase(int idx) { if (!_h.empty() && _h[_i].second == idx) ++_i; } void ...
11
#include <bits/stdc++.h> using namespace std; const double mod = 1e9 + 7; long long msum(long long a, long long b, long long m) { return (a % m + b % m) % m; } long long msub(long long a, long long b, long long m) { return (a % m - b % m) % m; } long long mpro(long long a, long long b, long long m) { return ((a %...
7
#include <bits/stdc++.h> using namespace std; const long long MAX = 2e5 + 5; const long long MOD = 1e9 + 7; long long testnum = 0; long long testflag = 0; void solve() { long long a, b, c, d; cin >> a >> b >> c >> d; bool aa = 0, bb = 0, cc = 0, dd = 0; if (b == 0 && c == 0) { if (a == 0 && d > 0) dd = 1; ...
5
#include <bits/stdc++.h> using namespace std; const int sz = 1e2 + 4; const int oo = 1e9 + 4; long long n, m; void solve() { cin >> n >> m; cout << (m + n - 1) / n; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } vector<long long> b_v; void bin(unsigned long long n) { if (n > 1) bin(n >> 1); b_v.emplace_back(n & 1); } long long isPowerof2(long long x) { return (x && !(x & x - 1)); } double ...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long t; cin >> t; while (t--) { long long n, k, a, b; cin >> n >> k; vector<set<long long> > g(n + 1), l(n + 1); for (int i = 1; i <= n - 1; i++) { cin >> a >> b; g[a].insert(b...
15
#include <bits/stdc++.h> using namespace std; char a[200005], b[200005]; int chk(char* a1, char* b1, int l) { bool flag = 0; for (int i = 0; i < l; i++) { if (a1[i] != b1[i]) { flag = 1; break; } } if (flag == 0) return 1; if (l % 2 == 1) return 0; return (chk(a1, b1, l / 2) && chk(a1 + ...
9
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int MODV = 1e9 + 7; int in_hand[200001]; int pile[200000]; vector<int> tmp; int straight(int n) { int now = pile[n - 1] + 1; for (int i = n - 2; i >= n - now + 1; i--) { if (pile[i] + 1 != pile[i + 1]) now = 1; } for (int i = 0; i < n;...
10
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = long double; // or double, if TL is tight using str = string; // yay python! using pi = pair<int,int>; using pl = pair<ll,ll>; using pd = pair<db,db>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vec...
21
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; long long A[maxn]; long long f[maxn], cnt[maxn]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) cin >> A[i]; memset(f, 0, sizeof(f)); memset(cnt, 0, sizeof(cnt)); for (int i = 1; i < m; i++) { long long x = A[i],...
12
#include <bits/stdc++.h> using namespace std; struct node { int q, s; node() { s = -1; q = 1e9; } }; const int N = 1e5 + 2000; int n, m, b[N], c[N], s, t; node a[N * 4]; void up(int l, int r, int ld, int rd, int x, int index, int q) { if (ld >= r || rd <= l) return; if (ld <= l && r <= rd) { a[x]....
11
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int res = pow(n, 2) + pow(n - 1, 2); printf("%d", res); return 0; }
0
#include <bits/stdc++.h> char s[2010]; int a[2010][2010]; int main() { int n, m, k, i, j; scanf("%d %d %d", &n, &m, &k); for (i = 0; i < n; i++) { scanf("%s", s); for (j = 0; j < m; j++) { if (s[j] == 'U') a[i][j] = 1; else if (s[j] == 'R') a[i][j] = 2; else if (s[j] == '...
6
#include <bits/stdc++.h> using namespace std; long long sum[500]; int deep[500]; int in1[500]; int out1[500]; int in2[500]; int out2[500]; vector<int> maps1[500]; vector<int> maps2[500]; long long dp[150000]; long long n, m, t; int a[500]; int cnt = 0; void topo2() { queue<int> q; for (int i = 1; i <= n; ++i) i...
13
#include <bits/stdc++.h> using namespace std; int main() { int n, s; scanf("%d%d", &n, &s); int ans = 100, res = 0; int flag = 0; int a, b; for (int i = 0; i < n; ++i) { scanf("%d%d", &a, &b); if (a < s) { if (100 - b <= 99 && b > 0) res = (res > 100 - b ? res : 100 - b); flag = 1; }...
4
#include <bits/stdc++.h> using namespace std; template <typename S, typename T> ostream& operator<<(ostream& out, pair<S, T> const& p) { out << '(' << p.first << ", " << p.second << ')'; return out; } template <typename T> ostream& operator<<(ostream& out, vector<T> const& v) { long long l = v.size(); for (long...
14
#include <bits/stdc++.h> using namespace std; const int MAXN = 'z' - 'a' + 1; int need[MAXN], have[MAXN], put[MAXN], ready[MAXN], is[MAXN]; vector<int> inte; int main() { char t[1000001], s[1000001]; int c = 0; scanf("%s%s", s, t); int lt = strlen(t), ls = strlen(s); for (int i = 0; i < lt; i++) { need[t[...
7
#include <bits/stdc++.h> using namespace std; int d, m, gen[25], b[30010], n; long long l, r; bitset<30010> all; class dat { public: long long len, ans; bitset<30010> pre, suf; dat operator+(const dat &t) const { dat c; c.ans = ans + t.ans; c.len = len + t.len; c.pre = pre; c.suf = t.suf; ...
26
#include <bits/stdc++.h> const int pi = acos(-1); using namespace std; int n, k; vector<int> v, u; int main() { cin >> k >> n; v.resize(k); u.resize(n); for (int i = 0; i < k; i++) v[i] = i + 1; for (int i = 0; i < n; i++) cin >> u[i]; int leader = 0; int i = 0; while (n--) { int lel = u[i] % v.size...
5
#include <bits/stdc++.h> using namespace std; struct Node { Node *ZERO = nullptr, *ONE = nullptr; bool isRoot = false; int count = 0; }; Node root; void insert(int n, int bit, Node *node) { node->count++; if (bit < 0) { node->isRoot = true; return; } if ((n & (1 << bit)) == 0) { if (node->ZERO...
13
#include <bits/stdc++.h> using namespace std; int n, aa[200005], Left[200005], Right[200005]; long long a[200005][6]; void push_back(long long x, long long *a) { if (x <= 0) return; while (a[0]) a++; a[0] = x; } void minimize(int &a, int b) { if (a > b) a = b; } bool FF[6][6][3][200005]; int F[6][6][3][200005];...
16
#include <bits/stdc++.h> int n, m, sol, val; inline int rid_log(int a, int put) { if (!put) return 1; int val2 = rid_log(a, put / 2); val2 = (long long)val2 * val2 % 1000000009; if (put & 1) val2 = (long long)val2 * a % 1000000009; return val2; } int main() { int i; scanf("%d%d", &n, &m); val = rid_log(...
5
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1000000007; const int INF = 2147483647; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, i, j, u, v; cin >> n; vector<pair<int, int>> g(n), ans; queue<int> q; for (i = 0; i < n; ++i) c...
7
#include <bits/stdc++.h> using namespace std; long long n; bool ok(long long d, vector<long long>& c) { long long ans = 0; for (long long i = 0; i < int((c).size()); i++) { ans += (c[i] / d); } if (ans >= n) return true; else return false; } int main() { ios_base::sync_with_stdio(false); cin.t...
4
#include <bits/stdc++.h> using namespace std; using ll = long long; class SegTree { private: const ll NEUT = 4 * (ll)1e18; vector<ll> seg, tag; int h = 1; void apply(int i, ll v) { seg[i] += v; if (i < h) tag[i] += v; } void push(int i) { if (tag[i] == 0) return; apply(2 * i, tag[i]); a...
18
#include <bits/stdc++.h> using namespace std; struct AhoCorasick { static const int nletter = 2; struct node { vector<int> keywords; node *go[nletter] = {}, *failure = {}, *output = {}; } *root = new node(); node *add(int size, int *values, int keywordid) { assert(size > 0); node *v = root; ...
24
#include <bits/stdc++.h> using namespace std; int main() { int n, k, ans = 0; cin >> n >> k; if (k / n != 2) cout << 0 << endl; else { ans = n - k % n; cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> const int N = 1e5 + 5; int tt, n, m, x, y, ans[N], pos[N], col[N]; std::vector<int> c[N]; std::priority_queue<std::pair<int, int> > h; int main() { scanf("%d", &tt); int i, a, C, mx; std::pair<int, int> o; while (tt--) { scanf("%d%d%d", &n, &x, &y); for (i = 1; i < n + 2; i++) c...
17
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e9 + 7; long long int int_pow(long long int base, long long int exp) { long long int result = 1; while (exp) { if (exp & 1) result *= base; exp /= 2; base *= base; } return result; } string s[2000]; int main() { ios::sync_with_stdio(0...
9
#include <bits/stdc++.h> using namespace std; int u1[610], u2[610], f[610]; vector<int> v1, v2, e[610]; int ans[610]; bool v[610]; bool dfs(int x, int y, int d) { ans[d++] = x; v[x] = true; if (x == y) { cout << "N " << d << "\n"; for (int i = 0; i < d; i++) cout << ans[i] << " "; cout << "\n"; co...
20
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const int oo = 0x3f3f3f3f; const int Mod = 1e9 + 7; const int maxn = 1000100; int fac[...
22
#include <bits/stdc++.h> using namespace std; double max(double a, double b) { return a > b ? a : b; } double min(double a, double b) { return a < b ? a : b; } double sqr(double x) { return x * x; } int n, i, x[100010], y[100010]; double xx, yy, minn, maxx, a, b, k, c, A, B, now1, now2, now3, x0, Y0; int main() { dou...
11
#include<bits/stdc++.h> #define M 1000000007 #define ll long long #define pb push_back #define pi pair<int,int> #define mp make_pair #define vi vector<int> #define vb vector<bool> #define vll vector <long long> #define tests int tc; cin>>tc; while(tc--) #define defsort(arr) sort(arr.begin(), arr.end()); #define csort(a...
12
#include <bits/stdc++.h> int faster_in() { int r = 0, c; for (c = getchar(); c <= 32; c = getchar()) ; if (c == '-') return -faster_in(); for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar()) ; return r; } using namespace std; const int INF = int(1e9 + 7); int v[10]; long long fact(int n) {...
10
#include <bits/stdc++.h> using namespace std; int main() { int n, d; int arr[100012]; cin >> n >> d; for (int i = 0; i < n; i++) { cin >> arr[i]; } long long lower = 0; int higher = 0; long long kount = 0; arr[n] = 1e9 + 7; for (long long i = 0; i < n; i++) { while (arr[higher] - arr[i] <= d...
5
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(0); int cases, caseno = 0, n, i, j, k, cnt, sum; string s; cin >> s; sum = 8; if (s[0] == 'a' || s[0] == 'h') sum -= 3; if (s[1] == '1' || s[1] == '8') sum -= 3; if (s[0] == 'a' && s[1] == '1' || s[0] == 'a' && s[1...
0
#include <bits/stdc++.h> using namespace std; pair<int, int> road[1003]; int main() { bool win = false; bool r = true; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> road[i].first >> road[i].second; if (road[i].first != road[i].second) win = true; } if (win) { cout << "rated" << endl; ...
1
#include <bits/stdc++.h> using namespace std; struct rec { long long x1, y1, x2, y2; }; int main() { int n; cin >> n; rec co[n]; long long x, y, p, q, x1min = INT_MAX, x2max = INT_MIN, y2max = INT_MIN, y1min = INT_MAX; vector<pair<long long, long long> > vx1, vx2, vy1, vy2; for (in...
8
#include <bits/stdc++.h> using namespace std; int N, K, pos[6][1001], dp[1001]; vector<int> way[1001]; int dfs(int n) { if (dp[n]) return dp[n]; for (int i = 0; i < (int)way[n].size(); i++) dp[n] = max(dp[n], dfs(way[n][i])); return ++dp[n]; } int main() { scanf("%d%d", &N, &K); for (int i = 1; i <= K; i+...
11
#include <bits/stdc++.h> const int N = 2000; const int A = 26; int n, amt[A]; char s[N + 1], t[N + 1], k[N]; std::vector<int> v; void apply(int x) { v.push_back(x); for (int i = n - 1; i >= std::min(x, n - x); --i) { if (i >= n - x) k[n - i - 1] = s[i]; if (i >= x) s[i] = s[i - x]; } for (int i = 0; i <...
15
#include <bits/stdc++.h> using namespace std; long long a[222222]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int ans = 0; int it = 0; while (it < n) { ans++; int pr = 0; int suf = 0; int val = 0; while (it < n && a[it] == -1) { pr++; it++; } ...
16
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; long long a[maxn], b[maxn], n, q, dp[maxn][5]; int main() { cin >> q; while (q--) { cin >> n; for (int i = 1; i <= n; i++) scanf("%lld%lld", &a[i], &b[i]); for (int i = 1; i <= n; i++) { for (int j = 0; j <= 2; j++) { ...
10
#include <bits/stdc++.h> using namespace std; const int MAX_N = 2111; string p[MAX_N]; char s[MAX_N][MAX_N]; char r1[MAX_N], r2[MAX_N], c1[MAX_N], c2[MAX_N]; int main() { ios_base::sync_with_stdio(false); int n, tot = 0; cin >> n; for (int i = 0; i < n; ++i) { cin >> p[i]; for (int j = 0; j < p[i].lengt...
12
#include <bits/stdc++.h> using namespace std; const int di[] = {-1, 0, 1, 0, -1, 1, 1, -1}; const int dj[] = {0, 1, 0, -1, -1, 1, -1, 1}; const long long MOD = 1e9 + 7; const long long INF = 1e9; const long double EPS = 1e-8; int main() { ios::sync_with_stdio(false), cin.tie(0); long long n, m; cin >> n >> m; l...
1
#include <bits/stdc++.h> using namespace std; void Start() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); } bool endline = false; bool endlineA = false; bool space = true; template <class T> istream& operator>>(istream& in, vector<T>& v) { for (auto& i : v) { in >> i; } return in; } template <cl...
12
#include <bits/stdc++.h> using namespace std; int dp[5050][5050]; int dp1[8][5050]; int a[5050]; int last[5050 * 20]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); int res = 0; for (int i = 1; i <= n; i++) { int x = a[i] % 7 + 1; for (int j = 0; j < i; j++) { ...
18
#include <bits/stdc++.h> using namespace std; int p, k, a[1000006], sz[1000006], link[1000006], ok[1000006]; int findLink(int x) { while (x != link[x]) x = link[x]; return x; } bool isSameLink(int a, int b) { return findLink(a) == findLink(b); } void unite(int a, int b) { a = findLink(a); b = findLink(b); if ...
10
#include <bits/stdc++.h> const long long max9 = 10 + 1e9, max6 = 10 + 1e6, max12 = 10 + 1e12, max15 = 10 + 1e15; const long long min6 = -1 * max6, min9 = -1 * max9, min12 = -1 * max12, min15 = -1 * max15; const long long R = 7 + 1e9, NN = 10 + 1e5; using namespace std; int main() { ios...
3
#include <bits/stdc++.h> using namespace std; int a[100]; int main() { int n; cin >> n; for (int i = 0; i < n / 2; i++) { cin >> a[i]; } sort(a, a + n / 2); int tmp = 0, ans = 0; for (int i = 0; i < n / 2; i++) { ans += abs(2 * (i + 1) - a[i]); } for (int i = 0; i < n / 2; i++) { tmp += ab...
3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, INF = 0x3f3f3f; inline int read() { int x = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); return x; } bool nc1; struct edge { int to, nxt; } e[N << 1]; int head[N], ecnt;...
24
#include <bits/stdc++.h> using namespace std; const int M = 4005; int read() { int x = 0, flag = 1; char c; while ((c = getchar()) < '0' || c > '9') if (c == '-') flag = -1; while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return x * flag; } int n, m, x[M], y[M], d[M], d...
13
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops") using namespace std; int n, m, kol, curt, NUM, deg; vector<vector<int> > gr, tree, pred; vector<int> t, u, comp, h; stack<int> liss; void init(int v, int pr, int h1) { h[v] = h1; int pr1 = pr; if (v != 0) { pred[v][0] = pr; for (int i ...
10
#include <bits/stdc++.h> using namespace std; vector<int> colors(200005, 0); vector<int> graph[200005]; vector<pair<int, int>> perechi; inline int oppositeColor(int node) { return (colors[node] == 1) ? 2 : 1; } bool color(int nodulet) { bool ok = true; for (auto vecin : graph[nodulet]) { if (colors[vecin] == co...
18
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int a[1000000]; int b[1000000]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } for (int i = 0; i < m; ++i) { scanf("%d", &b[i]); } sort(b, b + m); int toAdd = 0; for (long long ...
11
#include <bits/stdc++.h> using namespace std; long long int n, m, i; void fm(void) {} long long A[100001]; map<string, int> v; int main() { scanf("%lld", &n); long long a[n + 1]; scanf("%lld", &a[0]); for (i = 1; i < n; i++) { scanf("%lld", &m); a[i] = m + a[i - 1]; } long long p = a[n - 1] / 2; i...
5
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cer...
7
#include <bits/stdc++.h> using namespace std; int n, m, q, h, k; int flag, tmp; int ans, mxm, mnm; int max(int a, int b) { if (a > b) return a; else return b; } int min(int a, int b) { if (a < b) return a; else return b; } int gcd(int a, int b) { if (b == 0) return a; else return gcd...
7
#include <bits/stdc++.h> using namespace std; const int N_MAX = 1e5 + 10; int N, arr[N_MAX], num[N_MAX]; int main() { scanf("%d", &N); priority_queue<pair<int, int>> que; for (int i = 1; i <= N; i++) { scanf("%d", arr + i); que.push(make_pair(arr[i] - i, i)); } while (!que.empty()) { pair<int, int...
9
#include <bits/stdc++.h> using namespace std; int n, m, ta, tb, y[100005], z[100005]; long long x[100005], ans; vector<int> la; int main() { scanf("%d%d", &n, &m); for (int a = 0; a < n; a++) scanf("%I64d", &x[a]); n--; for (int a = 0; a < n; a++) x[a] = abs(x[a] - x[a + 1]); la.clear(); for (int a = 0; a <...
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; for (int test = 0; test < t; test++) { long long x, y; cin >> x >> y; if (x >= 4) { cout << "YES" << endl; } else { if (y < 4 && x > 1) { cout ...
2
#include <bits/stdc++.h> using namespace std; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int n, m; int bit[(int)2e5 + 100]; void update(int i) { for (; i <= n; i += (i & (-i))) bit[i]++; } int q(int i) { int sum = 0; for (; i; i -= (i & (-i))) sum += bit[i]; return sum; } int getIndex(int i) { int l ...
13
#include <bits/stdc++.h> using namespace std; long long pwr(long long base, long long p, long long mod = (1000000007LL)) { long long ans = 1; while (p) { if (p & 1) ans = (ans * base) % mod; base = (base * base) % mod; p /= 2; } return ans; } long long LCA(long long a, long long b) { set<long long...
7
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n, k; cin >> n >> k; string s; cin >> s; set<char> myset; for (auto x : s) myset.insert(x); vector<char> v; for (auto x : myset) v.push_back(x); if (k > n) { long long i = 0; string ans = ""; while (i < n) { ans...
7
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s; cin >> n >> m; s = 0; if (n >= m) { cout << n - m; return 0; } while (m > n) { if (m % 2 == 0) { m = m / 2; s++; } else { m = m + 1; s++; } } cout << s + n - m; }
6
#include <bits/stdc++.h> int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; int dy[] = {0, 1, 0, -1, -1, 1, 1, -1}; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; if (n % 2 == 0) { cout << "-1"; exit(0); } vector<long long> p1, p2, ...
5
#include <bits/stdc++.h> using namespace std; inline int read() { int res = 0; int neg; while (true) { char ch = getchar(); if (ch >= '0' && ch <= '9' || ch == '-') { if (ch == '-') neg = -1; else neg = 1, res = ch - '0'; break; } } while (true) { char ch = ge...
5
#include <bits/stdc++.h> using namespace std; int grundy[70]; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> s(n); for (int i = 0; i < n; i++) { cin >> s[i]; } int idx = 1; for (int x = 1; idx <= 60; x++) { for (int i = 0; i < x + 1 and idx <= 60; i++) { ...
13
#include <bits/stdc++.h> using namespace std; long long n, m; struct zuo { long long val, l, r; bool operator<(const zuo &b) const { return (l - r) < (b.l - b.r); } }; struct you { long long val, l, r; bool operator<(const you &b) const { return r - l < (b.r - b.l); } }; struct fc { long long num, l, r; }; ve...
11
#include <bits/stdc++.h> using namespace std; const long long MAXN = 19; long long n, m, ans, dp[1 << MAXN][MAXN]; bool edge[MAXN][MAXN]; vector<long long> adj[MAXN]; int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (long long u, v, i = 0; i < m; i++) { cin >> v >> u;...
14
#include <bits/stdc++.h> using namespace std; string s; int l, r, m; int main() { cin >> s; while (s.size() != 3 && s.size() != 7) { l = 0, r = 1; do { cout << "? " << l << " " << r << endl; cin >> s; if (s == "y") l = r, r <<= 1; } while (s == "y"); while (l + 1 < r) { m = (...
12
#include <bits/stdc++.h> using namespace std; int a[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, neg = 0; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; a[i] < 0 ? ++neg : neg; } if (neg >= k) { for (int i = 0; i < k; i++) { a[i] *= -1; } } e...
4
#include <bits/stdc++.h> using namespace std; string read_string() { char *str; scanf("%ms", &str); string result(str); free(str); return result; } int main() { string s = read_string(), t = read_string(); reverse(s.begin(), s.end()); if (s == t) puts("YES"); else puts("NO"); return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > q[1000010]; vector<int> ev[1000010]; int b[1000010]; void add(int p, int v) { while (p < 1000010) { b[p] += v; p += p & -p; } } int sum(int p) { int res = 0; while (p > 0) { res += b[p]; p -= p & -p; } return res; } int ma...
16
#include <bits/stdc++.h> using namespace std; template <class T> inline void amin(T &x, const T &y) { if (y < x) x = y; } template <class T> inline void amax(T &x, const T &y) { if (x < y) x = y; } template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) { for (bool sp = 0; begin != end; ++begin)...
15
#include <bits/stdc++.h> using namespace std; int vt, ve[(2 * 4 * 2010)], re[(2 * 4 * 2010)], ze[(2 * 4 * 2010)], nextt[(2 * 4 * 2010)]; int in[2010], head[2010], path[2010], dis[2010], qu[2010], lim[2010]; void init() { vt = 1; memset(head, 0, sizeof(head)); } int auxm = 0; void add(int x, int y, int cap, int ...
16
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } using ll = long long; using ld = long double; const int MAXMEM = 200 * 1000 * 1024; char _...
26
#include <bits/stdc++.h> using namespace std; vector<string> inp; string RED; void query() { int ans = 0; string q; getline(cin, q); stringstream cnv; cnv << q; vector<string> vs; string x; while (cnv >> x) vs.push_back(x); vector<string> open; vector<int> open_index; vector<int> other_index; fo...
14
#include <bits/stdc++.h> using namespace std; struct Debug { int cnt = 0; bool on = false; char debug[10] = "debug"; template <class T> Debug& operator<<(const T& v) { if (on && cnt++ == 0) cerr << "debug: "; if (on) cerr << v << ' '; return *this; } Debug& operator<<(ostream& (*pManip)(ostrea...
9
#include <bits/stdc++.h> using namespace std; int n, x, y, son[100005], q; int fir[100005], nxt[200005], to[200005], cnt; long long wi[200005], z, dp[100005][2], f[100005]; struct node { int id; long long val; } c[100005]; int size[100005], fa[100005], Fa[100005]; long long read() { long long x = 0; char ch = g...
20
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; string name; cin >> name; int f[9]; ; for (int i = 0; i < 9; i++) { cin >> f[i]; } int data[n]; ; for (int i = 0; i < n; i++) { data[i] = (int)(name[i] - '0') - f[(int)(name[i] - '0') - 1]; }; for (int i ...
5