text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; int n, u, v, z = 0, f[100001]; int main() { for (int i = 1; i <= 10000; i++) z++; for (int i = 1; i <= 100; i++) z--; cin >> n; for (int i = 1; i < n; i++) { cin >> u >> v; f[u]++; f[v]++; } for (int i = 1; i <= n; i++) if (f[i] != 1 && f[i] <= 2) { cout << "NO"; return 0; } cout << "YES"; return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int n, x, y; vector<int> g[100010]; int main() { cin >> n; for (int i = 0; i < n - 1; i++) { scanf("%d%d", &x, &y); g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; i++) { if (g[i].size() == 2) { puts("NO"); return 0; } } puts("YES"); return 0; }
#include <bits/stdc++.h> using namespace std; long long int n; vector<long long int> G[200020]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } for (int i = 1; i <= n; i++) { if (G[i].size() == 2) return cout << "NO", 0; } cout << "YES"; }
#include <bits/stdc++.h> using namespace std; int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1}; long long bigmod(long long x, long long p) { long long res = 1; while (p) { if (p & 1) res = (res * x) % 998244353; x = (x * x) % 998244353; p >>= 1; } return res; } int32_t main() { ios_base::sync_with_stdio(false); int n, m; cin >> n; m = n - 1; vector<int> cnt(n); while (m--) { int u, v; cin >> u >> v; cnt[u - 1]++; cnt[v - 1]++; } int i = 0; for (; i < n; i++) if (cnt[i] == 2) break; cout << (i < n ? "NO" : "YES\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int g[100005]; int main() { int n; memset(g, 0, sizeof(g)); cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u]++; g[v]++; } for (int i = 1; i <= n; i++) { if (g[i] == 2) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
#include <bits/stdc++.h> using namespace std; ; const double eps = 1e-8; const int mod = 1e9 + 7; const int maxn = 3e5 + 5; const int INF = 0x3f3f3f3f; const long long INFLL = 0x3f3f3f3f3f3f3f3f; struct EDGE { int v, nxt; } edge[maxn << 1]; int head[maxn], tot; void add_edge(int u, int v) { edge[tot].v = v, edge[tot].nxt = head[u], head[u] = tot++; } int degree[maxn]; int main() { memset(head, -1, sizeof(head)); tot = 0; int n; scanf("%d", &n); for (int i = 1, u, v; i < n; i++) { scanf("%d%d", &u, &v); degree[u]++; degree[v]++; } bool flag = true; for (int i = 1; i <= n; i++) { if (degree[i] == 2) { flag = false; } } if (flag) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const int BASE = 1e9 + 7; const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; const int N = 1e5 + 1; vector<int> a[N]; int pa[N], n; int F[N]; void DFS(int p, int u) { bool Check = true; for (__typeof(a[u].begin()) it = a[u].begin(); it != a[u].end(); it++) { int v = *it; if (v == p) continue; Check = false; DFS(u, v); pa[v] = u; F[u] = F[u] + F[v]; } F[u] += Check; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; int u, v; for (int i = 1; i < n; i++) { cin >> u >> v; a[u].push_back(v); a[v].push_back(u); } bool Check = true; for (int u = 1; u <= n; u++) { for (__typeof(a[u].begin()) it = a[u].begin(); it != a[u].end(); it++) { int v = *it; if (a[u].size() == 1) { if (a[v].size() == 1) continue; if (a[v].size() < 3) { Check = false; } } else if (a[v].size() == 1) { if (a[u].size() == 1) continue; if (a[u].size() < 3) { Check = false; } } else { if (a[v].size() < 3 || a[u].size() < 3) { Check = false; } } } } cout << (Check ? "YES" : "NO"); }
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } std::vector<long long> edge[100045]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t, i, j, n, m, k, q, temp, x, y; { long long count = 0; bool flag = 0; long long a[200050] = {0}; string s; cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x >> y; edge[x].push_back(y); edge[y].push_back(x); } { flag = true; for (i = 1; i <= n; i++) { if (edge[i].size() == 2) { flag = false; } } if (flag) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; bool is_prime(long long n) { for (long long i = 2; i * i <= n; ++i) { if (n % i == 0) { return false; } } return true; } vector<long long> fact(long long n) { n = abs(n); vector<long long> ans; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { ans.push_back(i); ans.push_back(n / i); } } return ans; } inline long long getPow(long long a, long long b) { long long res = 1ll, tp = a; while (b) { if (b & 1ll) { res *= tp; } tp *= tp; b >>= 1ll; } return res; } long long vec_mult(long long x1, long long y1, long long x2, long long y2, long long x3, long long y3) { return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)); } void ok() { cout << "YES" << endl; exit(0); } void no() { cout << "NO" << endl; exit(0); } inline long long nxt() { long long x; cin >> x; return x; } const long long N = 3e5 + 5, inf = 8e18; int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); long long n = nxt(); vector<vector<long long>> g(n); for (int i = 1; i < n; i++) { long long t1 = nxt() - 1, t2 = nxt() - 1; g[t1].push_back(t2); g[t2].push_back(t1); } map<long long, long long> mp; for (auto x : g) { if (x.size() == 2) { no(); } } ok(); return 0; }
#include <bits/stdc++.h> using namespace std; int n, a, b; vector<int> adj[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = (int)1; i <= (int)n - 1; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = (int)1; i <= (int)n; i++) if (adj[i].size() == 2) { cout << "NO"; return 0; } cout << "YES"; }
#include <bits/stdc++.h> using namespace std; const long long inff = 0x3f3f3f3f3f3f3f3f; int n, x, y, du[100008]; int main() { cin.tie(0); cout.tie(0); cin >> n; for (int i(1); i <= (n - 1); ++i) { scanf("%d", &x), scanf("%d", &y); du[x]++, du[y]++; } for (int i(1); i <= (n); ++i) if (du[i] == 2) return puts("NO"), 0; puts("YES"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, f = 0; cin >> n; vector<int> V[100008]; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; V[u].push_back(v); V[v].push_back(u); } for (int i = 0; i <= n; i++) { if (V[i].size() == 2) { f = 1; break; } } if (f) cout << "NO" << endl; else cout << "YES" << endl; }
#include <bits/stdc++.h> using namespace std; int cnt[100005]; int main() { int n; scanf("%d", &n); if (n == 2) { printf("YES\n"); return 0; } if (n == 3) { printf("NO\n"); return 0; } for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d%d", &u, &v); cnt[u]++; cnt[v]++; } for (int i = 1; i <= n; i++) { if (cnt[i] == 2) { printf("NO\n"); return 0; } } printf("YES\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<int> v[101010]; int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d %d", &a, &b); v[a].push_back(b); v[b].push_back(a); } for (int i = 1; i <= n; i++) if (v[i].size() == 2) return !printf("NO"); printf("YES"); }
#include <bits/stdc++.h> using namespace std; vector<int> g[100005]; int find(int v, int* parent) { if (parent[v] == v) { return v; } return find(parent[v], parent); } int vis[100005]; int dp[100005]; int dfs(int u) { vis[u] = 1; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (!vis[v]) dfs(v); } } int main() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++) { if (g[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
#include <bits/stdc++.h> int main() { long long in[100005]; long long n, i, j, m, t; memset(in, 0, sizeof(in)); scanf("%lld", &n); for (i = 1; i <= n - 1; i++) { long long u, v; scanf("%lld %lld", &u, &v); in[u]++; in[v]++; } long long num = 0; long long ans = 0; int flag = 1; for (i = 1; i <= n; i++) { if (in[i] == 1) num++; if (in[i] == 2) flag = 0; } num--; ans = (1 + num) * num / 2; if (ans >= n - 1 && flag) printf("YES\n"); else printf("NO\n"); return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<int>> adj; int main() { int n; cin >> n; adj = vector<vector<int>>(n + 1); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } string ans = "YES\n"; int flag = 1; for (int i = 1; i <= n; i++) { if (adj[i].size() == 2) { ans = "NO\n"; flag = 0; break; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int degree[100005]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; ++degree[u]; ++degree[v]; } for (int i = 1; i <= n; ++i) { if (degree[i] == 2) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
#include <bits/stdc++.h> using namespace std; const long long max_n = 1e6 + 20; long long n, m, k, ans, sum; long long a[max_n]; long long mark[max_n]; vector<long long> v, adj[max_n], jda[max_n]; void dfs(long long v) { mark[v] = 1; if (adj[v].size() == 2) ans = -1; for (auto i : adj[v]) { if (!mark[i]) { dfs(i); } } } int32_t main() { cin >> n; for (long long i = 1; i < n; i++) { long long u, v; cin >> u >> v; u--, v--; adj[v].push_back(u); adj[u].push_back(v); } if (n == 2) { cout << "YES"; return 0; } for (long long i = 0; i < n; i++) { if (adj[i].size() == 1 && !mark[i]) dfs(i); } if (ans == -1) { cout << "NO"; } else cout << "YES"; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int n, u, v; int a[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; memset(a, 0, sizeof(a)); for (int i = 0; i < n - 1; i++) { cin >> u >> v; a[u]++; a[v]++; } long long cnt[3] = {0, 0, 0}; for (int i = 1; i <= n; i++) { if (a[i] <= 2) cnt[a[i]]++; } if (cnt[2] == 0 && cnt[1] * (cnt[1] - 1) / 2 >= n - 1) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, cnt[100009]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; cnt[x]++, cnt[y]++; } for (int i = 1; i <= n; i++) if (cnt[i] == 2) return cout << "NO", 0; cout << "YES"; }
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 10; long long n, deg[maxn]; long long read() { long long 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; } void print(long long x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + '0'); } void write(long long x) { print(x); puts(""); } signed main() { n = read(); bool flag = 0; for (long long i = 2; i <= n; i++) deg[read()]++, deg[read()]++; for (long long i = 1; i <= n; i++) if (deg[i] == 2) { puts("NO"); flag = 1; break; } if (!flag) puts("YES"); return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> tree[500500]; long long num[500500], fine = 0, n; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 1; i < n; i++) { long long u, v; cin >> u >> v; tree[u].push_back(v); tree[v].push_back(u); } for (int i = 1; i <= n; i++) if (tree[i].size() == 2) return cout << "NO", 0; cout << "YES"; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; void solve() { int n, u, v; cin >> n; vector<int> Adj[n]; for (int i = 1; i < n; ++i) { cin >> u >> v; --u; --v; Adj[u].push_back(v); Adj[v].push_back(u); } for (int i = 0; i < n; ++i) { if (Adj[i].size() == 2) { cout << "NO\n"; return; } } cout << "YES\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const int maxn = 1e5 + 100; int n, x; int a[maxn]; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x; a[x]++; cin >> x; a[x]++; } for (int i = 1; i < n + 1; i++) { if (a[i] == 2) { cout << "No" << endl; return 0; } } cout << "YES" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int>> g(n); int f, t; for (int i = 0; i < n - 1; ++i) { cin >> f >> t; f--; t--; g[f].push_back(t); g[t].push_back(f); } for (int i = 0; i < n; ++i) { if (g[i].size() == 2) { cout << "NO\n"; return 0; } } cout << "YES\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int a[N]; int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); a[x]++; a[y]++; } for (int i = 1; i <= n; i++) if (a[i] == 2) { printf("NO\n"); return 0; } printf("YES\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 7; long long n; vector<long long> add[N]; void nie() { cout << "NO"; exit(0); } void yie() { cout << "YES"; exit(0); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); vector<pair<long long, long long>> v; cin >> n; for (long long i = 1; i < n; i++) { long long x, y; cin >> x >> y; add[x].push_back(y); add[y].push_back(x); v.push_back({x, y}); } if (n == 2) yie(); if (n == 3) nie(); for (auto i : v) { if (add[i.second].size() == 2 || add[i.first].size() == 2) nie(); } yie(); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("-O2") using namespace std; const int LIM = 1e5 + 5, MOD = 1e9 + 7; const long double EPS = 1e-9; vector<vector<int> > g; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; g.resize(n); for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); } for (int i = 0; i < n; ++i) { if (g[i].size() == 2) { cout << "NO" << '\n'; exit(0); } } cout << "YES" << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; void func() { int n; cin >> n; vector<int> adj[n + 1]; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; i++) { if (adj[i].size() == 1) continue; if (adj[i].size() < 3) { cout << "NO" << endl; return; } } cout << "YES" << endl; return; } int main() { std::ios::sync_with_stdio(false); int t = 1; while (t--) func(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAX = 1e5 + 10; long long arr[MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; arr[u]++; arr[v]++; } for (int i = 1; i <= n; i++) if (arr[i] == 2) return puts("NO"); puts("YES"); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int N = 3e5 + 5, M = 1e8 + 5, OO = 1000000; int T, n, m; int u, v; int deg[N]; int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { scanf("%d %d", &u, &v); ++deg[u], ++deg[v]; } for (int i = 1; i <= n; ++i) { if (deg[i] == 2) { printf("NO\n"); return 0; } } printf("YES\n"); }
#include <bits/stdc++.h> using namespace std; int a[100010]; int main() { int n, flag = 1; scanf("%d", &n); for (int i = 1, x, y; i < n; i++) { scanf("%d %d", &x, &y); a[x]++; a[y]++; } for (int i = 1; i <= n; i++) if (a[i] == 2) flag = 0; flag ? printf("YES") : printf("NO"); }
#include <bits/stdc++.h> using namespace std; int deg[100005]; int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); deg[u]++; deg[v]++; } for (int i = 1; i <= n; i++) { if (deg[i] == 2) { printf("NO\n"); return 0; } } printf("YES\n"); return 0; }
#include <bits/stdc++.h> using ll = int64_t; using namespace std; const int N = 1e6 + 5; int n; int a[N]; basic_string<int> g[N]; int32_t main() { cin.tie(0)->sync_with_stdio(0); cin >> n; for (int i = 1, u, v; i < n; i++) { cin >> u >> v; g[u] += v; g[v] += u; } for (int i = 1; i <= n; i++) { if (g[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; }
#include <bits/stdc++.h> using namespace std; int n; int n1, n2; int grau[100010]; vector<pair<int, int> > edges; int main() { scanf("%d", &n); for (int g = 0; g < n - 1; g++) { scanf("%d %d", &n1, &n2); grau[n1]++; grau[n2]++; } for (int g = 1; g <= n; g++) { if (grau[g] == 2) { printf("NO\n"); return 0; } } printf("YES\n"); }
#include <bits/stdc++.h> using namespace std; int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}; int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1}; int dx4[] = {1, 0, -1, 0}; int dy4[] = {0, 1, 0, -1}; template <class A, class B> ostream& operator<<(ostream& out, const pair<A, B>& a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream& operator<<(ostream& out, const vector<A>& a) { for (const A& it : a) out << it << " "; return out; } template <class A, class B> istream& operator>>(istream& in, pair<A, B>& a) { return in >> a.first >> a.second; } template <class A> istream& operator>>(istream& in, vector<A>& a) { for (A& i : a) in >> i; return in; } vector<int>* inputG(int n, int m) { vector<int>* edges = new vector<int>[n + 1]; for (int i = 0; i < m; i++) { int sv, ev; cin >> sv >> ev; edges[sv].push_back(ev); edges[ev].push_back(sv); } return edges; } void solve() { int n; cin >> n; vector<int>* edges = inputG(n, n - 1); for (int i = 1; i <= n; i++) { if (edges[i].size() == 2) { cout << "NO" << "\n"; ; return; } } cout << "YES" << "\n"; ; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int deg[200000]; int main() { int n; cin >> n; for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; deg[u]++; deg[v]++; } for (int i = 1; i <= n; ++i) { if (deg[i] == 2) { cout << "NO\n"; return 0; } } cout << "YES\n"; }
#include <bits/stdc++.h> using namespace std; int main() { int x, y, n; cin >> n; vector<int> v[100010], barg; for (int i = 0; i < n - 1; i++) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } for (int i = 1; i <= n; i++) { if (v[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1000, mod = 1e9 + 7; vector<int> vec[N]; long long bin_pow(int x, int y) { if (y == 0) return 1; if (y == 1) return x; if (y % 2 == 0) { long long z = bin_pow(x, y / 2); return (z * z) % mod; } return (bin_pow(x, y - 1) * x) % mod; } bool check(int x) {} int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; vec[x].push_back(y); vec[y].push_back(x); } int k = 0; for (int i = 1; i <= n; i++) { if (vec[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<long long> > g; void endProg() { cout << "NO"; exit(0); } void dfs(long long v, long long p) { long long l = 0; for (auto i : g[v]) { if (i == p) continue; if (g[i].size() == 1) { l++; continue; } dfs(i, v); } if (v == 0) { if (g[v].size() == 2) endProg(); } else { if (g[v].size() == 2) endProg(); } } int main() { long long n; cin >> n; g.resize(n); for (long long i = 0; i < n - 1; i++) { long long a, b; cin >> a >> b; g[a - 1].push_back(b - 1); g[b - 1].push_back(a - 1); } dfs(0, 0); cout << "YES"; return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = LONG_MAX; const long long arr = 1000000; bool comp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) return a.second < b.second; else return a.first < b.first; } vector<vector<long long> > g(arr); int main(void) { long long n; cin >> n; for (int i = 0; i < n - 1; i++) { long long l, r; cin >> l >> r; g[l].push_back(r); g[r].push_back(l); } for (long long i = 1; i <= n; i++) { if (g[i].size() == 2) return cout << "NO" << '\n', 0; } cout << "YES" << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int n, x, y, mx, idx; int deg[100010]; vector<int> edge[100010]; bool vis[100010]; bool can = true; void dfs(int u) { vis[u] = true; int sz_u = (int)edge[u].size(); if (u != idx && sz_u == 2) { can = false; return; } for (int i = 0; i < sz_u && can; ++i) { if (!vis[edge[u][i]]) dfs(edge[u][i]); } } int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { scanf("%d%d", &x, &y); edge[x].push_back(y); edge[y].push_back(x); deg[x]++; deg[y]++; if (deg[x] > deg[idx]) { idx = x; } if (deg[y] > deg[idx]) { idx = y; } } if (n == 2) { puts("YES"); } else if (n == 3) { puts("NO"); } else { dfs(idx); can ? puts("YES") : puts("NO"); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse4") const long double EPS = 1e-9; const long long INF = 1e9; const long long mod = 1e9 + 7; using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); long long n, m, k; long long q; vector<vector<long long>> vec; vector<bool> used; vector<long long> cnt; vector<long long> ans; vector<long long> pref; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; vec.resize(n); cnt.resize(n, 0); for (int i = 0; i < n - 1; ++i) { int v, to; cin >> v >> to; --v; --to; vec[v].push_back(to); vec[to].push_back(v); cnt[v]++; cnt[to]++; } int cntl = 0, cntnl = 0; for (int i = 0; i < n; ++i) { if (cnt[i] == 1) cntl++; else if (cnt[i] == 2) { cout << "NO"; return 0; } else { int cntal = 0; for (int j = 0; j < vec[i].size(); ++j) { if (cnt[vec[i][j]] == 1) { cntal++; } } if (cntal <= 1) { cntnl++; } } } if ((cntl == 2 && n > 2)) { cout << "NO"; } else { cout << "YES"; } return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { cin.sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long n; cin >> n; vector<vector<long long>> graph(n + 1); if (n == 2) { cout << "YES" << "\n"; return 0; } for (long long i = 0; i < n - 1; i++) { long long u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } for (long long i = 0; i < graph.size(); i++) { if (graph[i].size() == 1) continue; if (graph[i].size() == 2) { cout << "NO" << "\n"; return 0; } } cout << "YES" << "\n"; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 1; vector<long long> v[N]; int main() { long long n; cin >> n; for (int i = 0; i < n - 1; i++) { long long x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } for (int i = 1; i <= n; i++) { if (v[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; }
#include <bits/stdc++.h> using namespace std; using ll = long long; long mmid(long a, long b, long c) { return a < b ? (b < c ? b : max(a, c)) : (b > c ? b : min(a, c)); } ll modinv(ll a) { ll b = 1000000007, u = 1, v = 0, t; while (b) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return (u + 1000000007) % 1000000007; } ll moddevide(ll a, ll b) { return (a * modinv(b)) % 1000000007; } ll modncr(ll n, ll r) { ll i, plus = 1; for (i = 0; i < r; i++) { plus = (plus * (n - i)) % 1000000007; plus = moddevide(plus, i + 1); } return plus; } ll euclidean_gcd(ll a, ll b) { if (a < b) return euclidean_gcd(b, a); ll r; while ((r = a % b)) { a = b; b = r; } return b; } int main() { int i, N, K, a, b, ch, now; long t; cin >> N; vector<vector<int>> v(N); for (i = 0; i < N - 1; i++) { cin >> a >> b; a--; b--; v[a].push_back(b); v[b].push_back(a); } for (i = 0; i < N; i++) { K = v[i].size(); if (K == 2) { printf("NO\n"); return 0; } } printf("YES\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int main() { ios::sync_with_stdio(0), cin.tie(0); long long int n; cin >> n; vector<long long int> v[n + 1]; for (long long int i = 0; i < n - 1; i++) { long long int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } for (long long int i = 0; i <= n; i++) { if (v[i].size() == 2) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; }
#include <bits/stdc++.h> long long mod = 998244353; using namespace std; const long long N = 1e5 + 5; vector<long long> g[N]; long long n; void fail() { cout << "NO\n"; exit(0); } long long cnt = 1; void dfs(long long node, long long p = 0) { if (g[node].size() == 2) cnt = 0; for (long long nx : g[node]) { if (nx == p) continue; dfs(nx, node); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; if (n == 2) { cout << "YES\n"; return 0; } if (n == 3) fail(); for (long long i = 1, u, v; i < n; i++) { cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1); if (cnt) cout << "YES"; else cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = 998244353; bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) { if (a.first >= b.first) return false; return true; } int main() { long long n, i = 0, j, k, m, q, l, r = 0, w, x, y, z = 0; string t, s; cin >> n; vector<vector<long long>> a(n); for (int i = 0; i < int(n - 1); i++) { cin >> x >> y; a[x - 1].push_back(y); a[y - 1].push_back(x); } for (int i = 0; i < int(n); i++) { if (int(a[i].size()) == 2) z = 1; } if (z == 1) cout << "NO"; else cout << "YES"; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<int> d(n + 1); for (int i = 1; i <= n - 1; ++i) { int u, v; cin >> u >> v; d[u]++, d[v]++; } bool ok = 1; for (int i = 1; i <= n; ++i) { if (d[i] == 2) { ok = 0; break; } } if (!ok) { cout << "no" << "\n"; } else { cout << "yes" << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } int main() { long long n; cin >> n; vector<long long> a[100001]; for (long long i = 0; i < n - 1; i++) { long long x, y; cin >> x >> y; a[x].push_back(y); a[y].push_back(x); } for (long long i = 1; i <= n; i++) { if (a[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; }
#include <bits/stdc++.h> using namespace std; void fast(string name = "") { ios_base::sync_with_stdio(0); cin.tie(0); if ((int)name.size() > 0) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } } const int N = 1e5; int deg[N]; int n; void solve() { cin >> n; int a, b; fill(deg, deg + n + 1, 0); for (int i = 1; i < n; ++i) { cin >> a >> b; deg[a]++; deg[b]++; } for (int i = 1; i <= n; ++i) { if (deg[i] == 2) { cout << "NO\n"; return; } } cout << "YES\n"; } int main() { fast(""); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long bigmod(long long b, long long p, long long md) { if (p == 0) return 1; if (p % 2 == 1) { return ((b % md) * bigmod(b, p - 1, md)) % md; } else { long long y = bigmod(b, p / 2, md); return (y * y) % md; } } int deg[100005]; int main() { int n; cin >> n; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; deg[a]++; deg[b]++; } for (int i = 1; i <= n; i++) { if (deg[i] == 2) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 500010; int n, m, d[N]; inline int read() { int sym = 0, res = 0; char ch = 0; while (!isdigit(ch)) sym |= (ch == '-'), ch = getchar(); while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar(); return sym ? -res : res; } void file() { freopen("read.in", "r", stdin); freopen("write.out", "w", stdout); } int main() { n = read(); for (int i = 1; i <= n - 1; i++) { int x = read(), y = read(); d[x]++, d[y]++; } for (int i = 1; i <= n; i++) { if (d[i] == 2) { printf("NO"); return 0; } } printf("YES"); return 0; }
#include <bits/stdc++.h> using namespace std; long long ar[100005], n, flag; int main() { cin >> n; for (int i = 0; i < n - 1; i++) { long long l, r; cin >> l >> r; ar[l]++; ar[r]++; } for (int i = 0; i < n + 1; i++) if (ar[i] == 2) flag = 1; if (!flag) cout << "YES"; else cout << "NO"; }
#include <bits/stdc++.h> using namespace std; bool b2(const pair<pair<int, int>, int> &p1, const pair<pair<int, int>, int> &p2) { return p1.first.first > p2.first.first; } bool b1(const pair<pair<int, int>, int> &p1, const pair<pair<int, int>, int> &p2) { return p1.first.second < p2.first.second; } bool bbk(vector<pair<int, int>> &v1, vector<pair<int, int>> &v2) { return v1.size() < v2.size(); } int n; vector<int> v[100005]; bool check = true; void solve() { for (long long int i = 1; i <= n; i++) { if (v[i].size() == 2) { check = false; break; } } } int main() { cin >> n; int a, b; for (long long int i = 1; i <= n - 1; i++) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } solve(); if (check) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
#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]; void solve() { long long int n; cin >> n; long long int a, b; for (long long int i = 1; i < n; ++i) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } for (long long int i = 1; i <= n; ++i) { if (v[i].size() == 2ll) { cout << "NO"; return; } } cout << "YES"; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n + 1); int u, v; for (int i = 0; i < n - 1; i++) { cin >> u >> v; a[u]++; a[v]++; } bool check = false; for (int i = 1; i <= n; i++) if (a[i] == 2) check = true; if (check) cout << "NO"; else cout << "YES"; }
#include <bits/stdc++.h> using namespace std; vector<int> v[100005]; int main() { int t, q, n, m, x, y, i, j, k; scanf("%d", &n); for (i = 1; i < n; i++) { scanf("%d%d", &x, &y); v[x].push_back(y); v[y].push_back(x); } int f = 0; for (i = 1; i <= n; i++) { if (v[i].size() == 2) { f = 1; break; } } if (f == 0) printf("YES\n"); else printf("NO\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a, b; vector<vector<int>> A(n); for (int i = 0; i < (n - 1); i++) { cin >> a >> b; A[a - 1].push_back(b - 1); A[b - 1].push_back(a - 1); } if (n <= 2) { cout << "YES" << endl; return 0; } bool flag = false; for (int i = 0; i < A.size(); i++) { if (A[i].size() != 1 && A[i].size() < 3) { flag = true; } } if (flag == false) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
#include <bits/stdc++.h> using namespace std; vector<int> N[100005]; void addE(int a, int b) { N[a].push_back(b); N[b].push_back(a); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, a, b; cin >> n; for (int i = 1; i < n; i++) { cin >> a >> b; addE(a - 1, b - 1); } int sz; for (int i = 0; i < n; i++) { sz = N[i].size(); if (sz == 2) { cout << "NO\n"; return 0; } } cout << "YES\n"; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { std::cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); std::cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const int fx[] = {+1, -1, +0, +0}; const int fy[] = {+0, +0, +1, -1}; int ans = 0; void nishantwrp(int cas) { int n; cin >> n; vector<int> nodes[n + 1]; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; nodes[a].push_back(b); nodes[b].push_back(a); } for (int i = 0; i < n; i++) { if ((int)nodes[i + 1].size() == 2) { cout << "NO"; return; } } cout << "YES"; } signed main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; int cas = 1; while (cas <= t) { nishantwrp(cas); cas++; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); srand(time(0)); solve(); return 0; } long long max(long long a, long long b) { if (a > b) return a; return b; } long long min(long long a, long long b) { if (a < b) return a; return b; } long long n; vector<vector<long long> > a(200001); void solve() { cin >> n; for (long long i = 0; i < n - 1; i++) { long long u, v; cin >> u >> v; u--, v--; a[u].push_back(v); a[v].push_back(u); } if (n == 2) { cout << "YES"; return; } for (long long i = 0; i < n; i++) { if (a[i].size() == 2) { cout << "NO"; return; } } cout << "YES"; }
#include <bits/stdc++.h> using namespace std; vector<vector<long long int>> graph; int solve() { long long int n; cin >> n; graph.resize(n + 1); n--; while (n--) { long long int a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } long long int flag = 0; for (auto adj : graph) { if (adj.size() == 2) flag = 1; } if (flag) cout << "NO\n"; else cout << "YES\n"; return 0; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<int> G[100005]; int main() { scanf("%d", &n); for (int i = 1; i <= n - 1; i++) { int u, v; scanf("%d %d", &u, &v); G[u].push_back(v); G[v].push_back(u); } for (int i = 1; i <= n; i++) if (G[i].size() == 2) { printf("NO\n"); return 0; } printf("YES\n"); return 0; }
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename S37, typename... Args> void err(istream_iterator<string> it, S37 a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } const long long N = 200010, mod = 1e9 + 7, mod2 = 1e9 + 9, mod3 = 998244353, sq = 450, base = 37, lg = 25, inf = 1e18 + 10, del = 67733; long long n, m, x, y, w, z, X, Y, Z, t, k, ans, a[N]; vector<long long> v[N]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x >> y; v[x].push_back(y); a[x]++; a[y]++; v[y].push_back(x); } for (int i = 1; i <= n; i++) { if (a[i] == 2) return cout << "NO", 0; } cout << "YES"; return 0; }
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } const int N = 1e5 + 7; const int xinc[] = {0, 0, 1, -1}; const int yinc[] = {1, -1, 0, 0}; void solve() { long long n, i, a, b; cin >> n; ; std::vector<long long> adj[n + 1]; std::vector<bool> visited(n + 1, false); queue<long long> q; for (i = 0; i < n - 1; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } q.push(1); visited[1] = true; while (!q.empty()) { long long curr = q.front(); q.pop(); long long deg = adj[curr].size(); if (!(deg == 1 || deg > 2)) { cout << "NO"; return; } for (auto nei : adj[curr]) { if (!visited[nei]) { q.push(nei); visited[nei] = true; } } } cout << "YES"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using namespace std; vector<int> conn[200100]; bool oka = true; void dfs(int u, int par) { if (par == -1 && conn[u].size() == 1) ; else { if (conn[u].size() == 1) return; if (conn[u].size() < 3 || oka == false) { oka = false; return; } } for (int v : conn[u]) { if (v == par) continue; dfs(v, u); } } int main() { int n; cin >> n; int u, v; for (int i = 1; i <= n - 1; i++) { cin >> u >> v; conn[u].push_back(v); conn[v].push_back(u); } dfs(1, -1); if (oka) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 10; int n; vector<int> vs[N]; bool dfs(int node, int par) { if (par != -1 && vs[node].size() == 1) { return false; } int children = 0; bool bad = false; for (int v : vs[node]) if (v != par) { ++children; bad |= dfs(v, node); } if (par != -1) bad |= children == 1; return bad; } void solve() { cin >> n; for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v, --u, --v; vs[u].push_back(v); vs[v].push_back(u); } int cnt = 0; bool bad = false; for (int i = 0; i < n && cnt < 2; ++i) { if (vs[i].size() == 1) { ++cnt; bad |= dfs(i, -1); } } cout << (bad ? "NO" : "YES") << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout.precision(20); cout << fixed; solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; long long mod = 1000000007; const int MX = 0x3f3f3f; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, i, u, v; cin >> n; vector<int> adj[n]; for (i = 0; i <= n - 2; i++) { cin >> u >> v; adj[u - 1].emplace_back(v - 1); adj[v - 1].emplace_back(u - 1); } if (n == 2) { cout << "YES"; return 0; } for (i = 0; i <= n - 1; i++) { if (adj[i].size() == 1) continue; if (adj[i].size() < 3) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int de[maxn]; int main() { ios::sync_with_stdio(false); int n; cin >> n; memset(de, 0, sizeof de); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; de[u]++; de[v]++; } bool flag = true; for (int i = 1; i <= n; i++) { if (de[i] == 2) { flag = false; break; } } if (flag) puts("YES"); else puts("NO"); }
#include <bits/stdc++.h> using namespace std; int const maxn = 1e5 + 5; vector<int> p[maxn]; int main() { int n; scanf("%d", &n); int u, v; for (int i = 1; i < n; i++) { scanf("%d%d", &u, &v); p[u].push_back(v); p[v].push_back(u); } bool flag = 1; for (int i = 1; i <= n; i++) if (p[i].size() == 2) flag = 0; if (flag) printf("YES\n"); else printf("NO\n"); }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, i, flag = 0, x, y; cin >> n; long long int a[200005]; for (i = 1; i <= n; i++) { a[i] = 0; } for (i = 1; i < n; i++) { cin >> x >> y; a[x]++; a[y]++; } for (i = 1; i <= n; i++) { if (a[i] == 2) { flag = 1; } } if (flag == 0) { cout << "YES"; } else { cout << "NO"; } }
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 7; int d[N]; int main() { int n; scanf("%d", &n); for (int i = 1, u, v; i < n; i++) { scanf("%d%d", &u, &v); d[u]++, d[v]++; } bool flag = true; for (int i = 1; i <= n; i++) { if (d[i] == 2) flag = false; } if (flag) printf("YES\n"); else printf("NO\n"); }
#include <bits/stdc++.h> using namespace std; const int limit = 1000000; int n; vector<int> g[limit]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int u = 1; u <= n; u++) { vector<int> &ar = g[u]; if (int(ar.size()) == 2) { cout << "NO" << endl; exit(0); } } cout << "YES" << endl; }
#include <bits/stdc++.h> const double eps = 1e-7; using namespace std; int n; int deg[100010]; int main() { while (~scanf("%d", &n)) { int a = 0, b = 0; memset(deg, 0, sizeof(deg)); for (int i = 0; i < n - 1; ++i) { scanf("%d%d", &a, &b); deg[a] += 1; deg[b] += 1; } bool isOk = 1; for (int i = 1; i <= n; ++i) { if (deg[i] == 2) { isOk = 0; break; } } printf("%s\n", isOk ? "YES" : "NO"); } return 0; }
#include <bits/stdc++.h> using namespace std; int a[100006]; int main(void) { int n, x, y; cin >> n; for (int i = 1; i < n; i++) cin >> x >> y, a[x]++, a[y]++; for (int i = 1; i <= n; i++) if (a[i] == 2) { cout << "NO"; return 0; } cout << "YES"; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; unordered_map<int, int> m; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; m[a]++; m[b]++; } for (int i = 1; i <= n; i++) { if (m[i] == 2) { cout << "NO"; return 0; } } cout << "YES"; }
#include <bits/stdc++.h> using namespace std; const long long maxN = 2 * 100224; struct BIT { long long data[maxN] = {0}; void update(long long idx, long long val) { while (idx < maxN) { data[idx] += val; idx += idx & -idx; } } void update(long long l, long long r, long long val) { update(l, val); update(r + 1, -val); } long long query(long long idx) { long long res = 0; while (idx > 0) { res += data[idx]; idx -= idx & -idx; } return res; } long long query(long long l, long long r) { return query(r) - query(l); } }; struct LazyBIT { BIT bitAdd, bitSub; void update(long long l, long long r, long long val) { bitAdd.update(l, r, val); bitSub.update(l, r, (l - 1) * val); bitSub.update(r + 1, (-r + l - 1) * val); } long long query(long long idx) { return idx * bitAdd.query(idx) - bitSub.query(idx); } long long query(long long l, long long r) { return query(r) - query(l - 1); } }; long long parent[maxN]; long long rnk[maxN]; long long lfmost[maxN]; long long rtmost[maxN]; long long vis[maxN]; void make_set(long long v) { parent[v] = v; rnk[v] = 0; lfmost[v] = v; rtmost[v] = v; } long long find_set(long long v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(long long a, long long b) { a = find_set(a); b = find_set(b); if (a != b) { if (rnk[a] < rnk[b]) swap(a, b); parent[b] = a; lfmost[a] = min(lfmost[a], lfmost[b]); rtmost[a] = max(rtmost[a], rtmost[b]); if (rnk[a] == rnk[b]) rnk[a]++; } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long n; cin >> n; vector<long long> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } long long sum = 0; LazyBIT B; for (long long i = 1; i <= n; i++) { B.update(i, i, sum); sum += i; } vector<long long> haha; for (int i = n - 1; i >= 0; i--) { long long lo = 1; long long hi = n; long long mid; long long ans; long long val; long long temp; while (lo <= hi) { mid = (lo + hi) / 2; if (vis[mid] == 1) { if (lfmost[find_set(mid)] - 1 >= lo) mid = lfmost[find_set(mid)] - 1; else if (rtmost[find_set(mid)] + 1 <= hi) mid = rtmost[find_set(mid)] + 1; else break; } val = B.query(mid, mid); if (val == v[i]) { ans = mid; hi = mid - 1; } else if (v[i] > val) { lo = mid + 1; } else if (v[i] < val) { hi = mid - 1; } } B.update(ans, n, -ans); vis[ans] = 1; make_set(ans); if (vis[ans] == 1 && vis[ans + 1] == 1) union_sets(ans, ans + 1); if (vis[ans] == 1 && vis[ans - 1] == 1) union_sets(ans, ans - 1); haha.push_back(ans); } reverse(haha.begin(), haha.end()); for (auto u : haha) { cout << u << " "; } }
#include <bits/stdc++.h> using namespace std; int const maxn = 1e6 + 5; long long tree[4 * maxn], lazy[4 * maxn], a[maxn]; int kq[maxn]; int n; void down(int g) { if (lazy[g] != 0) { tree[g << 1] += lazy[g]; lazy[g << 1] += lazy[g]; tree[(g << 1) | 1] += lazy[g]; lazy[(g << 1) | 1] += lazy[g]; lazy[g] = 0; } } void buld(int g, int l, int r) { if (l == r) { tree[g] = a[l]; } else { int m = (l + r) >> 1; buld(g << 1, l, m); buld((g << 1) | 1, m + 1, r); tree[g] = min(tree[g << 1], tree[(g << 1) | 1]); } } void update(int g, int l, int r, int u, int v, long long val) { if (r < u || v < l) return; if (u <= l && r <= v) { tree[g] += val; lazy[g] += val; } else { down(g); int m = (l + r) >> 1; update(g << 1, l, m, u, v, val); update((g << 1) | 1, m + 1, r, u, v, val); tree[g] = min(tree[g << 1], tree[(g << 1) | 1]); } } int find_zero(int g, int l, int r) { if (l == r) return l; else { down(g); int m = (l + r) >> 1; int res = -1; if (tree[(g << 1) | 1] == 0) res = find_zero((g << 1) | 1, m + 1, r); else res = find_zero(g << 1, l, m); tree[g] = min(tree[g << 1], tree[(g << 1) | 1]); return res; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; buld(1, 1, n); for (int i = 1; i <= n; i++) { int pos = find_zero(1, 1, n); kq[pos] = i; update(1, 1, n, pos, pos, 1e18); update(1, 1, n, pos + 1, n, -i); } for (int i = 1; i <= n; i++) cout << kq[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, a[500001], f[500001], ans[500001], mn; int lowbit(int x) { return x & (-x); } void ins(long long x, long long w) { long long i; for (i = x; i <= n; i += lowbit(i)) f[i] += w; } long long query(long long x) { long long xlh = 0, i; for (i = x; i; i -= lowbit(i)) xlh += f[i]; return xlh; } int main() { long long i, l, r, mid; scanf("%lld", &n); for (i = 1; i <= n; i++) scanf("%lld", &a[i]); for (i = 1; i <= n; i++) ins(i, i); for (i = n; i; i--) { l = 1; r = n; mn = 1; while (l <= r) { mid = (l + r) / 2; if (query(mid - 1) <= a[i]) mn = mid, l = mid + 1; else r = mid - 1; } ins(mn, -mn); ans[i] = mn; } for (i = 1; i <= n; i++) printf("%lld ", ans[i]); }
#include <bits/stdc++.h> using namespace std; long long int ar[4 * 200005]; void update(int a, int b, int l, int h, int p) { if (a < l || a > h) return; if (l == h) { ar[p] += b; return; } int m = (l + h) / 2; update(a, b, l, m, 2 * p); update(a, b, m + 1, h, 2 * p + 1); ar[p] += b; return; } int query(int l, int h, int p, long long int s) { if (l == h) return l; int m = (l + h) / 2; if (ar[2 * p] > s) return query(l, m, 2 * p, s); else return query(m + 1, h, 2 * p + 1, s - ar[2 * p]); } int compute(long long int s) { int x; s *= 2; x = sqrt(s); return x + 1; } int main() { int i, n; scanf("%d", &n); vector<long long int> sum(n); for (i = 0; i < n; i++) cin >> sum[i]; vector<int> ans(n); ans[n - 1] = compute(sum[n - 1]); for (i = 1; i < n + 1; i++) update(i, i, 0, n, 1); update(ans[n - 1], -ans[n - 1], 0, n, 1); for (i = n - 2; i >= 0; i--) { ans[i] = query(0, n, 1, sum[i]); update(ans[i], -ans[i], 0, n, 1); } for (i = 0; i < n; i++) printf("%d ", ans[i]); printf("\n"); }
#include <bits/stdc++.h> using namespace std; const long long MAX = 200100; pair<long long, long long> tree[256 * 1024 * 2]; long long lazy[256 * 1024 * 2]; void updateRangeUtil(long long si, long long ss, long long se, long long us, long long ue, long long diff) { if (lazy[si] != 0) { tree[si].first += lazy[si]; if (ss != se) { lazy[si * 2 + 1] += lazy[si]; lazy[si * 2 + 2] += lazy[si]; } lazy[si] = 0; } if (ss > se || ss > ue || se < us) return; if (ss >= us && se <= ue) { tree[si].first += diff; if (ss != se) { lazy[si * 2 + 1] += diff; lazy[si * 2 + 2] += diff; } return; } long long mid = (ss + se) / 2; updateRangeUtil(si * 2 + 1, ss, mid, us, ue, diff); updateRangeUtil(si * 2 + 2, mid + 1, se, us, ue, diff); tree[si] = min(tree[si * 2 + 1], tree[si * 2 + 2]); } void updateRange(long long n, long long us, long long ue, long long diff) { updateRangeUtil(0, 0, n - 1, us, ue, diff); } void updateRangeUtil2(long long si, long long ss, long long se, long long us, long long ue, long long diff) { if (ss > se || ss > ue || se < us) return; if (ss >= us && se <= ue) { tree[si].second += diff; return; } long long mid = (ss + se) / 2; updateRangeUtil2(si * 2 + 1, ss, mid, us, ue, diff); updateRangeUtil2(si * 2 + 2, mid + 1, se, us, ue, diff); tree[si] = min(tree[si * 2 + 1], tree[si * 2 + 2]); } void updateRange2(long long n, long long us, long long ue, long long diff) { updateRangeUtil2(0, 0, n - 1, us, ue, diff); } int main() { long long arr[MAX]; long long n; cin >> n; for (long long i = 0; i < n; ++i) { cin >> arr[i]; } reverse(arr, arr + n); for (long long i = 0; i < n; ++i) { updateRange2(n, i, i, i); } for (long long i = 0; i < n; ++i) { updateRange(n, i, i, arr[i]); } long long res[MAX]; for (long long iter = 1; iter <= n; ++iter) { long long smallest = tree[0].second; updateRange(n, 0, smallest, -iter); updateRange(n, smallest, smallest, 1LL << 62); res[smallest] = iter; } reverse(res, res + n); for (long long i = 0; i < n; ++i) { if (i) cout << ' '; cout << res[i]; } cout << endl; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; const int maxm = 4e6 + 10; const long long mod = 1e9 + 7; const int inf = 0x3ffffff; const double eps = 1e-5; long long n, s[maxn]; long long a[maxn], ans[maxn << 2], tag[maxn << 2], m; long long res[maxn]; inline void push_up(long long p) { ans[p] = ans[p << 1] + ans[p << 1 | 1]; } inline void f(long long p, long long l, long long r, long long k) { tag[p] += k; ans[p] += (r - l + 1) * k; } void build(long long l, long long r, long long p) { if (l == r) { ans[p] = a[l]; return; } long long mid = (l + r) >> 1; build(l, mid, p << 1); build(mid + 1, r, p << 1 | 1); push_up(p); } inline void push_down(long long p, long long l, long long r) { long long mid = (l + r) >> 1; f(p << 1, l, mid, tag[p]); f(p << 1 | 1, mid + 1, r, tag[p]); tag[p] = 0; } inline void update(long long nl, long long nr, long long l, long long r, long long p, long long k) { if (nl <= l && r <= nr) { ans[p] += k * (r - l + 1); tag[p] += k; return; } push_down(p, l, r); long long mid = (l + r) >> 1; if (nl <= mid) update(nl, nr, l, mid, p << 1, k); if (nr > mid) update(nl, nr, mid + 1, r, p << 1 | 1, k); push_up(p); } long long query(long long q_x, long long q_y, long long l, long long r, long long p) { long long res = 0; if (q_x <= l && q_y >= r) return ans[p]; long long mid = (l + r) >> 1; push_down(p, l, r); if (q_x <= mid) res += query(q_x, q_y, l, mid, p << 1); if (q_y > mid) res += query(q_x, q_y, mid + 1, r, p << 1 | 1); return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; a[i] = i; } build(1, n, 1); for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l < r) { int mid = (l + r) >> 1; if (query(1, mid, 1, n, 1) > s[i]) r = mid; else l = mid + 1; } update(l, l, 1, n, 1, -l); res[i] = l; } for (int i = 1; i <= n; i++) cout << res[i] << " "; return ~~(0 - 0); }
#include <bits/stdc++.h> using namespace std; using ll = long long; ll P[200001]; ll sum[200001]; ll fen[200001]; int ans[200001]; void build() { for (int i = 1; i <= 200000; i++) { sum[i] = sum[i - 1] + ll(i - 1); fen[i] = sum[i] - sum[i - (i & -i)]; } } ll query(int l, int r) { if (l != 1LL) return query(1, r) - query(1, l - 1); ll ans = 0LL; while (r > 0) { ans += fen[r]; r -= r & -r; } return ans; } void update(int i) { ll taking = ll(i); i++; while (i <= 200000) { fen[i] -= taking; i += (i & -i); } } int find(ll s) { ll cur = 0LL; int ind = 0; for (int b = 17; b >= 0; b--) { if (ind + (1 << b) <= 200000 && cur + fen[ind + (1 << b)] <= s) { cur += fen[ind + (1 << b)]; ind += (1 << b); } } return ind; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> P[i]; build(); for (int i = n; i >= 1; i--) { ans[i] = find(P[i]); update(ans[i]); } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } cout << endl; }
#include <bits/stdc++.h> using namespace std; long long read() { char ch = getchar(); long long x = 0, ff = 1; while (ch < '0' || ch > '9') { if (ch == '-') ff = -ff; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * ff; } void write(long long aa) { if (aa < 0) putchar('-'), aa = -aa; if (aa > 9) write(aa / 10); putchar(aa % 10 + '0'); return; } long long n; long long a[200005]; long long tt[800005], tts[800005], lz[800005]; void up(long long rt) { tt[rt] = min(tt[rt << 1], tt[rt << 1 | 1]); if (tt[rt << 1] >= tt[rt << 1 | 1]) tts[rt] = tts[rt << 1 | 1]; else tts[rt] = tts[rt << 1]; return; } void bt(long long rt, long long ll, long long rr) { if (ll == rr) { tt[rt] = read(), tts[rt] = ll; return; } long long mid = (ll + rr) >> 1; bt(rt << 1, ll, mid); bt(rt << 1 | 1, mid + 1, rr); up(rt); return; } void push(long long rt) { if (!lz[rt]) return; tt[rt << 1] += lz[rt]; tt[rt << 1 | 1] += lz[rt]; lz[rt << 1] += lz[rt]; lz[rt << 1 | 1] += lz[rt]; lz[rt] = 0; return; } void update(long long rt, long long ll, long long rr, long long L, long long R, long long kk) { if (ll == L && rr == R) { tt[rt] += kk; lz[rt] += kk; return; } long long mid = (ll + rr) >> 1; push(rt); if (R <= mid) update(rt << 1, ll, mid, L, R, kk); else if (L > mid) update(rt << 1 | 1, mid + 1, rr, L, R, kk); else update(rt << 1, ll, mid, L, mid, kk), update(rt << 1 | 1, mid + 1, rr, mid + 1, R, kk); up(rt); return; } int main() { n = read(); bt(1, 1, n); for (long long i = 1; i <= n; ++i) { long long now = tts[1]; a[now] = i; update(1, 1, n, now, now, n * n); if (now < n) update(1, 1, n, now + 1, n, -i); } for (long long i = 1; i <= n; ++i, putchar(' ')) write(a[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 5; int p[M << 2], res[M]; long long V[M << 2], tag[M << 2]; void push_up(int o) { if (V[o << 1] < V[o << 1 | 1]) { V[o] = V[o << 1]; p[o] = p[o << 1]; } else { V[o] = V[o << 1 | 1]; p[o] = p[o << 1 | 1]; } } void push_down(int o) { tag[o << 1] += tag[o]; tag[o << 1 | 1] += tag[o]; V[o << 1] += tag[o]; V[o << 1 | 1] += tag[o]; tag[o] = 0; } void build(int o, int l, int r) { if (l == r) { scanf("%lld", &V[o]); p[o] = l; return; } int mid = (l + r) / 2; build(o << 1, l, mid); build(o << 1 | 1, mid + 1, r); push_up(o); } void upd(int o, int l, int r, int L, int R, long long x) { if (L > R) return; if (L <= l && r <= R) { tag[o] += x; V[o] += x; return; } int mid = (l + r) / 2; push_down(o); if (L <= mid) upd(o << 1, l, mid, L, R, x); if (R > mid) upd(o << 1 | 1, mid + 1, r, L, R, x); push_up(o); } int main() { int n; scanf("%d", &n); build(1, 1, n); for (int i = 1; i <= n; i++) { int pos = p[1]; res[pos] = i; upd(1, 1, n, pos, pos, 1e11); upd(1, 1, n, pos + 1, n, -i); } for (int i = 1; i <= n; i++) { printf("%d ", res[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int ans[maxn]; int n; long long a[maxn]; const int maxnode = maxn << 2; long long sum[maxnode], add[maxnode]; struct Node { int l, r; int mid() { return (l + r) >> 1; } } node[maxnode]; void Pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } void Pushdown(int rt, int m) { if (add[rt]) { add[rt << 1] += add[rt]; add[rt << 1 | 1] += add[rt]; sum[rt << 1] += add[rt] * (m - (m >> 1)); sum[rt << 1 | 1] += add[rt] * (m >> 1); add[rt] = 0; } } void build(int l, int r, int rt) { node[rt].l = l; node[rt].r = r; add[rt] = 0; if (l == r) { sum[rt] = (long long)l; return; } int m = node[rt].mid(); build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); Pushup(rt); } void update(int c, int l, int r, int rt) { if (node[rt].l == l && node[rt].r == r) { add[rt] += c; sum[rt] += (long long)c * (r - l + 1); return; } if (node[rt].l == node[rt].r) return; Pushdown(rt, node[rt].r - node[rt].l + 1); int m = node[rt].mid(); if (r <= m) update(c, l, r, rt << 1); else if (l > m) update(c, l, r, rt << 1 | 1); else { update(c, l, m, rt << 1); update(c, m + 1, r, rt << 1 | 1); } Pushup(rt); } int res; void query(int l, int r, int rt, long long x) { if (l == r) { res = l; return; } int m = node[rt].mid(); Pushdown(rt, m); if (sum[rt << 1] > x && sum[rt << 1] > 0) query(l, m, rt << 1, x); else query(m + 1, r, rt << 1 | 1, x - sum[rt << 1]); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); build(1, n, 1); for (int i = n; i; i--) { query(1, n, 1, a[i]); ans[i] = res; update(-res, res, res, 1); } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 10; const long long INF = 0x3f3f3f3f; const long long LLINF = 0x3f3f3f3f3f3f3f3f; const long long MOD = 1e9 + 7; long long segtree[MAXN << 2]; long long lazy[MAXN << 2]; long long arr[MAXN]; long long ans[MAXN]; void pushup(long long rt) { segtree[rt] = min(segtree[rt << 1], segtree[rt << 1 | 1]); } void pushdown(long long rt) { if (lazy[rt]) { segtree[rt << 1] = max(0ll, segtree[rt << 1] - lazy[rt]); segtree[rt << 1 | 1] = max(0ll, segtree[rt << 1 | 1] - lazy[rt]); lazy[rt << 1] += lazy[rt]; lazy[rt << 1 | 1] += lazy[rt]; lazy[rt] = 0; } } void build(long long l, long long r, long long rt) { lazy[rt] = 0; if (l == r) { segtree[rt] = arr[l]; return; } long long m = l + r >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); pushup(rt); } long long query(long long l, long long r, long long rt) { if (l == r) { return l; } long long m = l + r >> 1; pushdown(rt); if (!segtree[rt << 1 | 1]) return query(m + 1, r, rt << 1 | 1); else return query(l, m, rt << 1); } void update(long long l, long long r, long long L, long long R, long long C, long long rt) { if (L <= l && r <= R) { segtree[rt] = max(0ll, segtree[rt] - C); lazy[rt] += C; return; } long long m = l + r >> 1; pushdown(rt); if (L <= m) update(l, m, L, R, C, rt << 1); if (R > m) update(m + 1, r, L, R, C, rt << 1 | 1); pushup(rt); } void update(long long l, long long r, long long pos, long long rt) { if (l == r && l == pos) { segtree[rt] = LLINF; return; } long long m = l + r >> 1; pushdown(rt); if (pos <= m) update(l, m, pos, rt << 1); else update(m + 1, r, pos, rt << 1 | 1); pushup(rt); } signed main() { long long n; scanf("%lld", &n); for (long long i = 1; i <= n; ++i) { scanf("%lld", &arr[i]); } build(1, n, 1); long long now = 0; for (long long i = n; i > 0; --i) { long long index = query(1, n, 1); ans[index] = ++now; if (index + 1 <= n) update(1, n, index + 1, n, now, 1); update(1, n, index, 1); } for (long long i = 1; i <= n; ++i) printf("%lld ", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; long long b[200005]; int n; long long sum(long long x) { if (x == 0) { return 0; } long long pas = 0; while (x > 0) { pas += b[x]; x = x & (x - 1); } return pas; } void update(long long x, long long i) { while (i <= n) { b[i] += x; i += (i & (-i)); } } int main() { cin >> n; long long s[n + 1]; s[0] = 0; int p[n + 1]; p[0] = 0; for (int i = 1; i <= n; i++) { cin >> s[i]; update(i, i); } int mid = 0; for (int i = n; i > 0; i--) { int l = 1; int r = n; while (l != r) { mid = (l + r + 1) / 2; if (sum(mid - 1) <= s[i]) { l = mid; } else { r = mid - 1; } } update(-l, l); p[i] = l; } for (int i = 1; i <= n; i++) { cout << p[i] << " "; } }
#include <bits/stdc++.h> using namespace std; const int N = 200010; int n, p[N]; long long a[N], tr[N << 2]; void build(int k, int l, int r) { if (l == r) { tr[k] = l; return; } int mid = l + r >> 1; build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r); tr[k] = tr[k << 1] + tr[k << 1 | 1]; } void add(int k, int l, int r, int v) { if (l == r) { tr[k] = 0; return; } int mid = l + r >> 1; if (v <= mid) add(k << 1, l, mid, v); else add(k << 1 | 1, mid + 1, r, v); tr[k] = tr[k << 1] + tr[k << 1 | 1]; } int query(int k, int l, int r, long long v) { if (l == r) { return l - 1; } int mid = l + r >> 1; if (tr[k << 1] > v) return query(k << 1, l, mid, v); else return query(k << 1 | 1, mid + 1, r, v - tr[k << 1]); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); build(1, 1, n); for (int i = n; i >= 1; i--) { p[i] = query(1, 1, n, a[i]) + 1; add(1, 1, n, p[i]); } for (int i = 1; i <= n; i++) printf("%d ", p[i]); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<int, int>; using ld = long double; using pii = pair<int, int>; using dbl = long double; using vll = vector<int>; #pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") const ll mod = 1000000007; const ll N = 2e5 + 10; const ll inf = 1e16 + 10; ll seg[4 * N], lz[4 * N], a[N], n; ll merge(ll x, ll y) { return min(x, y); } ll cnt(ll l, ll r) { return 1; } void build(ll cur, ll s, ll e) { if (s == e) { seg[cur] = a[s]; return; } build(cur + cur, s, (s + e) / 2); build(cur + cur + 1, (s + e) / 2 + 1, e); seg[cur] = merge(seg[cur + cur], seg[cur + cur + 1]); } void propogate(ll cur, ll s, ll e) { if (s != e) { lz[cur + cur] += lz[cur]; lz[cur + cur + 1] += lz[cur]; } seg[cur] += cnt(s, e) * lz[cur]; lz[cur] = 0; } void update(ll cur, ll s, ll e, ll l, ll r, ll val) { propogate(cur, s, e); if (e < l || s > r) return; if (l <= s && e <= r) { lz[cur] += val; propogate(cur, s, e); return; } update(cur + cur, s, (s + e) / 2, l, r, val); update(cur + cur + 1, (s + e) / 2 + 1, e, l, r, val); seg[cur] = merge(seg[cur + cur], seg[cur + cur + 1]); } ll query(ll cur, ll s, ll e, ll l, ll r) { propogate(cur, s, e); if (e < l || s > r) return inf; if (l <= s && e <= r) return seg[cur]; ll q1 = query(cur + cur, s, (s + e) / 2, l, r); ll q2 = query(cur + cur + 1, (s + e) / 2 + 1, e, l, r); return merge(q1, q2); } ll dfs(ll cur, ll s, ll e) { propogate(cur, s, e); if (s == e) { return s; } propogate(cur + cur, s, (s + e) / 2); propogate(cur + cur + 1, (s + e) / 2 + 1, e); if (seg[cur + cur + 1] == 0) { return dfs(cur + cur + 1, (s + e) / 2 + 1, e); } else { return dfs(cur + cur, s, (s + e) / 2); } } void build() { build(1, 1, n); } void update(ll pos, ll val) { update(1, 1, n, pos, pos, val); } void update(ll l, ll r, ll val) { update(1, 1, n, l, r, val); } ll query(ll l, ll r) { return query(1, 1, n, l, r); } signed main() { cin >> n; for (ll i = 1; i <= n; i++) { cin >> a[i]; } build(); ll ans[n]; for (ll i = 0; i < n; i++) { ll x = dfs(1, 1, n); update(x, inf); update(x + 1, n, -(i + 1)); ans[x - 1] = i + 1; } for (ll i = 0; i < n; i++) { cout << ans[i] << " "; } }
#include <bits/stdc++.h> using namespace std; long long int fen[200005]; void upd(long long int x, long long int a) { for (long long int i = x; i < 200005; i += (i & (-i))) { fen[i] += a; } } long long int ret(long long int x) { long long int o = 0; for (long long int i = x; i > 0; i -= (i & (-i))) { o += fen[i]; } return o; } int main() { long long int n; cin >> n; long long int s[n]; for (long long int i = 0; i < n; i++) { cin >> s[i]; upd(i + 1, i + 1); } long long int out[n]; for (long long int i = n - 1; i >= 0; i--) { long long int l = 0, h = n - 1; while (h - l > 1) { long long int mi = (h + l) / 2; if (ret(mi) > s[i]) { h = mi; } else { l = mi; } } if (ret(h) == s[i]) { out[i] = h + 1; upd(h + 1, -h - 1); } else { out[i] = l + 1; upd(l + 1, -l - 1); } cout << "\n"; } for (long long int i = 0; i < n; i++) { cout << out[i] << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 200100; const long long INF = 2000000100; const long long MOD = 998244353; long long N, S[MAXN], P[MAXN]; long long B[MAXN]; void update(int ind, long long v) { for (int i = ind; i <= N; i += (i & -i)) { B[i] += v; } } long long query(int ind) { if (ind == 0) return 0; long long s = 0; for (int i = ind; i > 0; i -= (i & -i)) { s += B[i]; } return s; } int main() { ios_base::sync_with_stdio(0); cin >> N; for (int i = 0; i < N; i++) cin >> S[i]; for (int i = 1; i <= N; i++) update(i, i); for (int i = N - 1; i >= 0; i--) { long long a = 1, b = N, c; long long ans = -39; while (a <= b) { c = (a + b) / 2; if (query(c - 1) > S[i]) { b = c - 1; } else { ans = c; a = c + 1; } } P[i] = ans; update(ans, -ans); } for (int i = 0; i < N; i++) cout << P[i] << (i == N - 1 ? "\n" : " "); }
#include <bits/stdc++.h> using namespace std; struct node { int64_t data, lazy; int pos; }; node st[200001 * 4]; int64_t input[200001]; int anw[200001]; void output(int root, int L, int R) { if (L == R) { return; } int mid = (L + R) >> 1; output(root << 1, L, mid); output(root << 1 | 1, mid + 1, R); } void Init(int root, int L, int R) { st[root].lazy = 0; if (L == R) { st[root].data = input[L]; st[root].pos = L; return; } int mid = (L + R) >> 1; Init(root << 1, L, mid); Init(root << 1 | 1, mid + 1, R); int lpos = root << 1, rpos = root << 1 | 1; st[root].data = min(st[lpos].data, st[rpos].data); if (st[root].data == st[rpos].data) { st[root].pos = st[rpos].pos; } else { st[root].pos = st[lpos].pos; } } void Update(int root, int l, int r, int L, int R, int64_t val) { if (l == L && r == R) { st[root].lazy += val; st[root].data += val; return; } int mid = (L + R) >> 1; if (st[root].lazy != 0) { Update(root << 1, L, mid, L, mid, st[root].lazy); Update(root << 1 | 1, mid + 1, R, mid + 1, R, st[root].lazy); st[root].lazy = 0; } if (r <= mid) { Update(root << 1, l, r, L, mid, val); } else if (mid < l) { Update(root << 1 | 1, l, r, mid + 1, R, val); } else { Update(root << 1, l, mid, L, mid, val); Update(root << 1 | 1, mid + 1, r, mid + 1, R, val); } int lpos = root << 1, rpos = root << 1 | 1; st[root].data = min(st[lpos].data, st[rpos].data); if (st[root].data == st[rpos].data) { st[root].pos = st[rpos].pos; } else { st[root].pos = st[lpos].pos; } } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> input[i]; } Init(1, 1, n); int64_t MAX = int64_t(n) * int64_t(n) * int64_t(100); if (n == 2) { output(1, 1, n); } for (int i = 1; i <= n; i++) { int pos = st[1].pos; anw[pos] = i; Update(1, pos, pos, 1, n, MAX); Update(1, pos, n, 1, n, int64_t(i * -1)); if (n == 2) { output(1, 1, n); } } for (int i = 1; i <= n; i++) { cout << anw[i] << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 10, Max = 101; const int MOD = 1000000007; const long long OO = 2e12 + 5000; const long long Sqrt = 710; const double PI = acos(-1); const double EPS = 1e-9; int n; long long a[N]; pair<long long, long long> seg[4 * N]; long long lazy[8 * N]; void push(int p) { if (!lazy[p]) return; seg[p].first += lazy[p]; lazy[p * 2] += lazy[p]; lazy[p * 2 + 1] += lazy[p]; lazy[p] = 0; } void build(int p = 1, int s = 1, int e = n) { if (s == e) { seg[p] = {a[s], -s}; return; } int mid = (s + e) / 2; build(p * 2, s, mid); build(p * 2 + 1, mid + 1, e); seg[p] = min(seg[p * 2], seg[p * 2 + 1]); } void update(int from, int to, long long val, int p = 1, int s = 1, int e = n) { push(p); if (s > to || from > e) return; if (s >= from && e <= to) { lazy[p] += val; push(p); return; } int mid = (s + e) / 2; update(from, to, val, p * 2, s, mid); update(from, to, val, p * 2 + 1, mid + 1, e); seg[p] = min(seg[p * 2], seg[p * 2 + 1]); } pair<long long, long long> get(int from = 1, int to = n, int p = 1, int s = 1, int e = n) { push(p); if (s > to || from > e) return {OO, OO}; if (s >= from && e <= to) { return seg[p]; } int mid = (s + e) / 2; pair<long long, long long> first = get(from, to, p * 2, s, mid); pair<long long, long long> second = get(from, to, p * 2 + 1, mid + 1, e); return min(first, second); } int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } vector<int> sol(N); build(); int cnt = 0; for (int i = 1; i <= n; ++i) { pair<long long, long long> mn = get(); int idx = mn.second * -1; assert(mn.second != OO); sol[idx] = i; long long val = OO; update(idx, idx, val); val = i; update(1 + idx, n, -val); } for (int i = 1; i <= n; ++i) cout << sol[i] << " "; cout << endl; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; class { public: pair<long long, long long> T[200009 << 2]; long long lz[200009 << 2]; long long n, f[200009], ans[200009]; void solve() { cin >> n; for (int i = 1; i <= n; ++i) cin >> f[i]; build(1, 1, n); for (int i = 1; i <= n; ++i) { auto res = query(1, 1, n, 1, n); ans[res.second] = i; update(1, 1, n, res.second + 1, n, -i); update(1, 1, n, res.second, res.second, 1e18); } for (int i = 1; i <= n; ++i) cout << ans[i] << " "; } void build(int rt, int l, int r) { lz[rt] = 0; if (l == r) { T[rt] = {f[l], l}; return; } int mid = l + r >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); up(rt); } void up(int rt) { if (T[rt << 1].first == T[rt << 1 | 1].first) { T[rt] = T[rt << 1 | 1]; } else { T[rt] = min(T[rt << 1], T[rt << 1 | 1]); } } void update(int rt, int l, int r, int ll, int rr, long long v) { if (ll > rr) return; if (ll <= l && rr >= r) { lz[rt] += v; T[rt].first += v; return; } int mid = l + r >> 1; down(rt); if (ll <= mid) update(rt << 1, l, mid, ll, rr, v); if (rr > mid) update(rt << 1 | 1, mid + 1, r, ll, rr, v); up(rt); } pair<long long, long long> query(int rt, int l, int r, int ll, int rr) { if (ll <= l && rr >= r) { return T[rt]; } int mid = l + r >> 1; down(rt); pair<long long, long long> res = {1e18, 1e18}; if (ll <= mid) { pair<long long, long long> ret = query(rt << 1, l, mid, ll, rr); if (ret.first == res.first) res.second = ret.second; else res = min(res, ret); } if (rr > mid) { pair<long long, long long> ret = query(rt << 1 | 1, mid + 1, r, ll, rr); if (ret.first == res.first) res.second = ret.second; else res = min(res, ret); } return res; } void down(int rt) { if (lz[rt]) { T[rt << 1].first += lz[rt]; lz[rt << 1] += lz[rt]; T[rt << 1 | 1].first += lz[rt]; lz[rt << 1 | 1] += lz[rt]; lz[rt] = 0; } } } NSPACE; int main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); NSPACE.solve(); }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const long long inf = 1000000000000; const int N = 200005; const long long LLMAX = 2e18; const long long MOD = 1000000009ll; const double eps = 1e-8; const int MAXN = 1e6 + 10; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int read() { int 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; } int n, ans[N]; long long a[N]; struct SegmentTree { int l, r, id; long long v, add; } tree[N * 4]; void push_up(int i) { if (tree[i << 1 | 1].v == tree[i << 1].v) { if (tree[i << 1 | 1].id > tree[i << 1].id) { tree[i].id = tree[i << 1 | 1].id; tree[i].v = tree[i << 1 | 1].v; } else { tree[i].id = tree[i << 1].id; tree[i].v = tree[i << 1].v; } } else { if (tree[i << 1].v < tree[i << 1 | 1].v) { tree[i].id = tree[i << 1].id; tree[i].v = tree[i << 1].v; } else { tree[i].id = tree[i << 1 | 1].id; tree[i].v = tree[i << 1 | 1].v; } } return; } void build(int p, int l, int r) { tree[p].l = l, tree[p].r = r; if (l == r) { tree[p].v = a[l]; tree[p].id = r; return; } int mid = (l + r) >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); push_up(p); } void spread(int p) { if (tree[p].add) { tree[p << 1].add += tree[p].add; tree[p << 1 | 1].add += tree[p].add; tree[p << 1].v += tree[p].add; tree[p << 1 | 1].v += tree[p].add; tree[p].add = 0; } } void change(int p, int l, int r, long long d) { if (l <= tree[p].l && r >= tree[p].r) { tree[p].v += d; tree[p].add += d; return; } spread(p); int mid = (tree[p].l + tree[p].r) >> 1; if (l <= mid) change(p << 1, l, r, d); if (r > mid) change(p << 1 | 1, l, r, d); push_up(p); } int main(int argc, char const *argv[]) { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]); build(1, 1, n); for (int i = 1; i <= n; ++i) { ans[tree[1].id] = i; int d = tree[1].id; change(1, d, d, 10000000000000); change(1, d + 1, n, -i); } for (int i = 1; i <= n; ++i) { printf("%d%c", ans[i], " \n"[i == n]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int INF = 2e9; const long long INFLL = 1e18; const int MAX_N = 1; int N; vector<long long> v; struct SEG { struct NODE { int l, r; long long data; }; vector<NODE> seg; int SZ; void add() { seg.push_back((NODE){-1, -1, (long long)0}); } void Init(int x) { SZ = x; add(); init(0, 1, SZ); } void init(int idx, int s, int e) { if (s == e) return; seg[idx].l = seg.size(); add(); seg[idx].r = seg.size(); add(); init(seg[idx].l, s, (s + e) / 2); init(seg[idx].r, (s + e) / 2 + 1, e); } void Update(int x, long long y) { update(0, 1, SZ, x, y); } void update(int idx, int s, int e, int x, long long y) { seg[idx].data += y; if (s == e) return; if (x <= (s + e) / 2) update(seg[idx].l, s, (s + e) / 2, x, y); else update(seg[idx].r, (s + e) / 2 + 1, e, x, y); } long long Sum(int x, int y) { return sum(0, 1, SZ, x, y); } long long sum(int idx, int s, int e, int x, int y) { if (x <= s && e <= y) return seg[idx].data; else if (x > e || y < s) return 0; return sum(seg[idx].l, s, (s + e) / 2, x, y) + sum(seg[idx].r, (s + e) / 2 + 1, e, x, y); } }; SEG Seg; vector<long long> ans; set<int> st; int main() { cin >> N; Seg.Init(N); for (int i = 0; i < N; i++) { long long x; scanf("%lld", &x); v.push_back(x); } for (int i = 1; i <= N; i++) { Seg.Update(i, i); st.insert(i); } while (!v.empty()) { long long x = v.back(); v.pop_back(); int s = 1, e = N, m; while (s < e) { m = (s + e) / 2 + 1; if (Seg.Sum(1, m - 1) > x) { e = m - 1; } else { s = m; } } s = (*st.lower_bound(s)); st.erase(s); ans.push_back(s); Seg.Update(s, -s); } while (!ans.empty()) { printf("%d ", (int)ans.back()); ans.pop_back(); } return 0; }