我一不小心把分治的块长设置为了 n,即 操作数 的根号。
但是我仍然通过了 CF 的数据,CF 的 hacker 仍需努力。
那么问题来了,这种分治方法的时间复杂度是什么呢?
#include <bits/extc++.h>
#pragma GCC optimize(2)
template <typename T> inline void chkmax(T &x, const T &y) { if (x < y) x = y; }
template <typename T> inline void chkmin(T &x, const T &y) { if (x > y) x = y; }
template <typename T> inline void read(T &x)
{
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch); ch = getchar()) flag = ch == '-';
for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + (ch - '0');
if (flag) x = -x;
}
const int MaxN = 5e5 + 5, lim = 5e5, MaxB = 768;
int n, a[MaxN], b[MaxB][MaxB];
int main()
{
read(n);
int t = std::sqrt(n);
for (int i = 0, op, x, y; i < n; i++)
{
read(op), read(x), read(y);
if (op == 1)
{
a[x] += y;
for (int i = 1; i < t; i++)
b[i][x % i] += y;
}
else
{
int64_t ans = 0;
if (x < t) ans = b[x][y];
else for (int i = y; i <= lim; i += x) ans += a[i];
printf("%ld\n", ans);
}
}
return 0;
}