solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
vector<int> G[300007];
int P[300007];
int H[300007];
int D[300007];
bool vis[300007];
void dfs(int src, int par, int d, int &ansDis, int &ans) {
vis[src] = true;
if (d > ansDis) {
ans = src;
ansDis = d;
}
for (int i = 0; i < (int)G[src].size(); i++) {
if (G[src][i] != par) {
dfs(G[src][i], src, d + 1, ansDis, ans);
}
}
}
int CompDiameter(int x) {
int src = x, disSrc = 0;
dfs(x, x, 0, disSrc, src);
dfs(src, src, 0, disSrc, src);
return disSrc;
}
int Find(int x) {
if (x == P[x]) return x;
P[x] = Find(P[x]);
return P[x];
}
int Diameter(int x) {
int Px = Find(x);
return D[Px];
}
void Union(int x, int y) {
int Px = Find(x);
int Py = Find(y);
if (Px == Py) return;
if (H[Px] == H[Py]) {
P[Px] = Py;
D[Py] =
max(D[Py], ((D[Px] % 2 == 0) ? (D[Px] / 2) : (D[Px] / 2 + 1)) +
((D[Py] % 2 == 0) ? (D[Py] / 2) : (D[Py] / 2 + 1)) + 1);
D[Py] = max(D[Py], D[Px]);
H[Py]++;
} else if (H[Px] > H[Py]) {
P[Py] = Px;
D[Px] =
max(D[Px], ((D[Px] % 2 == 0) ? (D[Px] / 2) : (D[Px] / 2 + 1)) +
((D[Py] % 2 == 0) ? (D[Py] / 2) : (D[Py] / 2 + 1)) + 1);
D[Px] = max(D[Py], D[Px]);
} else {
P[Px] = Py;
D[Py] =
max(D[Py], ((D[Px] % 2 == 0) ? (D[Px] / 2) : (D[Px] / 2 + 1)) +
((D[Py] % 2 == 0) ? (D[Py] / 2) : (D[Py] / 2 + 1)) + 1);
D[Py] = max(D[Py], D[Px]);
}
}
int main() {
int i, j, k, l, m, n, x, y, z, a, b, r, q;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &q);
for (i = 1; i <= n; i++) {
P[i] = i;
D[i] = -1;
}
for (i = 0; i < m; i++) {
scanf("%d", &x);
scanf("%d", &y);
Union(x, y);
G[x].push_back(y);
G[y].push_back(x);
}
for (i = 1; i <= n; i++) {
if (vis[i]) continue;
D[Find(i)] = CompDiameter(i);
}
for (i = 0; i < q; i++) {
scanf("%d", &x);
if (x == 2) {
scanf("%d", &a);
scanf("%d", &b);
Union(a, b);
} else {
scanf("%d", &a);
printf("%d\n", D[Find(a)]);
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long b, g, n;
cin >> b >> g >> n;
long long x = max((long long)0, n - g), y = min(n, b);
cout << y - x + 1;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, m, n;
long long t1, t2, x1, x2, t0, a1, a2;
const double eps = 1e-10;
double ans = 1e100, ivs;
int x[2005], y[2005], xy[4005], z[4005], yx[4005];
short sh[2005][2005], sv[2005][2005], s[2005][2005];
char p[2005];
int main() {
scanf("%d\n", &n);
for (i = 0; i < n; i++) {
gets(p);
for (j = 0; j < n; j++)
if (p[j] == '1') {
s[i + 1][j + 1] = 1;
}
}
int ans = 0;
for (i = 1; i <= n; i++)
for (j = n; j > i; j--) {
sh[i][j] ^= sh[i][j + 1];
sv[i][j] ^= sv[i - 1][j];
if (sh[i][j] ^ sv[i][j] != s[i][j]) {
sh[i][j] ^= 1;
sv[i][j] ^= 1;
s[i][j] = 1;
ans++;
} else
s[i][j] = 0;
}
for (i = n; i >= 1; i--)
for (j = 1; j < i; j++) {
sh[i][j] ^= sh[i][j - 1];
sv[i][j] ^= sv[i + 1][j];
if (sh[i][j] ^ sv[i][j] != s[i][j]) {
sh[i][j] ^= 1;
sv[i][j] ^= 1;
ans++;
s[i][j] = 1;
} else
s[i][j] = 0;
}
for (i = 1; i <= n; i++) {
j = i;
sh[i][j] ^= sh[i][j - 1] ^ sh[i][j + 1];
sv[i][j] ^= sv[i + 1][j] ^ sv[i - 1][j];
if (sh[i][j] ^ sv[i][j] != s[i][j]) {
sh[i][j] ^= 1;
sv[i][j] ^= 1;
ans++;
s[i][j] = 1;
} else
s[i][j] = 0;
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "inline")
using namespace std;
template <class T>
inline void read(T &x) {
int ch = 0, f = 0;
x = 0;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = 1;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if (f) x = -x;
}
const int N = 500005;
vector<int> g[N], d[N], Ans1, Ans2;
int dfn[N], low[N], id[N], st[N], ins[N], Index, nowID, top;
int w[N], c[N], n, m, cnt;
inline void tarjan(int u) {
dfn[u] = low[u] = ++Index, st[++top] = u, ins[u] = 1;
for (int i = 0; i < (int)g[u].size(); i++) {
int v = g[u][i];
if (!dfn[v])
tarjan(v), low[u] = min(low[u], low[v]);
else if (ins[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
++nowID;
while (st[top] != u) ins[st[top]] = 0, id[st[top]] = nowID, top--;
ins[u] = 0, id[u] = nowID, top--;
}
}
inline int check(int mid) {
for (int i = 1; i <= cnt; i++) g[i].clear();
cnt = m + m;
for (int i = 1; i <= m; i++)
if (w[i] > mid) g[i].push_back(i + m);
for (int i = 1; i <= n; i++) {
int l = 0, r = cnt, sz = (int)d[i].size();
for (int j = 0; j < sz; j++) {
g[r = ++cnt].push_back(d[i][j] + m);
if (j) {
g[r].push_back(r - 1);
g[d[i][j]].push_back(r - 1);
}
}
for (int j = sz - 1; ~j; j--) {
g[r = ++cnt].push_back(d[i][j] + m);
if (j < sz - 1) {
g[r].push_back(r - 1);
g[d[i][j]].push_back(r - 1);
}
}
for (int j = 0; j <= sz; j++)
if ((j && j < sz && c[d[i][j]] != c[d[i][j - 1]]) || j == sz) {
for (int k = l; k <= j - 1; k++) {
g[r = ++cnt].push_back(d[i][k]);
if (k > l) {
g[r].push_back(r - 1);
g[d[i][k] + m].push_back(r - 1);
}
}
for (int k = j - 1; k >= l; k--) {
g[r = ++cnt].push_back(d[i][k]);
if (k < j - 1) {
g[r].push_back(r - 1);
g[d[i][k] + m].push_back(r - 1);
}
}
l = j;
}
}
Index = 0, nowID = 0;
for (int i = 1; i <= cnt; i++) dfn[i] = low[i] = id[i] = 0;
for (int i = 1; i <= cnt; i++)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= m; i++)
if (id[i] == id[i + m]) return 0;
Ans1.clear(), Ans2.clear();
for (int i = 1; i <= m; i++)
if (id[i] < id[i + m])
Ans1.push_back(i);
else
Ans2.push_back(i);
return 1;
}
inline bool cmp(int x, int y) { return c[x] < c[y]; }
int main() {
read(n), read(m);
for (int i = 1, x, y; i <= m; i++) {
read(x), read(y), read(c[i]), read(w[i]);
d[x].push_back(i), d[y].push_back(i);
}
for (int i = 1; i <= n; i++) sort(d[i].begin(), d[i].end(), cmp);
int l = 0, r = (0x3f3f3f3f), ans = (0x3f3f3f3f);
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
ans = mid, r = mid - 1;
else
l = mid + 1;
}
if (ans == (0x3f3f3f3f)) return puts("No"), 0;
puts("Yes");
cout << ans << " " << Ans1.size() << endl;
for (int i = 0; i < (int)Ans1.size(); i++) printf("%d ", Ans1[i]);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000018;
const size_t SIZE = 300005;
unsigned int n;
pair<long long, long long> p[SIZE];
long long max_y = -INF, max_x = -INF, min_y = INF, min_x = INF;
long long max_y_ind, max_x_ind, min_y_ind, min_x_ind;
int main() {
cin.tie();
ios_base::sync_with_stdio(0);
cin >> n;
long long x, y;
for (unsigned int i = 0; i < n; ++i) {
cin >> x >> y;
p[i] = make_pair(x, y);
if (x > max_x) {
max_x = max(max_x, x);
max_x_ind = i;
}
if (y > max_y) {
max_y = max(max_y, y);
max_y_ind = i;
}
if (x < min_x) {
min_x = min(min_x, x);
min_x_ind = i;
}
if (y < min_y) {
min_y = min(min_y, y);
min_y_ind = i;
}
}
long long big_ans = max_x + max_y - min_x - min_y;
if (n == 3) {
cout << 2 * big_ans << " ";
return 0;
}
long long small_ans = -INF;
long long cur_ans;
for (unsigned int i = 0; i < n; ++i) {
if (i != min_x_ind && i != min_y_ind) {
cur_ans = p[i].first - min_x + p[i].second - min_y;
small_ans = max(small_ans, cur_ans);
}
}
for (unsigned int i = 0; i < n; ++i) {
if (i != max_x_ind && i != min_y_ind) {
cur_ans = max_x - p[i].first + p[i].second - min_y;
small_ans = max(small_ans, cur_ans);
}
}
for (unsigned int i = 0; i < n; ++i) {
if (i != min_x_ind && i != max_y_ind) {
cur_ans = p[i].first - min_x + max_y - p[i].second;
small_ans = max(small_ans, cur_ans);
}
}
for (unsigned int i = 0; i < n; ++i) {
if (i != max_x_ind && i != max_y_ind) {
cur_ans = max_x - p[i].first + max_y - p[i].second;
small_ans = max(small_ans, cur_ans);
}
}
cout << 2 * small_ans << " ";
for (unsigned int i = 4; i <= n; ++i) {
cout << 2 * big_ans << " ";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, x, y, q;
int a[300005];
long long ans[300005], res[500 + 5][500 + 5];
vector<pair<pair<int, int>, int> > query;
void print() {
for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
}
void solve() {
int last = n + 1;
for (auto x : query) {
int ind = x.first.first;
int inc = x.first.second;
int wh = x.second;
while (last - 1 >= 1 && last - 1 >= ind) {
last--;
for (int i = 1; i <= 500; i++) {
res[i][last % i] += a[last];
}
}
ans[wh] = res[inc][ind % inc];
}
}
long long solve_naive(int x, int y) {
long long res = 0;
for (int i = x; i <= n; i += y) res += a[i];
return res;
}
void store() {
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d %d", &x, &y);
if (y > 500) {
ans[i] = solve_naive(x, y);
} else {
query.push_back({{x, y}, i});
}
}
sort(query.begin(), query.end(), greater<pair<pair<int, int>, int> >());
}
void read() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
}
int main() {
read();
store();
solve();
print();
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 10, inf = 1e18 + 21, LG = 20;
int tmp;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int q;
cin >> q;
while (q--) {
int n, s, k, a;
set<int> st;
cin >> n >> s >> k;
for (int i = 0; i < k; i++) {
cin >> a;
st.insert(a);
}
for (int i = 0; i < n; i++) {
if (s + i <= n) {
tmp = *st.lower_bound(s + i);
if (tmp != (s + i)) {
cout << i << '\n';
break;
}
}
if (s - i >= 1) {
tmp = *st.lower_bound(s - i);
if (tmp != (s - i)) {
cout << i << '\n';
break;
}
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[110][110], b[110];
int main() {
int n, m, k;
while (scanf("%d %d %d", &n, &m, &k) != EOF) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
}
}
int ans = 100;
if (n > k) {
for (int i = 1; i <= n; i++) {
int sum = 0;
for (int j = 1; j <= n; j++) {
if (j == i) continue;
int x = 0, y = 0;
for (int c = 1; c <= m; c++) {
if (a[i][c] != a[j][c]) x++;
if (a[i][c] == a[j][c]) y++;
}
sum += min(x, y);
if (sum > k) break;
}
if (sum > k) continue;
if (sum < ans) ans = sum;
}
if (ans < 100)
printf("%d\n", ans);
else
printf("-1\n");
} else {
int s = 1 << n;
for (int i = 0; i < s; i++) {
for (int j = 1; j <= n; j++) {
if (i & (1 << (j - 1)))
b[j] = 1;
else
b[j] = 0;
}
int sum = 0;
for (int j = 1; j <= m; j++) {
int x = 0, y = 0;
for (int r = 1; r <= n; r++) {
if (a[r][j] != b[r]) x++;
if (a[r][j] == b[r]) y++;
}
if (j == 1)
sum += x;
else
sum += min(x, y);
}
if (sum <= k && sum < ans) {
ans = sum;
}
}
if (ans < 100)
printf("%d\n", ans);
else
printf("-1\n");
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
while (q--) {
long long oddX = 0, oddY = 0;
long long evenX = 0, evenY = 0;
long long n, m;
long long ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x % 2 == 0)
evenX++;
else
oddX++;
}
cin >> m;
for (int i = 0; i < m; i++) {
int x;
cin >> x;
if (x % 2 == 0)
evenY++;
else
oddY++;
}
ans += oddX * oddY;
ans += evenX * evenY;
cout << ans << '\n';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int q;
int n, m, t[800000], val[200300], days = 0;
vector<pair<int, int> > a(200300);
bool comp(pair<int, int> d, pair<int, int> d1) {
if (d.first != d1.first) {
return (d.first < d1.first);
}
return (d.second < d1.second);
}
void build(int ver, int l, int r) {
if (l == r) {
t[ver] = a[l].second;
return;
}
int mid = l + (r - l) / 2;
build(ver * 2, l, mid);
build(ver * 2 + 1, mid + 1, r);
t[ver] = max(t[ver * 2], t[ver * 2 + 1]);
}
int getMax(int ver, int l, int r, int nl, int nr) {
if (l > nr || r < nl) {
return -1;
}
if (l >= nl && r <= nr) {
return t[ver];
}
int mid = l + (r - l) / 2;
return max(getMax(ver * 2, l, mid, nl, nr),
getMax(ver * 2 + 1, mid + 1, r, nl, nr));
}
int _findPos(int ch) {
int l = 0, r = m;
if (ch > a[r].first) {
return -1;
}
while (r - l > 1) {
int mid = l + (r - l) / 2;
if (a[mid].first >= ch) {
r = mid;
} else {
l = mid;
}
}
return r;
}
void solve() {
days = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> val[i];
}
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a.begin() + 1, a.begin() + m + 1, comp);
build(1, 1, m);
int curL = 1;
int curR = 1;
int curMax = 0;
int border = 1;
while (curL != n + 1) {
if (val[curR] > curMax) {
curMax = val[curR];
border = _findPos(curMax);
}
if (border == -1) {
cout << -1 << endl;
return;
}
int t = getMax(1, 1, m, border, m);
if (t < curR - curL + 1) {
days++;
curL = curR;
curR = curL;
curMax = 0;
border = 1;
} else {
curR++;
if (curR == n + 1) {
days++;
break;
}
}
}
cout << days << endl;
}
int main() {
cin >> q;
while (q != 0) {
solve();
q--;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string itoa(int i) {
stringstream ss;
ss << i;
return ss.str();
}
typedef struct _john {
char start;
int i, r, c;
} John;
typedef struct _inst {
char direc;
int len;
} Inst;
vector<string> mm;
int val[1000][1000][4];
int mr[] = {0, 0, 1, -1};
int mc[] = {1, -1, 0, 0};
int main() {
int n, m, k;
mm.clear();
cin >> n >> m;
deque<John> q;
for (int i = (0); i < (n); ++i) {
string row;
cin >> row;
mm.push_back(row);
for (int j = (0); j < (row.length()); ++j) {
if (row[j] != '#' && row[j] != '.') {
John captain;
captain.start = row[j];
captain.i = 0;
captain.r = i;
captain.c = j;
q.push_back(captain);
}
}
}
cin >> k;
vector<Inst> v;
for (int i = (0); i < (k); ++i) {
Inst inst;
cin >> inst.direc >> inst.len;
v.push_back(inst);
}
for (int c = (0); c < (m); ++c) {
int poss = 0;
for (int r = (0); r < (n); ++r) {
val[r][c][0] = poss;
if (mm[r][c] != '#')
poss++;
else
poss = 0;
}
poss = 0;
for (int r = (n - 1); r >= (0); --r) {
val[r][c][1] = poss;
if (mm[r][c] != '#')
poss++;
else
poss = 0;
}
}
for (int r = (0); r < (n); ++r) {
int poss = 0;
for (int c = (0); c < (m); ++c) {
val[r][c][2] = poss;
if (mm[r][c] != '#')
poss++;
else
poss = 0;
}
poss = 0;
for (int c = (m - 1); c >= (0); --c) {
val[r][c][3] = poss;
if (mm[r][c] != '#')
poss++;
else
poss = 0;
}
}
vector<char> v_res;
while (!q.empty()) {
John _now = q.front();
q.pop_front();
if (_now.i >= v.size()) {
v_res.push_back(_now.start);
continue;
}
Inst _cur = v[_now.i];
bool poss = true;
switch (_cur.direc) {
case 'N':
if (_now.r - _cur.len >= 0) {
if (val[_now.r][_now.c][0] < _cur.len) poss = false;
_now.r -= _cur.len;
} else
poss = false;
break;
case 'S':
if (_now.r + _cur.len < n) {
if (val[_now.r][_now.c][1] < _cur.len) poss = false;
_now.r += _cur.len;
} else
poss = false;
break;
case 'E':
if (_now.c + _cur.len < m) {
if (val[_now.r][_now.c][3] < _cur.len) poss = false;
_now.c += _cur.len;
} else
poss = false;
break;
case 'W':
if (_now.c - _cur.len >= 0) {
if (val[_now.r][_now.c][2] < _cur.len) poss = false;
_now.c -= _cur.len;
} else
poss = false;
break;
}
if (poss && mm[_now.r][_now.c] != '#') {
_now.i += 1;
q.push_back(_now);
}
}
sort((v_res).begin(), (v_res).end());
if (v_res.size() > 0) {
for (int i = (0); i < (v_res.size()); ++i) {
cout << v_res[i];
}
cout << endl;
} else {
cout << "no solution" << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 1;
int a[maxn];
int ost[maxn];
int dp[maxn][maxn];
int p[maxn][maxn];
long long now[maxn];
int main() {
int n, k, v;
scanf("%d%d%d", &n, &k, &v);
long long sum = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
sum += a[i];
}
if (sum < v) {
cout << "NO";
return 0;
}
for (int i = 1; i <= n; ++i) ost[i] = a[i] % k;
dp[0][0] = 1;
for (int i = 0; i < n; ++i) {
for (int w = 0; w < k; ++w)
if (dp[i][w]) {
dp[i + 1][w] = 1;
p[i + 1][w] = w;
int nw = w + ost[i + 1];
if (nw >= k) nw -= k;
p[i + 1][nw] = w;
dp[i + 1][nw] = 1;
}
}
int need = v % k;
if (!dp[n][need]) {
cout << "NO";
return 0;
}
cout << "YES\n";
vector<int> use;
int w = need;
for (int i = n; i >= 1; --i) {
int old_w = p[i][w];
if (old_w == w) {
continue;
} else {
use.push_back(i);
w = old_w;
}
}
reverse(use.begin(), use.end());
use.push_back(100000);
int iter = 0;
for (int i = 1; i <= n; ++i) {
if (i == use[iter]) {
++iter;
now[1] += a[i];
if (i == 1) continue;
if (!a[i]) continue;
cout << ((a[i] + k - 1) / k) << " " << i << " " << 1 << "\n";
} else {
now[2] += a[i];
if (i == 2) continue;
if (!a[i]) continue;
cout << ((a[i] + k - 1) / k) << " " << i << " " << 2 << "\n";
}
}
if (now[1] == v) {
return 0;
}
if (now[1] < v) {
cout << (v - now[1]) / k << " " << 2 << " " << 1 << "\n";
} else
cout << (now[1] - v) / k << " " << 1 << " " << 2 << "\n";
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
char ch = getchar();
long long f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
}
template <typename T, typename... Args>
void read(T &first, Args &...args) {
read(first);
read(args...);
}
template <typename T>
void write(T arg) {
T x = arg;
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
template <typename T, typename... Ts>
void write(T arg, Ts... args) {
write(arg);
if (sizeof...(args) != 0) {
putchar(' ');
write(args...);
}
}
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int T;
int n, k;
string s;
int buck[30];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T--) {
for (int i = 0; i < 30; i++) buck[i] = 0;
cin >> n >> k;
cin >> s;
for (int i = 0; i < n; i++) buck[s[i] - 'a']++;
for (int i = n; i >= 1; i--) {
int t = gcd(i, k);
int block = i / t;
int tot = 0;
for (int j = 0; j < 26; j++) tot += buck[j] / block;
if (tot >= t) {
cout << i << endl;
break;
}
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char a[] = "hello";
char c;
int i = 0;
while (scanf("%c", &c) == 1) {
if (c == a[i]) i++;
}
if (i == 5)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, nQ;
vector<int> x;
struct Query {
int t, a1, a2;
void read() { scanf("%d%d%d", &t, &a1, &a2); }
};
vector<Query> qs;
vector<int> cx;
vector<int> allx;
vector<bool> has;
struct Data {
bool exist;
int cnt;
long long sum, op;
Data() {
sum = op = cnt = 0;
exist = false;
}
Data(int v) {
sum = v;
op = 0;
cnt = 1;
exist = true;
}
};
Data operator+(const Data& a, const Data& b) {
if (!a.exist) return b;
if (!b.exist) return a;
Data ret;
ret.exist = true;
ret.sum = a.sum + b.sum;
ret.op = a.op + b.op - b.cnt * a.sum + a.cnt * b.sum;
ret.cnt = a.cnt + b.cnt;
return ret;
}
struct Tree {
Data data;
Tree *pl, *pr;
int l, r;
void update() { data = pl->data + pr->data; }
Tree(int l, int r) : l(l), r(r) {
if (l + 1 == r) {
if (has[l]) {
data = Data(allx[l]);
} else {
data = Data();
}
return;
}
pl = new Tree(l, (l + r) >> 1);
pr = new Tree((l + r) >> 1, r);
update();
}
Data calc(int L, int R) {
if (L <= l && R >= r) return data;
if (L >= r || l >= R) return Data();
return pl->calc(L, R) + pr->calc(L, R);
}
void change(int i) {
if (i < l || i >= r) return;
if (l + 1 == r) {
if (has[i]) {
has[i] = false;
data = Data();
} else {
data = Data(allx[i]);
has[i] = true;
}
return;
}
pl->change(i);
pr->change(i);
update();
return;
}
} * rt;
int get(int x) {
return lower_bound(allx.begin(), allx.end(), x) - allx.begin();
}
int main() {
cin >> n;
x.assign(n, 0);
for (int i = 0; i < n; ++i) {
scanf("%d", &x[i]);
}
allx = x;
cx = x;
cin >> nQ;
qs.resize(nQ);
for (int i = 0; i < nQ; ++i) {
qs[i].read();
}
for (int i = 0; i < nQ; ++i) {
if (qs[i].t == 1) {
int p = qs[i].a1, d = qs[i].a2;
--p;
cx[p] += d;
allx.push_back(cx[p]);
}
}
sort(allx.begin(), allx.end());
allx.erase(unique(allx.begin(), allx.end()), allx.end());
has.assign(allx.size(), false);
for (int i = 0; i < n; ++i) {
has[get(x[i])] = true;
}
rt = new Tree(0, allx.size());
cx = x;
for (int i = 0; i < qs.size(); ++i) {
Query q = qs[i];
if (q.t == 1) {
int p = q.a1, d = q.a2;
--p;
int x = cx[p];
int nx = cx[p] += d;
rt->change(get(x));
rt->change(get(nx));
} else {
int l = q.a1, r = q.a2;
l = lower_bound(allx.begin(), allx.end(), l) - allx.begin();
r = upper_bound(allx.begin(), allx.end(), r) - allx.begin();
Data ret = rt->calc(l, r);
cout << ret.op << endl;
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int a[101][101];
vector<int> gg;
int main() {
int n, v, s, m, k = 0;
cin >> n >> v;
for (int i = 1; i <= n; i++) {
s = 0;
cin >> m;
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] < v) s = 1;
}
if (s == 1) {
k++;
gg.push_back(i);
}
}
if (k == 0)
cout << 0;
else {
cout << k;
cout << endl;
for (int i = 0; i < gg.size(); i++) cout << gg[i] << ' ';
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxpq = priority_queue<T, vector<T>, less<T>>;
template <typename T1, typename T2, typename H1 = hash<T1>,
typename H2 = hash<T2>>
struct pair_hash {
size_t operator()(const pair<T1, T2> &p) const {
return 31 * H1{}(p.first) + H2{}(p.second);
}
};
char _inputBuffer[4096 + 1], *_inputPtr = _inputBuffer, _outputBuffer[4096], _c,
_sign, *_tempInputBuf = nullptr, _numBuf[128],
_tempOutputBuf[128], _fill = ' ';
const char *_delimiter = " ";
int _cur, _outputPtr = 0, _numPtr = 0, _precision = 6, _width = 0,
_tempOutputPtr = 0, _cnt;
unsigned long long _precisionBase = 1000000;
void read(int &x) {
do {
do {
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
} while ((x) <= ' ');
_sign = (x) == '-';
if (_sign)
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
for ((x) -= '0';
(_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1,
4096, stdin)] = '\0',
*_inputPtr++))) >= '0';
(x) = (x)*10 + _c - '0')
;
if (_sign) (x) = -(x);
} while (0);
}
void read(unsigned int &x) {
do {
do {
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
} while ((x) <= ' ');
_sign = (x) == '-';
if (_sign)
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
for ((x) -= '0';
(_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1,
4096, stdin)] = '\0',
*_inputPtr++))) >= '0';
(x) = (x)*10 + _c - '0')
;
if (_sign) (x) = -(x);
} while (0);
}
void read(long long &x) {
do {
do {
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
} while ((x) <= ' ');
_sign = (x) == '-';
if (_sign)
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
for ((x) -= '0';
(_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1,
4096, stdin)] = '\0',
*_inputPtr++))) >= '0';
(x) = (x)*10 + _c - '0')
;
if (_sign) (x) = -(x);
} while (0);
}
void read(unsigned long long &x) {
do {
do {
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
} while ((x) <= ' ');
_sign = (x) == '-';
if (_sign)
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
for ((x) -= '0';
(_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1,
4096, stdin)] = '\0',
*_inputPtr++))) >= '0';
(x) = (x)*10 + _c - '0')
;
if (_sign) (x) = -(x);
} while (0);
}
void read(double &x) {
do {
do {
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
} while ((x) <= ' ');
_sign = (x) == '-';
if (_sign)
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
for ((x) -= '0';
(_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1,
4096, stdin)] = '\0',
*_inputPtr++))) >= '0';
(x) = (x)*10 + _c - '0')
;
if (_c == '.')
for (double _div = 1.0;
(_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1,
4096, stdin)] = '\0',
*_inputPtr++))) >= '0';
(x) += (_c - '0') / (_div *= 10))
;
if (_sign) (x) = -(x);
} while (0);
}
void read(long double &x) {
do {
do {
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
} while ((x) <= ' ');
_sign = (x) == '-';
if (_sign)
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
for ((x) -= '0';
(_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1,
4096, stdin)] = '\0',
*_inputPtr++))) >= '0';
(x) = (x)*10 + _c - '0')
;
if (_c == '.')
for (long double _div = 1.0;
(_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1,
4096, stdin)] = '\0',
*_inputPtr++))) >= '0';
(x) += (_c - '0') / (_div *= 10))
;
if (_sign) (x) = -(x);
} while (0);
}
void read(char &x) {
do {
do {
(x) = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
} while ((x) <= ' ');
} while (0);
}
void read(char *x) {
do {
_cur = 0;
do {
_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1, 4096,
stdin)] = '\0',
*_inputPtr++));
} while (_c <= ' ');
do {
(x)[_cur++] = _c;
} while ((_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer,
1, 4096, stdin)] = '\0',
*_inputPtr++))) > ' ');
(x)[_cur] = '\0';
} while (0);
}
void read(string &x) {
do {
if (!_tempInputBuf) assert(0);
do {
_cur = 0;
do {
_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer, 1,
4096, stdin)] = '\0',
*_inputPtr++));
} while (_c <= ' ');
do {
(_tempInputBuf)[_cur++] = _c;
} while ((_c = (*_inputPtr ? *_inputPtr++
: (_inputBuffer[fread(_inputPtr = _inputBuffer,
1, 4096, stdin)] = '\0',
*_inputPtr++))) > ' ');
(_tempInputBuf)[_cur] = '\0';
} while (0);
(x) = string(_tempInputBuf, _cur);
} while (0);
}
template <typename T, typename... Ts>
void read(T &&x, Ts &&...xs) {
read(x);
read(forward<Ts>(xs)...);
}
void write(bool x) {
do {
if (x)
(_tempOutputBuf[_tempOutputPtr++] = ('1'));
else
(_tempOutputBuf[_tempOutputPtr++] = ('0'));
int _tempLen = _tempOutputPtr;
for (int _i = 0; _i < (_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (int _i = 0; _i < _tempOutputPtr;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_tempOutputBuf[_i++]),
_outputPtr++))
;
_tempOutputPtr = 0;
for (int _i = 0; _i < (-_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
void write(int x) {
do {
if ((x) < 0) {
(_tempOutputBuf[_tempOutputPtr++] = ('-'));
_cnt = 0;
for (unsigned int _y = (-(x)); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
} else {
_cnt = 0;
for (unsigned int _y = (x); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
}
int _tempLen = _tempOutputPtr;
for (int _i = 0; _i < (_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (int _i = 0; _i < _tempOutputPtr;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_tempOutputBuf[_i++]),
_outputPtr++))
;
_tempOutputPtr = 0;
for (int _i = 0; _i < (-_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
void write(unsigned int x) {
do {
if ((x) < 0) {
(_tempOutputBuf[_tempOutputPtr++] = ('-'));
_cnt = 0;
for (unsigned int _y = (-(x)); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
} else {
_cnt = 0;
for (unsigned int _y = (x); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
}
int _tempLen = _tempOutputPtr;
for (int _i = 0; _i < (_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (int _i = 0; _i < _tempOutputPtr;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_tempOutputBuf[_i++]),
_outputPtr++))
;
_tempOutputPtr = 0;
for (int _i = 0; _i < (-_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
void write(long long x) {
do {
if ((x) < 0) {
(_tempOutputBuf[_tempOutputPtr++] = ('-'));
_cnt = 0;
for (unsigned long long _y = (-(x)); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
} else {
_cnt = 0;
for (unsigned long long _y = (x); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
}
int _tempLen = _tempOutputPtr;
for (int _i = 0; _i < (_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (int _i = 0; _i < _tempOutputPtr;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_tempOutputBuf[_i++]),
_outputPtr++))
;
_tempOutputPtr = 0;
for (int _i = 0; _i < (-_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
void write(unsigned long long x) {
do {
if ((x) < 0) {
(_tempOutputBuf[_tempOutputPtr++] = ('-'));
_cnt = 0;
for (unsigned long long _y = (-(x)); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
} else {
_cnt = 0;
for (unsigned long long _y = (x); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
}
int _tempLen = _tempOutputPtr;
for (int _i = 0; _i < (_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (int _i = 0; _i < _tempOutputPtr;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_tempOutputBuf[_i++]),
_outputPtr++))
;
_tempOutputPtr = 0;
for (int _i = 0; _i < (-_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
void write(double x) {
do {
if (std::isnan(x)) {
do {
int _slen = strlen("NaN");
for (int _i = 0; _i < (_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (const char *_p = ("NaN"); *_p;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (*_p++),
_outputPtr++))
;
for (int _i = 0; _i < (-_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
} else if (std::isinf(x)) {
do {
int _slen = strlen("Inf");
for (int _i = 0; _i < (_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (const char *_p = ("Inf"); *_p;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (*_p++),
_outputPtr++))
;
for (int _i = 0; _i < (-_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
} else if ((x) < 0) {
(_tempOutputBuf[_tempOutputPtr++] = ('-'));
unsigned long long _I = (unsigned long long)(-(x));
unsigned long long _F = ((-(x)) - _I) * _precisionBase + (double)(0.5);
if (_F >= _precisionBase) {
_I++;
_F = 0;
}
_cnt = 0;
for (unsigned long long _y = (_I); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
(_tempOutputBuf[_tempOutputPtr++] = ('.'));
_cnt = 0;
for (unsigned long long _y = (_F); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < _precision; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
} else {
unsigned long long _I = (unsigned long long)(x);
unsigned long long _F = ((x)-_I) * _precisionBase + (double)(0.5);
if (_F >= _precisionBase) {
_I++;
_F = 0;
}
_cnt = 0;
for (unsigned long long _y = (_I); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
(_tempOutputBuf[_tempOutputPtr++] = ('.'));
_cnt = 0;
for (unsigned long long _y = (_F); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < _precision; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
}
int _tempLen = _tempOutputPtr;
for (int _i = 0; _i < (_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (int _i = 0; _i < _tempOutputPtr;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_tempOutputBuf[_i++]),
_outputPtr++))
;
_tempOutputPtr = 0;
for (int _i = 0; _i < (-_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
void write(long double x) {
do {
if (std::isnan(x)) {
do {
int _slen = strlen("NaN");
for (int _i = 0; _i < (_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (const char *_p = ("NaN"); *_p;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (*_p++),
_outputPtr++))
;
for (int _i = 0; _i < (-_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
} else if (std::isinf(x)) {
do {
int _slen = strlen("Inf");
for (int _i = 0; _i < (_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (const char *_p = ("Inf"); *_p;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (*_p++),
_outputPtr++))
;
for (int _i = 0; _i < (-_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
} else if ((x) < 0) {
(_tempOutputBuf[_tempOutputPtr++] = ('-'));
unsigned long long _I = (unsigned long long)(-(x));
unsigned long long _F =
((-(x)) - _I) * _precisionBase + (long double)(0.5);
if (_F >= _precisionBase) {
_I++;
_F = 0;
}
_cnt = 0;
for (unsigned long long _y = (_I); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
(_tempOutputBuf[_tempOutputPtr++] = ('.'));
_cnt = 0;
for (unsigned long long _y = (_F); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < _precision; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
} else {
unsigned long long _I = (unsigned long long)(x);
unsigned long long _F = ((x)-_I) * _precisionBase + (long double)(0.5);
if (_F >= _precisionBase) {
_I++;
_F = 0;
}
_cnt = 0;
for (unsigned long long _y = (_I); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < 1; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
(_tempOutputBuf[_tempOutputPtr++] = ('.'));
_cnt = 0;
for (unsigned long long _y = (_F); _y; _y /= 10, _cnt++)
_numBuf[_numPtr++] = '0' + _y % 10;
for (; _cnt < _precision; _cnt++) _numBuf[_numPtr++] = '0';
for (; _numPtr; (_tempOutputBuf[_tempOutputPtr++] = (_numBuf[--_numPtr])))
;
;
}
int _tempLen = _tempOutputPtr;
for (int _i = 0; _i < (_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (int _i = 0; _i < _tempOutputPtr;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_tempOutputBuf[_i++]),
_outputPtr++))
;
_tempOutputPtr = 0;
for (int _i = 0; _i < (-_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
void write(char x) {
do {
(_tempOutputBuf[_tempOutputPtr++] = (x));
int _tempLen = _tempOutputPtr;
for (int _i = 0; _i < (_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (int _i = 0; _i < _tempOutputPtr;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_tempOutputBuf[_i++]),
_outputPtr++))
;
_tempOutputPtr = 0;
for (int _i = 0; _i < (-_width - _tempLen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
void write(char *x) {
do {
int _slen = strlen(x);
for (int _i = 0; _i < (_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (const char *_p = (x); *_p;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (*_p++),
_outputPtr++))
;
for (int _i = 0; _i < (-_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
void write(const char *x) {
do {
int _slen = strlen(x);
for (int _i = 0; _i < (_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (const char *_p = (x); *_p;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (*_p++),
_outputPtr++))
;
for (int _i = 0; _i < (-_width - _slen); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
void write(string &x) {
do {
for (int _i = 0; _i < (_width - (int)(x).length()); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
for (int _i = 0; _i < (int)(x).length();
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (x[_i++]),
_outputPtr++))
;
for (int _i = 0; _i < (-_width - (int)(x).length()); _i++)
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (_fill),
_outputPtr++);
} while (0);
}
template <typename T, typename... Ts>
void write(T &&x, Ts &&...xs) {
write(x);
for (const char *_p = _delimiter; *_p;
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = (*_p++),
_outputPtr++))
;
write(forward<Ts>(xs)...);
}
void writeln() {
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = ('\n'),
_outputPtr++);
}
template <typename... Ts>
void writeln(Ts &&...xs) {
write(forward<Ts>(xs)...);
(_outputBuffer[_outputPtr == 4096
? (fwrite(_outputBuffer, 1, _outputPtr, stdout),
_outputPtr = 0)
: _outputPtr] = ('\n'),
_outputPtr++);
}
void flush() {
do {
(fwrite(_outputBuffer, 1, _outputPtr, stdout), _outputPtr = 0);
fflush(stdout);
} while (0);
}
class IOManager {
public:
~IOManager() { flush(); }
};
unique_ptr<IOManager> _iomanager;
template <class SrcIt, class DstIt, class Comparator>
long long count_inversions(SrcIt src_st, SrcIt src_en, DstIt dst_st,
DstIt dst_en, Comparator cmp) {
int n = src_en - src_st;
if (n <= 1) return 0;
int mid = (n - 1) / 2;
long long ret = 0;
ret +=
count_inversions(dst_st, dst_st + mid + 1, src_st, src_st + mid + 1, cmp);
ret +=
count_inversions(dst_st + mid + 1, dst_en, src_st + mid + 1, src_en, cmp);
for (int i = 0, j = mid + 1, k = 0; k < n; k++) {
if (i > mid) {
dst_st[k] = src_st[j++];
} else if (j >= n) {
dst_st[k] = src_st[i++];
ret += j - (mid + 1);
} else if (cmp(src_st[j], src_st[i])) {
dst_st[k] = src_st[j++];
} else {
dst_st[k] = src_st[i++];
ret += j - (mid + 1);
}
}
return ret;
}
template <class It, class Comparator>
long long count_inversions(It st, It en, Comparator cmp) {
int n = en - st;
typename std::iterator_traits<It>::value_type *aux =
new typename std::iterator_traits<It>::value_type[n];
for (int i = 0; i < n; i++) aux[i] = st[i];
long long ret = count_inversions(aux, aux + n, st, en, cmp);
delete[](aux);
return ret;
}
int N, A[105 * 2], B[105 * 2], firstInd[105], lastInd[105];
bool cmpFirst(int i, int j) { return firstInd[i] < firstInd[j]; }
bool cmpLast(int i, int j) { return lastInd[i] < lastInd[j]; }
int main() {
_iomanager.reset(new IOManager());
read(N);
for (auto i = (0); i < (N); i++) {
firstInd[i] = 2 * N;
lastInd[i] = -1;
}
for (auto i = (0); i < (2 * N); i++) {
read(A[i]);
A[i]--;
B[i] = A[i];
((firstInd[A[i]]) = min((firstInd[A[i]]), (i)));
((lastInd[A[i]]) = max((lastInd[A[i]]), (i)));
}
writeln(min(count_inversions(A, A + (2 * N), cmpFirst),
count_inversions(B, B + (2 * N), cmpLast)));
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> d[10000004];
long l[103], r[103], size[103], ans[103], v[103];
int z[10000004];
int main() {
int n;
cin >> n;
set<long> s;
for (long i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
long h = r[i] - l[i] + 1;
if (h > 200) {
l[i] = r[i] - 200 + 1;
}
h = r[i] - l[i] + 1;
size[i] = h;
for (long j = l[i]; j <= r[i]; j++) {
d[j].push_back(i);
s.insert(j);
}
}
long p = 100000000;
long e;
while (!s.empty()) {
long h = *s.begin();
long m = p;
s.erase(s.begin());
for (long j = 0; j < d[h].size(); j++) {
long w = d[h][j];
if (v[w] == 0) {
if (r[w] < m) {
m = r[w];
e = w;
}
}
}
if (m != p) {
ans[e] = h;
v[e] = 1;
}
}
for (long i = 1; i <= n; i++) cout << ans[i] << " ";
cout << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
t = 1;
while (t--) {
long long int n, m, l, k, ans = 0;
cin >> n >> m;
long long int a[n];
for (int i = 0; i < n; i++) {
cin >> k >> l;
ans += min(k, l);
a[i] = min(2 * k, l) - min(k, l);
}
sort(a, a + n);
for (int i = n - m; i < n; i++) ans += a[i];
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, k, ans = 0;
cin >> n;
vector<int> vec(n);
for (int i = 0; i < n; i++) cin >> vec[i];
int depth = 1;
int index = 0, count = 0, eipd = 1, eicd = 0, irr = 0;
for (int i = 1; i < n - 1; i++) {
eicd++;
if (vec[i] > vec[i + 1]) irr++;
if (irr == eipd) {
depth++;
eipd = eicd;
eicd = 0;
irr = 0;
}
}
cout << depth << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, a[2 * 100010], b[2 * 100010], k[2 * 100010], par[2 * 100010], m;
struct inter {
int beg, end, ind;
inter(int beg_ = 0, int end_ = 0, int ind_ = 0) {
beg = beg_;
end = end_;
ind = ind_;
}
bool operator<(inter B) const {
return ((beg < B.beg) || ((beg == B.beg) && (ind > B.ind)));
}
};
vector<inter> v;
set<pair<int, int> > s;
int main() {
while (scanf("%d", &n) != EOF) {
v.clear();
for (int i = 0; i < n; i++) {
scanf("%d %d", &a[i], &b[i]);
v.push_back(inter(a[i], b[i], i));
}
scanf("%d", &m);
for (int i = n; i < m + n; i++) {
scanf("%d %d %d", &a[i], &b[i], &k[i]);
v.push_back(inter(a[i], b[i], i));
}
sort(v.begin(), v.end());
bool pode = true;
s.clear();
set<pair<int, int> >::iterator it;
int notas = 0;
for (int i = 0; i < n + m; i++) {
if (v[i].ind >= n) {
s.insert(pair<int, int>(v[i].end, v[i].ind));
} else {
it = s.lower_bound(pair<int, int>(v[i].end, -1));
int idx;
if (it == s.end()) {
pode = false;
break;
} else {
idx = (*it).second;
par[v[i].ind] = idx;
}
k[idx]--;
if (k[idx] == 0) {
s.erase(*it);
}
}
}
if (!pode) {
printf("NO\n");
} else {
printf("YES\n");
for (int i = 0; i < n; i++) {
printf("%d%c", par[i] - n + 1, " \n"[i == n - 1]);
}
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, q, num, dp[30][1505];
vector<int> pl[30];
char ch;
string nm;
int main(void) {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> nm >> q;
for (int i = 0; i < int((int)(nm).size()); i++) {
for (int j = 0; j < int(26); j++) {
if (nm[i] == j + 'a')
continue;
else
pl[j].push_back(i);
}
}
for (int i = 0; i < int(26); i++) {
dp[i][0] = 0;
int tmp = 1;
for (int j = 1; j < (int)(pl[i]).size(); ++j) {
if (nm[j] != nm[j - 1]) {
dp[i][0] = max(dp[i][0], tmp);
tmp = 1;
} else {
++tmp;
}
}
dp[i][0] = max(dp[i][0], tmp);
for (int j = 1; j <= n; ++j) {
if (j >= (int)(pl[i]).size())
dp[i][j] = n;
else {
int L = 0, R = j - 1;
while (R <= (int)(pl[i]).size() - 1) {
int goL = pl[i][L], goR = pl[i][R];
while (goL >= 1 && nm[goL - 1] == i + 'a') --goL;
while (goR < n - 1 && nm[goR + 1] == i + 'a') ++goR;
dp[i][j] = max(dp[i][j], goR - goL + 1);
++L, ++R;
}
}
}
}
while (q--) {
cin >> num >> ch;
cout << dp[ch - 'a'][num] << "\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX / 2, MOD = 1000000007, N = (int)2e5 + 10;
const long long INFL = LLONG_MAX / 2;
template <typename T, typename U>
static void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
static void amax(T &x, U y) {
if (x < y) x = y;
}
long long s, x;
int main() {
scanf("%lld%lld", &s, &x);
long long tp = x;
long long cnt = 0;
long long cur = (s - x) / 2;
if (cur * 2LL + x != s || (cur & x) != 0 || x > s) {
puts("0");
return 0;
}
while (tp) {
cnt += (1 & tp);
tp >>= 1;
}
long long ans = (1LL << cnt);
if (s == x) ans -= 2;
printf("%lld\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, k;
cin >> n >> k;
string s;
cin >> s;
set<char> ST;
for (char x : s) ST.insert(x);
if (k > n) {
for (int i = 0; i < n; i++) cout << s[i];
for (int i = n; i < k; i++) cout << *ST.begin();
return;
}
for (int i = k - 1; i >= 0; i--) {
if (s[i] < *ST.rbegin()) {
string f = "";
for (int j = 0; j < i; j++) f += s[j];
char mnThatWorkz = 'z';
for (char c : ST) {
if (c > s[i] && mnThatWorkz > c) mnThatWorkz = c;
}
f += mnThatWorkz;
while ((int)f.size() < k) f += *ST.begin();
cout << f << "\n";
return;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int t = 1;
while (t--) {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3 * 1e4 + 100;
vector<int> mp[maxn];
int vis[maxn];
int n, num, m;
void dfs(int x) {
if (vis[x]) return;
vis[x] = 1;
for (int i = 0; i < mp[x].size(); i++) dfs(mp[x][i]);
num++;
if (num == n)
cout << x << endl;
else
cout << x << " ";
}
int main() {
int x, y;
while (cin >> n >> m) {
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++) mp[i].clear();
for (int i = 0; i < m; i++) {
cin >> x >> y;
mp[x].push_back(y);
}
num = 0;
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int sum = 0, ff = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') ff = -1;
ch = getchar();
}
while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar();
return sum * ff;
}
const int mod = 1e9 + 7;
const int mo = 998244353;
const int N = 2e5 + 5;
int n, m, s, du[N], ok[N], ux[N], uy[N], sum[N], cnt, ans;
vector<int> G[N];
inline void del(int x) {
queue<int> q;
q.push(x);
ans--;
ok[x] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = (0); i <= ((int)G[u].size() - 1); i++) {
int v = G[u][i];
if (ok[v]) continue;
if ((--du[v]) < s) q.push(v), ok[v] = 1, ans--;
}
}
}
int main() {
n = read();
m = read();
s = read();
for (int i = (1); i <= (m); i++) {
int x, y;
x = read(), y = read();
G[x].push_back(y);
G[y].push_back(x);
du[y]++, du[x]++;
ux[i] = x, uy[i] = y;
}
ans = n;
for (int i = (1); i <= (n); i++)
if (!ok[i] && du[i] < s) del(i);
sum[m] = ans;
for (int i = (m); i >= (1); i--) {
int x = ux[i], y = uy[i];
if (!ok[x]) du[y]--;
if (!ok[y]) du[x]--;
G[x].pop_back();
G[y].pop_back();
if (!ok[x] && du[x] < s) del(x);
if (!ok[y] && du[y] < s) del(y);
sum[i - 1] = ans;
}
for (int i = (1); i <= (m); i++) printf("%d\n", sum[i]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e6 + 3;
long long inf = 1e18;
double eps = 1e-2;
ifstream in("input.txt");
ofstream out("output.txt");
int main() {
int n, q;
cin >> n >> q;
string s;
cin >> s;
vector<vector<int>> links(n + 3, vector<int>(26, n + 1));
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < 26; j++) {
links[i][j] = links[i + 1][j];
}
links[i][s[i] - 'a'] = i;
}
string s1, s2, s3;
vector<vector<vector<int>>> dp(
251, vector<vector<int>>(251, vector<int>(251, n + 2)));
dp[0][0][0] = 0;
int a = 0, b = 0, c = 0;
for (int w = 0; w < q; w++) {
char ch;
cin >> ch;
int t;
cin >> t;
if (ch == '+') {
cin >> ch;
if (t == 1) {
s1.push_back(ch);
a++;
for (int i = 0; i <= b; i++) {
for (int j = 0; j <= c; j++) {
dp[a][i][j] = links[dp[a - 1][i][j]][ch - 'a'] + 1;
if (i > 0)
dp[a][i][j] =
min(dp[a][i][j], links[dp[a][i - 1][j]][s2[i - 1] - 'a'] + 1);
if (j > 0)
dp[a][i][j] =
min(dp[a][i][j], links[dp[a][i][j - 1]][s3[j - 1] - 'a'] + 1);
}
}
}
if (t == 2) {
s2.push_back(ch);
b++;
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= c; j++) {
dp[i][b][j] = links[dp[i][b - 1][j]][ch - 'a'] + 1;
if (i > 0)
dp[i][b][j] =
min(dp[i][b][j], links[dp[i - 1][b][j]][s1[i - 1] - 'a'] + 1);
if (j > 0)
dp[i][b][j] =
min(dp[i][b][j], links[dp[i][b][j - 1]][s3[j - 1] - 'a'] + 1);
}
}
}
if (t == 3) {
s3.push_back(ch);
c++;
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= b; j++) {
dp[i][j][c] = links[dp[i][j][c - 1]][ch - 'a'] + 1;
if (i > 0)
dp[i][j][c] =
min(dp[i][j][c], links[dp[i - 1][j][c]][s1[i - 1] - 'a'] + 1);
if (j > 0)
dp[i][j][c] =
min(dp[i][j][c], links[dp[i][j - 1][c]][s2[j - 1] - 'a'] + 1);
}
}
}
} else {
if (t == 1) {
for (int i = 0; i <= b; i++) {
for (int j = 0; j <= c; j++) {
dp[a][i][j] = n + 2;
}
}
s1.pop_back();
a--;
}
if (t == 2) {
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= c; j++) {
dp[i][b][j] = n + 2;
}
}
s2.pop_back();
b--;
}
if (t == 3) {
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= b; j++) {
dp[i][j][c] = n + 2;
}
}
s3.pop_back();
c--;
}
}
if (dp[a][b][c] != n + 2) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
long long ans = 0;
int n, f[18 + 1][200000 + 5], a[200000 + 5], lg[200000 + 5];
map<int, int> mp;
int Query(int l, int r) {
int s = lg[r - l + 1];
return f[s][l] | f[s][r - (1 << s) + 1];
}
int main() {
n = read();
ans = 1LL * n * (n + 1) / 2;
lg[0] = -1;
for (int i = 1; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= n; ++i) a[i] = f[0][i] = read();
for (int i = 1; i <= 18; ++i)
for (int j = 1; j <= n; ++j)
if (j + (1 << i) - 1 <= n)
f[i][j] = f[i - 1][j] | f[i - 1][j + (1 << i - 1)];
for (int i = 1; i <= n; ++i) {
int l = 1, r = i - 1, mid, L = i, R = i;
while (l <= r) {
mid = l + r >> 1;
if ((Query(mid, i)) == a[i])
L = mid, r = mid - 1;
else
l = mid + 1;
}
l = i + 1, r = n;
while (l <= r) {
mid = l + r >> 1;
if ((Query(i, mid)) == a[i])
R = mid, l = mid + 1;
else
r = mid - 1;
}
L = max(L, mp[a[i]] + 1);
ans -= 1LL * (i - L + 1) * (R - i + 1);
mp[a[i]] = i;
}
printf("%lld\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long modn = 1000000007;
inline long long mod(long long x) { return x % modn; }
int n;
vector<int> v[30];
int qt[30];
int main() {
int i, j;
string s;
cin >> s;
n = s.size();
for (i = 0; i < n; i++) v[s[i] - 'a'].push_back(i);
double ans = 0;
for (i = 0; i < 26; i++) {
if (v[i].empty()) continue;
if (v[i].size() == 1) {
ans += 1;
continue;
}
if (v[i].size() == 2) {
string t1 = s, t2 = s;
rotate(t1.begin(), t1.begin() + v[i][0], t1.end());
rotate(t2.begin(), t2.begin() + v[i][1], t2.end());
if (t1 != t2) ans += 2;
continue;
}
int mx = 0;
for (j = 0; j < n; j++) {
for (int k : v[i]) qt[s[(k + j) % n] - 'a']++;
int cur = 0;
int k;
for (k = 0; k < 26; k++)
if (qt[k] == 1) cur++;
mx = max(mx, cur);
for (int k : v[i]) qt[s[(k + j) % n] - 'a']--;
}
ans += mx;
}
printf("%.15f\n", ans / n);
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<long long, long long> > xs;
vector<pair<long long, long long> > ys;
vector<pair<long long, long long> > c;
vector<long long> m;
for (int i = 0; i < n; ++i) {
long long x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
x0 += 100;
y0 += 100;
x1 += 100;
y1 += 100;
x0 <<= 1;
x1 <<= 1;
y0 <<= 1;
y1 <<= 1;
if (x0 > x1) swap(x0, x1);
if (y0 > y1) swap(y0, y1);
xs.push_back(make_pair(x0, x1));
ys.push_back(make_pair(y0, y1));
c.push_back(make_pair((x0 + x1) >> 1, (y0 + y1) >> 1));
m.push_back(1LL * (x1 - x0) * (x1 - x0) * (x1 - x0));
for (int j = 0; j < i; ++j) {
long long CX = 0;
long long CY = 0;
long long mass = 0LL;
for (int k = j + 1; k <= i; ++k) {
CX += c[k].first * m[k];
CY += c[k].second * m[k];
mass += m[k];
}
if (CX < mass * xs[j].first || CX > mass * xs[j].second ||
CY < mass * ys[j].first || CY > mass * ys[j].second) {
cout << i;
int o = 0;
return o;
}
}
}
cout << n;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
struct node {
long long l, r;
long long color;
long long sum;
long long change;
} tree[maxn << 2];
long long n, m;
int a[maxn];
void pushup(long long cur) {
if (tree[cur * 2].color == tree[cur * 2 + 1].color)
tree[cur].color = tree[cur * 2].color;
tree[cur].sum = tree[cur * 2].sum + tree[cur * 2 + 1].sum;
}
void build(long long l, long long r, long long cur) {
tree[cur].l = l;
tree[cur].r = r;
tree[cur].sum = 0;
tree[cur].change = 0;
tree[cur].color = 0;
if (l == r) {
tree[cur].color = l;
return;
}
long long mid = (r + l) >> 1;
build(l, mid, cur << 1);
build(mid + 1, r, cur << 1 | 1);
pushup(cur);
}
void pushdown(long long cur) {
if (tree[cur].change) {
tree[cur * 2].sum +=
tree[cur].change * (tree[cur * 2].r - tree[cur * 2].l + 1);
tree[cur * 2 + 1].sum +=
tree[cur].change * (tree[cur * 2 + 1].r - tree[cur * 2 + 1].l + 1);
tree[cur * 2].change += tree[cur].change;
tree[cur * 2 + 1].change += tree[cur].change;
tree[cur].change = 0;
}
if (tree[cur].color)
tree[cur * 2].color = tree[cur * 2 + 1].color = tree[cur].color;
tree[cur].color = 0;
}
void update(long long pl, long long pr, long long l, long long r, long long cur,
long long x) {
if ((pl <= l && r <= pr && tree[cur].color) || l == r) {
tree[cur].sum += abs(x - tree[cur].color) * (r - l + 1);
tree[cur].change += abs(x - tree[cur].color);
tree[cur].color = x;
return;
}
pushdown(cur);
long long mid = (r + l) >> 1;
if (pl <= mid) update(pl, pr, l, mid, cur << 1, x);
if (pr > mid) update(pl, pr, mid + 1, r, cur << 1 | 1, x);
pushup(cur);
}
long long query(long long pl, long long pr, long long l, long long r,
long long cur) {
if (pl <= l && r <= pr) {
return tree[cur].sum;
}
pushdown(cur);
long long mid = (r + l) >> 1;
long long res = 0;
if (pl <= mid) res += query(pl, pr, l, mid, cur << 1);
if (pr > mid) res += query(pl, pr, mid + 1, r, cur << 1 | 1);
return res;
}
int main() {
while (~scanf("%lld%lld", &n, &m)) {
build(1, n, 1);
long long op, l, r, x;
while (m--) {
scanf("%lld%lld%lld", &op, &l, &r);
if (op == 1) {
scanf("%lld", &x);
update(l, r, 1, n, 1, x);
} else {
printf("%lld\n", query(l, r, 1, n, 1));
}
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, even = 0, odd = 0, epos, opos;
cin >> n;
int ara[n];
for (int i = 1; i <= n; i++) {
cin >> ara[i];
if (ara[i] % 2 == 0) {
even++;
epos = i;
} else {
odd++;
opos = i;
}
}
if (even > odd)
cout << opos << endl;
else
cout << epos << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n, m, x;
cin>>n>>m>>x;
int h[n];
cout<<"YES"<<endl;
set<pair<int, int> >s;
for(int i=1; i<=m; i++)
s.insert({0,i});
for(int i=0; i<n; i++){
cin >> h[i];
pair<int, int>p = *s.begin();
s.erase(p);
cout << p.second << ' ';
s.insert({h[i]+p.first, p.second});
}
}
return 0;
} | 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void show(T a, int n) {
for (int i = 0; i < n; ++i) cout << a[i] << ' ';
cout << endl;
}
template <class T>
void show(T a, int r, int l) {
for (int i = 0; i < r; ++i) show(a[i], l);
cout << endl;
}
const int N = 128;
const int M = 1440;
const int oo = 10000 * 10000 * 10;
int m, n, dn;
map<string, int> lis;
bool use[M] = {0};
int unext[M + 10][M + 10];
int lef;
int dp[N][M * 32];
bool take[N][M * 32];
string h_s[N];
int h_t[N];
int h_c[N];
int st[N];
int get_c[128];
bool cmp(int i, int j) { return h_t[i] < h_t[j]; }
int go(int i, int ti) {
int need = get_c[i];
int di = ti + (need - 1) / lef * M;
need -= (need - 1) / lef * lef;
di += unext[di % M][need];
return di;
}
int cnt[128] = {0};
int cal(int dex, int t) {
int i, j, k;
if (t >= dn * M) return 0;
if (dex >= n) return 0;
int &ret = dp[dex][t];
if (ret != -1) return ret;
ret = 0;
if (use[t % M]) {
ret = cal(dex, t + 1);
return ret;
}
ret = cal(dex + 1, t);
if (get_c[dex] == -1) return ret;
int dt = go(dex, t);
if (dt < h_t[st[dex]]) {
int tv = h_c[st[dex]] + cal(dex + 1, dt + 1);
if (tv > ret) {
ret = tv;
take[dex][t] = true;
}
}
return ret;
}
vector<int> s_d;
vector<int> s_s;
vector<int> s_t;
void print(int dex, int t) {
if (t >= dn * M || dex >= n) return;
int i, j;
if (take[dex][t]) {
s_d.push_back(dex);
s_s.push_back(t);
int dt = go(dex, t);
s_t.push_back(dt);
print(dex + 1, dt + 1);
} else if (use[t % M])
print(dex, t + 1);
else
print(dex + 1, t);
}
void solve() {
int i, j, k;
memset(get_c, -1, sizeof(get_c));
for (i = 0; i < n; ++i) {
if (lis.count(h_s[st[i]]) == 0)
continue;
else
get_c[i] = lis[h_s[st[i]]];
}
memset(take, 0, sizeof(take));
memset(dp, -1, sizeof(dp));
int ans = cal(0, 0);
print(0, 0);
printf("%d\n", ans);
printf("%d\n", s_d.size());
for (i = 0; i < s_d.size(); ++i) {
printf("%d ", st[s_d[i]] + 1);
printf("%d %02d:%02d ", s_s[i] / M + 1, (s_s[i] % M) / 60, s_s[i] % 60);
printf("%d %02d:%02d\n", s_t[i] / M + 1, (s_t[i] % M) / 60, s_t[i] % 60);
}
}
int main() {
int i, j, cas = 0;
scanf("%d %d %d", &m, &n, &dn);
vector<string> name;
for (i = 0; i < m; ++i) {
string s;
cin >> s;
name.push_back(s);
}
for (i = 0; i < m; ++i) {
int v;
scanf("%d", &v);
lis[name[i]] = v;
}
lef = 1440;
for (i = 0; i < 4; ++i) {
int h1, m1, h2, m2;
scanf("%d:%d-%d:%d\n", &h1, &m1, &h2, &m2);
int s = h1 * 60 + m1;
int t = h2 * 60 + m2;
for (j = s; j != t; j = (j + 1) % 1440) {
use[j] = true;
--lef;
}
use[j] = true;
--lef;
}
for (i = 0; i < M; ++i) {
if (use[i]) continue;
unext[i][1] = i;
for (j = 2; j <= lef; ++j) {
int k = unext[i][j - 1] + 1;
if (k == M) k = 0;
while (use[k]) {
++k;
if (k == M) k = 0;
}
unext[i][j] = k;
}
}
for (i = 0; i < M; ++i) {
if (use[i]) continue;
for (j = 1; j <= lef; ++j) {
int k = unext[i][j];
if (k >= i)
unext[i][j] = k - i;
else
unext[i][j] = M - i + k;
}
}
for (i = 0; i < n; ++i) {
cin >> h_s[i];
int a, b, d;
scanf(" %d %d:%d %d\n", &d, &a, &b, &h_c[i]);
h_t[i] = d * M - M + a * 60 + b;
st[i] = i;
}
sort(st, st + n, cmp);
solve();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
namespace io {
template <typename T>
void println(const T &t) {
cout << t << '\n';
}
template <typename T, typename... Args>
void println(const T &t, const Args &...rest) {
cout << t << ' ';
println(rest...);
}
template <typename T>
void print(const T &t) {
cout << t << ' ';
}
template <typename T, typename... Args>
void print(const T &t, const Args &...rest) {
cout << t;
print(rest...);
}
template <class T>
void scan(T &t) {
cin >> t;
}
template <class T, class... Args>
void scan(T &a, Args &...rest) {
cin >> a;
scan(rest...);
}
} // namespace io
using namespace io;
using ll = long long;
ll ple(ll fz, ll fm) { return fz / fm; }
ll pge(ll fz, ll fm) { return (fz + fm - 1) / fm; }
ll le(ll fz, ll fm) { return fz >= 0 ? ple(fz, fm) : -pge(-fz, fm); }
ll ge(ll fz, ll fm) { return fz >= 0 ? pge(fz, fm) : -ple(-fz, fm); }
int main() {
ios::sync_with_stdio(false);
ll n, l, r, k;
cin >> n >> l >> r >> k;
ll x = r - l + 1;
if (x <= 0) x += n;
ll y = n - x;
ll ans = -1;
if (n <= (ll)cbrt(k)) {
for (ll a = 0; a <= x; a++) {
for (ll b = 0; b <= y; b++) {
ll s = a + x + b + y;
ll bb = k - a - x;
if (bb >= 0 && bb % s == 0) {
ans = max(ans, a + b);
}
if (a > 0 && (bb + 1) >= 0 && (bb + 1) % s == 0) {
ans = max(ans, a + b);
}
}
}
} else {
for (ll t = 0; t <= k / n; t++) {
ll bb = k - n * t - x;
if (bb >= 0) {
if (t == 0) {
if (bb <= x) ans = max(ans, bb + y);
if (bb + 1 <= x) ans = max(ans, bb + 1 + y);
} else {
ll minz = max(ge(bb - x, t), ge(bb, t + 1));
ll maxz = min(le(bb, t), le(bb + y, t + 1));
if (maxz >= minz) {
ans = max(ans, maxz);
}
bb++;
minz = max(ge(bb - x, t), ge(bb, t + 1));
maxz = min(le(bb - 1, t), le(bb + y, t + 1));
if (maxz >= minz) {
ans = max(ans, maxz);
}
}
}
}
}
cout << ans << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 10;
const double PI = cos(-1.0);
map<int, int> a, b;
int n, m, t;
int main() {
cin >> t;
while (t--) {
int x;
cin >> n >> m;
a.clear();
b.clear();
for (int i = 1; i <= n; i++) {
cin >> x;
a[x] = 1;
}
for (int i = 1; i <= m; i++) {
cin >> x;
b[x] = 1;
}
int f = 0;
for (int i = 1; i <= 1000; i++) {
if (a[i] && b[i]) {
f = 1;
printf("YES\n1 %d\n", i);
break;
}
}
if (f == 0) printf("NO\n");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, i, x, ans = 0, rem = 0;
scanf("%lld", &n);
vector<long long> ara;
for (i = 0; i < n; i++) {
scanf("%lld", &x);
if (x) rem = i;
ara.push_back(x);
}
ans = ara[0];
for (i = 1; i <= rem; i++) {
if (ara[i] == 1) {
ans++;
} else if (ara[i] == 0 && ara[i - 1] == 1) {
ans++;
}
}
printf("%lld\n", ans);
}
| 1 |
#include <bits/stdc++.h>
long long shouldendl = 1;
long long INF = (long long)(1e16 + 7);
long long MOD = (long long)(1e9 + 7);
using namespace std;
long long modInverse(long long a) {
long long m = MOD, m0 = m, y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m, t = m;
m = a % m, a = t, t = y;
y = x - q * y, x = t;
}
if (x < 0) x += m0;
return x;
}
long long add(long long a, long long b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
long long mul(long long a, long long b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
long long sub(long long a, long long b) {
long long ans = ((a % MOD - b % MOD)) % MOD;
if (ans < 0) ans += MOD;
return ans;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long get(long long f = 2) {
if (f != 2) return 1;
long long t;
cin >> t;
;
return t;
}
template <class T>
long long logd(T name) {
cout << name << endl;
return 0;
}
long long power(long long x, long long y) {
if (y == 0) return 1;
long long res = 1;
x = x % MOD;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U> &l, const std::pair<T, U> &r) {
return {l.first + r.first, l.second + r.second};
}
long long popcnt(long long x) { return __builtin_popcountll(x); }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
template <class T>
bool input(vector<T> &v, long long n = -1) {
if (n == -1) assert(v.size() != 0);
if (n == -1) n = v.size();
for (auto &i : v) cin >> i;
return 0;
}
template <class T>
bool print(vector<T> v, long long l = 0, long long r = -1) {
if (r == -1) r = v.size();
for (long long i = l; i < r; i++) cout << v[i] << " ";
return 0;
}
void MAIN(long long i, long long t);
void test(long long t) {
for (long long i = 1; i <= t; i++) {
MAIN(i, t);
if (shouldendl) cout << "\n";
}
}
void MAIN(long long current_test_case, long long total_test_cases) {
long long n, m;
cin >> n >> m;
vector<long long> v(n);
input(v);
vector<long long> inc(n, 1), dec(n, 1), dec1(n, 1);
for (long long i = 1; i < n; i++) {
if (v[i - 1] <= v[i]) inc[i] += inc[i - 1];
}
for (long long i = 1; i < n; i++)
if (v[i - 1] >= v[i]) dec[i] += dec[i - 1];
for (long long i = n - 2; i >= 0; i--)
if (v[i] >= v[i + 1]) {
dec1[i] += dec1[i + 1];
}
for (long long i = 0; i < m; i++) {
long long l, r;
cin >> l >> r;
l--;
r--;
long long mid = r - dec[r] + 1;
long long sum =
min(mid - l + 1, inc[mid]) + min(dec1[mid], r - mid + 1) - 1;
if (sum >= (r - l + 1)) {
cout << "Yes";
} else {
cout << "No";
}
cout << endl;
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
shouldendl = 1;
test(get(1));
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
namespace isap {
struct edge {
int v, c;
};
vector<edge> eg;
vector<int> to[20010];
int dis[20010], gap[20010], vcnt;
void add(int u, int v, int c) {
edge e;
e.v = v;
e.c = c;
to[u].push_back(eg.size());
eg.push_back(e);
}
int dfs(int u, int f) {
if (u == vcnt) return f;
int g = f, mh = vcnt - 1;
for (int i = 0; i <= int(to[u].size() - 1); ++i) {
edge &e = eg[to[u][i]], &ev = eg[to[u][i] ^ 1];
if (e.c) {
if (dis[e.v] + 1 == dis[u]) {
int t = dfs(e.v, min(g, e.c));
e.c -= t, ev.c += t, g -= t;
if (!g || dis[1] == vcnt) return f - g;
}
mh = min(mh, dis[e.v]);
}
}
if (!--gap[dis[u]]) dis[1] = vcnt;
++gap[dis[u] = mh + 1];
return f - g;
}
int run() {
int r = 0;
gap[0] = vcnt;
while (dis[1] < vcnt) r += dfs(1, ~0u >> 1);
return r;
}
} // namespace isap
int n, m, g, sex[10010];
int main() {
scanf("%d%d%d", &n, &m, &g);
isap::vcnt = n + m + 2;
for (int i = 1; i <= n; ++i) scanf("%d", &sex[i]);
for (int i = 1; i <= n; ++i) {
int v;
scanf("%d", &v);
if (!sex[i])
isap::add(1, i + 1, v), isap::add(i + 1, 1, 0);
else
isap::add(i + 1, n + m + 2, v), isap::add(n + m + 2, i + 1, 0);
}
int ans = 0;
for (int i = 1; i <= m; ++i) {
int tsex, w, k, isf;
scanf("%d%d%d", &tsex, &w, &k);
ans += w;
for (int j = 1; j <= k; ++j) {
int t;
scanf("%d", &t);
if (!tsex)
isap::add(n + 1 + i, t + 1, ~0u >> 1), isap::add(t + 1, n + 1 + i, 0);
else
isap::add(n + 1 + i, t + 1, 0), isap::add(t + 1, n + 1 + i, ~0u >> 1);
}
scanf("%d", &isf);
if (!tsex)
isap::add(1, n + 1 + i, w + isf * g), isap::add(n + 1 + i, 1, 0);
else
isap::add(n + 1 + i, n + m + 2, w + isf * g),
isap::add(n + m + 2, n + 1 + i, 0);
}
printf("%d\n", ans - isap::run());
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
const int INF = 0x3f3f3f3f;
int n, a, b;
string s, pref = "";
int z[N];
int dp[N];
void calc(string s) {
reverse(s.begin(), s.end());
int n = s.size();
fill(z, z + n, 0);
int l = 0, r = 0;
for (int i = 1; i < n; i++) {
if (i <= r) {
z[i] = min(z[i - l], r - i + 1);
}
while (i + z[i] < n && s[i + z[i]] == s[z[i]]) {
z[i]++;
}
if (i + z[i] - 1 > r) {
l = i, r = i + z[i] - 1;
}
}
}
int main() {
memset(dp, INF, sizeof dp);
cin >> n >> a >> b;
cin >> s;
dp[0] = a;
pref += s[0];
for (int i = 1; i < n; i++) {
dp[i] = min(dp[i], dp[i - 1] + a);
pref.push_back(s[i]);
calc(pref);
reverse(z, z + (int)pref.size());
for (int pos = 0; pos < n; pos++) {
int len = z[pos];
if (i - len >= pos) {
dp[i] = min(dp[i], dp[i - len] + b);
} else {
dp[i] = min(dp[i], dp[pos] + b);
}
}
}
cout << dp[n - 1];
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int u = 1010;
vector<int> a[u];
int d[u][u], e[u * u], v[u], x[u], y[u], n, m, k, i, j, l, r, mid;
bool dfs(int x, int k) {
if (x > n) return 1;
if (v[x]) return dfs(x + 1, k);
int i, temp;
for (i = 0; i < a[x].size(); i++) k -= !v[a[x][i]]++;
if (k >= 0 && dfs(x + 1, k)) return 1;
temp = max(-1, k);
for (i = 0; i < a[x].size(); i++) k += !--v[a[x][i]];
v[x] = 1, k--;
if (k > temp && dfs(x + 1, k)) return 1;
v[x] = 0;
return 0;
}
bool calc(int dist) {
for (i = 1; i <= n; i++) a[i].clear(), v[i] = 0;
for (i = 1; i < n; i++)
for (j = 1; j <= n; j++)
if (d[i][j] > dist) a[i].push_back(j), a[j].push_back(i);
return dfs(1, k);
}
int main() {
cin >> n >> k;
for (i = 1; i <= n; i++) cin >> x[i] >> y[i];
for (i = 1; i < n; i++)
for (j = i + 1; j <= n; j++)
e[++m] = d[i][j] =
(x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
sort(e, e + m + 1);
m = unique(e, e + m + 1) - e - 1;
l = 0, r = m;
while (l < r) {
mid = (l + r) / 2;
if (calc(e[mid]))
r = mid;
else
l = mid + 1;
}
calc(e[l]);
for (i = 1; i <= n; i++)
if (v[i]) cout << i << ' ', k--;
for (i = 1; i <= n; i++)
if (k && !v[i]) cout << i << ' ', k--;
cout << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long n;
int main() {
long long t, rr, i, ii, u, k, p, lh, rh, md, zz;
scanf("%lld", &t);
for (rr = 0; rr < t; rr++) {
scanf("%lld", &n);
k = n / 2;
p = 1;
for (; k < n - 1;) {
p += (k + n) / 2 - k;
for (ii = 0; ii < 2; ii++) {
k = (k + n) / 2;
}
}
printf("? %lld\n", p);
fflush(stdout);
scanf("%lld", &k);
u = 1;
zz = n;
for (lh = 1, rh = n - 1; lh <= rh; u *= -1) {
md = (lh + rh) / 2;
p += md * u;
printf("? %lld\n", p);
fflush(stdout);
scanf("%lld", &k);
if (k) {
zz = md;
rh = md - 1;
} else {
lh = md + 1;
}
}
printf("= %lld\n", zz);
fflush(stdout);
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
const double error = 1e-7;
const double PI = acos(-1);
mt19937_64 rng(
(unsigned int)chrono::system_clock::now().time_since_epoch().count());
inline long long int MOD(long long int x, long long int m = mod) {
long long int y = x % m;
return (y >= 0) ? y : y + m;
}
const int inf = 1e9 + 5;
const long long int infl = 0x3f3f3f3f3f3f3f3f;
const int nmax = 1e7 + 3;
int main() {
int n;
cin >> n;
if (n & 1) {
cout << "black";
} else {
cout << "white\n";
cout << 1 << " " << 2;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T>
inline void print(T first) {
if (first < 0) {
putchar('-');
return print(-first);
}
if (first < 10) {
putchar('0' + first);
return;
}
print(first / 10);
putchar(first % 10 + '0');
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
gn(first);
gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
gn(first);
gn(second);
gn(z);
}
template <class T>
inline void println(T first) {
print(first);
putchar('\n');
}
template <class T>
inline void printsp(T first) {
print(first);
putchar(' ');
}
template <class T1, class T2>
inline void print(T1 x1, T2 x2) {
printsp(x1), println(x2);
}
template <class T1, class T2, class T3>
inline void print(T1 x1, T2 x2, T3 x3) {
printsp(x1), printsp(x2), println(x3);
}
template <class T1, class T2, class T3, class T4>
inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {
printsp(x1), printsp(x2), printsp(x3), println(x4);
}
vector<pair<int, pair<int, int> > > adj[300300];
long long dis[300300];
int pre[300300], flag[300300];
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
greater<pair<long long, int> > >
pq;
int main() {
int n, m, k;
gn(n, m, k);
for (int i = 1; i <= n; i++) dis[i] = 0x3f3f3f3f3f3f3f3fLL;
for (int i = 1; i <= m; i++) {
int u, v, w;
gn(u, v, w);
adj[u].push_back(pair<int, pair<int, int> >(v, pair<int, int>(w, i)));
adj[v].push_back(pair<int, pair<int, int> >(u, pair<int, int>(w, i)));
}
dis[1] = 0;
for (int i = 1; i <= n; i++) pq.push(pair<long long, int>(dis[i], i));
println(k);
while (!pq.empty() && k) {
int u = pq.top().second;
long long d = pq.top().first;
pq.pop();
if (d > dis[u]) continue;
if (pre[u]) {
printsp(pre[u]);
flag[pre[u]] = 1;
k--;
}
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i].first;
if (dis[v] > d + adj[u][i].second.first) {
dis[v] = d + adj[u][i].second.first;
pre[v] = adj[u][i].second.second;
pq.push(pair<long long, int>(dis[v], v));
}
}
}
for (int i = 1; k && i <= m; i++)
if (!flag[i]) printsp(i), k--;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff, SINF = 0x3f3f3f3f;
const double PI = 3.141592653589793;
const long long LINF = 0x7fffffffffffffffLL, SLINF = 0x3f3f3f3f3f3f3f3fLL;
int _v, _f, _c;
long long _l;
void read() {
_c = getchar(), _v = _f = 0;
while (_c < '0' || _c > '9') {
if (_c == -1) {
_f = -1;
return;
} else if (_c == '-') {
_f = 1;
}
_c = getchar();
}
while (_c <= '9' && _c >= '0') {
_v = (_v << 3) + (_v << 1) + (_c ^ 48);
_c = getchar();
}
if (_f) {
_v = -_v;
}
}
void read_ll() {
_c = getchar(), _l = _f = 0;
while (_c < '0' || _c > '9') {
if (_c == -1) {
_f = -1;
return;
} else if (_c == '-') {
_f = 1;
}
_c = getchar();
}
while (_c <= '9' && _c >= '0') {
_l = (_l << 3) + (_l << 1) + (_c ^ 48);
_c = getchar();
}
if (_f) {
_l = -_l;
}
}
void read_str(char* _s) {
_v = 0, _c = getchar();
while (_c != '\n' && _c != ' ' && _c != -1) {
_s[_v++] = (char)_c;
_c = getchar();
}
if (_c == -1) _f = -1;
}
const int MAXN = 200005, MAXM = 100005, MAXK = 100005, MOD = 1e9 + 7;
const double EPS = 1e-8;
int n, k;
struct Edge {
int t, n;
} edge[MAXN * 2];
int head[MAXN], en;
void add_e(int f, int t) {
edge[en].t = t, edge[en].n = head[f], head[f] = en++;
}
int siz[MAXN];
long long dp[MAXN][5], dp2[MAXN][5], tot[5];
void dp_work(int cur = 0, int last = -1) {
siz[cur] = 1;
for (int i = head[cur]; ~i; i = edge[i].n) {
if (edge[i].t != last) {
dp_work(edge[i].t, cur);
siz[cur] += siz[edge[i].t];
for (int j = 0; j < k; j++) {
for (int l = 0; l < k; l++) {
dp[cur][(j + l + 1) % k] += dp2[cur][j] * dp2[edge[i].t][l];
}
dp[cur][(j + 1) % k] += dp2[cur][j];
}
dp2[cur][1 % k]++;
for (int j = 0; j < k; j++) {
dp2[cur][(j + 1) % k] += dp2[edge[i].t][j];
}
}
}
for (int i = 0; i < k; i++) {
tot[i] += dp[cur][i] + dp2[cur][i];
}
}
int main() {
memset(head, -1, sizeof(head));
n = (read(), _v), k = (read(), _v);
for (int i = 1; i < n; i++) {
int f = (read(), _v) - 1, t = (read(), _v) - 1;
add_e(f, t), add_e(t, f);
}
dp_work();
long long flen = 0;
for (int i = 1; i < n; i++) {
flen += (long long)(siz[i]) * (n - siz[i]);
}
for (int i = 1; i < k; i++) {
flen += tot[i] * (k - i);
}
printf("%lld\n", flen / k);
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
int x, i;
scanf("%d", &x);
if (x < 5) {
printf("1");
} else {
if (x % 5 == 0) {
printf("%d", x / 5);
} else {
printf("%d", x / 5 + 1);
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
map<string, int> mp;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
mp[s]++;
}
int ans = 0;
for (int i = 0; i < m; i++) {
string s;
cin >> s;
if (mp[s] == 1) ans++;
mp[s]++;
}
n -= ans;
m -= ans;
if (n + (ans % 2) > m) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0, q = 0, p = 0;
cin >> n >> p;
int arr[500] = {0};
for (int i = 1; i <= p; i++) cin >> arr[i];
cin >> q;
for (int i = p + 1; i <= q + p; i++) cin >> arr[i];
int cntr = 0;
sort(arr + 0, arr + p + q + 1);
int dif = abs(n - (p + q));
for (int i = 0; i <= n + dif; i++) {
for (int j = 1; j <= q + p; j++) {
if (arr[j] == i) {
cntr++;
break;
}
}
}
if (cntr == n)
cout << "I become the guy." << endl;
else
cout << "Oh, my keyboard!" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize( \
"Ofast,no-stack-protector,unroll-loops,no-stack-protector,fast-math")
using namespace std;
const int N = 2e5 + 5, M = 2 * N + 5;
struct DoubleHash {
const int p1 = 31, p2 = 1337;
const int m1 = 1e9 + 9, m2 = 1e9 + 7;
vector<long long> pow1, pow2, h1, h2;
void build(vector<int> &s) {
int n = s.size();
pow1.resize(n);
h1.resize(n);
pow1[0] = 1;
for (int i = 1; i < n; ++i) pow1[i] = pow1[i - 1] * p1 % m1;
h1[0] = s[0];
for (int i = 1; i < n; ++i) h1[i] = (h1[i - 1] * p1 + s[i]) % m1;
pow2.resize(n);
h2.resize(n);
pow2[0] = 1;
for (int i = 1; i < n; ++i) pow2[i] = pow2[i - 1] * p2 % m2;
h2[0] = s[0];
for (int i = 1; i < n; ++i) h2[i] = (h2[i - 1] * p2 + s[i]) % m2;
}
pair<long long, long long> getHash(int i, int j) {
long long a = h1[j];
if (i) a -= h1[i - 1] * pow1[j - i + 1];
a = (a % m1 + m1) % m1;
long long b = h2[j];
if (i) b -= h2[i - 1] * pow2[j - i + 1];
b = (b % m2 + m2) % m2;
return make_pair(a, b);
}
};
map<int, vector<int> > idx;
int main() {
int n;
scanf("%d", &n);
vector<int> a(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
idx[a[i]].emplace_back(i);
}
DoubleHash h;
h.build(a);
vector<pair<int, int> > p;
for (auto P : idx) {
auto v = P.second;
int sz = v.size();
for (int i = 0; i < sz; ++i) {
for (int j = i + 1; j < sz; ++j) {
int len = v[j] - v[i] - 1;
if (v[j] + len >= n) continue;
if (h.getHash(v[i], v[i] + len) == h.getHash(v[j], v[j] + len))
p.emplace_back(v[i], v[i] + len);
}
}
}
sort(p.begin(), p.end(), [](pair<int, int> &a, pair<int, int> &b) {
if (a.second - a.first == b.second - b.first) return a.first < b.first;
return a.second - a.first < b.second - b.first;
});
int lst = -1;
int sz = p.size();
if (sz) lst = p[0].second;
for (int i = 1; i < sz; ++i)
if (p[i].first > lst) lst = p[i].second;
printf("%d\n", n - ++lst);
while (lst < n) printf("%d ", a[lst++]);
}
| 7 |
#include <bits/stdc++.h>
size_t n, m, l[200001], r[200001], pos[26];
char s[200001], t[200001];
int main(int argc, char const *argv[]) {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cin >> (s + 1) >> (t + 1);
n = std::strlen(s + 1);
m = std::strlen(t + 1);
std::fill(l, l + n + 1, 0);
std::fill(r, r + n + 1, m + 1);
size_t p = 1;
for (size_t i = 1; i <= n; i++) {
if (p <= m && s[i] == t[p]) {
l[i] = pos[s[i] - 'a'] = p++;
} else {
l[i] = pos[s[i] - 'a'];
}
}
p = m;
std::fill(pos, pos + 26, m + 1);
for (size_t i = n; i >= 1; i--) {
if (p && s[i] == t[p]) {
r[i] = pos[s[i] - 'a'] = p--;
} else {
r[i] = pos[s[i] - 'a'];
}
}
for (size_t i = 1; i <= n; i++) {
if (l[i] < r[i]) {
std::cout << "No";
return 0;
}
}
std::cout << "Yes";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int, int> > QQ;
int n, total;
vector<pair<int, int> > vec, ans;
int main() {
int e, num, error, a00, a22, b00, b22;
while (scanf("%d %d", &n, &total) + 1) {
ans.clear();
while (QQ.size()) QQ.pop();
for (e = 1; e <= n; e++) {
scanf("%d", &num);
if (num > 0) QQ.push(make_pair(num, e));
}
error = 0;
while (!error && QQ.size()) {
a00 = QQ.top().first, a22 = QQ.top().second, QQ.pop();
vec.clear();
for (e = 1; e <= a00; e++) {
if (QQ.size() == 0) {
error = 1;
break;
} else {
b00 = QQ.top().first, b22 = QQ.top().second, QQ.pop();
ans.push_back(make_pair(a22, b22));
if (b00 - 1 > 0) vec.push_back(make_pair(b00 - 1, b22));
}
}
if (error) break;
for (e = 0; e < (int)vec.size(); e++) QQ.push(vec.at(e));
}
printf("%s\n", error ? "No" : "Yes");
if (!error) {
printf("%d\n", ans.size());
if (ans.size())
for (e = 0; e < (int)ans.size(); e++)
printf("%d %d\n", ans.at(e).first, ans.at(e).second);
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
struct debugger {
static void call(string::iterator it, string::iterator ed) {}
template <typename T, typename... aT>
static void call(string::iterator it, string::iterator ed, T a, aT... rest) {
string b;
for (; *it != ','; ++it)
if (*it != ' ') b += *it;
cout << b << "=" << a << " ";
call(++it, ed, rest...);
}
};
int main() {
int n;
scanf("%d", &n);
int ans = 0;
while (n) ans += n & 1, n >>= 1;
printf("%d\n", ans);
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000,100000000000")
using namespace std;
const long long inf = 1e18 + 7;
const long long mod = 1e9 + 7;
const double eps = 1e-12;
const double PI = 2 * acos(0.0);
const double E = 2.71828;
int a[305];
bool used[305];
vector<pair<int, int> > q;
int main(void) {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (long long(i) = 0; (i) < (long long)(k); (i)++)
scanf("%d", a + i), a[i]--;
if (k == n) {
puts("-1");
exit(0);
}
for (long long(i) = 0; (i) < (long long)(k); (i)++) used[a[i]] = true;
int v = 0;
for (long long(i) = 0; (i) < (long long)(n); (i)++)
if (!used[i]) {
v = i;
break;
}
int x = a[0], y = a[1];
q.push_back(make_pair(v, x));
q.push_back(make_pair(v, y));
for (long long(i) = 0; (i) < (long long)(n); (i)++)
if (i != x && i != y && i != v) q.push_back(make_pair(x, i));
for (long long(i) = 0; (i) < (long long)(n); (i)++)
for (long long(j) = 0; (j) < (long long)(i); (j)++)
if (i != x && j != y && j != x && i != y) {
q.push_back(make_pair(j, i));
}
for (long long(i) = 0; (i) < (long long)(n); (i)++)
if (!used[i] && i != v) {
q.push_back(make_pair(i, y));
}
if (q.size() < m) {
puts("-1");
exit(0);
}
for (long long(i) = 0; (i) < (long long)(m); (i)++)
printf("%d %d\n", q[i].first + 1, q[i].second + 1);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int qreadInt() {
int x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') (x *= 10) += (ch ^ 48), ch = getchar();
return x;
}
int s[2010][2010], Ans[5010], dp[2010][2010];
pair<int, int> q[4000010];
int vis[2010][2010];
int main() {
int n = qreadInt(), m = qreadInt(), l = 1, r = 0;
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (n); ++j) {
char ch = getchar();
while (ch < 'a' || ch > 'z') ch = getchar();
s[i][j] = (ch - 'a');
}
for (int i = (1); i <= (n); ++i) dp[i][0] = dp[0][i] = -233;
dp[1][1] = m - min(s[1][1], 1);
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (n); ++j) {
if (i == 1 && j == 1) continue;
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
dp[i][j] -= ((s[i][j] == 0) ? 0 : 1);
}
for (int i = (1); i <= (2 * n - 1); ++i) Ans[i] = 27;
int now = n, res = 0;
while (now) {
int flag = 0;
for (int i = (1); i <= (n - now + 1); ++i)
if (dp[n - i + 1][now + i - 1] >= 0) {
q[++r] = make_pair(n - i + 1, now + i - 1);
flag = 1;
}
res = n + now - 1;
if (flag) break;
now--;
}
if (now == 0) {
now = n - 1;
while (now) {
int flag = 0;
for (int i = (1); i <= (now); ++i)
if (dp[now - i + 1][i] >= 0) {
q[++r] = make_pair(now - i + 1, i);
flag = 1;
}
res = now;
if (flag) break;
now--;
}
}
if (now == 0) {
res = 0;
q[++r] = make_pair(1, 1);
Ans[1] = s[1][1];
}
for (int i = (1); i <= (res); ++i) Ans[i] = 0;
while (l <= r) {
pair<int, int> t = q[l];
if (t.first == n && t.second == n) break;
if (t.first + t.second - 1 > res && Ans[t.first + t.second - 1] < 26 &&
Ans[t.first + t.second - 1] != s[t.first][t.second]) {
l++;
continue;
}
int dx = t.first + 1, dy = t.second;
int cx = t.first, cy = t.second + 1;
if (dx > n) dx = cx, dy = cy;
if (cy > n) cx = dx, cy = dy;
int Min = min(s[dx][dy], s[cx][cy]);
Ans[dx + dy - 1] = min(Ans[dx + dy - 1], Min);
if (s[dx][dy] == Min && !vis[dx][dy])
q[++r] = make_pair(dx, dy), vis[dx][dy] = 1;
if (s[cx][cy] == Min && !vis[cx][cy])
q[++r] = make_pair(cx, cy), vis[cx][cy] = 1;
++l;
}
for (int i = (1); i <= (2 * n - 1); ++i) putchar('a' + Ans[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long int sum = 0;
map<long long int, long long int> mp;
void dfs(long long int i, long long int j, long long int c) {
if (i == j)
return;
else if (i > j) {
mp[i] += c;
i /= 2;
} else {
mp[j] += c;
j /= 2;
}
dfs(i, j, c);
}
void dfs2(long long int i, long long int j) {
if (i == j)
return;
else if (i > j) {
sum += mp[i];
i /= 2;
} else {
sum += mp[j];
j /= 2;
}
dfs2(i, j);
}
int main() {
long long int q, x, y, t, c;
cin >> q;
while (q--) {
cin >> x;
if (x == 1) {
cin >> y >> t >> c;
dfs(y, t, c);
} else if (x == 2) {
sum = 0;
cin >> y >> t;
dfs2(y, t);
cout << sum << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long int result = 0;
scanf("%d", &n);
int i;
for (i = 1; i <= n; i = i * 10) {
result = result + n - i + 1;
}
printf("%I64d\n", result);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
long long cases, no_points, xcoord, ycoord;
cin >> cases;
for (int i = 0; i < cases; i++)
{
vector <long long> xpoints;
vector <long long> ypoints;
long long choicesx,choicesy = 1;
cin >> no_points;
for (int x = 0; x < no_points; x++)
{
cin >> xcoord >> ycoord;
xpoints.push_back(xcoord);
ypoints.push_back(ycoord);
}
if(no_points % 2 == 1){
cout << 1 << "\n";
}
else{
sort(xpoints.begin(), xpoints.end());
sort(ypoints.begin(), ypoints.end());
choicesx = xpoints[(no_points/2)] - xpoints[(no_points/2) - 1] + 1;
choicesy = ypoints[(no_points/2)] - ypoints[(no_points/2) - 1] + 1;
cout << choicesx * choicesy << "\n";
}
}
return 0;
} | 3 |
#include <bits/stdc++.h>
using namespace std;
long long const MAXN = 1e5 + 8;
long long const KAM = 2e2 + 8;
long long const delta = 1000000007;
vector<long long> st[MAXN];
vector<long long> en[MAXN];
long long s[MAXN];
long long t[MAXN];
long long d[MAXN];
long long w[MAXN];
long long ok[MAXN];
long long dp[MAXN][KAM];
int main() {
std::ios::sync_with_stdio(0);
;
cin.tie(0);
cout.tie(0);
long long n, m, k;
cin >> n >> m >> k;
for (long long i = 1; i <= k; i++) {
cin >> s[i] >> t[i] >> d[i] >> w[i];
st[s[i]].push_back(i);
;
en[t[i]].push_back(i);
;
}
multiset<pair<long long, pair<long long, long long> > > temp;
for (long long i = 1; i <= n; i++) {
for (long long j = 0; j < st[i].size(); j++)
temp.insert(make_pair(w[st[i][j]], make_pair(d[st[i][j]], st[i][j])));
for (long long j = 0; j < en[i - 1].size(); j++)
temp.erase(temp.find(make_pair(
w[en[i - 1][j]], make_pair(d[en[i - 1][j]], en[i - 1][j]))));
if (temp.size()) ok[i] = temp.rbegin()->second.second;
}
for (long long i = 0; i <= m; i++) {
for (long long j = n; j >= 1; j--) {
if (ok[j])
dp[j][i] = dp[d[ok[j]] + 1][i] + w[ok[j]];
else
dp[j][i] = dp[j + 1][i];
if (i) dp[j][i] = min(dp[j][i], dp[j + 1][i - 1]);
}
}
long long ans = dp[1][m];
for (long long i = m - 1; i >= 0; i--) ans = min(ans, dp[1][i]);
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, x, y, T, ans, big, cas, r[1000], len, stage, num, all;
char s[1000];
bool flag;
int main() {
scanf("%s", &s);
len = strlen(s);
for (i = 'a'; i <= 'z'; i++) r[i] = true;
for (i = 'A'; i <= 'Z'; i++) r[i] = true;
for (i = '0'; i <= '9'; i++) r[i] = true;
r['_'] = true;
stage = 1;
num = 0;
for (i = 0; i <= len; i++) {
if (stage == 1) {
if (i == len) {
printf("NO\n");
return 0;
} else if (s[i] == '@') {
stage = 2;
if (num > 16 || num <= 0) {
printf("NO\n");
return 0;
}
num = 0;
all = 0;
} else if (!r[s[i]]) {
printf("NO\n");
return 0;
} else {
num++;
}
} else if (stage == 2) {
if (i == len || s[i] == '/') {
stage = 3;
if (num > 16 || num <= 0) {
printf("NO\n");
return 0;
}
num = 0;
if (all > 32 || all <= 0) {
printf("NO\n");
return 0;
}
all = 0;
if (i == len) {
printf("YES\n");
return 0;
}
} else if (s[i] == '.') {
if (num > 16 || num <= 0) {
printf("NO\n");
return 0;
}
num = 0;
all++;
} else if (!r[s[i]]) {
printf("NO\n");
return 0;
} else {
num++;
all++;
}
} else {
if (i == len) {
if (num > 16 || num <= 0) {
printf("NO\n");
return 0;
}
printf("YES\n");
} else if (!r[s[i]]) {
printf("NO\n");
return 0;
} else {
num++;
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline void read(int &data) {
data = 0;
int e = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') e = -1, ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + ch - 48, ch = getchar();
data *= e;
}
inline void read(long long &data) {
data = 0;
int e = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') e = -1, ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + ch - 48, ch = getchar();
data *= e;
}
inline void write(int data) {
if (data < 0) data = -data, putchar('-');
if (data > 9) write(data / 10);
putchar(data % 10 + 48);
}
inline void write(long long data) {
if (data < 0) data = -data, putchar('-');
if (data > 9) write(data / 10);
putchar(data % 10 + 48);
}
bool cmp(int a, int b) { return a < b; }
priority_queue<long long, vector<long long>, greater<long long> > q;
long long n, x, ans = 0;
int main() {
read(n);
for (int i = 1; i <= n; ++i) {
read(x);
if (q.empty() || q.top() > x)
q.push(x);
else {
ans += x - q.top();
q.pop();
q.push(x), q.push(x);
}
}
write(ans), puts("");
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, MOD = 1e9 + 7;
int n, k, last[N], dp[N], b[N], sum;
string s;
inline bool cmp(int x, int y) { return last[x] < last[y]; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> s;
for (int i = 1; i <= (int)s.size(); i++) {
int c = s[i - 1] - 'a';
last[c] = i;
sum = (sum - dp[c] + MOD) % MOD;
dp[c] = (dp[c] + sum + 1) % MOD;
sum = (sum + dp[c]) % MOD;
}
for (int j = 0; j < k; j++) {
b[j] = j;
}
sort(b, b + k, cmp);
for (int i = 1; i <= n; i++) {
int c = b[(i - 1) % k];
sum = (sum - dp[c] + MOD) % MOD;
dp[c] = (dp[c] + sum + 1) % MOD;
sum = (sum + dp[c]) % MOD;
}
cout << (sum + 1) % MOD;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int kN = 2000;
const int kD = 10;
int add[kN];
int arr[kN][kN];
int dp[kN][kN];
int costs[kN];
int sizes[kN];
vector<int> orders[kN];
string inc;
void ComputeOrder(int n) {
vector<int> order(n);
for (int i = 0; i < n; ++i) {
order[i] = i;
}
reverse(order.begin(), order.end());
for (int pos = 0; pos < kN; ++pos) {
vector<int> cnt(kD);
for (int i = 0; i < n; ++i) {
++cnt[arr[i][pos]];
}
for (int i = 1; i < kD; ++i) {
cnt[i] += cnt[i - 1];
}
orders[pos].resize(n);
for (int x : order) {
int ind = --cnt[arr[x][pos]];
orders[pos][ind] = x;
}
reverse(orders[pos].begin(), orders[pos].end());
order = orders[pos];
}
}
int Overhead(const vector<int>& cnt) {
int res = 0;
for (int d = kD; d < 2 * kD; ++d) {
res += cnt[d];
}
return res;
}
int main() {
std::ios_base::sync_with_stdio(false);
memset(arr, 0, sizeof(arr));
cin >> inc;
reverse(inc.begin(), inc.end());
for (int i = 0; i < inc.size(); ++i) {
add[i] = inc[i] - '0';
if (inc[i] == '?') {
add[i] = -1;
}
}
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
string num;
cin >> num;
sizes[i] = num.size();
reverse(num.begin(), num.end());
for (int j = 0; j < num.size(); ++j) {
arr[i][j] = num[j] - '0';
}
}
for (int i = 0; i < kD; ++i) {
cin >> costs[i];
}
ComputeOrder(n);
memset(dp, 0, sizeof(dp));
for (int pos = kN - 2; pos >= 0; --pos) {
int lhs = add[pos] == -1 ? 0 : add[pos];
int rhs = add[pos] == -1 ? kD : add[pos] + 1;
if (pos + 1 == inc.size() && add[pos] == -1) {
lhs = 1;
}
for (int d = lhs; d < rhs; ++d) {
vector<int> cnt(2 * kD);
int cost = 0;
for (int i = 0; i < n; ++i) {
int dig = arr[i][pos] + d;
++cnt[dig];
cost +=
(dig != 0 || pos < sizes[i] || pos < inc.size()) * costs[dig % kD];
}
int overhead = Overhead(cnt);
dp[pos][0] = max(dp[pos][0], cost + dp[pos + 1][overhead]);
if (pos == 0) {
continue;
}
for (int k = 1; k <= n; ++k) {
int ind = orders[pos - 1][k - 1];
int dig = arr[ind][pos] + d;
--cnt[dig];
++cnt[dig + 1];
overhead += (dig + 1 >= kD) - (dig >= kD);
cost -= (dig != 0 || pos < sizes[ind] || pos < inc.size()) *
costs[dig % kD];
cost += costs[(dig + 1) % kD];
dp[pos][k] = max(dp[pos][k], cost + dp[pos + 1][overhead]);
}
}
}
cout << dp[0][0] << '\n';
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n, m, down[107][10007];
char st[107][10007];
int getLeft(int i, int l);
int getRight(int i, int r);
int main() {
int i, j, l, r, dir, cur, pos;
long long res = 0;
char tmp[10007];
scanf("%d %d", &n, &m);
for (i = 0; i < n; ++i) {
scanf("%s", tmp);
st[i][0] = '#';
for (j = 1; j <= m; ++j) {
st[i][j] = tmp[j - 1];
}
st[i][m + 1] = '#';
}
for (i = 0; i < n - 1; ++i) {
for (j = 1; j <= m; ++j) {
if (st[i + 1][j] == '.') {
down[i][j] = 1;
}
}
for (j = 1; j <= m; ++j) {
down[i][j] += down[i][j - 1];
}
}
pos = 1;
dir = 1;
cur = 0;
l = r = 1;
while (1) {
if (cur == n - 1) {
break;
}
l = getLeft(cur, l);
r = getRight(cur, r);
if (down[cur][r - 1] - down[cur][l] == 0 && st[cur][l] == '#' &&
st[cur][r] == '#') {
printf("Never\n");
return 0;
}
if (dir == 1) {
if (down[cur][r - 1] - down[cur][pos - 1] > 0) {
while (st[cur + 1][pos] != '.') {
++pos;
++res;
}
++cur;
++res;
l = r = pos;
} else {
res += r - pos;
if (st[cur][r] == '#') {
pos = r - 1;
dir = -1;
} else if (st[cur][r] == '+') {
st[cur][r] = '.';
pos = r - 1;
dir = -1;
}
}
} else {
if (down[cur][pos] - down[cur][l] > 0) {
while (st[cur + 1][pos] != '.') {
--pos;
++res;
}
++cur;
++res;
l = r = pos;
} else {
res += pos - l;
if (st[cur][l] == '#') {
pos = l + 1;
dir = 1;
} else if (st[cur][l] == '+') {
st[cur][l] = '.';
pos = l + 1;
dir = 1;
}
}
}
}
printf("%I64d\n", res);
return 0;
}
int getLeft(int i, int l) {
while (l >= 1 && st[i][l] == '.') {
--l;
}
return l;
}
int getRight(int i, int r) {
while (r <= m && st[i][r] == '.') {
++r;
}
return r;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1e4;
const long long INFLL = 1e18;
const int MAX_N = 5000;
int N;
vector<int> gp[MAX_N + 1];
int deg[MAX_N + 1], num[MAX_N + 1], p[MAX_N + 1];
int sz[MAX_N + 1], dp[MAX_N + 1][2][MAX_N + 1];
int ndp[2][MAX_N + 1];
void dfs(int x) {
for (int i : gp[x]) {
if (i != p[x]) {
p[i] = x;
dfs(i);
sz[x] += sz[i];
}
}
if (sz[x] == 0) {
sz[x] = 1;
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j <= sz[x]; j++) {
dp[x][i][j] = INF;
}
}
if (sz[x] == 1) {
dp[x][0][0] = 0;
dp[x][1][1] = 0;
return;
}
sz[x] = 0;
for (int n : gp[x]) {
if (p[n] != x) continue;
if (sz[x] == 0) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j <= sz[n]; j++) {
dp[x][i][j] = min(dp[n][i][j], dp[n][1 - i][j] + 1);
}
}
sz[x] += sz[n];
} else {
for (int i = 0; i < 2; i++) {
for (int j = 0; j <= sz[x]; j++) {
for (int k = 0; k <= sz[n]; k++) {
ndp[i][j + k] =
min(ndp[i][j + k], min(dp[x][i][j] + dp[n][i][k],
dp[x][i][j] + dp[n][1 - i][k] + 1));
}
}
}
sz[x] += sz[n];
for (int i = 0; i < 2; i++) {
for (int j = 0; j <= sz[x]; j++) {
dp[x][i][j] = ndp[i][j];
ndp[i][j] = INF;
}
}
}
}
}
int main() {
scanf("%d", &N);
for (int i = 0; i < 2; i++) {
for (int j = 0; j <= N; j++) ndp[i][j] = INF;
}
for (int i = 1; i < N; i++) {
int a, b;
scanf("%d%d", &a, &b);
gp[a].push_back(b);
gp[b].push_back(a);
deg[a]++;
deg[b]++;
}
int r = 0;
for (int i = 1; i <= N; i++) {
if (deg[i] != 1) {
r = i;
break;
}
}
dfs(r);
printf("%d", min(dp[r][0][sz[r] / 2], dp[r][1][sz[r] / 2]));
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int t;
cin >> t;
while (t--) {
int a, b, c, d, k;
cin >> a >> b >> c >> d >> k;
int x = ceil((float)a / c);
int y = ceil((float)b / d);
if (x + y <= k)
cout << x << ' ' << y << endl;
else
cout << -1 << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int used[N];
int max_health[N];
int base_health[N];
int regen[N];
int n, m, bounty, increase, damage;
tuple<int, int, int> health_point[N];
map<int, int> cnt;
set<int> update[N];
map<long long, int> add;
map<long long, int> erase;
vector<pair<int, int> > event[N];
int main() {
scanf("%d%d%d%d%d", &n, &m, &bounty, &increase, &damage);
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &max_health[i], &base_health[i], ®en[i]);
event[i].push_back(make_pair(0, base_health[i]));
}
for (int i = 0; i < m; i++) {
int time, enemy, hp;
scanf("%d%d%d", &time, &enemy, &hp);
enemy--;
event[enemy].push_back(make_pair(time, hp));
}
for (int i = 0; i < n; i++) {
sort(event[i].begin(), event[i].end());
}
if (increase > 0) {
for (int i = 0; i < n; i++) {
if (max_health[i] <= damage ||
(regen[i] == 0 && event[i].back().second <= damage)) {
puts("-1");
return 0;
}
}
}
for (int i = 0; i < n; i++) {
auto &v = event[i];
for (int j = 0; j < v.size(); j++) {
pair<int, int> e = v[j];
if (e.second > damage) continue;
add[e.first]++;
pair<int, int> q;
if (j < v.size() - 1)
q = v[j + 1];
else
q = make_pair(2e9, 0);
long long delta = min(
q.first - e.first - 1,
regen[i] == 0 ? (int)2e9
: (min(damage, max_health[i]) - e.second) / regen[i]);
erase[e.first + delta]++;
}
}
int alive = 0;
set<long long> timeline;
for (auto v : add) timeline.insert(v.first);
for (auto v : erase) timeline.insert(v.first);
long long ans = 0;
for (auto v : timeline) {
alive += add[v];
ans = max(ans, alive * (bounty + increase * (long long)1 * v));
alive -= erase[v];
}
printf("%lld", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, k;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> k;
if (k % 2 == 0) {
k = k - 1;
}
cout << k << " ";
}
cout << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long ans[50005];
std::vector<vector<long long> > v;
bool visited[50005];
void dfs(long long src, long long prev) {
ans[src] = prev;
visited[src] = 1;
for (auto i : v[src]) {
if (!visited[i]) {
dfs(i, src);
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, r1, r2;
cin >> n >> r1 >> r2;
v.resize(n + 1);
long long a[n + 1];
memset(a, -1, sizeof(a));
for (long long i = 1; i < n + 1; i++) {
if (i == r1) {
continue;
}
cin >> a[i];
v[i].push_back(a[i]);
v[a[i]].push_back(i);
}
dfs(r2, -1);
for (long long i = 1; i <= n; i++) {
if (ans[i] != -1) {
cout << ans[i] << ' ';
}
}
cout << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int P = 998244353;
template <typename T>
void chkmax(T &x, T y) {
x = max(x, y);
}
template <typename T>
void chkmin(T &x, T y) {
x = min(x, y);
}
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
const int Matsize = 2;
struct mat {
int a[Matsize][Matsize];
};
void update(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
mat Cipher() {
mat ans;
memset(ans.a, 0, sizeof(ans.a));
return ans;
}
mat Mat(int x) {
mat ans;
memset(ans.a, 0, sizeof(ans.a));
for (int i = 0; i <= Matsize - 1; i++) {
ans.a[i][i] = 1;
if (i <= x) update(ans.a[i][x], 1);
}
return ans;
}
mat operator*(mat a, mat b) {
mat ans;
static long long tmp[Matsize][Matsize];
memset(tmp, 0, sizeof(tmp));
for (int i = 0; i <= Matsize - 1; i++)
for (int k = 0; k <= Matsize - 1; k++)
if (a.a[i][k]) {
for (int j = 0; j <= Matsize - 1; j++)
tmp[i][j] += 1ll * a.a[i][k] * b.a[k][j];
}
for (int i = 0; i <= Matsize - 1; i++)
for (int j = 0; j <= Matsize - 1; j++) ans.a[i][j] = tmp[i][j] % P;
return ans;
}
struct SegmentTree {
struct Node {
int lc, rc;
mat sum;
} a[MAXN * 2];
int n, root, size;
void update(int root) { a[root].sum = a[a[root].lc].sum * a[a[root].rc].sum; }
void build(int &root, int l, int r, mat *v) {
root = ++size;
if (l == r) {
a[root].sum = v[l];
return;
}
int mid = (l + r) / 2;
build(a[root].lc, l, mid, v);
build(a[root].rc, mid + 1, r, v);
update(root);
}
void init(int x, mat *v) {
n = x, root = size = 0;
build(root, 1, n, v);
}
void modify(int root, int l, int r, int pos, mat d) {
if (l == r) {
a[root].sum = d;
return;
}
int mid = (l + r) / 2;
if (mid >= pos)
modify(a[root].lc, l, mid, pos, d);
else
modify(a[root].rc, mid + 1, r, pos, d);
update(root);
}
void modify(int pos, mat d) { modify(root, 1, n, pos, d); }
mat query() { return a[root].sum; }
} ST;
struct info {
int x, y, pos;
bool a, b;
};
mat a[MAXN];
info b[MAXN];
int n, x[MAXN], v[MAXN], p[MAXN];
int power(int x, int y) {
if (y == 0) return 1;
int tmp = power(x, y / 2);
if (y % 2 == 0)
return 1ll * tmp * tmp % P;
else
return 1ll * tmp * tmp % P * x % P;
}
bool cmp(info a, info b) { return 1ll * a.x * b.y < 1ll * b.x * a.y; }
int main() {
read(n);
if (n == 1) {
puts("0");
return 0;
}
int tot = 0, inv = power(100, P - 2);
for (int i = 1; i <= n; i++) {
read(x[i]), read(v[i]), read(p[i]), p[i] = 100 - p[i];
int tmp = 1ll * p[i] * inv % P;
if (i != 1) {
a[i - 1].a[0][0] = (P + 1 - tmp) % P;
a[i - 1].a[1][0] = (P + 1 - tmp) % P;
a[i - 1].a[0][1] = tmp;
a[i - 1].a[1][1] = tmp;
b[++tot] = (info){x[i] - x[i - 1], v[i] + v[i - 1], i - 1, false, true};
if (v[i] > v[i - 1])
b[++tot] = (info){x[i] - x[i - 1], v[i] - v[i - 1], i - 1, true, true};
if (v[i] < v[i - 1])
b[++tot] =
(info){x[i] - x[i - 1], v[i - 1] - v[i], i - 1, false, false};
}
}
ST.init(n - 1, a);
sort(b + 1, b + tot + 1, cmp);
int ans = 0;
for (int i = 1; i <= tot; i++) {
int pos = b[i].pos, timer = 1ll * b[i].x * power(b[i].y, P - 2) % P;
mat tmp = a[pos];
for (int j = 0; j <= 1; j++)
for (int k = 0; k <= 1; k++)
if (j != b[i].a || k != b[i].b) a[pos].a[j][k] = 0;
ST.modify(pos, a[pos]);
mat res = ST.query();
int tp = 1ll * p[1] * inv % P, prob = 0;
for (int j = 0; j <= 1; j++)
for (int k = 0; k <= 1; k++)
if (j)
update(prob, 1ll * tp * res.a[j][k] % P);
else
update(prob, 1ll * (P + 1 - tp) * res.a[j][k] % P);
update(ans, 1ll * timer * prob % P);
a[pos] = tmp, a[pos].a[b[i].a][b[i].b] = 0;
ST.modify(pos, a[pos]);
}
cout << ans << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
for (int i = 1;; i++) {
if (i == n + 1) i = 1;
if (m >= i)
m -= i;
else
break;
}
cout << m << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '-')
cnt--;
else if (s[i] == '+')
cnt++;
if (cnt < 0) cnt = 0;
}
cout << cnt << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18;
const long long mod = (long long)1e9 + 7;
const double pi = acos(-1.0);
const double eps = (double)1e-9;
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 100500;
const int LOG = 26;
int n, q, sz, bor[N * LOG][2], cnt[N * LOG];
inline void add(int p) {
int v = 0;
for (int i = LOG; i >= 0; i--) {
int bit = ((p >> i) & 1);
if (!bor[v][bit]) bor[v][bit] = ++sz;
v = bor[v][bit];
cnt[v]++;
}
}
inline void del(int p) {
int v = 0;
for (int i = LOG; i >= 0; i--) {
int bit = ((p >> i) & 1);
v = bor[v][bit];
cnt[v]--;
}
}
inline int get(int p, int l) {
int v = 0, cur = 0, res = n;
for (int i = LOG; i >= 0; i--) {
int bit = ((p >> i) & 1);
if (cur + (1 << i) < l) {
if (cnt[bor[v][bit ^ 1]]) {
cur += (1 << i);
v = bor[v][bit ^ 1];
} else
v = bor[v][bit];
} else {
if (cnt[bor[v][bit ^ 1]]) res -= cnt[bor[v][bit ^ 1]];
if (cnt[bor[v][bit]])
v = bor[v][bit];
else
return res;
}
}
return res;
}
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> q;
for (int i = 1; i <= q; i++) {
int tp;
cin >> tp;
if (tp <= 2) {
int p;
cin >> p;
if (tp == 1)
add(p), n++;
else
del(p), n--;
} else {
int p, l;
cin >> p >> l;
cout << get(p, l) << endl;
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
queue<string> q;
set<string> s;
int main() {
int n, k;
cin >> n >> k;
string a;
cin >> a;
int ans = 0;
q.push(a);
s.insert(a);
while (!q.empty() && s.size() < k) {
string tmp = q.front();
q.pop();
for (int i = 0; i < tmp.size(); i++) {
string v = tmp;
v.erase(i, 1);
if (!s.count(v) && (s.size() + 1) <= k) {
q.push(v);
s.insert(v);
ans += (n - v.size());
}
}
}
if (s.size() < k) {
printf("-1\n");
} else {
printf("%d\n", ans);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 8, inf = 1e9 + 7, mod = 998244353;
const long long mod2 = (long long)mod * mod;
int n;
int a[N];
struct Node {
Node *l, *r;
int len;
bool reset_, laz;
long long la2, val;
Node(int len)
: l(NULL), r(NULL), len(len), reset_(false), laz(false), la2(0), val(0) {}
void reset() {
reset_ = true;
laz = la2 = val = 0;
}
void upd() {
laz ^= 1;
la2 = mod - la2;
val = mod - val;
}
void upd(long long v) {
la2 += v;
if (la2 >= mod) la2 -= mod;
(val += v * len) %= mod;
}
void down() {
if (reset_) {
l->reset();
r->reset();
reset_ = false;
}
if (laz) {
l->upd();
r->upd();
laz = false;
}
if (la2) {
l->upd(la2);
r->upd(la2);
la2 = 0;
}
}
} * root(new Node(inf));
void upd(Node *u, int l, int r, int a, int b, long long v) {
if (b < l || r < a) return;
if (a <= l && r <= b) {
if (v == -2)
u->reset();
else {
if (~v)
u->upd(v);
else
u->upd();
}
return;
}
int m = (l + r) >> 1;
if (!u->l) u->l = new Node(m - l + 1);
if (!u->r) u->r = new Node(r - l);
u->down();
upd(u->l, l, m, a, b, v);
upd(u->r, m + 1, r, a, b, v);
u->val = (u->l->val + u->r->val) % mod;
}
long long get(Node *u, int l, int r, int a, int b) {
if (b < l || r < a) return 0;
if (a <= l && r <= b) return u->val;
int m = (l + r) >> 1;
if (!u->l) u->l = new Node(m - l + 1);
if (!u->r) u->r = new Node(r - l);
u->down();
return get(u->l, l, m, a, b) + get(u->r, m + 1, r, a, b);
}
long long sum[N];
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
sum[0] = 1;
for (int i = 1; i <= n; ++i) {
sum[i] =
(a[i] * sum[i - 1] - get(root, 1, inf, 1, min(a[i - 1], a[i])) + mod2) %
mod;
upd(root, 1, inf, a[i - 1] + 1, inf, -2);
upd(root, 1, inf, 1, a[i - 1], -1);
upd(root, 1, inf, 1, a[i], sum[i - 1]);
}
cout << sum[n] << '\n';
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int ara[302][302];
int dp[604][302][302];
int n;
int dpp(int i, int j, int k) {
if (i == 1 + 2 * n) return 0;
if (j > n || k > n) return -1e8;
int x1 = i - j, x2 = i - k;
if (x1 > n || x2 > n) return -1e8;
int now = 0;
if (x1 == x2 && j == k)
now = ara[x1][k];
else
now += ara[x1][j] + ara[x2][k];
if (dp[i][j][k] != -1) return dp[i][j][k];
int ret = -1e8;
ret = max(ret, dpp(i + 1, j, k));
ret = max(ret, dpp(i + 1, j + 1, k));
ret = max(ret, dpp(i + 1, j + 1, k + 1));
ret = max(ret, dpp(i + 1, j, k + 1));
return dp[i][j][k] = ret + now;
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> ara[i][j];
long long ans = 0;
if (n == 1) {
cout << ara[1][1] << endl;
return 0;
}
cout << dpp(2, 1, 1) << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long GCD(long long a, long long b) {
return (a % b) == 0 ? b : GCD(b, a % b);
}
long long mod(long long x) { return ((x % 998244353 + 998244353) % 998244353); }
bool cmp(const pair<int, int> &left, const pair<int, int> &right) {
return left.first > right.first ||
(left.first == right.first && left.second < right.second);
}
int main() {
int t, cs = 1;
cin >> t;
while (t--) {
long long n, m, a, b, c, i, j, k, mx = 0, mn = 1e18;
long long p[205] = {0}, ar[200005], cnt[200005] = {0};
string s;
cin >> n >> m;
cin >> s;
for (i = 0; i < m; i++) {
scanf("%lld", &ar[i]);
cnt[ar[i] - 1]++;
}
for (i = n - 1; i > 0; i--) {
cnt[i - 1] += cnt[i];
}
for (j = 0; j < n; j++) {
p[s[j]] += cnt[j] + 1;
}
for (i = 'a'; i <= 'z'; i++) {
printf("%lld ", p[i]);
}
cout << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string t;
long long te;
queue<string> q;
int m, n;
int month[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
long long cha(string d) {
int mo = (d[5] - '0') * 10 + d[6] - '0';
int da = (d[8] - '0') * 10 + d[9] - '0';
int ho = (d[11] - '0') * 10 + d[12] - '0';
int mi = (d[14] - '0') * 10 + d[15] - '0';
int se = (d[17] - '0') * 10 + d[18] - '0';
for (register int i = 1; i <= mo - 1; ++i) {
da += month[i];
}
return da * 86400 + ho * 3600 + mi * 60 + se;
}
int main() {
scanf("%d%d", &n, &m);
getchar();
while (getline(cin, t)) {
te = cha(t);
while (!q.empty() && (te - cha(q.front())) >= n) {
q.pop();
}
if (q.size() >= m - 1) {
for (register int i = 0; i <= 18; ++i) {
printf("%c", t[i]);
}
return 0;
}
q.push(t);
}
printf("-1");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
double a, b, c, s1, s2;
int main() {
cin >> a >> b >> c;
s1 = (-b + sqrt(b * b - 4 * a * c)) / 2 / a;
s2 = (-b - sqrt(b * b - 4 * a * c)) / 2 / a;
cout << fixed << setprecision(6) << max(s1, s2) << endl << min(s1, s2);
}
| 2 |
#include <bits/stdc++.h>
int mysign(long long x) {
if (x == 0) return 0;
if (x < 0) return -1;
return 1;
}
long long val[300005];
long long mask[300005];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld%lld", &val[i], &mask[i]);
long long fsum = 0;
for (int i = 1; i <= n; i++) fsum += val[i];
int tgt = mysign(fsum);
long long ans = 0;
for (int i = 0; i < 62; i++) {
long long vsum = 0;
for (int j = 1; j <= n; j++) {
if (mask[j] >= (1ll << i) && mask[j] < (1ll << (i + 1))) vsum += val[j];
}
if (mysign(vsum) == tgt) {
ans |= (1ll << i);
for (int j = 1; j <= n; j++) {
if ((mask[j] & (1ll << i)) == (1ll << i)) val[j] *= -1ll;
}
}
}
printf("%lld\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> myVector;
for (int i = 0; i < N; i++) {
int temp;
cin >> temp;
myVector.push_back(temp);
}
for (int i = 0; i < (int)myVector.size(); i++) {
if (i == 0) {
cout << myVector[1] - myVector[0] << " "
<< myVector[(int)myVector.size() - 1] - myVector[0] << endl;
} else {
if (i == (int)myVector.size() - 1) {
cout << myVector[(int)myVector.size() - 1] -
myVector[(int)myVector.size() - 2]
<< " " << myVector[(int)myVector.size() - 1] - myVector[0] << endl;
} else {
cout << min(myVector[i] - myVector[i - 1],
myVector[i + 1] - myVector[i])
<< " "
<< max(myVector[i] - myVector[0], myVector[N - 1] - myVector[i])
<< endl;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void dbg(const char *xn, T x) {
if (0) cout << xn << ": " << x << "\n";
}
template <class T, class... TA>
void dbg(const char *xn, T x, TA... t) {
while (*xn != ',')
if (0) cout << *xn++;
if (0) cout << ": " << x << ",";
dbg(xn + 1, t...);
}
template <class T>
inline bool upd_max(T &x, T y) {
return y > x ? x = y, true : false;
}
template <class T>
inline bool upd_min(T &x, T y) {
return y < x ? x = y, true : false;
}
template <class T>
inline void add(T &x, T y) {
x += y;
}
using namespace std;
const int N = 1000007;
long long n, a[N], cnt[N], dividing[N], pot[N], dp[N];
void solve() {
cin >> n;
for (long long i = 1; i <= (n); ++i) {
cin >> a[i];
++cnt[a[i]];
}
pot[0] = 1;
for (long long i = 1; i <= (N - 5); ++i)
pot[i] = (pot[i - 1] * 2) % 1000000007LL;
for (long long i = 1; i <= N - 5; ++i) {
for (long long j = i; j <= N - 5; j += i) {
dividing[i] += cnt[j];
}
}
for (long long i = N - 5; i >= 2; --i) {
if (dividing[i]) dp[i] = dividing[i] * pot[dividing[i] - 1] % 1000000007LL;
for (long long j = 2 * i; j <= N - 5; j += i) {
dp[i] -= dp[j];
dp[i] = ((dp[i] % 1000000007LL) + 1000000007LL) % 1000000007LL;
}
}
long long rst = 0;
for (long long i = 2; i <= N - 5; ++i) rst += dp[i] * i, rst %= 1000000007LL;
cout << rst;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout << fixed << setprecision(10);
solve();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
inline long long in() {
long long x = 0, n = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) n = (c == '-');
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return n ? -x : x;
}
const int N = 2e3 + 5;
int n, m, n2;
pair<int, int> a[N];
int A = 1;
int now[N];
int p[N];
int d[N];
int use2;
vector<pair<int, int> > pr;
int Pr;
void USE1(int x) {
d[now[x]] = 1;
cout << x << endl;
}
void USE2() {
cin >> use2;
d[now[use2]] = 1;
}
void NOR() {
while (Pr <= (int)pr.size() - 1) {
pair<int, int> x = pr[Pr];
if (d[now[x.first]]) {
Pr++;
continue;
}
USE1(x.first);
Pr++;
return;
}
while (d[A]) A++;
USE1(a[A].second);
}
int main() {
cin >> n >> m;
n2 = n * 2;
for (int i = 1; i <= n2; i++) cin >> a[i].first, a[i].second = i;
for (int drfds = 1; drfds <= m; drfds++) {
int x, y;
cin >> x >> y;
p[x] = y, p[y] = x;
if (a[x].first < a[y].first) swap(x, y);
pr.push_back({x, y});
}
sort(a + 1, a + n2 + 1, greater<pair<int, int> >());
for (int i = 1; i <= n2; i++) now[a[i].second] = i;
int t = in();
for (int asdoe = 1; asdoe <= n; asdoe++) {
if (t == 1) {
NOR();
USE2();
} else {
USE2();
int P = p[use2];
if (P && !d[now[P]])
USE1(P);
else
NOR();
}
cout << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = 10 * x + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m, tot, cnt, l[100000 + 5], r[100000 + 5], b[100000 + 5], c[100000 + 5],
v[350][100000 + 5];
long long a[100000 + 5], ans[100000 + 5], g[100000 + 5];
int main() {
n = read();
m = read();
int T = read(), size = sqrt(n);
for (int i = 1; i <= (n); i++) a[i] = read();
for (int i = 1; i <= (m); i++) {
int x = read();
l[i] = tot + 1;
for (int j = 1; j <= (x); j++) b[tot + j] = read(), ans[i] += a[b[tot + j]];
tot += x;
r[i] = tot;
if (x > size) c[++cnt] = i;
}
memset(a, 0, sizeof(a));
for (int i = 1; i <= (cnt); i++) {
for (int j = (l[c[i]]); j <= (r[c[i]]); j++) a[b[j]] = i;
for (int j = 1; j <= (m); j++)
for (int k = (l[j]); k <= (r[j]); k++)
if (a[b[k]] == i) v[i][j]++;
}
memset(a, 0, sizeof(a));
while (T--) {
char ch[5];
scanf("%s", ch);
if (ch[0] == '+') {
int x = read(), y = read();
if (r[x] - l[x] + 1 <= size) {
for (int i = (l[x]); i <= (r[x]); i++) a[b[i]] += y;
for (int i = 1; i <= (cnt); i++) ans[c[i]] += (long long)v[i][x] * y;
} else
g[x] += y;
} else {
int x = read();
long long ret = ans[x];
if (r[x] - l[x] + 1 <= size) {
for (int i = (l[x]); i <= (r[x]); i++) ret += a[b[i]];
for (int i = 1; i <= (cnt); i++) ret += g[c[i]] * v[i][x];
} else {
for (int i = 1; i <= (cnt); i++) ret += g[c[i]] * v[i][x];
}
printf("%I64d\n", ret);
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long q = 1;
while (q--) {
string input;
cin >> input;
long long n = (long long)input.size();
char a[n];
for (long long i = 0; i < n; i++) {
a[i] = input[i];
}
vector<vector<long long> > dp(26, vector<long long>(26));
vector<long long> cnt = dp[0];
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < 26; j++) {
dp[a[i] - 'a'][j] += cnt[j];
}
cnt[a[i] - 'a']++;
}
long long ans = 0;
for (long long j = 0; j < 26; j++) ans = max(ans, cnt[j]);
for (long long j = 0; j < 26; j++) {
for (long long k = 0; k < 26; k++) {
ans = max(ans, dp[j][k]);
}
}
cout << ans << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
constexpr char nl = '\n';
template <class array, class B>
array&& mini(array&& a, B&& b) {
if (b < a) a = b;
return a;
}
template <class array, class B>
array&& maxi(array&& a, B&& b) {
if (b > a) a = b;
return a;
}
int first_bit(int x) { return x == 0 ? 0 : sizeof(x) * 8 - __builtin_clz(x); }
int ceil2(int x) { return x < 2 ? x : 1 << first_bit(x - 1); }
constexpr int inf = 0x3f3f3f3f;
constexpr long long inf_l = 0x3f3f3f3f3f3f3f3f;
struct Debug;
template <class T>
auto _is_debug_func(T&& x)
-> decltype(x(declval<add_lvalue_reference<Debug>::type>()), true_type{});
false_type _is_debug_func(...);
template <class T>
struct is_debug_func : decltype(_is_debug_func(declval<T>())) {};
;
template <class T>
auto _is_func(T&& x) -> decltype(x(), true_type{});
false_type _is_func(...);
template <class T>
struct is_func : decltype(_is_func(declval<T>())) {};
template <class T>
auto _is_string(T&& x) -> decltype(string(x), true_type{});
false_type _is_string(...);
template <class T>
struct is_string : decltype(_is_string(declval<T>())) {};
template <class T>
auto _is_ptr(T&& x)
-> decltype(*x, typename enable_if<is_string<T>() == false, int>::type(),
true_type{});
false_type _is_ptr(...);
template <class T>
struct is_ptr : decltype(_is_ptr(declval<T>())) {};
;
template <class T>
auto _is_container(T&& x)
-> decltype(x.begin(),
typename enable_if<is_string<T>() == false, int>::type(),
true_type{});
false_type _is_container(...);
template <class T>
struct is_container : decltype(_is_container(declval<T>())) {};
template <class Iter>
struct Off {
Iter _a, _b;
};
template <class T>
auto O(T&& x)
-> decltype(typename enable_if<is_container<T>() == true, int>::type(),
Off<decltype(x.begin())>()) {
return {x.begin(), x.end()};
}
template <class T>
auto O(T&& x)
-> decltype(typename enable_if<is_container<T>() == false, int>::type(),
x) {
return x;
}
struct Debug {
Debug() { cerr << boolalpha; }
~Debug() { cerr << nl; }
Debug& operator()(int x = 1) {
for (auto _ = (0), ___ = ((x)-1); _ <= ___; ++_) *this << " ";
return *this;
}
template <class T>
auto operator<<(T&& x)
-> decltype(cerr << x,
typename enable_if<
is_func<T>() == false && is_ptr<T>() == false &&
is_integral<typename remove_reference<T>::type>() ==
true,
int>::type(),
*this) {
using L = long long;
if (abs(int(x)) == inf || abs(L(x)) == inf_l)
cerr << ((int(x) == inf || L(x) == inf_l) ? "+∞"
: (int(x) == -inf || L(x) == -inf_l) ? "-∞"
: "?");
else
cerr << x;
return *this;
}
template <class T>
auto operator<<(T&& x)
-> decltype(cerr << x,
typename enable_if<
is_func<T>() == false && is_ptr<T>() == false &&
is_integral<typename remove_reference<T>::type>() ==
false,
int>::type(),
*this) {
cerr << x;
return *this;
}
template <class T>
auto operator<<(T&& x) -> decltype(x.first, *this) {
return *this << "(" << O(x.first) << ", " << O(x.second) << ")";
}
template <class T>
auto operator<<(T&& x)
-> decltype(typename enable_if<is_container<T>() == true, int>::type(),
*this) {
*this << "{\n";
for (auto a = x.begin(); a != x.end(); ++a)
*this << " " << distance(x.begin(), a) << ": " << O(*a) << '\n';
return *this << "}";
}
template <class T>
auto operator<<(T&& x) -> decltype(x._a, *this) {
*this << "{";
for (auto a = x._a, b = x._b; a != b; ++a)
*this << O(*a) << (next(a) == b ? "" : ", ");
return *this << "}";
}
template <class T>
auto operator<<(T&& x)
-> decltype(typename enable_if<is_func<T>() == true, int>::type(),
*this) {
x();
return *this;
}
template <class T>
auto operator<<(T&& x)
-> decltype(typename enable_if<is_debug_func<T>() == true, int>::type(),
*this) {
x(*this);
return *this;
}
template <class T>
auto operator<<(T&& x)
-> decltype(typename enable_if<is_ptr<T>() == true &&
is_func<T>() == false &&
is_debug_func<T>() == false,
int>::type(),
*this) {
return *this << *x;
}
};
struct DebugOff {
template <class T>
DebugOff& operator<<(T&&) {
return *this;
}
DebugOff& operator()(int = 0) { return *this; }
};
using VI = vector<int>;
using VVI = vector<VI>;
using L = long long;
using VL = vector<L>;
using VB = vector<bool>;
using II = pair<int, int>;
using VII = vector<II>;
using VVII = vector<VII>;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int x = 1, lg = 0;
while (x <= n) {
++lg;
x *= 2;
}
cout << lg << nl;
}
| 2 |
#include <bits/stdc++.h>
constexpr int MAX_LVL = 63;
using namespace std;
using ll = long long;
int n;
ll off[MAX_LVL];
int get_lvl(ll x) {
int ans = 0;
while (x > 0) {
++ans;
x >>= 1;
}
return ans - 1;
}
void shift(int lvl, ll k) {
ll lvl_size = 1LL << lvl;
k %= lvl_size;
k = (k + lvl_size) % lvl_size;
off[lvl] = (off[lvl] + k) % lvl_size;
}
ll snum(int lvl, ll start, ll amt) {
ll lvl_size = 1LL << lvl;
amt = (amt % lvl_size + lvl_size) % lvl_size;
return lvl_size + (start + amt) % lvl_size;
}
int main() {
scanf(" %d", &n);
for (int i = 0; i < n; ++i) {
int typ;
scanf(" %d", &typ);
if (typ == 1) {
ll x, k;
scanf(" %lld %lld", &x, &k);
shift(get_lvl(x), k);
} else if (typ == 2) {
ll x, k;
scanf(" %lld %lld", &x, &k);
for (int lvl = get_lvl(x); lvl < MAX_LVL; ++lvl) {
shift(lvl, k);
k *= 2;
}
} else if (typ == 3) {
ll x;
scanf(" %lld", &x);
int lvl = get_lvl(x);
ll cur_pos = snum(lvl, x, off[lvl]);
while (cur_pos != 1) {
printf("%lld ", snum(lvl, cur_pos, -off[lvl]));
cur_pos /= 2;
lvl -= 1;
}
printf("1\n");
} else {
assert(false);
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
std::vector<int> to[100005], topo, ans;
int vis[100005], tempvis[100005], mark = 0, etopo[100005];
struct x {
int a, b, c, index;
} arr[100005];
bool cmp(x a, x b) { return a.c < b.c; }
void visit(int x) {
if (mark == 1) return;
if (vis[x] == 1) return;
if (tempvis[x] == 1) {
mark = 1;
return;
}
tempvis[x] = 1;
for (int i = 0; i < to[x].size(); i++) visit(to[x][i]);
tempvis[x] = 0;
vis[x] = 1;
topo.push_back(x);
}
int main() {
int n, m, i, i2;
scanf("%d %d", &n, &m);
for (i = 1; i <= m; i++) {
scanf("%d %d %d", &arr[i].a, &arr[i].b, &arr[i].c);
arr[i].index = i;
}
std::sort(arr + 1, arr + 1 + m, cmp);
int l = -1, r = m, mid;
for (mid = (l + r) / 2; l + 1 != r; mid = (l + r) / 2) {
for (i = 1; i <= n; i++) {
to[i].clear();
vis[i] = 0;
tempvis[i] = 0;
}
topo.clear();
mark = 0;
for (i = mid + 1; i <= m; i++) to[arr[i].a].push_back(arr[i].b);
for (i = 1; i <= n; i++)
if (vis[i] == 0) visit(i);
if (mark == 1)
l = mid;
else
r = mid;
}
for (i = 1; i <= n; i++) {
to[i].clear();
vis[i] = 0;
tempvis[i] = 0;
}
topo.clear();
mark = 0;
for (i = r + 1; i <= m; i++) to[arr[i].a].push_back(arr[i].b);
for (i = 1; i <= n; i++)
if (vis[i] == 0) visit(i);
for (i = 0; i < topo.size(); i++) {
etopo[topo[i]] = topo.size() - i;
}
for (i = 1; i <= r; i++) {
if (etopo[arr[i].a] > etopo[arr[i].b]) ans.push_back(arr[i].index);
}
printf("%d %d\n", arr[r].c, ans.size());
for (i = 0; i < ans.size(); i++) printf("%d ", ans[i]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long inf = 999999999;
long long mod = 1000000007;
long long parent[200005], size[200005];
long long find_set(long long a) {
if (a == parent[a])
return a;
else
return parent[a] = find_set(parent[a]);
}
void union_set(long long a, long long b) {
long long x = find_set(a);
long long y = find_set(b);
if (x != y) {
if (size[x] < size[y]) swap(x, y);
parent[y] = x;
size[x] += size[y];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
bool flag = true;
long long test_case = 1, tc;
if (flag) cin >> test_case;
tc = test_case;
while (tc--) {
long long i, j, k, n, m, t, x, y, z, sum = 0, ans = 0, c = 0, flag = 0;
long long maxi = -1e9, mini = 1e9;
vector<long long> v;
map<long long, long long> mp;
string s;
cin >> n;
long long a[n + 2];
for (i = 1; i <= n; i++) {
cin >> a[i];
parent[i] = i;
size[i] = 1;
}
for (i = 1; i <= n; i++) {
union_set(i, a[i]);
}
for (i = 1; i <= n; i++) cout << size[find_set(i)] << " ";
cout << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, pastx = 0;
cin >> n;
int energy = 0, money = 0;
for (int i = 0; i < n; i++) {
cin >> x;
energy += pastx - x;
if (energy < 0) {
money += energy;
energy = 0;
}
pastx = x;
}
cout << -money;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int in[N];
vector<int> g[N];
long long cal(int x) {
return (long long)g[x].size() * (in[x] - (long long)g[x].size());
}
int main() {
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
int i;
for (i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
if (a > b) swap(a, b);
g[a].push_back(b);
in[a]++, in[b]++;
}
long long ans = 0;
for (i = 1; i <= n; i++) {
ans += cal(i);
}
int t;
cin >> t;
cout << ans << endl;
while (t--) {
int x;
cin >> x;
ans -= cal(x);
for (auto b : g[x]) {
ans -= cal(b);
g[b].push_back(x);
ans += cal(b);
}
g[x].clear();
cout << ans << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
bool ok = true;
cin >> n;
int t, res;
t = res = 0;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
int k = min(x, y);
if (k >= t) {
res += k - t + 1;
if (!ok) res--;
}
if (x == y)
ok = false;
else
ok = true;
t = max(x, y);
}
cout << res;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 * 1000;
int n, m;
int num[maxn + 1];
bool is[maxn];
int dp[maxn + 1];
vector<int> v[maxn];
int dfs(int a) {
is[a] = true;
int ans = 1;
for (int i = 0; i < (int)v[a].size(); ++i)
if (!is[v[a][i]]) ans += dfs(v[a][i]);
return ans;
}
bool lucky(int a) {
while (a > 0) {
if (a % 10 != 4 && a % 10 != 7) return false;
a /= 10;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 0; i < n; ++i)
if (!is[i]) num[dfs(i)]++;
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; num[i]; j <<= 1) {
num[i] -= (j = min(j, num[i]));
int p = i * j;
for (int k = n - p; k >= 0; --k)
if (dp[k] != -1 && (dp[k + p] == -1 || dp[k] + j < dp[k + p]))
dp[k + p] = dp[k] + j;
}
}
int ans = -1;
for (int i = 1; i <= n; ++i)
if (lucky(i)) {
if (dp[i] != -1 && (ans == -1 || dp[i] - 1 < ans)) ans = dp[i] - 1;
}
cout << ans << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 55, maxm = 105, maxt = 20005;
const double pai = acos(-1.0);
int n, m, t, x;
int a[maxm], b[maxm], c[maxm];
long long d[maxn][maxn];
vector<int> out[maxn];
struct C {
double a, b;
void clear() {
a = b = 0;
return;
}
C operator+(C x) const { return (C){a + x.a, b + x.b}; }
C operator-(C x) const { return (C){a - x.a, b - x.b}; }
C operator*(C x) const { return (C){a * x.a - b * x.b, a * x.b + b * x.a}; }
};
int rev[8 * maxt];
double p[maxm][maxt];
double g[maxm][maxt];
double f[maxn][2 * maxt];
int read() {
int x = 0, y = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') y = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return x * y;
}
void FFT(C *f, int lim, int opt) {
for (int i = 0; i < lim; i++)
if (i < rev[i]) swap(f[i], f[rev[i]]);
for (int mid = 1; mid < lim; mid <<= 1) {
C wn = (C){cos(pai / mid), opt * sin(pai / mid)};
for (int j = 0; j < lim; j += (mid << 1)) {
C w = (C){1, 0};
for (int k = 0; k < mid; k++, w = w * wn) {
C u = f[j + k], v = w * f[j + k + mid];
f[j + k] = u + v;
f[j + k + mid] = u - v;
}
}
}
if (opt == 1) return;
for (int i = 0; i < lim; i++) f[i].a /= lim;
return;
}
C A[8 * maxt], B[8 * maxt];
void cdq(int l, int r) {
if (l == r) {
for (int i = 1; i < n; i++) {
double res = 1e18;
for (int j = 0; j < out[i].size(); j++) {
int e = out[i][j];
res = min(res, g[e][l] + c[e]);
}
f[i][l] = res;
}
f[n][l] = 0;
return;
}
int mid = l + ((r - l) >> 1);
if (mid == t)
for (int i = t + 1; i <= 2 * t; i++)
for (int j = 1; j <= n; j++) f[j][i] = d[j][n] + x;
else
cdq(mid + 1, r);
int lim = 1, k = 0, l1 = max(t - (r - l), 0);
while (lim < min(r - l + 1, t) + r - mid) lim <<= 1, k++;
for (int i = 1; i < lim; i++)
rev[i] = rev[i >> 1] >> 1 | ((i & 1) << (k - 1));
for (int i = 1; i <= m; i++) {
for (int j = 0; j < lim; j++) A[j].clear(), B[j].clear();
for (int j = mid + 1; j <= r; j++) A[j - mid - 1].a = f[b[i]][j];
for (int j = l1; j <= t; j++) B[j - l1].a = p[i][j];
FFT(A, lim, 1);
FFT(B, lim, 1);
for (int j = 0; j < lim; j++) A[j] = A[j] * B[j];
FFT(A, lim, -1);
for (int j = 0; j <= r - mid - 1 + t - l1; j++) {
if (j + mid + 1 + l1 - t >= l && j + mid + 1 + l1 - t <= mid)
g[i][j + mid + 1 + l1 - t] += A[j].a;
}
}
cdq(l, mid);
return;
}
int main() {
n = read();
m = read();
t = read();
x = read();
memset(d, 0x3f, sizeof(d));
for (int i = 1; i <= n; i++) d[i][i] = 0;
for (int i = 1; i <= m; i++) {
a[i] = read();
b[i] = read();
c[i] = read();
out[a[i]].push_back(i);
for (int j = 1; j <= t; j++) p[i][t - j] = 1.0 * read() / 100000;
d[a[i]][b[i]] = min(d[a[i]][b[i]], 1ll * c[i]);
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
cdq(0, 2 * t);
printf("%.9lf\n", f[1][0]);
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int gdc(int a, int b) {
if (a == b) return a;
if (a > b) return gdc(a - b, b);
return gdc(a, b - a);
}
int main() {
int n;
int a[26];
int oddCount = 0;
int oddIndex = 0;
cin >> n;
for (int index = 0; index < n; index++) {
cin >> a[index];
if (a[index] % 2) {
oddCount++;
oddIndex = index;
}
}
if (n == 1) {
cout << a[0] << endl;
for (int index = 0; index < a[0]; index++) {
cout << "a";
}
cout << endl;
return 0;
}
if (oddCount > 1) {
cout << 0 << endl;
for (int index = 0; index < n; index++) {
for (int index2 = 0; index2 < a[index]; index2++) {
cout << (char)('a' + index);
}
}
cout << endl;
return 0;
}
int mingdc = a[0];
for (int index = 0; index < n; index++) {
for (int index2 = 0; index2 < index; index2++) {
int currgdc = gdc(a[index], a[index2]);
if (mingdc > currgdc) mingdc = currgdc;
}
}
cout << mingdc << endl;
if (mingdc % 2) {
} else {
mingdc /= 2;
}
for (int index = 0; index < n; index++) {
a[index] /= mingdc;
}
for (int index3 = 0; index3 < mingdc; index3++) {
for (int index = 0; index < n; index++) {
for (int index2 = 0; index2 < a[index] / 2; index2++) {
cout << (char)('a' + index);
}
}
if (oddCount) cout << (char)('a' + oddIndex);
for (int index = n - 1; index >= 0; index--) {
for (int index2 = 0; index2 < a[index] / 2; index2++) {
cout << (char)('a' + index);
}
}
}
cout << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
string elements[101] = {
"", "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na",
"Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V",
"Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br",
"Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag",
"Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr",
"Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu",
"Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi",
"Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am",
"Cm", "Bk", "Cf", "Es", "Fm"};
int N, M;
int ori[18], targ[18];
int geti() {
string s;
cin >> s;
return find(elements, elements + 101, s) - elements;
}
int sums[1 << 17];
int mem[1 << 17][17];
int path[1 << 17][17];
int dp(int bitm, int ti) {
if (ti == M) {
return true;
}
int &res = mem[bitm][ti];
if (res != -1) return res;
res = false;
stack<pair<int, int> > poss;
poss.push(pair<int, int>(0, 0));
while (poss.size()) {
int curb = poss.top().first;
int curi = poss.top().second;
poss.pop();
if (sums[curb] == targ[ti]) {
if (dp(bitm | curb, ti + 1)) {
path[bitm][ti] = curb;
return res = true;
}
} else if (curi < N) {
poss.push(pair<int, int>(curb, curi + 1));
int nxtb = curb | (1 << curi);
if ((nxtb & bitm) == 0 && sums[nxtb] <= targ[ti]) {
poss.push(pair<int, int>(nxtb, curi + 1));
}
}
}
return res = false;
}
int main() {
memset(mem, -1, sizeof(mem));
cin >> N >> M;
int cursum = 0;
for (int i = 0; i < N; ++i) {
ori[i] = geti();
cursum += ori[i];
}
sort(ori, ori + N);
for (int i = 0; i < (1 << N); ++i) {
for (int j = 0; j < N; ++j) {
if ((i >> j) & 1) {
sums[i] += ori[j];
}
}
}
for (int i = 0; i < M; ++i) {
targ[i] = geti();
cursum -= targ[i];
}
sort(targ, targ + M);
if (cursum) {
cout << "NO\n";
return 0;
}
if (dp(0, 0)) {
cout << "YES\n";
int curb = 0;
for (int curi = 0; curi < M; ++curi) {
int nxt = path[curb][curi];
int fi = false;
for (int i = 0; i < N; ++i) {
if ((nxt >> i) & 1) {
if (fi) cout << '+';
cout << elements[ori[i]];
fi = true;
}
}
cout << "->" << elements[targ[curi]] << '\n';
curb |= nxt;
}
} else {
cout << "NO\n";
}
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,avx2,abm,mmx")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T1, typename T2>
inline void chkmin(T1 &x, const T2 &y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, const T2 &y) {
if (x < y) x = y;
}
struct fenvik {
vector<int> tree;
int n;
fenvik() {}
fenvik(int sz) {
n = sz;
tree.assign(n, 0);
}
int get(int r) {
int ans = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) ans += tree[r];
return ans;
}
int get(int l, int r) { return get(r) - get(l - 1); }
void upd(int pos, int val) {
for (; pos < n; pos |= pos + 1) tree[pos] += val;
}
};
const int MAXN = 2e5 + 10;
int n, k;
int a[MAXN];
struct event {
int x, l, r, ind, id;
event() {}
event(int _x, int _l, int _r, int _ind) {
x = _x, l = _l, r = _r, ind = _ind;
}
};
bool operator<(const event &a, const event &b) { return a.x < b.x; }
const int MAXQ = 2e5 * 6;
event q[MAXQ];
int topQ = 0;
int Qans[MAXQ];
int l[MAXN], r[MAXN], d[MAXN], u[MAXN];
void build_query() {
for (int i = 0; i < k; i++) {
cin >> l[i] >> d[i] >> r[i] >> u[i];
q[topQ] = {l[i] - 1, 1, d[i] - 1, topQ};
topQ++;
q[topQ] = {l[i] - 1, d[i], u[i], topQ};
topQ++;
q[topQ] = {l[i] - 1, u[i] + 1, n, topQ};
topQ++;
q[topQ] = {r[i], 1, d[i] - 1, topQ};
topQ++;
q[topQ] = {r[i], d[i], u[i], topQ};
topQ++;
q[topQ] = {r[i], u[i] + 1, n, topQ};
topQ++;
}
sort(q, q + topQ);
}
fenvik tree;
void build_Qans() {
tree = fenvik(n + 1);
int posQ = 0;
for (int i = 0; i <= n; i++) {
if (i > 0) tree.upd(a[i], 1);
while (posQ < topQ && q[posQ].x == i) {
Qans[q[posQ].ind] = tree.get(q[posQ].l, q[posQ].r);
posQ++;
}
}
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
build_query();
build_Qans();
for (int i = 0; i < k; i++) {
int s1 = Qans[i * 6];
int s2 = Qans[i * 6 + 1];
int s3 = Qans[i * 6 + 2];
int s4 = Qans[i * 6 + 3];
int s5 = Qans[i * 6 + 4];
int s6 = Qans[i * 6 + 5];
int s7 = tree.get(1, d[i] - 1);
int s8 = tree.get(d[i], u[i]);
int s9 = tree.get(u[i] + 1, n);
s9 -= s6;
s8 -= s5;
s7 -= s4;
s6 -= s3;
s5 -= s2;
s4 -= s1;
ll ans = 0;
ans += (ll)s1 * (s5 + s6 + s8 + s9);
ans += (ll)s2 * (s4 + s5 + s6 + s7 + s8 + s9);
ans += (ll)s3 * (s4 + s5 + s7 + s8);
ans += (ll)s4 * (s5 + s6 + s8 + s9);
ans += (ll)s5 * (s6 + s7 + s8 + s9);
ans += (ll)s6 * (s7 + s8);
ans += (ll)s5 * (s5 - 1) / 2;
cout << ans << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
template <class C, class E>
inline bool contains(const C& container, const E& element) {
return container.find(element) != container.end();
}
template <class T>
inline void checkmin(T& a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T& a, T b) {
if (b > a) a = b;
}
using namespace std;
int n;
int rem;
void Solve(int n, int curDiv) {
cout << curDiv << " ";
if (n == 1) return;
if (n == 2) {
cout << curDiv * 2 << " ";
return;
}
if (n == 3) {
cout << curDiv << " " << curDiv * 3 << " ";
return;
}
for (int i = (0), _b = (((n + 1) / 2 - 1) - 1); i <= _b; i++)
cout << curDiv << " ";
Solve(n - (n + 1) / 2, curDiv * 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << std::setprecision(15);
cout << std::fixed;
cin >> n;
Solve(n, 1);
return 0;
}
| 4 |
#include <bits/stdc++.h>
const long long int MOD = 1e9 + 7;
const int cfnum = 1e5 + 5;
using namespace std;
const long long int cfmod = 998244353;
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int main() {
long long int n, m;
cin >> n >> m;
long long int arr[m];
vector<long long int> opr;
map<int, set<int> > s;
int flag = 1;
long long int sum = 0;
for (int i = 0; i < m; i++) {
cin >> arr[i];
if (i + arr[i] > n) {
flag = 0;
}
opr.push_back(arr[i]);
s[arr[i]].insert(i);
sum += arr[i];
}
if (sum >= n && m <= n && flag == 1) {
int curr = 1;
for (int i = 0; i < m; i++) {
cout << curr << " ";
curr++;
sum -= arr[i];
if (n - curr + 1 > sum) {
curr = n - sum + 1;
}
}
} else {
cout << -1;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
do {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
} while (false);
string L, R;
cin >> L >> R;
reverse(L.begin(), L.end());
L.resize(R.size(), '0');
reverse(L.begin(), L.end());
int n, m = 0;
cin >> n;
const int sigma = 10, M = 3 * R.size() * sigma + 100;
vector<vector<int> > son(M, vector<int>(sigma)),
sum(M, vector<int>(R.size()));
function<void(int, int, bool, bool, bool)> gen = [&](int idx, int u, bool big,
bool small, bool start) {
if (idx >= R.size() || (big && small)) {
sum[u][R.size() - idx]++;
} else {
for (int i = (big ? 0 : L[idx] - '0'); i <= (small ? 9 : R[idx] - '0');
i++) {
int v = u;
if (start || i) {
if (!son[u][i]) {
son[u][i] = m++;
}
v = son[u][i];
}
gen(idx + 1, v, big || L[idx] - '0' < i, small || R[idx] - '0' > i,
start || i);
}
}
};
gen(0, m++, false, false, false);
vector<int> fail(m);
auto build = [&]() {
queue<int> que;
que.push(0);
while (!que.empty()) {
int u = que.front();
que.pop();
for (int i = 0; i < sigma; i++) {
if (!son[u][i]) {
son[u][i] = son[fail[u]][i];
} else {
int v = fail[u];
while (v && !son[v][i]) {
v = fail[v];
}
if (son[v][i] && son[v][i] != son[u][i]) {
fail[son[u][i]] = son[v][i];
for (int j = 0; j < R.size(); j++) {
sum[son[u][i]][j] += sum[son[v][i]][j];
}
}
que.push(son[u][i]);
}
}
}
};
build();
vector<vector<int> > trans(m, vector<int>(sigma));
for (int u = 0; u < m; u++) {
for (int i = 1; i < R.size(); i++) {
sum[u][i] += sum[u][i - 1];
}
for (int i = 0; i < sigma; i++) {
int v = u;
while (v && !son[v][i]) {
v = fail[v];
}
trans[u][i] = son[v][i];
}
}
vector<vector<int> > dp(n + 1, vector<int>(m, -1e9));
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (int u = 0; u < m; u++) {
if (dp[i][u] >= 0) {
for (int j = 0; j < sigma; j++) {
int v = trans[u][j];
dp[i + 1][v] = max(dp[i + 1][v],
dp[i][u] + sum[v][min((int)R.size(), n - i) - 1]);
}
}
}
}
int goal = *max_element(dp[n].begin(), dp[n].end());
cout << goal << endl;
vector<vector<bool> > on(n + 1, vector<bool>(m));
for (int i = 0; i < m; i++) {
on[n][i] = dp[n][i] == goal;
}
for (int i = n - 1; i >= 0; i--) {
for (int u = 0; u < m; u++) {
if (dp[i][u] >= 0) {
bool ok = false;
for (int j = 0; j < sigma; j++) {
int v = trans[u][j];
ok |=
on[i + 1][v] &&
dp[i][u] + sum[v][min((int)R.size(), n - i) - 1] == dp[i + 1][v];
}
on[i][u] = ok;
}
}
}
int u = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < sigma; j++) {
int v = trans[u][j];
if (on[i + 1][v] &&
dp[i][u] + sum[v][min((int)R.size(), n - i) - 1] == dp[i + 1][v]) {
cout << j;
u = v;
break;
}
}
}
cout << endl;
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[8][11];
char nums[10][11];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++) cin >> s[i][j];
s[i][10] = '\0';
}
for (int i = 0; i < 10; i++) cin >> nums[i];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++)
if (!strcmp(nums[j], s[i])) {
cout << j;
break;
}
}
return 0;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.