AtCoder Beginner Contest 339
AtCoder Beginner Contest 339
最水的一场,但打得稀里哗啦。
E - Smooth Subsequence
Problem Statement
You are given a sequence
Find the maximum length of a subsequence of
A subsequence of a sequence
Constraints
- All input values are integers.
Solution
一眼 DP。
设
然后可以发现如果要满足
所以写单点修改区间查询的线段树就结束了。
Code
int n, d, a[N];
int f[N];
struct Tree {
int l, r, v;
}tr[N << 2];
void pushup(int u) {
tr[u].v = max(tr[ls].v, tr[rs].v);
return;
}
void build(int u, int l, int r) {
tr[u] = {l, r};
if (l != r) {
int mid = l + r >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
}
return;
}
void modify(int u, int x, int d) {
if (tr[u].l == tr[u].r) tr[u].v = max(tr[u].v, d);
else {
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(ls, x, d);
else modify(rs, x, d);
pushup(u);
}
return;
}
int query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) return tr[u].v;
int mid = tr[u].l + tr[u].r >> 1, res = 0;
if (l <= mid) res = query(ls, l, r);
if (r > mid) res = max(res, query(rs, l, r));
return res;
}
signed main()
{
n = read(), d = read();
read(a + 1, a + n + 1);
build(1, 1, 5e5);
int res = 0;
fup (i, 1, n) {
f[i] = max(1ll, query(1, max(1ll, a[i] - d), min(500000ll, a[i] + d)) + 1);
modify(1, a[i], f[i]);
res = max(f[i], res);
}
wel(res);
return 0;
}
F - Product Equality
Problem Statement
You are given
Find the number of triples of integers
Constraints
Solution
如果
反过来,如果
当这个
这启发我们将读入的
找一个大质数可以这样做:
- 打开一个叫作质数发生器和校验器的东西;
- 选择“上一个质数”;
- 上面的框里输上
( ),点击计算。
Code
注意要开 __int128
。
#define int __int128
const int P = 99999999999999997;
int n, a[N];
map<int, int> p;
signed main()
{
n = read();
fup (i, 1, n) {
string s;
cin >> s;
for (char &t : s) a[i] = (a[i] * 10 + t - '0') % P;
++ p[a[i]];
}
int res = 0;
fup (i, 1, n)
fup (j, 1, n)
res += p[a[i] * a[j] % P];
wel(res);
return 0;
}
G - Smaller Sum
Problem Statement
You are given a sequence
Answer the following
- Find the sum of the elements among
that are not greater than .
Here, you need to answer these queries online.
That is, only after you answer the current query is the next query revealed.
For this reason, instead of the
- Let
and (the answer to the -th query). - Then, the query can be decrypted as follows:
Here,
What is bitwise XOR? The bitwise XOR of non-negative integers
- The digit in the
place ( ) of in binary is if exactly one of the corresponding digits of and in binary is , and otherwise.
For example,
Constraints
- All input values are integers.
- For the encrypted inputs, the following holds:
- For the decrypted queries, the following holds:
Solution
某大神曾经说过:“看到一道 ABC G 题,我们第一想到的应该是卡常和分块”。
做法分块。
维护每个块的左右端点,并在最开始将块内元素排序。
查询时,对于在边上的两个块,我们暴力处理。注意这里需要处理最开始的原序列。
对于中间的块,由于已经排好序了,所以可以直接二分找到最后一个满足它的值小于等于
注意块长需要调成
Code
int n, q, a[N], b[N];
LL l, r, k;
LL s[N]; // 前缀和
struct Node {
int l, r;
}p[N];
int t[N], cnt, B;
inline void build() {
register int l = 1, r = B;
while (r <= n) {
p[ ++ cnt] = {l, r};
fup (i, l, r) t[i] = cnt;
sort(a + l, a + r + 1);
l += B, r += B;
}
if (r != n) {
r = n;
p[ ++ cnt] = {l, r};
fup (i, l, r) t[i] = cnt;
sort(a + l, a + r + 1);
}
fup (i, 1, n) s[i] = s[i - 1] + a[i];
return;
}
inline LL query(const int& l, const int& r, const LL& k) {
const int& L = t[l], R = t[r];
register LL res = 0;
if (L == R) {
fup (i, l, r) res += (b[i] <= k) * b[i];
return res;
}
fup (i, l, p[L].r) res += (b[i] <= k) * b[i];
fup (i, p[R].l, r) res += (b[i] <= k) * b[i];
fup (i, L + 1, R - 1) {
register int x = p[i].l, y = p[i].r, pos = p[i].l - 1;
while (x <= y) {
const int& mid = x + y >> 1;
if (a[mid] <= k) pos = mid, x = mid + 1;
else y = mid - 1;
}
res += s[pos] - s[p[i].l - 1];
}
return res;
}
signed main()
{
n = read();
B = sqrt(n * log2(n));
fup (i, 1, n) a[i] = b[i] = read();
build();
q = read();
register LL lst = 0;
while (q -- ) {
l = (read() ^ lst), r = (read() ^ lst), k = (read() ^ lst);
lst = query(l, r, k);
wel(lst);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!