# 「HAOI2017」新型城市化

## 题意

$$n \le 10^4 , m \le 1.5 × 10^5$$

## 代码

#include <bits/stdc++.h>

#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl

using namespace std;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
}

void File() {
freopen ("2276.in", "r", stdin);
freopen ("2276.out", "w", stdout);
#endif
}

const int N = 1e4 + 1e3, M = 1.5e5 + 1e3, inf = 0x3f3f3f3f;

template<int Maxn, int Maxm>
struct Dinic {

int Head[Maxn], Next[Maxm], to[Maxm], cap[Maxm], e;

Dinic() { e = 1; }

inline void add_edge(int u, int v, int w) {
to[++ e] = v; cap[e] = w; Next[e] = Head[u]; Head[u] = e;
}

inline void Add(int u, int v, int w) {
}

int dis[Maxn], S, T;

bool Bfs() {
queue<int> Q;
Set(dis, 0); dis[S] = 1; Q.push(S);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = Head[u], v = to[i]; i; v = to[i = Next[i]])
if (cap[i] && !dis[v]) dis[v] = dis[u] + 1, Q.push(v);
}
return dis[T];
}

int cur[Maxn];
int Dfs(int u, int flow) {
if (u == T || !flow) return flow;
int res = 0, f;
for (int &i = cur[u], v = to[i]; i; v = to[i = Next[i]])
if (dis[v] == dis[u] + 1 && (f = Dfs(v, min(flow, cap[i])))) {
cap[i] -= f; cap[i ^ 1] += f; res += f;
if (!(flow -= f)) break;
}
return res;
}

int Run() {
int res = 0;
while (Bfs())
Cpy(cur, Head), res += Dfs(S, inf);
return res;
}

};

Dinic<N, M << 1> T;

int n, m; vector<int> G[N];
map<int, bool> Map[N];

void Build() {
For (u, 1, T.T)
for (int i = T.Head[u], v = T.to[i]; i; v = T.to[i = T.Next[i]])
if (T.cap[i]) G[u].push_back(v), Map[v][u] = true;
}

int lowlink[N], dfn[N], sccno[N], stk[N], top, scc_cnt;

void Tarjan(int u) {
static int clk = 0;
lowlink[u] = dfn[stk[++ top] = u] = ++ clk;
for (int v : G[u]) if (!dfn[v])
else if (!sccno[v]) chkmin(lowlink[u], dfn[v]);
if (lowlink[u] == dfn[u]) {
++ scc_cnt; int cur;
do sccno[cur = stk[top --]] = scc_cnt; while (cur != u);
}
}

int u[M], v[M];

vector<pair<int, int>> ans;

vector<int> E[N];

int col[N];
void Color(int u) {
for (int v : E[u]) if (!col[v])
col[v] = col[u] ^ 3, Color(v);
}

int main () {

File();

T.T = (T.S = n + 1) + 1;

For (i, 1, m) {
E[u[i]].push_back(v[i]);
E[v[i]].push_back(u[i]);
}
For (i, 1, n) if (!col[i]) col[i] = 1, Color(i);

For (i, 1, n)
if (col[i] == 1) T.Add(T.S, i, 1); else T.Add(i, T.T, 1);

For (i, 1, m) {
if (col[u[i]] == 2) swap(u[i], v[i]);
}
T.Run(); Build();

For (i, 1, T.T) if (!dfn[i]) Tarjan(i);
For (i, 1, m)
if (sccno[u[i]] != sccno[v[i]] && Map[u[i]][v[i]]) {
if (u[i] > v[i]) swap(u[i], v[i]); ans.emplace_back(u[i], v[i]);
}

sort(ans.begin(), ans.end());
printf ("%d\n", int(ans.size()));
for (auto it : ans)
printf ("%d %d\n", it.first, it.second);

return 0;

}

# 「HAOI2017」方案数

## 题意

1. $(x, y, z) \to (ax, y, z)$ 当且仅当 $x \subseteq ax$；
2. $(x, y, z) \to (x, ay, z)$ 当且仅当 $y \subseteq ay$；
3. $(x, y, z) \to (x, y, az)$ 当且仅当 $z \subseteq az$。

$$o$$ 个点不能经过了。现在问你到某个点 $$(n, m, r)$$ 的方案数，答案对 $$998244353$$ 取模。

$$n, m, r \le 10^{18}, o \le 10^4$$

## 题解

$$f_i$$ 为第一次碰到的关键点为 $$i$$ 个点的方案数，那么直接做 $$\mathcal O(o^2)$$ 容斥即可。

## 代码

#include <bits/stdc++.h>

#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define bit(x) __builtin_popcountll(x)

using namespace std;

using ll = long long;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

template<typename T = ll>
inline ll read() {
ll x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
}

void File() {
freopen ("2277.in", "r", stdin);
freopen ("2277.out", "w", stdout);
#endif
}

const int N = 1e4 + 1e3, LN = 80, Mod = 998244353;

ll n, m, r;

struct Node {
ll x, y, z;
} P[N];

struct Cmp {
inline bool operator () (const Node &lhs, const Node &rhs) const {
if (lhs.x != rhs.x) return lhs.x < rhs.x;
if (lhs.y != rhs.y) return lhs.y < rhs.y;
return lhs.z < rhs.z;
}
};

int f[N], g[LN][LN][LN], comb[LN][LN];

int main () {

File();

int lim = ceil(log2(max({n, m, r})) + 1);
For (i, 0, lim) {
comb[i][0] = 1;
For (j, 1, i) comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % Mod;
}

g[0][0][0] = 1;
For (x, 0, lim) For (y, 0, lim) For (z, 0, lim) {
For (ax, 0, x - 1)
g[x][y][z] = (g[x][y][z] + 1ll * g[ax][y][z] * comb[x][x - ax]) % Mod;
For (ay, 0, y - 1)
g[x][y][z] = (g[x][y][z] + 1ll * g[x][ay][z] * comb[y][y - ay]) % Mod;
For (az, 0, z - 1)
g[x][y][z] = (g[x][y][z] + 1ll * g[x][y][az] * comb[z][z - az]) % Mod;
}

int o = read<int>();
For (i, 1, o) {
ll x = read(), y = read(), z = read();
P[i] = (Node) {x, y, z};
}
P[++ o] = (Node) {n, m, r};
sort(P + 1, P + o + 1, Cmp());

For (i, 1, o) {
f[i] = g[bit(P[i].x)][bit(P[i].y)][bit(P[i].z)];
For (j, 1, i - 1)
if ((P[j].x & P[i].x) == P[j].x && (P[j].y & P[i].y) == P[j].y && (P[j].z & P[i].z) == P[j].z)
f[i] = (f[i] - 1ll * f[j] * g[bit(P[i].x ^ P[j].x)][bit(P[i].y ^ P[j].y)][bit(P[i].z ^ P[j].z)]) % Mod;
}
f[o] = (f[o] + Mod) % Mod;
printf ("%d\n", f[o]);

return 0;

}

# 「HAOI2017」字符串

## 题意

1. $|a| = |b|$；
2. 对于所有 $a_i \neq b_i$ 以及 $a_j \neq b_j$，满足 $|i-j| < k$。

$|s|, \sum |p_i| \le 2 \cdot 10^5$

## 题解

$$s$$ 匹配的位置产生贡献同样挂到 $$AC$$ 自动机上的节点上。

## 代码


#include <bits/stdc++.h>

#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define epb emplace_back
#define fir first
#define sec second

using namespace std;

using PII = pair<int, int>;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
}

void File() {
freopen ("2278.in", "r", stdin);
freopen ("2278.out", "w", stdout);
#endif
}

const int N = 2e5 + 1e3;

vector<int> G[N << 1];

template<int Maxn, int Alpha>
struct Aho_Corasick_Automaton {

int ch[Maxn][Alpha], fail[Maxn], Node;

Aho_Corasick_Automaton() { Node = 1; }

inline int Insert(int pos, int c) {
if (!ch[pos][c]) ch[pos][c] = ++ Node;
return ch[pos][c];
}

void Get_Fail() {
queue<int> Q;
Rep (i, Alpha) {
if (ch[1][i])
Q.push(ch[1][i]), fail[ch[1][i]] = 1;
else ch[1][i] = 1;
}
while (!Q.empty()) {
int u = Q.front(); Q.pop();
Rep (i, Alpha) {
int &v = ch[u][i];
if (v) fail[v] = ch[fail[u]][i], Q.push(v);
else v = ch[fail[u]][i];
}
}
}

void Get_Tree() {
For (i, 2, Node) G[fail[i]].epb(i);
}

};

int clk;

template<int Maxn>
struct Fenwick_Tree {

#define lowbit(x) (x & -x)

int sumv[Maxn];

inline void Update(int pos) {
for (; pos <= clk; pos += lowbit(pos))
++ sumv[pos];
}

inline int Query(int pos) {
int res = 0;
for (; pos; pos -= lowbit(pos))
res += sumv[pos];
return res;
}

};

Aho_Corasick_Automaton<N << 1, 94> ACAM;

Fenwick_Tree<N << 1> FT[2];

int dfn[N << 1], efn[N << 1];

void Dfs_Init(int u = 1) {
dfn[u] = ++ clk; for (int v : G[u]) Dfs_Init(v); efn[u] = clk;
}

inline int Ask(int opt, int pos) {
return FT[opt].Query(efn[pos]) - FT[opt].Query(dfn[pos] - 1);
}

int ans[N]; vector<PII> Q[N << 1]; vector<int> V[N << 1];

void Process(int u) {
for (PII cur : Q[u])
ans[cur.fir] += (cur.sec > 0 ? -1 : 1) * Ask(cur.sec < 0, abs(cur.sec));
for (int cur : V[u])
FT[cur < 0].Update(dfn[abs(cur)]);
for (int v : G[u]) Process(v);
for (PII cur : Q[u])
ans[cur.fir] += (cur.sec > 0 ? 1 : -1) * Ask(cur.sec < 0, abs(cur.sec));
}

int n, k; char S[N], T[N];

int L[N], R[N], pos[2][N];

int main () {

File();

k = read(); scanf ("%s", S + 1);
int lenS = strlen(S + 1);

For (i, 1, n) {
scanf ("%s", T + 1);
int lenT = strlen(T + 1), u = 1;
if (lenT < k) {
ans[i] = lenS - lenT + 1; continue;
}

For (j, 1, lenT) L[j] = u = ACAM.Insert(u, T[j] - 33); u = 1;
Fordown (j, lenT, 1) R[j] = u = ACAM.Insert(u, T[j] - 33);

For (j, 0, lenT - k) Q[j ? L[j] : 1].epb(i, j == jend ? 1 : R[j + k + 1]);
For (j, 1, lenT - k) Q[L[j]].epb(i, - R[j + k]);
}
ACAM.Get_Fail(); ACAM.Get_Tree(); Dfs_Init();

pos[0][0] = pos[1][lenS + 1] = 1;
For (i, 1, lenS)
pos[0][i] = ACAM.ch[pos[0][i - 1]][S[i] - 33];
Fordown (i, lenS, 1)
pos[1][i] = ACAM.ch[pos[1][i + 1]][S[i] - 33];

For (i, 0, lenS - k)
V[pos[0][i]].epb(pos[1][i + k + 1]);
For (i, 1, lenS - k)
V[pos[0][i]].epb(- pos[1][i + k]);

Process(1);
For (i, 1, n) printf ("%d\n", ans[i]);

return 0;

}

# 「HAOI2017」八纵八横

## 题意

$$n \le 500, m \le 500, Q \le 1000, len \le 1000$$

$$len$$ 为边权的二进制位长度。

## 代码

#include <bits/stdc++.h>

#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

template<typename T> inline bool chkmin(T &a, T b) {return b < a ? a = b, 1 : 0;}
template<typename T> inline bool chkmax(T &a, T b) {return b > a ? a = b, 1 : 0;}

inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
}

void File() {
freopen ("2312.in", "r", stdin);
freopen ("2312.out", "w", stdout);
#endif
}

const int N = 1005, M = N << 1;

typedef bitset<N> Info;

struct Base {

Info B[N];

inline void Insert(Info cur) {
Fordown (i, N - 5, 0)
if (cur[i]) {
if (!B[i].any()) { B[i] = cur; break; }
else cur ^= B[i];
}
}

Info Query() {
Info res; res.reset();
Fordown (i, N - 5, 0)
if (!res[i]) res ^= B[i];
return res;
}

};

char str[N];
Info Trans() {
Info res; res.reset();
scanf ("%s", str);
int len = strlen(str);
reverse(str, str + len);
For (i, 0, len)
res[i] = str[i] == '1';
return res;
}

inline void Out(Info cur) {
bool flag = false;
Fordown (i, N - 5, 0) {
if (cur[i] == 1) flag = true;
if (flag) putchar (cur[i] + 48);
}
putchar ('\n');
}

int n, m, q;

int fa[N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

int Head[N], Next[M], to[M], e = 0; Info val[M];
inline void add_edge(int u, int v, Info w) {
to[++ e] = v; Next[e] = Head[u]; Head[u] = e; val[e] = w;
}

Info dis[N];
void Dfs_Init(int u = 1, int fa = 0) {
for (int i = Head[u]; i; i = Next[i]) {
int v = to[i]; if (v == fa) continue ;
dis[v] = dis[u] ^ val[i];
Dfs_Init(v, u);
}
}

int tim[N], now = 0;

struct Option {

int x, y; Info z;

} lt[N];

#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r

Info ans[N];

template<int Maxn>
struct Segment_Tree {

vector<Option> V[Maxn];

void Update(int o, int l, int r, int ul, int ur, Option uv) {
if (ul <= l && r <= ur) { V[o].push_back(uv); return ; }
int mid = (l + r) >> 1;
if (ul <= mid) Update(lson, ul, ur, uv);
if (ur > mid) Update(rson, ul, ur, uv);
}

void Dfs(int o, int l, int r, Base cur) {
For (i, 0, V[o].size() - 1) {
int u = V[o][i].x, v = V[o][i].y; Info w = V[o][i].z;
cur.Insert(dis[u] ^ dis[v] ^ w);
}
if (l == r) { ans[l] = cur.Query(); return ; }
int mid = (l + r) >> 1;
Dfs(lson, cur); Dfs(rson, cur);
}

};

Segment_Tree<N << 2> T;

bool Cancel[N];

int main () {

File();

For (i, 1, n) fa[i] = i;
For (i, 1, m) {
int u = read(), v = read(); Info w = Trans();
if (find(u) != find(v))
fa[find(u)] = find(v), add_edge(u, v, w), add_edge(v, u, w);
else
T.Update(1, 1, q + 1, 1, q + 1, (Option){u, v, w});
}
Dfs_Init();

For (i, 1, q) {
scanf ("%s", str + 1);
if (str[1] == 'A') {
int u = read(), v = read(); Info w = Trans();
lt[++ now] = (Option){u, v, w}, tim[now] = i + 1;
} else if (str[2] == 'a') {
int id = read(); Cancel[id] = true;
T.Update(1, 1, q + 1, tim[id], i, lt[id]);
} else {
int id = read(); Info w = Trans();
T.Update(1, 1, q + 1, tim[id], i, lt[id]); tim[id] = i + 1; lt[id].z = w;
}
}

For (i, 1, q) if (!Cancel[i])
T.Update(1, 1, q + 1, tim[i], q + 1, lt[i]);

T.Dfs(1, 1, q + 1, Base());

For (i, 1, q + 1) Out(ans[i]);

return 0;

}

# 「HAOI2017」供给侧改革

## 题意

$$Q$$ 次询问。对于每一个询问 $$L$$$$R$$。求

$\mathit{ans} = \sum\limits_{ L \le i \lt R } \operatorname{data}(i, R)$

$$S$$ 随机生成

$n \leq 100000, Q \leq 100000$

## 代码

#include <bits/stdc++.h>

#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl

using namespace std;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
}

void File() {
freopen ("2313.in", "r", stdin);
freopen ("2313.out", "w", stdout);
#endif
}

const int N = 1e5 + 1e3, L = 40;

vector<int> V[N]; int lef[L][N];

namespace Trie {

const int Maxn = N * L;

int ch[Maxn][2], Node; vector<int> ver[Maxn];

int Insert(int *P, int Len, int pos) {
int u = 0;
Rep (i, Len) {
ver[u].push_back(pos);
int &v = ch[u][P[i]];
if (!v) v = ++ Node; u = v;
}
ver[u].push_back(pos);
return u;
}

void Get(int u, int len) {
if (int(ver[u].size()) <= 1) return;
Rep (i, ver[u].size() - 1)
lef[len][ver[u][i + 1]] = ver[u][i];
Rep (id, 2) if (ch[u][id]) Get(ch[u][id], len + 1);
}

}

int P[N], n, q, id[N]; char str[N];

struct Seg { int pos, val; } S[N];

int main () {

using namespace Trie;

File();

scanf ("%s", str + 1);

For (i, 1, n) P[i] = str[i] ^ '0';
For (i, 1, n) id[i] = Insert(P + i, min(L - 1, n - i + 1), i);

Get(0, 0);

Rep (i, L) For (j, 1, n) chkmax(lef[i][j], lef[i][j - 1]);

For (tim, 1, q) {
int l = read(), r = read();

Rep (i, L)
S[i] = (Seg) {lef[i][r], i};
sort(S, S + L, [&](Seg a, Seg b) { return a.pos != b.pos ? a.pos < b.pos : a.val > b.val; });

int ans = 0, Last = l - 1;
Rep (i, L) {
if (S[i].pos < l) continue;
if (i && S[i].pos == S[i - 1].pos) continue;
ans += S[i].val * (S[i].pos - Last); Last = S[i].pos;
}

printf ("%d\n", ans);
}

return 0;

}

0条评论