solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 9223372036854775807;
const long long int mod = 998244353;
int MOD(int a, int b) {
if (a > b)
return a - b;
else
return b - a;
}
long long int max3(long long int a, long long int b, long long int c) {
return max(c, max(a, b));
}
long long int min3(long long int a, long long int b, long long int c) {
return min(a, min(b, c));
}
long long int power(long long int x, long long int y, long long int mod1) {
long long int res = 1;
while (y > 0) {
if (y & 1) res = (res * x) % mod1;
y = y >> 1;
x = (x * x) % mod1;
}
return res;
}
long long int logg(long long int a) {
long long int x = 0;
while (a > 1) {
x++;
a /= 2;
}
return x;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int abso(long long int x) {
if (x < 0) {
return -x;
}
return x;
}
long long int ceiling(long long int a, long long int b) {
if (a % b == 0) {
return a / b;
} else {
return a / b + 1;
}
}
long long int modinv(long long int x, long long int p) {
return power(x, p - 2, p);
}
struct point {
long long int x;
long long int y;
};
long long int cross(point a, point b) { return a.x * b.y - b.x * a.y; }
bool ccw(point p1, point p2) {
if (cross(p1, p2) > 0) {
return true;
} else {
return false;
}
}
void ccw_sort(vector<point> &v) {
vector<point> v1, v2;
for (int i = 0; i < v.size(); i++) {
if (v[i].y > 0) {
v1.push_back(v[i]);
} else {
v2.push_back(v[i]);
}
}
sort(v1.begin(), v1.end(), ccw);
sort(v2.begin(), v2.end(), ccw);
for (int i = 0; i < v2.size(); i++) {
v1.push_back(v2[i]);
}
v = v1;
}
long long int n;
long long int cnt = 0;
vector<long long int> bit1, bit2, bit3;
void upd(int i, int x, vector<long long int> &bit) {
for (int j = i; j <= 2 * n; j += (j & (-j))) {
bit[j] += x;
}
}
void upd1(int i, int x, vector<long long int> &bit) {
for (int j = i; j <= n; j += (j & (-j))) {
bit[j] += x;
}
}
long long int query(int i, vector<long long int> &bit) {
long long int sum = 0;
for (int j = i; j > 0; j -= (j & (-j))) {
sum += bit[j];
}
return sum;
}
void add(int x) {
upd(x, 1, bit1);
upd(x, x, bit2);
}
int find_median() {
int beg = 1;
int end = n;
int mn = n;
while (beg <= end) {
int mid = (beg + end) / 2;
if (query(mid, bit1) >= (cnt + 1) / 2) {
mn = min(mn, mid);
end = mid - 1;
} else {
beg = mid + 1;
}
}
return mn;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> n;
vector<int> p(n + 1);
vector<int> pos(n + 1);
for (int i = 1; i <= n; i++) {
cin >> p[i];
pos[p[i]] = i;
}
bit1.resize(2 * n + 10);
bit2.resize(2 * n + 10);
bit3.resize(n + 10);
long long int inv = 0;
for (long long int k = 1; k <= n; k++) {
add(pos[k]);
upd1(pos[k], 1, bit3);
cnt++;
long long int x = find_median();
long long int left = query(x - 1, bit2) - x * ((cnt + 1) / 2 - 1) +
((cnt + 1) / 2) * ((cnt + 1) / 2 - 1) / 2;
long long int r = k - (cnt + 1) / 2;
long long int right =
query(n, bit2) - query(x, bit2) - x * (r)-r * (r + 1) / 2;
inv += (query(n, bit3) - query(pos[k], bit3));
cout << inv + right - left << endl;
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
void egcd(int a, int b, int64_t& x, int64_t& y) {
if (a == 0) {
x = 0;
y = 1;
return;
}
int64_t tx, ty;
egcd(b % a, a, tx, ty);
x = ty - tx * (b / a);
y = tx;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int& v : a) cin >> v;
if (n == 1) {
cout << "1 1\n" << -a[0] << "\n1 1\n0\n1 1\n0\n";
return 0;
}
cout << "1 1\n" << -a[0] << "\n2 " << n << '\n';
int64_t x, y;
vector<int64_t> b(n);
for (int i = 1; i < n; ++i) {
egcd(n - 1, n, x, y);
b[i] = -y * a[i] * n;
cout << -x * a[i] * (n - 1) << ' ';
}
cout << "\n1 " << n << '\n';
for (int64_t v : b) cout << v << ' ';
cout << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int n;
cin >> n >> s;
for (int i = 0; i < s.size() - 1; i++) {
if ((s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' || s[i] == 'y') &&
(s[i + 1] == 'a' || s[i + 1] == 'e' || s[i + 1] == 'i' ||
s[i + 1] == 'o' || s[i + 1] == 'u' || s[i + 1] == 'y')) {
s.erase(s.begin() + i + 1);
i--;
}
}
cout << s << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long infi = INT_MAX;
const long long infll = LLONG_MAX;
const long double PI = 3.1415926535897932384626;
void solve(long long test_case) {
long long n, a, b;
cin >> n >> a >> b;
if (a + b + 2 > n) {
cout << -1 << "\n";
return;
}
if (abs(a - b) > 1) {
cout << -1 << "\n";
return;
}
vector<long long> arr(n);
for (long long i = 0; i < n; i++) arr[i] = i + 1;
if (a > b) {
for (long long i = n - 1; i - 1 >= 0; i -= 2) {
swap(arr[i], arr[i - 1]);
if (--a == 0) break;
}
} else if (b > a) {
for (long long i = 0; i + 1 < n; i += 2) {
swap(arr[i], arr[i + 1]);
if (--b == 0) break;
}
} else {
for (long long i = 1; i + 1 < n; i += 2) {
if (a == 0) break;
swap(arr[i], arr[i + 1]);
a--;
}
}
for (auto x : arr) cout << x << " ";
cout << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ios_base ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long tc = 1;
cin >> tc;
while (tc--) solve(tc);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
int N, W, H, p[MAX], q[MAX], ans[MAX][2], who[MAX];
vector<pair<int, int> > xx, yy;
vector<int> X[MAX * 2], Y[MAX * 2];
int main() {
scanf("%d%d%d", &N, &W, &H);
for (int i = 0; i < N; ++i) {
int g;
scanf("%d%d%d", &g, &p[i], &q[i]);
if (g == 1) {
ans[i][0] = p[i];
ans[i][1] = H;
xx.push_back({p[i], i});
} else {
ans[i][0] = W;
ans[i][1] = p[i];
yy.push_back({p[i], i});
}
}
sort(xx.begin(), xx.end());
sort(yy.begin(), yy.end());
for (auto &it : xx) {
int cur = it.second;
X[q[cur] - p[cur] + MAX].push_back(cur);
}
for (auto &it : yy) {
int cur = it.second;
Y[q[cur] - p[cur] + MAX].push_back(cur);
}
for (int i = 0; i < 2 * MAX; ++i)
if (!X[i].empty()) {
int opp = Y[i].size();
for (int j = 0; j < X[i].size(); ++j) {
if (Y[i].size() > X[i].size() - j - 1)
who[X[i][j]] = Y[i][X[i].size() - j - 1];
else
who[X[i][j]] = X[i][j + Y[i].size()];
}
}
for (int i = 0; i < 2 * MAX; ++i)
if (!Y[i].empty()) {
int opp = X[i].size();
for (int j = 0; j < Y[i].size(); ++j) {
if (X[i].size() > Y[i].size() - j - 1)
who[Y[i][j]] = X[i][Y[i].size() - j - 1];
else
who[Y[i][j]] = Y[i][j + X[i].size()];
}
}
for (int i = 0; i < N; ++i) printf("%d %d\n", ans[who[i]][0], ans[who[i]][1]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a[100002], t[10000];
int main() {
int n;
cin >> n;
int MAX = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
t[a[i]]++;
MAX = max(MAX, a[i]);
}
int ans = 0;
for (int i = 0; i < MAX; i++)
if (t[i] == 1)
ans++;
else if (t[i] > 1)
ans += 2;
ans++;
cout << ans << endl;
sort(a, a + n);
n = unique(a, a + n) - a;
for (int i = 0; i < n; i++) cout << a[i] << " ";
for (int i = n - 2; i >= 0; i--)
if (t[a[i]] > 1) cout << a[i] << " ";
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T sqr(const T& a) {
return a * a;
}
template <typename T>
inline int sign(const T& a) {
return a < 0 ? -1 : a > 0;
}
void task();
int main() {
task();
return 0;
}
int v1, v2;
int t, d;
int a[110];
void task() {
ios_base::sync_with_stdio(0);
cin >> v1 >> v2;
cin >> t >> d;
int ans = 0;
for (int i = 0; i < 110; ++i) a[i] = v1 + d * i;
for (int i = t - 1; i >= 0; --i) a[i] = min(a[i], v2 + d * (t - i - 1));
for (int i = 0; i < t; ++i) ans += a[i];
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define fi first
#define se second
#define en "\n"
#define pii pair<int,int>
#define vt vector
using namespace std;
ll p(int n, int m, vector<int>& t)
{
ll s1 = 0;
vector<ll> suf(m);
suf[m-1]=n - t[m-1];
// cout << suf[m-1] << " ";
for(int i = m- 2; i >= 0; i--)
{
suf[i] = n - t[i] + suf[i+1];
// cout << suf[i] << " ";
}
for(int i = 0; i < m - 1; i++)
{
s1 += (t[i]+1)*suf[i+1];
}
return s1;
}
void solve()
{
int n; cin >> n;
vt<int> a(n); for(int& i : a) cin >> i;
map<int,vt<int>> m;
for(int i = 0; i < n; i++)
{
m[a[i]].pb(i);
}
ll ans = 0;
for(auto x : m) ans += p(n,x.se.size(),x.se);
cout << ans << en;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t; cin >> t;
while(t--) solve();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
int c[200005];
int n;
double w;
struct data {
double x, y;
int id;
bool operator<(data s) const { return fabs(x * s.y) < fabs(y * s.x); }
} t[200005];
int a[200005], b[200005];
void update(int x) {
for (; x <= n; x += (x & (-x))) ++c[x];
}
int query(int x) {
int res = 0;
for (; x; x -= (x & (-x))) res += c[x];
return res;
}
int main() {
scanf("%d%lf", &n, &w);
w += 1e-6;
for (int i = 1; i <= n; i++) {
scanf("%lf%lf", &t[i].x, &t[i].y);
t[i].id = i;
t[i].y -= w;
}
sort(t + 1, t + n + 1);
for (int i = 1; i <= n; i++) {
a[i] = t[i].id;
}
for (int i = 1; i <= n; i++) t[i].y += 2 * w;
sort(t + 1, t + n + 1);
for (int i = 1; i <= n; i++) b[t[i].id] = i;
long long ans = 0;
for (long long i = 1; i <= n; i++) {
ans += i - 1 - query(b[a[i]]);
update(b[a[i]]);
}
printf("%I64d\n", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char a[101], b[101];
cin >> a;
cin >> b;
int i;
for (i = 0; a[i] != '\0' && b[i] != '\0'; i++) {
if (a[i] == b[i])
cout << "0";
else
cout << "1";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void chmin(T &x, const T &y) {
if (x > y) x = y;
}
template <typename T>
inline void chmax(T &x, const T &y) {
if (x < y) x = y;
}
int read() {
char c;
while ((c = getchar()) < '-')
;
int x = c - '0';
while ((c = getchar()) >= '0') x = x * 10 + c - '0';
return x;
}
const int N = 5e3 + 5, D = 998244353;
int C[N][N], jie[N];
long long F(int n, int m) {
int mi = min(n, m);
long long ans = 0;
for (int i = 0; i <= mi; ++i)
(ans += (long long)C[n][i] * C[m][i] % D * jie[i]) %= D;
return ans;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
int n = 5e3;
for (int i = 0; i <= n; ++i) {
C[i][0] = 1;
for (int j = 1; j <= i; ++j) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % D;
}
jie[0] = 1;
for (int i = 1; i <= n; ++i) jie[i] = (long long)jie[i - 1] * i % D;
cout << F(a, b) * F(a, c) % D * F(b, c) % D;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, k, p[N];
bool used[N];
vector<int> q;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= n; i++) {
if (!used[i]) {
int cur = 0, v = i;
while (!used[v]) {
cur++;
used[v] = true;
v = p[v];
}
q.push_back(cur);
}
}
bitset<N> dp;
sort(q.begin(), q.end());
dp[0] = 1;
for (int i = 0; i < q.size(); i++) {
int cnt = 1;
while (i + 1 < q.size() && q[i] == q[i + 1]) {
i++;
cnt++;
}
for (int j = 1; j <= cnt; j *= 2) {
dp |= dp << (j * q[i]);
cnt -= j;
}
dp |= dp << (cnt * q[i]);
}
cout << k + !dp[k] << " ";
int ans = 0;
for (auto &it : q) {
while (it >= 2 && k) {
k--;
ans += 2;
it -= 2;
}
}
for (auto &it : q) {
while (it >= 1 && k) {
k--;
ans += 1;
it -= 1;
}
}
cout << ans;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
constexpr long long int MOD = 1000000007;
;
constexpr double EPS = 1e-8;
long long int N, M, K, T, H, W, L, R;
void Solve() {
cin >> N >> K;
vector<int> v(N);
for (auto& i : v) cin >> i, i--;
vector<vector<int>> p(N);
for (int i = 0; i < N; i++) {
p[v[i]].push_back(i);
}
for (int i = 0; i < N; i++) {
p[i].push_back(N);
}
vector<vector<int>> dp(N + 1, vector<int>(N + 1, MOD));
dp[0][0] = -1;
for (int i = 0; i < N; i++) {
for (int j = 0; j <= i; j++) {
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
dp[i + 1][j + 1] = min(
dp[i + 1][j + 1],
*lower_bound(p[i].begin(), p[i].end(), min((int)N, dp[i][j] + 1)));
}
}
for (int i = N; i >= K; i--) {
if (dp[i][K] < N) {
cout << N - i << endl;
return;
}
}
cout << -1 << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> T;
while (T--) {
Solve();
}
}
| 6 |
#include <bits/stdc++.h>
char s[5010];
bool f[5010];
int main() {
register int n, i, j, k, to, cnt = 0;
char c;
scanf("%s", s + 1);
n = strlen(s + 1);
while ((1 << cnt) <= n) ++cnt;
--cnt;
int full = (1 << cnt) - 1, left = n - full;
memset(f, 1, 1 << cnt);
for (i = 1; i <= left; ++i) {
for (c = 'z', j = 0; j < (1 << cnt); ++j)
if (f[j] && i + j <= n && s[i + j] < c) c = s[i + j];
putchar(c);
for (j = 0; j < (1 << cnt); ++j)
if (f[j] && i + j <= n && s[i + j] != c) f[j] = 0;
for (j = 0; j < cnt; ++j)
for (k = 0; k <= full; ++k)
if ((k >> j) & 1) f[k] |= f[k ^ (1 << j)];
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int a, b;
cin >> a >> b;
if (a + b <= n) {
cout << 1 << " " << a + b - 1;
} else {
int v = (a + b - n) + 1;
if (v >= n) v = n;
cout << v << " " << n;
}
cout << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
struct point {
double first, second;
point(double first = 0, double second = 0) : first(first), second(second) {}
point operator-(point a) { return point(first - a.first, second - a.second); }
point operator+(point a) { return point(first + a.first, second + a.second); }
point operator/(double a) { return point(first / a, second / a); }
point operator*(double a) { return point(first * a, second * a); }
double operator*(point a) { return first * a.first + second * a.second; }
double operator^(point a) { return first * a.second - second * a.first; }
void input() { scanf("%lf%lf", &first, &second); }
double al() { return atan2(second, first); }
};
point p[100010], O;
double area[100010];
const double pi = acos(-1);
double a, b, c;
int calc(int st, int ed, int ok = 0) {
while (st < ed) {
int ii = (st + ed + 1 - ok) >> 1;
double ui = a * p[ii].first + b * p[ii].second + c;
if (!ok)
if (ui > 0)
st = ii;
else
ed = ii - 1;
else if (ui < 0)
st = ii + 1;
else
ed = ii;
}
return st;
}
point intersect(point A, point B) {
return A + (B - A) * (-(c + point(a, b) * A) / (point(a, b) * (B - A)));
}
int main() {
int n, Q;
scanf("%d%d", &n, &Q);
for (int i = 0; i < n; i++) p[n - i - 1].input();
for (int i = 0; i < n * 3; i++) p[i + n] = p[i];
for (int i = 1; i < n * 4; i++) area[i] = area[i - 1] + (p[i - 1] ^ p[i]);
double tot_area = area[n];
while (Q--) {
O.input();
double st = 0, ed = pi, u0, u1, al, zero, ans;
int found = false;
for (int i = 0; i < 60; i++) {
if (i == 0)
al = st;
else
al = (st + ed) / 2;
a = sin(al), b = -cos(al), c = -(a * O.first + b * O.second);
u0 = a * p[0].first + b * p[0].second + c;
u1 = a * p[1].first + b * p[1].second + c;
bool flag = false;
if (u0 > u1) {
u0 = -u0, u1 = -u1;
a = -a, b = -b, c = -c;
flag = true;
}
int lft = 0, rgt = n - 1;
while (lft < rgt) {
int ii = lft + rgt + 1 >> 1;
int jj = ii + 1;
double ui = a * p[ii].first + b * p[ii].second + c;
double uj = a * p[jj].first + b * p[jj].second + c;
if (ui <= uj && ui >= (u0 + u1) * 0.5)
lft = ii;
else
rgt = ii - 1;
}
int id_max = lft + 1;
lft = id_max + 1, rgt = id_max + n - 1;
while (lft < rgt) {
int ii = lft + rgt >> 1;
int jj = ii + 1;
double ui = a * p[ii].first + b * p[ii].second + c;
double uj = a * p[jj].first + b * p[jj].second + c;
if (ui > uj)
lft = ii + 1;
else
rgt = ii;
}
int id_min = lft;
int eed = calc(id_max, id_min, 0) + n;
int sst = calc(id_min, id_max + n, 1);
point ped = intersect(p[eed % n], p[(eed + 1) % n]);
point pst = intersect(p[(sst + n - 1) % n], p[sst % n]);
double mid_area = 0;
mid_area += pst ^ p[sst];
mid_area += area[eed] - area[sst];
mid_area += p[eed] ^ ped;
mid_area += ped ^ pst;
if (flag) mid_area = tot_area - mid_area;
if (fabs(mid_area * 2 - tot_area) < 1e-8) {
found = true;
ans = al;
break;
}
if (i == 0) {
zero = mid_area;
continue;
}
if ((mid_area > tot_area * 0.5) == (zero > tot_area * 0.5))
st = al;
else
ed = al;
}
printf("%.18lf\n", found ? ans : al);
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i;
vector<int> vec;
cin >> n >> m;
while ((n * m) != 0) {
vec.push_back(n * m);
n--;
m--;
}
if (vec.size() % 2 == 0)
cout << "Malvika" << endl;
else
cout << "Akshat" << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
static const int maxn = 1e5 + 5;
static const int mod = 1e9 + 7;
static const int base = 9527;
int pw[maxn] = {1};
void ini() {
for (int i = 1; i < maxn; i++) pw[i] = 1ll * pw[i - 1] * base % mod;
}
struct tree {
struct star {
int v, nex, hash, ok;
} edge[maxn << 1];
int head[maxn], cnt, n, siz[maxn], hash[maxn], ok[maxn];
void ini(int n) {
this->n = n;
for (int i = 0; i <= n; i++)
head[i] = -1, siz[i] = 1, hash[i] = 0, ok[i] = 1;
cnt = -1;
}
void add_edge(int u, int v) {
edge[++cnt] = star{v, head[u], 0, 1};
head[u] = cnt;
edge[++cnt] = star{u, head[v], 0, 1};
head[v] = cnt;
}
void dfs1(int u, int father) {
for (int i = head[u]; ~i; i = edge[i].nex) {
star& e = edge[i];
if (e.v == father) continue;
dfs1(e.v, u);
siz[u] += siz[e.v];
vector<int> buf;
for (int j = head[e.v]; ~j; j = edge[j].nex) {
if (edge[j].v == u) continue;
buf.push_back(edge[j].hash);
}
sort(buf.begin(), buf.end());
for (int x : buf) e.hash = (1ll * e.hash * base + x) % mod;
e.hash = (e.hash + siz[e.v]) % mod;
}
}
void dfs2(int u, int father) {
vector<int> buf;
for (int i = head[u]; ~i; i = edge[i].nex) buf.push_back(edge[i].hash);
sort(buf.begin(), buf.end());
vector<int> pre(buf), suf(buf);
int sz = suf.size();
for (int i = 1; i < sz; i++) {
pre[i] = (1ll * pre[i - 1] * base + pre[i]) % mod;
suf[sz - 1 - i] = (1ll * suf[sz - 1 - i] * pw[i] + suf[sz - i]) % mod;
}
if (suf.empty())
hash[u] = n;
else
hash[u] = (suf[0] + n) % mod;
for (int i = head[u]; ~i; i = edge[i].nex) {
if (father == edge[i].v) continue;
int idx = lower_bound(buf.begin(), buf.end(), edge[i].hash) - buf.begin();
if (idx - 1 >= 0)
edge[i ^ 1].hash += 1ll * pre[idx - 1] * pw[sz - idx - 1] % mod;
if (idx + 1 < sz) edge[i ^ 1].hash += suf[idx + 1];
edge[i ^ 1].hash = (edge[i ^ 1].hash + n - siz[edge[i].v]) % mod;
dfs2(edge[i].v, u);
}
}
struct dfs34node {
map<int, int> mp;
int kind = 0, bad = 0;
void add(int hash, int ok) {
if (mp[hash] == 0) kind++;
mp[hash]++;
if (ok == 0) bad++;
}
void del(int hash, int ok) {
if (ok == 0) bad--;
mp[hash]--;
if (mp[hash] == 0) kind--;
}
int get() { return bad == 0 && kind <= 1; }
};
void dfs3(int u, int father) {
for (int i = head[u]; ~i; i = edge[i].nex) {
if (edge[i].v == father) continue;
dfs3(edge[i].v, u);
dfs34node mation;
for (int j = head[edge[i].v]; ~j; j = edge[j].nex) {
if (edge[j].v == u) continue;
mation.add(edge[j].hash, edge[j].ok);
}
edge[i].ok = mation.get();
}
}
void dfs4(int u, int father) {
dfs34node mation;
for (int i = head[u]; ~i; i = edge[i].nex) {
mation.add(edge[i].hash, edge[i].ok);
}
ok[u] = mation.get();
for (int i = head[u]; ~i; i = edge[i].nex) {
if (edge[i].v == father) continue;
mation.del(edge[i].hash, edge[i].ok);
edge[i ^ 1].ok = mation.get();
dfs4(edge[i].v, u);
mation.add(edge[i].hash, edge[i].ok);
}
}
int getans() {
int ans = -1;
dfs1(1, 0);
dfs2(1, 0);
dfs3(1, 0);
dfs4(1, 0);
for (int i = n; i >= 1; i--) {
if (ok[i]) ans = i;
}
return ans;
}
} t1;
void debug() {
ini();
int n;
while (cin >> n) {
t1.ini(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
t1.add_edge(u, v);
}
t1.dfs1(1, 0);
t1.dfs2(1, 0);
for (int i = 1; i <= n; i++) {
cout << t1.hash[i] << " ";
}
cout << endl;
}
}
int main() {
ini();
int n;
while (cin >> n) {
t1.ini(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
t1.add_edge(u, v);
}
cout << t1.getans() << endl;
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int a[N][26];
char foo[N];
bool can_win[N], can_lose[N];
int main() {
int nnn, k;
scanf("%d %d", &nnn, &k);
memset(a, 0, sizeof(a));
int n = 1;
for (int i = 0; i < nnn; i++) {
scanf("%s", foo);
int t = 1;
for (int j = 0; foo[j]; j++) {
int p = foo[j] - 'a';
if (a[t][p] == 0) {
n++;
a[t][p] = n;
}
t = a[t][p];
}
}
for (int i = n; i >= 1; i--) {
bool mv = false;
for (int j = 0; j < 26; j++) {
if (a[i][j] != 0) {
mv = true;
break;
}
}
if (!mv) {
can_win[i] = false;
can_lose[i] = true;
continue;
}
can_win[i] = false;
can_lose[i] = false;
for (int j = 0; j < 26; j++) {
if (a[i][j] != 0) {
if (!can_win[a[i][j]]) {
can_win[i] = true;
}
if (!can_lose[a[i][j]]) {
can_lose[i] = true;
}
}
}
}
if (!can_win[1]) {
puts("Second");
return 0;
}
if (can_lose[1]) {
puts("First");
return 0;
}
puts((k % 2 == 1) ? "First" : "Second");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 29;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
int res = 0;
while (n && ++res) n -= n & (-n);
return res;
}
long long int MOD = 998244353;
long long int powa(long long int x, long long int y) {
long long int ans = 1;
long long int m = MOD;
while (y > 0) {
if (y % 2 == 1) {
ans *= x;
ans %= m;
}
y /= 2;
x = x * x;
x %= m;
}
return ans % m;
}
long long int fac[200005 + 1];
long long int invfac[200005 + 1];
long long int cnk(long long int n, long long int k) {
return (fac[n] * invfac[n - k] % MOD * invfac[k] % MOD) % MOD;
}
int main() {
fac[0] = invfac[0] = 1;
for (int i = 1; i <= 200005; i++) {
fac[i] = (fac[i - 1] * i) % MOD;
invfac[i] = powa(fac[i], MOD - 2);
}
long long int n, k;
cin >> n >> k;
long long int cint = n - k;
long long int ans = 0;
for (long long int i = cint; i >= 1; i--) {
if (i % 2 == cint % 2) {
ans += (cnk(cint, i) * powa(i, n)) % MOD;
} else {
ans -= (cnk(cint, i) * powa(i, n)) % MOD;
}
ans %= MOD;
if (ans < 0) ans += MOD;
}
if (cint < n && cint >= 1) {
ans *= cnk(n, cint);
ans %= MOD;
}
if (k != 0) {
ans = ans * 2;
ans %= MOD;
}
cout << ans % MOD;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > white;
vector<pair<int, int> > black;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int c, s;
cin >> c >> s;
if (c)
black.push_back(pair<int, int>(s, i));
else
white.push_back(pair<int, int>(s, i));
}
n--;
pair<int, int> w, b;
sort(white.begin(), white.end());
sort(black.begin(), black.end());
while (white.size() && black.size()) {
w = white.back();
b = black.back();
int mn = min(w.first, b.first);
w.first -= mn;
b.first -= mn;
if (w.first == 0) {
black[black.size() - 1] = b;
white.pop_back();
} else if (b.first == 0) {
white[white.size() - 1] = w;
black.pop_back();
}
cout << w.second << ' ' << b.second << ' ' << mn << endl;
}
if (white.size()) {
for (auto idx : white)
if (idx.second != w.second)
cout << idx.second << ' ' << b.second << ' ' << 0 << endl;
}
if (black.size()) {
for (auto idx : black)
if (idx.second != b.second)
cout << idx.second << ' ' << w.second << ' ' << 0 << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 28;
struct SAM {
int a[26], par, max;
} sam[100010];
int tot = 1, Root = 1, tail = 1, fa[100010][22], dep[100010];
int n, m;
struct Node {
int y, next;
} a[100010];
int len = 0, last[100010];
struct node {
int x, c;
node() {
x = inf;
c = 0;
}
node(int a, int b) {
x = a;
c = b;
}
};
bool operator>(node a, node b) { return a.c == b.c ? a.x < b.x : a.c > b.c; }
node mymax(node a, node b) { return a > b ? a : b; }
struct trnode {
int lc, rc;
node c;
} tr[6000010];
int trtot = 0, root[100010];
void ins(int x, int y) {
a[++len].y = y;
a[len].next = last[x];
last[x] = len;
}
void update(int &x, int l, int r, int k) {
if (!x) x = ++trtot, tr[x].c.x = l;
if (l == r) {
tr[x].c.c++;
return;
}
int mid = (l + r) / 2;
if (k <= mid)
update(tr[x].lc, l, mid, k);
else
update(tr[x].rc, mid + 1, r, k);
tr[x].c = mymax(tr[tr[x].lc].c, tr[tr[x].rc].c);
}
int addsam(int c, int p, int k) {
int np = ++tot;
sam[np].max = sam[p].max + 1;
for (; p && !sam[p].a[c]; p = sam[p].par) sam[p].a[c] = np;
tail = np;
update(root[np], 1, m, k);
if (!p)
sam[np].par = Root;
else {
int q = sam[p].a[c];
if (sam[q].max == sam[p].max + 1)
sam[np].par = q;
else {
int nq = ++tot;
sam[nq] = sam[q];
sam[nq].max = sam[p].max + 1;
sam[np].par = sam[q].par = nq;
for (; p && sam[p].a[c] == q; p = sam[p].par) sam[p].a[c] = nq;
}
}
return np;
}
int merge(int x1, int x2, int l, int r) {
if (!x1 || !x2) {
return x1 + x2;
}
int x = ++trtot;
tr[x] = tr[x1];
if (l == r)
tr[x].c.c = tr[x1].c.c + tr[x2].c.c;
else {
int mid = (l + r) / 2;
tr[x].lc = merge(tr[x1].lc, tr[x2].lc, l, mid);
tr[x].rc = merge(tr[x1].rc, tr[x2].rc, mid + 1, r);
tr[x].c = mymax(tr[tr[x].lc].c, tr[tr[x].rc].c);
}
return x;
}
char s[500010], s1[50010];
int pre[500010], f[500010];
node findans(int x, int l, int r, int fl, int fr) {
if (!x) return node(fl, 0);
if (fl == l && fr == r) return tr[x].c;
int mid = (l + r) / 2;
if (fr <= mid) return findans(tr[x].lc, l, mid, fl, fr);
if (fl > mid) return findans(tr[x].rc, mid + 1, r, fl, fr);
return mymax(findans(tr[x].lc, l, mid, fl, mid),
findans(tr[x].rc, mid + 1, r, mid + 1, fr));
}
void dfs(int x) {
fa[x][0] = sam[x].par;
dep[x] = dep[fa[x][0]] + 1;
for (int i = 1; (1 << i) <= dep[x]; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
for (int i = last[x]; i; i = a[i].next) {
int y = a[i].y;
dfs(y);
root[x] = merge(root[x], root[y], 1, m);
}
}
void solve(int l, int r, int fl, int fr) {
int x = pre[fr], len = fr - fl + 1;
if (f[fr] < len) {
printf("%d 0\n", l);
return;
}
for (int i = 20; i >= 0; i--)
if ((1 << i) <= dep[x] && sam[fa[x][i]].max >= len) x = fa[x][i];
node t = findans(root[x], 1, m, l, r);
if (t.x < l)
printf("f***\n");
else
printf("%d %d\n", t.x, t.c);
}
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
scanf("%d", &m);
getchar();
for (int i = 1; i <= m; i++) {
scanf("%s", s1 + 1);
int Len = strlen(s1 + 1);
int p = Root;
for (int j = 1; j <= Len; j++) p = addsam(s1[j] - 'a', p, i);
}
int x = Root, now = 0;
for (int i = 1; i <= n; i++) {
int c = s[i] - 'a';
while (x != Root && !sam[x].a[c]) x = sam[x].par, now = sam[x].max;
if (sam[x].a[c]) x = sam[x].a[c], now++;
pre[i] = x;
f[i] = now;
}
for (int i = 2; i <= tot; i++) ins(sam[i].par, i);
dep[0] = -1;
dfs(Root);
int Q;
scanf("%d", &Q);
while (Q--) {
int l, r, fl, fr;
scanf("%d %d %d %d", &l, &r, &fl, &fr);
solve(l, r, fl, fr);
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
auto SOLUTION_FUNCTION(void) -> void {
int n;
string s;
cin >> n >> s;
int counter{INT_MAX};
for (auto i = 0; i < n; ++i) {
counter = min(counter, (s[i] == '>') ? i : INT_MAX);
counter = min(counter, (s[n - 1 - i] == '<') ? i : INT_MAX);
}
cout << counter << '\n';
return;
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << setprecision(10);
cout << fixed;
cout << boolalpha;
int testcase = 1;
cin >> testcase;
while (testcase--) {
SOLUTION_FUNCTION();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
inline bool eq(double a, double b) { return fabs(a - b) < 1e-9; }
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
int res = 0;
while (n && ++res) n -= n & (-n);
return res;
}
inline void check(long long int& a) {
a %= 1000000007;
if (a < 0) a += 1000000007;
}
auto start = high_resolution_clock::now();
inline void measure();
int ar[27];
char ch[102][102];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a, b, c, d;
cin >> a >> b >> c >> d;
int n;
cin >> n;
for (int i = 0; i < (102); i++) {
for (int j = 0; j < (102); j++) {
ch[i][j] = '.';
}
}
for (int i = 0; i < (n); i++) {
cin >> ar[i];
}
int sr, sc, dir, k = 0, first, g;
if (b > d) {
if (d & 1) {
dir = -1;
sr = 0, sc = a + c - 1;
} else {
dir = 1;
sr = 0, sc = 0;
}
first = 0;
g = a - 1;
} else if (d >= b) {
if (b & 1) {
dir = 1;
sr = 0, sc = 0;
} else {
dir = -1;
sr = 0, sc = a + c - 1;
}
first = a;
g = a + c - 1;
}
int e = min(b, d);
while (true) {
if (k < n && ar[k]) {
ch[sr][sc] = k + 'a';
ar[k]--;
}
if (ar[k] == 0) k++;
if (k >= n) break;
if (sr < e && (dir == -1 && sc == 0 || dir == 1 && sc == a + c - 1)) {
sr++;
if (dir == -1)
dir = 1;
else
dir = -1;
} else if (sr < e && ((dir == 1 && sc >= 0 && sc < a + c - 1) ||
((dir == -1 && sc > 0 && sc <= a + c - 1)))) {
sc = sc + dir;
} else if (sr >= e && (dir == -1 && sc == first || dir == 1 && sc == g)) {
sr++;
if (dir == -1)
dir = 1;
else
dir = -1;
} else if (sr >= e && ((dir == 1 && sc >= first && sc < g) ||
((dir == -1 && sc > first && sc <= g)))) {
sc = sc + dir;
}
}
cout << "YES" << endl;
for (int i = 0; i < (max(b, d)); i++) {
for (int j = 0; j < (a + c); j++) {
cout << ch[i][j];
}
cout << endl;
}
return 0;
}
inline void measure() {
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << duration.count() << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long arr[400007];
int main() {
long long n, b, x;
cin >> n >> b;
map<long long, long long> mp;
map<long long, long long>::iterator it;
map<long long, long long>::reverse_iterator rit;
for (long long i = 1; i <= n; ++i) {
cin >> x;
mp[x]++;
}
long long lg = ceil(log2(mp.size()));
long long need = lg * n, have = b * 8;
if (need <= have)
cout << 0 << endl;
else {
long long mn = have / n;
long long so = pow(2, mn);
long long rmv = mp.size() - so, s1 = 0, s2 = 0, i, ans = 1e9;
vector<long long> v;
for (it = mp.begin(); it != mp.end(); ++it) {
v.push_back(it->second);
s2 += it->second;
}
for (long long i = 0; i < v.size(); ++i) {
if (i < so)
s1 += v[i];
else {
ans = min(ans, s2 - s1);
s1 += v[i];
s1 -= v[i - so];
}
}
cout << min(ans, s2 - s1) << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
class Team {
public:
string name;
int pt, goal, lost, tot, cnt;
bool operator<(const Team a) const {
if (pt != a.pt) return pt > a.pt;
if (tot != a.tot) return tot > a.tot;
if (goal != a.goal) return goal > a.goal;
return name < a.name;
}
} team[4], temp[4];
string str1, str2;
int d1, d2, tot, x, y;
bool okay(int win, int loss, int t1, int t2) {
for (int i = 0; i < 4; ++i) temp[i] = team[i];
team[t1].tot += win - loss;
team[t1].goal += win;
team[t1].lost += loss;
if (win > loss)
team[t1].pt += 3;
else if (win == loss) {
team[t1].pt++;
team[t2].pt++;
} else
team[t2].pt += 3;
team[t2].tot += loss - win;
team[t2].goal += loss;
team[t2].lost += win;
sort(team, team + 4);
bool flag = ((team[0].name == "BERLAND") || (team[1].name == "BERLAND"));
for (int i = 0; i < 4; ++i) team[i] = temp[i];
return flag;
}
int main() {
tot = 0;
for (int i = 1; i <= 5; ++i) {
cin >> str1 >> str2;
scanf("%d:%d", &d1, &d2);
x = -1;
y = -1;
for (int j = 0; j < tot; ++j)
if (str1 == team[j].name) {
x = j;
break;
}
if (x == -1) {
x = tot++;
team[x].name = str1;
}
for (int j = 0; j < tot; ++j)
if (str2 == team[j].name) {
y = j;
break;
}
if (y == -1) {
y = tot++;
team[y].name = str2;
}
team[x].goal += d1;
team[x].lost += d2;
team[x].tot += (d1 - d2);
if (d1 > d2)
team[x].pt += 3;
else if (d1 == d2)
team[x].pt += 1;
team[y].goal += d2;
team[y].lost += d1;
team[y].tot += (d2 - d1);
if (d2 > d1)
team[y].pt += 3;
else if (d2 == d1)
team[y].pt += 1;
team[x].cnt++;
team[y].cnt++;
}
sort(team, team + 4);
int bl = -1, opp = -1;
for (int i = 0; i < 4; ++i)
if (team[i].name == "BERLAND") {
bl = i;
break;
}
if (bl == -1) {
puts("IMPOSSIBLE");
return 0;
}
for (int i = 0; i < 4; ++i)
if ((team[i].name != "BERLAND") && (team[i].cnt < 3)) {
opp = i;
break;
}
int delta = 1000000, mloss = 1000000, mgoal;
for (int g = 0; g <= 100; ++g)
for (int l = 0; l < g; ++l)
if (okay(g, l, bl, opp)) {
if (g - l < delta) {
delta = g - l;
mloss = l;
mgoal = g;
} else if ((g - l == delta) && (l < mloss)) {
mloss = l;
mgoal = g;
}
}
if (delta == 1000000)
puts("IMPOSSIBLE");
else
printf("%d:%d\n", mgoal, mloss);
return 0;
}
| 5 |
#include <bits/stdc++.h>
const double ex = 1e-12;
const double INF = 1e15;
using namespace std;
int n, ct, h[305], dp[305];
double p[305], mid, dis[305];
bool vis[305], ins[305];
queue<int> Q;
struct ed {
int t, nxt;
double ds;
} e[305 * 305];
void add(int fr, int t, double ds) {
e[++ct] = (ed){t, h[fr], ds};
h[fr] = ct;
}
bool spfa(int s) {
dp[s] = 1;
dis[s] = 0;
while (!Q.empty()) Q.pop();
Q.push(s);
ins[s] = 1;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
ins[u] = 0;
vis[u] = 1;
++dp[u];
if (dp[u] > 3 * n) return 1;
for (int i = h[u]; i; i = e[i].nxt) {
int v = e[i].t;
if (dis[v] > dis[u] + e[i].ds + mid) {
dis[v] = dis[u] + e[i].ds + mid;
if (!ins[v]) {
Q.push(v);
ins[v] = 1;
}
}
}
}
return 0;
}
bool ok() {
memset(vis, 0, sizeof(vis));
memset(dp, 0, sizeof(dp));
memset(ins, 0, sizeof(ins));
for (int i = 0; i <= n * 3; i++) dis[i] = INF;
for (int i = 0; i <= n * 3; i++)
if (!vis[i])
if (spfa(i)) return 1;
return 0;
}
int main() {
cin >> n;
for (int i = 0; i <= n; i++) cin >> p[i];
for (int j = 0; j <= n * 3; j++)
for (int i = 0; i <= n; i++)
if (j + n - 2 * i <= n * 3 && j + n - 2 * i >= 0)
add(j, j + n - 2 * i, -p[i]);
double l = 0, r = 1, ans = 0;
while (r - l > ex) {
mid = (r + l) * 0.5;
if (ok())
l = mid + ex, ans = mid;
else
r = mid - ex;
}
printf("%.10lf", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<pair<int, string> > v(n);
for (int i = 0; i < n; ++i) {
char s[25];
int p, m, a, b, c, d, e, sum = 0;
scanf("%s%d%d%d%d%d%d%d", s, &p, &m, &a, &b, &c, &d, &e);
sum += p * 100;
sum -= m * 50;
sum += a + b + c + d + e;
v[i] = make_pair(-sum, string(s));
}
sort(v.begin(), v.end());
cout << v[0].second;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005, MAXM = 10005;
struct Chairman_tree {
int n;
struct Node {
int value, cnt;
Node *left, *right;
inline void maintain() { value = left->value + right->value; }
};
Node *root[MAXM], pool[MAXN * 50], *alloc;
vector<Node*> bin;
Node* new_Node() {
Node* ret;
if (bin.empty())
ret = alloc++;
else {
ret = bin.back();
bin.pop_back();
}
ret->value = 0;
ret->cnt = 1;
ret->left = ret->right = NULL;
return ret;
}
void build(int left, int right, Node*& curr) {
curr = new_Node();
if (left == right) return;
int mid = (left + right) / 2;
build(left, mid, curr->left);
build(mid + 1, right, curr->right);
}
void init(int n, int m) {
this->n = n;
alloc = pool;
bin.clear();
build(1, m, root[0]);
for (int i = 1; i <= n; i++) {
root[i] = root[0];
root[i]->cnt++;
}
}
void recycle(Node* curr) {
if (curr == NULL) return;
if (--curr->cnt == 0) {
recycle(curr->left);
recycle(curr->right);
bin.push_back(curr);
}
}
void update(int pos, int value, int left, int right, Node*& curr,
Node* last) {
curr = new_Node();
if (left == right) {
curr->value = last->value + value;
return;
}
int mid = (left + right) / 2;
if (pos <= mid) {
curr->right = last->right;
curr->right->cnt++;
update(pos, value, left, mid, curr->left, last->left);
} else {
curr->left = last->left;
curr->left->cnt++;
update(pos, value, mid + 1, right, curr->right, last->right);
}
curr->maintain();
}
int query(int l, int r, int left, int right, Node* curr, Node* last) {
if (l == left && r == right) return curr->value - last->value;
int mid = (left + right) / 2;
if (r <= mid)
return query(l, r, left, mid, curr->left, last->left);
else if (l > mid)
return query(l, r, mid + 1, right, curr->right, last->right);
else
return query(l, mid, left, mid, curr->left, last->left) +
query(mid + 1, r, mid + 1, right, curr->right, last->right);
}
};
Chairman_tree tree;
int n, k;
struct Record {
int x, r, f;
int left, right;
} rec[MAXN];
vector<int> scan;
inline int idx(int x) {
return upper_bound(scan.begin(), scan.end(), x) - scan.begin();
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d%d%d", &rec[i].x, &rec[i].r, &rec[i].f);
scan.push_back(rec[i].x - rec[i].r);
scan.push_back(rec[i].x);
scan.push_back(rec[i].x + rec[i].r);
}
sort(scan.begin(), scan.end());
scan.resize(unique(scan.begin(), scan.end()) - scan.begin());
sort(rec + 1, rec + n + 1,
[](const Record& a, const Record& b) -> bool { return a.r > b.r; });
for (int i = 1; i <= n; i++) {
rec[i].left = idx(rec[i].x - rec[i].r);
rec[i].right = idx(rec[i].x + rec[i].r);
rec[i].x = idx(rec[i].x);
}
tree.init(10000, scan.size());
long long ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = max(1, rec[i].f - k); j <= min(10000, rec[i].f + k); j++)
ans += tree.query(rec[i].left, rec[i].right, 1, scan.size(), tree.root[j],
tree.root[0]);
Chairman_tree::Node* temp;
tree.update(rec[i].x, 1, 1, scan.size(), temp, tree.root[rec[i].f]);
tree.recycle(tree.root[rec[i].f]);
tree.root[rec[i].f] = temp;
}
printf("%lld\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
inline long long gcd(long long a, long long b) {
while (a && b)
if (a > b)
a %= b;
else
b %= a;
return max(a, b);
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long num[101];
vector<long long> ver[101], g[101], l[101];
bool arr[101];
inline void addedge(long long a, long long b, long long c, long long d) {
ver[a].push_back(b);
g[a].push_back(c);
l[a].push_back(d);
ver[b].push_back(a);
g[b].push_back(c);
l[b].push_back(d);
}
inline bool bfs(long long start, long long startnum) {
queue<long long> q;
bool inq[101];
memset(inq, 0, sizeof(inq));
bool vis[101];
memset(vis, 0, sizeof(vis));
q.push(start);
num[start] = startnum;
vis[start] = 1;
while (q.size()) {
long long old = q.front();
q.pop();
inq[old] = 0;
for (long long i = 0; i < (long long)ver[old].size(); i++) {
long long y = ver[old][i], nxtnum = l[old][i] / num[old] * g[old][i];
if (g[old][i] * l[old][i] % num[old] ||
gcd(num[old], nxtnum) != g[old][i] ||
lcm(num[old], nxtnum) != l[old][i])
return 0;
if (vis[y]) {
if (num[y] != nxtnum) return 0;
continue;
}
vis[y] = 1;
num[y] = nxtnum;
q.push(y);
}
}
for (long long i = 1; i <= n; i++)
if (vis[i]) arr[i] = 1;
return 1;
}
inline void deal(long long x) {
if (ver[x].size() == 0) {
arr[x] = 1;
num[x] = 1;
return;
}
for (long long i = g[x][0]; i <= l[x][0]; i += g[x][0])
if (bfs(x, i)) return;
puts("NO");
exit(0);
}
inline void init() {
cin >> n >> m;
while (m--) {
long long a, b, c, d;
cin >> a >> b >> c >> d;
addedge(a, b, c, d);
}
}
signed main() {
init();
for (long long i = 1; i <= n; i++)
if (!arr[i]) deal(i);
puts("YES");
for (long long i = 1; i <= n; i++) cout << num[i] << ' ';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int st_min[300001][20], st_gcd[300001][20];
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
bool f(long long int n, long long int M, long long int log_2[]) {
for (auto i = 0; i < n - M; i++) {
long long int j = log_2[M + 1];
if (min(st_min[i][j], st_min[i + M - (1 << j) + 1][j]) ==
gcd(st_gcd[i][j], st_gcd[i + M - (1 << j) + 1][j]))
return true;
}
return false;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long int n;
cin >> n;
long long int a[n];
for (auto i = 0; i < n; i++) cin >> a[i];
for (auto i = 0; i < n; i++) {
st_min[i][0] = a[i];
st_gcd[i][0] = a[i];
}
for (auto j = 1; j < 20; j++)
for (auto i = 0; i < n; i++) {
st_min[i][j] = st_min[i][j - 1];
st_gcd[i][j] = st_gcd[i][j - 1];
if (i + (1 << (j - 1)) < n) {
st_min[i][j] = min(st_min[i][j], st_min[i + (1 << (j - 1))][j - 1]);
st_gcd[i][j] = gcd(st_gcd[i][j], st_gcd[i + (1 << (j - 1))][j - 1]);
}
}
long long int log_2[n + 1];
log_2[1] = 0;
for (auto i = 2; i < n + 1; i++) log_2[i] = log_2[i / 2] + 1;
long long int L = 0, R = n - 1, M, chk = -1;
while (L <= R) {
M = (L + R) >> 1;
if (f(n, M, log_2)) {
chk = M;
L = M + 1;
} else
R = M - 1;
}
vector<long long int> v;
for (auto i = 0; i < n - chk; i++) {
long long int j = log_2[chk + 1];
if (min(st_min[i][j], st_min[i + chk - (1 << j) + 1][j]) ==
gcd(st_gcd[i][j], st_gcd[i + chk - (1 << j) + 1][j]))
v.push_back(i + 1);
}
cout << (long long int)(v).size() << " " << chk << "\n";
for (auto i = 0; i < (long long int)(v).size(); i++) cout << v[i] << " ";
cout << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T abs(T x) {
return x > 0 ? x : -x;
}
int a[300005], b[2005];
int dp[300005];
int ll[300005], rr[300005];
int main(int argc, char *argv[]) {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
for (int j = 0; j < m; j++) {
scanf("%d", b + j);
}
sort(a, a + n);
sort(b, b + m);
for (int i = 0; i < n; i++) {
ll[i] = i;
if (i && a[i] - 1 == a[i - 1]) ll[i] = ll[i - 1];
}
for (int j = n - 1; j >= 0; j--) {
rr[j] = j;
if (j < n - 1 && a[j] + 1 == a[j + 1]) rr[j] = rr[j + 1];
}
int bp = 0;
for (int i = 0; i < n; i++) {
while (bp < m && b[bp] < a[i]) bp++;
int _left = 0;
if (bp < m && b[bp] == a[i]) _left++;
int curr = 0;
if (ll[i] - 1 >= 0) curr = dp[ll[i] - 1];
for (int j = bp - 1; j >= 0; j--) {
int diff = a[i] - b[j];
if (diff > i) break;
int v = bp - j;
int g = ll[i - diff];
if (g - 1 >= 0) v += dp[g - 1];
curr = max(curr, v);
}
_left += curr;
dp[rr[i]] = max(dp[rr[i]], _left);
int j = bp;
if (b[j] == a[i]) j++;
int cnt = 1;
for (; j < m; j++) {
int diff = b[j] - a[i];
if (n - i - 1 < diff) break;
int v = cnt;
int g = rr[i + diff];
if (g < n) dp[g] = max(dp[g], _left + v);
cnt++;
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
#define int long long
#define INT_MAX; INT64_MAX
#define INT_MIN INT64_MIN
#define PI 3.14159265358979323846
#define CLK clock_t clk = clock();//Start of main
#define OCLK cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';//End of main
#define MOD 1000000007
/* DEFINE cout */
#define pln cout << "===============================\n"
#define on cout<<"\n"
#define os cout<<" "
#define o2(a,b) cout<<a<<" "<<b
#define o(a) cout << a
#define bitcount __builtin_popcountll
/* DEFINE cout */
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);//fastio
using namespace std;
struct hash_pair { // use pair as key in unordered_map<pair<int,int>,int,hash_pair>;
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const
{
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
bool prime[10000001];
int P=MOD-2;
int factorial[10000001]={0};
int powerFunction(int x,int y){
int res = 1;
int p = MOD;
x = x % p;
while (y > 0){
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
void sieveFunction(int maxLimit){
memset(prime, true, sizeof(prime));
prime[0] = prime[1]=false;
for(int i=2; maxLimit >= i ; i++){
if(prime[i]){
for(int j=2*i ; maxLimit >= j ; j+=i)
prime[j]=false;
}
}
}
void factorialFunction(int maxLimit){
factorial[0]=1;
for(int i=1 ; i <= maxLimit ; i++)
factorial[i]=(factorial[i-1]*i)%MOD;
return;
}
int gcd(int a, int b){
if (b == 0)
return a;
return gcd(b, a % b);
}
void solve(){
int n, m;
cin >> n >> m;
int arr[n][n];
memset(arr, -1, sizeof(arr));
for(int i=0; n>i; i++){
string str;
cin >> str;
for(int j=0; n>j; j++){
if(str[j] != '*'){
arr[i][j] = str[j] - 'a';
}
}
}
for(int i=0; n>i; i++){
for(int j=0; n>j; j++){
if(i != j){
if(arr[i][j] == arr[j][i]){
cout << "YES\n";
for(int k=0; m+1>k; k++){
if(k%2 == 0){
cout << i+1 << ' ';
}else{
cout << j+1 << ' ';
}
}
on;
return;
}
}
}
}
if(m%2 == 1){
cout << "YES\n";
for(int k=0; m+1>k; k++){
if(k%2 == 0){
cout << 1 << ' ';
}else{
cout << 2 << ' ';
}
}
on;
return;
}else{
for(int i=0; n>i; i++){
int a, b;
a = -1;
b = -1;
for(int j=0; n>j; j++){
if(i != j){
if(arr[i][j] == 0){
a = j;
}else{
b = j;
}
}
}
if(a != -1 && b != -1){
if(m%4 == 0){
cout << "YES\n";
for(int k=0; m+1>k; k++){
if(k%2 == 0){
cout << i+1 << ' ';
}else{
if(k%4 == 1){
cout << a+1 << ' ';
}else{
cout << b+1 << ' ';
}
}
}
on;
return;
}else{
if(arr[b][a] == 1){
cout << "YES\n";
for(int k=0; m+1>k; k++){
if(k%3 == 0){
cout << i+1 << ' ';
}else if(k%3 == 1){
cout << b+1 << ' ';
}else{
cout << a+1 << ' ';
}
}
on;
return;
}else if(m > 2){
cout << "YES\n";
cout << i+1 << ' ';
cout << b+1 << ' ';
cout << a+1 << ' ';
m -= 6;
for(int k=0; m+1>k; k++){
if(k%2 == 0){
cout << i+1 << ' ';
}else{
if(k%4 == 1){
cout << a+1 << ' ';
}else{
cout << b+1 << ' ';
}
}
}
cout << b+1 << ' ';
cout << a+1 << ' ';
cout << i+1 << ' ';
on;
return;
}else{
cout << "YES\n";
cout << a+1 << ' ';
cout << i+1 << ' ';
cout << b+1 << ' ';
on;
return;
}
}
}
}
}
cout << "NO\n";
return;
}
signed main(){
fastio;
int t = 1;
cin >> t;
while(t--){
solve();
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, m, a[maxn], id[maxn], tot, nxt[2 * maxn], fst[maxn], to[2 * maxn],
fa[maxn];
int find(int x) { return fa[x] < 0 ? x : fa[x] = find(fa[x]); }
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", a + i), id[i] = i, fa[i] = -1;
sort(id + 1, id + 1 + n,
[](int p, int q) { return a[p] > a[q] || (a[p] == a[q] && p > q); });
for (int i = 1, a, b; i <= m; i++) {
scanf("%d%d", &a, &b);
nxt[++tot] = fst[a];
fst[a] = tot;
to[tot] = b;
nxt[++tot] = fst[b];
fst[b] = tot;
to[tot] = a;
}
long long ans = 0;
for (int i = 1; i <= n; i++)
for (int j = fst[id[i]]; j; j = nxt[j])
if (a[to[j]] > a[id[i]] || (a[to[j]] == a[id[i]] && to[j] > id[i])) {
int v = find(to[j]);
if (v == id[i]) continue;
ans += 1ll * fa[id[i]] * fa[v] * a[id[i]];
fa[id[i]] += fa[v], fa[v] = id[i];
}
printf("%.6f\n", 2.0 * ans / n / (n - 1));
return 0;
}
| 5 |
#include <bits/stdc++.h>
int N, O;
long long S, R, L[200000], NEED[200000], COVERED[200000], PRELEN;
std::pair<long long, long long> W[900000];
int main() {
scanf("%d%lld", &N, &R);
for (int i = 0; i < N; i++) scanf("%lld", L + i);
for (int i = 0; i < N; i++) {
long long X = L[i], Y;
scanf("%lld", &Y);
if (X > Y) {
puts("-1");
return 0;
}
NEED[i] = X + X - Y;
if (NEED[i] < 0) NEED[i] = 0;
}
for (int i = 0; i < N; i++)
if (NEED[i]) {
long long WHOLE = (NEED[i] - 1) / R, OVER = (NEED[i] - 1) % R + 1;
S += WHOLE + 1;
if (WHOLE) {
W[O++] = std::make_pair(0, PRELEN);
if (WHOLE > 1) W[O++] = std::make_pair(WHOLE - 1, R);
}
W[O++] = std::make_pair(0, PRELEN + (L[i] - OVER << 1) - WHOLE * R);
PRELEN += (L[i] + R - OVER << 1) - (WHOLE + 1) * R;
OVER = R - OVER;
for (int j = i + 1; OVER && j < N; j++)
if (OVER < L[j]) {
L[j] -= OVER;
NEED[j] -= OVER;
if (NEED[j] < 0) NEED[j] = 0;
OVER = 0;
} else {
OVER -= L[j];
L[j] = NEED[j] = 0;
}
} else
PRELEN += L[i] << 1;
printf("%lld\n", S);
if (S <= 100000) {
long long LASTANS = -W[0].second;
for (int i = 0; i < O; i++)
if (W[i].first)
while (W[i].first--) printf("%lld ", LASTANS += W[i].second);
else
printf("%lld ", LASTANS = W[i].second);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e5;
long long f(long long a) {
if (a < 10) return a;
vector<int> digs;
while (a) digs.push_back(a % 10), a /= 10;
reverse(digs.begin(), digs.end());
int s = digs.size();
long long res = 9, tmp = 1;
for (int i = 0; i < s - 2; ++i) {
res += 9 * tmp;
tmp *= 10;
}
for (int i = 0; i < s - 1; ++i) {
res += (digs[i] - (i == 0)) * tmp;
tmp /= 10;
}
if (digs[0] <= digs.back()) res++;
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long l, r;
cin >> l >> r;
cout << f(r) - f(l - 1);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long inff = 1e18, MOD = 1e9 + 9, P1 = 61, P2 = 39;
const long long sz = 3;
void run() {
long long n;
cin >> n;
vector<pair<long long, long long> > sob(2 * n, {0, 0});
set<long long> s;
for (auto &v : sob) {
char ch;
cin >> ch;
if (ch == '+')
v.first = 1;
else
cin >> v.second;
}
reverse(sob.begin(), sob.end());
vector<long long> ans;
long long fl = 1;
for (auto v : sob) {
if (v.first) {
if (s.empty()) {
fl = 0;
break;
} else {
ans.push_back(*(s.begin()));
s.erase(s.begin());
}
} else {
while (!s.empty() && (*s.begin()) < v.second) {
s.erase(s.begin());
fl = 0;
}
s.insert(v.second);
}
}
if (fl) {
cout << "YES\n";
reverse(ans.begin(), ans.end());
for (auto v : ans) cout << v << ' ';
} else {
cout << "NO";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long k = 1;
while (k--) {
run();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
const int mod = 998244353;
int v[N + 10], fac[N + 10], inv[N + 10];
inline int power(int a, int b) {
int res = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) res = 1ll * res * a % mod;
return res;
}
inline int C(int x, int y) {
return 1ll * fac[x] * inv[y] % mod * inv[x - y] % mod;
}
int main() {
fac[0] = 1;
for (int i = 1; i <= N; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
inv[N] = power(fac[N], mod - 2);
for (int i = N - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
int n, k, cnt = 0, ans = 0;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &v[i]);
for (int i = 0; i < n; i++) cnt += (v[i] != v[(i + 1) % n]);
for (int i = 0; i <= cnt; i++)
if (i % 2 == 0)
ans = (ans + 1ll * C(cnt, i) * power(k - 2, cnt - i) % mod * C(i, i / 2) %
mod) %
mod;
ans = 1ll * ans * power(k, n - cnt) % mod;
ans = (power(k, n) - ans + mod) % mod;
printf("%d\n", 1ll * ans * power(2, mod - 2) % mod);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long a[100010];
int main() {
long long n, k;
int m;
while (~scanf("%lld %d %lld", &n, &m, &k)) {
for (int i = 1; i <= m; i++) {
scanf("%lld", &a[i]);
}
int sumdiscard = 0, ans = 0, _begin = 1;
long long curpage = k;
while (sumdiscard < m) {
if (curpage + sumdiscard >= a[_begin]) {
int cnt = 0;
while (_begin <= m && curpage + sumdiscard >= a[_begin]) {
_begin++;
cnt++;
}
sumdiscard += cnt;
ans++;
} else {
curpage = (a[_begin] - sumdiscard + k - 1) / k * k;
}
}
printf("%d\n", ans);
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int scores[100];
int coors[100][3];
int board[2][100001];
bool done[100];
int n, hl, hr;
int testit(int d, double r, int ni) {
for (int i = 0; i < n; i++) done[i] = false;
int ans = 0;
double x = 0;
bool top = true;
if (d == 0) {
x = hl / r;
top = false;
} else {
x = (100.0 - hl) / r;
top = true;
}
if (board[top][(int)x] == -1) return -1;
ans += scores[board[top][(int)x]];
done[board[top][(int)x]] = true;
for (int i = 2; i <= ni; i++) {
x += 100.0 / r;
top = !top;
if (board[top][(int)x] == -1) return -1;
if (done[board[top][(int)x]]) return -1;
ans += scores[board[top][(int)x]];
done[board[top][(int)x]] = true;
}
return ans;
}
int main() {
cin >> hl >> hr >> n;
if (n == 0) {
cout << 0 << endl;
return 0;
}
char c;
int v, a, b;
for (int i = 0; i <= 100000; i++) board[0][i] = -1;
for (int i = 0; i <= 100000; i++) board[1][i] = -1;
for (int i = 0; i < n; i++) {
cin >> v >> c >> a >> b;
scores[i] = v;
coors[i][0] = (c == 'T');
coors[i][1] = a;
coors[i][2] = b;
if (b < a) swap(coors[i][2], coors[i][1]);
for (int j = coors[i][1]; j < coors[i][2]; j++) board[coors[i][0]][j] = i;
}
int best = 0;
for (int h = 1; h <= n; h += 2) {
v = 100 * (h - 1) + hl + hr;
double ang = (v / 100000.0);
best = max(best, testit(0, ang, h));
v = 100 * (h - 1) + 200 - hl - hr;
ang = v / 100000.0;
best = max(best, testit(1, ang, h));
}
for (int h = 2; h <= n; h += 2) {
v = 100 * (h - 1) + hl + 100 - hr;
double ang = v / 100000.0;
best = max(best, testit(0, ang, h));
v = 100 * (h - 1) + 100 - hl + hr;
ang = v / 100000.0;
best = max(best, testit(1, ang, h));
}
cout << best << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[100005];
long long int ans1 = 0, ans2 = 0, i, j, bodd = 0, beven = 0, aodd = 0,
aeven = 0;
scanf(" %s", s);
for (i = 0; s[i]; i++) {
if (s[i] == 'b') {
if (i % 2) {
ans1 += beven;
ans2 += bodd;
bodd++;
} else {
ans1 += bodd;
ans2 += beven;
beven++;
}
} else {
if (i % 2) {
ans1 += aeven;
ans2 += aodd;
aodd++;
} else {
ans1 += aodd;
ans2 += aeven;
aeven++;
}
}
}
printf("%lld %lld\n", ans1, ans2 + strlen(s));
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
inline long long sbt(long long x) { return __builtin_popcountll(x); }
inline long long iceil(double a) { return (long long)(ceil(a)); }
inline long long mul(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
return ((a % m) * (b % m)) % m;
}
inline long long add(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
return (a + b) % m;
}
inline long long sub(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
return (a - b + m) % m;
}
long long fastpow(long long a, long long b,
long long m = (long long)(1e9 + 7)) {
long long res = 1;
while (b > 0) {
if (b & 1) res = mul(res, a, m);
a = mul(a, a, m);
b >>= 1;
}
return res;
}
long long modinv(long long a, long long m = (long long)(1e9 + 7)) {
return fastpow(a, m - 2, m);
}
vector<pair<long long, long long> > adj[510][510];
long long n, m;
map<long long, long long> mp;
long long vis[510][510] = {0};
string a, b;
void dfs(long long i, long long j) {
vis[i][j] = 1;
for (auto k : adj[i][j]) {
if (!vis[k.first][k.second]) {
dfs(k.first, k.second);
}
}
}
void get_ac() {
cin >> n >> m;
cin >> a >> b;
long long c = 1;
for (auto i : a) {
if (i == '<') {
for (auto j = 2; j <= m; j++) {
adj[c][j].push_back({c, j - 1});
}
} else {
for (auto j = 1; j <= m - 1; j++) {
adj[c][j].push_back({c, j + 1});
}
}
c++;
}
c = 1;
for (auto i : b) {
if (i == 'v') {
for (auto j = 1; j <= n - 1; j++) {
adj[j][c].push_back({j + 1, c});
}
} else {
for (auto j = 2; j <= n; j++) {
adj[j][c].push_back({j - 1, c});
}
}
c++;
}
for (auto i = 1; i <= n; i++) {
for (auto j = 1; j <= m; j++) {
for (auto y = 1; y <= n; y++) {
for (auto z = 1; z <= m; z++) {
vis[y][z] = 0;
}
}
dfs(i, j);
for (auto y = 1; y <= n; y++) {
for (auto z = 1; z <= m; z++) {
if (!vis[y][z]) {
cout << "NO";
return;
}
}
}
}
}
cout << "YES";
}
int main() {
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
cin.tie(NULL);
{
get_ac();
cout << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long N = 3e5 + 1;
vector<long> g[N];
bool c[N];
bool vis[N];
long x, y;
bool found;
long size(long vtx) {
long sum = 1;
vis[vtx] = true;
for (long v : g[vtx]) {
if (!vis[v] && !c[v]) {
sum += size(v);
}
}
return sum;
}
void color(long vtx) {
if (vtx == y) {
c[vtx] = true;
found = true;
return;
}
vis[vtx] = true;
for (long v : g[vtx]) {
if (!vis[v]) {
color(v);
if (found) {
c[v] = true;
return;
}
}
}
}
int main() {
long n;
cin >> n >> x >> y;
for (long i = 1; i < n; i++) {
long u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
color(x);
long sy = size(y);
for (long i = 1; i <= n; i++) {
vis[i] = false;
}
long sx = size(x);
cout << 1LL * n * (n - 1) - 1LL * sx * sy;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int64_t n, a[500005], p[500005], m, b[500005];
int64_t dp[500005];
int64_t cost[500005];
signed main() {
if (false) {
} else {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
cin >> n;
for (int64_t i = 0; i < n; i++) cin >> a[i];
for (int64_t i = 0; i < n; i++) cin >> p[i];
cin >> m;
for (int64_t i = 0; i < m; i++) cin >> b[i];
b[m] = 1152921504606847000L;
for (int64_t i = 0; i < n; i++) {
int64_t at = lower_bound(b, b + m + 1, a[i]) - b;
if (p[i] < 0 || at == m) cost[at] += p[i];
}
for (int64_t i = 0; i < m + 1; i++) dp[i] = 1152921504606847000L;
dp[0] = 0;
for (int64_t i = 0; i < n; i++) {
int64_t at = lower_bound(b, b + m + 1, a[i]) - b;
if (at < m) {
if (a[i] == b[at]) {
if (p[i] < 0)
dp[at + 1] = min(dp[at + 1], dp[at] + cost[at] - p[i]);
else
dp[at + 1] = min(dp[at + 1], dp[at] + cost[at]);
}
if (p[i] > 0) cost[at] += p[i];
}
if (false) {
cout << "after step " << i << endl;
printf("\ta[%ld] = %ld\n", i, a[i]);
printf("\tp[%ld] = %ld\n", i, p[i]);
printf("\tat = %ld\n", at);
cout << "\tdp = [";
for (int64_t j = 0; j < m + 1; j++) {
if (j != 0) cout << ",";
cout << dp[j];
}
cout << "]" << endl;
cout << "\tcost = [";
for (int64_t j = 0; j < m + 1; j++) {
if (j != 0) cout << ",";
cout << cost[j];
}
cout << "]" << endl;
}
}
if (dp[m] > 1000000000000000000L) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
cout << (dp[m] + cost[m]) << endl;
}
}
| 8 |
#include <bits/stdc++.h>
const int maxn = 3e5 + 3;
int n, m, a[maxn], fp[maxn], ansa[maxn];
std::set<int> tas;
int bit[maxn];
struct info {
int l, r;
void print() const { printf("%d %d\n", l, r); }
} ans[maxn];
int prev[maxn];
int query(int k) {
int ans = 0;
for (k += 1; k <= m; k += k & -k) ans += bit[k];
return ans;
}
void modify(int k, int v) {
for (k += 1; k >= 1; k -= k & -k) bit[k] += v;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
scanf("%d", a + i);
if (tas.find(a[i]) == tas.end()) {
fp[a[i]] = i + 1;
}
tas.insert(a[i]);
}
for (int i = n; i >= 1; --i) {
if (fp[i]) {
ansa[i] += i;
for (int k = fp[i]; k > 0; k -= k & -k) ansa[i] += bit[k];
for (int k = fp[i]; k <= m; k += k & -k) bit[k] += 1;
}
}
for (int i = n, c = 0; i >= 0; --i) {
if (tas.find(i) == tas.end()) {
ans[i] = {i, i + c};
} else {
c += 1;
ans[i] = {1, ansa[i]};
}
}
memset(prev, 0xff, sizeof(prev));
memset(bit, 0, sizeof(bit));
for (int i = 0; i < m; ++i) {
if (prev[a[i]] != -1) {
ans[a[i]].r = std::max(ans[a[i]].r, query(prev[a[i]] + 1) + 1);
modify(prev[a[i]], -1);
}
modify(i, 1);
prev[a[i]] = i;
}
for (int i = 1; i <= n; ++i) {
if (prev[i] != -1) {
ans[i].r = std::max(ans[i].r, query(prev[i] + 1) + 1);
}
}
for (int i = 1; i <= n; ++i) ans[i].print();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<int> ar;
ar.resize(n);
for (int i = 0; i < n; i++) cin >> ar[i];
int k = 0;
int ind = 0;
while (ind < n) {
if (ar[ind] == ar[0]) {
k++;
ind++;
} else
break;
}
int cl = n / k;
if (n % k != 0) {
cout << "NO";
return 0;
}
for (int i = 0; i < cl; i++) {
set<int> s;
for (int j = 0; j < k; j++) s.insert(ar[i * k + j]);
if (s.size() != 1) {
cout << "NO";
return 0;
}
if (i < cl - 1)
if (ar[i * k] == ar[(i + 1) * k]) {
cout << "NO";
return 0;
}
}
cout << "YES";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline 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;
}
struct node {
int l, r, num;
friend bool operator<(node a, node b) {
return a.l < b.l || (a.l == b.l && a.r > b.r);
}
} a[200005], d[200005];
int lx[200005];
int n, m, cnt, aa, bb;
long long ans;
int erfen(int x) {
int l = 1, r = cnt, mid, ans = 0;
while (l <= r) {
mid = (l + r) >> 1;
if (lx[mid] >= x)
r = mid - 1;
else {
l = mid + 1;
ans = mid;
}
}
return ans;
}
int main() {
n = read();
m = read();
for (int i = 1; i <= n; i++) {
a[i].l = read();
a[i].r = read();
a[i].num = i;
}
sort(a + 1, a + n + 1);
d[1] = a[1];
lx[1] = a[1].l;
cnt = 1;
for (int i = 2; i <= n; i++)
if (a[i].r > d[cnt].r) {
d[++cnt] = a[i];
lx[cnt] = a[i].l;
}
for (int i = 1; i <= m; i++) {
int x = read(), y = read(), z = read();
int fx = erfen(x);
if (fx == 0) fx++;
while (fx <= cnt && d[fx].l <= y) {
int l = max(x, d[fx].l), r = min(y, d[fx].r);
long long sum = 1ll * (r - l) * z;
if (ans < sum) {
ans = sum;
aa = d[fx].num;
bb = i;
}
fx++;
}
}
printf("%I64d\n", ans);
if (ans != 0) {
printf("%d %d\n", aa, bb);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int q, type;
long long l[100], r[100], val[100], node[100], x, v;
int findDepth(long long x) {
if (x == 1) return 0;
for (int i = 1;; ++i) {
if (r[i] >= x) return i;
}
}
void shift(long long &x, long long k, int d) {
long long n = r[d] - l[d] + 1;
x += k;
x -= l[d];
x = (x % n + n) % n;
x += l[d];
}
void shiftVal(long long x, long long k) {
int d = findDepth(x);
val[d] += k;
val[d] %= (r[d] - l[d] + 1);
}
void shiftNode(long long x, long long k) {
int d = findDepth(x);
node[d] += k;
node[d] %= (r[d] - l[d] + 1);
}
void print(long long x, int d) {
if (!x) return;
printf("%lld ", x);
shift(x, val[d], d);
shift(x, node[d], d);
x /= 2;
--d;
shift(x, -val[d], d);
print(x, d);
}
int main() {
l[0] = r[0] = 1;
for (int i = 1;; ++i) {
l[i] = l[i - 1] * 2;
r[i] = r[i - 1] * 2 + 1;
if (r[i] > 1e18) {
break;
}
}
scanf("%d", &q);
while (q--) {
scanf("%d%lld", &type, &x);
if (type == 3) {
print(x, findDepth(x));
puts("");
} else {
scanf("%lld", &v);
if (type == 1) {
shiftVal(x, v);
} else {
shiftNode(x, v);
}
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 7;
const int INF = (int)1e9;
int n;
int q;
int tab[N][2];
struct Node {
int dist[2][2];
} bs;
Node operator+(Node a, Node b) {
if (a.dist[0][0] == -1) {
return b;
}
if (b.dist[0][0] == -1) {
return a;
}
Node c;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
c.dist[i][j] = INF;
}
}
for (int i = 0; i < 2; i++) {
for (int k = 0; k < 2; k++) {
for (int j = 0; j < 2; j++) {
c.dist[i][j] = min(c.dist[i][j], a.dist[i][k] + b.dist[k][j] + 1);
}
}
}
return c;
}
Node t[4 * N];
void build(int v, int tl, int tr) {
if (tl == tr) {
bool av0 = tab[tl][0];
bool av1 = tab[tr][1];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
t[v].dist[i][j] = INF;
}
}
if (av0) {
t[v].dist[0][0] = 0;
}
if (av1) {
t[v].dist[1][1] = 0;
}
if (av0 && av1) {
t[v].dist[0][1] = t[v].dist[1][0] = 1;
}
} else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
t[v] = t[2 * v] + t[2 * v + 1];
}
}
Node get(int v, int tl, int tr, int l, int r) {
if (tr < l || r < tl) {
return bs;
}
if (l <= tl && tr <= r) {
return t[v];
} else {
int tm = (tl + tr) / 2;
Node a = get(2 * v, tl, tm, l, r);
Node b = get(2 * v + 1, tm + 1, tr, l, r);
return a + b;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
bs.dist[0][0] = -1;
cin >> n >> q;
for (int i = 0; i < 2; i++) {
string s;
cin >> s;
for (int j = 0; j < n; j++) {
tab[j + 1][i] = 1 ^ (s[j] == 'X');
}
}
build(1, 1, n);
while (q--) {
int x, y, r1, c1, r2, c2;
cin >> x >> y;
if (x <= n) {
r1 = 0;
c1 = x;
} else {
r1 = 1;
c1 = x - n;
}
if (y <= n) {
r2 = 0;
c2 = y;
} else {
r2 = 1;
c2 = y - n;
}
if (c1 > c2) {
swap(r1, r2);
swap(c1, c2);
}
int sol = get(1, 1, n, c1, c2).dist[r1][r2];
if (sol == INF) {
sol = -1;
}
cout << sol << "\n";
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, z, a[10039], foo, bar, baz, quz = 1, pur;
int main() {
register int i, j;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &pur);
foo = foo + pur;
bar++;
if (foo * quz > baz * bar) baz = foo, quz = bar;
}
printf("%lf\n", baz * 1.0 / quz);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int64_t ncr(int64_t n, int64_t r) {
if (r > n - r) r = n - r;
int64_t C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (int64_t i = 1; i <= n; i++) {
for (int64_t j = min(i, r); j > 0; j--) {
C[j] = (C[j] + C[j - 1]) % 998244353;
}
}
return C[r];
}
int64_t modular_exp(int64_t a, int64_t b) {
int64_t res = 1;
int64_t p = 1;
while (b) {
int64_t bit = b & 1;
if (bit == 1) {
res = (res * a) % 998244353;
}
b = b >> 1;
a = (a * a) % 998244353;
}
return res % 998244353;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int64_t n, m, k;
cin >> n >> m >> k;
int64_t res = ncr(n - 1, k);
int64_t x = (m * modular_exp(m - 1, k)) % 998244353;
res = (res * x) % 998244353;
cout << res;
return 0;
}
| 3 |
#include <bits/stdc++.h>
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
const long double PI = acos((long double)(-1));
using namespace std;
struct mint {
long long x;
mint(long long x = 0) : x((x % mod + mod) % mod) {}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(long long t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
mint inv() const { return pow(mod - 2); }
mint& operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
int n, k;
vector<vector<int>> g;
vector<vector<mint>> dp;
void dfs(int cur, int pre) {
dp[cur][0] = 1;
dp[cur][k + 1] = 1;
for (auto nxt : g[cur]) {
if (nxt == pre) continue;
dfs(nxt, cur);
vector<mint> ndp(k * 2 + 3);
for (int i = 0; i <= k * 2; i++) {
for (int j = 0; j <= k * 2; j++) {
int t = min(i, j + 1);
if (i + j > 2 * k) t = max(i, j + 1);
ndp[t] += dp[cur][i] * dp[nxt][j];
}
}
dp[cur] = ndp;
}
}
int main() {
scanf("%d %d", &n, &k);
g.resize(n);
dp.assign(n, vector<mint>(k * 2 + 3));
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--;
v--;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(0, -1);
mint res = 0;
for (int i = 0; i <= k; i++) res += dp[0][i];
cout << res.x << "\n";
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lf = long double;
using pii = pair<ll, ll>;
using ppi = pair<ll, pii>;
using pll = pair<ll, ll>;
using pff = pair<lf, lf>;
using ti = tuple<ll, ll, ll>;
using base = complex<double>;
const lf PI = 3.14159265358979323846264338L;
template <typename T>
inline T umax(T& u, T v) {
return u = max(u, v);
}
template <typename T>
inline T umin(T& u, T v) {
return u = min(u, v);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 100000;
const ll mod = 1000000007;
int t, N, M, A[MAXN];
ll add(ll a, ll b) { return (a + b + mod) % mod; }
ll mul(ll a, ll b) { return (a * b) % mod; }
void solve() {
cin >> N;
string S;
cin >> S;
ll ans = S.size();
for (int i = 1; i <= N; i++) {
for (int k = 0; k < S[i - 1] - '1'; k++) {
if (S.size() > N) {
break;
}
for (int j = i; j < ans; j++) {
S.push_back(S[j]);
if (S.size() > N) break;
}
}
ans = add(ans, mul(ans - i, S[i - 1] - '1'));
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> t;
for (; t; t--) solve();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc, i, j;
scanf("%d", &tc);
while (tc--) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (a + c > b) {
int kiri = 0, kanan = c, mid, ans;
while (kiri <= kanan) {
mid = (kiri + kanan) / 2;
int sisa = c - mid;
if (a + mid > sisa + b) {
ans = mid;
kanan = mid - 1;
} else {
kiri = mid + 1;
}
}
printf("%d\n", c - ans + 1);
} else {
printf("0\n");
}
}
return 0;
};
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, a, b, f, g, ans, num[200100], qz[200100], top;
inline long long qh(long long u, long long v) {
return (u + v) * (v - u + 1) / 2;
}
inline long long gcd(long long u, long long v) {
for (; v;) {
u %= v;
swap(u, v);
}
return u;
}
int main() {
long long i, j, p, q, t;
cin >> n >> a >> b;
g = gcd(a, b);
num[top = 1] = 0;
for (i = 0;;) {
i += a;
num[++top] = i;
for (; i >= b;) {
i -= b;
if (!i) break;
num[++top] = i;
}
if (!i) break;
}
t = 0;
for (i = 1; i <= top; i++) {
t = max(t, num[i]);
qz[t]++;
}
t = 0;
for (i = 0; i <= min(a + b - 1, n); i++) {
t += qz[i];
ans += t;
}
if (n < a + b) {
cout << ans;
return 0;
}
if ((a + b) / g == n / g) {
for (i = n; i <= a + b; i++) ans += i / g + 1;
cout << ans;
return 0;
}
p = ((a + b) / g + 1) * g;
ans += (p - (a + b)) * ((a + b) / g + 1);
q = n / g * g - 1;
ans += (n - q) * (n / g + 1);
ans += qh(p / g + 1, q / g + 1) * g;
cout << ans;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
vector<int> x[26];
vector<int> in(26);
bool f;
vector<int> res;
void bfs() {
queue<int> q;
for (int i = 0; i <= 25; i++) {
if (in[i] == 0) {
q.push(i);
res.push_back(i);
}
}
while (!q.empty()) {
int ab = q.front();
q.pop();
for (auto i : x[ab]) {
--in[i];
if (in[i] == 0) {
res.push_back(i);
q.push(i);
}
}
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string tmp;
cin >> tmp;
v.push_back(tmp);
}
for (int i = 0; i < n - 1; i++) {
string a = v[i];
string b = v[i + 1];
bool f1 = 0;
for (int j = 0; j < min(a.size(), b.size()); j++) {
if (b[j] != a[j]) {
f1 = 1;
x[a[j] - 'a'].push_back(b[j] - 'a');
in[b[j] - 'a']++;
break;
}
if (j == min(a.size(), b.size()) - 1 && !f) {
if (a.size() >= b.size()) {
f = 1;
}
}
}
}
bfs();
if (res.size() != 26 || f) {
cout << "Impossible";
return 0;
}
for (char i : res) {
cout << (char)(i + 'a');
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long powmod(long long a, long long b) {
long long res = 1;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a;
a = a * a;
}
return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int n = (int)(s).size();
int ans = 0;
vector<int> cnt[10];
for (int i = 0; i < n; ++i) cnt[s[i] - 48].push_back(i);
for (int i = 0; i < 10; ++i) ans = max(ans, (int)(cnt[i]).size());
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
if (i == j) continue;
if ((int)(cnt[i]).size() == 0 || (int)(cnt[j]).size() == 0) continue;
int a = 0, b = 0, ni = 1, prev = -1, t = 0;
while ((ni && a < (int)(cnt[i]).size()) ||
(ni == 0 && b < (int)(cnt[j]).size())) {
while (ni && a < (int)(cnt[i]).size() && cnt[i][a] < prev) ++a;
if (ni && a < (int)(cnt[i]).size()) {
ni = 0;
prev = cnt[i][a++];
t++;
continue;
}
while (ni == 0 && b < (int)(cnt[j]).size() && cnt[j][b] < prev) ++b;
if (!ni && b < (int)(cnt[j]).size()) {
ni = 1;
prev = cnt[j][b++];
t++;
}
}
if ((t & 1) == 0) ans = max(ans, t);
}
}
ans = n - ans;
cout << ans << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
const int maxa = 10000000 + 10;
const int inf = (-1u) >> 1;
struct disjoint_set {
int p[maxn];
void clear(int sz) {
for (int i = (1); i <= (sz); ++i) {
p[i] = i;
}
}
int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }
bool join(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
p[x] = y;
return true;
}
};
int id[maxa], n;
disjoint_set ds;
bool check(int x, int y) { return id[x] && id[y] && ds.join(id[x], id[y]); }
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n); ++i) {
int t;
scanf("%d", &t);
id[t] = i;
}
ds.clear(n);
int ans = n;
for (int s = 1; s <= 6000; ++s) {
for (int t = (s & 1) + 1; t < s; t += 2) {
if (gcd(s, t) != 1) continue;
int x = 2 * s * t;
int y = s * s - t * t;
int z = s * s + t * t;
if (x >= maxa || y >= maxa) {
continue;
}
ans -= check(x, y);
if (z < maxa) {
ans -= check(x, z);
ans -= check(y, z);
}
}
}
printf("%d\n", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int tong[222222], a[222222];
int main() {
int n, i;
memset(a, -1, sizeof(a));
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int cnt = 0;
for (i = 0; i < n - 1; i++) cnt += a[i] == 0 ? 1 : a[i] == a[i + 1];
cout << cnt + (!a[i]);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<long long, bool>>> g;
vector<long long> f, f1, sz;
bool ok(long long a) {
while (a) {
if (a % 10 != 4 && a % 10 != 7) return 0;
a /= 10;
}
return 1;
}
void upd(long long v, long long p = -1) {
sz[v] = 1;
for (auto to : g[v]) {
if (to.first != p) {
upd(to.first, v);
sz[v] += sz[to.first];
}
}
}
void dfs1(long long v, long long p = -1) {
for (auto to : g[v]) {
if (to.first != p) {
dfs1(to.first, v);
if (to.second)
f[v] += sz[to.first];
else
f[v] += f[to.first];
}
}
}
void dfs2(long long v, long long p = -1) {
for (auto to : g[v]) {
if (to.first != p) {
if (to.second)
f1[to.first] = sz[0] - sz[to.first];
else
f1[to.first] = f1[v] + f[v] - f[to.first];
dfs2(to.first, v);
}
}
}
signed main() {
long long n;
cin >> n;
f.resize(n);
f1.resize(n);
g.resize(n);
sz.resize(n);
for (long long i = 0; i < n - 1; ++i) {
long long a, b, c;
cin >> a >> b >> c;
a--, b--;
c = ok(c);
g[a].push_back({b, c});
g[b].push_back({a, c});
}
upd(0);
dfs1(0);
f1[0] = 0;
dfs2(0);
long long ans = 0;
for (long long i = 0; i < n; ++i)
ans += f[i] * (f[i] - 1) + f1[i] * (f1[i] - 1) + 2 * f1[i] * f[i];
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1605;
int n, m, tot;
int a[MAXN][MAXN];
int sz[MAXN * MAXN];
int ans[MAXN * MAXN];
int cnt[MAXN * MAXN];
int fa1[MAXN * MAXN];
int fa2[MAXN * MAXN];
double v[MAXN][MAXN];
double t[MAXN][MAXN];
int getroot(int *fa, int u) {
return u == fa[u] ? u : fa[u] = getroot(fa, fa[u]);
}
void run() {
memcpy(t, v, sizeof(t));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j]) {
double sum = 0, cnt = 0;
for (int k = i - 4; k <= i + 4; k++)
for (int l = j - 4; l <= j + 4; l++)
if (k >= 1 && k <= n && l >= 1 && l <= m &&
(i - k) * (i - k) + (j - l) * (j - l) <= 25)
sum += t[k][l], cnt++;
v[i][j] = sum / cnt;
}
}
void merge(int *fa, int xa, int ya, int xb, int yb) {
if (!a[xa][ya] || !a[xb][yb]) return;
fa[getroot(fa, (xa - 1) * m + ya)] = getroot(fa, (xb - 1) * m + yb);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
fa1[(i - 1) * m + j] = fa2[(i - 1) * m + j] = (i - 1) * m + j;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j]) {
v[i][j] = 1;
if (i > 1) merge(fa1, i, j, i - 1, j);
if (i < n) merge(fa1, i, j, i + 1, j);
if (j > 1) merge(fa1, i, j, i, j - 1);
if (j < m) merge(fa1, i, j, i, j + 1);
}
run();
run();
run();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] && v[i][j] < 0.2) {
if (i > 1 && v[i - 1][j] < 0.2) merge(fa2, i, j, i - 1, j);
if (i < n && v[i + 1][j] < 0.2) merge(fa2, i, j, i + 1, j);
if (j > 1 && v[i][j - 1] < 0.2) merge(fa2, i, j, i, j - 1);
if (j < m && v[i][j + 1] < 0.2) merge(fa2, i, j, i, j + 1);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] && v[i][j] < 0.2) sz[getroot(fa2, (i - 1) * m + j)]++;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] && v[i][j] < 0.2 &&
getroot(fa2, (i - 1) * m + j) == (i - 1) * m + j &&
sz[(i - 1) * m + j] >= 5)
cnt[getroot(fa1, (i - 1) * m + j)]++;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] && getroot(fa1, (i - 1) * m + j) == (i - 1) * m + j)
ans[++tot] = cnt[(i - 1) * m + j];
sort(ans + 1, ans + tot + 1);
printf("%d\n", tot);
for (int i = 1; i <= tot; i++) printf("%d ", ans[i]);
printf("\n");
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
bool sortbysec(const pair<int, int>& a, const pair<int, int>& b) {
if (a.first == b.first) return a.second > b.second;
return a.first < b.first;
}
class cmp {
public:
bool operator()(vector<int> v1, vector<int> v2) { return v1[0] < v2[0]; }
};
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a;
swap(a, m);
u -= t * v;
swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod())
v = static_cast<Type>(x);
else
v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const {
return static_cast<U>(value);
}
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) {
if ((value += other.value) >= mod()) value -= mod();
return *this;
}
Modular& operator-=(const Modular& other) {
if ((value -= other.value) < 0) value += mod();
return *this;
}
template <typename U>
Modular& operator+=(const U& other) {
return *this += Modular(other);
}
template <typename U>
Modular& operator-=(const U& other) {
return *this -= Modular(other);
}
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) {
Modular result(*this);
*this += 1;
return result;
}
Modular operator--(int) {
Modular result(*this);
*this -= 1;
return result;
}
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value,
Modular>::type&
operator*=(const Modular& rhs) {
value = normalize(static_cast<int64_t>(value) *
static_cast<int64_t>(rhs.value));
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, long long>::value,
Modular>::type&
operator*=(const Modular& rhs) {
long long q = static_cast<long long>(static_cast<long double>(value) *
rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value,
Modular>::type&
operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) {
return *this *= Modular(inverse(other.value, mod()));
}
friend const Type& abs(const Modular& x) { return x.value; }
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename V, typename U>
friend V& operator>>(V& stream, Modular<U>& number);
private:
Type value;
};
template <typename T>
bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) {
return lhs.value == rhs.value;
}
template <typename T, typename U>
bool operator==(const Modular<T>& lhs, U rhs) {
return lhs == Modular<T>(rhs);
}
template <typename T, typename U>
bool operator==(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) == rhs;
}
template <typename T>
bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) {
return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(const Modular<T>& lhs, U rhs) {
return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(U lhs, const Modular<T>& rhs) {
return !(lhs == rhs);
}
template <typename T>
bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) {
return lhs.value < rhs.value;
}
template <typename T>
Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T>
Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T>
Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T>
Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
assert(b >= 0);
Modular<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
bool IsZero(const Modular<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
return stream << number();
}
template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, long long>::type x;
stream >> x;
number.value = Modular<T>::normalize(x);
return stream;
}
constexpr int md = 1e9 + 7;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
long long binpow(long long a, long long b) {
if (b == 0) return 1;
long long res = binpow(a, b / 2);
if (b % 2)
return res * res * a;
else
return res * res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int k;
cin >> k;
long long t = binpow(2, k) - 2;
Mint res = 4;
res = power(res, t);
res *= 6;
cout << res;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)(1e2) + 1;
vector<int> g[N];
int vis[N];
void dfs(int s) {
vis[s] = 1;
for (int i : g[s]) {
if (!vis[i]) dfs(i);
}
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
if (n < 3 || m != n) {
cout << "NO";
return 0;
}
int cc = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cc++;
dfs(i);
}
}
cout << ((cc == 1) ? "FHTAGN!" : "NO");
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, q, a[210000], w[210000];
long long W[210000], AW[210000];
void modify(long long *b, int x, long long y) {
for (; x <= n; x += x & -x) b[x] += y;
}
long long query(long long *b, int x) {
long long ans = 0;
for (; x; x -= x & -x) ans += b[x];
return ans;
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
a[i] -= i;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &w[i]);
}
for (int i = 1; i <= n; i++) {
modify(W, i, w[i]);
modify(AW, i, 1LL * a[i] * w[i] % 1000000007);
}
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
if (x < 0) {
x = -x;
modify(W, x, -w[x]);
modify(AW, x, -(1LL * a[x] * w[x] % 1000000007));
w[x] = y;
modify(W, x, w[x]);
modify(AW, x, (1LL * a[x] * w[x] % 1000000007));
} else {
long long base = query(W, x - 1);
long long SW = query(W, y) - base;
int l = x - 1, r = y, mid;
while (l < r - 1) {
mid = (l + r) / 2;
if (query(W, mid) - base >= (SW + 1) / 2)
r = mid;
else
l = mid;
}
long long ans = 0;
if (x < r) {
ans += 1LL * a[r] * ((query(W, r - 1) - query(W, x - 1)) % 1000000007) %
1000000007;
ans -= (query(AW, r - 1) - query(AW, x - 1)) % 1000000007;
}
if (r < y) {
ans -= 1LL * a[r] * ((query(W, y) - query(W, r)) % 1000000007) %
1000000007;
ans += (query(AW, y) - query(AW, r)) % 1000000007;
}
ans = ((ans % 1000000007) + 1000000007) % 1000000007;
printf("%I64d\n", ans);
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, mod = 1000000007;
const double pi = 3.1415926535897932, eps = 1e-6;
void chmax(int &x, int y) {
if (x < y) x = y;
}
void chmin(int &x, int y) {
if (x > y) x = y;
}
int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = (long long)ret * x % mod;
x = (long long)x * x % mod;
y >>= 1;
}
return ret;
}
int n;
char s[505];
int dp[505][505];
int dfs(int l, int r) {
int &res = dp[l][r];
if (res != -1) return res;
if (l == r) return res = 0;
for (int(k) = (l); (k) < (r); (k)++)
res = max(res, dfs(l, k) + dfs(k + 1, r) + (s[l] == s[k + 1]));
return res;
}
int main() {
scanf("%d%s", &n, s + 1);
for (int(i) = (1); (i) <= (n); (i)++)
for (int(j) = (1); (j) <= (n); (j)++) dp[i][j] = -1;
printf("%d\n", n - dfs(1, n));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("B.in");
ofstream fout("B.out");
vector<pair<int, int> > v;
int n, m, c;
int t[5001], r[5001], l[5001], mm[5001], d[5001], a[5001];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) a[i] = 100000000;
for (int i = 1; i <= m; ++i) {
cin >> t[i] >> l[i] >> r[i] >> mm[i];
}
for (int i = 1; i <= m; ++i) {
if (t[i] == 1) {
for (int j = l[i]; j <= r[i]; ++j) {
d[j] += mm[i];
}
} else {
for (int j = l[i]; j <= r[i]; ++j) {
a[j] = min(a[j], mm[i] - d[j]);
}
}
}
memset(d, 0, sizeof(d));
for (int i = 1; i <= m; ++i) {
if (t[i] == 1) {
for (int j = l[i]; j <= r[i]; ++j) {
d[j] += mm[i];
}
} else {
int maxv = -100000000;
for (int j = l[i]; j <= r[i]; ++j) {
maxv = max(maxv, a[j] + d[j]);
}
if (maxv != mm[i]) {
cout << "NO";
return 0;
}
}
}
cout << "YES\n";
for (int i = 1; i <= n; ++i) {
cout << a[i] << " ";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, char> > graph[105];
int dp[105][105][26][2];
int visit[105][105][26][2];
bool func(int u, int v, int turn, char a) {
if (visit[u][v][a - 'a'][turn]) return dp[u][v][a - 'a'][turn];
if (turn) {
bool res = false;
for (int j = 0; j < graph[u].size(); j++) {
int y = graph[u][j].first;
char c = graph[u][j].second;
if (c >= a) {
if (func(y, v, 0, c) == 0) {
res = true;
break;
}
}
}
visit[u][v][a - 'a'][turn] = 1;
return dp[u][v][a - 'a'][turn] = res;
} else {
bool res = false;
for (int j = 0; j < graph[v].size(); j++) {
int y = graph[v][j].first;
char c = graph[v][j].second;
if (c >= a) {
if (func(u, y, 1, c) == 0) {
res = true;
break;
}
}
}
visit[u][v][a - 'a'][turn] = 1;
return dp[u][v][a - 'a'][turn] = res;
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
int n, m;
cin >> n >> m;
while (m--) {
int u, v;
char a;
cin >> u >> v >> a;
graph[u].push_back(make_pair(v, a));
}
for (int i = 1; i <= n; i++) {
string a;
for (int j = 1; j <= n; j++) {
if (func(i, j, 1, 'a'))
a.push_back('A');
else
a.push_back('B');
}
cout << a << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int k;
cin >> k;
map<int, int> sum;
map<int, int> vall;
for (int i = 1; i <= k; ++i) {
int n;
cin >> n;
int a[n + 10];
int cnt = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
cnt += a[i];
}
for (int j = 1; j <= n; ++j) {
if (sum[cnt - a[j]] != 0) {
cout << "YES\n";
cout << sum[cnt - a[j]] << ' ' << vall[cnt - a[j]] << '\n';
cout << i << ' ' << j << '\n';
return 0;
}
}
for (int j = 1; j <= n; ++j) {
sum[cnt - a[j]] = i;
vall[cnt - a[j]] = j;
}
}
cout << "NO\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
constexpr int kDx[] = {1, 0, -1, 0}, kDy[] = {0, 1, 0, -1};
struct Query {
int x, y, c, pre;
} b[2000001];
int n, m, q, fa[4000001], a[305][305], ans[2000001], id[301][301], cnt, tmp;
int Find(int x) {
if (x == fa[x]) {
return x;
}
return fa[x] = Find(fa[x]);
}
void Merge(int x, int y) {
int fx = Find(x), fy = Find(y);
if (fx == fy) {
return;
}
tmp--;
fa[fx] = fy;
}
void Check(int x, int y) {
if (a[x][y] == a[x][y - 1]) {
Merge(id[x][y], id[x][y - 1]);
}
if (a[x][y] == a[x][y + 1]) {
Merge(id[x][y], id[x][y + 1]);
}
if (a[x][y] == a[x - 1][y]) {
Merge(id[x][y], id[x - 1][y]);
}
if (a[x][y] == a[x + 1][y]) {
Merge(id[x][y], id[x + 1][y]);
}
}
int main(int argc, char const *argv[]) {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cin >> n >> m >> q;
for (int i = 1; i <= q; i++) {
std::cin >> b[i].x >> b[i].y >> b[i].c;
b[i].pre = a[b[i].x][b[i].y];
a[b[i].x][b[i].y] = b[i].c;
}
ans[0] = 1;
std::memset(a, -1, sizeof(a));
for (int i = 1; i <= n; i++) {
std::memset(a[i] + 1, 0, 4 * m);
}
for (int i = 1; i <= q; i++) {
if (b[i].c == b[i].pre) {
continue;
}
tmp = 1;
a[b[i].x][b[i].y] = b[i].c;
id[b[i].x][b[i].y] = ++cnt;
fa[cnt] = cnt;
Check(b[i].x, b[i].y);
ans[i] += tmp;
}
std::memset(fa, 0, sizeof(fa));
cnt = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
id[i][j] = ++cnt;
fa[cnt] = cnt;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
Check(i, j);
}
}
for (int i = q; i >= 1; i--) {
if (b[i].pre == b[i].c) {
continue;
}
tmp = 1;
a[b[i].x][b[i].y] = b[i].pre;
id[b[i].x][b[i].y] = ++cnt;
fa[cnt] = cnt;
Check(b[i].x, b[i].y);
ans[i] -= tmp;
}
std::partial_sum(ans, ans + q + 1, ans);
for (int i = 1; i <= q; i++) {
std::cout << ans[i] << '\n';
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int> > T(n);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
T[a].push_back(b);
T[b].push_back(a);
}
vector<bool> odw(n, false);
stack<int> S;
vector<double> odl(n);
odw[0] = true;
S.push(0);
odl[0] = 1;
double odp = 0.0;
while (!S.empty()) {
int akt = S.top();
S.pop();
odp += 1.0 / odl[akt];
for (int i = 0; i < T[akt].size(); ++i) {
if (!odw[T[akt][i]]) {
odw[T[akt][i]] = true;
S.push(T[akt][i]);
odl[T[akt][i]] = odl[akt] + 1;
}
}
}
cout.precision(10);
cout << odp;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
int main() {
int n, k;
cin >> n >> k;
string s;
cin >> s;
int c[30] = {};
for (int i = 0; i < (n); i++) c[s[i] - 'a']++;
bool flag = true;
for (int i = 0; i < (26); i++)
if (c[i] > k) {
flag = false;
}
cout << (flag ? "YES" : "NO") << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int N;
string s;
stack<int> S;
int main(void) {
cin >> s;
N = s.size();
int p;
for (int i = 0; i < N; i++) {
if (s[i] == '(')
S.push(i);
else {
if (S.size() == 0) {
printf("-1");
return 0;
}
S.pop();
}
if (s[i] == '#') p = i;
}
for (int i = N - 1; i >= 0; i--) {
if (s[i] == ')' || s[i] == '#')
break;
else {
printf("-1");
return 0;
}
}
if (!S.empty() && S.top() > p) {
printf("-1\n");
return 0;
}
int otvorene = 0;
for (int i = 0; i < N; i++) {
if (s[i] == '(') otvorene++;
if (s[i] == ')') otvorene--;
if (s[i] == '#' && i != p) printf("1\n");
if (s[i] == '#') otvorene--;
}
printf("%d\n", otvorene + 1);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char arr[200000 + 10];
int main() {
int n;
while (~scanf("%s", arr)) {
long long i, j, cnt = 0, posi, seat, t, sum = 0;
for (i = 0, posi = 0; arr[i] <= '9' && arr[i] >= '0'; i++)
posi = posi * 10 + arr[i] - '0';
j = i;
t = (posi - 1) / 4;
sum = t * 16;
t = posi % 4;
if (t == 1 || t == 3) {
if (arr[j] == 'f') {
sum += 1;
}
if (arr[j] == 'e') {
sum += 2;
}
if (arr[j] == 'd') {
sum += 3;
}
if (arr[j] == 'a') {
sum += 4;
}
if (arr[j] == 'b') {
sum += 5;
}
if (arr[j] == 'c') {
sum += 6;
}
} else {
if (arr[j] == 'f') {
sum += 1 + 7;
}
if (arr[j] == 'e') {
sum += 2 + 7;
}
if (arr[j] == 'd') {
sum += 3 + 7;
}
if (arr[j] == 'a') {
sum += 4 + 7;
}
if (arr[j] == 'b') {
sum += 5 + 7;
}
if (arr[j] == 'c') {
sum += 6 + 7;
}
}
printf("%I64d\n", sum);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long max_limit = 1e6 + 6;
long long a[max_limit], ans[max_limit], lastocc[max_limit];
int main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
memset(ans, 0, sizeof(ans));
memset(lastocc, 0, sizeof(lastocc));
ans[0] = 0;
double sum = 0.0;
for (long long i = 1; i <= n; i++) {
ans[i] = ans[i - 1] + (i - lastocc[a[i]]);
lastocc[a[i]] = i;
sum += ans[i];
}
double final = (2 * (sum - n) + n) / (n * n * 1.0);
cout << fixed << setprecision(6) << final << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long v[100005];
long long st[400005];
void init(int idx, int a, int b) {
if (a == b) {
st[idx] = v[a];
} else {
int m = (a + b) / 2;
init(idx * 2 + 1, a, m);
init(idx * 2 + 2, m + 1, b);
st[idx] = max(st[idx * 2 + 1], st[idx * 2 + 2]);
}
}
long long query(int idx, int a, int b, int i, int j) {
if (i > b || j < a)
return -1;
else if (j >= b && i <= a)
return st[idx];
int m = (a + b) / 2;
long long x0 = query(idx * 2 + 1, a, m, i, j);
long long x1 = query(idx * 2 + 2, m + 1, b, i, j);
return max(x0, x1);
}
int main() {
int n;
int qry;
ios_base ::sync_with_stdio(0);
cin.tie(0);
char c;
int x, y;
cin >> n;
for (int i = 0; i < n; ++i) cin >> v[i];
init(0, 0, n - 1);
for (int i = 0; i < n; ++i) {
long long mn = query(0, 0, n - 1, i + 1, n - 1);
if (v[i] > mn)
cout << 0 << ' ';
else
cout << mn - v[i] + 1 << ' ';
}
cout << '\n';
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, N, M, K;
int c[1000000], p[1000000];
struct node {
int a, b;
bool operator<(const node& t) const { return t.a > a; }
} t1, t2;
priority_queue<node> q;
void clear() {
memset(c, 0, sizeof(c));
memset(p, 0, sizeof(p));
while (!q.empty()) q.pop();
}
void init() {
t1.a = 1, t2.a = 2;
scanf("%d", &N);
for (i = 1; i <= N; i++) {
scanf("%d", &c[i]);
if (c[i] == 0) {
if (q.size() >= 3) {
p[i] = 3;
node x = q.top();
q.pop();
p[x.b] = -3;
x = q.top();
q.pop();
p[x.b] = -2;
x = q.top();
q.pop();
p[x.b] = -1;
} else if (q.size() == 2) {
p[i] = 2;
node x = q.top();
q.pop();
p[x.b] = -2;
x = q.top();
q.pop();
p[x.b] = -1;
} else if (q.size() == 1) {
p[i] = 1;
node x = q.top();
q.pop();
p[x.b] = -1;
} else {
p[i] = 100;
}
while (!q.empty()) q.pop();
} else {
node x;
p[i] = 4;
x.a = c[i], x.b = i;
q.push(x);
}
}
}
void calc() {
char str[10][10] = {"Queue", "Front", "Stack"};
for (i = 1; i <= N; i++) {
if (p[i] == 100)
printf("0\n");
else if (p[i] == 4) {
printf("pushBack\n");
} else if (p[i] >= 1) {
printf("%d", p[i]);
for (j = 0; j <= p[i] - 1; j++) {
printf(" pop%s", str[j]);
}
printf("\n");
} else {
printf("push%s\n", str[-p[i] - 1]);
}
}
}
int main() {
clear();
init();
calc();
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
string mp[35], ans[35];
long long int color[35][35], n, m, cnt = 0, res = 0;
vector<pair<long long int, long long int> > sq[35];
vector<long long int> used, unused;
vector<pair<pair<long long int, long long int>,
pair<long long int, long long int> > >
domino;
bool check[7][7];
int main() {
for (long long int i = 0; i < ((long long int)(35)); i++)
for (long long int j = 0; j < ((long long int)(35)); j++) color[i][j] = -1;
cin >> n >> m;
for (long long int i = 0; i < ((long long int)(n)); i++) cin >> mp[i];
for (long long int i = 0; i < ((long long int)(n)); i++)
for (long long int j = 0; j < ((long long int)(m)); j++)
if (color[i][j] == -1 && mp[i][j] != '.') {
for (long long int k = 0; k < ((long long int)(2)); k++)
for (long long int l = 0; l < ((long long int)(2)); l++)
color[i + k][j + l] = cnt;
for (long long int k = 0; k < ((long long int)(2)); k++)
for (long long int l = 0; l < ((long long int)(2)); l++)
sq[cnt].push_back(make_pair(i + k, j + l));
cnt++;
}
for (long long int i = 0; i < ((long long int)(n)); i++)
for (long long int j = 0; j < ((long long int)(m)); j++)
for (long long int k = 0; k < ((long long int)(2)); k++)
for (long long int l = 0; l < ((long long int)(2)); l++)
if (color[i][j] != -1 && k + l == 1) {
if (i + k < n && j + l < m && mp[i + k][j + l] == mp[i][j]) {
if (color[i + k][j + l] == color[i][j])
used.push_back(color[i + k][j + l]);
domino.push_back(
make_pair(make_pair(i, j), make_pair(i + k, j + l)));
}
}
for (long long int i = 0; i < ((long long int)(14)); i++)
if (find(used.begin(), used.end(), i) == used.end()) unused.push_back(i);
for (long long int i = 0; i < ((long long int)(((long long int)used.size())));
i++)
for (long long int j = 0; j < ((long long int)(4)); j++)
color[sq[used[i]][j].first][sq[used[i]][j].second] = i;
vector<long long int> per;
for (long long int i = 0;
i < ((long long int)(((long long int)unused.size()))); i++)
per.push_back((i + ((long long int)used.size())) % 7);
sort(per.begin(), per.end());
do {
for (long long int i = 0; i < ((long long int)(7)); i++)
for (long long int j = 0; j < ((long long int)(7)); j++)
check[i][j] = false;
for (long long int i = 0;
i < ((long long int)(((long long int)unused.size()))); i++)
for (long long int j = 0; j < ((long long int)(4)); j++) {
color[sq[unused[i]][j].first][sq[unused[i]][j].second] = per[i];
}
for (long long int i = 0;
i < ((long long int)(((long long int)domino.size()))); i++) {
long long int c0 = color[domino[i].first.first][domino[i].first.second];
long long int c1 = color[domino[i].second.first][domino[i].second.second];
check[max(c0, c1)][min(c0, c1)] = true;
}
bool ok = true;
for (long long int i = 0; i < 7 && ok; i++)
for (long long int j = 0; j <= i && ok; j++)
if (!check[i][j]) ok = false;
if (ok) {
for (long long int i = 0; i < ((long long int)(n)); i++) {
ans[i] = "";
for (long long int j = 0; j < ((long long int)(m)); j++)
ans[i] += ((color[i][j] == -1) ? "." : string(1, '0' + color[i][j]));
}
res++;
}
} while (next_permutation(per.begin(), per.end()));
for (long long int i = 0; i < ((long long int)(((long long int)used.size())));
i++)
res *= (7 - i);
cout << res << endl;
for (long long int i = 0; i < ((long long int)(n)); i++)
cout << ans[i] << endl;
}
| 8 |
#include <bits/stdc++.h>
int ext[45][45];
int f[45][45][45][45], h, w;
char mat[45][45];
inline int min(int a, int b) { return a < b ? a : b; }
int main() {
int m;
scanf("%d%d%d", &h, &w, &m);
for (int i = 1; i <= h; i++) {
scanf("%s", mat[i] + 1);
for (int j = 1; j <= w; j++)
ext[i][j] = mat[i][j] == '1' ? 0 : (ext[i][j - 1] + 1);
}
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++) {
for (int ii = i; ii <= h; ii++) {
for (int jj = j; jj <= w; jj++) {
f[i][j][ii][jj] = f[i][j][ii - 1][jj] + f[i][j][ii][jj - 1] -
f[i][j][ii - 1][jj - 1];
int minn = 0x3f3f3f3f;
for (int k = ii; k >= i && minn; k--) {
minn = min(minn, min(jj - j + 1, ext[k][jj]));
f[i][j][ii][jj] += minn;
}
}
}
}
while (m--) {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
printf("%d\n", f[x1][y1][x2][y2]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int sizz = 1e6 + 4;
const long long int MIN = numeric_limits<long long int>::min();
bool compare(const pair<int, pair<int, int> > &a,
const pair<int, pair<int, int> > &b) {
return a.first < b.first;
}
int main() {
long long int n, m;
cin >> n >> m;
vector<pair<int, pair<int, int> > > v;
for (int i = 0; i < m; i++) {
long long int l, r;
cin >> l >> r;
v.push_back({(r - l + 1), {l, r}});
}
int flag[n + 1];
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
flag[i] = 0;
} else {
flag[i] = 1;
}
}
for (int i = 1; i <= n; i++) {
cout << flag[i];
}
cout << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T inline sqr(T x) {
return x * x;
}
const long double pi = 3.1415926535897932384626433832795;
const long double eps = 1e-8;
bool le(pair<long long, long long> a, pair<long long, long long> b) {
return a.first * b.second < a.second * b.first;
}
const int N = 200500;
long long a[N];
long long pref[N];
int n;
long long sum(int l, int r) {
long long res = 0;
if (r) res += pref[r - 1];
if (l) res -= pref[l - 1];
return res;
}
void pr(pair<long long, long long> a) {
cerr << a.first << " " << a.second << endl;
}
pair<long long, long long> solve() {
sort(a, a + n);
pref[0] = a[0];
for (int i = 1; i <= (int)(n - 1); ++i) pref[i] = pref[i - 1] + a[i];
pair<long long, long long> best1(-1, 1);
int med1, l1;
for (int med = 0; med < n; med++) {
int l = 0, r = min(med, n - med - 1) + 1;
while (l + 1 < r) {
int m = (l + r) / 2;
pair<long long, long long> cur(
sum(med - m, med) + sum(n - m, n) + a[med] - a[med] * (m + m + 1),
m + m + 1);
m--;
pair<long long, long long> prev(
sum(med - m, med) + sum(n - m, n) + a[med] - a[med] * (m + m + 1),
m + m + 1);
m++;
if (le(prev, cur))
l = m;
else
r = m;
}
pair<long long, long long> cur(
sum(med - l, med) + sum(n - l, n) + a[med] - a[med] * (l + l + 1),
l + l + 1);
if (le(best1, cur)) {
best1 = cur;
med1 = med;
l1 = l;
}
}
cout << l1 + l1 + 1 << "\n";
for (int i = med1 - l1; i <= med1; i++) cout << a[i] << " ";
for (int i = n - l1; i < n; i++) cout << a[i] << " ";
return best1;
}
void test(int x) {
n = x;
for (int tt = 0; tt < (int)(1000000); ++tt) {
for (int i = 0; i < (int)(n); ++i) a[i] = rand() % 10;
pair<long long, long long> best(-1, 1);
for (int msk = 1; msk <= (int)((1 << n) - 1); ++msk) {
pair<long long, long long> cur;
vector<int> c;
for (int i = 0; i < (int)(n); ++i)
if (msk & (1 << i)) {
c.push_back(a[i]);
}
sort((c).begin(), (c).end());
if (c.size() % 2 == 1) {
cur.second = c.size();
cur.first =
accumulate((c).begin(), (c).end(), 0) - c.size() * c[c.size() / 2];
} else {
cur.second = c.size();
cur.first = accumulate((c).begin(), (c).end(), 0) -
c.size() / 2 * (c[c.size() / 2] + c[c.size() / 2 - 1]);
}
if (le(best, cur)) best = cur;
}
pair<long long, long long> x = solve();
if (le(x, best) || le(best, x)) {
pr(x);
pr(best);
for (int i = 0; i < (int)(n); ++i) cerr << a[i] << " ";
assert(false);
}
}
exit(0);
}
int main() {
cout.precision(10);
cout << fixed;
srand(time(NULL));
ios::sync_with_stdio(false);
scanf("%d", &n);
for (int i = 0; i < (int)(n); ++i) {
int x;
scanf("%d", &x);
a[i] = x;
}
solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int len = s.length();
if (len <= 20) {
cout << 1 << " " << len << endl << s;
} else {
int count = len / 20;
if (len % 20 != 0) count++;
double middle_len = (double)len / (double)count;
int max_len = (int)ceil(middle_len);
int need_astra = count * max_len - len;
cout << count << " " << max_len << endl;
int pos_str = 0;
for (int i = 0; i < count; i++) {
if (i < need_astra) {
cout << "*";
for (int j = 1; j < max_len; j++) cout << s[pos_str++];
} else
for (int j = 0; j < max_len; j++) cout << s[pos_str++];
cout << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353, MAX = 100003, INF = 1 << 30;
long long rui(long long a, long long b) {
if (b == 1)
return a % mod;
else if (b == 0)
return 1;
else if (b % 2 == 0)
return (rui(a, b / 2) * rui(a, b / 2)) % mod;
else
return (rui(a, b - 1) * a) % mod;
}
int main() {
int Q;
cin >> Q;
for (int q = 0; q < Q; q++) {
int N, M;
cin >> N >> M;
vector<int> S[N], color(N, 0);
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
S[a].push_back(b);
S[b].push_back(a);
}
long long ans = 1;
for (int i = 0; i < N; i++) {
if (color[i]) continue;
int odd = 1, even = 0;
color[i] = 1;
queue<int> Q;
Q.push(i);
while (!Q.empty()) {
int a = Q.front();
Q.pop();
for (int j = 0; j < S[a].size(); j++) {
int b = S[a][j];
if (color[b] == 0) {
color[b] = 3 - color[a];
if (color[b] % 2)
odd++;
else
even++;
Q.push(b);
} else if (color[a] == color[b]) {
ans = 0;
}
}
}
ans *= (rui(2, odd) + rui(2, even)) % mod;
ans %= mod;
}
cout << ans << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int n;
cin >> n;
vector<long long int> all(n);
vector<long long> k(n);
for (int i = 0; i < n; i++) {
cin >> all[i];
k[i] = all[i];
}
int cnt = int(sqrt(n)) + 1;
vector<long long> f(cnt);
for (int i = 0; i < n; i++) f[i / cnt] += all[i];
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int a;
cin >> a;
if (a == 1) {
int j, x;
cin >> j >> x;
j--;
int now = j;
while (j / cnt == now / cnt && now < n) {
if (f[j / cnt] == 0) break;
if (all[now] >= x) {
all[now] -= x;
f[j / cnt] -= x;
x = 0;
break;
}
if (all[now] < x) {
x -= all[now];
f[j / cnt] -= all[now];
all[now] = 0;
}
now++;
}
if (x > 0 && j / cnt < cnt - 1) {
int now = j / cnt + 1;
while (now < cnt) {
if (f[now] >= x) {
for (int h = now * cnt; h < min(now * cnt + cnt, n); h++) {
if (all[h] >= x) {
all[h] -= x;
f[now] -= x;
x = 0;
break;
} else {
x -= all[h];
f[now] -= all[h];
all[h] = 0;
}
}
break;
}
if (f[now] < x) {
x -= f[now];
f[now] = 0;
}
now++;
}
}
}
if (a == 2) {
int j;
cin >> j;
j--;
if (f[j / cnt] > 0)
cout << k[j] - all[j] << endl;
else
cout << k[j] << endl;
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1010;
struct Point {
long long x, y;
friend Point operator-(const Point &a, const Point &b) {
return (Point){a.x - b.x, a.y - b.y};
}
friend Point operator+(const Point &a, const Point &b) {
return (Point){a.x + b.x, a.y + b.y};
}
friend long long operator*(const Point &a, const Point &b) {
return a.x * b.y - a.y * b.x;
}
} P[MaxN], S[10];
long long dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }
vector<int> path[10][MaxN];
int n, k;
Point read() {
int x, y;
scanf("%d%d", &x, &y);
return (Point){x, y};
}
bool on_segment(const Point &x, const Point &a, const Point &b) {
if ((x - a) * (x - b) != 0) return 0;
return dot(x - a, b - a) > 0 && dot(x - b, a - b) > 0;
}
int key;
int ord[10], otot;
int done[MaxN], dtime;
bool check(int b) {
if (otot == k) return 0;
int now = ord[otot];
++otot;
for (auto x : path[now][b])
if (done[x] != dtime) {
if (!check(x)) return 0;
}
done[b] = dtime;
return 1;
}
bool dfs(int now) {
static int vis[10];
if (now == k) {
++dtime;
otot = 0;
return check(key);
}
bool ret = 0;
for (int i = 0; i < k && !ret; ++i)
if (!vis[i]) {
ord[now] = i;
vis[i] = 1;
ret |= dfs(now + 1);
vis[i] = 0;
}
return ret;
}
bool afraid(int p) {
key = p;
return dfs(0);
}
int main() {
scanf("%d%d", &k, &n);
for (int i = 0; i < k; ++i) S[i] = read();
for (int i = 0; i < n; ++i) P[i] = read();
for (int i = 0; i < k; ++i)
for (int j = 0; j < n; ++j) {
auto &tmp = path[i][j];
for (int k = 0; k < n; ++k)
if (on_segment(P[k], S[i], P[j])) tmp.push_back(k);
}
int ans = 0;
for (int i = 0; i < n; ++i)
if (afraid(i)) ++ans;
cout << ans << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
int main() {
int s, i, y;
int f = 0;
scanf("%d", &s);
int n[s];
int r[s];
for (i = 0; i < s; i++) {
scanf("%d", &n[i]);
}
int g = n[s - 1];
for (i = s - 2; i >= 0; i--) {
if (n[i] > g) {
g = n[i];
r[i] = 0;
} else {
r[i] = (g - n[i]) + 1;
}
}
r[s - 1] = 0;
for (i = 0; i < s; i++) {
printf("%d ", r[i]);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const long double pi = acos(-1);
const long long int mod = 1e9 + 7;
bool debug = false;
long long int dx[] = {0, 1, 0, -1};
long long int dy[] = {1, 0, -1, 0};
bool sortbysec(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
return (a.second < b.second);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int t = 1, n, i, j, k, len, x, y, z, c, f, flag, p, q, mx, mn, l, r,
sum, ans, tmp, it, pos, avg, m, cnt;
string s;
char ch;
vector<long long int> v;
vector<pair<long long int, long long int>> vec;
unordered_map<long long int, long long int> mappu;
pair<long long int, long long int> pr;
{
long long int avg;
f = 0;
sum = 0;
flag = 0;
ans = 0;
cnt = 0;
v.clear();
mappu.clear();
vec.clear();
cin >> n >> mx >> avg;
for (i = 0; i < n; i++) {
cin >> l >> r;
vec.push_back({l, r});
sum += l;
}
long long int req = avg * n - sum;
sort(vec.begin(), vec.end(), sortbysec);
for (auto it : vec) {
if (req <= 0) break;
l = mx - it.first;
x = min(req, l);
req -= x;
ans += x * it.second;
}
cout << ans << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 * 4;
inline int get() {
int x;
cin >> x;
return x;
}
inline long long getlong() {
long long x;
cin >> x;
return x;
}
inline string getString() {
string s;
cin >> s;
return s;
}
template <typename T>
inline vector<T> getvector(int len) {
vector<T> a(len);
for (auto &it : a) cin >> it;
return a;
}
inline vector<int> getvector(int len) {
vector<int> a(len);
for (auto &it : a) cin >> it;
return a;
}
inline vector<pair<int, int>> getvector_andPairCnt(int len) {
vector<pair<int, int>> a(len);
int i = 1;
for (auto &it : a) {
it.second = i;
i++;
cin >> it.first;
}
return a;
}
double power_log(double power, double val) { return log(val) / log(power); }
template <typename T>
int sqr(T x) {
return x * x;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto it : v) os << it << "";
os << "\n";
return os;
}
template <typename T, typename H>
inline vector<pair<T, H>> getvector_andBiPair(int len) {
vector<pair<T, H>> a(len);
for (auto &it : a) {
cin >> it.first >> it.second;
}
return a;
}
template <typename T>
inline set<T> vector_to_set(const vector<T> &vec) {
set<T> s;
for (auto &it : vec) s.insert(it);
return s;
}
int main() {
int n = get(), m = get();
auto v = getvector(m);
map<int, int> mp;
for (auto it : v) {
mp[it]++;
cout << (mp.size() == n) << "";
if (mp.size() == n) {
for (int i = 1; i <= n; i++) {
mp[i]--;
if (mp[i] == 0) mp.erase(i);
}
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
map<long long, long long> dp;
map<long long, long long> mapi;
long long grundy(long long val) {
if (val == 1) {
return 0;
}
set<long long> seti;
seti.clear();
long long haha, papa;
if (dp.find(val) != dp.end()) {
return dp[val];
}
long long i = 1;
while (val >= (1LL << i)) {
haha = val % (1LL << i);
papa = val / (1LL << i);
seti.insert(grundy(haha | papa));
i++;
}
for (i = 0; i < 10000; i++) {
if (seti.find(i) == seti.end()) {
dp[val] = i;
return i;
}
}
}
vector<vector<long long> > vec(123);
long long factorise(long long ind, long long val) {
long long i;
for (i = 2; i * i <= val; i++) {
while (val % i == 0) {
val /= i;
vec[ind].push_back(i);
mapi[i] = 0;
}
}
if (val != 1) {
mapi[val] = 0;
vec[ind].push_back(val);
}
return 0;
}
long long a[123];
long long lol[123456];
int main() {
std::ios::sync_with_stdio(false);
long long n;
cin >> n;
long long i;
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
factorise(i, a[i]);
}
long long coutner = 1;
map<long long, long long>::iterator it;
for (it = mapi.begin(); it != mapi.end(); it++) {
it->second = coutner++;
}
long long prev, counti, j;
for (i = 0; i < n; i++) {
if (vec[i].empty()) continue;
sort(vec[i].begin(), vec[i].end());
prev = vec[i][0];
counti = 1;
for (j = 1; j < vec[i].size(); j++) {
if (vec[i][j] != prev) {
lol[mapi[prev]] |= (1LL << counti);
prev = vec[i][j];
counti = 1;
} else {
counti++;
}
}
lol[mapi[prev]] |= (1LL << counti);
}
long long ans = 0;
for (i = 1; i < coutner; i++) {
ans ^= grundy(lol[i]);
}
if (!ans) {
cout << "Arpa" << endl;
} else {
cout << "Mojtaba" << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const int MAXN = 2000005;
const lint mod = 1e9 + 7;
int n, a[MAXN];
int che[MAXN];
int cnt[MAXN];
int yielder[MAXN];
lint ipow(lint x, lint p) {
lint ret = 1, piv = x;
while (p) {
if (p & 1) ret = ret * piv % mod;
piv = piv * piv % mod;
p >>= 1;
}
return ret;
}
vector<int> factor(int v) {
if (v == 1) {
return vector<int>();
}
vector<int> ans;
while (v > 1) {
ans.push_back(che[v]);
v /= che[v];
}
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 2; i < MAXN; i++) {
for (int j = i; j < MAXN; j += i) {
if (!che[j]) che[j] = i;
}
}
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
set<int> s;
int useless = 0;
queue<int> que;
for (int i = 0; i < n;) {
int e = i;
while (e < n && a[i] == a[e]) e++;
if (e - i == 1)
s.insert(a[i]);
else {
if (e - i >= 3) useless = 1;
que.push(a[i] - 1);
que.push(a[i]);
}
i = e;
}
vector<int> will_check;
while (!que.empty()) {
auto x = que.front();
will_check.push_back(x);
que.pop();
auto v = factor(x);
for (int i = 0; i < v.size();) {
int e = i;
while (e < v.size() && v[i] == v[e]) e++;
if (cnt[v[i]] < e - i) {
cnt[v[i]] = e - i;
}
if (cnt[v[i]] && s.find(v[i]) != s.end()) {
s.erase(v[i]);
que.push(v[i] - 1);
}
i = e;
}
}
for (auto &i : s) {
cnt[i] = 1;
}
lint ans = 1;
for (int i = 2; i < MAXN; i++) {
ans *= ipow(i, cnt[i]);
ans %= mod;
}
if (!useless) {
for (int i = 0; i < n;) {
int e = i;
while (e < n && a[e] == a[i]) e++;
if (e - i == 2)
a[i]--;
else if (s.find(a[i]) == s.end())
a[i]--;
i = e;
}
for (int i = 0; i < n; i++) {
auto x = factor(a[i]);
for (int j = 0; j < x.size();) {
int e = j;
while (e < x.size() && x[e] == x[j]) e++;
if (cnt[x[j]] == e - j) yielder[x[j]]++;
j = e;
}
}
for (int i = 0; i < n; i++) {
auto x = factor(a[i]);
bool useful = 0;
for (int j = 0; j < x.size();) {
int e = j;
while (e < x.size() && x[e] == x[j]) e++;
if (cnt[x[j]] == e - j && yielder[x[j]] == 1) useful = 1;
j = e;
}
if (!useful) useless = 1;
}
}
ans += useless;
ans %= mod;
cout << ans << endl;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
namespace FastIO {
const int SIZE = 1 << 16;
char buf[SIZE], obuf[SIZE], str[60];
int bi = SIZE, bn = SIZE, opt;
int read(char* s) {
while (bn) {
for (; bi < bn && buf[bi] <= ' '; bi++)
;
if (bi < bn) break;
bn = fread(buf, 1, SIZE, stdin);
bi = 0;
}
int sn = 0;
while (bn) {
for (; bi < bn && buf[bi] > ' '; bi++) s[sn++] = buf[bi];
if (bi < bn) break;
bn = fread(buf, 1, SIZE, stdin);
bi = 0;
}
s[sn] = 0;
return sn;
}
bool read(int& x) {
int n = read(str), bf;
if (!n) return 0;
int i = 0;
if (str[i] == '-')
bf = -1, i++;
else
bf = 1;
for (x = 0; i < n; i++) x = x * 10 + str[i] - '0';
if (bf < 0) x = -x;
return 1;
}
}; // namespace FastIO
const int N = 1e5 + 7, INF = 0x3f3f3f3f, mz = 1e9 + 7, M = 1e5 + 3;
const double PI = acos(0.0) * 2;
template <typename T1>
T1 gcd(T1 a, T1 b) {
return b ? gcd(b, a % b) : a;
}
struct node {
int f, p, t;
node() { p = t = f = 0; }
node(int a, int b, int e) : p(b), t(e), f(a) {}
bool operator<(const node& rhs) const {
if (f == rhs.f) return f == 2 ? p > rhs.p : p < rhs.p;
return f > rhs.f;
}
} p[N];
vector<int> e[N << 1];
vector<int> x, y;
struct Edge {
int x, y;
Edge() { x = y = 0; }
Edge(int a, int b) : x(a), y(b) {}
bool operator<(const Edge& rhs) const {
if (x) return x < rhs.x;
return y < rhs.y;
}
} ans[N];
bool cmp(const int a, const int b) { return p[a] < p[b]; }
int main() {
int n, w, h, maxn = -1;
FastIO::read(n);
FastIO::read(w);
FastIO::read(h);
for (int i = 1; i <= n; i++) {
int g, q, t;
FastIO::read(g);
FastIO::read(q);
FastIO::read(t);
p[i] = node(g, q, t);
e[q - t + M].push_back(i);
maxn = max(q - t + M, maxn);
}
for (int i = 0; i <= maxn; i++) {
if (e[i].empty()) continue;
x.clear();
y.clear();
sort(e[i].begin(), e[i].end(), cmp);
for (auto it = e[i].begin(); it != e[i].end(); it++)
if (p[*it].f == 1)
x.push_back(p[*it].p);
else
y.push_back(p[*it].p);
sort(x.begin(), x.end());
sort(y.begin(), y.end());
auto js = e[i].begin();
for (auto it = x.begin(); it != x.end(); it++, js++)
ans[*js] = Edge(*it, h);
for (int j = y.size() - 1; ~j; j--, js++) ans[*js] = Edge(w, y[j]);
}
for (int i = 1; i <= n; i++) printf("%d %d\n", ans[i].x, ans[i].y);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long double sum = 1.000;
long double ok;
int n, m, h;
long double good, bad;
int main() {
int i, j;
int x;
cin >> n >> m >> h;
h--;
int vsego = 0;
for (i = 0; i < m; i++) {
cin >> x;
if (i == h)
good = x;
else
bad += x;
vsego += x;
}
if (vsego < n) {
cout << "-1" << endl;
return 0;
}
good--;
vsego--;
for (i = vsego; i > vsego - (n - 1); i--)
sum *= (long double)i / (long double)(n - 1 - (vsego - i));
for (i = 1; i <= min(good, (long double)n - 1); i++) {
long double tem = 1.00;
for (j = good; j > good - i; j--)
tem *= (long double)j / (long double)(i - (good - j));
long double koff = 1.00;
if (bad < n - 1 - i)
koff = 0.00000;
else {
for (j = bad; j > bad - (n - 1 - i); j--)
koff *= (long double)j / (long double)(n - 1 - i - (bad - j));
}
ok += tem * koff;
}
cout << setprecision(20) << ok / sum << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int MAX = 1e5 + 5;
long long int ar[20] = {1,
1,
1,
3,
8,
40,
180,
1260,
8064,
72576,
604800,
6652800,
68428800,
889574400,
10897286400,
163459296000,
2324754432000,
39520825344000,
640237370572800,
12164510040883200};
void solve() {
long long int n;
cin >> n;
cout << ar[n - 1] << "\n";
}
int32_t main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
while (t--) {
solve();
}
cerr << "\n"
<< "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
const int INF = 1e9 + 100;
const int MAXN = 3010;
const int MOD = 1e9 + 7;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (auto &i : a) {
cin >> i;
}
int m;
cin >> m;
vector<int> b(m);
for (auto &i : b) {
cin >> i;
}
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
vector<vector<pair<int, int>>> pr(n + 1,
vector<pair<int, int>>(m + 1, {-1, -1}));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[i] == b[j]) {
dp[i][j] = 1;
for (int k = 0; k < i; ++k) {
if (a[k] < a[i]) {
if (dp[k][j] + 1 > dp[i][j]) {
dp[i][j] = dp[k][j] + 1;
pr[i][j] = {k, j};
}
}
}
} else if (j >= 1) {
dp[i][j] = dp[i][j - 1];
pr[i][j] = pr[i][j - 1];
}
}
}
int pos = 0;
for (int i = 0; i < n; ++i) {
if (dp[pos][m - 1] < dp[i][m - 1]) {
pos = i;
}
}
int pos1 = m - 1, len = dp[pos][m - 1];
vector<int> ans;
for (int i = 0; i < len; ++i) {
ans.push_back(a[pos]);
auto tmp = pr[pos][pos1];
pos = tmp.first, pos1 = tmp.second;
}
reverse((ans).begin(), (ans).end());
cout << len << "\n";
for (auto &el : ans) {
cout << el << ' ';
}
}
signed main() {
fastio();
int t = 1;
while (t--) {
solve();
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[14], i, j;
for (i = 0; i < 14; i++) cin >> arr[i];
long long sum = 0, ma = -1;
for (i = 0; i < 14; i++) {
int q = arr[i] / 14;
int r = arr[i] % 14;
sum = 0;
for (j = i + 1; 1; j++) {
if (j % 14 == i) break;
int c = arr[j % 14] + q;
if (r > 0) {
c++;
r--;
}
if (c % 2 == 0) sum += c;
}
if (q % 2 == 0) sum += q;
ma = max(ma, sum);
}
cout << ma;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void Print() { cout << endl; }
template <typename T1, typename... T>
void Print(const T1 &t1, const T &...t) {
cout << t1 << " ";
Print(t...);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
int L;
struct query {
int l, r, i, k;
bool operator<(const query &rhs) const {
if (l / L == rhs.l / L) return r < rhs.r;
return l / L < rhs.l / L;
}
query() = default;
query(int _l, int _r, int _i, int _k) : l(_l), r(_r), i(_i), k(_k) {}
} q[100005];
int col[100005];
int int_col[100005];
int v_int[100005];
int v_size[100005];
vector<int> edges[100005];
int dfs(int r, int c) {
v_int[r] = c;
int_col[c] = col[r];
int total = 0;
for (auto to : edges[r]) {
if (v_int[to] == -1) {
int cnt = dfs(to, c + 1);
c += cnt;
total += cnt;
}
}
return v_size[r] = 1 + total;
}
int cnt[100005];
int l = -1, r = -1, colcnt[100005];
inline void add(int p) {
if (p < 0) return;
++cnt[++colcnt[int_col[p]]];
}
inline void del(int p) {
if (p < 0) return;
--cnt[colcnt[int_col[p]]--];
}
int ans[100005];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
L = (int)sqrt(n);
for (int i = 0; i < n; ++i) {
cin >> col[i];
}
for (int i = 0; i < n - 1; ++i) {
int f, t;
cin >> f >> t;
edges[f - 1].push_back(t - 1);
edges[t - 1].push_back(f - 1);
}
memset(v_int, -1, sizeof(v_int));
dfs(0, 0);
for (int i = 0; i < m; ++i) {
int x, y, k;
cin >> x >> k;
q[i] = query(v_int[x - 1], v_int[x - 1] + v_size[x - 1] - 1, i, k);
}
sort(q, q + m);
for (int i = 0; i < m; ++i) {
if (q[i].r - q[i].l + 1 < q[i].k) {
ans[q[i].i] = 0;
continue;
}
while (l > q[i].l) add(--l);
while (r < q[i].r) add(++r);
while (l < q[i].l) del(l++);
while (r > q[i].r) del(r--);
ans[q[i].i] = cnt[q[i].k];
}
for (int i = 0; i < m; ++i) Print(ans[i]);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
int n;
string s;
cin >> n >> s;
string perms[] = {"RGB", "GRB", "BGR", "GBR", "BRG", "RBG"};
int best = 1e9;
int bestp;
for (int p = 0; p < 6; p++) {
int c = 0;
for (int i = 0; i < n; i++) {
if (s[i] != perms[p][i % 3]) c++;
}
if (c < best) {
best = c;
bestp = p;
}
}
cout << best << endl;
for (int i = 0; i < n; i++) {
cout << perms[bestp][i % 3];
}
cout << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, x, time = 0;
cin >> n >> k >> x;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n - k; i++) time += a[i];
time += k * x;
cout << time << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
char op[4] = {'U', 'D', 'L', 'R'};
vector<char> ans;
vector<char> temp;
int visit[105][105];
int X[4] = {-1, 1, 0, 0};
int Y[4] = {0, 0, -1, 1};
char c[105][105];
int n, m;
void dfs(int first, int second) {
if (c[first][second] == 'F') {
ans = temp;
return;
}
visit[first][second] = 1;
for (int i = 0; i < 4; i++) {
if (first + X[i] > 0 && first + X[i] <= n && second + Y[i] > 0 &&
second + Y[i] <= m && c[first + X[i]][second + Y[i]] != '*' &&
!visit[first + X[i]][second + Y[i]]) {
temp.push_back(op[i]);
dfs(first + X[i], second + Y[i]);
temp.pop_back();
}
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
int first = 1, second = 1;
if (c[first][second] == 'F') return 0;
int okud = 0, oklr = 0;
if (c[first + 1][second] != '*') {
printf("%c\n", op[0]);
fflush(stdout);
scanf("%d %d", &first, &second);
if (c[first][second] == 'F') return 0;
if (first != 1) {
swap(op[0], op[1]);
printf("%c\n", op[0]);
fflush(stdout);
scanf("%d %d", &first, &second);
if (c[first][second] == 'F') return 0;
}
okud = 1;
}
if (c[first][second + 1] != '*') {
printf("%c\n", op[2]);
fflush(stdout);
scanf("%d %d", &first, &second);
if (c[first][second] == 'F') return 0;
if (second != 1) {
swap(op[2], op[3]);
printf("%c\n", op[2]);
fflush(stdout);
scanf("%d %d", &first, &second);
if (c[first][second] == 'F') return 0;
}
oklr = 1;
}
vector<char> v;
if (!okud) {
while (c[first + 1][second] == '*') {
printf("%c\n", op[3]);
v.push_back(op[2]);
fflush(stdout);
scanf("%d %d", &first, &second);
if (c[first][second] == 'F') return 0;
}
printf("%c\n", op[0]);
fflush(stdout);
scanf("%d %d", &first, &second);
if (c[first][second] == 'F') return 0;
if (first != 1) {
swap(op[0], op[1]);
printf("%c\n", op[0]);
fflush(stdout);
scanf("%d %d", &first, &second);
}
while (!v.empty()) {
printf("%c\n", v.back());
v.pop_back();
fflush(stdout);
scanf("%d %d", &first, &second);
}
}
if (!oklr) {
while (c[first][second + 1] == '*') {
printf("%c\n", op[1]);
v.push_back(op[0]);
fflush(stdout);
scanf("%d %d", &first, &second);
if (c[first][second] == 'F') return 0;
}
printf("%c\n", op[2]);
fflush(stdout);
scanf("%d %d", &first, &second);
if (c[first][second] == 'F') return 0;
if (second != 1) {
swap(op[2], op[3]);
printf("%c\n", op[2]);
fflush(stdout);
scanf("%d %d", &first, &second);
}
while (!v.empty()) {
printf("%c\n", v.back());
v.pop_back();
fflush(stdout);
scanf("%d %d", &first, &second);
}
}
assert(first == 1 && second == 1);
memset(visit, 0, sizeof(visit));
dfs(first, second);
for (vector<char>::iterator it = ans.begin(); it != ans.end(); it++) {
printf("%c\n", *it);
fflush(stdout);
scanf("%d %d", &first, &second);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long M = (1e9 + 7);
template <class A = int, class B = int, class C = int>
struct tri {
A a;
B b;
C c;
tri() {}
tri(const A& a, const B& b, const C& c) : a(a), b(b), c(c) {}
void set(const A& a, const B& b, const C& c) {
this->a = a;
this->b = b;
this->c = c;
}
};
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vec) {
o << "[ ";
for (const auto& v : vec) {
o << v << ' ';
}
o << "]";
return o;
}
int strcmp(const char* a, const char* b, int n) {
while (--n > -1) {
if (a[n] != b[n]) return a[n] > b[n] ? 1 : -1;
}
return 0;
}
long long gcd(long long a, long long b) {
while (b != 0) {
long long t = b;
b = a % b;
a = t;
}
return a;
}
int main() {
int tt;
cin >> tt;
while (tt--) {
int n, q;
scanf("%d %d", &n, &q);
vector<int> vec(n);
for (auto& v : vec) {
scanf("%d", &v);
}
vector<int> s(n);
long long ans = 0;
for (int i = 0; i < n; i++) {
int cur = vec[i];
int nex = (i == n - 1) ? 0 : vec[i + 1];
int v = max(cur - nex, 0);
s[i] = v;
ans += v;
}
cout << ans << '\n';
for (int i = 0; i < q; ++i) {
int l, r;
scanf("%d %d", &l, &r);
--l, --r;
if (l == r) {
cout << ans << '\n';
continue;
}
ans -= s[l] + s[r];
if (r - l > 1) ans -= s[r - 1];
if (l) ans -= s[l - 1];
vec[r] ^= vec[l], vec[l] ^= vec[r], vec[r] ^= vec[l];
auto lam = [&](int l) {
int cur = vec[l];
int nex = (l == n - 1) ? 0 : vec[l + 1];
int v = max(cur - nex, 0);
s[l] = v;
ans += v;
};
lam(r);
lam(l);
if (r - l > 1) lam(r - 1);
if (l) lam(l - 1);
cout << ans << '\n';
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n;
string arr[N];
int ans[N];
string ori[N];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
ori[i] = arr[i];
}
for (int i = 1; i <= n; ++i) {
if (arr[i].size() > arr[i - 1].size()) {
if (arr[i][0] == '?') {
arr[i][0] = '1';
}
}
}
for (int i = 0; i <= n; ++i) {
while (arr[i].size() < 9) {
arr[i] = "0" + arr[i];
}
}
for (int i = 1; i <= n; ++i) {
string pre = arr[i - 1];
string cur = arr[i];
int last = -1;
for (int j = 0; j < 9; ++j) {
if (cur[j] == '?' && pre[j] != '9') {
last = j;
}
if (cur[j] != '?' && cur[j] > pre[j]) {
last = j;
break;
}
if (cur[j] != '?' && cur[j] < pre[j]) {
break;
}
}
for (int j = 0; j < 9; ++j) {
if (cur[j] == '?') {
if (j < last) {
cur[j] = pre[j];
} else if (j == last) {
cur[j] = pre[j] + 1;
} else {
cur[j] = '0';
}
}
}
arr[i] = cur;
}
for (int i = 1; i <= n; ++i) {
stringstream ss;
ss << arr[i];
ss >> ans[i];
}
for (int i = 2; i <= n; ++i) {
if (ans[i] <= ans[i - 1]) {
printf("NO\n");
return 0;
}
}
for (int i = 1; i <= n; ++i) {
if (to_string(ans[i]).size() != ori[i].size()) {
if (n == 100) {
cout << i << " " << ans[i] << " " << ori[i] << " " << ori[i - 1] << " ";
}
printf("NO\n");
return 0;
}
}
printf("YES\n");
for (int i = 1; i <= n; ++i) {
printf("%d\n", ans[i]);
}
}
| 6 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.