A
按照题面模拟即可。
code
#include<bits/stdc++.h>
char ch;
int main()
{
std::cin >> ch;
if(ch == 'B')
puts("Y");
else if(ch == 'Y')
puts("R");
else puts("B");
return 0;
}B
按照题面模拟即可。
code
#include<bits/stdc++.h>
const int MAXN = 100 + 5;
int n, d, x[MAXN];
int ans[MAXN], tot;
int main()
{
scanf("%d%d", &n, &d);
for(int i = 1; i <= n; i++)
scanf("%d", &x[i]);
for(int i = 1; i <= n; i++)
{
bool fl = 1;
for(int j = 1; j <= n && fl; j++)
fl &= ((j == i) || (abs(x[i] - x[j]) >= d));
if(fl) ans[++tot] = i;
}
printf("%d\n", tot);
for(int i = 1; i <= tot; i++)
printf("%d%c", ans[i], (i == tot ? '\n' : ' '));
return 0;
}也可以排完序后双指针,时间复杂度为 ,瓶颈在于排序。
C
先求出 内每一个子串为 的位置,对于一个串 合法,当且仅当里面存在一个 的子串,且末尾小于等于 ,转化为求后缀 ,时间复杂度为 。
注意后缀 时处理到第 位。
code
#include<bits/stdc++.h>
const int MAXN = 4e5 + 5;
const int inf = 1e9;
constexpr int M1 = 998244353, M2 = 1e9 + 7, B = 131;
std::string s, t; int q;
struct HASH
{
int hash1[MAXN], hash2[MAXN], pw1[MAXN], pw2[MAXN];
inline void init(const std::string s, int n)
{
for(int i = 1; i <= n; i++)
hash1[i] = (1ll * hash1[i - 1] * B % M1 + s[i]) % M1, hash2[i] = (1ll * hash2[i - 1] * B % M2 + s[i]) % M2;
pw1[0] = pw2[0] = 1;
for(int i = 1; i <= n; i++)
pw1[i] = 1ll * pw1[i - 1] * B % M1, pw2[i] = 1ll * pw2[i - 1] * B % M2;
}
inline int gethash1(int l, int r)
{
return ((hash1[r] - 1ll * hash1[l - 1] * pw1[r - l + 1] % M1) % M1 + M1) % M1;
}
inline int gethash2(int l, int r)
{
return ((hash2[r] - 1ll * hash2[l - 1] * pw2[r - l + 1] % M2) % M2 + M2) % M2;
}
}H1, H2;
int pre[MAXN];
int main()
{
scanf("%d", &q); std::cin >> s >> t;
int n = s.size(), m = t.size(); s = " " + s, t = " " + t;
H1.init(s, n), H2.init(t, m);
for(int i = 1; i <= n + 1; i++)
pre[i] = inf;
for(int i = n - m + 1; i >= 1; i--)
{
pre[i] = pre[i + 1];
if(H1.gethash1(i, i + m - 1) == H2.gethash1(1, m) && H1.gethash2(i, i + m - 1) == H2.gethash2(1, m))
pre[i] = i + m - 1;
}
for(int _ = 1, l, r; _ <= q; _++)
{
scanf("%d%d", &l, &r);
if(pre[l] <= r)
puts("Yes");
else puts("No");
}
return 0;
}D
倒着处理,因为正着处理会算错操作二夹在两个操作一的情况。
code
#include<bits/stdc++.h>
const int MAXN = 3e5 + 5;
int n, q;
int vis[MAXN]; char ans[MAXN];
struct Query
{
int op, x; char ch;
}qy[MAXN];
bool used[MAXN];
int main()
{
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i++)
ans[i] = 'a';
for(int _ = 1, op, x; _ <= q; _++)
{
scanf("%d", &op);
if(op == 1)
{
scanf("%d", &x);
vis[x] ^= 1, qy[_] = {1, x, '1'};
}
else
{
char ch; std::cin >> ch;
qy[_] = {2, 0, ch};
}
}
std::set<int> s;
for(int i = 1; i <= n; i++)
if(vis[i] == 0)
s.insert(i);
for(int i = q; i >= 1; i--)
{
int op = qy[i].op;
if(op == 2)
{
for(int it : s)
ans[it] = qy[i].ch, used[it] = 1;
s.clear();
}
else
{
int x = qy[i].x;
if(vis[x])
{
if(!used[x])
s.insert(x);
}
else s.erase(x);
vis[x] ^= 1;
}
}
for(int i = 1; i <= n; i++)
std::cout << ans[i];
return 0;
}E
容易发现连边 本质上是在做一个长度为 的环,这个环上没有点 ,所以分讨,如果没有点 ,这一部分的答案就是 ,前缀和做,预处理 ,查询 ;对于有 点,其实就是 去 ,然后 再去 ,也就是 和 的最短路,对于 这一部分需要建出反图,因为题中给的是无向图,所以不用,对于 这一部分正常跑就行了。时间复杂度为 。
code
#include<bits/stdc++.h>
const int MAXN = 2e5 + 5;
#define pb push_back
typedef long long LL;
const LL inf = 1e15;
typedef std::pair<int, int> PII;
typedef std::pair<LL, int> PLI;
int n, q, a[MAXN], b[MAXN];
std::vector<PII> e[MAXN];
inline void add(int u, int v, int w)
{
e[u].pb({v, w});
}
LL suf[MAXN], dis[MAXN]; bool vis[MAXN];
int main()
{
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for(int i = 1; i <= n; i++)
scanf("%d", &b[i]);
for(int i = 1; i <= n; i++)
suf[i] = suf[i - 1] + a[i], add(i, i % n + 1, a[i]), add(i % n + 1, i, a[i]), add(i, n + 1, b[i]), add(n + 1, i, b[i]);
for(int i = 1; i <= n + 1; i++)
dis[i] = inf;
std::priority_queue<PLI> PQ;
dis[n + 1] = 0; PQ.push({0, n + 1});
while(PQ.size())
{
int u = PQ.top().second; PQ.pop();
if(vis[u]) continue;
vis[u] = 1;
for(auto it : e[u])
{
int v = it.first, w = it.second;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
if(!vis[v])
PQ.push({-dis[v], v});
}
}
}
for(int _ = 1, s, t; _ <= q; _++)
{
scanf("%d%d", &s, &t);
LL ans = dis[s] + dis[t];
if(t <= n)
ans = std::min({ans, suf[t - 1] - suf[s - 1], suf[n] - suf[t - 1] + suf[s - 1]});
printf("%lld\n", ans);
}
return 0;
}F
记 表示行为 ,列为 的长方形。
对于一次询问 把它拆成 和 ,这个东西离线下来,然后枚举行,用线段树维护列。时间复杂度为 。
code
#include<bits/stdc++.h>
const int MAXN = 2e5 + 5;
typedef long long LL;
static char ibuf[1 << 20], *ip = ibuf, *iend = ibuf;
inline int gc() { if (ip == iend) iend = (ip = ibuf) + fread(ibuf, 1, 1 << 20, stdin); return ip == iend ? EOF : *ip++; }
const int BUFSIZE = 1 << 20;
char obuf[BUFSIZE], *op = obuf;
inline void flush() { fwrite(obuf, 1, op - obuf, stdout); op = obuf; }
inline void pc(char c) { if (op == obuf + BUFSIZE) flush(); *op++ = c; }
inline bool blank(const char x) { return x == ' ' || x == '\n' || x == '\r' || x == '\t'; }
template<typename Tp> inline void read(Tp &x) { x = 0; bool z = true; int a = gc(); for(; !('0' <= a && a <= '9'); a = gc()) if(a == '-') z = false; for(; ('0' <= a && a <= '9'); a = gc()) x = (x << 1) + (x << 3) + (a ^ 48); x = (z ? x : ~x + 1); }
inline void read(double &x) { x = 0.0; bool z = true; double y = 0.1; int a = gc(); for(; !('0' <= a && a <= '9'); a = gc()) if(a == '-') z = false; for(; ('0' <= a && a <= '9'); a = gc()) x = x * 10 + (a ^ 48); if(a != '.') return x = z ? x : -x, void(); for(a = gc(); ('0' <= a && a <= '9'); a = gc(), y /= 10) x += y * (a ^ 48); x = (z ? x : -x); }
inline void read(char &x) { for(x = gc(); blank(x) && (x ^ -1); x = gc()); }
inline void read(char *x) { char a = gc(); for(; blank(a) && (a ^ -1); a = gc()); for(; !blank(a) && (a ^ -1); a = gc()) *x++ = a; *x = 0; }
inline void read(std::string &x) { x = ""; char a = gc(); for(; blank(a) && (a ^ -1); a = gc()); for(; !blank(a) && (a ^ -1); a = gc()) x += a; }
template<typename T, typename ...Tp> inline void read(T &x, Tp &...y) { read(x), read(y...); }
template<typename Tp> inline void write(Tp x) { if(!x) return pc(48), void(); if(x < 0) pc('-'), x = ~x + 1; int len = 0; char tmp[64]; for(; x; x /= 10) tmp[++len] = x % 10 + 48; while(len) pc(tmp[len--]); }
inline void write(const double x) { int a = 6; double b = x, c = b; if(b < 0) pc('-'), b = -b, c = -c; double y = 5 * powl(10.0, -a - 1); b += y, c += y; int len = 0; char tmp[64]; if(b < 1) pc(48); else for(; b >= 1; b /= 10) tmp[++len] = std::floor(b) - std::floor(b / 10) * 10 + 48; while(len) pc(tmp[len--]); pc('.'); for(c *= 10; a; a--, c *= 10) pc(std::floor(c) - std::floor(c / 10) * 10 + 48); }
inline void write(const std::pair<int, double> x) { int a = x.first; if(a < 7) { double b = x.second, c = b; if(b < 0) pc('-'), b = -b, c = -c; double y = 5 * powl(10.0, -a - 1); b += y, c += y; int len = 0; char tmp[64]; if(b < 1) pc(48); else for(; b >= 1; b /= 10) tmp[++len] = std::floor(b) - std::floor(b / 10) * 10 + 48; while(len) pc(tmp[len--]); a ? (pc('.')) : void(); for(c *= 10; a; a--, c *= 10) pc(std::floor(c) - std::floor(c / 10) * 10 + 48); } else { flush(); std::cout << std::fixed << std::setprecision(a) << x.second; } }
inline void write(const char x) { pc(x); }
inline void write(const bool x) { pc(x ? 49 : 48); }
inline void write(char *x) { while (*x) pc(*x++); }
inline void write(const char *x) { while (*x) pc(*x++); }
inline void write(const std::string &x) { for (char c : x) pc(c); }
template<typename T, typename ...Tp> inline void write(T x, Tp ...y) { write(x), write(y...); }
int n, m, q, l[MAXN], r[MAXN];
LL s[MAXN << 2], add[MAXN << 2]; int len[MAXN << 2];
inline int ls(int x)
{
return x << 1;
}
inline int rs(int x)
{
return x << 1 | 1;
}
inline void pushup(int p)
{
s[p] = s[ls(p)] + s[rs(p)];
}
inline void build(int p, int l, int r)
{
s[p] = add[p] = 0, len[p] = r - l + 1;
if(l == r) return;
int mid = l + r >> 1;
build(ls(p), l, mid), build(rs(p), mid + 1, r);
}
inline void spread(int p)
{
if(add[p] == 0)
return;
s[ls(p)] += 1ll * len[ls(p)] * add[p], s[rs(p)] += 1ll * len[rs(p)] * add[p];
add[ls(p)] += add[p], add[rs(p)] += add[p], add[p] = 0;
}
inline void modify(int p, int l, int r, int ql, int qr, int k)
{
if(ql <= l && r <= qr)
return (void)(s[p] += 1ll * len[p] * k, add[p] += k);
spread(p); int mid = l + r >> 1;
if(ql <= mid) modify(ls(p), l, mid, ql, qr, k);
if(mid < qr) modify(rs(p), mid + 1, r, ql, qr, k);
pushup(p);
}
inline LL query(int p, int l, int r, int ql, int qr)
{
if(ql <= l && r <= qr)
return s[p];
spread(p); int mid = l + r >> 1; LL ans = 0;
if(ql <= mid) ans += query(ls(p), l, mid, ql, qr);
if(mid < qr) ans += query(rs(p), mid + 1, r, ql, qr);
return ans;
}
struct Query
{
int id, x, l, r, v;
inline friend bool operator<(const Query &x, const Query &y)
{
return x.x < y.x;
}
}qy[MAXN << 1];
LL ans[MAXN];
int main()
{
read(n, m, q);
for(int i = 1; i <= n; i++)
read(l[i], r[i]);
for(int i = 1, a, b, c, d; i <= q; i++)
{
read(a, b, c, d);
qy[i << 1] = {i, b, c, d, 1}, qy[(i << 1) - 1] = {i, a - 1, c, d, -1};
}
std::sort(qy + 1, qy + (q << 1) + 1);
qy[0].x = 0; build(1, 1, m);
for(int i = 1; i <= (q << 1); i++)
{
for(int j = qy[i - 1].x + 1; j <= qy[i].x; j++)
modify(1, 1, m, l[j], r[j], 1);
ans[qy[i].id] += qy[i].v * query(1, 1, m, qy[i].l, qy[i].r);
}
for(int i = 1; i <= q; i++)
write(ans[i], '\n');
flush(); return 0;
}G
树上莫队板子题,代码不想写了。
在维护 的数的个数的时候用值域分块,不要用线段树之类的,因为分块单点修改是 的,而查询是 的,和莫队复杂度一样,反观线段树之类的会多带一个 。
时间复杂度为 。