x[0] = x[1] = 1;
for (int i = 2; i <= n; ++i)
{
if (x[i] == 0) prime[++cur] = i;
for (int j = 1; j <= cur && i * prime[j] <= n; ++j)
{
x[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
快读快写:
C++
int read()
{
int s = 0, w = 1;
char c = getchar_unlocked();
while (c < '0' || c > '9')
{
if (c == '-') w = -1;
c = getchar_unlocked();
}
while (c >= '0' && c <= '9')
{
s = s * 10 + c - '0';
c = getchar_unlocked();
}
return s * w;
}
void write(int n)
{
if (n < 0)
{
putchar_unlocked('-');
n = -n;
}
if (n >= 10) write(n / 10);
putchar_unlocked(n % 10 + '0');
return;
}
重载运算符加减乘:
C++
struct Big_long
{
int len, a[10001];
Big_long (int x = 0)
{
memset(a, 0, sizeof(a));
for (len = 1; x; len++)
{
a[len] = x % 10;
x /= 10;
}
len--;
}
int &operator[] (int i) {return a[i];}
void carry(int l)
{
len = l;
for (int i = 1; i <= len; i++)
{
a[i + 1] += a[i] / 10;
a[i] %= 10;
}
while (!a[len]) len--;
}
void read()
{
string s;
cin >> s;
len = s.size();
for (int i = s.size() - 1; i >= 0; i--)
a[len - i] = s[i] - '0';
}
void write()
{
for (int i = max(len, 1); i >= 1; i--)
cout << a[i];
}
};
bool operator< (Big_long a, Big_long b)
{
for (int i = max(a.len, b.len); i >= 1; i--)
{
if (a[i] != b[i])
return a[i] < b[i];
}
return 0;
}
bool operator> (Big_long a, Big_long b)
{
for (int i = max(a.len, b.len); i >= 1; i--)
{
if (a[i] != b[i])
return a[i] > b[i];
}
return 0;
}
bool operator<= (Big_long a, Big_long b)
{
for (int i = max(a.len, b.len); i >= 1; i--)
{
if (a[i] != b[i])
return a[i] < b[i];
}
return 1;
}
bool operator>= (Big_long a, Big_long b)
{
for (int i = max(a.len, b.len); i >= 1; i--)
{
if (a[i] != b[i])
return a[i] > b[i];
}
return 1;
}
Big_long operator+ (Big_long a, Big_long b)
{
Big_long c;
int len = max(a.len, b.len);
for (int i = 1; i <= len; i++) c[i] += a[i] + b[i];
c.carry(len + 1);
return c;
}
Big_long operator- (Big_long a, Big_long b)
{
Big_long c;
bool f = 0;
int len = max(a.len, b.len);
c.len = len;
if (a < b)
{
f = 1;
swap(a, b);
}
for (int i = 1; i <= len; ++i)
{
c[i] += a[i] - b[i];
if (c[i] < 0 && i != len)
{
c[i + 1]--;
c[i] += 10;
}
}
while (!(c[c.len])) c.len--;
if (f) c[c.len] = -c[c.len];
return c;
}
Big_long operator* (Big_long a, int b)
{
Big_long c;
int len = a.len;
for (int i = 1; i <= len; i++) c[i] = a[i] * b;
c.carry(len + 11);
return c;
}
exgcd:
C++
void exgcd(ll a, ll b, ll &x, ll &y)
{
if (b == 0)
{
x = 1, y = 0;
return;
}
exgcd(b, a % b, y, x);
y -= a / b * x;
}
CRT:
C++
ll CRT(ll *a, ll *b, ll n)
{
for (int i = 1; i <= n; i++) tot *= a[i];
for (int i = 1; i <= n; i++)
{
m[i] = tot / a[i];
exgcd(m[i], a[i], t[i], y);
t[i] = (a[i] + t[i] % a[i]) % a[i];
x = (x + quickmul(quickmul(m[i], t[i], tot), b[i], tot)) % tot;
}
return x > 0 ? x : x + tot;
}
并查集(计算路径长度):
C++
#define maxn 5001int f[maxn], size[maxn], dict[maxn];
void init()
{
for (int i = 1; i <= n; i++)
{
f[i] = i;
size[i] = 1;
}
}
int find(int x)
{
if (x == f[x]) return x;
f[x] = find(f[x]);
dict[x] += f[x];
return f[x];
}
void join(int x, int y, int d)
{
int f1 = find(x), f2 = find(y);
if (f1 != f2)
{
if (size[f1] < size[f2])
swap(f1, f2);
f[f2] = f1;
dict[f2] = d;
size[f1] += size[f2];
}
}
并查集(路径压缩):
C++
#define maxn 5001int f[maxn], size[maxn];
void init()
{
for (int i = 1; i <= n; i++)
{
f[i] = i;
size[i] = 1;
}
}
int find(int x)
{
if (x == f[x]) return x;
return f[x] = find(f[x]);
}
void join(int x, int y)
{
int f1 = find(x), f2 = find(y);
if (f1 != f2)
{
if (size[f1] < size[f2])
swap(f1, f2);
f[f2] = f1;
size[f1] += size[f2];
}
}
树状数组(单点修改,区间查询):
C++
typedef long long ll;
ll n, m, a, opt, x, y, tree[1000001];
#define lowbit(x) (x & (-x))
void add(ll x, ll k)
{
for (ll i = x; i <= n; i += lowbit(i))
tree[i] += k;
}
ll sum (ll x)
{
ll ans = 0;
for (ll i = x; i; i -= lowbit(i))
ans += tree[i];
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a;
add(i, a);
}
while (m--)
{
cin >> opt >> x >> y;
if (opt == 1)
add(x, y);
if (opt == 2)
cout << sum(y) - sum(x - 1) << '\n';
}
return 0;
}
树状数组(区间修改,单点查询):
C++
typedef long long ll;
ll n, m, opt, x, y, num, a[500001], tree[1000001];
#define lowbit(x) (x & (-x))
void add(ll x, ll k)
{
for (ll i = x; i <= n; i += lowbit(i))
tree[i] += k;
}
ll sum (ll x)
{
ll ans = 0;
for (ll i = x; i; i -= lowbit(i))
ans += tree[i];
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
add(i, a[i] - a[i - 1]);
}
while (m--)
{
cin >> opt;
if (opt == 1)
{
cin >> x >> y >> num;
add(x, num);
add(y + 1, -num);
}
if (opt == 2)
{
cin >> x;
cout << sum(x) << '\n';
}
}
return 0;
}
树状数组(区间修改,区间查询):
C++
typedef long long ll;
ll n, m, opt, x, y, num, a[100001], tree1[100001], tree2[100001];
#define lowbit(x) (x & (-x))
void add(ll x, ll k)
{
for (ll i = x; i <= n; i += lowbit(i))
{
tree1[i] += k;
tree2[i] += x * k;
}
}
ll sum (ll x)
{
ll ans = 0;
for (ll i = x; i; i -= lowbit(i))
ans += (x + 1) * tree1[i] - tree2[i];
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
add(i, a[i] - a[i - 1]);
}
while (m--)
{
cin >> opt;
if (opt == 1)
{
cin >> x >> y >> num;
add(x, num);
add(y + 1, -num);
}
if (opt == 2)
{
cin >> x >> y;
cout << sum(y) - sum(x - 1) << '\n';
}
}
return 0;
}
线段树:
C++
struct tree{ll w, lzy_tag;}t[400001];
void maketag(int u, int len, ll x)
{
t[u].lzy_tag += x;
t[u].w += len * x;
}
void pushup(int u) {t[u].w = t[u << 1].w + t[u << 1 | 1].w;}
void pushdown(int u, int l, int r)
{
int mid = (l + r) >> 1;
maketag(u << 1, mid - l + 1, t[u].lzy_tag);
maketag(u << 1 | 1, r - mid, t[u].lzy_tag);
t[u].lzy_tag = 0;
}
bool is_in(int l, int r, int L, int R){return (L <= l) && (r <= R);}
bool is_out(int l, int r, int L, int R){return (l > R) || (r < L);}
void build(int u, int l, int r)
{
if (l == r)
{
t[u].w = a[l];
return;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void update(int u, int l, int r, int L, int R, ll x)
{
if (is_in(l, r, L, R)) maketag(u, r - l + 1, x);
else if (!is_out(l, r, L, R))
{
int mid = (l + r) >> 1;
pushdown(u, l, r);
update(u << 1, l, mid, L, R, x);
update(u << 1 | 1, mid + 1, r, L, R, x);
pushup(u);
}
}
ll query(int u, int l, int r, int L, int R)
{
if (is_in(l, r, L, R)) return t[u].w;
else if (is_out(l, r, L, R)) return 0;
else
{
int mid = (l + r) >> 1;
pushdown(u, l, r);
return query(u << 1, l, mid, L, R) + query(u << 1 | 1, mid + 1, r, L, R);
}
}