solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000 + 7;
const long long LINF = INF * (long long)INF;
const int mod = 998244353;
const int MAX = 2 * 1000 * 100 + 5;
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int mult(int a, int b) { return a * (long long)b % mod; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void Add(int &a, int b) { a = add(a, b); }
int modPow(int a, int step) {
int ans = 1;
while (step) {
if (step & 1) ans = mult(ans, a);
a = mult(a, a);
step >>= 1;
}
return ans;
}
int dp[2][2005][2005];
int ans[2005];
int main() {
ios_base::sync_with_stdio(0);
string s;
cin >> s;
dp[0][0][0] = dp[1][0][0] = 1;
char c1 = '(', c2 = ')';
for (int it = 0; it < 2; it++) {
for (int i = 0; i < (int)(s.size()); i++) {
for (int j = 0; j <= i; j++) {
if (s[i] == c1) {
Add(dp[it][i + 1][j + 1], dp[it][i][j]);
} else if (s[i] == c2) {
Add(dp[it][i + 1][j], dp[it][i][j]);
} else {
Add(dp[it][i + 1][j + 1], dp[it][i][j]);
Add(dp[it][i + 1][j], dp[it][i][j]);
}
}
}
swap(c1, c2);
reverse(s.begin(), s.end());
}
for (int i = 0; i < (int)(s.size()); i++)
for (int j = (int)(s.size()) - 1; j >= 0; j--)
Add(dp[1][i][j], dp[1][i][j + 1]);
for (int i = 0; i < (int)(s.size()); i++) {
if (s[i] == ')') continue;
for (int j = 0; j <= (int)(s.size()); j++)
Add(ans[j + 1],
mult(dp[0][i][j], dp[1][(int)(s.size()) - (i + 1)][j + 1]));
}
int res = 0;
for (int i = (int)(s.size()); i > 0; i--)
res = add(res, mult(i, sub(ans[i], ans[i + 1])));
cout << res;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
void file(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
template <typename Tp>
void read(Tp &x) {
int fh = 1;
char c = getchar();
x = 0;
while (c > '9' || c < '0') {
if (c == '-') {
fh = -1;
}
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c & 15);
c = getchar();
}
x *= fh;
}
struct Edge {
int f, t, w, nxt;
} edge[2000005];
int head[1000005], etot = 1;
void add_edge(int f, int t, int w = 0) {
edge[++etot] = (Edge){f, t, w, head[f]};
head[f] = etot;
}
int n, m;
int pf[1000005], ss[1000005], ans[1000005];
int fa[1000005], son[1000005], siz[1000005], dep[1000005], tp[1000005];
void dfs1(int x, int pa) {
fa[x] = pa;
siz[x] = 1;
dep[x] = dep[pa] + 1;
for (int i = head[x]; i; i = edge[i].nxt) {
int y = edge[i].t;
if (y == pa) continue;
pf[y] = (i >> 1);
dfs1(y, x);
siz[x] += siz[y];
if (siz[son[x]] < siz[y]) son[x] = y;
}
}
void dfs2(int x, int tpf) {
tp[x] = tpf;
if (son[x]) dfs2(son[x], tpf);
for (int i = head[x]; i; i = edge[i].nxt) {
int y = edge[i].t;
if (y == fa[x] || y == son[x]) continue;
dfs2(y, y);
}
}
void dfs3(int x) {
for (int i = head[x]; i; i = edge[i].nxt) {
int y = edge[i].t;
if (y == fa[x]) continue;
dfs3(y);
ss[x] += ss[y];
}
}
int lca(int x, int y) {
while (tp[x] != tp[y]) {
if (dep[tp[x]] < dep[tp[y]]) swap(x, y);
x = fa[tp[x]];
}
if (dep[x] > dep[y]) swap(x, y);
return x;
}
signed main() {
read(n);
for (int i = 1, x, y; i < n; i++) {
read(x);
read(y);
add_edge(x, y);
add_edge(y, x);
}
dfs1(1, 0);
dfs2(1, 1);
read(m);
for (int i = 1, x, y; i <= m; i++) {
read(x);
read(y);
int lc = lca(x, y);
ss[x]++;
ss[y]++;
ss[lc] -= 2;
}
dfs3(1);
for (int i = 2; i <= n; i++) ans[pf[i]] = ss[i];
for (int i = 1; i < n; i++) printf("%d ", ans[i]);
puts("");
fclose(stdin);
fclose(stdout);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2010;
char P[MAX][MAX];
int top_down[MAX][MAX], bottom_up[MAX][MAX];
int left_right[MAX][MAX], right_left[MAX][MAX];
int main() {
int n, m;
while (scanf("%d%d", &n, &m) > 0) {
for (int i = 0; i < n; i++) {
scanf("%s", P[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
top_down[i][j] = 1;
if (i and P[i][j] == P[i - 1][j]) {
top_down[i][j] += top_down[i - 1][j];
}
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < m; j++) {
bottom_up[i][j] = 1;
if (i + 1 < n and P[i][j] == P[i + 1][j]) {
bottom_up[i][j] += bottom_up[i + 1][j];
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j == 0 or P[i][j] != P[i][j - 1]) {
left_right[i][j] = 1;
continue;
}
if (min(top_down[i][j], bottom_up[i][j]) > left_right[i][j - 1]) {
left_right[i][j] = left_right[i][j - 1] + 1;
} else {
left_right[i][j] = min(top_down[i][j], bottom_up[i][j]);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = m - 1; j >= 0; j--) {
if (j + 1 == m or P[i][j] != P[i][j + 1]) {
right_left[i][j] = 1;
continue;
}
if (min(top_down[i][j], bottom_up[i][j]) > right_left[i][j + 1]) {
right_left[i][j] = right_left[i][j + 1] + 1;
} else {
right_left[i][j] = min(top_down[i][j], bottom_up[i][j]);
}
}
}
long long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans += min(left_right[i][j], right_left[i][j]);
}
}
printf("%lld\n", ans);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
set<int> v[3];
int n, x;
int solve(int x, int n) {
if (x < 10) {
for (int i = 0; i < n; i++)
if (v[i].count(x)) return 1;
} else if (x < 100) {
int a = x % 10;
x /= 10;
if (n == 2) {
if (v[0].count(a) && v[1].count(x)) return 1;
if (v[0].count(x) && v[1].count(a)) return 1;
} else {
if (v[0].count(a) && v[1].count(x)) return 1;
if (v[0].count(x) && v[1].count(a)) return 1;
if (v[0].count(a) && v[2].count(x)) return 1;
if (v[0].count(x) && v[2].count(a)) return 1;
if (v[2].count(a) && v[1].count(x)) return 1;
if (v[2].count(x) && v[1].count(a)) return 1;
}
} else if (x < 1000) {
int a = x % 10, b = (x / 10) % 10;
x = x / 100;
if (v[0].count(a) && v[1].count(b) && v[2].count(x)) return 1;
if (v[0].count(a) && v[2].count(b) && v[1].count(x)) return 1;
if (v[1].count(a) && v[0].count(b) && v[2].count(x)) return 1;
if (v[1].count(a) && v[2].count(b) && v[0].count(x)) return 1;
if (v[2].count(a) && v[0].count(b) && v[1].count(x)) return 1;
if (v[0].count(a) && v[1].count(b) && v[0].count(x)) return 1;
}
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 6; j++) {
scanf("%d", &x);
v[i].insert(x);
}
}
int l = 1, r = 1, k = 0;
for (int i = 0; i < n; i++) {
r *= 10;
for (int j = l; j < r; j++) {
if (solve(j, n))
k++;
else
goto eg;
}
l = r;
}
eg:
printf("%d\n", k);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100000 + 10;
const long long mode = 1000000000 + 9;
long long par[maxn];
long long h[maxn];
long long k;
long long getPar(long long x) {
if (par[x] == x) return x;
par[x] = getPar(par[x]);
return par[x];
}
void mergeVert(long long fi, long long se) {
if (h[fi] == h[se]) {
h[fi]++;
par[se] = fi;
} else if (h[fi] > h[se])
par[se] = fi;
else
par[fi] = se;
}
int main() {
ios_base::sync_with_stdio(false);
long long n, m;
cin >> n >> m;
for (long long i = 1; i <= n; i++) par[i] = i;
long long ans = 1;
for (long long i = 1; i <= m; i++) {
long long fi, se;
cin >> fi >> se;
if (getPar(fi) != getPar(se)) {
mergeVert(getPar(fi), getPar(se));
} else {
ans *= 2;
ans %= mode;
}
if (ans == 0)
cout << mode - 1 << endl;
else
cout << ans - 1 << endl;
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
struct TrieNode {
TrieNode *arr[2];
TrieNode() { arr[0] = arr[1] = nullptr; }
};
void insert(TrieNode *root, int num) {
TrieNode *curr = root;
for (int i = 20; i >= 0; i--) {
if (curr->arr[(num >> i) & 1] == nullptr)
curr->arr[(num >> i) & 1] = new TrieNode();
curr = curr->arr[(num >> i) & 1];
}
return;
}
int query(TrieNode *root, int xr) {
int ans = 0;
TrieNode *curr = root;
for (int i = 20; i >= 0; i--) {
if (curr->arr[(xr >> i) & 1] == nullptr) {
ans |= (1 << i);
curr = curr->arr[((xr >> i) & 1) ^ 1];
} else {
curr = curr->arr[(xr >> i) & 1];
}
}
return ans;
}
const int N = 1 << 20 + 1;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
bool present[N];
memset(present, false, sizeof(present));
vector<int> a(n);
for (int &i : a) cin >> i, present[i] = true;
TrieNode *root = new TrieNode();
for (int i = 0; i < N; i++)
if (!present[i]) insert(root, i);
int curr_xor = 0;
while (m--) {
int x;
cin >> x;
curr_xor ^= x;
cout << query(root, curr_xor) << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << ' ' << H;
debug_out(T...);
}
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const int N = 1e5 + 5;
int H[N], HD[N], P[N], S[N], ST[N], FN[N], W[N], C[N], ts;
vector<int> G[N], Q[N];
pair<long long, int> SEG[N << 2];
long long LAZY[N << 2];
int DFSZ(int v) {
S[v] = 1;
for (int u : G[v])
if (u != P[v]) H[u] = H[v] + 1, P[u] = v, S[v] += DFSZ(u);
return S[v];
}
void DFSHLD(int v) {
ST[v] = ts;
for (int c : Q[v]) W[ts++] = c;
int b = 0;
for (int u : G[v])
if (u != P[v] && S[u] > S[b]) b = u;
if (b) HD[b] = HD[v], DFSHLD(b);
for (int u : G[v])
if (u != P[v] && u != b) HD[u] = u, DFSHLD(u);
FN[v] = ts;
}
pair<long long, int> min(pair<long long, int> a, pair<long long, int> b) {
if (a.first < b.first)
return a;
else if (a.first > b.first)
return b;
else if (C[W[a.second]] < C[W[b.second]])
return a;
return b;
}
void Apply(int id, long long x) {
if (x == 0)
SEG[id] = make_pair(INF, -1), LAZY[id] = 0;
else
SEG[id].first += x, LAZY[id] += x;
}
void Shift(int id) {
if (LAZY[id] == 0) return;
Apply(id << 1, LAZY[id]);
Apply(id << 1 | 1, LAZY[id]);
LAZY[id] = 0;
}
void Build(int id = 1, int s = 0, int e = ts) {
if (e - s < 2) {
SEG[id] = make_pair(W[s], s);
return;
}
int md = (s + e) >> 1;
Build(id << 1, s, md);
Build(id << 1 | 1, md, e);
SEG[id] = min(SEG[id << 1], SEG[id << 1 | 1]);
}
void Update(int l, int r, int x, int id = 1, int s = 0, int e = ts) {
if (l >= e || s >= r) return;
if (l <= s && e <= r) return Apply(id, x);
Shift(id);
int md = (s + e) >> 1;
Update(l, r, x, id << 1, s, md);
Update(l, r, x, id << 1 | 1, md, e);
SEG[id] = min(SEG[id << 1], SEG[id << 1 | 1]);
}
pair<long long, int> Get(int l, int r, int id = 1, int s = 0, int e = ts) {
if (l >= e || s >= r) return make_pair(INF, -1);
if (l <= s && e <= r) return SEG[id];
Shift(id);
int md = (s + e) >> 1;
return min(Get(l, r, id << 1, s, md), Get(l, r, id << 1 | 1, md, e));
}
pair<long long, int> GetMn(int v, int u) {
pair<long long, int> res = make_pair(INF, -1);
while (HD[v] != HD[u]) {
if (H[HD[v]] < H[HD[u]]) swap(v, u);
res = min(res, Get(ST[HD[v]], ST[v] + (int)(Q[v].size())));
v = P[HD[v]];
}
if (H[v] < H[u]) swap(v, u);
res = min(res, Get(ST[u], ST[v] + (int)(Q[v].size())));
return res;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
G[v].push_back(u);
G[u].push_back(v);
}
for (int i = 1; i <= m; i++) cin >> C[i], Q[C[i]].push_back(i);
DFSZ(1);
HD[1] = 1;
DFSHLD(1);
Build();
while (q--) {
int t;
cin >> t;
if (t == 1) {
int v, u, k;
cin >> v >> u >> k;
vector<int> res;
while (k--) {
int x = GetMn(v, u).second;
if (x == -1) break;
res.push_back(W[x]);
Update(x, x + 1, 0);
}
cout << (int)(res.size()) << ' ';
for (int x : res) cout << x << ' ';
cout << '\n';
} else {
int v, k;
cin >> v >> k;
Update(ST[v], FN[v], k);
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,ssse3")
using namespace std;
const int mn = 3003;
const long long INF = 1e15;
int sub[mn], b[mn], w[mn], n;
pair<int, long long> dp[mn][mn], dp2[mn];
vector<int> edges[mn];
pair<int, long long> add(pair<int, long long> a, pair<int, long long> b) {
return {a.first + b.first, a.second + b.second};
}
void dfs(int c, int p) {
sub[c] = 1;
dp[c][0] = {0, w[c] - b[c]};
for (int i : edges[c])
if (i != p) {
dfs(i, c);
sub[c] += sub[i];
for (int j = 0; j < sub[c]; ++j) dp2[j] = {0, -INF};
for (int j = sub[c] - 1; j + 1; --j)
for (int k = max(0, sub[i] + j - sub[c] - 1); k <= min(sub[i], j);
++k) {
if (j + 1 <= sub[c]) {
if (dp[i][k].second > 0)
dp2[j + 1] =
max(dp2[j + 1], {dp[c][j - k].first + dp[i][k].first + 1,
dp[c][j - k].second});
else
dp2[j + 1] = max(dp2[j + 1], {dp[c][j - k].first + dp[i][k].first,
dp[c][j - k].second});
}
dp2[j] = max(dp2[j], add(dp[i][k], dp[c][j - k]));
}
for (int j = 0; j < sub[c]; ++j) dp[c][j] = dp2[j];
}
}
void solve() {
int m, x, y;
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> b[i];
for (int i = 1; i <= n; ++i) {
cin >> w[i];
edges[i].clear();
}
for (int i = 1; i < n; ++i) {
cin >> x >> y;
edges[x].push_back(y);
edges[y].push_back(x);
}
for (int i = 1; i <= n; ++i)
for (int j = 0; j <= n; ++j) dp[i][j] = {0, -INF};
dfs(1, 0);
if (dp[1][m - 1].second > 0)
cout << dp[1][m - 1].first + 1 << "\n";
else
cout << dp[1][m - 1].first << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using VI = vector<int>;
using VL = vector<ll>;
using VVI = vector<vector<int>>;
using VVL = vector<vector<ll>>;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
const double PI = acos(-1.0);
namespace Backlight {
const int __BUFFER_SIZE__ = 1 << 20;
bool NEOF = 1;
int __top;
char __buf[__BUFFER_SIZE__], *__p1 = __buf, *__p2 = __buf, __stk[996];
template <typename T>
T MIN(T a, T b) {
return min(a, b);
}
template <typename First, typename... Rest>
First MIN(First f, Rest... r) {
return min(f, MIN(r...));
}
template <typename T>
T MAX(T a, T b) {
return max(a, b);
}
template <typename First, typename... Rest>
First MAX(First f, Rest... r) {
return max(f, MAX(r...));
}
template <typename T>
void updMin(T& a, T b) {
if (a > b) a = b;
}
template <typename T>
void updMax(T& a, T b) {
if (a < b) a = b;
}
inline char nc() {
return __p1 == __p2 && NEOF &&
(__p2 =
(__p1 = __buf) + fread(__buf, 1, __BUFFER_SIZE__, stdin),
__p1 == __p2)
? (NEOF = 0, EOF)
: *__p1++;
}
template <typename T>
inline bool read(T& x) {
char c = nc();
bool f = 0;
x = 0;
while (!isdigit(c)) c == '-' && (f = 1), c = nc();
while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = nc();
if (f) x = -x;
return NEOF;
}
inline bool need(char c) { return (c != '\n') && (c != ' '); }
inline bool read(char& a) {
while ((a = nc()) && need(a) && NEOF)
;
return NEOF;
}
inline bool read(char* a) {
while ((*a = nc()) && need(*a) && NEOF) ++a;
*a = '\0';
return NEOF;
}
inline bool read(double& x) {
bool f = 0;
char c = nc();
x = 0;
while (!isdigit(c)) {
f |= (c == '-');
c = nc();
}
while (isdigit(c)) {
x = x * 10.0 + (c ^ 48);
c = nc();
}
if (c == '.') {
double temp = 1;
c = nc();
while (isdigit(c)) {
temp = temp / 10.0;
x = x + temp * (c ^ 48);
c = nc();
}
}
if (f) x = -x;
return NEOF;
}
template <typename First, typename... Rest>
inline bool read(First& f, Rest&... r) {
read(f);
return read(r...);
}
template <typename T>
inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x == 0) {
putchar('0');
return;
}
__top = 0;
while (x) {
__stk[++__top] = x % 10 + '0';
x /= 10;
}
while (__top) {
putchar(__stk[__top]);
--__top;
}
}
template <typename First, typename... Rest>
inline void print(First f, Rest... r) {
print(f);
putchar(' ');
print(r...);
}
template <typename T>
inline void println(T x) {
print(x);
putchar('\n');
}
template <typename First, typename... Rest>
inline void println(First f, Rest... r) {
print(f);
putchar(' ');
println(r...);
}
template <typename T>
inline void _dbg(const char* format, T value) {
cerr << format << '=' << value << endl;
}
template <typename First, typename... Rest>
inline void _dbg(const char* format, First f, Rest... r) {
while (*format != ',') cerr << *format++;
cerr << '=' << f << ", ";
_dbg(format + 1, r...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> V) {
os << "[ ";
for (auto v : V) os << v << ",";
return os << " ]";
}
template <typename T>
ostream& operator<<(ostream& os, set<T> V) {
os << "[ ";
for (auto v : V) os << v << ",";
return os << " ]";
}
template <typename T>
ostream& operator<<(ostream& os, multiset<T> V) {
os << "[ ";
for (auto v : V) os << v << ",";
return os << " ]";
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, map<T1, T2> V) {
os << "[ ";
for (auto v : V) os << v << ",";
return os << " ]";
}
template <typename L, typename R>
ostream& operator<<(ostream& os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
} // namespace Backlight
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return l + rng() % (r - l + 1); }
using namespace Backlight;
const int N = 3e5 + 5;
const int M = 3e6 + 5;
const int K = 1e7 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a;
swap(a, m);
u -= t * v;
swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod())
v = static_cast<Type>(x);
else
v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const {
return static_cast<U>(value);
}
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) {
if ((value += other.value) >= mod()) value -= mod();
return *this;
}
Modular& operator-=(const Modular& other) {
if ((value -= other.value) < 0) value += mod();
return *this;
}
template <typename U>
Modular& operator+=(const U& other) {
return *this += Modular(other);
}
template <typename U>
Modular& operator-=(const U& other) {
return *this -= Modular(other);
}
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) {
Modular result(*this);
*this += 1;
return result;
}
Modular operator--(int) {
Modular result(*this);
*this -= 1;
return result;
}
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value,
Modular>::type&
operator*=(const Modular& rhs) {
value = normalize(static_cast<int64_t>(value) *
static_cast<int64_t>(rhs.value));
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, long long>::value,
Modular>::type&
operator*=(const Modular& rhs) {
long long q = static_cast<long long>(static_cast<long double>(value) *
rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value,
Modular>::type&
operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) {
return *this *= Modular(inverse(other.value, mod()));
}
friend const Type& abs(const Modular& x) { return x.value; }
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename V, typename U>
friend V& operator>>(V& stream, Modular<U>& number);
private:
Type value;
};
template <typename T>
bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) {
return lhs.value == rhs.value;
}
template <typename T, typename U>
bool operator==(const Modular<T>& lhs, U rhs) {
return lhs == Modular<T>(rhs);
}
template <typename T, typename U>
bool operator==(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) == rhs;
}
template <typename T>
bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) {
return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(const Modular<T>& lhs, U rhs) {
return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(U lhs, const Modular<T>& rhs) {
return !(lhs == rhs);
}
template <typename T>
bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) {
return lhs.value < rhs.value;
}
template <typename T>
Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T>
Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T>
Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T>
Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
assert(b >= 0);
Modular<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
bool IsZero(const Modular<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
return stream << number();
}
template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, long long>::type x;
stream >> x;
number.value = Modular<T>::normalize(x);
return stream;
}
const int md = 998244353;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
vector<Mint> fact(1, 1);
vector<Mint> inv_fact(1, 1);
Mint nCk(int n, int k) {
if (k < 0 || k > n) {
return 0;
}
while ((int)fact.size() < n + 1) {
fact.push_back(fact.back() * (int)fact.size());
inv_fact.push_back(1 / fact.back());
}
return fact[n] * inv_fact[k] * inv_fact[n - k];
}
int n, k, L[N], R[N], m, t[N << 1];
void solve(int Case) {
read(n, k);
m = 0;
for (int i = (1); i <= (n); ++i) {
read(L[i], R[i]);
t[++m] = L[i];
t[++m] = R[i];
}
sort(t + 1, t + 1 + m);
m = unique(t + 1, t + 1 + m) - t - 1;
vector<PII> op;
for (int i = (1); i <= (n); ++i) {
L[i] = lower_bound(t + 1, t + 1 + m, L[i]) - t;
R[i] = lower_bound(t + 1, t + 1 + m, R[i]) - t;
op.emplace_back(L[i], 1);
op.emplace_back(R[i] + 1, -1);
}
sort((op).begin(), (op).end());
Mint ans(0);
int cnt = 0;
for (PII p : op) {
if (p.second == -1 && cnt >= k) ans += nCk(cnt - 1, k - 1);
cnt += p.second;
;
}
cout << ans << endl;
}
int main() {
int T = 1;
for (int _ = 1; _ <= T; _++) solve(_);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m, s, f, t, k;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> s >> f >> t;
if (s == f)
cout << t << endl;
else if (s < f) {
if (t < s - 1)
k = 0;
else {
k = (t - (s - 1)) / (2 * (m - 1));
if ((t - (s - 1)) % (2 * (m - 1))) ++k;
}
cout << (s - 1) + 2 * (m - 1) * k + f - s << endl;
} else {
if (t < 2 * (m - 1) - (s - 1))
k = 0;
else {
k = (t - 2 * (m - 1) + (s - 1)) / (2 * (m - 1));
if ((t - 2 * (m - 1) + (s - 1)) % (2 * (m - 1))) ++k;
}
cout << 2 * (m - 1) - (s - 1) + 2 * (m - 1) * k + s - f << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream &operator>>(istream &cin, pair<T, T> &v) {
cin >> v.first >> v.second;
return cin;
}
template <typename T>
istream &operator>>(istream &cin, vector<T> &v) {
for (T &e : v) cin >> e;
return cin;
}
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int first = 0;
cerr << '{';
for (auto &i : x) cerr << (first++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
void JUDGE() {}
long long inline mo(long long a) { return a % (long long)(1000000000 + 7); }
long long po(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y >>= 1;
x = (x * x) % p;
}
return res % p;
}
void test_case();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int test_cases = 1;
cin >> test_cases;
while (test_cases--) test_case();
return 0;
}
void test_case() {
long long n, m, r_x, r_y, d_x, d_y, p;
cin >> n >> m >> r_x >> r_y >> d_x >> d_y >> p;
r_x--;
r_y--;
d_x--;
d_y--;
p = mo(p *
po(100, (long long)(1000000000 + 7) - 2, (long long)(1000000000 + 7)));
long long inv_p =
(1 - p + (long long)(1000000000 + 7)) % (long long)(1000000000 + 7);
long long x = r_x, y = r_y, cur_time = 0;
long long product = 1, sum = 0;
long long di_x = 1, di_y = 1;
long long ac_x = 1, ac_y = 1;
if (x - 1 < 0) ac_x = -1;
if (y - 1 < 0) ac_y = -1;
while (1) {
if (x == d_x || y == d_y) {
sum = mo(sum + mo(cur_time * mo(product)));
sum %= (long long)(1000000000 + 7);
product *= inv_p;
product %= (long long)(1000000000 + 7);
}
if (di_x == 1 && x == n - 1)
di_x = -1;
else if (di_x == -1 && !x)
di_x = 1;
if (di_y == 1 && y == m - 1)
di_y = -1;
else if (di_y == -1 && !y)
di_y = 1;
x += di_x;
y += di_y;
cur_time++;
cur_time %= (long long)(1000000000 + 7);
if (x == r_x && y == r_y && di_x == ac_x && di_y == ac_y) break;
}
long long inv_val =
po(mo(1 - product + (long long)(1000000000 + 7)),
(long long)(1000000000 + 7) - 2, (long long)(1000000000 + 7));
sum = mo(mo(sum * p) + mo(product * mo(cur_time)));
sum %= (long long)(1000000000 + 7);
long long ans = mo(sum * inv_val);
cout << ans << "\n";
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
double l, v1, v2;
int p;
int main() {
scanf("%d %lf %lf %lf %d", &n, &l, &v1, &v2, &k);
double L = 0;
p = n / k;
if ((n % k) > 0) p++;
L = l / (p * v2 - (v2 - v1) / (v2 + v1) * v2 * (p - 1));
printf("%.7f\n", (l - v2 * L) / v1 + L);
return 0;
}
| 5 |
#include <bits/stdc++.h>
int n, m, x, y;
double ans[1011][1011];
int main() {
scanf("%d %d", &n, &m);
scanf("%d %d", &x, &y);
x--, y--;
for (int i = n - 2; i >= x; i--) {
for (int runs = 0; runs < 50; runs++) {
for (int j = 0; j < m; j++) {
if (m == 1) {
ans[i][j] = (ans[i][j] + ans[i + 1][j]) / 2 + 1.0;
continue;
}
if (j == 0)
ans[i][j] = (ans[i][j] + ans[i][j + 1] + ans[i + 1][j]) / 3.0 + 1.0;
else if (j == m - 1)
ans[i][j] = (ans[i][j] + ans[i][j - 1] + ans[i + 1][j]) / 3.0 + 1.0;
else
ans[i][j] =
(ans[i][j - 1] + ans[i][j] + ans[i][j + 1] + ans[i + 1][j]) /
4.0 +
1.0;
}
}
}
printf("%.4lf\n", ans[x][y]);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
int s = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
s += a[i];
}
vector<int> b(n);
for (int i = 0; i < n; i++) {
if (a[i] == 1)
b[i] = -1;
else
b[i] = 1;
}
int currma = 0;
int ma = -1000;
for (int i = 0; i < n; i++) {
currma = currma + b[i];
ma = max(currma, ma);
if (currma < 0) currma = 0;
}
cout << ma + s;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int f(string s) {
long long int n1 = 0, s1 = 0;
for (long long int i = 0; i < s.length(); i++)
if (s[i] == 's')
n1++;
else
s1 += n1;
return s1;
}
bool comp(string& s1, string& s2) { return (f(s1 + s2) > f(s2 + s1)); }
int main() {
long long int n;
cin >> n;
string tab[n];
for (long long int i = 0; i < n; i++) cin >> tab[i];
string sf;
sort(tab, tab + n, comp);
for (long long int i = 0; i < n; i++) sf += tab[i];
cout << f(sf);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int maxE;
bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
return a.first > b.first;
}
struct node {
int val;
node *left, *right;
node() { val = 0, left = NULL, right = NULL; }
};
void update(int start, int end, node*& r, int x, int v) {
if (r == NULL) {
r = new node;
}
r->val = max(v, r->val);
int mid = (start + end) / 2;
if (start == end) {
} else if (x <= mid)
update(start, mid, r->left, x, v);
else
update(mid + 1, end, r->right, x, v);
}
int query(int start, int end, node*& r, int a, int b) {
if (r == NULL) {
return 0;
}
if (start >= a && end <= b)
return r->val;
else if (start > b || end < a)
return 0;
else {
int mid = (start + end) / 2;
return max(query(start, mid, r->left, a, b),
query(mid + 1, end, r->right, a, b));
}
}
pair<int, pair<int, int>> arr_input[3 * N];
pair<int, pair<int, int>> arr[N];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &arr_input[i].first);
arr_input[i].second.first = i, arr_input[i].second.second = 1;
}
for (int i = n + 1; i <= 2 * n; i++) {
scanf("%d", &arr_input[i].first);
arr_input[i].second.first = i - n, arr_input[i].second.second = 2;
}
for (int i = 2 * n + 1; i <= 3 * n; i++) {
scanf("%d", &arr_input[i].first);
arr_input[i].second.first = i - 2 * n, arr_input[i].second.second = 3;
}
sort(arr_input + 1, arr_input + 1 + 3 * n);
int i = 1, h;
int count = 0, ans = 0;
maxE = 0;
while (i <= 3 * n) {
h = i;
maxE++;
while (i <= 3 * n && arr_input[h].first == arr_input[i].first) {
if (arr_input[i].second.second == 1) {
arr[arr_input[i].second.first].first = maxE;
} else if (arr_input[i].second.second == 2) {
arr[arr_input[i].second.first].second.first = maxE;
} else if (arr_input[i].second.second == 3) {
arr[arr_input[i].second.first].second.second = maxE;
}
i++;
}
}
sort(arr + 1, arr + 1 + n, cmp);
i = 1;
node* root = NULL;
while (i <= n) {
h = i;
while (i <= n && arr[h].first == arr[i].first) {
if (query(1, maxE, root, arr[i].second.first + 1, maxE) >
arr[i].second.second) {
ans++;
}
i++;
}
for (int j = h; j < i; j++) {
update(1, maxE, root, arr[j].second.first, arr[j].second.second);
}
}
cout << ans << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int dp[N][26] = {0};
vector<int> g[N];
char cl[N] = {0};
int n, m;
string line;
int answer = 0;
int ctoi(char c) { return c - 'a'; }
bool dfs(int v) {
int cIndex = ctoi(line[v]);
cl[v] = 1;
dp[v][cIndex] = 1;
for (int to : g[v]) {
if (cl[to] == 0) {
if (dfs(to)) return true;
} else if (cl[to] == 1) {
return true;
}
for (int i = 0; i < 26; ++i) {
int add = i == cIndex;
dp[v][i] = max(dp[v][i], dp[to][i] + add);
answer = max(answer, dp[v][i]);
}
}
answer = max(answer, dp[v][cIndex]);
cl[v] = 2;
return false;
}
int main(int argc, const char* argv[]) {
ios::sync_with_stdio(false);
cin >> n >> m;
cin.get();
getline(cin, line);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
g[a - 1].push_back(b - 1);
}
for (int i = 0; i < n; ++i) {
if (!cl[i]) {
if (dfs(i)) {
cout << -1 << endl;
return 0;
}
}
}
cout << answer << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 35;
int N, H;
long long f[Maxn + 5][Maxn + 5];
long long DFS(int n, int h) {
if (n != 0 && h == 0) return f[n][h] = 0;
if (n == 0 && h != 0) return f[n][h] = 0;
if (n == 0 && h == 0) return f[n][h] = 1;
if (f[n][h] != -1) return f[n][h];
long long ret = 0;
for (int m = 1; m <= n; m++) {
for (int i = 0; i <= h - 1; i++) ret += DFS(m - 1, h - 1) * DFS(n - m, i);
for (int i = 0; i <= h - 2; i++) ret += DFS(m - 1, i) * DFS(n - m, h - 1);
}
return f[n][h] = ret;
}
int main() {
scanf("%d %d", &N, &H);
memset(f, -1, sizeof f);
DFS(N + 1, N + 1);
long long ans = 0;
for (int i = H; i <= N; i++) ans += f[N][i];
printf("%lld\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, nr[100069], inf = 1e18;
pair<long long, long long> ed[100069], dp[100069];
bitset<100069> wg, vtd, spc;
vector<pair<long long, bool>> al[100069], cd[100069];
queue<long long> q;
void dfs(long long x) {
long long i, sz = cd[x].size(), l, w;
vtd[x] = 1;
dp[x] = {inf * (x != 1), -1};
for (i = 0; i < sz; i++) {
l = cd[x][i].first;
w = cd[x][i].second;
if (!vtd[l]) {
dfs(l);
}
dp[x] = min(dp[x], {dp[l].first + !w * 2 - 1, l});
}
}
int main() {
long long i, k, l, w, sz, c = 0, p;
scanf("%lld%lld", &n, &m);
for (i = 1; i <= m; i++) {
scanf("%lld%lld%lld", &k, &l, &w);
ed[i] = {k, l};
wg[i] = w;
al[k].push_back({l, w});
al[l].push_back({k, w});
c += w;
}
for (i = 1; i <= n; i++) {
nr[i] = inf;
}
q.push(1);
nr[1] = 0;
for (; !q.empty();) {
k = q.front();
q.pop();
sz = al[k].size();
for (i = 0; i < sz; i++) {
l = al[k][i].first;
w = al[k][i].second;
if (nr[k] + 1 < nr[l]) {
q.push(l);
nr[l] = nr[k] + 1;
}
if (nr[k] + 1 == nr[l]) {
cd[l].push_back({k, w});
}
}
}
dfs(n);
for (p = n; p + 1; p = dp[p].second) {
spc[p] = 1;
}
printf("%lld\n", c + dp[n].first);
for (i = 1; i <= m; i++) {
k = ed[i].first;
l = ed[i].second;
if ((spc[k] && spc[l]) ^ wg[i]) {
printf("%lld %lld %lld\n", k, l, (long long)!wg[i]);
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void INPUT() {}
const int N = 200005;
set<int> s;
map<pair<int, int>, bool> h;
int tot, n, x, y, m;
void dfs(int node) {
tot++;
s.erase(node);
set<int>::iterator it = s.begin();
int id;
while (it != s.end()) {
id = *it;
if (!h.count(make_pair(node, id))) {
dfs(id);
it = (s.begin());
} else
it++;
}
}
void solve() {
cin >> n >> m;
h.clear();
s.clear();
while (m--) {
cin >> x >> y;
x--;
y--;
h[make_pair(x, y)] = 1;
h[make_pair(y, x)] = 1;
}
for (int i = 0; i <= n - 1; i++) s.insert(i);
vector<int> ans;
int node;
while (!s.empty()) {
node = *(s.begin());
tot = 0;
dfs(node);
ans.push_back(tot);
}
sort(ans.begin(), ans.end());
cout << ans.size() << "\n";
for (auto &i : ans) cout << i << " ";
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
INPUT();
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int INF = 2e9 + 9;
const long long INF1 = 1e18 + 9;
const long long MAXN = 4e5 + 7;
const long long MAXN1 = 1 << 11;
const long long MAXN2 = 4e6 + 9;
const long long MOD = 1e9 + 7;
const long long MOD1 = 1e9 + 9;
const long long ALPH = 50;
const long long PW1 = 239;
const long long PW2 = 199;
const long long PW3 = 193;
const long long PW4 = 117;
const long double EPS = 1e-9;
const long long BLOCK = 3684;
const long long BLOCK1 = 1 << 9;
void solve();
signed main() {
srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q = 1;
if (0) cin >> q;
while (q--) solve();
}
bool cmp(pair<set<int>, int> &a, pair<set<int>, int> &b) {
return a.first.size() < b.first.size();
}
void solve() {
int n, m, cnt = 0;
cin >> n >> m;
vector<pair<set<int>, int>> v(n), ans(n);
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
cnt += k;
for (int j = 0; j < k; ++j) {
int a;
cin >> a;
v[i].first.emplace(a);
}
v[i].second = i;
}
sort(v.begin(), v.end(), cmp);
int l = 0, r = n - 1;
vector<pair<pair<int, int>, int>> ans1;
while (l != r) {
if (ans[l].first.size() == cnt / n + (l >= n - cnt % n ? 1 : 0)) {
++l;
continue;
}
if (v[r].first.size() + ans[r].first.size() ==
cnt / n + (r >= n - cnt % n ? 1 : 0)) {
--r;
continue;
}
for (auto &j : v[l].first) ans[l].first.emplace(j);
v[l].first.clear();
while (ans[l].first.size() < cnt / n + (l >= n - cnt % n ? 1 : 0) &&
v[r].first.size() + ans[r].first.size() >
cnt / n + (r >= n - cnt % n ? 1 : 0)) {
auto p = *v[r].first.begin();
v[r].first.erase(p);
if (ans[l].first.find(p) == ans[l].first.end()) {
ans1.emplace_back(make_pair(v[r].second, v[l].second), p);
ans[l].first.emplace(p);
} else {
ans[r].first.emplace(p);
}
}
}
cout << ans1.size() << "\n";
for (auto &i : ans1) {
cout << i.first.first + 1 << " " << i.first.second + 1 << " " << i.second
<< "\n";
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, c, d1, d2, p1, p2, p3, p4, t = 0;
cin >> a >> b >> c;
d1 = a / c;
d2 = b / c;
p1 = a % c;
p2 = b % c;
p3 = p1 + p2;
p4 = p3 / c;
if (p3 >= c) {
if (p1 >= p2) {
t = c - p1;
} else
t = c - p2;
}
cout << (d1 + d2 + p4) << " " << t;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int tu(int val) { return (1 << val); }
bool iset(int mask, int id) {
if ((mask & tu(id)) != 0) return true;
return false;
}
void doset(int &mask, int id) { mask |= tu(id); }
void dounset(int &mask, int id) { mask = mask & (~tu(id)); }
template <typename T>
string tos(T a) {
stringstream ss;
string ret;
ss << a;
ss >> ret;
return ret;
}
int main() {
string st = "^>v<";
char ca, cb;
int n;
while (cin >> ca >> cb >> n) {
n = n % 4;
int ia = 0;
for (int(i) = (0); (i) < (4); (i)++) {
if (st[i] == ca) {
ia = i;
break;
}
}
int va, vb;
va = vb = 0;
if (st[(ia + n + 4) % 4] == cb) va = 1;
if (st[(ia - n + 4) % 4] == cb) vb = 1;
if (va + vb == 2)
puts("undefined");
else if (va == 1)
puts("cw");
else if (vb == 1)
puts("ccw");
else
puts("undefined");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long a[2000][2000], sum, n, ans;
int main() {
scanf("%lld", &n);
for (int i = 1; i <= 500; i++) {
sum += i;
if (sum > n) {
ans = i - 1;
break;
}
}
long long ehlel = 0;
for (int i = 1; i <= ans; i++) {
for (int j = i; j <= i + ans - i; j++) {
ehlel++;
a[i][j] = ehlel;
}
}
for (int i = 1; i <= ans; i++) {
for (int x = i + 1; x <= ans + 1; x++) {
a[x][i] = a[i][x - 1];
}
}
cout << ans + 1 << endl;
for (int i = 1; i <= ans + 1; i++) {
for (int j = 1; j <= ans; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXL = 1e6 + 1e3;
char s[MAXL + 1];
int cnts[MAXL + 1];
int main() {
int k;
while (scanf("%d%s", &k, s) >= 1) {
int n = strlen(s);
memset(cnts, 0, sizeof cnts);
long long res = 0;
int csum = 0;
cnts[csum]++;
for (int i = 0; i < n; i++) {
csum += s[i] == '1';
int nsum = csum - k;
if (nsum >= 0) res += cnts[nsum];
cnts[csum]++;
}
printf("%I64d\n", res);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define N 500010
#define NN 1000100
#define PB push_back
#define PF push_front
#define sz(x) int(x.size())
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define _ << " " <<
#define endl '\n'
#define M ll(998244353)
#define pri(x) cout << x << endl
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef array <int, 3> a3;
//typedef tree <int, null_type, less_equal <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int n, k;
int a[N], t[N * 4], psh[N * 4], skok[N];
void comp(int v)
{
if (t[v + v] == -1) t[v] = t[v + v + 1];
else t[v] = t[v + v];
}
void Push(int v, int tl, int tr)
{
if (psh[v] == 0) return;
t[v] = tl;
if (tl == tr)
{
t[v] = tl;
skok[tl] += psh[v];
}
else
{
int md = (tl + tr) >> 1;
t[v + v] = tl;
psh[v + v] += psh[v];
t[v + v + 1] = md + 1;
psh[v + v + 1] += psh[v];
}
psh[v] = 0;
}
void updr(int v, int tl, int tr, int ps)
{
Push(v, tl, tr);
if (tl == tr)
{
skok[tl]--;
if (skok[tl] == 0) t[v] = -1;
return;
}
int md = (tl + tr) >> 1;
if (ps <= md)
updr(v + v, tl, md, ps);
else updr(v + v + 1, md + 1, tr, ps);
comp(v);
}
void upd(int v, int tl, int tr, int l, int r)
{
Push(v, tl, tr);
if (tr < l || r < tl || l > r || tl > tr) return;
if (l <= tl && tr <= r) {psh[v]++; Push(v, tl, tr); return;}
int md = (tl + tr) >> 1;
upd(v + v, tl, md, l, r);
upd(v + v + 1, md + 1, tr, l, r);
comp(v);
}
bool can(int x)
{
for (int i = 0; i < N * 4; i++) {t[i] = -1; psh[i] = 0;}
for (int i = 0; i < N; i++) skok[i] = 0;
upd(1, 0, N - 1, 0, 0);
int num = 1, j = 0;
while (j < n && num < k && num > 0)
{
int val = a[j];
int ps = t[1];
updr(1, 0, N - 1, ps);
int L = val / 2, R = (val - 1) / 2;
num += min(L, x - ps - 1) + min(R, x - ps - 1);
num--;
upd(1, 0, N - 1, ps + 2, min(x, ps + L + 1));
upd(1, 0, N - 1, ps + 2, min(x, ps + R + 1));
j++;
}
return num >= k;
}
int main()
{
iostream::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("queries.in", "r", stdin); freopen("queries.out", "w", stdout);
cin >> n >> k;
ll sum = 0;
for (int i = 0; i < n; i++) {cin >> a[i]; sum += a[i];}
sum -= n + n - 1;
if (k > sum) {pri(-1); exit(0);}
sort(a, a + n);
reverse(a, a + n);
int l = 0, r = N - 1;
while (l < r)
{
int md = (l + r) >> 1;
if (can(md)) r = md;
else l = md + 1;
}
pri(l);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
int lk[10][10];
vector<int> v;
int func(vector<int> a) {
int i, j, ret = 0;
;
for (i = 0; i < a.size(); i++) {
for (j = 0; j < a.size(); j++) {
if (i == j) continue;
ret += lk[a[i]][a[j]];
}
}
return ret;
}
int main() {
int n, a, b, c, idx[7], k, i, j, l, cnt, mx, mn, ex, diff, lik;
string aa, tmp, bb;
while (cin >> n) {
mp.clear();
memset(lk, 0, sizeof(lk));
k = 1;
for (i = 0; i < n; i++) {
cin >> aa >> tmp >> bb;
if (mp.find(aa) == mp.end()) mp[aa] = k++;
if (mp.find(bb) == mp.end()) mp[bb] = k++;
lk[mp[aa]][mp[bb]] = 1;
}
cin >> a >> b >> c;
diff = 2000000009;
for (i = 1; i <= 7; i++) idx[i - 1] = i;
do {
for (i = 0; i < 1; i++) {
for (j = i + 1; j < 7; j++) {
for (k = j + 1; k < 7; k++) {
cnt = 0;
mx = 0;
mn = 2000000009;
v.clear();
for (l = i; l < j; l++) v.push_back(idx[l]);
cnt += func(v);
ex = a / v.size();
mx = ((mx) > (ex) ? (mx) : (ex));
mn = ((mn) < (ex) ? (mn) : (ex));
v.clear();
for (l = j; l < k; l++) v.push_back(idx[l]);
cnt += func(v);
ex = b / v.size();
mx = ((mx) > (ex) ? (mx) : (ex));
mn = ((mn) < (ex) ? (mn) : (ex));
v.clear();
for (l = k; l < 7; l++) v.push_back(idx[l]);
cnt += func(v);
ex = c / v.size();
mx = ((mx) > (ex) ? (mx) : (ex));
mn = ((mn) < (ex) ? (mn) : (ex));
if (mx - mn < diff) {
diff = mx - mn;
lik = cnt;
} else if (mx - mn == diff && cnt > lik) {
lik = cnt;
}
}
}
}
} while (next_permutation(idx, idx + 7));
cout << diff << " " << lik << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
return a < b ? a : b;
}
const char lf = '\n';
namespace ae86 {
const int bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline int fetch() {
if (s == t) {
t = (s = buf) + fread(buf, 1, bufl, stdin);
if (s == t) return EOF;
}
return *s++;
}
inline int ty() {
int a = 0;
int b = 1, c = fetch();
while (!isdigit(c)) b ^= c == '-', c = fetch();
while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
return b ? a : -a;
}
} // namespace ae86
using ae86::ty;
const int _ = 200007, __ = 2000007, mo = 1000000007;
int isnp[__] = {0}, lasp[__] = {0};
void fuck(int n = __ - 1) {
for (int i = 2; i <= n; i++) {
if (isnp[i]) continue;
lasp[i] = i;
for (long long j = 1ll * i * i; j <= n; j += i) {
isnp[j] = 1;
if (!lasp[j]) lasp[j] = i;
}
}
}
map<int, pair<int, int>> mp;
void gmax(int who, int d) {
if (!who) return;
if (mp[who].first < d) mp[who].second = 0;
if (mp[who].first <= d) mp[who].first = d, mp[who].second++;
}
void add(int x) {
int las = 0, cnt = 0;
while (x > 1) {
int a = lasp[x];
x /= a;
if (las != a) gmax(las, cnt), cnt = 0;
las = a, cnt++;
}
if (cnt) gmax(las, cnt);
}
int finder(int x) {
int las = 0, cnt = 0;
while (x > 1) {
int a = lasp[x];
x /= a;
if (las != a) {
if (las && mp[las].first == cnt && mp[las].second == 1) return 0;
cnt = 0;
}
las = a, cnt++;
}
if (las && mp[las].first == cnt && mp[las].second == 1) return 0;
return 1;
}
int n, val[_], fr[__] = {0};
int main() {
ios::sync_with_stdio(0), cout.tie(nullptr);
fuck();
n = ty();
for (int i = 1; i <= n; i++) val[i] = ty();
sort(val + 1, val + n + 1, greater<int>());
for (int i = 1, j = 1; i <= n; i = j + 1) {
j = i;
while (j < n && val[i] == val[j + 1]) j++;
add(val[i]);
}
for (int i = 1, j = 1; i <= n; i = j + 1) {
j = i;
while (j < n && val[i] == val[j + 1]) j++;
int cnt = j - i + 1;
if (cnt + finder(val[i]) >= 2) {
if (cnt == 1) {
fr[val[i]] = 1;
if (mp[val[i]].first == 1) mp[val[i]].second--;
}
add(val[i] - 1);
}
}
int temp = 0;
for (int i = 1, j = 1; i <= n; i = j + 1) {
j = i;
while (j < n && val[i] == val[j + 1]) j++;
int cnt = j - i + 1;
if (cnt + (cnt == 1 ? fr[val[i]] : finder(val[i])) + finder(val[i] - 1) >=
3)
temp = 1;
}
long long ans = 1;
for (auto i : mp)
while (i.second.first--) ans = ans * i.first % mo;
if (temp) ans = (ans + 1) % mo;
cout << ans << lf;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
int a[n], cnt = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
cnt += a[i];
}
if ((cnt + (n - 1) * 10) > d) {
cout << -1;
return 0;
}
int k = (d - cnt) / 5;
cout << k;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, a, b, c, d, e, f;
int n;
cin >> n;
cin >> s;
a = "RGB";
b = "RBG";
c = "GRB";
d = "GBR";
e = "BRG";
f = "BGR";
int cur = 0;
int counta = 0, countb = 0, countc = 0, countd = 0, counte = 0, countf = 0;
for (int i = 0; i < s.length(); i++) {
if (a[cur] != s[i]) {
counta++;
}
if (b[cur] != s[i]) {
countb++;
}
if (c[cur] != s[i]) {
countc++;
}
if (d[cur] != s[i]) {
countd++;
}
if (e[cur] != s[i]) {
counte++;
}
if (f[cur] != s[i]) {
countf++;
}
cur++;
cur %= 3;
}
int ans =
min(counta, min(countb, min(countc, min(countd, min(counte, countf)))));
if (ans == counta) {
cout << ans << "\n";
for (int i = 0; i < s.length(); i++) {
cout << a[i % 3];
}
return 0;
}
if (ans == countb) {
cout << ans << "\n";
for (int i = 0; i < s.length(); i++) {
cout << b[i % 3];
}
return 0;
}
if (ans == countc) {
cout << ans << "\n";
for (int i = 0; i < s.length(); i++) {
cout << c[i % 3];
}
return 0;
}
if (ans == countd) {
cout << ans << "\n";
for (int i = 0; i < s.length(); i++) {
cout << d[i % 3];
}
return 0;
}
if (ans == counte) {
cout << ans << "\n";
for (int i = 0; i < s.length(); i++) {
cout << e[i % 3];
}
return 0;
}
if (ans == countf) {
cout << ans << "\n";
for (int i = 0; i < s.length(); i++) {
cout << f[i % 3];
}
return 0;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
class Debugger {
public:
template <typename T>
void printVector(vector<T> const &vec, bool printSize = true) {
if (printSize) {
cout << vec.size() << endl;
}
for (auto &elem : vec) {
cout << elem << " ";
}
cout << endl;
}
template <typename T>
void printMatrix(vector<vector<T>> const &matrix) {
cout << matrix.size() << " ";
if (!matrix.empty()) {
cout << matrix[0].size();
}
cout << endl;
for (auto &vec : matrix) {
printVector(vec, false);
}
cout << endl;
}
};
const int INF = 1e9;
const int maxN = 100005;
class TaskF {
private:
Debugger debugger;
public:
void solveOne(int it) {
int n;
cin >> n;
string s, t;
cin >> s >> t;
vector<vector<int>> acumS(n, vector<int>(26, 0)),
acumT(n, vector<int>(26, 0));
for (int i = 0; i < n; i++) {
acumS[i][s[i] - 'a'] = 1;
acumT[i][t[i] - 'a'] = 1;
if (i > 0) {
for (int alpha = 0; alpha < 26; alpha++) {
acumS[i][alpha] += acumS[i - 1][alpha];
acumT[i][alpha] += acumT[i - 1][alpha];
}
}
}
vector<vector<int>> dp(n + 1, vector<int>(n + 1, INF));
dp[0][0] = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i > 0 and j > 0 and s[i - 1] == t[j - 1]) {
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
}
if (i > 0 and j > 0) {
dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
}
if (j > 0) {
int alpha = t[j - 1] - 'a';
int sum1 = acumS[n - 1][alpha] - (i > 0 ? acumS[i - 1][alpha] : 0);
int sum2 = acumT[n - 1][alpha] - acumT[j - 1][alpha];
if (sum1 > sum2) {
dp[i][j] = min(dp[i][j], dp[i][j - 1]);
}
}
}
}
if (dp[n][n] >= INF) {
cout << -1 << endl;
return;
}
cout << dp[n][n] << endl;
}
void solve() {
int tc = 1;
cin >> tc;
for (int it = 1; it <= tc; it++) {
solveOne(it);
}
}
};
int main() {
ios_base::sync_with_stdio(false);
TaskF solver;
solver.solve();
return 0;
}
| 9 |
#include <bits/stdc++.h>
long long power(long long base, long long exp) {
long long res = 1;
while (exp > 0) {
if (exp % 2 == 1) res = (res * base);
base = (base * base);
exp /= 2;
}
return res;
}
long long mod(long long a, long long b) { return (a % b + b) % b; }
long long powerm(long long base, long long exp, long long mod) {
long long ans = 1;
while (exp) {
if (exp & 1) ans = (ans * base) % mod;
exp >>= 1, base = (base * base) % mod;
}
return ans;
}
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long t = 1;
while (t--) {
long long n;
cin >> n;
long long arr[n];
unordered_map<long long, long long> mp;
long long csum[n];
for (long long i = 0; i < n; i++) cin >> arr[i];
csum[0] = arr[0];
for (long long i = 1; i < n; i++) csum[i] = arr[i] + csum[i - 1];
mp[arr[n - 1]] = n - 1;
long long s = arr[n - 1];
for (long long i = n - 2; i >= 0; i--) {
s += arr[i];
mp[s] = i;
}
long long ans = 0;
for (long long i = 0; i < n; i++) {
if (mp.count(csum[i]) && mp[csum[i]] > i) {
ans = max(ans, csum[i]);
}
}
cout << ans << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, i, j, a;
vector<int> z;
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
cin >> n;
z.push_back(-1000000010);
for (i = 0; i < n; i++) {
cin >> a;
if (a == 0) z.push_back(i);
}
z.push_back(1000000010);
j = 0;
for (i = 0; i < n; i++) {
if (i == z[j + 1]) {
j++;
}
cout << min(i - z[j], z[j + 1] - i) << " ";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef long long ll;
template <typename T>
inline void read(T &f) {
f = 0; T fu = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); }
while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
f *= fu;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
print(x); putchar(t);
}
const int N = 2e5 + 5, M = N * 60, ALL = (1 << 20) - 1;
int ch[M][2], tag0[M], tag1[M], tagx[M], siz[M], can[M], tot;
int a[N];
void update(int u, int dep) {
siz[u] = siz[ch[u][0]] + siz[ch[u][1]];
can[u] = can[ch[u][0]] | can[ch[u][1]];
if (ch[u][0] && ch[u][1]) can[u] |= (1 << dep);
}
void add_tag(int u, int tg0, int tg1, int tgx) {
int tem = ALL ^ tg0 ^ tg1 ^ tgx, temu = ALL ^ tag0[u] ^ tag1[u] ^ tagx[u];
int ans0 = tg0 | (tag1[u] & tgx) | (tag0[u] & tem);
int ans1 = tg1 | (tag0[u] & tgx) | (tag1[u] & tem);
tagx[u] = (tagx[u] & tem) | (tgx & temu);
tag0[u] = ans0; tag1[u] = ans1;
}
int merge(int u, int v, int dep);
void pushdown(int u, int dep) {
if ((tag0[u] >> dep) & 1) {
ch[u][0] = merge(ch[u][0], ch[u][1], dep - 1); ch[u][1] = 0;
}
if ((tag1[u] >> dep) & 1) {
ch[u][1] = merge(ch[u][0], ch[u][1], dep - 1); ch[u][0] = 0;
}
if ((tagx[u] >> dep) & 1) {
swap(ch[u][0], ch[u][1]);
}
update(u, dep);
for (int i = 0; i <= 1; i++) {
if (ch[u][i]) {
add_tag(ch[u][i], tag0[u], tag1[u], tagx[u]);
}
}
tag0[u] = tag1[u] = tagx[u] = 0;
}
void check(int u, int dep) {
if (!u || dep == -1) return;
if (((tag0[u] | tag1[u]) & can[u]) == 0) return;
pushdown(u, dep);
check(ch[u][0], dep - 1); check(ch[u][1], dep - 1);
update(u, dep);
}
int merge(int u, int v, int dep) {
if (!u || !v) return u | v;
if (dep == -1) return u;
pushdown(u, dep); pushdown(v, dep);
ch[u][0] = merge(ch[u][0], ch[v][0], dep - 1);
ch[u][1] = merge(ch[u][1], ch[v][1], dep - 1);
update(u, dep); return u;
}
// split <= x
void split(int u, int x, int &l, int &r, int dep) {
if (!u) { l = r = 0; return; }
if (dep == -1) {
l = u; r = 0;
return;
}
pushdown(u, dep);
if ((x >> dep) & 1) {
l = u; r = ++tot;
split(ch[u][1], x, ch[l][1], ch[r][1], dep - 1);
update(l, dep); update(r, dep);
if (!siz[r]) r = 0;
} else {
l = ++tot; r = u;
split(ch[u][0], x, ch[l][0], ch[r][0], dep - 1);
update(l, dep); update(r, dep);
if (!siz[l]) l = 0;
}
}
void insert(int &u, int x, int dep) {
if (dep == -1) {
u = ++tot;
siz[tot] = 1;
return;
}
if (!u) u = ++tot;
insert(ch[u][(x >> dep) & 1], x, dep - 1);
update(u, dep);
}
void dfs(int u, int dep) {
if (!u) return;
// if (dep != -1) pushdown(u, dep);
fprintf(stderr, "u = %d, tag0[u] = %d, tag1[u] = %d, tagx[u] = %d, ch[u][0] = %d, ch[u][1] = %d, siz[u] = %d, dep = %d\n", u, tag0[u], tag1[u], tagx[u], ch[u][0], ch[u][1], siz[u], dep);
dfs(ch[u][0], dep - 1); dfs(ch[u][1], dep - 1);
}
int n, q, root;
int main() {
read(n); read(q);
for (int i = 1; i <= n; i++) {
int a; read(a);
insert(root, a, 19);
}
while (q--) {
int opt, l, r; read(opt); read(l); read(r);
int a = 0, b = 0, c = 0;
// fprintf(stderr, "root = %d\n", root);
if (l != 0) split(root, l - 1, a, b, 19);
else b = root;
split(b, r, b, c, 19);
if (opt <= 3) {
int x; read(x);
if (opt == 1) add_tag(b, ALL ^ x, 0, 0);
if (opt == 2) add_tag(b, 0, x, 0);
if (opt == 3) add_tag(b, 0, 0, x);
// fprintf(stderr, ">>> %d\n", can[b] & (tag0[b] | tag1[b]));
check(b, 19);
}
// dfs(b, 19);
if (opt == 4) print(siz[b], '\n');
// fprintf(stderr, "b = %d, tag0[b] = %d, tag1[b] = %d, tag2[b] = %d\n", b, tag0[b], tag1[b], tagx[b]);
root = merge(merge(a, b, 19), c, 19);
// dfs(root, 19);
}
return 0;
} | 13 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, k;
long long ips[maxn];
long long fmt(int a[4]) {
return ((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
}
string outfmt(long long a) {
string res = "";
for (int i = 3; i >= 0; i--) {
int r = 0;
for (int j = 0; j < 8; j++) {
int b = ((a >> (i * 8 + j)) & 1) << j;
r |= b;
}
res += to_string(r);
if (i != 0) res += '.';
}
return res;
}
int main() {
scanf("%d %d\n", &n, &k);
for (int i = 0; i < n; i++) {
int a[4];
scanf("%d.%d.%d.%d\n", a, a + 1, a + 2, a + 3);
long long f = fmt(a);
ips[i] = f;
}
long long subnetMask = 0LL;
for (int i = 31; i > 0; i--) {
subnetMask |= (1 << i);
unordered_set<long long> addresses;
for (int j = 0; j < n; j++) {
addresses.insert(subnetMask & ips[j]);
}
if (addresses.size() == k) {
cout << outfmt(subnetMask) << '\n';
return 0;
}
}
cout << -1 << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[10010];
struct idk {
int x, y, z;
};
vector<idk> sol;
vector<pair<int, int> > v;
int t, n, i;
int main() {
cin >> t;
for (; t--;) {
cin >> n;
int sum = 0;
for (i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum % n) {
cout << "-1\n";
continue;
}
int val = sum / n;
sol.clear();
v.clear();
for (i = 2; i <= n; i++) {
if (a[i] % i)
v.push_back(make_pair(i - a[i] % i, i));
else {
sol.push_back({i, 1, a[i] / i});
a[1] += a[i];
a[i] = 0;
}
}
sort(v.begin(), v.end());
for (auto it : v) {
int dif = it.first, i = it.second;
sol.push_back({1, i, dif});
a[i] += dif;
a[1] -= dif;
sol.push_back({i, 1, a[i] / i});
a[1] += (a[i] / i) * i;
a[i] = 0;
}
for (i = 2; i <= n; i++) {
if (a[i] < val) {
int dif = val - a[i];
sol.push_back({1, i, dif});
a[i] += dif;
a[1] -= dif;
}
}
cout << sol.size() << "\n";
for (auto it : sol) cout << it.x << " " << it.y << " " << it.z << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:512000000")
using namespace std;
void solve(__attribute__((unused)) bool);
void precalc();
clock_t start;
int main() {
start = clock();
int t = 1;
cout.sync_with_stdio(0);
cin.tie(0);
precalc();
cout.precision(10);
cout << fixed;
int testNum = 1;
while (t--) {
solve(true);
}
cout.flush();
return 0;
}
template <typename T>
T binpow(T q, T w, T mod) {
if (!w) return 1 % mod;
if (w & 1) return q * 1LL * binpow(q, w - 1, mod) % mod;
return binpow(q * 1LL * q % mod, w / 2, mod);
}
template <typename T>
T gcd(T q, T w) {
while (w) {
q %= w;
swap(q, w);
}
return q;
}
template <typename T>
T lcm(T q, T w) {
return q / gcd(q, w) * w;
}
template <typename T>
void make_unique(vector<T>& a) {
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
}
template <typename T>
void relax_min(T& cur, T val) {
cur = min(cur, val);
}
template <typename T>
void relax_max(T& cur, T val) {
cur = max(cur, val);
}
void precalc() {}
struct Query {
int l, r;
int id;
bool operator<(const Query& ot) const { return l > ot.l; }
};
struct TreeSum {
int shift;
vector<int> tree;
TreeSum(int n) {
shift = 1;
while (shift < n) {
shift *= 2;
}
tree.assign(2 * shift, 0);
}
int get_res(int l, int r) {
l += shift;
r += shift;
int res = 0;
while (l < r) {
if (l & 1) {
res += tree[l];
++l;
continue;
}
if (r & 1) {
--r;
res += tree[r];
continue;
}
l /= 2;
r /= 2;
}
return res;
}
void update(int pos) {
pos += shift;
++tree[pos];
pos /= 2;
while (pos) {
tree[pos] = tree[2 * pos] + tree[2 * pos + 1];
pos /= 2;
}
}
};
void solve(__attribute__((unused)) bool read) {
int n, Q;
cin >> n >> Q;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
a[i] = (i + 1) - a[i];
}
vector<Query> qs;
for (int i = 0; i < Q; ++i) {
int x, y;
cin >> x >> y;
qs.push_back({x, n - y, i});
}
sort(qs.begin(), qs.end());
TreeSum tree_sum(n);
vector<vector<int>> add_to_pref(n);
for (int i = 0; i < n; ++i) {
int val = a[i];
if (val < 0) {
continue;
}
int pos = i;
int L = -1, R = pos + 1;
while (L + 1 < R) {
int M = (L + R) / 2;
if (tree_sum.get_res(M, pos) >= val) {
L = M;
} else {
R = M;
}
}
if (L != -1) {
add_to_pref[L].push_back(pos);
tree_sum.update(L);
}
}
TreeSum tree(n);
vector<int> ans(Q, -1);
int cur_pos = n - 1;
for (auto& cur_q : qs) {
while (cur_pos >= cur_q.l) {
for (int pos : add_to_pref[cur_pos]) {
tree.update(pos);
}
--cur_pos;
}
ans[cur_q.id] = tree.get_res(cur_q.l, cur_q.r);
}
for (int x : ans) {
cout << x << "\n";
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, a[200000], p[200000];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
p[a[i]] = x;
}
for (int i = 1; i <= n; ++i) printf("%d ", p[i]);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n;
cin >> n;
long long int sum = 0;
long long int temp;
while (n > 0) {
temp = n % 10;
n /= 10;
if (n == 0) {
cout << sum + temp << endl;
return;
}
sum += n * 10;
n += temp;
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dp[507][507];
int b[507], n;
long long dfs(int l, int r) {
if (dp[l][r] != -1) return dp[l][r];
if (l == r) return dp[l][r] = 1LL;
long long ans = 0;
for (int i = l + 1; i <= r; i++) {
if (i != r && b[l + 1] >= b[i + 1]) continue;
ans += dfs(l + 1, i) * dfs(i, r) % mod;
ans %= mod;
}
return dp[l][r] = ans;
}
int main() {
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
memset(dp, -1, sizeof(dp));
printf("%I64d\n", dfs(1, n));
}
}
| 7 |
#include <bits/stdc++.h>
const int maxn = 200005, K = 37, mod = 1e9 + 7;
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int dec(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
int mul(int a, int b) { return 1ll * a * b % mod; }
int ksm(int a, int b = mod - 2) {
int ret = 1;
for (; b; b >>= 1, a = mul(a, a))
if (b & 1) ret = mul(ret, a);
return ret;
}
int N, Q, A[maxn], L, R, D, sum[maxn], sumK[maxn], Inv;
int zz[maxn][K + 5], C[K + 5][K + 5];
int main() {
Inv = ksm(2);
scanf("%d%d", &N, &Q);
C[0][0] = 1;
for (int i = 1; i <= K; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);
}
for (int i = 0; i <= K; i++) {
zz[0][i] = i == 0 ? 1 : 0;
for (int j = 1; j <= N; j++) zz[j][i] = add(zz[j - 1][i], ksm(j, i));
}
for (int i = 1; i <= N; i++)
scanf("%d", A + i), A[i] = add(A[i], 114514),
sum[i] = add(sum[i - 1], A[i]),
sumK[i] = add(sumK[i - 1], ksm(A[i], K));
while (Q--) {
scanf("%d%d%d", &L, &R, &D);
int m = R - L + 1;
int s = dec(sum[R], sum[L - 1]);
int sK = dec(sumK[R], sumK[L - 1]);
int a1 = mul(dec(s, mul(mul(mul(m, m - 1), Inv), D)), ksm(m));
int res = 0;
for (int j = 0; j <= K; j++)
res = add(
res, mul(mul(mul(C[K][j], ksm(D, j)), ksm(a1, K - j)), zz[m - 1][j]));
puts(res == sK ? "Yes" : "No");
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<char, int> a, pair<char, int> b) {
if (a.first > b.first) return true;
if (a.first < b.first) return false;
return (a.second < b.second);
}
int main() {
char s[100500];
scanf("%s", s);
int l = strlen(s);
vector<pair<char, int> > v(l);
for (int i = 0; i < l; i++) {
v[i].first = s[i];
v[i].second = i;
}
char res[100500];
sort(v.begin(), v.end(), comp);
int kol = 0, cur = -1;
for (int i = 0; i < l; i++) {
if (v[i].second > cur) {
res[kol++] = v[i].first;
cur = v[i].second;
}
}
res[kol] = '\0';
printf("%s\n", res);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
while (n) {
if (n % 3 == 0) {
n /= 3;
continue;
} else {
cout << n / 3 + 1 << endl;
return 0;
}
}
cout << 1 << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
namespace Dinic {
typedef long long T;
struct edge {
int from, to;
T f;
};
const int MAX = 4e4 + 5;
const T INF = 1e18;
int n, s, t, pt[MAX], d[MAX];
vector<edge> e;
vector<int> g[MAX];
queue<int> q;
void init(int n_, int s_, int t_) {
n = n_, s = s_, t = t_;
for (int i = 1; i <= n; i++) g[i].clear();
e.clear();
}
void addEdge(int from, int to, T c) {
edge ed;
ed.from = from, ed.to = to, ed.f = c;
e.push_back(ed);
g[from].push_back((int)e.size() - 1);
ed.from = to, ed.to = from, ed.f = 0;
e.push_back(ed);
g[to].push_back((int)e.size() - 1);
}
bool bfs() {
for (int i = 1; i <= n; i++) d[i] = -1;
d[s] = 0;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int id : g[u]) {
int v = e[id].to;
if (d[v] == -1 && e[id].f > 0) {
d[v] = d[u] + 1;
q.push(v);
}
}
}
return d[t] != -1;
}
T dfs(int u, T flow = INF) {
if (u == t) return flow;
for (; pt[u] < (int)g[u].size(); pt[u]++) {
int id = g[u][pt[u]];
int v = e[id].to;
if (d[u] + 1 != d[v] || e[id].f == 0) continue;
T pushed = dfs(v, min(flow, e[id].f));
if (pushed > 0) {
e[id].f -= pushed;
e[id ^ 1].f += pushed;
return pushed;
}
}
return 0;
}
T getMaxFlow() {
T ans = 0;
while (1) {
if (!bfs()) break;
for (int i = 1; i <= n; i++) pt[i] = 0;
T f;
while ((f = dfs(s))) ans += f;
}
return ans;
}
} // namespace Dinic
int n, m, v[1005];
int main() {
scanf("%d%d", &n, &m);
int s = m + n + 1;
int t = m + n + 2;
Dinic::init(m + n + 2, s, t);
long long sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &v[i]);
Dinic::addEdge(s, i, v[i]);
}
for (int i = 1; i <= m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
sum += c;
Dinic::addEdge(i + n, t, c);
Dinic::addEdge(a, i + n, Dinic::INF);
Dinic::addEdge(b, i + n, Dinic::INF);
}
long long F = Dinic::getMaxFlow();
printf("%lld\n", sum - F);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 3e5 + 100;
const long long MOD = 998244353;
vector<long long> L[MAXN];
long long DP[MAXN][5];
void DFS(long long nodo, long long padre) {
long long cont = 0;
for (auto v : L[nodo]) {
if (v != padre) {
DFS(v, nodo);
cont++;
}
}
if (cont == 0) {
DP[nodo][0] = DP[nodo][1] = DP[nodo][2] = 1;
return;
}
DP[nodo][0] = 1;
for (auto v : L[nodo]) {
if (v != padre) {
DP[nodo][0] = (DP[v][0] + DP[v][3] + DP[v][4]) * DP[nodo][0] % MOD;
}
}
DP[nodo][1] = 1;
for (auto v : L[nodo]) {
if (v != padre) {
DP[nodo][1] = (DP[v][0] + DP[v][3] + DP[v][4]) * DP[nodo][1] % MOD;
}
}
DP[nodo][2] = 1;
for (auto v : L[nodo]) {
if (v != padre) {
DP[nodo][2] = (DP[v][0] + DP[v][3] + DP[v][4]) * DP[nodo][2] % MOD;
}
}
DP[nodo][3] = 1;
long long tot1 = 1;
for (auto v : L[nodo]) {
if (v != padre) {
DP[nodo][3] = (DP[v][1] + DP[v][2] + DP[v][3] + DP[v][4] + DP[v][0] +
DP[v][3] + DP[v][4]) *
DP[nodo][3] % MOD;
tot1 = (DP[v][0] + DP[v][3] + DP[v][4]) * tot1 % MOD;
}
}
DP[nodo][3] = (DP[nodo][3] + MOD - tot1) % MOD;
DP[nodo][4] = 1;
long long tot2 = 1;
for (auto v : L[nodo]) {
if (v != padre) {
DP[nodo][4] = (DP[v][1] + DP[v][3] + DP[v][0] + DP[v][3] + DP[v][4]) *
DP[nodo][4] % MOD;
tot2 = (DP[v][0] + DP[v][3] + DP[v][4]) * tot2 % MOD;
}
}
DP[nodo][4] = (DP[nodo][4] + MOD - tot2) % MOD;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long N;
cin >> N;
for (long long i = 0; i < N - 1; i++) {
long long a, b;
cin >> a >> b;
L[a].push_back(b);
L[b].push_back(a);
}
DFS(1, 0);
cout << (DP[1][0] + DP[1][3] + DP[1][4] + MOD - 1) % MOD;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
signed main(void) {
long long n, l, r;
cin >> n >> l >> r;
vector<long long> a(3, r / 3);
for (long long i = 0; i < (3); i++)
if (i < r % 3) a[(i + 1) % 3]++;
l--;
vector<long long> b(3, l / 3);
for (long long i = 0; i < (3); i++)
if (i < l % 3) b[(i + 1) % 3]++;
for (long long i = 0; i < (3); i++) a[i] -= b[i];
vector<vector<long long> > dp(n, vector<long long>(3, 0));
for (long long i = 0; i < (3); i++) dp[0][i] = a[i];
for (long long i = 0; i < (n - 1); i++)
for (long long j = 0; j < (3); j++)
if (dp[i][j]) {
for (long long k = 0; k < (3); k++) {
long long t = dp[i][j] * a[k] % 1000000007;
dp[i + 1][(j + k) % 3] = (dp[i + 1][(j + k) % 3] + t) % 1000000007;
}
}
cout << dp[n - 1][0] << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n;
int g[maxn * 2][maxn * 2], tot;
int d[maxn];
int R, C;
char c[maxn];
vector<int> x, y, col, row;
int find(vector<int>& x, int y) {
return lower_bound(x.begin(), x.end(), y) - x.begin();
}
char check(int x, int y) {
return x >= 0 && x < R && y >= 0 && y < C && g[x][y] < tot;
}
int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
void dfs(int x, int y) {
if (check(x, y) == 0) return;
g[x][y] = tot + 1;
for (int i = 0; i < 4; i++) {
int nx = x + dir[i][0], ny = y + dir[i][1];
dfs(nx, ny);
}
}
int main() {
while (~scanf("%d", &n)) {
tot++;
for (int i = 0; i < n; i++) {
char s[3];
scanf("%s%d", s, d + i);
c[i] = s[0];
}
x.clear(), y.clear(), col.clear(), row.clear();
int nx = 0, ny = 0;
x.push_back(nx);
y.push_back(ny);
for (int i = 0; i < n; i++) {
if (c[i] == 'R')
ny += d[i];
else if (c[i] == 'L')
ny -= d[i];
else if (c[i] == 'U')
nx -= d[i];
else
nx += d[i];
x.push_back(nx);
y.push_back(ny);
}
sort(x.begin(), x.end());
int s = unique(x.begin(), x.end()) - x.begin();
x.erase(x.begin() + s, x.end());
sort(y.begin(), y.end());
s = unique(y.begin(), y.end()) - y.begin();
y.erase(y.begin() + s, y.end());
row.push_back(1);
for (int i = 1, j = x.size(); i < j; i++) {
if (x[i - 1] + 1 < x[i])
row.push_back(x[i] - x[i - 1] - 1), x.push_back(x[i] - 1);
row.push_back(1);
}
sort(x.begin(), x.end());
col.push_back(1);
for (int i = 1, j = y.size(); i < j; i++) {
if (y[i - 1] + 1 < y[i])
col.push_back(y[i] - y[i - 1] - 1), y.push_back(y[i] - 1);
col.push_back(1);
}
sort(y.begin(), y.end());
R = x.size(), C = y.size();
nx = 0, ny = 0;
int nxx = find(x, nx), nyy = find(y, ny);
for (int i = 0; i < n; i++) {
if (c[i] == 'R')
ny += d[i];
else if (c[i] == 'L')
ny -= d[i];
else if (c[i] == 'U')
nx -= d[i];
else
nx += d[i];
int tx = find(x, nx), ty = find(y, ny);
if (tx < nxx) swap(tx, nxx);
if (ty < nyy) swap(ty, nyy);
for (int k = nxx; k <= tx; k++)
for (int l = nyy; l <= ty; l++) g[k][l] = tot;
nxx = find(x, nx), nyy = find(y, ny);
}
for (int i = 0; i < R; i++) dfs(i, 0), dfs(i, C - 1);
for (int j = 0; j < C; j++) dfs(0, j), dfs(R - 1, j);
tot++;
unsigned long long ans = 0;
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
if (g[i][j] < tot) ans += (unsigned long long)row[i] * col[j];
cout << ans << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long quickmod(long long a, long long n) {
long long s = 1;
while (n) {
if (n & 1) {
s = s * a % mod;
}
a = (a * a) % mod;
n = n / 2;
}
return s;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
bool isPrime(long long x) {
if (x <= 1) return false;
if (x == 2) return true;
if (x % 2 == 0) return false;
long long i, m;
m = sqrt(x);
for (i = 2; i <= m; i++) {
if (x % i == 0) return false;
}
return true;
}
const int N = 1e5 + 5;
int t;
int main() {
cin >> t;
for (register int i = 1; i <= t; ++i) {
for (register int j = 1; j <= t; ++j) {
if (i & 1) {
if (j & 1)
cout << "W";
else
cout << "B";
} else {
{
if (j & 1)
cout << "B";
else
cout << "W";
}
}
}
puts("");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int yescnt = 0;
vector<int> ask;
ask.push_back(2);
ask.push_back(4);
ask.push_back(3);
ask.push_back(9);
ask.push_back(5);
ask.push_back(25);
ask.push_back(7);
ask.push_back(49);
ask.push_back(11);
ask.push_back(13);
ask.push_back(17);
ask.push_back(19);
ask.push_back(23);
ask.push_back(29);
ask.push_back(31);
ask.push_back(37);
ask.push_back(41);
ask.push_back(43);
ask.push_back(47);
ask.push_back(53);
for (int i = 0; i < (int((ask).size())); ++i) {
int num = ask[i];
cout << num << endl;
string str;
cin >> str;
if (str == "yes") {
yescnt++;
}
if (yescnt >= 2) {
cout << "composite" << endl;
return 0;
}
}
cout << "prime" << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, s, d, ans;
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d%d", &s, &d);
while (s <= ans) s += d;
ans = s;
}
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, k, ans;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
string a;
int lucky = 0;
cin >> a;
for (auto it : a)
if (it == '4' || it == '7') lucky++;
if (lucky <= k) ans++;
}
cout << ans << '\n';
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string st;
int main() {
cin >> st;
for (int i = 0; i < st.length(); i++) {
if (i == 0) {
if (st[i] != '9' && '9' - st[i] < st[i] - '0') {
st[i] = '0' + ('9' - st[i]);
}
} else if ('9' - st[i] < st[i] - '0') {
st[i] = '0' + ('9' - st[i]);
}
}
for (int i = 0; i < st.length(); i++) {
cout << st[i];
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e9 + 9;
int dx[8] = {1, 0, 0, -1, 1, -1, 1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
int main() {
cin.tie(0);
cout.tie(0);
std::ios::sync_with_stdio(false);
long long n, m;
cin >> n >> m;
char arr[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cin >> arr[i][j];
}
if (n % 3 != 0 && m % 3 != 0) return cout << "NO", 0;
vector<string> v1(n), v2(m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
v1[i].push_back(arr[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
v2[i].push_back(arr[j][i]);
}
}
if (n % 3 == 0) {
long long c = n / 3, cc = 0;
set<char> s1, s2, s3;
for (int i = cc; i < n / 3 + cc; i++) {
for (int j = 0; j < m; j++) {
s1.insert(v1[i][j]);
}
}
cc += (n / 3);
for (int i = cc; i < n / 3 + cc; i++) {
for (int j = 0; j < m; j++) {
s2.insert(v1[i][j]);
}
}
cc += (n / 3);
for (int i = cc; i < n / 3 + cc; i++) {
for (int j = 0; j < m; j++) {
s3.insert(v1[i][j]);
}
}
set<set<char> > ss;
ss.insert(s1);
ss.insert(s2);
ss.insert(s3);
if (s1.size() == 1 && s2.size() == 1 && s3.size() == 1) {
if (ss.size() == 3) {
cout << "YES" << endl;
return 0;
}
}
}
if (m % 3 == 0) {
long long c = m / 3, cc = 0;
set<char> s1, s2, s3;
for (int i = cc; i < m / 3 + cc; i++) {
for (int j = 0; j < n; j++) {
s1.insert(v2[i][j]);
}
}
cc += (m / 3);
for (int i = cc; i < m / 3 + cc; i++) {
for (int j = 0; j < n; j++) {
s2.insert(v2[i][j]);
}
}
cc += (m / 3);
for (int i = cc; i < (m / 3) + cc; i++) {
for (int j = 0; j < n; j++) {
s3.insert(v2[i][j]);
}
}
set<set<char> > ss;
ss.insert(s1);
ss.insert(s2);
ss.insert(s3);
if (s1.size() == 1 && s2.size() == 1 && s3.size() == 1) {
if (ss.size() == 3) {
cout << "YES" << endl;
return 0;
}
}
}
cout << "NO";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> path[20][20];
bool marked[20][20];
int counter = 0;
inline void dfs(int x, int y) {
if (x >= n || y >= m) return;
if (marked[x][y]) return;
counter++;
marked[x][y] = true;
if (path[x][y].first == 3) dfs(x, y + 1);
if (path[x][y].first == 4) dfs(x, y - 1);
if (path[x][y].second == 1) dfs(x - 1, y);
if (path[x][y].second == 2) dfs(x + 1, y);
}
int main() {
char dir;
memset(path, 0, sizeof(path));
memset(marked, 0, sizeof(marked));
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> dir;
if (dir == '>')
for (int j = 0; j < m - 1; j++) path[i][j].first = 3;
else
for (int j = 1; j < m; j++) path[i][j].first = 4;
}
for (int i = 0; i < m; i++) {
cin >> dir;
if (dir == 'v')
for (int j = 0; j < n - 1; j++) path[j][i].second = 2;
else
for (int j = 1; j < n; j++) path[j][i].second = 1;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
dfs(i, j);
if (counter != n * m) {
cout << "NO\n";
return 0;
}
counter = 0;
memset(marked, 0, sizeof(marked));
}
cout << "YES\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 9;
vector<long long> g[maxn];
long long n, m, a, b;
bool used[maxn];
pair<long long, long long> dfs(long long v, long long u) {
used[v] = 1;
pair<long long, long long> ans(1, v == u);
for (long long to : g[v]) {
if (!used[to]) {
pair<long long, long long> cur = dfs(to, u);
ans.first += cur.first;
ans.second |= cur.second;
}
}
return ans;
}
void solve() {
cin >> n >> m >> a >> b;
a--;
b--;
for (long long i = 0; i < m; i++) {
long long x, y;
cin >> x >> y;
x--;
y--;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
long long sz1 = 0, sz2 = 0;
for (long long i = 0; i < n; i++) used[i] = 0;
used[a] = 1;
for (long long to : g[a]) {
if (!used[to]) {
pair<long long, long long> cur = dfs(to, b);
if (!cur.second) sz1 += cur.first;
}
}
for (long long i = 0; i < n; i++) used[i] = 0;
used[b] = 1;
for (long long to : g[b]) {
if (!used[to]) {
pair<long long, long long> cur = dfs(to, a);
if (!cur.second) sz2 += cur.first;
}
}
for (long long i = 0; i < n; i++) {
g[i].resize(0);
g[i].shrink_to_fit();
}
cout << sz1 * sz2 << "\n";
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
for (long long j = 0; j < t; j++) solve();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 305;
const int MOD = 1e9 + 7;
int n, tot, m;
int a[N], f[N][N], fac[N], C[N][N], cnt[N];
bool vis[N];
inline void prepare() {
for (int i = (0); i <= (N - 1); i++) C[i][0] = 1;
for (int i = (1); i <= (N - 1); i++)
for (int j = (1); j <= (i); j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
fac[0] = 1;
for (int i = (1); i <= (N - 1); i++) fac[i] = 1LL * fac[i - 1] * i % MOD;
}
inline bool check(long long x) { return floor(sqrt(x)) == sqrt(x); }
int main() {
scanf("%d", &n);
prepare();
for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
for (int i = (1); i <= (n); i++) {
if (vis[i]) continue;
vis[i] = 1;
cnt[++tot] = 1;
for (int j = (i + 1); j <= (n); j++)
if (check(1LL * a[i] * a[j])) vis[j] = 1, cnt[tot]++;
}
f[1][cnt[1] - 1] = 1;
m = cnt[1];
for (int i = (2); i <= (tot); i++) {
for (int j = (0); j <= (m - 1); j++)
for (int k = (1); k <= (cnt[i]); k++)
for (int p = (0); p <= (k); p++) {
if (p > j) break;
f[i][j + cnt[i] - k - p] += 1LL * f[i - 1][j] * C[cnt[i] - 1][k - 1] %
MOD * C[j][p] % MOD *
C[m - 1 - j + 2][k - p] % MOD;
f[i][j + cnt[i] - k - p] %= MOD;
}
m += cnt[i];
}
int ans = f[tot][0];
for (int i = (1); i <= (tot); i++) ans = 1LL * ans * fac[cnt[i]] % MOD;
printf("%d\n", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
cerr << '{';
__print(x.first);
cerr << ", ";
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T& x) {
int first = 0;
cerr << '{';
for (auto& i : x) cerr << (first++ ? ", " : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;
const long long SZ = 131072 * 8;
pair<int, int> sum[2 * SZ], lazy[2 * SZ];
pair<int, int> combine(pair<int, int> A, pair<int, int> B) { return max(A, B); }
pair<int, int> combineUpd(pair<int, int> A, pair<int, int> B) {
return max(A, B);
}
void push(int index, long long L, long long R) {
sum[index] = combineUpd(sum[index], lazy[index]);
if (L != R)
lazy[2 * index] = combineUpd(lazy[2 * index], lazy[index]),
lazy[2 * index + 1] = combineUpd(lazy[2 * index + 1], lazy[index]);
lazy[index] = {0, -1};
}
void pull(int index) {
sum[index] = combine(sum[2 * index], sum[2 * index + 1]);
}
pair<int, int> query(int lo, int hi, int index = 1, long long L = 0,
long long R = SZ - 1) {
push(index, L, R);
if (lo > R || L > hi) return {0, -1};
if (lo <= L && R <= hi) return sum[index];
int M = (L + R) / 2;
return combine(query(lo, hi, 2 * index, L, M),
query(lo, hi, 2 * index + 1, M + 1, R));
}
void update(int lo, int hi, pair<int, int> increase, int index = 1,
long long L = 0, long long R = SZ - 1) {
push(index, L, R);
if (hi < L || R < lo) return;
if (lo <= L && R <= hi) {
lazy[index] = increase;
push(index, L, R);
return;
}
int M = (L + R) / 2;
update(lo, hi, increase, 2 * index, L, M);
update(lo, hi, increase, 2 * index + 1, M + 1, R);
pull(index);
}
void solve() {
for (int i = 0; i < (2 * SZ); i++) {
sum[i] = {0, -1};
lazy[i] = {0, -1};
}
int N, M;
cin >> N >> M;
vector<vector<pair<int, int>>> A(N + 1);
for (int i = 0; i < (M); i++) {
int X, Y, Z;
cin >> X >> Y >> Z;
A[X - 1].push_back({Y, Z});
}
set<int> vals;
for (int i = 0; i < (N); i++) {
for (auto& a : A[i]) {
vals.insert(a.first);
vals.insert(a.second);
}
}
map<int, int> comp;
for (auto& a : vals) comp[a] = (int)(comp).size();
for (int i = 0; i < (N); i++) {
for (auto& a : A[i]) {
a.second = comp[a.second];
a.first = comp[a.first];
}
}
A[N].push_back({0, (int)(comp).size() - 1});
pair<int, int> dp[N + 1];
for (int i = 0; i < (N + 1); i++) {
pair<int, int> best = {0, -1};
for (auto& a : A[i]) {
ckmax(best, query(a.first, a.second));
}
best.first++;
dp[i] = best;
best.second = i;
for (auto& a : A[i]) {
update(a.first, a.second, best);
}
}
cout << N + 1 - dp[N].first << nl;
int cur = N;
while (cur >= 0) {
int nxt = dp[cur].second;
for (int i = nxt + 1; i < (cur); i++) {
cout << i + 1 << " ";
}
cur = nxt;
}
cout << nl;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int T = 1;
while (T--) {
solve();
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int OO = (int)1e9;
const double eps = (1e-10);
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
int dcmp(double x, double y) { return fabs(x - y) <= eps ? 0 : x < y ? -1 : 1; }
double dist(double x1, double y1, double x2, double y2) {
return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
long long power(long long x, int y) {
long long temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
int n, m, k, l, a1[10000], a2[10000], x, y, bestd;
int main() {
ios::sync_with_stdio(false);
cout.precision(10);
cin >> n >> m >> k;
int dists[n + 1][m + 1];
bool ok[n + 1][m + 1];
for (int i = 0; i < (int)(n + 1); ++i)
for (int j = 0; j < (int)(m + 1); ++j) ok[i][j] = 1;
for (int i = 0; i < (int)(k); ++i) cin >> a1[i];
cin >> l;
for (int i = 0; i < (int)(l); ++i) cin >> a2[i];
sort(a1, a1 + k), sort(a2, a2 + l);
for (int i = (1); i < (int)(n + 1); ++i)
for (int j = (1); j < (int)(m + 1); ++j) dists[i][j] = i + m + 1 - j;
int cnt = 0;
while (cnt != k) {
x = y = bestd = -1;
for (int i = (1); i < (int)(n + 1); ++i)
for (int j = (1); j < (int)(m + 1); ++j)
if (ok[i][j] && dists[i][j] >= bestd && i + j <= a1[cnt])
x = i, y = j, bestd = dists[i][j];
if (x == -1) {
cout << "NO";
return 0;
}
ok[x][y] = 0, ++cnt;
}
cnt = 0;
while (cnt != l) {
x = y = -1;
for (int i = (1); i < (int)(n + 1); ++i)
for (int j = (1); j < (int)(m + 1); ++j)
if (ok[i][j] && i + m + 1 - j <= a2[cnt]) {
x = i, y = j;
break;
}
if (x == -1) {
cout << "NO";
return 0;
}
ok[x][y] = 0, ++cnt;
}
cout << "YES";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
long long dp[MAX] = {}, dp_max[MAX] = {};
void initiaize(int n) {
dp_max[n - 1] = dp[n - 1];
for (int i = n - 2; i >= 0; i--) dp_max[i] = max(dp_max[i + 1], dp[i]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, l;
cin >> n >> l;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i == 0)
dp[i] = a[i];
else
dp[i] = a[i] + dp[i - 1];
}
initiaize(n);
long long add = 0, ans = 0;
for (int i = 0; i < n; i++) {
if ((dp[i] + add) > l) {
cout << -1;
return 0;
}
if (a[i] == 0) {
if (dp[i] + add < 0) {
ans++;
add = l - dp_max[i];
if ((dp[i] + add) < 0) {
cout << -1;
return 0;
}
}
}
}
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long h1, m1, h2, m2, h, m;
scanf("%lld:%lld", &h1, &m1);
scanf("%lld:%lld", &h2, &m2);
long long ttl_mint = (h1 * 60) + m1 + (h2 * 60) + m2;
h = (ttl_mint / 2) / 60;
m = (ttl_mint / 2) % 60;
printf("%02lld:%02lld\n", h, m);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, k;
cin >> n >> k;
long long maxi = k * (k - 1);
if (n > maxi) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
long long curr = 0;
for (long long i = 0; i < n; i++) {
curr += (i % k == 0);
cout << (i % k + 1) << " " << (curr + i % k) % k + 1 << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int first[200000], second[200000];
bool jame[200000 + 1];
int main() {
int n;
scanf("%d", &n);
int ans = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", first + i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", second + i);
}
int i = n - 1, j = n - 1;
while (j >= 0 && i >= 0) {
if (second[j] == first[i]) {
--i;
--j;
} else if (jame[second[j]]) {
--j;
} else {
jame[first[i]] = true;
ans = n - i;
--i;
}
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n;
cin >> n;
long long int count = 0;
long long int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
unordered_map<int, int> mp;
for (int i = 0; i < n; i++) {
mp[arr[i]]++;
}
for (auto x : mp) {
if (x.first > 0) {
count++;
}
}
cout << count;
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests = 1;
while (tests--) {
solve();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long you[100050];
long long gg[100050];
int main() {
memset(you, 0, sizeof(you));
long long n;
cin >> n;
long long sum = 0;
for (long long i = 1; i <= n; i++) {
scanf("%I64d", &gg[i]);
sum += gg[i];
}
if ((sum % 3) != 0) {
printf("0\n");
return 0;
}
if (n == 1 || n == 2) {
printf("0\n");
return 0;
}
long long tar = sum / 3;
long long ans = 0;
if (tar != 0) {
sum = 0;
for (long long i = n; i >= 1; i--) {
sum += gg[i];
you[i] = you[i + 1];
if (sum == tar) {
you[i]++;
}
}
sum = 0;
for (long long i = 1; i <= n; i++) {
sum += gg[i];
if (sum == tar) {
ans += you[i + 1];
}
}
printf("%I64d\n", ans);
} else {
sum = 0;
long long num = 0;
for (long long i = 1; i <= n; i++) {
sum += gg[i];
if (sum == 0) {
num++;
}
}
num--;
printf("%I64d\n", num * (num - 1) / 2);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
const long double pie = 3.1415926535;
using namespace std;
long long fpow(long long n, long long p) {
long long m = 1;
while (p) {
if (p % 2) m *= n;
p >>= 1;
n *= n;
}
return m;
}
long long mfpow(long long n, long long p, long long M) {
long long m = 1;
n %= M;
while (p) {
if (p % 2) m = (m * n) % M;
n = (n * n) % M;
p >>= 1;
}
return m % M;
}
const vector<long long> days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool br1, br2, br;
char ch, ch1, ch2;
long long n, k, x, y, z, mini, maxi, l, ind, ini, sum, t, len, r, w, imini, m;
string s, s1, s2;
vector<long long> v[(int)1e6 + 100];
vector<pair<long long, long long> > edg;
bool vis[(int)1e6 + 100];
bool col[(int)1e6 + 100];
void dfs(long long x, bool y) {
if (vis[x]) {
if (col[x] != y) br = 1;
return;
}
vis[x] = 1;
col[x] = y;
for (auto i : v[x]) dfs(i, y ^ 1);
}
void solve() {
cin >> n >> m;
for (long long i = 0; i < m; i++) {
cin >> x >> y;
x--;
y--;
edg.push_back(make_pair(x, y));
v[x].push_back(y);
v[y].push_back(x);
}
dfs(0, 0);
if (br)
cout << "NO\n";
else {
cout << "YES\n";
for (auto i : edg) s.push_back('0' + col[i.first]);
cout << s << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MXN = 1e2;
const int MAXN = 1e5 + 1;
const int INF = 1e9 + 7;
const long long INFL = 1e18 + 7;
int n;
int a[MXN];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) {
if (a[i] - a[i - 1] >= 16) {
cout << a[i - 1] + 15;
return 0;
}
}
cout << min(90, a[n] + 15);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long n, m;
cin >> n >> m;
vector<long long> a(n), b(m);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (long long i = 0; i < m; i++) {
cin >> b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
vector<long long> steps_a(n, 0);
for (long long i = 1; i < n; i++) {
steps_a[i] = (a[i] - a[i - 1]) * i + steps_a[i - 1];
}
vector<long long> steps_b(m, 0);
for (long long i = m - 2; i >= 0; i--) {
steps_b[i] = (b[i + 1] - b[i]) * (m - i - 1) + steps_b[i + 1];
}
long long b_idx = m - 1;
while (b_idx >= 0 && b[b_idx] >= a[n - 1]) {
b_idx--;
}
b_idx++;
b_idx = min(b_idx, m - 1);
long long a_idx = n - 1;
long long moves = 1000000000000000000;
while (a_idx >= 0 && b_idx >= 0) {
long long a_min = a[a_idx];
long long b_max = b[b_idx];
long long steps = steps_a[a_idx] + steps_b[b_idx];
long long diff = abs(a_min - b_max);
long long a_count = a_idx + 1, b_count = m - b_idx;
if (b[b_idx] <= a[a_idx]) {
steps += 0;
} else {
steps += (diff * b_count);
}
if (steps < moves) {
moves = steps;
}
a_idx--;
while (b_idx >= 0 && b[b_idx] >= a[a_idx]) {
b_idx--;
}
b_idx++;
b_idx = min(b_idx, m - 1);
}
a_idx = 0;
while (a_idx < n && a[a_idx] <= b[0]) {
a_idx++;
}
a_idx--;
a_idx = max((long long)0, a_idx);
while (b_idx < m && a_idx < n) {
long long a_min = a[a_idx];
long long b_max = b[b_idx];
long long steps = steps_a[a_idx] + steps_b[b_idx];
long long diff = abs(a_min - b_max);
long long a_count = a_idx + 1, b_count = m - b_idx;
if (a[a_idx] > b[b_idx]) {
steps += 0;
} else
steps += (diff * a_count);
if (steps < moves) {
moves = steps;
}
b_idx++;
while (a_idx < n && a[a_idx] <= b[b_idx]) {
a_idx++;
}
a_idx--;
a_idx = max((long long)0, a_idx);
}
cout << moves << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int inf = (int)1e9;
const long long int infll = (long long int)1e18;
const double inf1 = 1e-9;
const int ss = (int)1e6 + 3;
const int base = inf;
bool pred(const pair<int, int>& i, const pair<int, int>& j) {
if (i.first == j.first)
return i.second > j.second;
else
return i.first < j.first;
}
bool pred2(const int& i, const int& j) { return i > j; }
vector<vector<int> > f;
int n;
int k;
vector<int> comp;
vector<bool> used;
long long int mx = 0;
vector<vector<bool> > e;
void dfs(int v) {
used[v] = true;
comp.push_back(v);
for (int i = 0; i < f[v].size(); ++i) {
int to = f[v][i];
if (!used[to]) dfs(to);
}
}
void find_comps() {
for (int i = 0; i < n; ++i) {
if (!used[i]) {
comp.clear();
dfs(i);
if (comp.size() > mx) {
bool t = true;
for (int j = 0; j < comp.size() - 1; ++j) {
for (int l = j + 1; l < comp.size(); ++l) {
if (e[comp[j]][comp[l]]) {
t = false;
break;
}
}
if (!t) break;
}
if (t) mx = comp.size();
}
}
}
}
int main() {
int n;
scanf("%d", &n);
vector<long long int> a(n);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
int z = 1;
vector<int> mas;
while (z < n) {
mas.push_back(z);
z *= 2;
}
mas.push_back(z);
vector<int> b(n);
z = 0;
long long int res = 0;
for (int i = 0; i < n - 1; ++i) {
z = -1;
do {
++z;
} while (i + mas[z] < n);
--z;
long long int t = a[i];
a[i + mas[z]] += t;
res += t;
printf("%d\n", res);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void init(T& x) {
x = 0;
char ch = getchar();
bool t = 0;
for (; ch > '9' || ch < '0'; ch = getchar())
if (ch == '-') t = 1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 1) + (x << 3) + (ch - 48);
if (t) x = -x;
return;
}
const int N = 1e6 + 10;
vector<int> ape[N];
int a[N], n, m;
int pre[N], suf[N], now[N], nxt[N];
bool vis[N];
int Idex = 0;
inline int getid() {
while (vis[Idex]) ++Idex;
return vis[Idex] = 1, Idex;
}
inline void Del(int l, int r) {
suf[pre[l]] = suf[r], pre[suf[r]] = pre[l];
return;
}
inline void Exit() {
puts("no");
exit(0);
}
void dfs(int l, int r) {
if (l > r) Exit();
if ((r - l + 1) & 1) Exit();
int rt = a[pre[l]];
for (int i = l; i < r; i = suf[i]) {
while (nxt[i]) {
if (nxt[i] > r) Exit();
int nx = nxt[nxt[i]];
dfs(suf[i], nxt[i]), Del(suf[i], nxt[i]);
nxt[i] = nx;
}
}
int num = 0, len = 0, lim;
for (int i = l; i < r; i = suf[i]) ++len, num += a[i] > 0;
if (num > (lim = (len + 2) / 2)) Exit();
for (int i = l; i < r; i = suf[i])
if (!a[i] && num < lim) a[i] = getid(), ++num;
for (int i = suf[l]; suf[i] <= r; i = suf[i]) {
while (suf[i] <= r && pre[i] >= l && !a[pre[i]] && a[i] && a[suf[i]])
a[pre[i]] = a[suf[i]], Del(i, suf[i]), i = pre[pre[i]];
while (suf[i] <= r && pre[i] >= l && a[pre[i]] && a[i] && !a[suf[i]])
a[suf[i]] = a[pre[i]], Del(pre[i], i), i = pre[i];
}
for (int i = l; i < r; i = suf[i])
if (!a[i]) a[i] = rt;
}
int main() {
init(n);
int tmp = 0;
m = 2 * n - 1;
for (int i = 1; i <= m; ++i) {
init(a[i]);
vis[a[i]] = 1;
if (a[i])
ape[a[i]].emplace_back(i);
else
++tmp;
}
int num = 0;
Idex = 1;
for (int i = 1; i <= n; ++i)
if (!ape[i].size()) ++num;
if (tmp < num) return puts("no"), 0;
if (n == 1) return puts("yes\n1"), 0;
if (a[1] && (a[1] == a[2] || (a[m] && a[1] != a[m]))) return puts("no"), 0;
if (!a[1] && !a[m]) a[1] = a[m] = getid();
a[1] = a[m] = a[1] | a[m];
for (int i = 1; i <= m; ++i) pre[i] = i - 1, suf[i] = i + 1;
for (int i = m; i; --i)
if (a[i]) nxt[i] = now[a[i]], now[a[i]] = i;
dfs(2, m);
puts("yes");
for (int i = 1; i <= m; ++i) printf("%d ", a[i]);
puts("");
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> d(n + 1);
for (int i = 1; i <= n - 1; ++i) {
int u, v;
cin >> u >> v;
d[u]++, d[v]++;
}
bool ok = 1;
for (int i = 1; i <= n; ++i) {
if (d[i] == 2) {
ok = 0;
break;
}
}
if (!ok) {
cout << "no"
<< "\n";
} else {
cout << "yes"
<< "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int eval(vector<pair<int, int> > a, vector<pair<int, int> > b, int x) {
int ret = 0;
while (1) {
if (a.empty()) return ret;
int n = a.size(), idx = -1, mx = 0;
for (int i = 0; i < n; i++) {
if (a[i].first <= x) {
if (mx < a[i].second) {
mx = a[i].second;
idx = i;
}
}
}
if (idx == -1) return ret;
ret++;
x += a[idx].second;
a.erase(a.begin() + idx);
swap(a, b);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, x;
cin >> n >> x;
vector<pair<int, int> > a, b;
for (int i = 0; i < n; i++) {
int t, h, m;
cin >> t >> h >> m;
pair<int, int> cur = make_pair(h, m);
if (t == 0)
a.push_back(cur);
else
b.push_back(cur);
}
cout << max(eval(a, b, x), eval(b, a, x)) << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
inline char in() {
static char buf[10001], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 10000, stdin), p1 == p2)
? EOF
: *p1++;
}
template <class Tp>
void read(register Tp &s) {
s = 0;
register bool neg = 0;
register char c;
while (!isdigit(c = in()))
if (c == '-') neg = 1;
while (s = (s << 3) + (s << 1) + c - 48, isdigit(c = in()))
;
s = (neg ? -s : s);
}
int T, n, a[1000005];
std::map<int, int> cnt;
int main() {
for (read(T); T; --T) {
cnt.clear();
read(n);
int t, m = 0;
for (int i = 1; i <= n; ++i) read(t), cnt[t]++;
bool flg = 0;
for (std::map<int, int>::iterator it = cnt.begin(); it != cnt.end(); ++it) {
if ((*it).second >= 2) a[++m] = (*it).first;
if ((*it).second >= 4) {
flg = 1;
int ans = (*it).first;
printf("%d %d %d %d\n", ans, ans, ans, ans);
break;
}
}
if (flg) continue;
int x = a[1], y = a[2];
for (int i = 3; i <= m; ++i)
if (x * a[i] < a[i - 1] * y) x = a[i - 1], y = a[i];
printf("%d %d %d %d\n", x, x, y, y);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int maxn = 5e6 + 5;
int n, q;
long long a[100005], t[100005];
pair<long long, long long> id[500005];
void up(int k, int l, int r) {
if (l == r)
id[k] = {t[l], t[l]};
else {
int mid = (l + r) / 2;
up(k * 2, l, mid);
up(k * 2 + 1, mid + 1, r);
id[k].first = max(id[k * 2].first, id[k * 2 + 1].first);
id[k].second = min(id[k * 2].second, id[k * 2 + 1].second);
}
}
long long getmax(int k, int l, int r, int u, int v) {
if (l > r || l > v || r < u) return -1e16;
if (l >= u && r <= v) return id[k].first;
int mid = (l + r) / 2;
return max(getmax(k * 2, l, mid, u, v), getmax(k * 2 + 1, mid + 1, r, u, v));
}
long long getmin(int k, int l, int r, int u, int v) {
if (l > r || l > v || r < u) return 1e16;
if (l >= u && r <= v) return id[k].second;
int mid = (l + r) / 2;
return min(getmin(k * 2, l, mid, u, v), getmin(k * 2 + 1, mid + 1, r, u, v));
}
int main() {
ios::sync_with_stdio(0);
;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
long long u;
cin >> u;
t[i] = t[i - 1] + a[i] - u;
}
up(1, 1, n);
for (; q--;) {
int u, v;
cin >> u >> v;
if (t[v] != t[u - 1]) {
cout << -1 << '\n';
continue;
}
if (getmax(1, 1, n, u, v) > t[u - 1]) {
cout << -1 << '\n';
continue;
}
cout << t[u - 1] - getmin(1, 1, n, u, v) << '\n';
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e5;
int n, w, h, s1, s2, t[mxN], x[mxN], y[mxN];
double lb, rb = 1000, mb, a[8], b[8];
struct ev {
int t;
double y1, y2, nl, nr;
};
vector<ev> es;
bool chk(double mb) {
int ct = 0;
double x1 = s1, y1 = s2, x2 = s1, y2 = s2;
auto o = [&](double x1, double y1, double x2, double y2, double x3,
double y3) {
return (x1 - x2) * (y2 - y3) < (x2 - x3) * (y1 - y2);
};
for (ev e : es) {
double ax = (e.t - ct) * mb, ay = (e.t - ct) * mb, nl = 2, nr = -1;
for (int j = 0; j < 4; swap(ax, ay), ax = -ax, ++j) {
a[j] = x1 + ax;
b[j] = y1 + ay;
a[j + 4] = x2 + ax;
b[j + 4] = y2 + ay;
}
auto ab = [&](int j1, int j2) {
if (o(0, e.y1, w, e.y2, a[j1], b[j1]) ==
o(0, e.y1, w, e.y2, a[j2], b[j2]))
return;
double c = (b[j1] * a[j2] - a[j1] * b[j2] + e.y1 * (a[j1] - a[j2])) /
((e.y2 - e.y1) * (a[j2] - a[j1]) / w - b[j2] + b[j1]) / w;
nl = min(c, nl);
nr = max(c, nr);
};
for (int j = 0; j < 4; ++j) {
ab(j, (j + 1) % 4);
ab(j + 4, (j + 1) % 4 + 4);
ab(j, j + 4);
}
nl = max(e.nl, nl);
nr = min(e.nr, nr);
if (nl > nr + 1e-9) return 0;
x1 = w * nl;
y1 = e.y2 * nl + e.y1 * (1 - nl);
x2 = w * nr;
y2 = e.y2 * nr + e.y1 * (1 - nr);
ct = e.t;
}
return 1;
}
void fk() {
cout << -1;
exit(0);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> w >> h >> s1 >> s2;
for (int i = 0; i < n; ++i) cin >> t[i] >> x[i] >> y[i];
for (int i = 0; i < n;) {
vector<array<int, 2>> ps{{x[i], y[i]}};
while (++i < n && t[i] == t[i - 1]) ps.push_back({x[i], y[i]});
sort(ps.begin(), ps.end());
ps.resize(unique(ps.begin(), ps.end()) - ps.begin());
for (int j = ps.size() - 1; j > 1; --j)
if ((ps[j][0] - ps[j - 1][0]) * (ps[j - 1][1] - ps[j - 2][1]) !=
(ps[j - 1][0] - ps[j - 2][0]) * (ps[j][1] - ps[j - 1][1]))
fk();
es.push_back(
{t[i - 1], (double)h * ps[0][0] / ps[0][1],
(double)(h * ps[0][0] - w * (h - ps[0][1])) / ps[0][1],
max((double)(h * ps[0][0] - w * ps[0][1]) / (h - ps[0][1]) / w, 0.0),
min((double)h * ps[0][0] / (h - ps[0][1]) / w, 1.0)});
if (ps.size() > 1) {
if (ps[0][1] == ps[1][1]) fk();
double a = (double)(ps[0][0] * ps[1][1] - ps[1][0] * ps[0][1]) /
(ps[1][1] - ps[0][1]) / w;
es.back().nl = max(a, es.back().nl);
es.back().nr = min(a, es.back().nr);
if (es.back().nl > es.back().nr + 1e-9) fk();
++i;
}
}
while (1) {
unsigned long long lb2, rb2, mb2;
memcpy(&lb2, &lb, 8);
memcpy(&rb2, &rb, 8);
if (rb2 - lb2 <= 1) break;
mb2 = (lb2 + rb2) / 2;
memcpy(&mb, &mb2, 8);
if (chk(mb))
rb = mb;
else
lb = mb;
}
cout << fixed << setprecision(9) << (lb + rb) / 2;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = 4 * atan(1);
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
int nCase = 0;
int dcmp(double x) {
if (fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
template <class T>
inline bool read(T &n) {
T x = 0, tmp = 1;
char c = getchar();
while ((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
if (c == EOF) return false;
if (c == '-') c = getchar(), tmp = -1;
while (c >= '0' && c <= '9') x *= 10, x += (c - '0'), c = getchar();
n = x * tmp;
return true;
}
template <class T>
inline void write(T n) {
if (n < 0) {
putchar('-');
n = -n;
}
int len = 0, data[20];
while (n) {
data[len++] = n % 10;
n /= 10;
}
if (!len) data[len++] = 0;
while (len--) putchar(data[len] + 48);
}
long long QMOD(long long x, long long k) {
long long res = 1LL;
while (k) {
if (k & 1) res = res * x % MOD;
k >>= 1;
x = x * x % MOD;
}
return res;
}
class BigInteger {
public:
const static int maxn = 2005;
int len, s[maxn];
BigInteger();
BigInteger(const char *);
BigInteger(int);
bool sign;
string toStr() const;
friend istream &operator>>(istream &, BigInteger &);
friend ostream &operator<<(ostream &, BigInteger &);
BigInteger operator=(const char *);
BigInteger operator=(int);
BigInteger operator=(const string);
bool operator>(const BigInteger &) const;
bool operator>=(const BigInteger &) const;
bool operator<(const BigInteger &) const;
bool operator<=(const BigInteger &) const;
bool operator==(const BigInteger &) const;
bool operator!=(const BigInteger &) const;
BigInteger operator+(const BigInteger &) const;
BigInteger operator++();
BigInteger operator++(int);
BigInteger operator+=(const BigInteger &);
BigInteger operator-(const BigInteger &) const;
BigInteger operator--();
BigInteger operator--(int);
BigInteger operator-=(const BigInteger &);
BigInteger operator*(const BigInteger &) const;
BigInteger operator*(const int num) const;
BigInteger operator*=(const BigInteger &);
BigInteger operator/(const BigInteger &) const;
BigInteger operator/=(const BigInteger &);
BigInteger operator%(const BigInteger &) const;
BigInteger factorial() const;
BigInteger Sqrt() const;
BigInteger pow(const BigInteger &) const;
void clean();
~BigInteger();
};
BigInteger::BigInteger() {
memset(s, 0, sizeof(s));
len = 1;
sign = 1;
}
BigInteger::BigInteger(const char *num) { *this = num; }
BigInteger::BigInteger(int num) { *this = num; }
string BigInteger::toStr() const {
string res;
res = "";
for (int i = 0; i < len; i++) res = (char)(s[i] + '0') + res;
if (res == "") res = "0";
if (!sign && res != "0") res = "-" + res;
return res;
}
istream &operator>>(istream &in, BigInteger &num) {
string str;
in >> str;
num = str;
return in;
}
ostream &operator<<(ostream &out, BigInteger &num) {
out << num.toStr();
return out;
}
BigInteger BigInteger::operator=(const char *num) {
memset(s, 0, sizeof(s));
char a[maxn] = "";
if (num[0] != '-')
strcpy(a, num);
else
for (int i = 1; i < strlen(num); i++) a[i - 1] = num[i];
sign = !(num[0] == '-');
len = strlen(a);
for (int i = 0; i < strlen(a); i++) s[i] = a[len - i - 1] - 48;
return *this;
}
BigInteger BigInteger::operator=(int num) {
if (num < 0)
sign = 0, num = -num;
else
sign = 1;
char temp[maxn];
sprintf(temp, "%d", num);
*this = temp;
return *this;
}
BigInteger BigInteger::operator=(const string num) {
const char *tmp;
tmp = num.c_str();
*this = tmp;
return *this;
}
bool BigInteger::operator<(const BigInteger &num) const {
if (sign ^ num.sign) return num.sign;
if (len != num.len) return len < num.len;
for (int i = len - 1; i >= 0; i--)
if (s[i] != num.s[i])
return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));
return !sign;
}
bool BigInteger::operator>(const BigInteger &num) const { return num < *this; }
bool BigInteger::operator<=(const BigInteger &num) const {
return !(*this > num);
}
bool BigInteger::operator>=(const BigInteger &num) const {
return !(*this < num);
}
bool BigInteger::operator!=(const BigInteger &num) const {
return *this > num || *this < num;
}
bool BigInteger::operator==(const BigInteger &num) const {
return !(num != *this);
}
BigInteger BigInteger::operator+(const BigInteger &num) const {
if (sign ^ num.sign) {
BigInteger tmp = sign ? num : *this;
tmp.sign = 1;
return sign ? *this - tmp : num - tmp;
}
BigInteger result;
result.len = 0;
int temp = 0;
for (int i = 0; temp || i < (max(len, num.len)); i++) {
int t = s[i] + num.s[i] + temp;
result.s[result.len++] = t % 10;
temp = t / 10;
}
result.sign = sign;
return result;
}
BigInteger BigInteger::operator++() {
*this = *this + 1;
return *this;
}
BigInteger BigInteger::operator++(int) {
BigInteger old = *this;
++(*this);
return old;
}
BigInteger BigInteger::operator+=(const BigInteger &num) {
*this = *this + num;
return *this;
}
BigInteger BigInteger::operator-(const BigInteger &num) const {
BigInteger b = num, a = *this;
if (!num.sign && !sign) {
b.sign = 1;
a.sign = 1;
return b - a;
}
if (!b.sign) {
b.sign = 1;
return a + b;
}
if (!a.sign) {
a.sign = 1;
b = BigInteger(0) - (a + b);
return b;
}
if (a < b) {
BigInteger c = (b - a);
c.sign = false;
return c;
}
BigInteger result;
result.len = 0;
for (int i = 0, g = 0; i < a.len; i++) {
int x = a.s[i] - g;
if (i < b.len) x -= b.s[i];
if (x >= 0)
g = 0;
else {
g = 1;
x += 10;
}
result.s[result.len++] = x;
}
result.clean();
return result;
}
BigInteger BigInteger::operator*(const BigInteger &num) const {
BigInteger result;
result.len = len + num.len;
for (int i = 0; i < len; i++)
for (int j = 0; j < num.len; j++) result.s[i + j] += s[i] * num.s[j];
for (int i = 0; i < result.len; i++) {
result.s[i + 1] += result.s[i] / 10;
result.s[i] %= 10;
}
result.clean();
result.sign = !(sign ^ num.sign);
return result;
}
BigInteger BigInteger::operator*(const int num) const {
BigInteger x = num;
BigInteger z = *this;
return x * z;
}
BigInteger BigInteger::operator*=(const BigInteger &num) {
*this = *this * num;
return *this;
}
BigInteger BigInteger::operator/(const BigInteger &num) const {
BigInteger ans;
ans.len = len - num.len + 1;
if (ans.len < 0) {
ans.len = 1;
return ans;
}
BigInteger divisor = *this, divid = num;
divisor.sign = divid.sign = 1;
int k = ans.len - 1;
int j = len - 1;
while (k >= 0) {
while (divisor.s[j] == 0) j--;
if (k > j) k = j;
char z[maxn];
memset(z, 0, sizeof(z));
for (int i = j; i >= k; i--) z[j - i] = divisor.s[i] + '0';
BigInteger dividend = z;
if (dividend < divid) {
k--;
continue;
}
int key = 0;
while (divid * key <= dividend) key++;
key--;
ans.s[k] = key;
BigInteger temp = divid * key;
for (int i = 0; i < k; i++) temp = temp * 10;
divisor = divisor - temp;
k--;
}
ans.clean();
ans.sign = !(sign ^ num.sign);
return ans;
}
BigInteger BigInteger::operator/=(const BigInteger &num) {
*this = *this / num;
return *this;
}
BigInteger BigInteger::operator%(const BigInteger &num) const {
BigInteger a = *this, b = num;
a.sign = b.sign = 1;
BigInteger result, temp = a / b * b;
result = a - temp;
result.sign = sign;
return result;
}
BigInteger BigInteger::pow(const BigInteger &num) const {
BigInteger result = 1;
for (BigInteger i = 0; i < num; i++) result = result * (*this);
return result;
}
BigInteger BigInteger::factorial() const {
BigInteger result = 1;
for (BigInteger i = 1; i <= *this; i++) result *= i;
return result;
}
void BigInteger::clean() {
if (len == 0) len++;
while (len > 1 && s[len - 1] == '\0') len--;
}
BigInteger BigInteger::Sqrt() const {
if (*this < 0) return -1;
if (*this <= 1) return *this;
BigInteger l = 0, r = *this, mid;
while (r - l > 1) {
mid = (l + r) / 2;
if (mid * mid > *this)
r = mid;
else
l = mid;
}
return l;
}
BigInteger::~BigInteger() {}
const int maxn = 2e5 + 10;
set<int> person[maxn];
int maxbit[maxn];
set<pair<int, int> > all;
int n;
int main(int argc, const char *argv[]) {
cin >> n;
for (int i = 1; i <= n; ++i) {
int a, b;
cin >> a >> b;
person[a].insert(b);
maxbit[a] = b;
}
for (int i = 1; i <= n; ++i) {
if (maxbit[i]) {
all.insert(pair<int, int>(maxbit[i], i));
}
}
int q;
cin >> q;
while (q--) {
int k;
cin >> k;
vector<int> vec;
for (int i = 1; i <= k; ++i) {
int x;
cin >> x;
vec.push_back(x);
if (maxbit[x]) {
all.erase(pair<int, int>(maxbit[x], x));
}
}
int Size = (int)all.size();
if (Size == 0) {
printf("0 0\n");
} else if (Size == 1) {
set<pair<int, int> >::iterator it = all.begin();
int id = it->second;
printf("%d %d\n", id, *person[id].begin());
} else {
set<pair<int, int> >::iterator it = all.end();
it--;
int Bigid = it->second;
it--;
int num = it->first;
printf("%d %d\n", Bigid, *person[Bigid].lower_bound(num));
}
for (int x : vec) {
if (maxbit[x]) {
all.insert(pair<int, int>(maxbit[x], x));
}
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int s, i, e, cnt = 0;
cin >> s >> i >> e;
int _ = ceil((e - i + s) / 2.0);
cout << min(e + 1, max(_, 0)) << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int gi() {
char cc = getchar();
int cn = 0, flus = 1;
while (cc < '0' || cc > '9') {
if (cc == '-') flus = -flus;
cc = getchar();
}
while (cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar();
return cn * flus;
}
const int N = 5e5 + 5;
const int M = 50 + 5;
int n, top, cnt, num;
struct node {
int x, y;
} st[N];
vector<int> col[N], rev[N], lst[M];
vector<int> merge(vector<int> x, vector<int> y) {
int m = x.size();
vector<int> ans;
ans.clear();
for (int i = 0; i < m; ++i) {
st[++cnt] = (node){x[i], y[i]};
ans.push_back(x[i]), ans.push_back(y[i]);
}
return ans;
}
void output() {
printf("%d\n", cnt);
for (register int i = (1); i <= (cnt); ++i)
printf("%d %d\n", st[i].x, st[i].y);
}
signed main() {
n = gi();
if (n <= 2) {
puts("0");
exit(0);
}
for (register int i = (1); i <= (n); ++i) col[i].push_back(i);
top = n;
while (1) {
int id = 0, bef = top;
while (top >= 2) {
++id;
rev[id] = merge(col[top], col[top - 1]);
top -= 2;
}
if (top) lst[++num] = col[top];
if (id == 0) break;
for (register int i = (1); i <= (bef); ++i) col[i].clear();
top = id;
for (register int i = (1); i <= (id); ++i) col[i] = rev[i], rev[i].clear();
}
if (num <= 2)
output();
else {
int siz = lst[1].size(), bef = 0;
for (register int i = 2; i < num; ++i) {
while (siz < lst[i].size()) {
vector<int> tk;
tk.clear();
for (register int j = 0; j < siz; ++j) {
tk.push_back(lst[1][j]), tk.push_back(lst[num][j + bef]);
st[++cnt] = (node){lst[1][j], lst[num][j + bef]};
}
lst[1].clear(), lst[1] = tk;
bef += siz;
siz *= 2;
}
lst[1] = merge(lst[1], lst[i]);
siz *= 2;
}
output();
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
vector<int> v[maxn];
int path[maxn], top = 0, child[maxn];
bool vis[maxn];
int pos[maxn];
void dfs(int u) {
path[top++] = u;
pos[u] = top - 1;
int len = v[u].size();
for (int i = 0; i < len; i++) {
int to = v[u][i];
if (!vis[to]) {
dfs(to);
child[u] += child[to];
}
}
}
int main() {
int n, k, d;
while (~scanf("%d%d", &n, &k)) {
for (int i = 1; i <= n; i++) child[i] = 1;
top = 0;
for (int i = 1; i <= n; i++) v[i].clear();
for (int i = 2; i <= n; i++) {
scanf("%d", &d);
v[d].push_back(i);
}
dfs(1);
int q1, q2;
while (k--) {
scanf("%d%d", &q1, &q2);
int poss = pos[q1];
if (poss + q2 - 1 > poss + child[q1] - 1) {
puts("-1");
} else {
printf("%d\n", path[poss + q2 - 1]);
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
long long int k, x;
long long int m = 0;
for (int i = 0; i < n; i++) {
cin >> k >> x;
if (k == 1)
cout << x << endl;
else {
m = x + 9 * (k - 1);
cout << m << endl;
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MAX_N = 1e5 + 5, MOD = (1LL << 32), MAX_P = 300 + 1;
int n, p, q, ans, PE;
int choose1[MAX_P][MAX_N];
int choose2[MAX_P][MAX_P];
int dp[MAX_P][MAX_P];
vector<int> Sor;
int gcd(int a, int b) { return (!b ? a : gcd(b, a % b)); }
void process_choose() {
choose2[0][n - p - 1 - PE] = 1;
for (int i = 1; i <= min(p, n - p - 1); i++) {
Sor.push_back(n - p - i);
sort(Sor.begin(), Sor.end());
reverse(Sor.begin(), Sor.end());
while (Sor.back() == 1) Sor.pop_back();
int res = i;
for (int j = 0; j < Sor.size(); j++) {
int h = gcd(Sor[j], res);
res /= h;
Sor[j] /= h;
}
choose2[i][n - p - 1 - PE] = 1;
for (int j = 0; j < Sor.size(); j++) choose2[i][n - p - 1 - PE] *= Sor[j];
}
for (int i = 0; i <= p; i++)
for (int j = n - p; j <= n; j++) {
choose2[i][j - PE] =
(i <= j ? (choose2[i - 1][j - 1 - PE] + choose2[i][j - 1 - PE]) : 0);
}
}
void process_dp() {
for (int i = 0; i <= p; i++) {
dp[i][0] = 1;
for (int j = 1; j <= i; j++)
for (int k = 1; k <= min(i, n - p + i - 1); k++)
dp[i][j] = (dp[i][j] + (choose2[k][n - p + i - PE] * dp[i - k][j - 1]));
}
}
int main() {
scanf("%d %d %d", &n, &p, &q);
p = min(n - 1, p);
PE = n - p - 10;
process_choose();
process_dp();
choose1[0][0] = 1;
for (int t = 1; t <= q; t++) {
int res = 0;
for (int k = 0; k <= p; k++) {
choose1[k][t] =
(k <= t ? (choose1[k][t - 1] + (k ? choose1[k - 1][t - 1] : 0)) : 0);
res = (res + (choose1[k][t] * dp[p][k]));
}
ans = (ans ^ (res * t));
}
printf("%lld\n", (ans * 1LL % MOD + MOD) % MOD);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
template <typename X>
void MA(X& a, X b) {
a = max(a, b);
}
template <typename X>
void MI(X& a, X b) {
a = min(a, b);
}
template <typename X>
void clr(X& x, int a) {
memset(x, a, sizeof(x));
};
int cond = 0, multi = 0, gcj = 0;
int N;
const int MAXN = 111;
vector<array<int, 2> > ED[MAXN];
long double wsp[MAXN];
array<int, 3> res[MAXN];
int numkr = 0;
void go(int u, int p, long double val) {
int kr = 0;
kr += ED[u].size();
{
if (cond)
cerr << 38 << " "
<< "u << \" \" << kr << \" \" << val"
<< " " << u << " " << kr << " " << val << endl;
};
val += 2.0 / kr;
while (val >= 2.0) val -= 2.0;
for (array<int, 2> ed : ED[u])
if (ed[0] != p) {
int v = ed[0];
int idx = ed[1];
{
if (cond)
cerr << 45 << " "
<< "u << \" \" << v << \" \" << val"
<< " " << u << " " << v << " " << val << endl;
};
go(v, u, 1 + val);
if (val <= 1.0) {
res[numkr] = array<int, 3>({v, u, idx});
wsp[numkr] = 1.0 - val;
} else {
res[numkr] = array<int, 3>({u, v, idx});
wsp[numkr] = 2.0 - val;
}
numkr++;
val += 2.0 / kr;
while (val >= 2.0) val -= 2.0;
}
}
void solve() {
cin >> N;
for (auto i = (1); i <= (N - 1); ++i) {
int u, v;
cin >> u >> v;
ED[u].push_back(array<int, 2>({v, i}));
ED[v].push_back(array<int, 2>({u, i}));
}
go(1, -1, 0);
cout << N - 1 << "\n";
for (auto i = 0; i < (N - 1); ++i) {
cout << 1 << " " << res[i][2] << " " << res[i][0] << " " << res[i][1] << " "
<< wsp[i] << "\n";
}
}
int main(int argc, char** argv) {
ios::sync_with_stdio(false), cin.tie(0),
cond = argc >= 2 && argv[1][0] == 'q' ? 1 << 30 : 0;
cout.setf(ios::fixed), cout.precision(10);
int t;
if (multi || gcj)
cin >> t;
else
t = 1;
for (auto i = (1); i <= (t); ++i) {
if (cond) cerr << 84 << " " << i << endl;
if (gcj) cout << "Case #" << i << ": ";
solve();
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int BITS = 31;
using namespace std;
const int N = 100005;
int n;
long long dem[N], inc[N], v[N], t[N], sums[N];
int lowest, highest, mid, curr;
int main() {
cin >> n;
int i;
for (i = 1; i <= n; i++) {
cin >> v[i];
}
for (i = 1; i <= n; i++) {
cin >> t[i];
}
for (i = 1; i <= n; i++) {
sums[i] = sums[i - 1] + t[i];
}
for (i = 1; i <= n; i++) {
lowest = i;
highest = n;
curr = i - 1;
while (highest >= lowest) {
mid = (lowest + highest) >> 1;
if (sums[mid] - sums[i - 1] <= v[i]) {
curr = mid;
lowest = mid + 1;
} else
highest = mid - 1;
}
if (curr >= i) {
dem[i] += 1;
dem[curr + 1] -= 1;
}
inc[curr + 1] = inc[curr + 1] + v[i] - (sums[curr] - sums[i - 1]);
}
for (i = 1; i <= n; i++) {
dem[i] = dem[i] + dem[i - 1];
cout << dem[i] * t[i] + inc[i] << " ";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
int dir[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
long long pow_mod(long long a, long long n, long long m) {
if (n == 0) return 1;
long long x = pow_mod(a, n >> 1, m);
long long ans = x * x % m;
if (n & 1) ans = ans * a % m;
return ans;
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
int n, a[500010] = {0}, res = 0, l = -1, r = -1;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n - 1; i++) {
if (a[i] != a[i - 1] && a[i] != a[i + 1]) {
if (l == -1 && r == -1) {
l = i;
r = i;
} else
r++;
} else {
if (l == -1 && r == -1)
continue;
else {
res = max(res, (r - l) / 2 + 1);
if ((r - l) % 2 == 0)
for (int j = l; j <= r; j++) a[j] = a[l - 1];
else
for (int j = 0; j <= (r - l) / 2; j++) {
a[l + j] = a[l - 1];
a[r - j] = a[r + 1];
}
l = -1;
r = -1;
}
}
}
if (l != -1 && r != -1) {
res = max(res, (r - l) / 2 + 1);
if ((r - l) % 2 == 0)
for (int j = l; j <= r; j++) a[j] = a[l - 1];
else
for (int j = 0; j <= (r - l) / 2; j++) {
a[l + j] = a[l - 1];
a[r - j] = a[r + 1];
}
}
printf("%d\n", res);
for (int i = 0; i < n; i++)
if (i == 0)
printf("%d", a[i]);
else
printf(" %d", a[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void MAZEN() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
MAZEN();
int t;
cin >> t;
while (t--) {
long long a, b, c, d, z;
cin >> a >> b >> c >> d;
z = a / d;
cout << z + (z / b) * c << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int mat[100][100], mas[100][100];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> mat[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> mas[i][j];
}
}
bool sw = true;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
int mn = min(mat[i][j], mas[i][j]);
int mx = max(mat[i][j], mas[i][j]);
if (mn <= mat[i][j - 1] || mn <= mat[i - 1][j]) {
mat[i][j] = mx;
mas[i][j] = mn;
} else {
mat[i][j] = mn;
mas[i][j] = mx;
}
if (mat[i][j] <= mat[i][j - 1] || mat[i][j] <= mat[i - 1][j] ||
mas[i][j] <= mas[i][j - 1] || mas[i][j] <= mas[i - 1][j]) {
sw = false;
}
}
}
if (sw) {
cout << "Possible\n";
} else {
cout << "Impossible\n";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long Nmax = 200010;
struct edge {
long long u;
long long v;
long long w;
bool ok;
} e[Nmax];
long long n, m, cnt;
long long l[Nmax], h[Nmax];
vector<pair<long long, long long> > adj[Nmax], ad[Nmax];
long long a[Nmax];
bool cmp(edge a, edge b) { return a.w < b.w; }
long long Find(long long x) {
if (x == l[x]) return x;
l[x] = Find(l[x]);
return l[x];
}
void Union(long long x, long long y) {
long long a = Find(x), b = Find(y);
if (h[a] < h[b]) {
h[b] += h[a];
l[a] = b;
} else {
h[a] += h[b];
l[b] = a;
}
}
long long dinh_thua[Nmax], dem;
long long d[50][Nmax];
bool kt[Nmax];
void dijkstra(long long k) {
priority_queue<pair<long long, long long> > pq;
long long tmp = dinh_thua[k];
for (int i = 1; i <= n; i++) d[tmp][i] = 1e18;
d[tmp][k] = 0;
pq.push(pair<long long, long long>(0, k));
while (!pq.empty()) {
long long u = pq.top().second;
long long w = -pq.top().first;
pq.pop();
if (w != d[tmp][u]) continue;
for (int i = 0; i < ad[u].size(); i++) {
long long v = ad[u][i].first;
long long w = ad[u][i].second;
if (d[tmp][u] + w < d[tmp][v]) {
d[tmp][v] = d[tmp][u] + w;
pq.push(pair<long long, long long>(-d[tmp][v], v));
}
}
}
}
long long times, pa[Nmax], tt[Nmax], head[Nmax], hldd[Nmax], pos[Nmax];
long long sz[Nmax], id;
void dfs(long long u, long long p) {
long long po = 0, sconmax = 0;
sz[u] = 1;
pa[u] = p;
if (adj[u].size() == 0) return;
for (int i = 0; i <= adj[u].size() - 1; i++) {
long long v = adj[u][i].first;
long long w = adj[u][i].second;
if (v == p) continue;
a[v] = w;
dfs(v, u);
sz[u] += sz[v];
if (sz[v] > sconmax) {
sconmax = sz[v];
po = i;
}
}
swap(adj[u][0], adj[u][po]);
}
void hld(long long u, long long p) {
times++;
pos[u] = times;
tt[times] = u;
for (int i = 0; i <= adj[u].size() - 1; i++) {
long long v = adj[u][i].first;
if (v == p) continue;
if (i == 0) {
hldd[v] = hldd[u];
head[v] = head[u];
hld(v, u);
} else {
id++;
hldd[v] = id;
head[v] = v;
hld(v, u);
}
}
}
long long st[400010];
void build(long long id, long long l, long long r) {
if (l == r) {
if (l == 1) {
st[id] = 0;
return;
}
st[id] = a[tt[l]];
return;
}
long long mid = (l + r) / 2;
build(id * 2, l, mid);
build(id * 2 + 1, mid + 1, r);
st[id] = st[id * 2] + st[id * 2 + 1];
return;
}
long long get(long long id, long long l, long long r, long long u,
long long v) {
if (u > r or v < l) {
return 0;
}
if (u <= l and r <= v) return st[id];
long long mid = (l + r) / 2;
return get(id * 2, l, mid, u, v) + get(id * 2 + 1, mid + 1, r, u, v);
}
long long solve(long long u, long long v) {
long long res = 0;
while (hldd[u] != hldd[v]) {
if (hldd[u] > hldd[v]) {
long long tg = get(1, 1, n, pos[head[u]], pos[u]);
res += tg;
u = head[u];
u = pa[u];
} else {
long long tg = get(1, 1, n, pos[head[v]], pos[v]);
res += tg;
v = head[v];
v = pa[v];
}
}
if (pos[u] > pos[v]) {
long long tg = get(1, 1, n, pos[v] + 1, pos[u]);
res += tg;
}
if (pos[v] > pos[u]) {
long long tg = get(1, 1, n, pos[u] + 1, pos[v]);
res += tg;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> e[i].u >> e[i].v >> e[i].w;
ad[e[i].u].push_back(pair<long long, long long>(e[i].v, e[i].w));
ad[e[i].v].push_back(pair<long long, long long>(e[i].u, e[i].w));
}
for (int i = 1; i <= n; i++) {
l[i] = i;
h[i] = 1;
}
sort(e + 1, e + m + 1, cmp);
for (int i = 1; i <= m; i++) {
long long u = e[i].u;
long long v = e[i].v;
long long w = e[i].w;
if (Find(u) != Find(v)) {
Union(u, v);
cnt++;
adj[u].push_back(pair<long long, long long>(v, w));
adj[v].push_back(pair<long long, long long>(u, w));
e[i].ok = true;
}
if (cnt == n - 1) break;
}
for (int i = 1; i <= m; i++) {
if (e[i].ok == true) continue;
long long u = e[i].u;
long long v = e[i].v;
long long w = e[i].w;
kt[u] = true;
kt[v] = true;
}
for (int i = 1; i <= n; i++) {
if (kt[i] == true) {
dem++;
dinh_thua[i] = dem;
dijkstra(i);
}
}
dfs(1, 0);
head[1] = 1;
hldd[1] = 1;
id = 1;
hld(1, 0);
build(1, 1, n);
long long q;
cin >> q;
while (q--) {
long long u, v;
cin >> u >> v;
long long res = solve(u, v);
for (int i = 1; i <= dem; i++) {
res = min(res, d[i][u] + d[i][v]);
}
cout << res << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, l, r, a[100005], b[100005], p[100005];
int main() {
cin >> n >> l >> r;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
int bmin = 2000000000, bmax = -2000000000, d1 = 0, d2 = 0;
for (int i = 1; i <= n; ++i) {
cin >> p[i];
b[i] = a[i] + p[i];
if (bmin > b[i]) {
bmin = b[i];
}
if (bmax < b[i]) {
bmax = b[i];
}
}
if (bmin < l) {
d1 = l - bmin;
}
if (bmax > r) {
d2 = r - bmax;
}
if (bmin + d1 + d2 < l || bmax + d1 + d2 > r) {
cout << "-1\n";
return 0;
}
for (int i = 1; i <= n; ++i) {
cout << b[i] + d1 + d2 << ' ';
}
cout << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int lmt = 10000000;
long long power(long long a, long long b) {
long long id = 1;
while (b > 0) {
if (b & 1LL) id = ((id % 1000000007) * (a % 1000000007)) % 1000000007;
a = ((a % 1000000007) * (a % 1000000007)) % 1000000007;
b = b >> 1LL;
}
return id;
}
long long brute(long long n, long long m) {
long long ans = 0;
for (int i = 1; i <= m; i++) {
ans += (n % i);
ans %= 1000000007;
}
return ans;
}
long long getAPSum(long long a, long long n, long long d) {
long long ans = 2LL * a;
ans %= 1000000007;
long long loc = (((n - 1LL) % 1000000007) + 1000000007) % 1000000007;
loc = ((loc % 1000000007) * (d % 1000000007)) % 1000000007;
ans += loc;
ans = ((ans % 1000000007) + 1000000007) % 1000000007;
ans = ((ans % 1000000007) * (n % 1000000007)) % 1000000007;
ans *= power(2, 1000000007 - 2);
ans %= 1000000007;
return ans;
}
int main() {
long long n, m;
cin >> n >> m;
long long cur = m;
long long ans = 0;
if (m > n) {
long long loc = (((m - n) % 1000000007) + 1000000007) % 1000000007;
loc = ((loc % 1000000007) * (n % 1000000007)) % 1000000007;
loc %= 1000000007;
ans += loc;
ans %= 1000000007;
cur = n;
}
while (cur > lmt) {
long long d = n / cur;
long long a = n % cur;
long long nn = cur - (n / (d + 1));
ans += getAPSum(a, nn, d);
ans %= 1000000007;
cur = n / (d + 1);
}
ans += brute(n, cur);
ans %= 1000000007;
printf("%lld\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
void ASS(bool bb) {
if (!bb) {
++*(int*)0;
}
}
#pragma comment(linker, "/STACK:106777216")
struct S2 {
unsigned a[4];
S2() { memset(a, 0, sizeof(a)); }
void set(int x) { a[x >> 5] |= 1 << (x & 31); }
bool operator<(const S2& s) const { return memcmp(a, s.a, sizeof(a)) < 0; }
};
struct S {
int x;
long long m;
S2 s2;
bool operator<(const S& s) const { return x > s.x; }
};
int main() {
int n, k, t;
cin >> n >> k >> t;
vector<S> v;
for (int i = 0; i < (int)(k); i++) {
S s;
int x, y;
cin >> x >> y >> s.x;
x--;
y--;
s.m = (1LL << x) + (1LL << (y + n));
v.push_back(s);
}
sort(v.begin(), v.end());
priority_queue<S> q;
{
S s;
s.m = 0;
s.x = 0;
q.push(s);
}
map<S2, int> st;
vector<int> all;
while (q.size() > 0 && st.size() < t + 10000) {
if (q.empty()) break;
S s = q.top();
q.pop();
all.push_back(s.x);
for (int i = 0; i < (int)(k); i++)
if ((s.m & v[i].m) == 0) {
S t = s;
t.m |= v[i].m;
t.x += v[i].x;
t.s2.set(i);
int& g = st[t.s2];
if (g == 0) {
g = 1;
q.push(t);
}
}
}
while (!q.empty()) {
all.push_back(q.top().x);
q.pop();
}
sort(all.begin(), all.end());
cout << all[t - 1] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a[250 + 5];
int main() {
int T_T;
cin >> T_T;
while (T_T--) {
for (int i = 1; i <= 250; i++) cin >> a[i];
sort(a + 1, a + 250 + 1);
int mid = (a[124] + a[125]) / 2;
int l = mid - mid / 2;
int r = mid + mid / 2;
double c1 = 0, c2 = 0;
for (int i = 1; i <= 250; i++)
if (a[i] >= l && a[i] <= r)
c1++;
else
c2++;
if (c2 == 0) c2 = 0.00001;
if (c1 / c2 <= 3)
cout << "uniform" << endl;
else
cout << "poisson" << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void cmax(T& x, T y) {
if (x < y) x = y;
}
template <typename T>
void cmin(T& x, T y) {
if (x > y) x = y;
}
inline int read() {
int f = 1, x = 0;
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 f * x;
}
const int N = 8e6, M = 2e5 + 1;
int n, m, tot, ls[N], rs[N], T[N];
long long t1[N], t2[N], p[N];
inline void add(int& rt, int lst, int l, int r, int L, int R, int a, int b) {
rt = ++tot;
ls[rt] = ls[lst], rs[rt] = rs[lst], t1[rt] = t1[lst], t2[rt] = t2[lst];
if (L <= l && r <= R) {
t1[rt] += a, t2[rt] += b;
return;
}
int mid = (l + r) >> 1;
if (L <= mid) add(ls[rt], ls[lst], l, mid, L, R, a, b);
if (mid < R) add(rs[rt], rs[lst], mid + 1, r, L, R, a, b);
}
inline long long query(int rt, int l, int r, int k) {
if (l == r) return k * t1[rt] + t2[rt];
int mid = (l + r) >> 1;
if (mid >= k) return query(ls[rt], l, mid, k) + k * t1[rt] + t2[rt];
return query(rs[rt], mid + 1, r, k) + k * t1[rt] + t2[rt];
}
int main() {
n = read();
for (int i = (1), iend = (n); i <= iend; i++) {
int xa = read(), xb = read(), ya = read(), a = read(), b = read(),
yb = read();
add(T[i], T[i - 1], 0, M, 0, xa, 0, ya);
add(T[i], T[i], 0, M, xa + 1, xb, a, b);
add(T[i], T[i], 0, M, xb + 1, M, 0, yb);
p[i] = p[i - 1] + yb;
}
m = read();
long long lst = 0;
for (int i = (1), iend = (m); i <= iend; i++) {
long long l = read(), r = read(), x = read();
x = (x + lst) % 1000000000;
if (x > M) x = M;
printf("%lld\n", lst = (query(T[r], 0, M, x) - query(T[l - 1], 0, M, x)));
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int R, D, n, m, d, h[100001];
vector<int> adj[N];
bool ans[N], mark[N];
void DFS(int u, int p = 0) {
h[u] = h[p] + 1;
ans[u] &= (h[u] <= d);
if (mark[u] && h[u] > D) {
D = h[u];
R = u;
}
for (int i = 0; i < adj[u].size(); ++i) {
int v = adj[u][i];
if (v != p) DFS(v, u);
}
}
int main() {
cin >> n >> m >> d;
memset(ans, true, sizeof(ans));
h[0] = -1;
int a, b, i;
D = 0;
for (i = 0; i < m; ++i) {
cin >> R, mark[R] = true;
}
for (i = 0; i < n - 1; ++i) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
DFS(R);
DFS(R);
DFS(R);
int ret = 0;
for (i = 1; i <= n; ++i) {
if (ans[i]) ++ret;
}
cout << ret << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[305][305];
int cnt = 1, tmp = 0;
for (int i = (int)0; i < (int)n; i++) {
if (tmp % 2) {
for (int j = (int)0; j < (int)n; j++) {
arr[i][j] = cnt;
cnt++;
}
} else {
for (int j = n - 1; j >= 0; j--) {
arr[i][j] = cnt;
cnt++;
}
}
tmp++;
}
for (int i = (int)0; i < (int)n; i++) {
for (int j = (int)0; j < (int)n; j++) {
cout << arr[j][i] << ' ';
}
cout << '\n';
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, m, sum = 0;
cin >> n >> m;
long long int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < m) {
cout << sum << endl;
} else {
cout << m << endl;
}
}
}
| 0 |
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
/*
10
2 4 2 2 8 4 8 2 6 2
YES
1 8 1
*/
void test_case(){
int n;
cin>>n;
vector<int>a(n),pmax(n),smax(n),pmind(n,-1),smind(n,n);
unordered_map<ll,ll>fst,lst;
int maxi=0, mini=1e9+2, mini_ind=-1;
for(int i=0; i<n; i++){
cin>>a[i];
maxi=max(maxi,a[i]);
pmax[i]=maxi;
if(fst.find(a[i])!=fst.end()){
lst[a[i]]=i;
}else{
fst[a[i]]=i;
}
}
maxi=0, mini=1e9+3, mini_ind=n;
for(int i=n-1; i>=0; i--){
maxi=max(maxi,a[i]);
smax[i]=maxi;
}
stack<pair<int,int>>stk,stk2;
for(int i=0; i<n; i++){
if(stk.empty()){
stk.push(make_pair(a[i],i));
continue;
}
while(!stk.empty() && stk.top().first>=a[i]){
if(stk.top().first==a[i] && fst[a[i]]==stk.top().second)break;
stk.pop();
}
if(!stk.empty())pmind[i]=stk.top().second;
stk.push(make_pair(a[i],i));
}
for(int i=n-1; i>=0; i--){
if(stk2.empty()){
stk2.push(make_pair(a[i],i));
continue;
}
while(!stk2.empty() && stk2.top().first>=a[i]){
if(stk2.top().first==a[i] && lst[a[i]]==stk2.top().second)break;
stk2.pop();
}
if(!stk2.empty())smind[i]=stk2.top().second;
stk2.push(make_pair(a[i],i));
}
for(int i=1; i<n-1; i++){
if(fst[a[i]]==i || lst[a[i]]==i)continue;
int minl=pmind[i], minr=smind[i];
int maxtl= minl!=-1 ? pmax[minl] : 0, maxtr= minr!=n ? smax[minr] : 0;
if(maxtl == a[i] && maxtr == a[i]){
cout<<"YES"<<endl;
cout<<minl+1<<" "<<minr-minl-1<<" "<<n-minr<<endl;
return;
}
}
cout<<"NO"<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
// t=1;
cin>>t;
while(t--){
test_case();
}
return 0;
} | 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int temp;
string s;
cin >> s;
for (int i = 0; i < s.length(); i = i + 2) {
for (int j = i + 2; j < s.length(); j = j + 2) {
if (s[i] > s[j]) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
cout << s;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
in >> a.first >> a.second;
return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
out << a.first << " " << a.second;
return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
if (b > a) a = b;
return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
if (b < a) a = b;
return a;
}
const long long fx[] = {+1, -1, +0, +0};
const long long fy[] = {+0, +0, +1, -1};
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
void solve() {
long long n;
cin >> n;
if (n == 2 || n == 3) {
cout << -1 << "\n";
return;
}
string a = "3 1 4 2";
long long x = 5;
while (x <= n) {
if (x % 2 == 1)
a = to_string(x) + ' ' + a;
else {
a = a + ' ' + to_string(x);
}
x++;
}
cout << a << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline long long gi() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
void bye() {
puts("-1");
exit(0);
}
long long mod7 = 1e9 + 7;
long long inf = INT_MAX - 10;
long long ncase = 1;
long long N, M;
string str;
const int mxn = 5e5 + 5;
vector<int> A, ans;
vector<int> G[mxn];
void pre() { return; }
void init() {
N = gi();
A = vector<int>(N);
ans = vector<int>(N + 1);
for (int i = 0; i < N; ++i) A[i] = gi() - 1;
}
int tag;
void dfs(int n) {
for (int i : G[n]) dfs(i);
ans[n] = tag++;
}
void sol() {
for (int i = 0; i < N; ++i)
if (A[i] == -2) A[i] = i + 1;
vector<int> stk;
for (int i = 0; i < N; ++i) {
if (stk.empty())
stk.push_back(A[i]);
else {
if (stk.back() == i) stk.pop_back();
if (stk.empty()) stk.push_back(A[i]);
if (A[i] > stk.back()) return puts("-1"), void();
if (A[i] < stk.back()) stk.push_back(A[i]);
}
}
for (int i = 0; i <= N; ++i) G[i].clear();
for (int i = 0; i < N; ++i) G[A[i]].push_back(i);
for (int i = 0; i <= N; ++i) reverse((G[i]).begin(), (G[i]).end());
tag = 1;
dfs(N);
for (int i = 0; i < N; ++i) cout << ans[i] << " \n"[i + 1 == N];
return;
}
int main() {
pre();
ncase = gi();
while (ncase--) {
init();
sol();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
int p[1010];
int select(int v, int n) {
for (int i = 1; i <= n; ++i) {
if (p[i] == v) return i;
}
return -1;
}
int main() {
int n, k;
int s1, e1, s2, e2;
while (~scanf("%d%d", &n, &k)) {
scanf("%d%d%d%d", &s1, &e1, &s2, &e2);
if (n < 5 || k < n + 1) {
puts("-1");
continue;
}
for (int i = 1; i <= n; ++i) {
p[i] = i;
}
if (e1 != 1) {
std::swap(p[1], p[s1]);
std::swap(p[n], p[e1]);
} else {
std::swap(p[n], p[e1]);
std::swap(p[1], p[s1]);
}
int pc = select(s2, n);
std::swap(p[2], p[pc]);
int pd = select(e2, n);
std::swap(p[n - 1], p[pd]);
for (int i = 1; i <= n; ++i) {
if (i > 1) printf(" ");
printf("%d", p[i]);
}
printf("\n");
printf("%d ", p[2]);
for (int i = 1; i <= n; ++i) {
if (i != 2 && i != n - 1) printf("%d ", p[i]);
}
printf("%d\n", p[n - 1]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 17;
const int mod1 = 1e9 + 7;
const int mod2 = 998244353;
std::vector<pair<int, int> > G[N];
std::vector<std::vector<int> > V;
int fa[N], w[N], dfn[N], idx, sum;
int f[N << 1], g[N << 1], ff[N << 1], gg[N << 1], n, m;
void dfs(int x, int f) {
dfn[x] = ++idx;
for (auto y : G[x]) {
if (y.first == f) continue;
if (dfn[y.first] > dfn[x]) {
int tmp = y.second;
for (int i = y.first; i != x; i = fa[i]) tmp ^= w[i];
std::vector<int> v;
v.push_back(tmp ^ y.second);
for (int i = y.first; i != x; i = fa[i]) v.push_back(tmp ^ w[i]);
V.push_back(v);
sum ^= tmp;
} else if (!dfn[y.first]) {
fa[y.first] = x;
w[y.first] = y.second;
dfs(y.first, x);
}
}
}
void FWT_xor(int *a, int opt, int MOD) {
int inv2 = (MOD + 1) / 2;
for (int i = 1; i < N; i <<= 1)
for (int p = i << 1, j = 0; j < N; j += p)
for (int k = 0; k < i; ++k) {
int X = a[j + k], Y = a[i + j + k];
a[j + k] = (X + Y) % MOD;
a[i + j + k] = (X + MOD - Y) % MOD;
if (opt == -1)
a[j + k] = 1ll * a[j + k] * inv2 % MOD,
a[i + j + k] = 1ll * a[i + j + k] * inv2 % MOD;
}
}
int main(int argc, char const *argv[]) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
G[x].push_back({y, w});
G[y].push_back({x, w});
sum ^= w;
}
dfs(1, 0);
f[sum] = g[sum] = 1;
FWT_xor(f, 1, mod1);
FWT_xor(g, 1, mod2);
for (auto v : V) {
memset(ff, 0, sizeof(ff));
memset(gg, 0, sizeof(gg));
for (auto x : v) ff[x]++, gg[x]++;
FWT_xor(ff, 1, mod1);
FWT_xor(gg, 1, mod2);
for (int i = 0; i < 1 << 17; i++) {
f[i] = (long long)f[i] * ff[i] % mod1;
g[i] = (long long)g[i] * gg[i] % mod2;
}
}
FWT_xor(f, -1, mod1);
FWT_xor(g, -1, mod2);
for (int i = 0; i < 1 << 17; i++)
if (f[i] || g[i]) {
printf("%d %d\n", i, f[i]);
break;
}
return 0;
}
| 8 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.