diff options
Diffstat (limited to '20.5')
-rw-r--r-- | 20.5/dec/plat/sleeping.cpp | 47 | ||||
-rw-r--r-- | 20.5/dec/plat/spaceship.cpp | 49 | ||||
-rw-r--r-- | 20.5/feb/plat/minimizing.cpp | 78 | ||||
-rw-r--r-- | 20.5/feb/plat/notime.cpp | 64 | ||||
-rw-r--r-- | 20.5/open/balanced.cpp | 118 | ||||
-rw-r--r-- | 20.5/open/balanced_slow.cpp | 73 | ||||
-rw-r--r-- | 20.5/open/ucfj.cpp | 71 |
7 files changed, 500 insertions, 0 deletions
diff --git a/20.5/dec/plat/sleeping.cpp b/20.5/dec/plat/sleeping.cpp new file mode 100644 index 0000000..a7ad9f5 --- /dev/null +++ b/20.5/dec/plat/sleeping.cpp @@ -0,0 +1,47 @@ +#include <bits/stdc++.h> +#define f first +#define s second +using namespace std; +using ll = long long; +using ii = pair<int, int>; +const int MX = 3005, MOD = 1e9+7; + + +ll s[MX], t[MX], DP[2][MX][2]; + + +int main() { + if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout); + ios_base::sync_with_stdio(0), cin.tie(0); + + int N; cin >> N; + for (int i = 0; i < N; ++i) cin >> s[i]; + for (int i = 0; i < N; ++i) cin >> t[i]; + + vector<ii> vc; + for (int i = 0; i < N; ++i) vc.emplace_back(s[i], 0); + for (int i = 0; i < N; ++i) vc.emplace_back(t[i], 1); + sort(begin(vc), end(vc)); + + DP[0][0][0] = 1; + for (int i = 0; i < 2*N; ++i) { + if (vc[i].s == 0) { + for (int j = 0; j <= N; ++j) { + (DP[1][j][1] += DP[0][j][0]) %= MOD; + (DP[1][j+1][0] += DP[0][j][0]) %= MOD; + (DP[1][j][1] += DP[0][j][1]) %= MOD; + (DP[1][j+1][1] += DP[0][j][1]) %= MOD; + } + } + else { + for (int j = 0; j <= N; ++j) { + (DP[1][j][0] += DP[0][j][0]) %= MOD; + if (j) (DP[1][j-1][0] += j*DP[0][j][0]) %= MOD; + if (j) (DP[1][j-1][1] += j*DP[0][j][1]) %= MOD; + } + } + memcpy(DP[0], DP[1], sizeof DP[1]); + memset(DP[1], 0, sizeof DP[1]); + } + cout << (DP[0][0][0]+DP[0][0][1])%MOD; +} diff --git a/20.5/dec/plat/spaceship.cpp b/20.5/dec/plat/spaceship.cpp new file mode 100644 index 0000000..d8d7849 --- /dev/null +++ b/20.5/dec/plat/spaceship.cpp @@ -0,0 +1,49 @@ +#include <bits/stdc++.h> +#define f first +#define s second +using namespace std; +using ll = long long; +using ii = pair<int, int>; +constexpr int MX = 65, MOD = 1e9+7; + +ll bs[MX], s[MX], bt[MX], t[MX], L[2*MX], R[2*MX], G[MX][MX], DP[MX][2*MX][2*MX]; + +int main() { + if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout); + ios_base::sync_with_stdio(0), cin.tie(0); + + int N, K, Q; cin >> N >> K >> Q; + for (int i = 0; i < N; ++i) { + string s; cin >> s; + for (int j = 0; j < N; ++j) G[i][j] = s[j] == '1'; + } + for (int i = 0; i < Q; ++i) + cin >> bs[i] >> s[i] >> bt[i] >> t[i], --bs[i], --s[i], --bt[i], --t[i]; + + for (int i = 0; i < K; ++i) { + for (int j = 0; j < N; ++j) { + for (int k = 0; k < N+Q; ++k) L[k] = k == j; + for (int k = 0; k < Q; ++k) + if (i == bs[k] && j == s[k]) L[N+k] = 1; + + for (int k = 0; k < N+Q; ++k) + for (int l = 0; l < N; ++l) + if (i && G[l][j]) (L[k] += DP[i-1][k][l]) %= MOD; + + for (int k = 0; k < N+Q; ++k) R[k] = k == j; + for (int k = 0; k < Q; ++k) + if (i == bt[k] && j == t[k]) R[N+k] = 1; + + for (int k = 0; k < N+Q; ++k) + for (int l = 0; l < N; ++l) + if (i && G[j][l]) (R[k] += DP[i-1][l][k]) %= MOD; + + for (int k = 0; k < N+Q; ++k) + for (int l = 0; l < N+Q; ++l) + (DP[i][k][l] += L[k]*R[l]) %= MOD; + } + memcpy(DP[i+1], DP[i], sizeof DP[i]); + } + + for (int i = 0; i < Q; ++i) cout << DP[K][N+i][N+i] << '\n'; +} diff --git a/20.5/feb/plat/minimizing.cpp b/20.5/feb/plat/minimizing.cpp new file mode 100644 index 0000000..9d5bf95 --- /dev/null +++ b/20.5/feb/plat/minimizing.cpp @@ -0,0 +1,78 @@ +#include <bits/stdc++.h> +#define f first +#define s second +using namespace std; +using ll = long long; +using ii = pair<int, int>; +constexpr int MX = 1e5+5; + +int ans, D[2*MX]; +vector<int> G[MX], H[2*MX]; + +int main() { + if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout); + cin.tie(0)->sync_with_stdio(0); + + int T; cin >> T; + while (T--) { + int N, M; cin >> N >> M; + for (int u = 1; u <= N; ++u) G[u].clear(); + for (int u = 1; u <= 2*N; ++u) H[u].clear(); + for (int i = 0; i < M; ++i) { + int x, y; cin >> x >> y; + G[x].push_back(y), G[y].push_back(x); + H[x].push_back(y+N), H[y+N].push_back(x); + H[y].push_back(x+N), H[x+N].push_back(y); + } + + for (int u = 1; u <= 2*N; ++u) D[u] = 1e9; + queue<int> q; + D[1] = 0, q.push(1); + while (q.size()) { + int u = q.front(); q.pop(); + for (int v : H[u]) if (D[v] == 1e9) + D[v] = D[u]+1, q.push(v); + } + + if (*max_element(D+1, D+2*N+1) == 1e9) { + cout << N-1 << '\n'; + continue; + } + + /*vector<ii> C; + C.emplace_back(0, 0); + for (int u = 1; u <= N; ++u) { + C.emplace_back(D[u], D[u+N]); + }*/ + + ans = 0; + map<ii, int> f; + map<ii, vector<int>> b; + for (int u = 1; u <= N; ++u) { + ii p(min(D[u], D[u+N]), max(D[u], D[u+N])); + ++f[p], b[p].push_back(u); + } + map<ii, int> ea; + for (auto [p, c] : f) { + int pr = ea[ii(p.f-1, p.s+1)]; + if (p.f+1 == p.s) { + if (p.f == 0) ans += (c+1)/2; + else if (f[ii(p.f-1, p.s-1)]) ans += max((c-pr)+(pr+1)/2, (c+1)/2); + else { + if (pr < c) ans += c-pr; + ans += (c+1)/2; + } + } + else { + ans += c; + if (p.f == 0) ea[p] = c; + else if (f[ii(p.f-1, p.s-1)]) ea[p] += min(c, pr); + else { + if (pr < c) ans += c-pr; + ea[p] = c; + } + } + } + cout << ans << '\n'; + } +} diff --git a/20.5/feb/plat/notime.cpp b/20.5/feb/plat/notime.cpp new file mode 100644 index 0000000..48794bf --- /dev/null +++ b/20.5/feb/plat/notime.cpp @@ -0,0 +1,64 @@ +#include <bits/stdc++.h> +#define f first +#define s second +using namespace std; +using ll = long long; +using ii = pair<int, int>; +constexpr int MX = 2e5+5, B = 300; + +int C[MX], L[MX], R[MX], ST[20][MX], ans[MX]; +pair<ii, int> P[MX]; + +inline int query(int i, int j) { + if (i > j) return MX; + int k = 31-__builtin_clz(j-i+1); + return min(ST[k][i], ST[k][j-(1<<k)+1]); +} + +int main() { + if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout); + cin.tie(0)->sync_with_stdio(0); + + int N, Q; cin >> N >> Q; + for (int i = 0; i < N; ++i) cin >> C[i]; + for (int i = 0; i < Q; ++i) { + cin >> P[i].f.f >> P[i].f.s; + --P[i].f.f, --P[i].f.s, P[i].s = i; + } + + vector<int> tmp(N+1, -1); + for (int i = 0; i < N; ++i) L[i] = tmp[C[i]], tmp[C[i]] = i; + fill(begin(tmp), end(tmp), -1); + for (int i = N-1; i >= 0; --i) R[i] = tmp[C[i]], tmp[C[i]] = i; + + memset(ST, '?', sizeof ST); + for (int i = 0; i < N; ++i) ST[0][i] = C[i]; + for (int i = 0; i < 18; ++i) + for (int j = 0; j+(1<<(i+1)) < N; ++j) ST[i+1][j] = min(ST[i][j], ST[i][j+(1<<i)]); + + sort(P, P+Q, [](auto a, auto b) { + return a.f.f/B == b.f.f/B ? ((a.f.f/B)%2 ? a.f.s > b.f.s : a.f.s < b.f.s) : a.f.f/B < b.f.f/B; + }); + + int l = 0, r = -1, cur = 0; + for (int i = 0; i < Q; ++i) { + while (l > P[i].f.f) { + --l; + if (R[l] == -1 || R[l] > r || query(l+1, R[l]-1) < C[l]) ++cur; + } + while (r < P[i].f.s) { + ++r; + if (L[r] == -1 || L[r] < l || query(L[r]+1, r-1) < C[r]) ++cur; + } + while (l < P[i].f.f) { + if (R[l] == -1 || R[l] > r || query(l+1, R[l]-1) < C[l]) --cur; + ++l; + } + while (r > P[i].f.s) { + if (L[r] == -1 || L[r] < l || query(L[r]+1, r-1) < C[r]) --cur; + --r; + } + ans[P[i].s] = cur; + } + for (int i = 0; i < Q; ++i) cout << ans[i] << '\n'; +} diff --git a/20.5/open/balanced.cpp b/20.5/open/balanced.cpp new file mode 100644 index 0000000..02404a3 --- /dev/null +++ b/20.5/open/balanced.cpp @@ -0,0 +1,118 @@ +#include <bits/stdc++.h> +#define f first +#define s second +using namespace std; +using ll = long long; +using ii = pair<int, int>; +constexpr int MX = 155, MOD = 1e9+7; + +inline void add(int & x, int y) { x += y; if (x > MOD) x -= MOD; } + +string S[MX]; + +struct fenwick_tree_2d { + int FT[MX][MX]; + fenwick_tree_2d() { memset(FT, 0, sizeof FT); } + inline void update(int x, int y, int v) { + for (int i = x+1; i < MX; i += i&-i) + for (int j = y+1; j < MX; j += j&-j) add(FT[i][j], v); + } + inline int query(int x, int y) { + int ret = 0; + for (int i = x+1; i > 0; i -= i&-i) + for (int j = y+1; j > 0; j -= j&-j) add(ret, FT[i][j]); + return ret; + } + inline int query(int xl, int xr, int yl, int yr) { + int ret = query(xr, yr); + add(ret, MOD-query(xl-1, yr)); + add(ret, MOD-query(xr, yl-1)); + add(ret, query(xl-1, yl-1)); + return ret; + } +} DP[MX][4]; + +int main() { + if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout); + cin.tie(0)->sync_with_stdio(0); + + int N; cin >> N; + for (int i = 0; i < N; ++i) cin >> S[i]; + + int ans = 0; + for (int i = 0; i < N; ++i) { + for (int a = 0; a < N; ++a) { + for (int b = a; b < N; ++b) { + if (S[i][b] != 'G') break; + + int sum = 1; + + int d0bb = DP[i][0].query(b,b); + int d0a1b = DP[i][0].query(a-1,b); + int d0ba1 = DP[i][0].query(b,a-1); + int d0a1a1 = DP[i][0].query(a-1,a-1); + // DP[i][0] query(b,b)-query(a-1,b)-query(b,a-1)+query(a-1,a-1); + // add(sum, DP[i][0].query(a, b, a, b)); + add(sum, d0bb), add(sum, MOD-d0a1b), add(sum, MOD-d0ba1), add(sum, d0a1a1); + DP[i+1][0].update(a, b, sum); + add(ans, sum); + + int d1bn1 = DP[i][1].query(b, N-1); + int d1a1n1 = DP[i][1].query(a-1, N-1); + int d1bb1 = DP[i][1].query(b,b-1); + int d1a1b1 = DP[i][1].query(a-1,b-1); + int d0bn1 = DP[i][0].query(b,N-1); + int d0a1n1 = DP[i][0].query(a-1,N-1); + // DP[i][1] query(b, N-1)-query(a-1,N-1)-query(b,b-1)+query(a-1, b-1) + // DP[i][0] query(b, N-1)-query(a-1, N-1)-query(b,b)+query(a-1, b) + // sum = DP[i][1].query(a, b, b, N-1); + sum = 0; add(sum, d1bn1), add(sum, MOD-d1a1n1), add(sum, MOD-d1bb1), add(sum, d1a1b1); + // add(sum, DP[i][0].query(a, b, b+1, N-1)); + add(sum, d0bn1), add(sum, MOD-d0a1n1), add(sum, MOD-d0bb), add(sum, d0a1b); + DP[i+1][1].update(a, b, sum); + add(ans, sum); + + int d2ab = DP[i][2].query(a, b); + //int d21b = 0; //DP[i][2].query(-1, b); + int d2aa1 = DP[i][2].query(a, a-1); + //int d21a1 = 0; //DP[i][2].query(-1, a-1); + //int d01b = 0; //DP[i][0].query(-1, b); + //int d01a1 = 0; //DP[i][0].query(-1, a-1); + // DP[i][2] query(a,b)-query(-1,b)-query(a, a-1)+query(-1, a-1) + // DP[i][0] query(a-1, b)-query(-1,b)-query(a-1,a-1)+query(-1,a-1) + // sum = DP[i][2].query(0, a, a, b); + sum = 0; add(sum, d2ab); //add(sum, MOD-d21b), + add(sum, MOD-d2aa1);//, add(sum, d21a1); + // add(sum, DP[i][0].query(0, a-1, a, b)); + add(sum, d0a1b);//, add(sum, MOD-d01b), + add(sum, MOD-d0a1a1);//, add(sum, d01a1); + DP[i+1][2].update(a, b, sum); + add(ans, sum); + + // int d01n1 = 0;//DP[i][0].query(-1,N-1); + // int d11n1 = 0;//DP[i][1].query(-1, N-1); + // int d11b1 = 0;//DP[i][1].query(-1, b-1); + int d2an1 = DP[i][2].query(a, N-1); + // int d21n1 = 0;//DP[i][2].query(-1, N-1); + // DP[i][3] query(a, N-1)-query(-1,N-1)-query(a,b-1)+query(-1, b-1) + // DP[i][0] query(a-1, N-1)-query(-1,N-1)-query(a-1,b)+query(-1, b) + // DP[i][1] query(a-1, N-1)-query(-1,N-1)-query(a-1,b-1)+query(-1, b-1) + // DP[i][2] query(a, N-1)-query(-1,N-1)-query(a,b)+query(-1, b) + sum = DP[i][3].query(a, N-1); add(sum, MOD-DP[i][3].query(a,b-1)); + // add(sum, DP[i][0].query(0, a-1, b+1, N-1)); + add(sum, d0a1n1);//, add(sum, MOD-d01n1), + add(sum, MOD-d0a1b);//, add(sum, d01b); + // add(sum, DP[i][1].query(0, a-1, b, N-1)); + add(sum, d1a1n1);//, add(sum, MOD-d11n1), + add(sum, MOD-d1a1b1);//, add(sum, d11b1); + // add(sum, DP[i][2].query(0, a, b+1, N-1)); + add(sum, d2an1); + //add(sum, MOD-d21n1), + add(sum, MOD-d2ab);//, add(sum, d21b); + DP[i+1][3].update(a, b, sum); + add(ans, sum); + } + } + } + cout << ans%MOD; +} diff --git a/20.5/open/balanced_slow.cpp b/20.5/open/balanced_slow.cpp new file mode 100644 index 0000000..3f7c3e9 --- /dev/null +++ b/20.5/open/balanced_slow.cpp @@ -0,0 +1,73 @@ +#include <bits/stdc++.h> +#define f first +#define s second +using namespace std; +using ll = long long; +using ii = pair<int, int>; +constexpr int MX = 155, MOD = 1e9+7; + +inline void add(int & x, int y) { x += y; if (x > MOD) x -= MOD; } + +string S[MX]; + +struct fenwick_tree_2d { + int FT[MX][MX]; + fenwick_tree_2d() { memset(FT, 0, sizeof FT); } + inline void update(int x, int y, int v) { + for (int i = x+1; i < MX; i += i&-i) + for (int j = y+1; j < MX; j += j&-j) add(FT[i][j], v); + } + inline int query(int x, int y) { + int ret = 0; + for (int i = x+1; i > 0; i -= i&-i) + for (int j = y+1; j > 0; j -= j&-j) add(ret, FT[i][j]); + return ret; + } + inline int query(int xl, int xr, int yl, int yr) { + int ret = query(xr, yr); + add(ret, MOD-query(xl-1, yr)); + add(ret, MOD-query(xr, yl-1)); + add(ret, query(xl-1, yl-1)); + return ret; + } +} DP[MX][4]; + +int main() { + if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout); + cin.tie(0)->sync_with_stdio(0); + + int N; cin >> N; + for (int i = 0; i < N; ++i) cin >> S[i]; + + int ans = 0; + for (int i = 0; i < N; ++i) { + for (int a = 0; a < N; ++a) { + for (int b = a; b < N; ++b) { + if (S[i][b] != 'G') break; + + int sum = 1; + add(sum, DP[i][0].query(a, b, a, b)); + DP[i+1][0].update(a, b, sum); + add(ans, sum); + + sum = DP[i][1].query(a, b, b, N-1); + add(sum, DP[i][0].query(a, b, b+1, N-1)); + DP[i+1][1].update(a, b, sum); + add(ans, sum); + + sum = DP[i][2].query(0, a, a, b); + add(sum, DP[i][0].query(0, a-1, a, b)); + DP[i+1][2].update(a, b, sum); + add(ans, sum); + + sum = DP[i][3].query(0, a, b, N-1); + add(sum, DP[i][0].query(0, a-1, b+1, N-1)); + add(sum, DP[i][1].query(0, a-1, b, N-1)); + add(sum, DP[i][2].query(0, a, b+1, N-1)); + DP[i+1][3].update(a, b, sum); + add(ans, sum); + } + } + } + cout << ans%MOD; +} diff --git a/20.5/open/ucfj.cpp b/20.5/open/ucfj.cpp new file mode 100644 index 0000000..e7d7475 --- /dev/null +++ b/20.5/open/ucfj.cpp @@ -0,0 +1,71 @@ +#include <bits/stdc++.h> +#define f first +#define s second +using namespace std; +using ll = long long; +using ii = pair<int, int>; +constexpr int MX = 2e5+5; + +int b[MX], l[MX], s[MX]; + +struct fenwick_tree { + int FT[MX]; + fenwick_tree() { memset(FT, 0, sizeof FT); } + inline void update(int x, int v) { if (++x) for (; x < MX; x += x&-x) FT[x] += v; } + inline int query(int x) { int ret = 0; if (++x) for (; x; x -= x&-x) ret += FT[x]; return ret; } + inline int query(int x, int y) { return query(y)-query(x-1); } +} range; + +struct seg_tree { + ll seg[4*MX], tmp[4*MX]; + seg_tree() { memset(seg, 0, sizeof seg), memset(tmp, 0, sizeof tmp); } + inline ll pull(ll a, ll b) { return a+b; } + inline void push(int l, int r, int n) { + if (tmp[n]) { + seg[n] += range.query(l, r) * tmp[n]; + if (l != r) tmp[n<<1] += tmp[n], tmp[n<<1|1] += tmp[n]; + tmp[n] = 0; + } + } + void update(int a, int b, ll v, int l = 0, int r = MX-1, int n = 1) { + push(l, r, n); + if (l > r || l > b || r < a) return; + if (l >= a && r <= b) { tmp[n] += v, push(l, r, n); return; } + int m = l+r>>1; + update(a, b, v, l, m, n<<1), update(a, b, v, m+1, r, n<<1|1); + seg[n] = pull(seg[n<<1], seg[n<<1|1]); + } + ll query(int a, int b, int l = 0, int r = MX-1, int n = 1) { + if (a > b || l > b || r < a) return 0; + push(l, r, n); + if (l >= a && r <= b) return seg[n]; + int m = l+r>>1; + return pull(query(a, b, l, m, n<<1), query(a, b, m+1, r, n<<1|1)); + } +} seg; + +int main() { + if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout); + cin.tie(0)->sync_with_stdio(0); + + int N; cin >> N; + for (int i = 0; i < N; ++i) cin >> b[i]; + + memset(l, -1, sizeof l), memset(s, -1, sizeof s); + ll ans = 0; + for (int i = 0; i < N; ++i) { + if (l[b[i]] == -1) { + seg.update(0, i-2, 1); + } + else { + seg.update(s[b[i]]+1, l[b[i]]-1, -1); + seg.update(l[b[i]], l[b[i]], -seg.query(l[b[i]], l[b[i]])); + seg.update(l[b[i]]+1, i-2, 1); + range.update(l[b[i]], -1); + } + range.update(i, 1); + ans += seg.query(l[b[i]]+1, i-1); + s[b[i]] = l[b[i]], l[b[i]] = i; + } + cout << ans; +} |