solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int mxn = 200010;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int col[mxn];
bool vis[mxn];
vector<int> ve[mxn];
int a[mxn], b[mxn];
void DFS(int u, int d) {
vis[u] = 1;
col[u] = d + 1;
for (int i = 0; i < ve[u].size(); i++) {
int v = ve[u][i];
if (!vis[v]) DFS(v, d ^ 1);
}
return;
}
int n;
int main() {
int i, j, u, v;
n = read();
for (i = 1; i <= n; i++) {
u = read();
v = read();
a[i] = u;
b[i] = v;
ve[u].push_back(v);
ve[v].push_back(u);
}
for (i = 1; i <= n; i++) {
ve[2 * i].push_back(2 * i - 1);
ve[2 * i - 1].push_back(2 * i);
}
for (i = 1; i <= 2 * n; i++)
if (!vis[i]) DFS(i, 0);
for (i = 1; i <= n; i++) printf("%d %d\n", col[a[i]], col[b[i]]);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long stoll(string target) {
stringstream s;
s << target;
long long w;
s >> w;
return w;
}
int stoi(string target) {
stringstream s;
s << target;
int w;
s >> w;
return w;
}
string itos(int i) {
stringstream s;
s << i;
return s.str();
}
string lltos(long long i) {
stringstream s;
s << i;
return s.str();
}
bool ok = true, final = false;
int i, j, k, n, m;
int t;
int br = 0, odgovor = 0, pom, mom, g, f, pamti;
int b[10000];
int a[10000];
void dfs(int x) {
if (x * 2 < (1 << (n + 1))) {
dfs(x * 2);
dfs(x * 2 + 1);
b[x] = max(b[x * 2] + a[x * 2 - 1], b[x * 2 + 1] + a[x * 2]);
}
}
void bfs(int x, int y) {
int f = x * 2;
int g = x * 2 + 1;
if (x * 2 * 2 >= (1 << (n + 1))) {
odgovor += y - min(a[f - 1], a[g - 1]);
return;
}
if (a[f - 1] + b[f] > a[g - 1] + b[g]) {
int br = y - a[g - 1] - b[g];
odgovor += y - a[g - 1] - b[g];
bfs(g, y - br - a[g - 1]);
bfs(f, y - a[f - 1]);
} else {
int br = y - a[f - 1] - b[f];
odgovor += y - a[f - 1] - b[f];
bfs(g, y - a[g - 1]);
bfs(f, y - br - a[f - 1]);
}
}
int main() {
ios::sync_with_stdio(false);
memset((b), (0), sizeof((b)));
memset((a), (0), sizeof((a)));
cin >> n;
for (i = 1; i <= (int)((1 << (n + 1)) - 2); i++) cin >> a[i];
for (i = (1 << n); i <= (int)((1 << (n + 1)) - 1); i++) {
f = 0;
j = i;
while (j > 0) {
f += a[j - 1];
br = max(br, f);
j /= 2;
}
}
dfs(1);
bfs(1, br);
cout << odgovor;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d;
long long calc(long long x) { return -(x + 1) * a + x * (x + 1) / 2 * b * d; }
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long ans = 0;
scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
if (d > c) {
printf("%lld\n", (a > b * c) ? -1 : a);
continue;
}
const long long k = c / d;
if (b * c < a) {
puts("-1");
continue;
} else
ans = -(k + 2) * a + k * (k + 1) / 2 * b * d + b * c;
long long l = 0, r = c / d;
while (l < r) {
long long mid = l + r >> 1;
long long y1 = calc(mid), y2 = calc(mid + 1);
if (y1 <= y2)
r = mid;
else
l = mid + 1;
ans = min(ans, min(y1, y2));
}
printf("%lld\n", -ans);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-4;
const int INF = 1e9 + 7;
const double pi = acos(-1);
void boost() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int a[200001];
int b[200001];
vector<int> was[200001];
int main() {
boost();
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
was[a[i]].push_back(i);
}
int k = 1;
for (int i = 1; i <= n; i++) {
if (!b[i]) {
if (was[a[i]].size() >= n - a[i]) {
int gg = 1;
for (int j = was[a[i]].size() - 1; j >= 0; j--) {
int to = was[a[i]][j];
if (gg == n - a[i]) break;
if (to != i && !b[to]) {
gg++;
b[to] = k;
}
}
if (gg != n - a[i]) {
cout << "Impossible";
return 0;
}
b[i] = k++;
} else {
cout << "Impossible";
return 0;
}
}
}
for (int i = 1; i <= n; i++) {
if (b[i] == 0) {
cout << "Impossible";
return 0;
}
}
cout << "Possible" << endl;
for (int i = 1; i <= n; i++) {
cout << b[i] << " ";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353, N = 56, M = 2 * N, C = 5005;
int a[N], b[N], memo[C][C];
int dp[2][M][M][M][2];
void add(int& a, int b) {
a += b;
if (a >= MOD) a -= MOD;
}
ll binPow(ll x, ll y) {
ll ans = 1;
while (y) {
if (y & 1) ans = ans * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return ans;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i];
int sumA = 0, sumB = 0;
for (int i = 1; i <= n; ++i) {
sumA += a[i] * b[i];
sumB += (!a[i]) * b[i];
}
for (int x = 0; x < M; ++x) {
for (int y = 0; y < M; ++y) {
for (int me = 0; me < M; ++me) {
for (int p = 0; p < 2; ++p) {
dp[0][x][y][me][p] = me;
}
}
}
}
for (int i = 0; i < C; ++i) {
ll save = binPow(i, MOD - 2);
for (int j = 0; j <= i; ++j) {
memo[j][i] = j * save % MOD;
}
}
for (int move = 1; move <= m; ++move) {
for (int x = 0; x < M; ++x) {
for (int y = 0; y < M; ++y) {
for (int me = 0; me < M; ++me) {
int sA = sumA + x - N, sB = sumB + y - N;
int& dp0 = dp[1][x][y][me][0];
int& dp1 = dp[1][x][y][me][1];
if (sA < 0 || sB < 0) continue;
if (me <= sA) {
int opt0 = memo[me][sA + sB];
if (x + 1 < M && me + 1 < M) {
add(dp0, (ll)opt0 * dp[0][x + 1][y][me + 1][0] % MOD);
}
int opt1 = memo[sA - me][sA + sB];
if (x + 1 < M) {
add(dp0, (ll)opt1 * dp[0][x + 1][y][me][0] % MOD);
}
int opt2 = memo[sB][sA + sB];
if (y > 0) {
add(dp0, (ll)opt2 * dp[0][x][y - 1][me][0] % MOD);
}
}
if (me <= sB) {
int opt0 = memo[me][sA + sB];
if (y > 0 && me > 0) {
add(dp1, (ll)opt0 * dp[0][x][y - 1][me - 1][1] % MOD);
}
int opt1 = memo[sB - me][sA + sB];
if (y > 0) {
add(dp1, (ll)opt1 * dp[0][x][y - 1][me][1] % MOD);
}
int opt2 = memo[sA][sA + sB];
if (x + 1 < M) {
add(dp1, (ll)opt2 * dp[0][x + 1][y][me][1] % MOD);
}
}
}
}
}
for (int x = 0; x < M; ++x) {
for (int y = 0; y < M; ++y) {
for (int me = 0; me < M; ++me) {
for (int p = 0; p < 2; ++p) {
dp[0][x][y][me][p] = dp[1][x][y][me][p];
dp[1][x][y][me][p] = 0;
}
}
}
}
}
for (int i = 1; i <= n; ++i) {
cout << dp[0][N][N][b[i]][!a[i]] << '\n';
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a;
cin >> a;
int v;
cin >> v;
string s;
cin >> s;
if (a == 1 && v > 0) {
s.at(0) = '0';
} else {
if (s.at(0) != '1' && v > 0) {
s.at(0) = '1';
v--;
}
for (int i = 1; i < a; i++) {
if (s[i] != '0' && v > 0) {
s[i] = '0';
v--;
}
}
}
cout << s;
return 0;
}
| 1 |
#include <bits/stdc++.h>
const int inf = 20;
using namespace std;
int n, k, second;
int s[1000005];
int main() {
scanf("%d\n", &n);
for (int i = 1; i <= n; i++) s[i] = s[i - 1] ^ i;
for (int i = 1; i <= n; i++) {
int x;
scanf("%d ", &x);
second ^= x;
second ^= ((n / i % 2) * s[i - 1]) ^ (s[n % i]);
}
printf("%d\n", second);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, x, y, n, s;
ifstream fin("input.txt");
ofstream fout("output.txt");
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n >> x >> y;
int n1 = n;
if (y < x) {
swap(x, y);
}
for (int j = 1; j <= 49; j++) {
if ((y - x) % j == 0) {
if (((y - x) / j) <= n - 1) {
s = j;
break;
}
}
}
for (int j = x; j <= y; j += s) {
cout << j << " ";
}
n -= ((y - x) / s + 1);
x -= s;
while ((n > 0) && (x >= 1)) {
cout << x << " ";
n--;
x -= s;
}
y += s;
while (n > 0) {
cout << y << " ";
n--;
y += s;
}
cout << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, qq;
cin >> n >> qq;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> diff(n);
for (int i = 0; i < n - 1; i++) {
diff[i] = a[i] - a[i + 1];
}
diff[n - 1] = a[n - 1];
bool big = true;
long long sum = 0;
for (int i = 0; i < n; i++) {
if (diff[i] > 0) {
sum += diff[i];
}
}
cout << sum << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long _ = 1e2;
const long long maxn = 1e5 + _;
pair<long long, long long> a[maxn];
long long plen, len, id[maxn];
int main() {
long long n, k, m, x;
scanf("%lld%lld%lld", &n, &k, &m);
plen = 0;
for (long long i = 1; i <= n; i++) {
scanf("%lld", &x);
if (plen == 0 || a[plen].first != x)
a[++plen] = make_pair(x, 1);
else
a[plen].second++;
}
long long now, nxt;
long long s = 0;
len = 0;
for (long long i = 1; i <= plen; i = nxt) {
now = i, nxt = i + 1;
while (now > 0 && a[now].second >= k) {
a[now].second %= k;
if (a[now].second == 0) {
if (len != 0)
now = id[len], s -= a[id[len--]].second;
else
break;
if (now > 0 && nxt <= plen && a[now].first == a[nxt].first)
a[now].second += a[nxt].second, nxt++;
}
}
if (a[now].second != 0) id[++len] = now, s += a[id[len]].second;
}
if (len == 0) {
puts("0");
return 0;
}
if (len == 1) {
printf("%lld\n", (long long)a[id[len / 2 + 1]].second * m % k);
return 0;
}
long long i;
long long g = 0, u = 0;
for (i = 1; i <= len / 2; i++) {
if (a[id[i]].first == a[id[len - i + 1]].first) {
long long num = (a[id[i]].second + a[id[len - i + 1]].second) % k;
g += a[id[i]].second + a[id[len - i + 1]].second - num;
if (num != 0) {
printf("%lld\n", s * m - g * (m - 1));
return 0;
}
} else {
printf("%lld\n", s * m - g * (m - 1));
return 0;
}
}
if (len % 2 == 0)
puts("0");
else {
if (a[id[len / 2 + 1]].second * m % k == 0)
puts("0");
else
printf("%lld\n", s * m - g * (m - 1) -
(a[id[len / 2 + 1]].second * m -
a[id[len / 2 + 1]].second * m % k));
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
char sol[100010];
int ask(int r1, int c1, int r2, int c2) {
printf("? %d %d %d %d\n", r1, c1, r2, c2);
fflush(stdout);
char sir[10];
scanf("%s", sir);
if (sir[0] == 'Y')
return 1;
else
return 0;
}
int main() {
int n;
scanf("%d", &n);
int nr = 0, pozx = n, pozy = n, poz = 2 * n - 2;
while (nr < n - 1) {
if (pozx - 1 > 0 && ask(1, 1, pozx - 1, pozy) == 1) {
sol[poz] = 'D';
poz--;
pozx--;
} else {
sol[poz] = 'R';
poz--;
pozy--;
}
nr++;
}
nr = 0;
poz = 0;
pozx = 1;
pozy = 1;
while (nr < n - 1) {
if (pozy + 1 <= n && ask(pozx, pozy + 1, n, n) == 1) {
sol[++poz] = 'R';
pozy++;
} else {
sol[++poz] = 'D';
pozx++;
}
nr++;
}
printf("! ");
for (int i = 1; i <= 2 * n - 2; i++) printf("%c", sol[i]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, t = 1, nn;
int p[N];
int lson[N * 4], rson[N * 4], used[N * 4];
pair<int, int> uni;
pair<int, int> edge[N];
vector<int> del;
vector<int> val[N * 4];
pair<int, pair<int, int> > eve[N];
map<int, int> mp;
void mge(pair<int, int> &x, pair<int, int> y) {
x.first = min(x.first, y.first);
x.second = max(x.second, y.second);
}
int fp(int x) { return (x == p[x]) ? x : (p[x] = fp(p[x])); }
void build(int pos, int x, int y) {
if (x == y) return;
int mid = (x + y) >> 1;
lson[pos] = ++t;
rson[pos] = ++t;
build(lson[pos], x, mid);
build(rson[pos], mid + 1, y);
}
void query(int pos, int x, int y, int l) {
if (x <= l && y >= l) {
for (int i = 0; i < val[pos].size(); i++) del.push_back(val[pos][i]);
val[pos].clear();
}
if (x == y) return;
if (x > l || y < l) return;
int mid = (x + y) >> 1;
query(lson[pos], x, mid, l);
query(rson[pos], mid + 1, y, l);
}
void modify(int pos, int x, int y, int l, int r, int v) {
if (x >= l && y <= r) {
val[pos].push_back(v);
return;
}
if (x > r || y < l) return;
int mid = (x + y) >> 1;
modify(lson[pos], x, mid, l, r, v);
modify(rson[pos], mid + 1, y, l, r, v);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int o, x, y;
scanf("%d%d%d", &o, &x, &y);
eve[i] = make_pair(o, make_pair(x, y));
if (o == 1) {
mp[x];
mp[y];
}
}
for (int i = 1; i <= n; i++) p[i] = i;
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
it->second = ++m;
for (int i = 1; i <= n; i++)
if (eve[i].first == 1) {
eve[i].second.first = mp[eve[i].second.first];
eve[i].second.second = mp[eve[i].second.second];
}
build(1, 1, m);
for (int i = 1; i <= n; i++) {
int o = eve[i].first;
int x = eve[i].second.first;
int y = eve[i].second.second;
if (o == 1) {
nn++;
del.clear();
uni = make_pair(x + 1, y - 1);
query(1, 1, m, x);
query(1, 1, m, y);
for (int i = 0; i < del.size(); i++) {
del[i] = fp(del[i]);
if (del[i] != nn) mge(uni, edge[del[i]]);
p[del[i]] = nn;
}
if (uni.first <= uni.second) modify(1, 1, m, uni.first, uni.second, nn);
edge[nn] = uni;
} else {
if (x > nn || y > nn) {
puts("NO");
continue;
}
x = fp(x);
y = fp(y);
if (x == y)
puts("YES");
else if (edge[x].first >= edge[y].first &&
edge[x].second <= edge[y].second && edge[x] != edge[y])
puts("YES");
else
puts("NO");
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
int N = S.size();
int i = 0, j = N - 1;
string A;
while (j - i >= 3) {
if (S[i] == S[j]) {
A.push_back(S[i]);
++i;
--j;
} else if (S[i] == S[j - 1]) {
A.push_back(S[i]);
++i;
j -= 2;
} else {
A.push_back(S[i + 1]);
if (S[i + 1] == S[j]) {
--j;
} else {
j -= 2;
}
i += 2;
}
}
string B = A;
if (j >= i) A.push_back(S[i]);
reverse(B.begin(), B.end());
cout << A << B << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100;
pair<int, int> c[MAXN], s[MAXN];
int main() {
int n, m;
scanf("%d %d", &n, &m);
long long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int x;
scanf("%d", &x);
if (x == 0) {
c[j].first++;
s[i].first++;
} else {
c[j].second++;
s[i].second++;
}
}
}
for (int i = 0; i < m; i++) {
ans += (1ll << c[i].first) - 1;
ans += (1ll << c[i].second) - 1;
}
for (int i = 0; i < n; i++) {
ans += (1ll << s[i].first) - 1;
ans += (1ll << s[i].second) - 1;
}
ans -= n * m;
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pair<int, int>>;
using pl = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pl>;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const ld PI = acos((ld)-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
bool ckmin(T &a, const T &b) {
return b < a ? a = b, 1 : 0;
}
template <typename T>
bool ckmax(T &a, const T &b) {
return b > a ? a = b, 1 : 0;
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
!is_same<T_container, string>::value,
typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
os << '{';
string sep;
for (const T &x : v) os << sep << x, sep = ", ";
return os << '}';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
struct chash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct Node {
ll cnt, tl, tr;
};
template <typename T>
struct LazySegTree {
const T seg_initial = {0, 0, 0};
const ll seg_lazy_sentinel = 0;
const T NEUTRAL = {0, 0, 0};
long long sz;
vector<T> t;
vl lazy;
inline T merge(T a, T b) {
if (a.tl == 0) return b;
if (b.tl == 0) return a;
return {a.cnt + b.cnt, a.tl, b.tr};
}
inline ll lazy_apply(ll lazy_val, ll new_val) {
if (lazy_val == 0 || new_val == 1 || new_val == 2) return new_val;
if (lazy_val == new_val) return 0;
return (lazy_val == 1 ? 2 : 1);
}
inline ll lazy_func(T nn, ll lazy_val, ll l, ll r) {
if (lazy_val == 1)
return nn.tr - nn.tl;
else if (lazy_val == 2)
return 0;
else
return nn.tr - nn.tl - nn.cnt;
}
void init(long long n) {
sz = 1;
while (sz < n) sz *= 2;
t.assign(2 * sz, seg_initial);
lazy.assign(2 * sz, seg_lazy_sentinel);
}
void seg_eval_lazy(long long i, ll l, ll r) {
if (lazy[i] == seg_lazy_sentinel) return;
t[i].cnt = lazy_func(t[i], lazy[i], l, r);
if (r - l > 1) {
lazy[i * 2 + 1] = lazy_apply(lazy[i * 2 + 1], lazy[i]);
lazy[i * 2 + 2] = lazy_apply(lazy[i * 2 + 2], lazy[i]);
}
lazy[i] = seg_lazy_sentinel;
}
void build(vector<Node> &vec, long long x, long long l, long long r) {
if (r - l == 1) {
if (l < (long long)vec.size()) t[x] = vec[l];
return;
}
long long mid = (l + r) / 2;
build(vec, 2 * x + 1, l, mid);
build(vec, 2 * x + 2, mid, r);
t[x] = merge(t[2 * x + 1], t[2 * x + 2]);
}
void build(vector<Node> &vec) { build(vec, 0, 0, sz); }
T upd(long long x, long long l, long long r, long long lx, long long rx,
long long v) {
seg_eval_lazy(x, lx, rx);
if (lx >= r || rx <= l) return t[x];
if (lx >= l && rx <= r) {
lazy[x] = lazy_apply(lazy[x], v);
seg_eval_lazy(x, lx, rx);
return t[x];
}
if (rx - lx == 1) return t[x];
long long mid = (lx + rx) / 2;
T a = upd(2 * x + 1, l, r, lx, mid, v);
T b = upd(2 * x + 2, l, r, mid, rx, v);
return t[x] = merge(a, b);
}
void upd(long long l, long long r, long long v) { upd(0, l, r, 0, sz, v); }
ll query(long long x, long long lx, long long rx) {
seg_eval_lazy(x, lx, rx);
if (t[x].cnt == 0) return 0;
if (t[x].cnt == t[x].tr - t[x].tl) return t[x].cnt;
long long mid = (lx + rx) / 2;
seg_eval_lazy(x * 2 + 1, lx, mid);
seg_eval_lazy(x * 2 + 2, mid, rx);
if (t[2 * x + 1].cnt == t[2 * x + 1].tr - t[2 * x + 1].tl)
return t[2 * x + 1].cnt + query(2 * x + 2, mid, rx);
else
return query(2 * x + 1, lx, mid);
}
ll query() { return query(0, 0, sz) + 1; }
};
void solve() {
long long n;
cin >> n;
vector<Node> v;
vl a;
vector<array<ll, 3>> q(n);
for (long long i = (0); i < (n); ++i) {
ll t, l, r;
cin >> t >> l >> r;
q[i] = {t, l, r};
a.push_back(l), a.push_back(r);
}
a.push_back(LINF), a.push_back(1);
sort((a).begin(), (a).end()),
a.resize(unique((a).begin(), (a).end()) - a.begin());
for (long long i = (0); i < (2 * (long long)(a).size() - 1); ++i) {
if (i & 1) {
v.push_back({0LL, a[i / 2LL] + 1LL, a[(i + 1LL) / 2LL]});
} else {
v.push_back({0LL, a[i / 2LL], a[i / 2LL] + 1LL});
}
}
LazySegTree<Node> st;
st.init((long long)(v).size());
st.build(v);
for (long long i = (0); i < (n); ++i) {
auto [t, l, r] = q[i];
l = lower_bound((a).begin(), (a).end(), l) - a.begin(),
r = lower_bound((a).begin(), (a).end(), r) - a.begin();
st.upd(2 * l, 2 * r + 1, t);
cout << st.query() << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long testcase = 1;
while (testcase--) {
solve();
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int b, p, f, h, c;
void wczytaj_dane() { cin >> b >> p >> f >> h >> c; }
inline void hamburger(int& wynik) {
assert(p > 0);
p--;
wynik += h;
}
inline void chickenburger(int& wynik) {
assert(f > 0);
f--;
wynik += c;
}
int rozwiaz() {
int wynik = 0;
b /= 2;
while (b > 0) {
if (p > 0 && f > 0) {
if (h >= c)
hamburger(wynik);
else
chickenburger(wynik);
} else if (p > 0)
hamburger(wynik);
else if (f > 0)
chickenburger(wynik);
b--;
}
return wynik;
}
void zrob_test() {
wczytaj_dane();
cout << rozwiaz() << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) zrob_test();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5;
array<int, maxn * 4> tree;
array<int, maxn> a, b;
vector<int> c[maxn];
int n;
void build(int u, int tl, int tr) {
if (tl + 1 == tr) {
tree[u] = c[tl].size() ? c[tl].back() : n;
return;
}
int tm = (tl + tr) / 2;
build(u * 2 + 1, tl, tm);
build(u * 2 + 2, tm, tr);
tree[u] = min(tree[u * 2 + 1], tree[u * 2 + 2]);
}
int getMin(int u, int tl, int tr, int ql, int qr) {
if (tl == ql && tr == qr) {
return tree[u];
}
int tm = (tl + tr) / 2;
int lhs = ql < tm ? getMin(u * 2 + 1, tl, tm, ql, min(qr, tm)) : n;
int rhs = tm < qr ? getMin(u * 2 + 2, tm, tr, max(ql, tm), qr) : n;
return min(lhs, rhs);
}
void pop(int u, int tl, int tr, int val) {
if (tl + 1 == tr) {
c[tl].pop_back();
tree[u] = c[tl].size() ? c[tl].back() : n;
return;
}
int tm = (tl + tr) / 2;
if (val < tm) {
pop(u * 2 + 1, tl, tm, val);
} else
pop(u * 2 + 2, tm, tr, val);
tree[u] = min(tree[u * 2 + 1], tree[u * 2 + 2]);
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
cin >> n;
vector<int> a(n), b(n), cnt(n, 0);
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; ++i) cin >> b[i];
for (int i = n - 1; i >= 0; --i) {
c[a[i] - 1].push_back(i);
}
build(0, 0, n);
for (int j = 0; j < n; ++j) {
int val = b[j] - 1;
int q = getMin(0, 0, n, 0, n);
if (a[q] - 1 == val) {
pop(0, 0, n, a[q] - 1);
continue;
}
int pos = getMin(0, 0, n, val, val + 1);
if (pos == n) {
goto No;
}
int mn = getMin(0, 0, n, 0, val);
if (mn < pos) {
goto No;
}
pop(0, 0, n, val);
}
Yes:
cout << "YES\n";
goto Cleanup;
No:
cout << "NO\n";
Cleanup:
for (int i = 0; i < n; ++i) {
c[i].clear();
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
vector<int> a[26];
int main() {
int len, q;
char x, y;
cin >> len >> q;
char s[200003];
scanf("%s", s);
for (int i = 0; i < len; i++) {
a[s[i] - 'a'].push_back(i);
}
for (int i = 0; i < q; ++i) {
cin >> x >> y;
a[x - 'a'].swap(a[y - 'a']);
}
char ans[200003];
for (int i = 0; i < 26; i++) {
if (a[i].size() > 0) {
for (int j = 0; j < (int)a[i].size(); ++j) ans[a[i][j]] = (char)('a' + i);
}
}
ans[len] = '\0';
printf("%s", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int S[100100];
void prtS(bool blspc, int &li, int in, int fn) {
for (int i = in; i < fn; i++) {
for (int j = 0; j < S[i]; j++) {
if (blspc) printf(" ");
blspc = 1;
printf("%d", li);
}
li += S[i];
}
}
int main() {
int h;
scanf("%d", &h);
for (int i = 0; i <= h; i++) {
scanf("%d", &S[i]);
}
int ci = -1;
for (int i = 0; i < h; i++) {
if (S[i] > 1 && S[i + 1] > 1) {
ci = i;
break;
}
}
if (ci != -1) {
puts("ambiguous");
int li = 0;
prtS(0, li, 0, h + 1);
puts("");
li = 0;
prtS(0, li, 0, ci + 1);
bool isF = 1;
for (int j = 0; j < S[ci + 1]; j++) {
printf(" ");
if (isF)
printf("%d", li);
else
printf("%d", li - 1);
isF = 0;
}
li += S[ci + 1];
prtS(1, li, ci + 2, h + 1);
puts("");
} else {
puts("perfect");
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool isFibo[1001];
int fibo[1001];
int main() {
ios::sync_with_stdio(false);
memset(isFibo, false, sizeof(isFibo));
fibo[0] = 1;
fibo[1] = 1;
isFibo[0] = true;
for (int i = 2; fibo[i - 1] <= 1000; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
isFibo[fibo[i] - 1] = true;
}
int n;
cin >> n;
for (int i = 0; i < n; i++) {
if (isFibo[i])
cout << "O";
else
cout << "o";
}
cout << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long n, k, d;
cin >> n >> k >> d;
long long i;
vector<long long> a(n);
for (i = 0; i < n; i++) cin >> a[i];
map<long long, long long> m;
long long cnt = 0, ans;
for (i = 0; i < d; i++) {
if (m[a[i]] == 0) cnt++;
m[a[i]]++;
}
ans = cnt;
for (i = d; i < n; i++) {
m[a[i - d]]--;
if (m[a[i - d]] == 0) cnt--;
if (m[a[i]] == 0) cnt++;
m[a[i]]++;
ans = min(ans, cnt);
}
cout << ans << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
const int N = 1e5 + 5;
const int OO = 1e9 + 7;
using namespace std;
long long ncr(long long n, long long r) {
if (r == 0) return 1;
return (n * ncr(n - 1, r - 1) / r);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m, t;
cin >> n >> m >> t;
long long i = 4;
long long sum = 0;
while (i <= n && t - i >= 1) {
sum += ncr(n, i) * ncr(m, t - i);
i++;
}
cout << sum;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
double binpow(double a, long long b) {
double res = 1e0;
while (b > 0) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int main() {
int n, m;
cin >> m >> n;
int i;
double sum = 0, total_prob = 0;
;
for (i = 1; i <= m; i++) {
double res1 = ((double)(i)) / ((double(m)));
double res2 = ((double)(i - 1)) / ((double(m)));
sum = binpow(res1, (long long)(n)) - binpow(res2, (long long)(n));
total_prob += (i * sum);
}
cout << total_prob << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int prime[5000001];
void SieveOfEratosthenes() {
int i, p;
for (i = 2; i < 5000001; i += 2) prime[i] = 2;
for (p = 3; p < 5000001; p += 2) {
if (!(prime[p])) {
prime[p] = p;
for (i = p * 3; i < 5000001; i += (2 * p)) prime[i] = p;
}
}
}
int aux[5000001], res[5000001];
int main() {
SieveOfEratosthenes();
int i, t, a, b;
aux[1] = 0;
for (i = 2; i < 5000001; i++) {
aux[i] = aux[i / prime[i]] + 1;
res[i] = res[i - 1] + aux[i];
}
scanf("%d", &t);
for (i = 0; i < t; i++) {
scanf("%d%d", &b, &a);
printf("%d\n", res[b] - res[a]);
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 6e5 + 51;
int n, r, pos, c;
long double res;
int x[MAXN];
inline int read() {
register int num = 0, neg = 1;
register char ch = getchar();
while (!isdigit(ch) && ch != '-') {
ch = getchar();
}
if (ch == '-') {
neg = -1;
ch = getchar();
}
while (isdigit(ch)) {
num = (num << 3) + (num << 1) + (ch - '0');
ch = getchar();
}
return num * neg;
}
int main() {
n = 1 << read(), r = read();
for (register int i = 0; i < n; i++) {
x[i] = read(), res += x[i];
}
cout << fixed << setprecision(8) << res / n << endl;
for (register int i = 1; i <= r; i++) {
pos = read(), c = read(), res -= x[pos], res += (x[pos] = c);
cout << fixed << setprecision(8) << res / n << endl;
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)1e9 + 7;
long long n;
vector<long long> dp;
void add(long long x) {
for (long long i = n; i >= x; --i) {
dp[i] += dp[i - x];
dp[i] %= mod;
}
}
void del(long long x) {
for (long long i = x; i <= n; ++i) {
dp[i] = (dp[i] - dp[i - x] + mod) % mod;
}
}
void solve() {
long long q;
cin >> n >> q;
vector<long long> ql(q + 1), qr(q + 1), qx(q + 1);
for (long long i = 1; i <= q; ++i) {
cin >> ql[i] >> qr[i] >> qx[i];
}
dp.resize(n + 1);
dp[0] = 1;
vector<long long> ans(n + 1);
vector<vector<long long> > ev;
for (long long i = 1; i <= q; ++i) {
ev.push_back({ql[i], -1, qx[i]});
ev.push_back({qr[i], 1, qx[i]});
}
long long res = 0;
sort(ev.begin(), ev.end());
for (long long i = 0; i < ev.size(); ++i) {
if (ev[i][1] == -1) {
add(ev[i][2]);
for (long long j = 1; j <= n; ++j) {
if (ans[j] == 0 && dp[j] != 0) {
ans[j] = 1;
res++;
}
}
} else {
del(ev[i][2]);
}
}
cout << res << "\n";
for (long long i = 1; i <= n; ++i) {
if (ans[i]) cout << i << " ";
}
cout << "\n";
}
signed main() {
int t = 1;
cout << fixed << setprecision(10);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while (t--) {
solve();
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e9;
const int N = 2 * 1e5 + 1, MOD = 1e8;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
if (n < 3)
cout << -1;
else if (n % 2 != 0)
cout << n * n / 2 << " " << (n * n + 1) / 2;
else
cout << n * n / 4 - 1 << " " << n * n / 4 + 1;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> answer;
vector<bool> did;
vector<int> portals;
int n;
int main() {
cin >> n;
for (long long int i = 0; i < n; i++) {
int d;
cin >> d;
d--;
portals.push_back(d);
}
answer = vector<int>(n, 1000000);
did = vector<bool>(n, false);
answer[0] = 0;
queue<int> que;
que.push(0);
while (que.size()) {
int cur = que.front();
que.pop();
answer[portals[cur]] = min(answer[portals[cur]], answer[cur] + 1);
if (!did[portals[cur]]) {
que.push(portals[cur]);
did[portals[cur]] = true;
}
if (cur > 0) {
answer[cur - 1] = min(answer[cur - 1], answer[cur] + 1);
if (!did[cur - 1]) {
que.push(cur - 1);
did[cur - 1] = true;
}
}
if (cur < n - 1) {
answer[cur + 1] = min(answer[cur + 1], answer[cur] + 1);
if (!did[cur + 1]) {
que.push(cur + 1);
did[cur + 1] = true;
}
}
}
for (int i = 0; i < n; i++) {
if (i != 0) cout << " ";
cout << setw(2);
cout << answer[i];
}
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
long long V;
long long f(long long x) { return min(x * (x - 1) / 2, V - x) + V - x; }
long long Bsearch(long long l, long long r) {
while (l < r) {
long long mid = (l + r) >> 1;
if (f(mid) > f(mid + 1))
r = mid;
else
l = mid + 1;
}
return l;
}
int main() {
int _;
scanf("%d", &_);
while (_--) {
scanf("%lld", &V);
long long k = Bsearch(1, V);
long long as = f(k);
for (long long p = (max(1ll, k - 1)); p < min(k + 2, (long long)V + 1); p++)
if (f(p) > as) as = f(p);
printf("%lld\n", as);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, r;
int p = -1, best = -1;
char op[3] = "TB";
void work(int v) {
int cnt = 0, tims = 0;
int b = r, c = v;
while (c) {
cnt++;
tims += b / c;
b %= c;
swap(b, c);
}
if (b == 1 && tims == n && cnt > best) {
best = cnt;
p = v;
}
}
int make(int a, int b) {
if (b == 0) {
putchar(op[0]);
return 1;
} else {
int c = make(b, a % b);
int cnt = a / b - (a % b == 0);
for (int i = 0; i < cnt; i++) putchar(op[c]);
return c ^ 1;
}
}
int main() {
scanf("%d%d", &n, &r);
if (r == 1) {
if (n != 1) {
puts("IMPOSSIBLE");
} else {
puts("0");
puts("T");
}
return 0;
}
for (int i = 1; i < r; i++) {
work(i);
}
if (p == -1) {
puts("IMPOSSIBLE");
} else {
printf("%d\n", n - best - 1);
make(r, p);
puts("");
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct CGlb {
void write(vector<int> &r) {
int i;
for (i = 0; i < (r.size()); i++) cout << r[i] << ' ';
cout << endl;
}
void write(int r[], int n) {
int i;
for (i = 0; i < (n); i++) cout << r[i] << ' ';
cout << endl;
}
} x_glb;
int n, m;
int T, Ti, Q, Qi;
int da, db, dc;
string ds;
string rs;
int r;
int main() {
ios::sync_with_stdio(false);
int i, j, k;
int a, b, c;
int x, y, z;
string zs;
cout << setiosflags(ios::fixed) << setprecision(8);
while (cin >> n) {
da = 0;
for (i = 0; i < (n); i++) {
cin >> r;
da = max(da, r);
}
cout << da << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int t, mx;
int n, m, k, cnt;
int l[100005], r[100005];
struct p {
int w, op, id;
} w[200005];
bool cmp(p a, p b) {
if (a.w == b.w) return a.op < b.op;
return a.w < b.w;
}
int f[200005][260], one[1025], Q[13];
int main() {
int i, j;
n = read(), m = read(), k = read();
for (i = 1; i <= n; i++) {
l[i] = read(), r[i] = read();
w[++cnt].w = l[i], w[cnt].op = 1, w[cnt].id = i;
w[++cnt].w = r[i] + 1, w[cnt].op = -1, w[cnt].id = i;
}
sort(w + 1, w + cnt + 1, cmp);
for (j = 0; j <= cnt; j++)
for (i = 0; i < (1 << k); i++) f[j][i] = -0x7fffffff;
for (i = 1; i < (1 << k); i++) one[i] = one[i - (i & -i)] ^ 1;
f[0][0] = 0;
for (i = 1; i <= k; i++) Q[i] = -1;
int now = 0;
for (i = 1; i <= cnt; i++) {
int st = -1;
if (w[i].op == 1) {
for (j = 1; j <= k; j++)
if (Q[j] == -1) {
st = j, Q[j] = w[i].id;
break;
}
for (j = 0; j <= now; j++)
if ((j & now) == j) {
f[i][j] = max(f[i][j], f[i - 1][j] + one[j] * (w[i].w - w[i - 1].w));
f[i][j | (1 << (st - 1))] =
max(f[i][j | (1 << (st - 1))],
f[i - 1][j] + one[j] * (w[i].w - 1 - w[i - 1].w) +
((one[j] + 1) & 1));
}
now |= (1 << (st - 1));
} else {
for (j = 1; j <= k; j++)
if (Q[j] == w[i].id) {
st = j, Q[j] = -1;
break;
}
now ^= (1 << (st - 1));
for (j = 0; j <= now; j++)
if ((j & now) == j) {
f[i][j] = max(f[i][j], f[i - 1][j] + one[j] * (w[i].w - w[i - 1].w));
f[i][j] =
max(f[i][j], f[i - 1][j | (1 << (st - 1))] +
((one[j] + 1) & 1) * (w[i].w - 1 - w[i - 1].w) +
one[j]);
}
}
}
printf("%d\n", f[cnt][0]);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
vector<pair<pair<int, int>, int>> op;
long long reputation[100005]{0};
long long a[100000]{0};
long long ans[100005]{0};
int main() {
cin >> n >> m >> k;
for (int i = 0; i < n; i++) cin >> a[i];
int l, r, d;
for (int i = 0; i < m; i++) {
cin >> l >> r >> d;
op.push_back({{l, r}, d});
}
int f, t;
for (int i = 0; i < k; i++) {
cin >> f >> t;
reputation[f - 1]++;
reputation[t]--;
}
for (int i = 1; i < m; i++) reputation[i] += reputation[i - 1];
for (int i = 0; i < m; i++) {
int from = op[i].first.first;
int to = op[i].first.second;
long long d = (long long)op[i].second;
ans[from - 1] += (long long)(d * reputation[i]);
ans[to] -= (long long)(d * reputation[i]);
}
for (int i = 1; i < n; i++) ans[i] += ans[i - 1];
for (int i = 0; i < n; i++) cout << (long long)a[i] + ans[i] << " ";
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char str[1000][1000];
long long mark[1000][1000];
long long n, m;
long long bfs(long long s, long long t) {
long long flag = 0;
queue<pair<long long, long long> > q;
q.push(make_pair(s, t));
while (!q.empty()) {
pair<long long, long long> p = q.front();
long long a = p.first;
long long b = p.second;
q.pop();
if (mark[a][b] == 1) {
flag = 1;
break;
}
mark[a][b] = 1;
char ch = str[a][b];
if ((a + 1) < n) {
if (str[a + 1][b] == ch && !mark[a + 1][b]) {
q.push(make_pair(a + 1, b));
}
}
if ((a - 1) >= 0) {
if (str[a - 1][b] == ch && !mark[a - 1][b]) {
q.push(make_pair(a - 1, b));
}
}
if ((b + 1) < m) {
if (str[a][b + 1] == ch && !mark[a][b + 1]) {
q.push(make_pair(a, b + 1));
}
}
if ((b - 1) >= 0) {
if (str[a][b - 1] == ch && !mark[a][b - 1]) {
q.push(make_pair(a, b - 1));
}
}
}
if (flag) {
return 1;
} else {
return 0;
}
}
int main() {
std::ios_base::sync_with_stdio(false);
long long i, j;
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> str[i];
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
mark[i][j] = 0;
}
}
long long flag = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (!mark[i][j]) {
if (bfs(i, j)) {
flag = 1;
break;
}
}
}
if (flag == 1) {
break;
}
}
if (flag == 1) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
struct Query {
int type, v;
Query(){};
Query(int type, int v) : type(type), v(v){};
};
struct Node {
int val, d, upd;
Node(){};
Node(int val, int d, int upd) : val(val), d(d), upd(upd){};
};
const int MOD = 1e9 + 7;
int n, q, x, c[N], tcur, tin[N], tin1[N], parent[N], lt[N], rt[N], s[N];
Node tree[4 * N];
vector<int> g[N];
Query queries[N];
void dfs(int v) {
tcur++;
tin[v] = tcur;
tin1[tin[v]] = v;
lt[v] = tin[v];
rt[v] = tin[v];
for (int i = 0; i < g[v].size(); i++) {
dfs(g[v][i]);
rt[v] = max(rt[v], rt[g[v][i]]);
}
}
void mult(int &a, int b) { a = (1ll * a * b) % MOD; }
int product(int a, int b) { return (1ll * a * b) % MOD; }
int sum(int a, int b) { return (a + b) % MOD; }
int power(int b, int p) {
int res = 1;
while (p) {
if (p & 1) res = product(res, b);
b = product(b, b);
p /= 2;
}
return res;
}
void build(int v, int tl, int tr) {
if (tl == tr) {
tree[v] = Node(0, 1, 1);
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
tree[v] = Node(0, 1, 1);
}
void push(int v) {
if (tree[v].upd != 1) {
mult(tree[v * 2].val, tree[v].upd);
mult(tree[v * 2 + 1].val, tree[v].upd);
mult(tree[v * 2].d, tree[v].upd);
mult(tree[v * 2 + 1].d, tree[v].upd);
mult(tree[v * 2].upd, tree[v].upd);
mult(tree[v * 2 + 1].upd, tree[v].upd);
tree[v].upd = 1;
}
}
void multOnSegment(int v, int tl, int tr, int l, int r, int val) {
if (l > r) return;
if (l == tl && r == tr) {
mult(tree[v].val, val);
mult(tree[v].d, val);
mult(tree[v].upd, val);
return;
}
push(v);
int tm = (tl + tr) / 2;
multOnSegment(v * 2, tl, tm, l, min(r, tm), val);
multOnSegment(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
tree[v].val = sum(tree[v * 2].val, tree[v * 2 + 1].val);
tree[v].d = sum(tree[v * 2].d, tree[v * 2 + 1].d);
}
void update(int v, int tl, int tr, int pos) {
if (tl == tr) {
tree[v].val = product(c[tin1[tl]], tree[v].d);
return;
}
push(v);
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v * 2, tl, tm, pos);
else
update(v * 2 + 1, tm + 1, tr, pos);
tree[v].val = sum(tree[v * 2].val, tree[v * 2 + 1].val);
tree[v].d = sum(tree[v * 2].d, tree[v * 2 + 1].d);
}
int getsum(int v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (l == tl && r == tr) return tree[v].val;
push(v);
int tm = (tl + tr) / 2;
return sum(getsum(v * 2, tl, tm, l, min(r, tm)),
getsum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
int getvalue(int v, int tl, int tr, int pos) {
if (tl == tr) {
return tree[v].d;
}
push(v);
int tm = (tl + tr) / 2;
if (pos <= tm) return getvalue(v * 2, tl, tm, pos);
return getvalue(v * 2 + 1, tm + 1, tr, pos);
}
int main() {
scanf("%d%d", &x, &q);
c[1] = x;
n = 1;
for (int i = 1; i <= q; i++) {
int type, v;
scanf("%d", &type);
if (type == 1) {
int p;
scanf("%d%d", &p, &x);
n++;
v = n;
g[p].push_back(v);
parent[v] = p;
c[v] = x;
} else {
scanf("%d", &v);
}
queries[i] = Query(type, v);
}
tcur = 0;
dfs(1);
build(1, 1, n);
update(1, 1, n, 1);
s[1] = 1;
for (int i = 1; i <= q; i++) {
if (queries[i].type == 1) {
int v = queries[i].v;
int tmp = s[parent[v]];
int val = product(tmp + 1, power(tmp, MOD - 2));
multOnSegment(1, 1, n, lt[parent[v]], rt[parent[v]], val);
update(1, 1, n, tin[v]);
s[parent[v]]++;
s[v]++;
} else {
int v = queries[i].v;
int res = getsum(1, 1, n, lt[v], rt[v]);
int val;
if (v == 1)
val = 1;
else
val = getvalue(1, 1, n, tin[parent[v]]);
mult(res, power(val, MOD - 2));
printf("%d\n", res);
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int p = 0; p < t; p++) {
long long x, y;
cin >> x >> y;
if ((x - y) > 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 5;
int sg[N], st[N];
bool vis[N];
int main() {
int n;
scanf("%d", &n);
sg[1] = 0;
for (int i = 2; i <= n; i++) {
int c = 2 * i;
int top = 0;
for (int j = 2; j * j <= c; j++)
if (c % j == 0) {
if (c / j > j && (c / j - j - 1) % 2 == 0) {
int cur = (c / j - j - 1) / 2;
vis[sg[cur + j] ^ sg[cur]] = true;
st[top++] = sg[cur + j] ^ sg[cur];
}
if (j > c / j && (j - c / j - 1) % 2 == 0) {
int cur = (j - c / j - 1) / 2;
vis[sg[cur + c / j] ^ sg[cur]] = true;
st[top++] = sg[cur + c / j] ^ sg[cur];
}
}
int tmp = 0;
while (vis[tmp]) tmp++;
sg[i] = tmp;
sg[i] ^= sg[i - 1];
while (top) vis[st[--top]] = false;
}
if ((sg[n] ^ sg[n - 1]) == 0)
puts("-1");
else {
int c = 2 * n;
for (int i = 2; i <= n; i++)
if (c % i == 0) {
if (c / i > i && (c / i - i - 1) % 2 == 0) {
int cur = (c / i - i - 1) / 2;
if ((sg[cur + i] ^ sg[cur]) == 0) return printf("%d\n", i), 0;
}
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int M4 = 1e4 + 100, M5 = 1e5 + 100, M6 = 1e6 + 100, M7 = 1e7 + 100,
M8 = 1e8 + 100, M9 = 1e9 + 100;
int cnt[M6];
stack<int> st;
int ans, q;
void clr() {
while ((int)(st).size()) st.pop();
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
string s;
cin >> s;
int t = 0;
for (int i = 0; i < (int)(s).size(); i++) {
char c = s[i];
if (c == '(')
st.push(i);
else if ((int)(st).size()) {
cnt[i] = i - st.top() + 1;
if (st.top() > 0) cnt[i] += cnt[st.top() - 1];
st.pop();
}
}
for (int i = 0; i < (int)(s).size(); i++) ans = max(ans, cnt[i]);
for (int i = 0; i < (int)(s).size(); i++)
if (cnt[i] == ans) q++;
cout << ans << ' ' << (ans == 0 ? 1 : q);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T Abs(T first) {
return (first < 0 ? -first : first);
}
template <typename T>
T Sqr(T first) {
return (first * first);
}
string plural(string s) {
return (int((s).size()) && s[int((s).size()) - 1] == 'x' ? s + "en"
: s + "s");
}
const int INF = (int)1e9;
const long double EPS = 1e-12;
const long double PI = acos(-1.0);
bool Read(int &first) {
char c, r = 0, n = 0;
first = 0;
for (;;) {
c = getchar();
if ((c < 0) && (!r)) return (0);
if ((c == '-') && (!r))
n = 1;
else if ((c >= '0') && (c <= '9'))
first = first * 10 + c - '0', r = 1;
else if (r)
break;
}
if (n) first = -first;
return (1);
}
int main() {
if (0) freopen("in.txt", "r", stdin);
int N;
long long X, Y;
long long ans = 0, cur, tmp;
int i, j, c, ind[3], nxt[3];
static int P[300003], M[3], S[3][300003];
static char C[300003];
Read(N);
for (i = 0; i < N; i++) {
scanf("%d %c", &P[i], &C[i]);
if (C[i] == 'G') c = 0;
if (C[i] == 'R') c = 1;
if (C[i] == 'B') c = 2;
S[c][M[c]++] = P[i];
}
if (!M[0]) {
for (c = 1; c <= 2; c++)
if (M[c]) ans += S[c][M[c] - 1] - S[c][0];
cout << ans << endl;
return (0);
}
for (c = 1; c <= 2; c++) {
if ((M[c]) && (S[c][0] < S[0][0])) ans += S[0][0] - S[c][0];
if ((M[c]) && (S[c][M[c] - 1] > S[0][M[0] - 1]))
ans += S[c][M[c] - 1] - S[0][M[0] - 1];
}
memset(ind, 0, sizeof(ind));
for (i = 0; i < M[0] - 1; i++) {
X = S[0][i];
Y = S[0][i + 1];
for (c = 1; c <= 2; c++) {
while ((ind[c] < M[c]) && (S[c][ind[c]] < X)) ind[c]++;
nxt[c] = ind[c];
while ((nxt[c] < M[c]) && (S[c][nxt[c]] < Y)) nxt[c]++;
}
cur = Y - X;
for (c = 1; c <= 2; c++)
if (ind[c] < nxt[c]) {
tmp = INF;
for (j = ind[c]; j <= nxt[c]; j++) {
long long v = 0;
if (j > ind[c]) v += S[c][j - 1] - X;
if (j < nxt[c]) v += Y - S[c][j];
tmp = min(tmp, v);
}
cur += tmp;
}
ans += min(2 * (Y - X), cur);
for (c = 1; c <= 2; c++) ind[c] = nxt[c];
}
cout << ans << endl;
return (0);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, q;
int main() {
cin >> q;
while (q--) {
cin >> n;
if (n < 4 || n == 5 || n == 7 || n == 11)
cout << -1 << '\n';
else
cout << n / 4 - n % 2 << '\n';
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
bitset<1000000000> bt;
int prim[100000];
void creat() {
prim[0] = 2;
int cnt = 1;
for (int i = 3; i <= 1000000000; i += 2) {
}
}
int main() {
int n;
while (cin >> n) {
int ans = n;
while (n) {
if (n == 1) break;
if (n % 2 == 0) {
n /= 2;
ans += n;
continue;
}
int k = 0;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) {
n /= i;
ans += n;
k++;
break;
}
}
if (k == 0) {
n = 1;
ans += n;
break;
}
}
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
std::mt19937 rnd(
std::chrono::high_resolution_clock::now().time_since_epoch().count());
const double PI = atan2(0.0, -1.0);
const int INF = 0x3f3f3f3f;
const ll LINF = (ll)2e18;
void run() {
int n, k;
scanf("%d%d", &n, &k);
ll sum1 = 0, sum2 = 0;
ll sum = 0;
sum1 = 1ll * n * (n - 1) / 2;
while (k--) {
int pos, val;
scanf("%d%d", &pos, &val);
sum += val;
sum2 += 1ll * (pos - 1) * val;
}
if (sum > n) {
printf("-1\n");
} else if (sum < n) {
printf("1\n");
} else {
if (sum1 % n != sum2 % n) {
printf("-1\n");
} else {
printf("1\n");
}
}
}
int main(void) {
auto start = std::chrono::high_resolution_clock::now();
run();
auto end = std::chrono::high_resolution_clock::now();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int a[n], i, p = 0, e = 0, j, k, h = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
h = max(h, a[i]);
}
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
for (int k = i; k <= j; k++) p = (p ^ a[k]);
e = max(e, p);
p = 0;
}
}
cout << max(h, e);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
char a[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
if (n < 3) {
cout << "0";
return 0;
}
int c = 0;
int x, y;
for (int i = 2; i <= n - 1; i++) {
for (int j = 2; j <= n - 1; j++) {
if (a[i][j] == 'X' && a[i + 1][j + 1] == 'X' && a[i + 1][j - 1] == 'X' &&
a[i - 1][j + 1] == 'X' && a[i - 1][j - 1] == 'X' &&
(x != i || y != j)) {
x = i;
y = j;
c++;
}
}
}
cout << c;
return 0;
}
| 0 |
#include <bits/stdc++.h>
const long long inf = 1e9;
const long long MOD = 1e9 + 7;
const long long zero = 0;
const long long N = 1e5 + 5;
using namespace std;
int main() {
long long a[25];
for (int i = 1; i <= 24; i++) cin >> a[i];
string ans = "NO";
if ((a[1] == a[2] and a[1] == a[3] and a[1] == a[4]) &&
(a[9] == a[10] and a[9] == a[11] and a[9] == a[12]) &&
(a[5] == a[6] and a[5] == a[19] and a[5] == a[20]) &&
(a[17] == a[18] and a[17] == a[23] and a[17] == a[24]) &&
(a[21] == a[22] and a[21] == a[15] and a[21] == a[16]) &&
(a[13] == a[14] and a[13] == a[7] and a[13] == a[8]))
ans = "YES";
if ((a[1] == a[2] and a[1] == a[3] and a[1] == a[4]) &&
(a[9] == a[10] and a[9] == a[11] and a[9] == a[12]) &&
(a[5] == a[6] and a[5] == a[15] and a[5] == a[16]) &&
(a[17] == a[18] and a[17] == a[7] and a[17] == a[8]) &&
(a[21] == a[22] and a[21] == a[19] and a[21] == a[20]) &&
(a[13] == a[14] and a[13] == a[23] and a[13] == a[24]))
ans = "YES";
if ((a[13] == a[14] and a[13] == a[15] and a[13] == a[16]) &&
(a[17] == a[18] and a[17] == a[19] and a[17] == a[20]) &&
(a[1] == a[3] and a[1] == a[6] and a[1] == a[8]) &&
(a[5] == a[7] and a[5] == a[10] and a[5] == a[12]) &&
(a[9] == a[11] and a[9] == a[21] and a[9] == a[23]) &&
(a[2] == a[4] and a[2] == a[22] and a[2] == a[24]))
ans = "YES";
if ((a[13] == a[14] and a[13] == a[15] and a[13] == a[16]) &&
(a[17] == a[18] and a[17] == a[19] and a[17] == a[20]) &&
(a[2] == a[4] and a[2] == a[5] and a[2] == a[7]) &&
(a[1] == a[3] and a[1] == a[21] and a[1] == a[23]) &&
(a[6] == a[8] and a[6] == a[9] and a[6] == a[11]) &&
(a[10] == a[12] and a[10] == a[22] and a[10] == a[24]))
ans = "YES";
if ((a[5] == a[6] and a[5] == a[7] and a[5] == a[8]) &&
(a[21] == a[22] and a[21] == a[23] and a[21] == a[24]) &&
(a[1] == a[2] and a[1] == a[17] and a[1] == a[19]) &&
(a[11] == a[12] and a[11] == a[14] and a[11] == a[16]) &&
(a[13] == a[15] and a[13] == a[3] and a[13] == a[4]) &&
(a[18] == a[20] and a[18] == a[9] and a[18] == a[10]))
ans = "YES";
if ((a[5] == a[6] and a[5] == a[7] and a[5] == a[8]) &&
(a[21] == a[22] and a[21] == a[23] and a[21] == a[24]) &&
(a[1] == a[2] and a[1] == a[14] and a[1] == a[16]) &&
(a[11] == a[12] and a[11] == a[17] and a[11] == a[19]) &&
(a[9] == a[10] and a[9] == a[13] and a[9] == a[15]) &&
(a[3] == a[4] and a[3] == a[18] and a[3] == a[20]))
ans = "YES";
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
vector<pair<int, int> > g[N];
pair<long long, int> p[N];
bool present[N];
int par[N], n;
long long d[N];
void dfs(int v, long long dep, int par) {
d[v] = max(d[v], dep);
for (int i = 0; i < g[v].size(); i++)
if (g[v][i].first != par) dfs(g[v][i].first, dep + g[v][i].second, v);
}
void pre() {
dfs(0, 0, -1);
dfs(max_element(d, d + n) - d, 0, -1);
dfs(max_element(d, d + n) - d, 0, -1);
for (int i = 0; i < n; i++) p[i] = make_pair(d[i], i);
sort(p, p + n);
}
int root(int v) { return par[v] < 0 ? v : par[v] = root(par[v]); }
int add(int v) {
present[v] = true;
for (int i = 0; i < g[v].size(); i++)
if (present[g[v][i].first]) {
par[v] += par[g[v][i].first];
par[g[v][i].first] = v;
}
return -par[v];
}
void erase(int v) {
present[v] = false;
par[root(v)]++;
}
int query(long long l) {
fill(par, par + N, -1);
int j = n - 1, ans = 0;
for (int i = n - 1; i > -1; i--) {
while (j >= 0 && p[i].first - p[j].first <= l) {
ans = max(ans, add(p[j].second));
j--;
}
erase(p[i].second);
}
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
u--, v--;
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
pre();
int q;
scanf("%d", &q);
while (q--) {
long long l;
scanf("%I64d", &l);
printf("%d\n", query(l));
}
}
| 10 |
#include <bits/stdc++.h>
int main(void) {
int n, i;
scanf("%d", &n);
long long mul = 1;
for (i = 1; i <= n; i++) mul *= 2;
mul = 2 * (mul - 1);
printf("%lld", mul);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string N[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"Aa", "Ba", "Ca", "Da", "Ea", "Fa", "Ga", "Ha", "Ia", "Ja",
"Ab", "Bb", "Cb", "Db", "Eb", "Fb", "Gb", "Hb", "Ib", "Jb",
"Ac", "Bc", "Cc", "Dc", "Ec", "Fc", "Gc", "Hc", "Ic", "Jc",
"Ad", "Bd", "Cd", "Dd", "Ed", "Fd", "Gd", "Hd", "Id", "Jd"};
int main() {
ios_base::sync_with_stdio(0);
int n, k;
cin >> n >> k;
int pt = 0;
vector<string> names;
string a;
cin >> a;
if (a == "YES") {
for (; pt < k; pt++) names.push_back(N[pt]);
} else {
for (; pt < k - 1; pt++) names.push_back(N[pt]);
names.push_back(names[0]);
}
for (int i = 0; i < n - k; i++) {
string a;
cin >> a;
if (a == "YES") {
names.push_back(N[pt++]);
} else {
names.push_back(names[names.size() - k + 1]);
}
}
for (int i = 0; i < names.size(); i++) {
cout << names[i] << " ";
}
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
long long a, b;
scanf("%lld %lld", &a, &b);
m1:
if (a <= 0 || b <= 0) goto end;
if (a >= 2 * b) {
a = a - 2 * b;
if (a / 2 / b > 2) a = a - (a / 2 / b - 1) * b * 2;
goto m1;
}
if (b >= 2 * a) {
b = b - 2 * a;
if (b / 2 / a > 2) b = b - (b / 2 / a - 1) * a * 2;
goto m1;
} else
goto end;
end:
printf("%lld %lld\n", a, b);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, sum = 0;
cin >> a;
int b[a][a];
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
cin >> b[i][j];
if (i == j || i == (a - 1) / 2 || j == (a - 1) / 2 || i + j == a - 1)
sum += b[i][j];
}
}
cout << sum;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, flag, ind, i;
int main() {
cin >> n;
int *a = (int *)malloc((n + 1) * sizeof(int));
for (long long i = 1; i <= n; i++) cin >> a[i];
for (i = 2; i <= n; i++) {
if (a[i] < a[i - 1]) {
flag++;
ind = i;
}
}
if (flag > 1) return cout << -1, 0;
if (flag == 0) return cout << 0, 0;
if (a[n] > a[1]) return cout << -1, 0;
cout << n - ind + 1;
return 0;
}
| 2 |
#include <bits/stdc++.h>
const int maxn = 1.2e5 + 5, Maxn = 2.4e5 + 5;
inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; }
int n, m, p[maxn];
int Log[maxn], Max[maxn][18], Min[maxn][18];
void getM() {
for (int i = 2; i <= n; ++i) Log[i] = Log[i >> 1] + 1;
for (int i = 1; i <= n; ++i) Max[i][0] = Min[i][0] = p[i];
for (int j = 1; j <= Log[n]; ++j)
for (int i = 1; i + (1 << j) - 1 <= n; ++i)
Max[i][j] = max(Max[i][j - 1], Max[i + (1 << j - 1)][j - 1]),
Min[i][j] = min(Min[i][j - 1], Min[i + (1 << j - 1)][j - 1]);
}
int gMin(int l, int r) {
int B = Log[r - l + 1];
return min(Min[l][B], Min[r - (1 << B) + 1][B]);
}
int gMax(int l, int r) {
int B = Log[r - l + 1];
return max(Max[l][B], Max[r - (1 << B) + 1][B]);
}
bool legal(int l, int r) { return gMax(l, r) - gMin(l, r) - (r - l) == 0; }
int minn[maxn << 2], tag[maxn << 2];
void psp(int k) { minn[k] = min(minn[k << 1], minn[k << 1 | 1]); }
void gtg(int k, int v) { tag[k] += v, minn[k] += v; }
void psd(int k) {
if (tag[k]) gtg(k << 1, tag[k]), gtg(k << 1 | 1, tag[k]), tag[k] = 0;
}
void bld(int k, int l, int r) {
if (l == r) return minn[k] = l, void();
int mid = l + r >> 1;
bld(k << 1, l, mid), bld(k << 1 | 1, mid + 1, r), psp(k);
}
void mdf(int k, int l, int r, int x, int y, int v) {
if (l > y || r < x) return;
if (l >= x && r <= y) return gtg(k, v);
int mid = l + r >> 1;
psd(k), mdf(k << 1, l, mid, x, y, v), mdf(k << 1 | 1, mid + 1, r, x, y, v),
psp(k);
}
int qry(int k, int l, int r) {
if (l == r) return l;
int mid = l + r >> 1;
psd(k);
return minn[k << 1] ? qry(k << 1 | 1, mid + 1, r) : qry(k << 1, l, mid);
}
struct Int {
int l, r, v;
Int(){};
Int(int L, int R, int V) : l(L), r(R), v(V){};
} stmn[maxn], stmx[maxn];
int st[maxn], tp, tpmn, tpmx;
std ::vector<int> son[Maxn];
int nod, rt, id[maxn], L[Maxn], R[Maxn], Rl[Maxn], ty[Maxn];
void Build() {
for (int i = 1; i <= n; ++i) {
mdf(1, 1, n, 1, n, -1);
while (tpmn && stmn[tpmn].v > p[i])
mdf(1, 1, n, stmn[tpmn].l, stmn[tpmn].r, stmn[tpmn].v - p[i]), --tpmn;
while (tpmx && stmx[tpmx].v < p[i])
mdf(1, 1, n, stmx[tpmx].l, stmx[tpmx].r, p[i] - stmx[tpmx].v), --tpmx;
++tpmn, stmn[tpmn] = Int(stmn[tpmn - 1].r + 1, i, p[i]), ++tpmx,
stmx[tpmx] = Int(stmx[tpmx - 1].r + 1, i, p[i]);
++nod, id[L[nod] = R[nod] = i] = nod;
int now = nod, Lef = qry(1, 1, n);
while (tp && L[st[tp]] >= Lef) {
if (ty[st[tp]] && legal(Rl[st[tp]], i))
son[st[tp]].push_back(now), R[st[tp]] = i, Rl[st[tp]] = L[now],
now = st[tp--];
else if (legal(L[st[tp]], i))
++nod, son[nod].push_back(st[tp]), son[nod].push_back(now),
L[nod] = L[st[tp]], R[nod] = i, ty[nod] = 1, Rl[nod] = L[now],
now = nod, --tp;
else {
++nod, R[nod] = i, son[nod].push_back(now);
do {
L[nod] = L[st[tp]], son[nod].push_back(st[tp]);
} while (!legal(L[st[tp--]], i));
now = nod;
std ::reverse(son[nod].begin(), son[nod].end());
}
}
st[++tp] = now;
}
rt = st[1];
}
int dep[Maxn], pos[Maxn], fa[Maxn][18];
long long val[Maxn], sum[Maxn], pre[Maxn], suf[Maxn], jL[Maxn][18],
jR[Maxn][18];
long long C2(int n) { return 1ll * n * (n - 1) / 2; }
void getS(int u) {
val[u] = sum[u] = ty[u] ? C2(son[u].size()) : 1;
for (int i = 0; i < son[u].size(); ++i)
dep[son[u][i]] = dep[u] + 1, fa[son[u][i]][0] = u, getS(son[u][i]),
sum[u] += sum[son[u][i]];
}
void getJ(int u) {
int sons = son[u].size();
for (int i = 1; i <= Log[dep[u]]; ++i)
fa[u][i] = fa[fa[u][i - 1]][i - 1],
jL[u][i] = jL[u][i - 1] + jL[fa[u][i - 1]][i - 1],
jR[u][i] = jR[u][i - 1] + jR[fa[u][i - 1]][i - 1];
if (!sons) return;
for (int i = 0; i < sons; ++i) pos[son[u][i]] = i;
pre[son[u][0]] = sum[son[u][0]];
for (int i = 1; i < sons; ++i)
pre[son[u][i]] = pre[son[u][i - 1]] + sum[son[u][i]];
suf[son[u][sons - 1]] = sum[son[u][sons - 1]];
for (int i = sons - 2; ~i; --i)
suf[son[u][i]] = suf[son[u][i + 1]] + sum[son[u][i]];
jR[son[u][0]][0] = 0;
for (int i = 1; i < sons; ++i)
jR[son[u][i]][0] =
jR[son[u][i - 1]][0] + sum[son[u][i - 1]] + ty[u] * (i - 1);
jL[son[u][sons - 1]][0] = 0;
for (int i = sons - 2; ~i; --i)
jL[son[u][i]][0] =
jL[son[u][i + 1]][0] + sum[son[u][i + 1]] + ty[u] * (sons - i - 2);
for (int i = 0; i < sons; ++i) getJ(son[u][i]);
}
struct pair {
int x, y;
pair(int X = 0, int Y = 0) : x(X), y(Y){};
};
pair LCA(int x, int y) {
int Flag = dep[x] < dep[y];
if (dep[x] < dep[y]) x ^= y ^= x ^= y;
while (dep[x] ^ dep[y]) x = fa[x][Log[dep[x] - dep[y]]];
for (int i = Log[dep[x]]; ~i; --i)
if (fa[x][i] ^ fa[y][i]) x = fa[x][i], y = fa[y][i];
return Flag ? pair(y, x) : pair(x, y);
}
long long getans(int l, int r) {
if (l == r) return 1;
long long ret = 0;
int lp = id[l - 1], rp = id[r + 1];
if (!lp && !rp)
return sum[rt];
else if (!lp) {
for (int i = Log[dep[rp]]; ~i; --i)
if (fa[rp][i] && (fa[rp][i] ^ rt)) ret += jR[rp][i], rp = fa[rp][i];
ret += jR[rp][0];
} else if (!rp) {
for (int i = Log[dep[lp]]; ~i; --i)
if (fa[lp][i] && (fa[lp][i] ^ rt)) ret += jL[lp][i], lp = fa[lp][i];
ret += jL[lp][0];
} else {
pair pr = LCA(lp, rp);
int lca = fa[pr.x][0];
for (int i = Log[dep[lp]]; ~i; --i)
if (dep[fa[lp][i]] >= dep[pr.x]) ret += jL[lp][i], lp = fa[lp][i];
for (int i = Log[dep[rp]]; ~i; --i)
if (dep[fa[rp][i]] >= dep[pr.y]) ret += jR[rp][i], rp = fa[rp][i];
ret += (sum[lca] - val[lca] - pre[lp] - suf[rp]) +
(ty[lca] ? C2(pos[pr.y] - pos[pr.x] - 1) : 0);
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &p[i]);
getM(), bld(1, 1, n), Build(), getS(rt), getJ(rt), scanf("%d", &m);
for (int i = 1, l, r; i <= m; ++i)
scanf("%d %d", &l, &r), printf("%lld\n", getans(l, r));
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
bool check(long long n, long long pos) { return n & (1LL << pos); }
long long Set(long long n, long long pos) { return n = n | (1LL << pos); }
struct segTree {
long long n;
vector<long long> tree, lazy;
segTree(long long n) : n(n), tree(4 * n, 0), lazy(4 * n, 0) {}
void propagate(long long node, long long l, long long r) {
tree[node] += lazy[node] * (r - l + 1);
if (l != r) lazy[2 * node] += lazy[node], lazy[2 * node + 1] += lazy[node];
lazy[node] = 0;
}
void treeUpdate(long long node, long long l, long long r, long long u,
long long v, long long val) {
long long mid = (l + r) >> 1;
propagate(node, l, r);
if (l > v || r < u) return;
if (l >= u && r <= v) {
lazy[node] += val;
propagate(node, l, r);
return;
}
treeUpdate(2 * node, l, mid, u, v, val);
treeUpdate(2 * node + 1, mid + 1, r, u, v, val);
tree[node] = tree[2 * node] + tree[2 * node + 1];
}
void update(long long l, long long r, long long val) {
treeUpdate(1, 1, n, l, r, val);
}
long long treeQuery(long long node, long long l, long long r, long long u,
long long v) {
long long mid = (l + r) >> 1;
propagate(node, l, r);
if (l > v || r < u) return 0;
if (l >= u && r <= v) return tree[node];
return treeQuery(2 * node, l, mid, u, v) +
treeQuery(2 * node + 1, mid + 1, r, u, v);
}
long long query(long long l, long long r) { return treeQuery(1, 1, n, l, r); }
};
int main() {
long long i, j, k, l, m, n, o, r, q;
long long testcase;
long long input, flag, tag, ans;
while (cin >> n >> m) {
set<pair<long long, long long> > st;
segTree tree(n);
for (i = 1; i <= n + 1; i++) {
st.insert({i, i});
}
for (i = 1; i <= m; i++) {
long long t;
scanf("%lld", &t);
scanf("%lld", &l);
scanf("%lld", &r);
if (t == 2) {
printf("%lld\n", tree.query(l, r));
} else {
scanf("%lld", &input);
set<pair<long long, long long> >::iterator it, jt, kt, mt;
it = st.lower_bound({l, 0});
if (it->first != l) {
jt = it;
jt--;
st.insert({l, jt->second});
}
it = st.upper_bound({r, LLONG_MAX});
if (it->first != r + 1) {
jt = it;
jt--;
st.insert({r + 1, jt->second});
}
it = st.lower_bound({l, 0});
jt = st.upper_bound({r, LLONG_MAX});
for (kt = it; kt != jt; kt = st.erase(kt)) {
long long a = kt->first;
mt = kt;
mt++;
long long b = mt->first - 1;
tree.update(a, b, abs(input - kt->second));
}
st.insert({l, input});
}
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long int DINF = 1e18;
double Pi = 3.14159265359;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int n = s.size();
char c = s[0];
bool imp = true;
for (int i = 0; i < n; i++) {
if (s[i] != c && ((n % 2 == 0) ? true : (i != n / 2))) {
imp = false;
break;
}
}
if (imp) return cout << "Impossible", 0;
for (int i = 0; i < n / 2; i++) {
string t = s.substr(i + 1, n - i - 1) + s.substr(0, i + 1);
bool pal = true;
for (int j = 0; j < n / 2; j++) {
if (t[j] != t[n - j - 1]) {
pal = false;
break;
}
}
if (pal && t != s) return cout << 1, 0;
}
cout << 2;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000007;
const long long linf = 1000000000000000000LL;
const double eps = 0.000001;
const double pi = 3.14159265358979323846;
template <class T>
T abs(T k) {
return k >= 0 ? k : -k;
}
template <class T>
T sqr(T n) {
return n * n;
}
template <class T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <class T>
T mod(T a, T b) {
return (a % b + b) % b;
}
template <class T>
void mode(T &a, T b) {
a = mod(a, b);
}
template <class T>
void maxe(T &a, T b) {
a = max(a, b);
}
template <class T>
void mine(T &a, T b) {
a = min(a, b);
}
void fileio_in_out() {
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
}
void fileio_txt() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int test;
int n, m, k, ans;
int a[1111111], b[1111111], sa[1111111], sb[1111111], x, y;
int check(int aa, int bb) {
int t = (n - aa) + 2 * (m - bb), w = sa[aa] + sb[bb];
return t >= w;
}
int main() {
scanf("%d", &k);
while (k--) {
scanf("%d%d", &x, &y);
if (x == 1)
a[++n] = y;
else
b[++m] = y;
}
sort(a + 1, a + 1 + n);
sort(b + 1, b + 1 + m);
for (int i = 1; i <= n; i++) sa[i] = sa[i - 1] + a[i];
for (int i = 1; i <= m; i++) sb[i] = sb[i - 1] + b[i];
ans = inf;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
if (check(i, j)) mine(ans, (n - i) + 2 * (m - j));
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int n, m, k, dp[1 << 20][20], a[20], b[20][20];
bool check(long long int num, long long int pos) {
if (num & ((long long int)1 << pos)) return true;
return false;
}
long long int setbit(long long int num, long long int pos) {
return (num | (1 << pos));
}
long long int dynamic(long long int mask, long long int now,
long long int cnt) {
if (cnt >= m) return dp[mask][now + 1] = 0;
if (dp[mask][now + 1] != -1) return dp[mask][now + 1];
for (long long int i = 0; i < n; i++) {
if (!check(mask, i)) {
long long int lol = dynamic(setbit(mask, i), i, cnt + 1);
lol += (a[i] + b[now + 1][i + 1]);
dp[mask][now + 1] = max(lol, dp[mask][now + 1]);
}
}
return dp[mask][now + 1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
memset(dp, -1, sizeof(dp));
scanf("%lld%lld%lld", &n, &m, &k);
for (long long int i = 0; i < n; i++) scanf("%lld", &a[i]);
for (long long int i = 1, x, y, z; i <= k; i++) {
scanf("%lld%lld%lld", &x, &y, &z);
b[x][y] += z;
}
printf("%lld", dynamic(0, -1, 0));
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7f7f7f7f;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int> > sons(n, vector<int>());
vector<vector<vector<int> > > can(1 << n,
vector<vector<int> >(n, vector<int>(n, 0)));
vector<vector<vector<int> > > path_can(
1 << n, vector<vector<int> >(n, vector<int>(n)));
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
u -= 1;
v -= 1;
sons[u].push_back(v);
sons[v].push_back(u);
}
for (int i = 0; i < n; ++i)
for (int j : sons[i]) can[0][i][j] = 1, path_can[0][i][j] = i;
for (int s = 0; s < (1 << n); ++s) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == j || !can[s][i][j]) continue;
for (int k : sons[j]) {
if (s & (1 << k)) continue;
if (k == path_can[s][i][j]) continue;
int _s = s | (1 << j);
if (can[_s][i][k]) continue;
can[_s][i][k] = 1;
path_can[_s][i][k] = j;
}
}
}
}
vector<int> dp(1 << n, INF);
vector<pair<int, int> > path_dp(1 << n);
vector<int> status_dp(1 << n);
for (int i = 0; i < n; ++i) dp[1 << i] = 0;
for (int s = 0; s < (1 << n); ++s) {
for (int from = s; from; from = (from - 1) & s) {
int add = s ^ from;
int cnt = __builtin_popcount(add) + 1;
if (dp[from] + cnt >= dp[s]) continue;
for (int i = 0; i < n; ++i) {
if (!(from & (1 << i))) continue;
for (int j = 0; j < n; ++j) {
if (!(from & (1 << j))) continue;
if (!can[add][i][j]) continue;
dp[s] = dp[from] + cnt;
path_dp[s] = make_pair(i, j);
status_dp[s] = add;
}
}
}
}
int s = (1 << n) - 1;
cout << dp[s] << endl;
while (true) {
if (__builtin_popcount(s) <= 1) break;
int u = path_dp[s].first;
int v = path_dp[s].second;
int sets = status_dp[s];
s ^= sets;
do {
int nv = path_can[sets][u][v];
cout << v + 1 << " " << nv + 1 << endl;
v = nv;
sets ^= (1 << nv);
} while (u != v);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
scanf("%lld", &n);
string s;
long long l;
vector<string> vs;
for (long long i = 0; i < n; i++) {
cin >> s;
l = s.length();
s = s + s;
vs.push_back(s);
}
long long mx = 1000000000;
for (long long i = 0; i < l; i++) {
s = vs[0].substr(i, l);
bool b = 1;
long long t = i;
for (long long j = 1; j < n; j++) {
size_t found = vs[j].find(s);
if (found != string::npos) {
t += (found);
} else {
b = 0;
break;
}
}
if (b) {
if (mx > t) mx = t;
}
}
if (mx == 1000000000)
cout << "-1";
else
cout << mx;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
queue<int> Q;
vector<int> edges[11000];
int num[11000];
int ans;
int n;
int bfs(int s) {
int cnt = 0;
for (int i = 0; i < edges[s].size(); i++) {
cnt += edges[edges[s][i]].size() - 1;
}
return cnt;
}
int main(void) {
int i;
scanf("%d", &n);
for (i = 1; i <= n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
edges[u].push_back(v);
edges[v].push_back(u);
}
for (i = 1; i <= n; i++) {
num[i] = bfs(i);
ans += num[i];
}
printf("%d", ans / 2);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[1000], i = 0, n;
cin >> n;
for (i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
cout << arr[0];
for (i = 1; i < n; i++) cout << ' ' << arr[i];
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcd(int a, int b) {
for (int i = 2; i <= min(a, b); i++) {
if ((a % i == 0) && (b % i == 0)) return i;
}
return 1;
}
bool isprime(int n) {
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
string int_to_str(long long x) {
string xs;
stringstream ss;
ss << x;
xs = ss.str();
return xs;
}
long long str_to_int(string x) {
long long tempi = 0, index = 1;
for (int j = x.size() - 1; j >= 0; j--) {
tempi += ((x[j] - '0') * index);
index *= 10;
}
return tempi;
}
int main() {
int n, temp, ans, index = 0, step1, step2;
vector<int> num, final;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> temp;
num.push_back(temp);
}
if (n == 1) {
cout << temp << endl;
return 0;
}
ans = min(num[0], num[num.size() - 1]);
if (n == 2) {
cout << ans << endl;
return 0;
}
while (index < n - 1) {
if ((index + 1) <= (n - 1)) step1 = index + 1;
if ((index + 2) <= (n - 1))
step2 = index + 2;
else
step2 = step1;
if (num[step1] >= ans && num[step2] < ans) {
index = step1;
} else if (num[step2] >= ans && num[step1] < ans) {
index = step2;
} else if (num[step1] < ans && num[step2] < ans) {
if (num[step1] > num[step2]) {
index = step1;
ans = num[step1];
} else {
index = step2;
ans = num[step2];
}
} else {
index = step2;
}
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, j, c = 0, cnt = 0, t = 1;
cin >> n >> m;
int a[n][m];
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) cin >> a[i][j];
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
if (a[j][i] == 1) {
cnt += c * t;
c = 0;
if (t == 1) t = 2;
} else
c++;
}
if (t == 2) cnt += c;
t = 1;
c = 0;
}
for (j = 0; j < m; j++) {
for (i = 0; i < n; i++) {
if (a[i][j] == 1) {
cnt += c * t;
c = 0;
if (t == 1) t = 2;
} else
c++;
}
if (t == 2) cnt += c;
t = 1;
c = 0;
}
cout << cnt << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
long long wupin[350];
vector<long long> mp[350];
long long a[350];
long long b[350];
long long dp[150000];
long long degree[150000];
long long vis[150000];
long long n, q, t;
void Top() {
queue<long long> s;
memset(dp, 0, sizeof(dp));
for (long long i = 1; i <= n; i++)
if (degree[i] == 0) s.push(i);
long long cnt = 0;
int cont = 0;
while (!s.empty()) {
long long u = s.front();
s.pop();
cont++;
if (vis[u] > 0) t -= a[u] - b[u];
if (a[u] <= t) wupin[cnt++] = a[u];
for (long long i = 0; i < mp[u].size(); i++) {
long long v = mp[u][i];
a[v] += a[u];
degree[v]--;
if (degree[v] == 0) s.push(v);
}
}
dp[0] = 1;
for (long long i = 0; i < cnt; i++) {
for (long long j = wupin[i]; j <= t; j++) {
dp[j] += dp[j - wupin[i]];
dp[j] %= mod;
}
}
if (t >= 0 && cont == n)
printf(
"%lld"
"\n",
dp[t]);
else
printf("0\n");
}
int main() {
while (
~scanf("%lld"
"%lld"
"%lld",
&n, &q, &t)) {
memset(vis, 0, sizeof(vis));
memset(degree, 0, sizeof(degree));
for (long long i = 1; i <= n; i++) mp[i].clear();
for (long long i = 1; i <= n; i++) scanf("%I64d", &a[i]), b[i] = a[i];
for (long long i = 0; i < q; i++) {
long long x, y;
scanf(
"%lld"
"%lld",
&x, &y);
vis[x]++, vis[y]++;
mp[x].push_back(y);
degree[y]++;
}
Top();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long n;
int m, mod;
long long solve(long long ql, long long qr, long long u, long long xl = 0,
long long xr = n, long long val = 0, int t = 0) {
if (xr <= ql || qr <= xl) return 0;
if (ql <= xl && xr <= qr) {
u--;
if (u - val < 0) return 0;
long long k = 1 + (u - val) / (1LL << t);
long long kp = 2 * val + ((k - 1) * (1LL << t));
long long ret = k;
if (kp & 1)
k >>= 1;
else
kp >>= 1;
ret += k % mod * (kp % mod) % mod;
if (ret >= mod) ret -= mod;
return ret;
}
long long xm = (xl + xr + 1) >> 1;
long long ret = solve(ql, qr, u, xl, xm, val, t + 1) +
solve(ql, qr, u, xm, xr, val + (1LL << t), t + 1);
if (ret >= mod) ret -= mod;
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> mod;
for (int i = 0; i < m; i++) {
long long l, r, u, v;
cin >> l >> r >> u >> v;
l--;
long long ret = solve(l, r, min(n, v)) - solve(l, r, min(n, u - 1));
ret = (ret + mod) % mod;
cout << ret << '\n';
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
bool P = false;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (x == 1) {
P = true;
break;
}
}
if (P)
cout << -1;
else
cout << 1;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, i, s = 0;
cin >> x;
for (i = 1; i < x; i++) {
s = (s + i) % x;
cout << s + 1 << " ";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class F, class S>
ostream& operator<<(ostream& o, const pair<F, S>& p) {
return o << "(" << p.first << ", " << p.second << ")";
}
template <class C>
void O__(ostream& o, const C& c) {
bool f = 1;
for (const auto& x : c) {
if (!f) o << ", ";
f = 0;
o << x;
}
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
o << "[";
O__(o, v);
o << "]";
return o;
}
template <class T, class V>
ostream& operator<<(ostream& o, const map<T, V>& v) {
o << "{";
O__(o, v);
o << "}";
return o;
}
template <class T>
ostream& operator<<(ostream& o, const set<T>& v) {
o << "{";
O__(o, v);
o << "}";
return o;
}
const double PI = 3.1415926535897932384626433832795;
const double EPS = 1e-9;
const int INF = (1 << 31) - 1;
const long long LLINF = (1LL << 60) - 1;
struct Rule {
int left;
vector<int> right;
int terminals;
};
vector<bool> isGenerative;
vector<Rule> rules;
vector<long long> maxLen;
vector<bool> opened;
vector<bool> closed;
vector<vector<int>> Index;
void check(long long& x) {
x = min(x, LLINF);
if (x == LLINF) return;
x = min(x, 314000000LL);
}
long long f(int i) {
if (closed[i]) return maxLen[i];
if (opened[i]) return maxLen[i] = LLINF;
opened[i] = 1;
for (int j = 0; j < Index[i].size(); ++j) {
int ruleIndex = Index[i][j];
bool gener = true;
for (int k = 0; k < rules[ruleIndex].right.size(); ++k) {
gener = gener && isGenerative[rules[ruleIndex].right[k]];
}
if (!gener) continue;
long long cur = rules[ruleIndex].terminals;
for (int k = 0; k < rules[ruleIndex].right.size(); ++k) {
cur += f(rules[ruleIndex].right[k]);
check(cur);
}
maxLen[i] = max(maxLen[i], cur);
}
closed[i] = 1;
check(maxLen[i]);
return maxLen[i];
}
void update(vector<long long>& ar, int i, long long v,
set<pair<long long, int>>& Q) {
if (ar[i] > v) {
Q.erase(make_pair(ar[i], i));
ar[i] = v;
Q.insert(make_pair(v, i));
}
}
int main(int argc, const char* argv[]) {
int rulesNum, nonternunals;
cin >> rulesNum >> nonternunals;
isGenerative.assign(nonternunals, false);
Index.resize(nonternunals);
for (int i = 0; i < rulesNum; ++i) {
int left, len;
scanf("%d%d", &left, &len);
--left;
Index[left].push_back(i);
Rule rule;
rule.left = left;
rule.terminals = 0;
while (len-- > 0) {
int x;
scanf("%d", &x);
if (x == -1)
++rule.terminals;
else {
rule.right.push_back(x - 1);
}
}
rules.push_back(rule);
}
set<pair<long long, int>> Q;
vector<long long> nonTerminalsRemain(rulesNum);
vector<long long> minNontermLen(nonternunals, LLINF);
vector<long long> minRuleLength(rulesNum, LLINF);
vector<vector<int>> revIndex(nonternunals);
for (int i = 0; i < rulesNum; ++i) {
minRuleLength[i] = rules[i].terminals;
nonTerminalsRemain[i] = rules[i].right.size();
for (int j = 0; j < rules[i].right.size(); ++j) {
revIndex[rules[i].right[j]].push_back(i);
}
if (nonTerminalsRemain[i] == 0) {
update(minNontermLen, rules[i].left, minRuleLength[i], Q);
}
}
while (!Q.empty()) {
pair<long long, int> top = *Q.begin();
Q.erase(Q.begin());
;
if (minNontermLen[top.second] != top.first) continue;
long long len = top.first;
int nonterm = top.second;
for (int i = 0; i < revIndex[nonterm].size(); ++i) {
int ruleIndex = revIndex[nonterm][i];
--nonTerminalsRemain[ruleIndex];
minRuleLength[ruleIndex] += len;
check(minRuleLength[ruleIndex]);
if (nonTerminalsRemain[ruleIndex] == 0) {
update(minNontermLen, rules[ruleIndex].left, minRuleLength[ruleIndex],
Q);
}
}
}
for (int i = 0; i < rules.size(); ++i) {
if (nonTerminalsRemain[i] == 0) {
isGenerative[rules[i].left] = 1;
}
}
maxLen.resize(nonternunals);
opened.resize(nonternunals);
closed.resize(nonternunals);
for (int i = 0; i < nonternunals; ++i) {
if (!isGenerative[i]) continue;
f(i);
check(maxLen[i]);
}
for (int i = 0; i < nonternunals; ++i) {
if (!isGenerative[i]) {
printf("-1 -1\n");
continue;
}
unsigned int a, b;
check(minNontermLen[i]);
a = minNontermLen[i];
printf("%u ", a);
check(maxLen[i]);
if (maxLen[i] == LLINF) {
printf("-2\n");
} else {
b = maxLen[i];
printf("%u\n", b);
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
long long MOD = 1e9 + 7, pw[MAXN], pw1[MAXN];
int jsum[MAXN], jmin[MAXN], jpar[MAXN], topar[MAXN];
string s;
long long fpow(long long base, long long exp) {
long long result = 1;
while (exp) {
if (exp & 1) result = (result * base) % MOD;
exp >>= 1;
base = (base * base) % MOD;
}
return result;
}
long long sum(long long x, long long y, long long mod = MOD) {
return (x + y) % mod;
}
long long sub(long long x, long long y, long long mod = MOD) {
return (x - y + mod) % mod;
}
long long mul(long long x, long long y, long long mod = MOD) {
return x * y % mod;
}
long long divv(long long x, long long y, long long mod = MOD) {
return x * fpow(y, mod - 2) % mod;
}
struct num {
long long len, val, val1;
num(long long len, long long val, long long val1)
: len(len), val(val), val1(val1) {}
num() {}
num concat(num x) {
long long nw = sum(mul(val, x.len, MOD - 1), x.val, MOD - 1);
return num(mul(len, x.len), sum(mul(val, x.len), x.val), nw);
}
};
num ran[MAXN];
void preproc() {
for (int i = 0, ThxDem = int(s.size()); i < ThxDem; ++i)
jsum[i] = jmin[i] = jpar[i] = topar[i] = int(s.size());
int dep = 0;
stack<pair<int, int> > sum, mn, open;
stack<int> par;
for (int i = int(s.size()) - 1; i >= 0; i--) {
if (s[i] == ')') par.push(i), dep++;
if (s[i] == '(') {
while (int(sum.size()) && sum.top().second == dep) sum.pop();
while (int(mn.size()) && mn.top().second == dep) mn.pop();
topar[i] = par.top();
par.pop();
dep--;
open.push({i, dep});
}
if (int(open.size()) && open.top().second == dep)
jpar[i] = open.top().first;
if (int(sum.size()) && sum.top().second == dep) jsum[i] = sum.top().first;
if (int(mn.size()) && mn.top().second == dep) jmin[i] = mn.top().first;
if (s[i] == '+') sum.push({i, dep});
if (s[i] == '-') mn.push({i, dep});
}
}
num getnum(int l, int r) {
num ans(pw[r - l + 1], 0, 0);
for (int i = l, ThxDem = r + 1; i < ThxDem; ++i) {
ans.val = sum(mul(ans.val, 10), s[i] - '0');
ans.val1 = sum(mul(ans.val1, 10, MOD - 1), s[i] - '0', MOD - 1);
}
return ans;
}
num range(num x, num y) {
long long rl1 = sum(sub(y.val, x.val), 1);
long long rl2 = sum(sub(y.val, x.val), 2);
long long rl11 = sum(sub(y.val1, x.val1, MOD - 1), 1, MOD - 1);
long long rl21 = sum(sub(y.val1, x.val1, MOD - 1), 2, MOD - 1);
num ans(fpow(x.len, rl11), 0, 0);
long long v = sub(fpow(x.len, rl11), 1);
v = mul(sum(y.val, 1), v);
v = divv(v, sub(x.len, 1));
long long v2 = mul(rl1, fpow(x.len, rl21));
long long v3 = mul(rl2, fpow(x.len, rl11));
v2 = divv(sum(sub(v2, v3), 1), fpow(sub(x.len, 1), 2));
v = sub(v, v2);
ans.val = v;
ans.val1 = v;
return ans;
}
num get_ran(int l1, int r1, int l2, int r2) {
int lle = r1 - l1 + 1, lri = r2 - l2 + 1;
num x = getnum(l1, r1), y = getnum(l2, r2);
if (lle == lri) return range(x, y);
num fir(pw[lle], sub(pw[lle], 1), sub(pw1[lle], 1, MOD - 1));
num sec(pw[lri], pw[lri - 1], pw1[lri - 1]);
num ans = range(x, fir);
num lst = range(sec, y);
for (int i = lle + 1, ThxDem = lri; i < ThxDem; ++i) ans = ans.concat(ran[i]);
ans = ans.concat(lst);
return ans;
}
num copy(num x, num y) {
num ans(fpow(y.len, x.val1), 0, 0);
if (y.len % MOD == 1)
ans.val = x.val, ans.val1 = x.val1;
else {
ans.val = divv(sub(fpow(y.len, x.val1), 1), sub(y.len, 1));
ans.val1 = ans.val % (MOD - 1);
}
ans.val = mul(ans.val, y.val);
ans.val1 = mul(ans.val1, y.val1, MOD - 1);
return ans;
}
num solve(int l, int r) {
if (l > r) return num(1, 0, 0);
if (jsum[l] <= r) {
num lef = solve(l, jsum[l] - 1), rig = solve(jsum[l] + 1, r);
return lef.concat(rig);
}
if (jmin[l] <= r) {
num lef = solve(l, jmin[l] - 1), rig = solve(jmin[l] + 1, r);
return get_ran(l, jmin[l] - 1, jmin[l] + 1, r);
}
if (jpar[l] <= r) {
num lef = solve(l, jpar[l] - 1), rig = solve(jpar[l] + 1, r - 1);
return copy(lef, rig);
}
return getnum(l, r);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
pw[0] = 1;
for (int i = 1, ThxDem = MAXN; i < ThxDem; ++i) pw[i] = mul(10, pw[i - 1]);
pw1[0] = 1;
for (int i = 1, ThxDem = MAXN; i < ThxDem; ++i)
pw1[i] = mul(10, pw1[i - 1], MOD - 1);
cin >> s;
for (int i = 2, ThxDem = int(s.size()) + 1; i < ThxDem; ++i) {
num l(pw[i], pw[i - 1], pw1[i - 1]);
num r(pw[i], sub(pw[i], 1), sub(pw1[i], 1, MOD - 1));
ran[i] = range(l, r);
}
preproc();
num ans = solve(0, int(s.size()) - 1);
cout << ans.val << "\n";
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100050;
int head[MaxN * 2], cnt;
struct edge {
int next, to;
} e[MaxN * 6];
int n, a[MaxN], b[MaxN], col[MaxN * 2];
void link(int u, int v) {
++cnt;
e[cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
++cnt;
e[cnt].next = head[v];
e[cnt].to = u;
head[v] = cnt;
}
void dfs(int x, int c) {
col[x] = c;
for (int i = head[x]; i; i = e[i].next)
if (col[e[i].to] == -1) dfs(e[i].to, c ^ 1);
}
int main() {
scanf("%d", &n);
memset(col, 0xff, sizeof(col));
for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i], &b[i]);
for (int i = 1; i <= n; ++i) {
link(a[i], b[i]);
if (i != n)
link(2 * i, 2 * i + 1);
else
link(2 * i, 1);
}
for (int i = 1; i <= 2 * n; ++i)
if (col[i] == -1) dfs(i, 1);
for (int i = 1; i <= n; ++i) printf("%d %d\n", col[a[i]] + 1, col[b[i]] + 1);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using point = complex<ld>;
bool onSegment(point p, point a1, point a2) {
return (fabs(((conj(((a2) - (a1))) * (((p) - (a1)))).imag())) < 1e-9 &&
((conj(((a2) - (a1))) * (((p) - (a1)))).real()) > -1e-9 &&
((conj(((a1) - (a2))) * (((p) - (a2)))).real()) > -1e-9);
}
int main() {
int t, x, y;
cin >> t;
while (t--) {
point f, s, th1, th2, com;
bool flag = 0;
vector<point> v(6);
for (int i = 0; i < 6; i++) {
scanf("%d%d", &x, &y);
v[i] = point(x, y);
}
for (int i = 0; i < 6; i++) {
int idx = find(v.begin() + i + 1, v.end(), v[i]) - v.begin();
if (idx != 6) {
com = v[i], f = v[i ^ 1], s = v[idx ^ 1];
if (i > 1)
th1 = v[0], th2 = v[1];
else if (idx < 4)
th1 = v[4], th2 = v[5];
else
th1 = v[2], th2 = v[3];
}
}
if (onSegment(th1, f, com) && onSegment(th2, s, com)) {
ld l1 =
((long double)hypot((((com) - (th1))).imag(),
(((com) - (th1))).real())) /
((long double)hypot((((f) - (th1))).imag(), (((f) - (th1))).real()));
ld l2 =
((long double)hypot((((com) - (th2))).imag(),
(((com) - (th2))).real())) /
((long double)hypot((((s) - (th2))).imag(), (((s) - (th2))).real()));
flag = (l1 >= 0.25 - 1e-9 && l1 <= 4 + 1e-9 && l2 >= 0.25 - 1e-9 &&
l2 <= 4 + 1e-9);
}
if (onSegment(th2, f, com) && onSegment(th1, s, com)) {
ld l1 =
((long double)hypot((((com) - (th2))).imag(),
(((com) - (th2))).real())) /
((long double)hypot((((f) - (th2))).imag(), (((f) - (th2))).real()));
ld l2 =
((long double)hypot((((com) - (th1))).imag(),
(((com) - (th1))).real())) /
((long double)hypot((((s) - (th1))).imag(), (((s) - (th1))).real()));
flag |= (l1 >= 0.25 - 1e-9 && l1 <= 4 + 1e-9 && l2 >= 0.25 - 1e-9 &&
l2 <= 4 + 1e-9);
}
ld a = (fabs((atan2((((f) - (com))).imag(), (((f) - (com))).real())) -
(atan2((((s) - (com))).imag(), (((s) - (com))).real()))) /
3.141592653589793238L * 180);
a = min(a, 360 - a);
puts((!flag || a < 1e-9 || a > 90 + 1e-9) ? "NO" : "YES");
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, c[105], d, i, j, n = 0, m = 0;
scanf("%lld", &a);
while (a--) {
scanf("%lld %lld", &b, &d);
n = sqrt(b);
m = 0;
for (i = 1; i <= n; i++) {
if (b % i == 0 && 1 <= i && i <= d) {
m = max(m, i);
j = b / i;
if (b % j == 0 && 1 <= j && j <= d) m = max(m, j);
}
}
printf("%lld\n", b / m);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const string H = "AHIMOTUVWXY";
bool Irm(char s) {
int i;
for (i = 0; i <= 10; i++)
if (s == H[i]) return true;
return false;
}
char S[100100];
int N, i, j;
int main() {
scanf("%s", S + 1);
N = strlen(S + 1);
for (i = 1, j = N; i < j; i++, j--)
if (S[i] != S[j]) {
printf("NO\n");
return 0;
}
for (i = 1; i <= N; i++)
if (!Irm(S[i])) {
printf("NO\n");
return 0;
}
printf("YES\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Woverflow"
using namespace std;
const short INF16 = ~(1 << 15);
const int INF32 = ~(1ll << 31);
const int SZ = 1e3 + 11;
const long long mod = 998244353;
const long long INF64 = 1e18;
const long long P = 53;
const long double eps = 1e-10;
const long double PI = acos(-1);
inline long long MOD(long long x) {
x %= mod, x += mod;
return x % mod;
}
inline long long MOD(long long x, long long m) {
x %= m, x += m;
return x % m;
}
inline long long T(long long x) { return x * (x + 1) >> 1; }
void Solve(int num_test) {
int n0, n1, n2;
cin >> n0 >> n1 >> n2;
string ans = "";
if (n1 == 0) {
for (int i = 0; i <= max(n0, n2); i++) ans.push_back(n0 ? '0' : '1');
} else if (n1 == 1) {
for (int i = 0; i <= n0; i++) ans.push_back('0');
for (int i = 0; i <= n2; i++) ans.push_back('1');
} else {
if (!n2 && !n0) {
for (int i = 0; i <= n1; i++) ans.push_back(i & 1 ? '0' : '1');
} else if (!n2) {
for (int i = 0; i <= n0; i++) ans.push_back('0');
for (int i = 0; i < n1; i++) ans.push_back(i & 1 ? '0' : '1');
} else if (!n0) {
for (int i = 0; i <= n2; i++) ans.push_back('1');
for (int i = 0; i < n1; i++) ans.push_back(i & 1 ? '1' : '0');
} else if (n1 & 1) {
for (int i = 0; i <= n0; i++) ans.push_back('0');
for (int i = 0; i < n1; i++) ans.push_back(i & 1 ? '0' : '1');
for (int i = 0; i < n2; i++) ans.push_back('1');
} else {
for (int i = 0; i <= n2; i++) ans.push_back('1');
for (int i = 0; i <= n0; i++) ans.push_back('0');
for (int i = 0; i < n1 - 1; i++) ans.push_back(i & 1 ? '0' : '1');
}
}
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed << setprecision(10);
srand(time(NULL));
int test = 1;
cin >> test;
for (int i = 1; i <= test; i++) Solve(i);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
const double eps = 1e-8;
double pi[maxn], sp[maxn];
bool _eq(double a, double b) { return (a > b - eps) && (a < b + eps); }
int main() {
int n;
double ans = 0;
cin >> n;
sp[0] = 1;
for (int i = 1; i <= n; i++) cin >> pi[i];
sort(&pi[1], &pi[n + 1], greater<double>());
if (_eq(pi[1], 1)) return printf("%.12lf\n", 1.0) & 0;
for (int i = 1; i <= n; i++) sp[i] = sp[i - 1] * (1 - pi[i]);
for (int i = 1; i <= n; i++) {
double tans = 0;
for (int j = 1; j <= i; j++) tans += pi[j] * sp[i] / (1 - pi[j]);
ans = max(ans, tans);
}
printf("%.12lf\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10;
int n, k;
int a, ans = 10000;
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
if (k % a == 0) ans = min(k / a, ans);
}
printf("%d\n", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
long long n;
std::cin >> n;
long long k = std::min((n + 1) / 2, 2LL);
long long p2 = 2;
long long p1 = 3;
while (p2 + p1 <= n) {
long long p3 = p2;
p2 = p1;
p1 = p3 + p2;
++k;
}
std::cout << k << std::endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > mush;
vector<pair<pair<long long, long long>, long long> > t;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
greater<pair<long long, long long> > >
q;
int main() {
int n, m;
long long x, h, l, r, v;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
cin >> x >> h >> l >> r;
t.push_back(make_pair(make_pair(x - h, x - 1ll), 100ll - l));
t.push_back(make_pair(make_pair(x + 1ll, x + h), 100ll - r));
}
for (int i = 0; i < m; ++i) {
cin >> x >> v;
mush.push_back(make_pair(x, v));
}
sort(t.begin(), t.end());
sort(mush.begin(), mush.end());
double p = 0, ans = 0;
int pos = 0, cnt = 0;
for (int i = 0; i < m; ++i) {
long long x = mush[i].first;
while (q.size() && q.top().first < x) {
pair<long long, long long> tt = q.top();
q.pop();
if (tt.second)
p -= log(tt.second) - log(100);
else
--cnt;
}
while (pos < n * 2 && t[pos].first.first <= x) {
if (t[pos].first.second >= x) {
if (t[pos].second) {
p += log(t[pos].second) - log(100);
} else
++cnt;
q.push(make_pair(t[pos].first.second, t[pos].second));
}
++pos;
}
if (!cnt) ans += mush[i].second * exp(p);
}
printf("%.10f\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10, inf = 1e10;
int n;
pair<int, int> v[300];
int main() {
cin >> n;
for (int(i) = 0; (i) < (n); (i)++) cin >> v[i].first >> v[i].second;
int ans = 0;
for (int(i) = 0; (i) < (n); (i)++) {
bool a, b, c, d;
a = b = c = d = false;
for (int(j) = 0; (j) < (n); (j)++)
if (i != j) {
int x = v[i].first, y = v[i].second, xx = v[j].first, yy = v[j].second;
if (xx > x && y == yy) a = 1;
if (xx < x && y == yy) b = 1;
if (x == xx && yy < y) c = 1;
if (x == xx && yy > y) d = 1;
}
ans += int(a && b && c && d);
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 100;
int t[maxn], d[maxn];
long long ans[maxn];
queue<pair<int, int> > que;
int n, cnt;
void solve() {
int pi = 1;
long long nxt = t[1];
for (;;) {
while (pi <= n && t[pi] < nxt) {
if (que.size() < cnt) {
que.push(make_pair(d[pi], pi));
} else {
ans[pi] = -1;
}
pi++;
}
if (!que.empty()) {
pair<int, int> cur = que.front();
que.pop();
nxt += cur.first;
ans[cur.second] = nxt;
} else if (pi <= n) {
nxt = t[pi] + d[pi];
ans[pi] = nxt;
pi++;
} else {
break;
}
}
for (int i = 1; i < n; i++) {
cout << ans[i] << " ";
}
cout << ans[n] << endl;
}
int main() {
while (cin >> n >> cnt) {
for (int i = 1; i <= n; i++) {
cin >> t[i] >> d[i];
}
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
long long int l, r;
cin >> l >> r;
if (r - l + 1 < 3) {
puts("-1");
return 0;
}
for (long long int i = l; i <= r; i++) {
for (long long int j = i + 1; j <= r; j++) {
for (long long int k = j + 1; k <= r; k++) {
if (gcd(i, j) == 1 && gcd(j, k) == 1 && gcd(i, k) != 1) {
cout << i << " " << j << " " << k << endl;
return 0;
}
}
}
}
puts("-1");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
string s, q;
int main() {
cin >> n >> s;
int i = 1;
q.push_back(s[0]);
while (i < s.length()) {
if (s[i] == q.back())
i++;
else {
q.push_back(s[i++]);
if (i < s.length()) q.push_back(s[i++]);
}
}
if (q.size() % 2) q.erase(q.end() - 1);
cout << s.size() - q.size() << endl;
cout << q;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int kek;
cin >> kek;
string s;
cin >> s;
int e = -1;
int d = -1;
long long ans = 0;
for (int i = 1; i < s.size(); ++i) {
if (s[i - 1] == s[i]) {
e = i - 1;
} else {
e = d;
d = i - 1;
}
if (e == -1) continue;
if (d == -1) {
ans += i;
continue;
}
if (i - e > 1) {
ans += e + 1;
continue;
}
ans += i - 1;
}
cout << ans << "\n";
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma G++ optimize(3)
using namespace std;
const int maxn = 100010;
string s;
int n, k;
multiset<pair<string, int> > st;
int main() {
cin >> s >> k;
n = s.size();
if (k > 1LL * n * (n + 1) / 2) {
cout << "No such line.";
return 0;
}
for (int i = 0; i < n; i++) st.insert({s.substr(i, 1), i});
while (--k) {
pair<string, int> x = *st.begin();
st.erase(st.begin());
if (x.second < n - 1) st.insert({x.first + s[++x.second], x.second});
}
cout << st.begin()->first;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long mat[100001], mat2[100001];
long long k, suma, a, b, s, c;
int n;
map<long long, long long> mapa;
long long ct;
int main() {
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> mat[i];
mapa[mat[i]]++;
}
sort(mat, mat + n);
suma = 1;
c = 1;
for (int i = 0; i < n; i++) {
if (i == 0) {
} else if (mat[i] == mat[i - 1]) {
suma += n;
continue;
}
ct = mapa[mat[i]];
if (suma + ct * n > k) {
a = mat[i];
s = k - suma;
b = mat[s / ct];
break;
}
suma += n;
}
cout << a << " " << b << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
char gb[65536], pb[65536], *gp = gb + 65536, *pp = pb, a[2][2005], s[2005];
int _5[65], n = 0, m = 0, f[2][2005][2005];
long long seed[2005];
inline char __getchar() {
if (gp == gb + 65536) fread(gb, 1, 65536, stdin), gp = gb;
return *gp++;
}
inline void _putchar(char c) {
if (pp == pb + 65536) fwrite(pb, 1, 65536, stdout), pp = pb;
*pp++ = c;
}
inline void putint(int x) {
if (!x) {
_putchar('0');
return;
}
register int _6;
for (_6 = 0; x; x /= 10) _5[++_6] = x % 10;
while (_6) _putchar(_5[_6--] ^ 48);
}
inline void flush() { fwrite(pb, 1, pp - pb, stdout); }
inline void add(int &x, int y) {
x += y;
if (x >= 1e9 + 7) x -= 1e9 + 7;
}
inline int gline(char *b) {
char c = __getchar();
int len = 0;
for (; !(((c)&64)); c = __getchar())
;
for (; ((c)&64); b[++len] = c, c = __getchar())
;
b[++len] = 0;
return len - 1;
}
inline void init() {
seed[0] = 1;
for (int i = 1; i <= 2005; i++) seed[i] = seed[i - 1] * 31 % 19260817;
}
inline void reverse(char *str, int len) {
if (!str) return;
for (register int i = 0; i < (len >> 1); i++) {
char temp = *(str + i);
*(str + i) = *(str + len - 1 - i);
*(str + len - 1 - i) = temp;
}
}
struct hash {
long long h[2005];
inline void make(int n, char *b) {
h[0] = 0;
for (register int i = 1; i <= n; i++)
h[i] = (h[i - 1] * 31 + b[i] - 'a') % 19260817;
}
inline void make_r(int n, char *b) {
h[0] = 0;
for (register int i = 1; i <= n; i++)
h[i] = (h[i - 1] * 31 + b[n + 1 - i] - 'a') % 19260817;
}
inline long long cut(int l, int r) {
return (h[r] + 19260817 - h[l - 1] * seed[r - l + 1] % 19260817) % 19260817;
}
} pre[2], suf[2], ss;
inline void __input() {
n = gline(a[0]);
pre[0].make(n, a[0]);
suf[0].make_r(n, a[0]);
n = gline(a[1]);
pre[1].make(n, a[1]);
suf[1].make_r(n, a[1]);
m = gline(s);
ss.make(m, s);
}
inline int solve(int flag) {
int ret = 0;
memset(f, 0, sizeof(f));
for (register int j = 1; j <= n; j++) {
f[0][j][0] = f[1][j][0] = 1;
for (register int i = 0; i < 2; i++)
for (int k = 2; k <= (((n - j + 1) < (m / 2)) ? (n - j + 1) : (m / 2));
k++)
if (ss.cut(m - 2 * k + 1, m - k) == pre[i].cut(j, j + k - 1) &&
ss.cut(m - k + 1, m) ==
suf[i ^ 1].cut(n - (j + k - 1) + 1, n - j + 1))
if (2 * k != m || flag) add(ret, f[i][j][m - 2 * k]);
for (register int i = 0; i < 2; i++)
for (int k = 2; k <= (((j) < (m / 2)) ? (j) : (m / 2)); k++)
if (ss.cut(k + 1, 2 * k) == pre[i].cut(j - k + 1, j) &&
ss.cut(1, k) == suf[i ^ 1].cut(n - j + 1, n - (j - k + 1) + 1))
if (2 * k != m || flag) add(f[i][j + 1][2 * k], 1);
for (register int i = 0; i < 2; i++)
for (int k = 0; k < m; k++)
if (a[i][j] == s[k + 1]) {
add(f[i][j + 1][k + 1], f[i][j][k]);
if (k + 2 <= m && a[i ^ 1][j] == s[k + 2])
add(f[i ^ 1][j + 1][k + 2], f[i][j][k]);
}
for (register int i = 0; i < 2; i++) add(ret, f[i][j + 1][m]);
}
return ret;
}
int main() {
init();
__input();
int register ans = 0;
add(ans, solve(1));
if (m > 1) {
reverse(s + 1, m);
ss.make(m, s);
add(ans, solve(0));
if (m == 2)
for (int j = 1; j <= n; j++)
for (int i = 0; i < 2; i++)
if (a[i][j] == s[1] && a[i ^ 1][j] == s[2]) add(ans, 1e9 + 7 - 1);
}
putint(ans);
flush();
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
string s;
bool ok(string t) {
int msk = 0;
for (int i = 0; i < int(t.size()); ++i) {
if (isupper(t[i])) msk |= 1;
if (islower(t[i])) msk |= 2;
if (isdigit(t[i])) msk |= 4;
}
return msk == 7;
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
cin >> s;
if (ok(s)) {
cout << s << endl;
continue;
}
bool fnd = false;
for (int i = 0; i < int(s.size()); ++i) {
string t = s;
t[i] = '1';
if (ok(t)) {
cout << t << endl;
fnd = true;
break;
}
t[i] = 'a';
if (ok(t)) {
cout << t << endl;
fnd = true;
break;
}
t[i] = 'A';
if (ok(t)) {
cout << t << endl;
fnd = true;
break;
}
}
if (fnd) continue;
if (isupper(s[2])) {
s[0] = 'a';
s[1] = '1';
cout << s << endl;
continue;
}
if (islower(s[2])) {
s[0] = 'A';
s[1] = '1';
cout << s << endl;
continue;
}
if (isdigit(s[2])) {
s[0] = 'a';
s[1] = 'A';
cout << s << endl;
continue;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int64_t inf = 1e17;
const int64_t MOD = 998244353;
int64_t inp() {
int64_t x;
cin >> x;
return x;
}
string inp_str() {
string x;
cin >> x;
return x;
}
int64_t sqr(int64_t n) { return n * n; }
int64_t power(int64_t a, int64_t b, int64_t mod = MOD) {
int64_t res = 1;
a %= mod;
for (int64_t i = (0); i <= (log2(b)); ++i) {
if (b & (1 << i)) res = (res * a) % mod;
a = (a * a) % mod;
}
return res;
}
int64_t gcd(int64_t a, int64_t b) {
while (b > 0) {
int64_t tmp = a % b;
a = b;
b = tmp;
}
return a;
}
bool is_pal(string s) { return equal((s).begin(), (s).end(), s.rbegin()); }
void fast_io() {
ios::sync_with_stdio(0);
cin.tie(0);
}
void file_io(string in, string out) {
freopen(in.c_str(), "r", stdin);
freopen(out.c_str(), "w", stdout);
}
int64_t d, m;
vector<int64_t> dp(32, 1);
vector<int64_t> pow2(32, 1);
void Input() {
d = inp(), m = inp();
dp[0] = 1;
pow2[0] = 1;
for (int64_t i = (1); i <= (dp.size() - 1); ++i) {
pow2[i] = (pow2[i - 1] * 2) % m;
dp[i] = (dp[i - 1]) * ((pow2[i] - pow2[i - 1] + 1) % m) % m;
}
}
void Solve() {
int64_t k = log2(d);
int64_t ans = (dp[k] * (d % m - pow2[k] % m + 2) % m - 1) % m;
if (ans < 0) ans += m;
cout << ans << "\n";
}
int main() {
fast_io();
int64_t t = inp();
while (t--) Input(), Solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int a[N];
int main() {
int n, m;
while (scanf("%d%d", &n, &m) == 2) {
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
for (int i = 0; i < m; i++) {
int x;
scanf("%d", &x);
int pos = upper_bound(a, a + n, x) - a;
printf("%d%c", pos, i == m - 1 ? '\n' : ' ');
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int n, m, ans;
struct node {
node *l, *r, *p, *m;
int s, os, v, i, id;
node() {}
node(int v, int s, int id)
: l(), r(), p(), m(this), i(), v(v), os(s), s(s), id(id) {}
} * nd[100005], *ev[300005];
bool is_root(node *x) { return x->p == NULL || x->p->l != x && x->p->r != x; }
node *max(node *x) { return x ? x->m : NULL; }
node *max(node *x, node *y) {
if (x == NULL) return y;
if (y == NULL) return x;
return x->v > y->v ? x : y;
}
bool left(node *x) { return x->p->l == x; }
int sum(node *x) { return x ? x->s : 0; }
void push_up(node *x) {
x->m = max(max(max(x->l), max(x->r)), x);
x->s = sum(x->l) + sum(x->r) + x->os;
return;
}
void push_down(node *x) {
if (x->i) {
swap(x->r, x->l);
if (x->r) x->r->i ^= 1;
if (x->l) x->l->i ^= 1;
x->i = 0;
}
return;
}
void down(node *x) {
if (!is_root(x)) down(x->p);
push_down(x);
}
void connect(node *x, node *y, bool l) {
(l ? x->l : x->r) = y;
if (y) y->p = x;
return;
}
void rot(node *x) {
node *y = x->p, *z = y->p;
bool l = left(x);
connect(y, l ? x->r : x->l, l);
if (!is_root(y))
connect(y->p, x, left(y));
else
x->p = z;
connect(x, y, !l);
push_up(y);
return;
}
void splay(node *x) {
down(x);
while (!is_root(x)) {
node *p = x->p;
if (!is_root(p)) rot(left(x) ^ left(p) ? x : p);
rot(x);
}
push_up(x);
return;
}
void expose(node *x) {
node *y = NULL;
for (node *z = x; z; z = z->p) {
splay(z);
z->os += sum(z->r);
z->os -= sum(y);
z->r = y;
y = z;
push_up(y);
}
splay(x);
return;
}
bool con(node *a, node *b) {
expose(a);
expose(b);
return a->p != NULL;
}
void evert(node *x) {
expose(x);
x->i ^= 1;
return;
}
int size(node *x) {
expose(x);
return sum(x);
}
void cut(node *x, node *y) {
evert(x);
expose(y);
y->l->p = NULL;
y->l = NULL;
}
void link(node *x, node *y) {
evert(x);
expose(y);
x->p = y;
y->os += sum(x);
return;
}
node *max_path(node *x, node *y) {
evert(x);
expose(y);
return y->m;
}
struct edge {
int a, b, c, id;
edge(int a, int b, int c, int id) : a(a), b(b), c(c), id(id) {}
bool operator<(const edge &a) const { return c != a.c ? c > a.c : id > a.id; }
};
int O;
set<edge> s;
vector<edge> ed;
bool delete_edge(edge x) {
cut(nd[x.a], ev[x.id]);
cut(nd[x.b], ev[x.id]);
if (size(nd[x.a]) & 1 && size(nd[x.b]) & 1) O += 2;
return !(size(nd[x.a]) & 1);
}
void add_edge(edge x) {
if (size(nd[x.a]) & 1 && size(nd[x.b]) & 1) O -= 2;
link(nd[x.a], ev[x.id]);
link(nd[x.b], ev[x.id]);
return;
}
void new_edge(int a, int b, int c) {
if (c >= ans) return;
int id = ed.size();
ev[id] = new node(c, 0, id);
edge e = edge(a, b, c, id);
ed.push_back(e);
if (con(nd[a], nd[b])) {
node *m = max_path(nd[a], nd[b]);
if (m->v <= c) return;
delete_edge(ed[m->id]);
s.erase(ed[m->id]);
}
add_edge(e);
s.insert(e);
return;
}
int query() {
if (O) return -1;
while (delete_edge(*s.begin())) {
s.erase(s.begin());
}
add_edge(*s.begin());
return s.begin()->c;
}
int main() {
scanf("%d %d", &n, &m);
int x, y, z;
ans = inf;
O = n;
for (int i = 1; i <= n; i++) nd[i] = new node(0, 1, 0);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &x, &y, &z);
new_edge(x, y, z);
printf("%d\n", ans = query());
if (ans < 0) ans = inf;
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
bool f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(48 + x % 10);
}
inline void writeln(int x) {
write(x);
putchar('\n');
}
inline void writebl(int x) {
write(x);
putchar(' ');
}
const int maxn = 2e5 + 5;
struct edge {
int v, w, next;
} e[maxn];
int head[maxn], ecnt;
int color[maxn], out[maxn];
int tot1, tot2, n, ans = -1, want;
queue<int> q;
void dfs(int x) {
if (color[x])
++tot1;
else
++tot2;
q.push(x);
for (register int i = head[x]; i; i = e[i].next) {
int v = e[i].v, w = color[x] ^ want ^ e[i].w;
if (color[v] < 0) {
color[v] = w;
dfs(v);
if (tot1 < 0) return;
} else if (color[v] != w) {
tot1 = -1;
return;
}
}
}
inline void solve(int op) {
want = op;
int res = 0;
memset(color, -1, sizeof(color));
for (register int i = 1; i <= n; ++i)
if (color[i] < 0) {
while (q.size()) q.pop();
tot1 = tot2 = 0;
color[i] = 1;
dfs(i);
if (tot1 < 0) {
res = -1;
break;
}
res += tot1 < tot2 ? tot1 : tot2;
register bool t = tot1 > tot2;
while (q.size()) color[q.front()] ^= t, q.pop();
}
if ((ans == -1 || ans > res) && res != -1) {
for (register int i = 1; i <= n; ++i) out[i] = color[i];
ans = res;
}
}
int main() {
n = read();
int m = read();
for (register int i = 1; i <= m; ++i) {
int u = read(), v = read(), w;
char ch = getchar();
while (ch != 'R' && ch != 'B') ch = getchar();
w = ch == 'R';
e[++ecnt] = (edge){v, w, head[u]}, head[u] = ecnt;
e[++ecnt] = (edge){u, w, head[v]}, head[v] = ecnt;
}
solve(0);
solve(1);
writeln(ans);
if (ans != -1)
for (register int i = 1; i <= n; ++i)
if (out[i]) writebl(i);
}
| 7 |
#include "bits/stdc++.h"
using namespace std;
#define forn(i, n) for(int i = 0; i < (n); ++i)
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef double dbl;
const int MAX_N = 1e5 + 100;
int a[MAX_N], b[MAX_N];
map<int, int> cnt[2];
int main(){
#ifdef LOCAL
assert(freopen("test.in", "r", stdin));
assert(freopen("test.out", "w", stdout));
#endif
int TestCnt;
scanf("%d", &TestCnt);
forn(testnum, TestCnt){
forn(k, 2)
cnt[k].clear();
int N;
scanf("%d", &N);
forn(i, N)
scanf("%d", &a[i]), b[i] = a[i];
sort(b, b + N);
forn(i, N)
++cnt[i % 2][b[i]];
bool ok = 1;
forn(i, N){
int &x = cnt[i % 2][a[i]];
if(!x){
ok = 0;
break;
}
--x;
}
puts(ok ? "YES" : "NO");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> factors(int x) {
vector<int> f;
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) {
f.push_back(i);
if (i * i != x) {
f.push_back(x / i);
}
}
}
return f;
}
int main() {
ios_base::sync_with_stdio(false);
int p, y;
cin >> p >> y;
for (int i = int(y); i >= int(p + 1); i--) {
auto f = factors(i);
bool ok = true;
for (auto &each : f) {
if (each <= p) {
ok = false;
break;
}
}
if (ok) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, m;
cin >> n >> k >> m;
int val;
bool flag = false;
multiset<int> ele;
unordered_map<int, int> hash;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
hash[a % m]++;
ele.insert(a);
if (hash[a % m] == k) {
val = a % m;
flag = true;
break;
}
}
if (flag) {
cout << "Yes" << endl;
multiset<int>::iterator it = ele.begin();
while (it != ele.end()) {
if (*it % m == val) cout << *it << " ";
it++;
}
cout << endl;
} else
cout << "No" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<pair<int, int>> arr;
pair<int, int> x, y;
int xmax = INT_MIN, xmin = INT_MAX, ymax = INT_MIN, ymin = INT_MAX;
for (int i = 0; i < n; i++) {
cin >> x.first >> x.second;
xmax = max(xmax, x.first);
xmin = min(xmin, x.first);
ymax = max(ymax, x.second);
ymin = min(ymin, x.second);
arr.push_back(x);
}
int ans = 0;
for (int i = 0; i < n; i++) {
x = arr[i];
ans = max(ans, abs(xmax - x.first) + abs(ymax - x.second));
ans = max(ans, abs(xmax - x.first) + abs(ymin - x.second));
ans = max(ans, abs(xmin - x.first) + abs(ymax - x.second));
ans = max(ans, abs(xmin - x.first) + abs(ymin - x.second));
}
cout << ans * 2 << " ";
for (int i = 4; i <= n; i++)
cout << (2 * ((ymax - ymin) + (xmax - xmin))) << " ";
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[100][10000];
int kl[100][10000];
int kr[100][10000];
int main() {
char c;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
bool g = 0;
for (int j = 0; j < m; j++) {
while (1) {
scanf("%c", &c);
if (c == '0' || c == '1') break;
}
a[i][j] = c;
if (a[i][j] == '1') {
g = 1;
}
}
if (!g) {
cout << -1 << endl;
return 0;
}
}
for (int i = 0; i < n; i++) {
int rr = 2000000000;
for (int j = m - 1; j >= 0; j--) {
if (a[i][j] == '1') {
rr = j;
}
kr[i][j] = (rr - j);
}
for (int j = m - 1; j >= 0; j--) {
if (a[i][j] == '1') {
break;
}
kr[i][j] = min(kr[i][j], m - j + rr);
}
}
for (int i = 0; i < n; i++) {
int ll = -2000000000;
for (int j = 0; j < m; j++) {
if (a[i][j] == '1') {
ll = j;
}
kl[i][j] = (j - ll);
}
for (int j = 0; j < m; j++) {
if (a[i][j] == '1') {
break;
}
kl[i][j] = min(kl[i][j], m - ll + j);
}
}
int best = 2000000000;
for (int i = 0; i < m; i++) {
int sum = 0;
for (int p = 0; p < n; p++) {
sum += min(kl[p][i], kr[p][i]);
}
best = min(best, sum);
}
printf("%d\n", best);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y;
node(int x1 = 0, int y1 = 0) : x(x1), y(y1){};
bool operator=(const node &b) const { return (x == b.x) && (y == b.y); }
};
const int dx[5] = {0, 0, 0, 1, -1};
const int dy[5] = {0, 1, -1, 0, 0};
int N, M, Q;
char maze[1007][1007];
int ans = 0;
int vis[1007][1007];
int memo[1000007];
void dfs(int x, int y, int id) {
if (maze[x][y] == '*') return;
for (int i = 1; i <= 4; i++) {
int x1 = x + dx[i];
int y1 = y + dy[i];
if (x1 > 0 && x1 <= N && y1 > 0 && y1 <= M && maze[x1][y1] == '*') ans++;
}
for (int i = 1; i <= 4; i++) {
int x1 = x + dx[i];
int y1 = y + dy[i];
if (maze[x1][y1] == '.' && vis[x1][y1] == 0 && x1 > 0 && x1 <= N &&
y1 > 0 && y1 <= M) {
vis[x1][y1] = id;
dfs(x1, y1, id);
}
}
return;
}
int main() {
scanf("%d%d%d", &N, &M, &Q);
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
cin >> maze[i][j];
}
}
int k = 1;
while (Q--) {
int x, y;
scanf("%d%d", &x, &y);
if (vis[x][y] == 0) {
ans = 0;
vis[x][y] = k;
dfs(x, y, k);
memo[k] = ans;
k++;
}
printf("%d\n", memo[vis[x][y]]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
string s;
cin >> s;
long long int cnt = 0;
vector<long long int> V;
for (long long int i = 0; i < s.size(); i++) {
if (s[i] == '1')
cnt++;
else {
if (cnt > 0) V.push_back(cnt);
cnt = 0;
}
}
if (cnt > 0) V.push_back(cnt);
sort(V.begin(), V.end());
reverse(V.begin(), V.end());
long long int res = 0;
for (long long int i = 0; i < V.size(); i += 2) res += V[i];
cout << res << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long int end = pow(2, n);
long long int start = 2;
long long int globalSum = 2 * end - 2;
long long int sum = 0;
end /= 2;
int length = n / 2;
int i = 0;
while (i < length) {
sum += end;
end /= 2;
i++;
}
long long int a = sum;
long long int b = globalSum - a;
cout << abs(a - b) << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1000000007;
long long t[(1 << 20)], z[(1 << 20)];
long long n, r, k;
bool ok(long long d) {
for (int i = 0; i < n; ++i) z[i] = 0;
long long asd = 0, c = 0;
for (int i = 0; i < r; ++i) asd += t[i];
for (int i = 0; i < n; ++i) {
if (i - r > 0) asd -= t[i - r - 1] + z[i - r - 1];
if (i + r < n) asd += t[i + r];
long long h = max(0LL, d - asd);
asd += h;
c += h;
if (c > k) return 0;
z[i + r] += h;
}
return c <= k;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> r >> k;
for (int i = 0; i < n; ++i) cin >> t[i];
long long V = -1;
for (long long b = 1e17; b >= 1; b /= 2) {
while (ok(V + b)) V += b;
}
cout << V;
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define ps(x,y) fixed<<setprecision(y)<<x
typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(ld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.fi); cerr << ","; _print(p.se); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
const int MOD = 1e9+7;
const ld PI = acos((ld)-1);
void solve() {
int n;
cin >> n;
int cnt = 0;
for (int i = 0;i < 2*n;++i) {
int x;
cin >> x;
if (x&1) ++cnt;
else --cnt;
}
if (!cnt) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
int main () {
#ifndef ONLINE_JUDGE
freopen("input1.txt","r",stdin);
freopen("output1.txt","w",stdout);
freopen("Error.txt", "w", stderr);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
} | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.