https://codeforces.com/contest/1089
#include
using namespace std;
const int maxm=50100;
const int maxn=500;
char ch[200];
int t,to[maxm],head[maxn],nex[maxm],tot;
struct Blossom {
int p[maxn], vi[maxn], tag, fl[maxn], c[maxn], pr[maxn], q[maxn], r;
int find(int x) {
return x == p[x] ? x : p[x] = find(p[x]);
}
void add(int u, int v) {
t++;
to[t] = v;
nex[t] = head[u];
head[u] = t;
}
int lca(int u, int v) {
++tag;
u = find(u);
v = find(v);
for (;; swap(u, v))
if (u) {
if (fl[u] == tag) return u;
fl[u] = tag;
u = find(pr[c[u]]);
}
}
void blo(int u, int v, int l) {
for (; find(u) != l; v = c[u], u = pr[v]) {
pr[u] = v;
if (vi[c[u]] == 1)
vi[q[++r] = c[u]] = 0;
if (find(u) == u) p[u] = l;
if (find(c[u]) == c[u]) p[c[u]] = l;
}
}
bool aug(int s) {
for (int i = 1; i <= tot; i++) {
p[i] = i;
vi[i] = -1;
}
vi[q[r = 1] = s] = 0;
int x, y;
for (int i = 1; i <= r; i++)
for (int j = head[x = q[i]]; j; j = nex[j])
if (vi[y = to[j]] == -1) {
pr[y] = x;
vi[y] = 1;
if (!c[y]) {
for (int u = x, v = y, t; u; v = t, u = pr[v]) {
t = c[u];
c[u] = v;
c[v] = u;
}
return 1;
}
vi[q[++r] = c[y]] = 0;
} else if (!vi[y] && find(x) != find(y)) {
int l = lca(x, y);
blo(x, y, l);
blo(y, x, l);
}
return 0;
}
void init() {
t = 0;
memset(head, 0, sizeof(head));
memset(c, 0, sizeof(c));
memset(pr, 0, sizeof(pr));
}
};
int main() {
int _, n, m;
scanf("%d", &_);
while (_--) {
Blossom blossom;
blossom.init();
scanf("%d%d", &n, &m);
tot = n * 2 + m;
for (int i = 1; i <= n; i++) {
scanf("%s", ch + 1);
blossom.add(i + m, i + m + n);
blossom.add(i + m + n, i + m);
for (int j = 1; j <= m; j++)
if (ch[j] - '0') {
blossom.add(j, i + m);
blossom.add(i + m, j);
blossom.add(j, i + m + n);
blossom.add(i + m + n, j);
}
}
int ans = 0;
for (int i = 1; i <= tot; i++) {
if (!blossom.c[i]) ans += blossom.aug(i);
}
printf("%d\n", ans - n);
}
return 0;
}
#include
using namespace std;
int f[4][4][205][205],hi;
int main() {
f[0][0][0][0] = 1;
for (int i = 0; i <= 3; i++)
for (int j = 0; j <= 3; j++) {
if (max(i, j) >= 3) continue;
if (i + j == 4) hi = 15; else hi = 25;
for (int k = 0; k <= 200; k++)
for (int l = 0; l <= 200; l++) {
if (!f[i][j][k][l]) continue;
for (int kk = hi; kk <= 200; kk++) {
if (k + kk - 2 <= 200 && l + kk <= 200 && f[i][j + 1][k + kk - 2][l + kk] == 0)
f[i][j + 1][k + kk - 2][l + kk] = 1;
if (k + kk <= 200 && l + kk - 2 <= 200 && f[i + 1][j][k + kk][l + kk - 2] == 0)
f[i + 1][j][k + kk][l + kk - 2] = 1;
}
for (int kk = 0; kk <= hi - 2; kk++) {
if (k + kk <= 200 && l + hi <= 200 && f[i][j + 1][k + kk][l + hi] == 0)
f[i][j + 1][k + kk][l + hi] = 1;
if (k + hi <= 200 && l + kk <= 200 && f[i + 1][j][k + hi][l + kk] == 0)
f[i + 1][j][k + hi][l + kk] = 1;
}
}
}
int _, ans1, ans2;
scanf("%d", &_);
while (_--) {
int a, b;
scanf("%d%d", &a, &b);
ans1 = ans2 = 0;
if (f[3][0][a][b]) {
ans1 = 3;
ans2 = 0;
}else
if (f[3][1][a][b]) {
ans1 = 3;
ans2 = 1;
}else
if (f[3][2][a][b]) {
ans1 = 3;
ans2 = 2;
}else
if (f[2][3][a][b]) {
ans1 = 2;
ans2 = 3;
}else
if (f[1][3][a][b]) {
ans1 = 1;
ans2 = 3;
}else
if (f[0][3][a][b]) {
ans1 = 0;
ans2 = 3;
}
if (ans1 == 0 && ans2 == 0) printf("Impossible\n"); else printf("%d:%d\n", ans1, ans2);
}
}
#include
using namespace std;
typedef long long ll;
ll n;
int main() {
ll cnt = 0;
ll f = 0;
scanf("%lld", &n);
ll x = n;
for (ll i = 2; i <= sqrt(n); ++i) {
if (x % i == 0) {
cnt++;
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1)
cnt++;
if (cnt >= 2)
f = 1;
if (f) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
#include
using namespace std;
const int inf=0x3f3f3f3f;
int a[1000],k,cnt,ans,kk;
int main() {
int _;
scanf("%d", &_);
while (_--) {
scanf("%d", &k);
cnt = 0;
for (int i = 1; i <= 7; i++) {
scanf("%d", &a[i]);
a[i + 7] = a[i];
if (a[i]) cnt++;
}
ans = inf;
for (int i = 1; i <= 7; i++) {
kk = k;
for (int j = i; j <= i + 7; j++) {
kk -= a[j];
if (kk % cnt == 0)
ans = min(ans, j - i + 1 + kk / cnt * 7);
}
}
printf("%d\n", ans);
}
}
#include
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
char s[10];
ll mx[maxn*4],sum[maxn*4],ans,p[maxn],v[maxn];
void pushup(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
mx[rt]=max(mx[rt<<1]+sum[rt<<1|1],mx[rt<<1|1]);
}
void build(int rt,int l,int r) {
if (l == r) {
mx[rt] = l;
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
void update(int rt,int l,int r,int pos,ll val) {
if (l == r) {
sum[rt] += val;
mx[rt] += val;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) update(rt << 1, l, mid, pos, val); else update(rt << 1 | 1, mid + 1, r, pos, val);
pushup(rt);
}
void query(int rt,int l,int r,int pos) {
if (r <= pos) {
ans = max(ans + sum[rt], mx[rt]);
return;
}
int mid = (l + r) >> 1;
query(rt << 1, l, mid, pos);
if (pos>mid) query(rt << 1 | 1, mid + 1, r, pos);
}
int main() {
int q;
ll x;
scanf("%d", &q);
build(1, 1, maxn - 1);
for (int i = 1; i <= q; i++) {
scanf("%s", s);
if (s[0] == '+') {
scanf("%lld%lld", &p[i], &v[i]);
update(1, 1, maxn - 1, p[i], v[i]);
} else if (s[0] == '-') {
scanf("%lld", &x);
update(1, 1, maxn-1, p[x], -v[x]);
} else {
scanf("%lld", &x);
ans = 0;
query(1, 1, maxn-1, x);
printf("%lld\n", max(ans - x, 0ll));
}
}
return 0;
}
#include
#include
#include
using namespace std;
typedef long long ll;
typedef pair pii;
const int maxn = 1e5 + 10;
struct node{
int a;
ll b;
bool operator<(const node& s)const {
return b1&&cnt){
//printf("%d %d %d\n",i,vis[i],c[i].b);
--vis[c[i].a];
--cnt;
res+=c[i].b;
}
}
printf("%lld\n",res);
return 0;
}
#include
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
char s[10];
ll mx[maxn*4],sum[maxn*4],ans,p[maxn],v[maxn];
void pushup(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
mx[rt]=max(mx[rt<<1]+sum[rt<<1|1],mx[rt<<1|1]);
}
void build(int rt,int l,int r) {
if (l == r) {
mx[rt] = l;
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
void update(int rt,int l,int r,int pos,ll val) {
if (l == r) {
sum[rt] += val;
mx[rt] += val;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) update(rt << 1, l, mid, pos, val); else update(rt << 1 | 1, mid + 1, r, pos, val);
pushup(rt);
}
void query(int rt,int l,int r,int pos) {
if (r <= pos) {
ans = max(ans + sum[rt], mx[rt]);
return;
}
int mid = (l + r) >> 1;
query(rt << 1, l, mid, pos);
if (pos>mid) query(rt << 1 | 1, mid + 1, r, pos);
}
int main() {
int q;
ll x;
scanf("%d", &q);
build(1, 1, maxn - 1);
for (int i = 1; i <= q; i++) {
scanf("%s", s);
if (s[0] == '+') {
scanf("%lld%lld", &p[i], &v[i]);
update(1, 1, maxn - 1, p[i], v[i]);
} else if (s[0] == '-') {
scanf("%lld", &x);
update(1, 1, maxn-1, p[x], -v[x]);
} else {
scanf("%lld", &x);
ans = 0;
query(1, 1, maxn-1, x);
printf("%lld\n", max(ans - x, 0ll));
}
}
return 0;
}