int n, m; int pre[maxn], siz[maxn]; intfind(int x){ return x == pre[x] ? x : pre[x] = find(pre[x]); } voidjoin(int x, int y){ x = find(x); y = find(y); if (x == y) return ; pre[x] = y; siz[y] += siz[x]; }
intmain(){ scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) pre[i] = i, siz[i] = 1; for (int i = 1, k, x; i <= m; i++) { scanf("%d", &k); if (k == 0) continue; scanf("%d", &x); k--; while (k--) { int y; scanf("%d", &y); join(x, y); } } for (int i = 1; i <= n; i++) cout << siz[find(i)] << ' '; return0; }
string t; intcheck(int maxd){ t = ""; int tot = 0, tot2 = 0, mx = 0; string a, b; for (int i = 1; i <= n; i++) { if (s[i] == '(') { if (tot < maxd) { tot++; a += '('; t += '0'; } else { tot2++; b += '('; mx = max(tot2, mx); t += '1'; } } else { if (tot > 0) { tot--; a += ')'; t += '0'; } else { assert(tot2 >= 0); tot2--; b += ')'; t += '1'; } } } // cout << a << endl << b << endl; return mx <= maxd; }
intmain(){ scanf("%d%s", &n, s + 1); check(2); int l = 1, r = n, ans = 0; string res; while (l <= r) { int m = (l + r) / 2; if (check(m)) res = t, r = m - 1; else l = m + 1; } // cout << ans; cout << res; return0; }
inlinevoidadd(int& x, int y){ x += y; if (x >= mod) x -= mod; } inlinevoidsub(int& x, int y){ x -= y; if (x < 0) x += mod; }
int n, a[maxn], pos[maxn];
inlineintlowbit(int x){ return x & -x; } structBIT { int a[maxn]; inlinevoidupdate(int i, int x){ for (; i <= n; i += lowbit(i)) add(a[i], x); } inlineintquery(int i){ int r = 0; for (; i > 0; i -= lowbit(i)) add(r, a[i]); return r; } } f1, f2;
intmain(){ scanf("%d", &n); vector<int> lsh; for (int i = 1; i <= n; i++) { scanf("%d", a + i); lsh.push_back(a[i]); } sort(lsh.begin(), lsh.end()); for (int i = 1; i <= n; i++) { int rk = lower_bound(lsh.begin(), lsh.end(), a[i]) - lsh.begin() + 1; pos[rk] = i; } int ans = 0; for (int i = 1; i <= n; i++) { f1.update(pos[i], pos[i]); f2.update(pos[i], n - pos[i] + 1); int x = 1ll * (n - pos[i] + 1) * f1.query(pos[i]) % mod; int y = 1ll * pos[i] * ((f2.query(n) - f2.query(pos[i] - 1) + mod) % mod) % mod; int z = 1ll * pos[i] * (n - pos[i] + 1) % mod; add(ans, 1ll * x * lsh[i - 1] % mod); add(ans, 1ll * y * lsh[i - 1] % mod); sub(ans, 1ll * z * lsh[i - 1] % mod); } cout << ans << endl; return0; }