solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; struct Node { int v, last; } e[100010 * 2]; int n, m, st[100010]; bool b[100010]; int d[100010], a[100010][17 + 2]; int ans[100010], f[100010], tp; int ee[100010][2]; void addedge(int k, int u, int v) { e[k].v = v; e[k].last = st[u]; st[u] = k; } void dfs(int k) { ...
11
#include <bits/stdc++.h> using namespace std; long long int tc, n, m, k; bool isPrime(int x) { for (int d = 2; d * d <= x; d++) { if (x % d == 0) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cin >> n; vector<long long> ans; for (_...
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; cout << (n - 2) * (n - 2); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAX_N = 3e5; const int INF_ = 0x3f3f3f3f; int a[MAX_N + 10]; int main() { int n; scanf("%d", &n); int b = 1; vector<pair<int, int>> ans; set<int> accur; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (accur.count(a[i]) > 0) { accur...
8
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long fpow(long long x, long long y) { if (y == 0) return 1; if (x == 1) return 1; if (x == 0) return 0; if (y == 1) return x; long long d = fpow(x, y / 2); d %= mod; if (y % 2) return (((d * d) % mod) * x) % mod; return (d * d) ...
13
#include <bits/stdc++.h> using namespace std; struct cs { int to, next; } a[200001]; int aa[400001], head[100001], sum[100001]; bool vi[100001], c[100001]; int n, m, z, y, x, ans, ll, S; void init(int x, int y) { a[++ll].to = y; a[ll].next = head[x]; head[x] = ll; } void dfs(int x, int y) { vi[x] = 1; aa[++...
14
#include <bits/stdc++.h> using namespace std; class Solution { public: void PalDeg() { int n; scanf("%d", &n); vector<int> a; for (int i = 0; i < n; i++) { int temp; scanf("%d", &temp); a.push_back(temp); } if (n == 1) { printf("1\n"); return; } bool *num...
17
#include <bits/stdc++.h> const auto PI = acos(-1); template <class T> inline T sqr(T x) { return x * x; } inline double fracpart(double x) { return x - floor(x); } class direction_map { private: std::multiset<std::pair<double, bool> > edges; int zero_counter = 0; public: void insert(double narg_center, int64...
17
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n; cin >> s; int l = s.length(); int count = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) { count++; } } cout << count << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * f; } int n, m; const int mod = 1e9 + 7; void add(int &a, int ...
17
#include <bits/stdc++.h> using namespace std; int main() { int x, y; cin >> x >> y; int arr[x + 1]; memset(arr, -1, sizeof arr); for (int i = 0; i <= x; i++) arr[i] = i; int arr2[x + 1]; memset(arr2, -1, sizeof arr2); int cur = y; for (int i = 0; i <= x && cur >= 0; i++) arr2[i] = cur, cur--; int cn...
7
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } long long pow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) { res *= (a) % 1000000007; res = (res) % 100...
8
#include <bits/stdc++.h> using namespace std; long long solve(int n, long long h) { if (n == 0) return 0; if (h < (1LL << (n - 1))) return solve(n - 1, h ^ ((1LL << (n - 1)) - 1)) + 1; return solve(n - 1, h ^ (1LL << (n - 1))) + (1LL << n); } int main() { ios ::sync_with_stdio(false); int n; long long h; ...
9
#include <bits/stdc++.h> using namespace std; const long long mx = (1e5) + 1; const long long mod = 1000000007; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; map<string, int> mp; while (n--) { cin >> s; if (mp[s] == 0) { cout << "NO" << ...
0
#include <bits/stdc++.h> using namespace std; int main() { int x, y = 0; int h, m; cin >> x >> h >> m; while (true) { if (h % 10 == 7 || h / 10 == 7 || m % 10 == 7 || m / 10 == 7) break; y++; m -= x; if (m < 0) { m = 60 + m; h--; } if (h < 0) h = 23; } cout << y << endl; ...
1
#include <bits/stdc++.h> using namespace std; void test2() { int t; cin >> t; while (t--) { int n, m, x, y; cin >> n >> m >> x >> y; char vec[n][m]; for (auto i = 0; i < n; i++) { for (auto j = 0; j < m; j++) { cin >> vec[i][j]; } } int64_t ans = 0; for (auto i = 0;...
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<int> a(n); for (auto &i : a) cin >> i; vector<int> suf(n), pref(n); for (int i = 0; i < n; ++i) { pref[i] = (a[i] >= k...
12
#include <bits/stdc++.h> using namespace std; using ll = long long; ll a[200500], xz[1000500]; int main() { ios::sync_with_stdio(0); ll n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) cin >> a[i]; int l = 0; sort(a, a + n); int ans = 0, t = 0; for (int r = 0; r < n; r++) { while (l <= r && ...
8
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9 + 7; const int inf = 1061109567; const int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; const int maxn = 1005; int vis[maxn][maxn], a[maxn][maxn], used[maxn][maxn]; int n, m; long long sum; int sol...
12
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n, greater<int>()); for (int i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } }
2
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") const long long INF = 1ll << 32; const int mod = 1e9 + 7; inline int add(int x, int y) { x += y; if (x >= mod) return x - mod; return x; } inline int sub(int x, int y) {...
7
#include <bits/stdc++.h> using namespace std; const int N = (int)1e6 + 7; int a[N], b[30]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", a + i); } sort(a, a + n); for (int i = 0; i < m; i++) { int x; scanf("%d", &x); b[x]++; } int ans = 0; for...
11
#include <bits/stdc++.h> using namespace std; double f[1 << 20]; double p[32]; int n, k, z; int main() { scanf("%d%d", &n, &k); for (int i = 0, to = n; i < to; ++i) { scanf("%lf", p + i); if (!p[i]) z++; } if (n - z < k) { for (int i = 0, to = n; i < to; ++i) { if (i) putchar(' '); print...
16
#include <bits/stdc++.h> using namespace std; int main() { long long t, i, n, m, p, f; cin >> t; while (t--) { vector<long long> a, b; map<long long, long long> x; cin >> n, p = 0, f = 0; for (i = 0; i < n; i++) { cin >> m; a.push_back(m); if (p > m) f = 1; p = m; } ...
5
#include <bits/stdc++.h> using namespace std; void solve() { long long n, i, j, l, m, k, ans, a, b, s, sum, last = 0, l1 = 0, l2 = 0; long long t1 = 0, t2 = 0; vector<int> p1, p2; cin >> m; for (i = 0; i < m; ++i) { cin >> n; if (n > 0) { t1 += n; p1.push_back(n); last = 0; l1 ...
6
#include <bits/stdc++.h> using namespace std; int N, ret[55]; long long K, dp[55]; int main() { cin >> N >> K; dp[0] = 1; dp[1] = 1; for (int i = 2; i <= N; ++i) { dp[i] = dp[i - 1] + dp[i - 2]; } for (int i = 0; i < N; ++i) { ret[i] = i + 1; } int pos = 0; while (pos != N) { if (K > dp[N ...
11
#include <bits/stdc++.h> using namespace std; int n, sz; vector<int> dat; inline void add(int tmp) { int pos = lower_bound(dat.begin(), dat.end(), tmp) - dat.begin(); dat.insert(dat.begin() + pos, tmp); ++sz; } inline void del(int tmp) { int pos = lower_bound(dat.begin(), dat.end(), tmp) - dat.begin(); dat.er...
15
#include <bits/stdc++.h> using namespace std; int main() { long long n, res = 0; cin >> n; long long ar[n]; for (int i = 0; i < n; i++) cin >> ar[i]; map<long long, long long> cou; for (int i = 0; i < n; i++) { cou[i - ar[i]] += ar[i]; if (cou[i - ar[i]] > res) res = cou[i - ar[i]]; } cout << re...
6
#include <bits/stdc++.h> using namespace std; const int N(1 << 16); int n; int d[N], s[N]; set<pair<int, int> > ver; vector<pair<int, int> > edges; int main() { cin >> n; for (int i = 0; i != n; ++i) { cin >> d[i] >> s[i]; ver.insert(make_pair(d[i], i)); } while (!ver.empty()) { auto p = *(ver.begin...
7
#include <bits/stdc++.h> const int MAXN = 2e6 + 5; int n, m; const int ha = 998244353; int fac[MAXN], inv[MAXN]; inline int qpow(int a, int n = ha - 2) { int res = 1; while (n) { if (n & 1) res = 1ll * res * a % ha; a = 1ll * a * a % ha; n >>= 1; } return res; } inline void prework() { fac[0] = 1;...
22
#include <bits/stdc++.h> using namespace std; template <class T> inline void input(T &x) { register char c = getchar(); x = 0; int neg = 0; for (; ((c < 48 || c > 57) && c != '-'); c = getchar()) ; if (c == '-') { neg = 1; c = getchar(); } for (; c > 47 && c < 58; c = getchar()) { x = (x <...
17
#include <bits/stdc++.h> using namespace std; struct A { int v; A* right; A* down; } G[1111][1111]; int n, m, k; int a, b, c, d, h, w; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &G[i][j].v); for (int i = 0; i <= n; i++) for (int j ...
17
#include <bits/stdc++.h> using namespace std; long long int inf = 1e14; long long int mod = 490019; char en = '\n'; long long int abs1(long long int x) { if (x < 0) return -x; return x; } long long int arr[105]; long long int dp[105][105]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long lon...
4
#include <bits/stdc++.h> using namespace std; struct node { int x, y, num; }; node e[3001]; int n; int ans = 0; int father[3001]; int s[3001]; int outlet[3001]; int sum_out = 0; bool cmp1(node xx, node yy) { return xx.num < yy.num; } int find(int x) { if (father[x] != x) { father[x] = find(father[x]); } ret...
19
#include <bits/stdc++.h> using namespace std; const long long MAX = 2e5, MOD = 1000000007; unordered_map<char, pair<long long, long long> > val; string a, qq[MAX]; inline pair<long long, long long> findVal(const string& a) { long long ans = 0, i, j; for (i = (long long)a.size() - 1, j = 1; i >= 0; i--) { ans +=...
13
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); long long int n; cin >> n; long long int a, b, c; cin >> a >> b >> c; long long int count = 0; if (a > b - c) { if (n >= b) count += ((n - b) / (b - c) + 1); n -= count * (b - c); if (n >= a) count += n / a; ...
9
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; constexpr int MAXN = 5 + 200000; int n, k; char a[MAXN], b[MAXN]; deque<int> ans; bool le() { for (int i = (int)0; i < (int)n; ++i) { if (a[i] != b[i]) { if (b[i] < a[i]) return true; else re...
9
#include <bits/stdc++.h> using namespace std; int n, m, a[200005], b[200005], top, q[200005], now, lat, ans[200005]; int gets() { int v = 0; bool f = 0; char ch; while (!isdigit(ch)) { f |= ch == '-'; ch = getchar(); } while (isdigit(ch)) { v = (v << 3) + (v << 1) + ch - 48; ch = getchar(); ...
4
#include <bits/stdc++.h> using namespace std; struct node { int id; vector<node*> neigh; node* parent; int color; int n; }; node T[200010]; int n, a, b, done; void visit(node* n) { cout << n->id << " "; while (n->n < n->neigh.size()) { auto c = n->neigh[n->n]; if (c == n->parent) { n->n++; ...
11
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; using ii = pair<long long int, long long int>; class prioritize { public: bool operator()(ii &p1, ii &p2) { return p1.first < p2.first; } }; auto start_time = high_resolution_clock::now(); void time() { return; } void ojudge() { ios_base::s...
14
#include <bits/stdc++.h> using namespace std; map<int, int> M; priority_queue<pair<int, int> > PQ; int n, a[100005], A[100005], B[100005], C[100005], tt; int main() { scanf("%d", &n); for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]), M[a[i]]++; auto IT = M.begin(); while (IT != M.end()) { PQ.push(make_pa...
10
#include <bits/stdc++.h> using namespace std; int main() { int n; char ch[30]; bool exist1[26] = {false}, exist2[26][26] = {false}; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", ch); for (int j = 0; j < strlen(ch); ++j) { exist1[ch[j] - 'a'] = true; } for (int j = 1; j < str...
7
#include <bits/stdc++.h> using namespace std; const int mxN = 200; int R, G, B, r[mxN], g[mxN], b[mxN]; int dp[mxN + 1][mxN + 1][mxN + 1]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> R >> G >> B; for (int i = 0; i < R; ++i) cin >> r[i]; for (int i = 0; i < G; ++i) cin >> g[i]; for (int i = 0; i...
10
#include <bits/stdc++.h> using namespace std; double R; long long ans, cur1, cur2, cnt; inline int pd(double a, double b) { if (fabs(a - b) <= 1e-8) return 0; if (a - b > 1e-8) return 1; return -1; } bool check(int i, int j) { if (i & 1) { if (pd((0.5 * (3 * i - 2) * 0.5 * (3 * i - 2)) + (sqr...
15
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { fast(); int t; cin >> t; while (t--) { int n; cin >> n; int c1 = n / 3; int c2 = (n - c1) / 2; if ((c1 + 1) + (2 * c2) == n) cout << c1 + 1 ...
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int tc; cin >> tc; while (tc--) { long long n, m, k; cin >> n >> m >> k; vector<long long> h(n + 1); for (int i = 1; i <= n; i++) cin >> h[i]; int cur = 1; while (cur < n) { if (h[...
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int>> graph(n); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; graph[u - 1].push_back(v - 1); graph[v - 1].push_back(u - 1); } string ans = "YES"; for (int i = 0; i < n; i++) { if (gra...
8
#include <bits/stdc++.h> using namespace std; int main() { long long ans = 0, a, b; cin >> a >> b; while (b > 0) { ans += a / b; a %= b; swap(a, b); } cout << ans; }
8
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const long long N = 2e5 + 7; const int mod = 1e9 + 7; bool cmp(const pair<long long, long long>& a, const pair<long long, long long>& b) { return a.first < b.first; } void solve() { long long n, A, b; cin >> n >> A >> b; long lon...
2
#include <bits/stdc++.h> const long long INF = 1LL << 60; const long long MOD = 1000000007; using namespace std; using ll = long long; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; ...
9
#include <bits/stdc++.h> #pragma GCC target("sse4.2") using namespace std; bool home = 1; signed realMain(); signed main() { home = 0; if (home) { freopen("tony_stark", "r", stdin); } else { ios::sync_with_stdio(0); cin.tie(0); } realMain(); } const int M = (int)1e9 + 7; int add(int a, int b) { ...
17
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v; v.resize(n); for (int i = 0; i < n; i++) { cin >> v[i]; v[i] %= 2; } vector<int> st; for (int i = 0; i < v.size(); i++) { st.push_back(v[i]); if (st.size() > 1) { if (st[st.size() - 1] == st[...
14
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<vector<int>> edg(n + 1); int u, v; for (int i = 1; i <= m; i++) { cin >> u >> v; edg[u].push_back(v); edg[v].push_back(u); } int dfn = ...
19
#include <bits/stdc++.h> using namespace std; const int MAXN = 100007; int main() { int n, i, a[MAXN]; bool flag; cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; if (a[n] == 1) { cout << "NO" << endl; return 0; } if (n == 1) { cout << "YES" << endl; cout << a[1] << endl; return 0; } ...
14
#include <bits/stdc++.h> using namespace std; long long n; vector<long long> edge[1015]; queue<long long> vertex; bool dfs(long long v, long long p, long long x) { if (v == x) return 1; for (long long i = 0; i < edge[v].size(); i++) { if (edge[v][i] != p) { if (dfs(edge[v][i], v, x)) return 1; } } ...
11
#include <bits/stdc++.h> using namespace std; struct node { long long data; int id; } e[200005]; long long cmp(node aa, node bb) { return aa.data < bb.data; } int main() { int n; while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) { scanf("%lld", &e[i].data); e[i].id = i; } sort(e + ...
4
#include <bits/stdc++.h> using namespace std; long long v[100005], pref[100005], len[100005]; int main() { map<long long, long long> mapa; long long n; cin >> n; long long sum = 0; for (long long i = 0; i < n; i++) { long long op; cin >> op; if (op == 1) { long long x; cin >> x; ...
8
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; const int MAXK = -1; const int MAXN = 602; const int MOD = 1; int n; int pc[MAXN]; int c[MAXN]; int nxt[MAXN]; void read(int &x) { string s; cin >> s; if (s == "?") { x = -1; return; } stringstream ss; ss << s; ...
26
#include <bits/stdc++.h> using namespace std; struct A { int val; int pos; }; bool compv(const A& one, const A& two) { return one.val < two.val; } bool compp(const A& one, const A& two) { return one.pos < two.pos; } int main() { int n; cin >> n; A a[300002]; for (int i = 1; i <= n; ++i) { cin >> a[i].va...
6
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int inf = 1e9 + 1; const double eps = 1e-15; const double EPS = 1e-9; const double PI = acos(-1.0); long long int dx_8[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy_8[] = {-1, 0, 1, -1, 1, -1, 0, 1}; long long int dx[] = {0, 0, 1, -1}, dy[] ...
0
#include <bits/stdc++.h> int oo[1 + 300000 * 2], oj[1 + 300000 * 2]; int link(int o, int j) { static int _ = 1; oo[_] = o, oj[_] = j; return _++; } int ae[100000], dd[100000], pp[100000], ta[100000], lower, upper; void dfs(int p, int i) { static int time; int o; pp[i] = p, ta[i] = ++time; for (o = ae[i]; ...
19
#include <bits/stdc++.h> using namespace std; template <class T> void splitstr(const string &s, vector<T> &out) { istringstream in(s); out.clear(); copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } static void redirec...
16
#include <bits/stdc++.h> using namespace std; int n; long long int x, y, t; int main() { scanf("%d", &n); for (int e = 1; e <= n; e++) { t = 0; scanf("%lld %lld", &x, &y); while (x != 0) { if (x % y == 0) { x /= y; t++; } else { t += x % y; x = x - (x % y); ...
1
#include <bits/stdc++.h> using namespace std; int R, C, N, K; vector<int> v[3005]; int temp[3005]; int lft[3005], rht[3005]; vector<int> idx; deque<int> dq; int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> R >> C >> N >> K; for (int i = 1; i <= N; i++) { int r, c; cin >> r >> c; ...
3
#include <bits/stdc++.h> using namespace std; int main() { char s1[1000], s2[1000]; int n, i, j, k, l, f = 0, a[26] = {0}, b[26] = {0}; scanf("%s", s1); scanf("%s", s2); n = strlen(s1); for (i = 0; s1[i] != '\0'; ++i) a[s1[i] - 97]++; for (i = 0; s2[i] != '\0'; ++i) b[s2[i] - 97]++; for (i = 0; i < 26; ...
6
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const long long INF = 1e9 + 5; const long long mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); ios_base::sync_with_stdio(0); ; string s; cin >> s; int size1 = s.size(); int last = s.size() - 1, cnt = 0; for (int i = ...
6
#include <bits/stdc++.h> #define endl '\n' #define IOS ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr) using namespace std; typedef long long LL; typedef pair<int, int> PII; const int maxn = 1e3 + 500; string str[maxn]; struct Edge{ int u,v,nxt,f,w; }edge[maxn*maxn << 1]; int head[maxn*maxn<<1],cnt...
18
#include <bits/stdc++.h> using namespace std; int mod(int a, int b) { if (a >= b) return a - b; else return b - a; } int main() { int n, h, a, b, k; cin >> n >> h >> a >> b >> k; int fa, ta, fb, tb; for (int i = 0; i < k; i++) { int count = 0; cin >> ta >> fa >> tb >> fb; if (ta == tb) ...
2
#include <bits/stdc++.h> using namespace std; unsigned long long m, n, i, t, k, a[1000]; int main() { cin >> m; t = 1; k = 1; for (i = 0; i < m; i++) { cin >> a[i]; } sort(a, a + m); for (i = 0; i < m; i++) { if (a[i] == a[i + 1]) k = k + 1; else { if (t <= k) t = k; ...
0
#include <bits/stdc++.h> const int N = 300005; struct Node { int l, r, x; } a[N]; int tree[N << 2], lazy[N << 2]; void init() { memset(tree, 0, sizeof(tree)); memset(lazy, -1, sizeof(lazy)); } void pushdown(int now) { if (lazy[now] == -1) return; tree[now << 1] = tree[now << 1 | 1] = lazy[now << 1] = lazy[now...
7
#include <bits/stdc++.h> using namespace std; void solve() {} int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; vector<int> A(t); for (int &i : A) cin >> i; if (t == 1) { cout << 0 << "\n"; } else { map<int, int> set; for (int i = 0; i < t; i++) { set[A[...
1
#include <bits/stdc++.h> std::mt19937 rng( (int)std::chrono::steady_clock::now().time_since_epoch().count()); const int ms = 300300; std::vector<int> edges[ms]; int c[ms]; void dfs(int on) { int same = 0; for (auto to : edges[on]) { if (c[to] == c[on]) same++; } if (same > 1) { c[on] ^= 1; for (...
14
#include <bits/stdc++.h> using namespace std; double inff = 1.0 / 0.0; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long int x, y, n, i, z, j, k, w, u, q, m, t, ans; long long int dp[700010][20]; bool mat[20][20]; long long int smallest_set(long long int x) { long long int cou = 0; w...
14
#include <bits/stdc++.h> using namespace std; const int N = 505; int n, k; char cell[N][N]; int pref[N][N]; int par[N * N], sz[N * N]; int freq[N * N], sum; int parent(int v) { return par[v] == v ? v : (par[v] = parent(par[v])); } void merge(int first, int second) { first = parent(first); second = parent(second); ...
16
#include <bits/stdc++.h> using namespace std; inline int read() { int sum = 0, p = 1; char ch = getchar(); while (!(('0' <= ch && ch <= '9') || ch == '-')) ch = getchar(); if (ch == '-') p = -1, ch = getchar(); while ('0' <= ch && ch <= '9') sum = sum * 10 + ch - 48, ch = getchar(); return sum * p; } const ...
16
#include <bits/stdc++.h> using namespace std; struct curve { int ost, ob; }; bool operator<(const curve &a, const curve &b) { return a.ob > b.ob; } int main() { int n; cin >> n; curve *a = new curve[n]; for (int i = 0; i < n; i++) cin >> a[i].ost; for (int i = 0; i < n; i++) cin >> a[i].ob; sort(a, a + n)...
1
#include<bits/stdc++.h> #define int long long #define gmax(x,y) x=max(x,y) #define gmin(x,y) x=min(x,y) #define F first #define S second #define P pair #define FOR(i,a,b) for(int i=a;i<=b;i++) #define rep(i,a,b) for(int i=a;i<b;i++) #define V vector #define RE return #define ALL(a) a.begin(),a.end() #define MP make_pai...
11
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 100; const int INF = 1e9 + 7; vector<int> g[N]; int d[N], used[N], x[N], sup[N]; map<pair<int, int>, int> cnt_e; map<int, int> cnt; void fail() { cout << "NO\n"; exit(0); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin ...
16
#include <bits/stdc++.h> using namespace std; vector<int> ad[444]; int rail[444][444], n, m, d[444]; void bfs() { queue<int> q; q.push(1); d[1] = 0; while (!q.empty()) { int v = q.front(); q.pop(); for (auto u : ad[v]) if (d[u] == -1) { d[u] = d[v] + 1; q.push(u); } } }...
8
#include <bits/stdc++.h> using namespace std; int main() { int n, x, i, j, k, mx = 0, c; bool d[101], b[101]; cin >> n; for (i = 1; i <= n; i++) cin >> d[i]; for (i = 1; i <= n; i++) for (j = i; j <= n; j++) { c = 0; for (k = 1; k <= n; k++) b[k] = d[k]; for (k = i; k <= j; k++) b[k] = !...
4
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e18, M = 1e9 + 7; const long long int N = 1e5 + 5; vector<long long int> v[N], val(N, 0); long long int dp[N][2]; long long int zero, one; void dfs(long long int x, long long int p) { dp[x][val[x]] = 1; for (auto c : v[x]) { if (c == p) co...
12
#include <bits/stdc++.h> using namespace std; template <typename T> void cetak(T t) { cout << t << ')' << endl; } template <typename T, typename... V> void cetak(T t, V... v) { cout << t; if (sizeof...(v)) cerr << ", "; cetak(v...); } const int MOD = 1e9 + 7; const int mx = 2e5 + 10; class Element_suffix { pub...
18
#include <bits/stdc++.h> using namespace std; void assign(unsigned int dest[42][42], unsigned int src[42][42], unsigned int N) { for (unsigned int i = 0; i < (N); ++i) for (unsigned int j = 0; j < (N); ++j) dest[i][j] = src[i][j]; } void mul(unsigned int A[42][42], unsigned int B[42][42], unsigned int...
17
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; const long long mod = 1e9 + 7; int main() { cin.tie(0); ios_base::sync_with_stdio(0); long long a1, a2, a3, a4, a5, a6; cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6; long long ans = (a1 + a2 + a3) * (a1 + a2 + a3) - a1 * a1 - a3 * a3 - a5 * a5;...
8
#include <bits/stdc++.h> int n; struct node { double x, y; } p[1010]; double t(int j, int k, int l) { if (k == 0) j = n - 1; if (k == n - 1) l = 0; double A = p[l].y - p[j].y; double B = p[j].x - p[l].x; double C = p[l].x * p[j].y - p[j].x * p[l].y; return fabs(A * p[k].x + B * p[k].y + C) / sqrt(A * A + ...
10
#include <bits/stdc++.h> using namespace std; long long a, b; long long lucky[2000]; long long wynik; int main(void) { lucky[0] = 4LL; lucky[1] = 7LL; for (int i = 0; lucky[2 * i + 1] < 1000000000LL; i++) { lucky[2 * i + 2] = lucky[i] * 10LL + 4LL; lucky[2 * i + 3] = lucky[i] * 10LL + 7LL; } cin >> a ...
3
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { int c = min(a, b); a = max(a, b); b = c; while (b != 0) { a = a % b; swap(a, b); } return a; } map<int, int> cnt; int ans[1000]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n * n; i++) { int a; scanf("%d...
9
#include <bits/stdc++.h> using namespace std; long long MOD = 1e9 + 7; int stmi[(int)22 + 1][(int)1e6]; int LOG[(int)1e6]; void minsptab(vector<int> a, int n) { for (int i = 0; i < n; i++) { stmi[0][i] = a[i]; } for (int j = 1; j <= (int)22; j++) { for (int i = 0; i + (1 << j) <= n; i++) { stmi[j][i...
12
#include <bits/stdc++.h> using namespace std; map<string, bool> mymap; int main() { int n, len; bool flag; char s[25]; char ss[25]; while (~scanf("%d", &n)) { mymap.clear(); flag = 0; for (int i = 0; i < n; i++) { scanf("%s", s); len = strlen(s); for (int i = 0; i < len; i++) { ...
7
#include <bits/stdc++.h> using namespace std; const int N = 4040, INF = 2e4; struct Edge { int from, to, flow; }; struct MaxFlow { int B[N], qu[N]; vector<Edge> E; vector<int> Adj[N]; void Add(int v, int u, int cap) { Adj[v].push_back(E.size()); E.push_back(Edge{v, u, cap}); Adj[u].push_back(E.siz...
17
#include <bits/stdc++.h> using namespace std; const int sz = 1e6 + 7; int n, a[sz], l[sz], r[sz], x[sz], mxi; long long c[sz], sum; struct node { int v, no; long long num; } q[sz]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] > a[mxi]) mxi = i; } for (int ...
16
#include <bits/stdc++.h> using namespace std; const long long inf = 9.2e18; const int maxn = 5e2 + 70; const int INF = 2.1e9; const int maxm = 1e6 + 70; const int MOD = 998244353; const double eps = 1e-7; const double PI = acos(-1.0); int n, m, k; int dp[maxn][maxn]; inline int add(int x, int y) { int z = x + y; if...
13
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; int c[n + 10]; fill_n(c, n + 10, 0); for (int i = 0; i < m; i++) { int mi = 0, t, me = -1; for (int j = 0; j < n; j++) { cin >> t; if (t > me) mi = j, me...
3
#include <bits/stdc++.h> using namespace std; long long int T; const long long int mod = 1e9 + 7; const long long int maxn = 1e5 + 3; void srinite() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); T = 1; } long long int bin_pow(long long int num, long long int power) { long long int ans = 1; while ...
5
#include <bits/stdc++.h> using namespace std; int n, k; long long arr[(int)3e5 + 5]; bool solve(int num) { for (int i = num; i < 1e6; i += num) { int l = 0, r = n - 1, mid, ans = -1; while (l <= r) { mid = (l + r) / 2; if (arr[mid] >= i && arr[mid] < i + num) ans = arr[mid], l = mid + 1; ...
13
#include <bits/stdc++.h> using namespace std; const int N = 1101; const int dx[8] = {0, -1, -1, -1, 0, 1, 1, 1}; const int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; const long long base = 1e9 + 7; int n, a[N][N]; int dp[N][N][8]; double lg[N][N], s1[N][N], s2[N][N], s3[2 * N][N], s4[2 * N][N], res; void DP() { for (int i ...
15
#include <bits/stdc++.h> using namespace std; const int MAXQ = 1e5 + 5; inline int rnd(void) { return (rand() << 15) | rand(); } struct Treap { struct Node { int val, siz, fix; Node *son[2]; long long sum[5]; Node(int val_) : val(val_), siz(1), fix(rnd()), son(), sum() { sum[1] = val; } } ...
15
#include <bits/stdc++.h> using namespace std; long long n, m, t, x, y; int val[100009]; vector<vector<int> > AdjList; bool llegada[100009]; bool cmp[100009]; long long acumulados[100009]; int ind; long long todo; int maxi_prof; void dfs(int nodo, long long acum, int prof) { maxi_prof = max(maxi_prof, prof); acumula...
13
#include <bits/stdc++.h> using namespace std; const int MN = 1000 * 1000 + 100; int q, ans[MN]; vector<string> s2; int mmm[MN]; void f(vector<string> rule, int III) { vector<string> stck; vector<int> p; for (int i = 0; i < s2.size(); i++) { if (s2[i] == rule[stck.size()]) { stck.push_back(s2[i]); ...
14
#include <bits/stdc++.h> int col[101], row[101]; int main() { int m, n, h, i, j; scanf("%d %d %d", &n, &m, &h); int ara[n + 1][m + 1]; for (i = 1; i <= m; i++) { scanf(" %d", &col[i]); } for (i = 1; i <= n; i++) { scanf("%d", &row[i]); } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { ...
4