代码模板
头文件
c++用
头文件模板
#include <bits/stdc++.h>
#define fi first
#define endl '\n'
#define se second
#define lowbit(x) ((x)&(-(x)))
#define all(a) begin(a),end(a)
#define bug(g) cout << "test: " << g << endl
#define lp(i,j,k) for(int i=int(j);i<=int(k);i++)
#define rlp(i,j,k) for(int i=int(j);i>=int(k);i--)
#define IO std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
using namespace std;
using ll = long long;
using pii = std::pair<int, int>;
template <class T> inline void chkmax(T &x,T y) {if(x<y) x=y;}
template <class T> inline void chkmin(T &x,T y) {if(x>y) x=y;}
const int N = 1e6 + 10;
const int TN = 2e3 + 10;
const int INF = 0x3f3f3f3f;
int dx[8] = {0,1,0,-1,1,-1,-1,1};
int dy[8] = {1,0,-1,0,1,-1,1,-1};
int a[N],b[N],f[N],dp[TN][TN],arr[TN][TN],n,m;
mt19937 rd(time(0));
const int MOD = 998244353;
void slove()
{
}
int main()
{
IO
int __T = 1;
// cin >> __T;
while(__T--)slove();
return 0;
}
python
点击查看代码
读取单个数字
x,y,z = (int(i) for i in input().split())
x,y,z = map(int,input().split())
读取数组
num = [int(x) for x in input().split()]
n,m,k = list(map(int,input().split()))
l = list(map(int,input().split()))
求阶乘
math.factorial(3)
基础算法
分治
求解等比数列前\(\mathrm{k}\)项(\(1+p^{2}+\cdots+p^{k-1}\))
偶数分解为
\[ \mathrm{\sum_{i=0}^{\frac{k}{2} -1}P^{i} + \sum_{i=\frac{k}{2}\mathrm{} }^{k-1}P^{i} = (1 + P^{\frac{k}{2}})\sum_{i=0}^{\frac{k}{2} -1}P^{i}}
\]
奇数处理方法\(\mathrm{k-1}\)为偶数转化为\(p^{k-1} + sum(p,k-1)\)
时间复杂度\(\mathrm{O(\log K)}\)
ll sum(int p, int k) {
if (!k)return 1;
if (k & 1) {
return (ksm(p, k - 1) + sum(p, k - 1)) % mod;
}
return ((1 + ksm(p, k / 2)) % mod * (sum(p, k / 2) % mod)) % mod;
}
数据结构
线段树
朴素线段树
#include <bits/stdc++.h>
#define fi first
#define endl '\n'
#define se second
#define lowbit(x) ((x)&(-(x)))
#define all(a) begin(a),end(a)
#define bug(g) cout << "test: " << g << endl
#define lp(i,j,k) for(int i=int(j);i<=int(k);i++)
#define rlp(i,j,k) for(int i=int(j);i>=int(k);i--)
#define IO std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
using namespace std;
using ll = long long;
using pii = std::pair<int, int>;
template <class T> inline void chkmax(T &x, T y) {
if (x < y) x = y;
}
template <class T> inline void chkmin(T &x, T y) {
if (x > y) x = y;
}
const int N = 1e6 + 10;
const int TN = 2e3 + 10;
const int INF = 0x3f3f3f3f;
int dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
int a[N], b[N], f[N], dp[TN][TN], arr[TN][TN], n, m;
mt19937 rd(time(0));
struct Seg {
ll l, r, sum, tag;
} seg[N << 2];
const int MOD = 998244353;
inline int lc(int x) {
return x << 1;
}
inline int rc(int x) {
return x << 1 | 1;
}
void pp(int cur) {
seg[cur].sum = seg[lc(cur)].sum + seg[rc(cur)].sum;
}
void pd(int cur) {
if (seg[cur].tag) {
seg[lc(cur)].tag += seg[cur].tag;
seg[rc(cur)].tag += seg[cur].tag;
seg[lc(cur)].sum += (seg[lc(cur)].r - seg[lc(cur)].l + 1) * seg[cur].tag;
seg[rc(cur)].sum += (seg[rc(cur)].r - seg[rc(cur)].l + 1) * seg[cur].tag;
seg[cur].tag = 0;
}
}
void build(int cur, int l, int r) {
seg[cur] = {l, r, 0, 0};
if (l == r)seg[cur].sum = a[l];
else {
int mid = l + r >> 1;
build(lc(cur), l, mid);
build(rc(cur), mid + 1, r);
pp(cur);
}
}
void add(int cur, int l, int r, int val) {
if (seg[cur].l >= l and seg[cur].r <= r) {
seg[cur].tag += val;
seg[cur].sum += (seg[cur].r - seg[cur].l + 1) * val;
} else {
pd(cur);
int mid = seg[cur].l + seg[cur].r >> 1;
if (l <= mid) {
add(lc(cur), l, r, val);
}
if (r > mid) {
add(rc(cur), l, r, val);
}
pp(cur);
}
}
ll que(int cur, int l, int r) {
if (seg[cur].l >= l and seg[cur].r <= r) {
return seg[cur].sum;
}
pd(cur);
ll ans = 0;
int mid = seg[cur].l + seg[cur].r >> 1;
if (l <= mid) ans += que(lc(cur), l, r);
if (r > mid) ans += que(rc(cur), l, r);
return ans;
}
void slove() {
cin >> n >> m;
lp(i, 1, n)cin >> a[i];
build(1, 1, n);
while (m--) {
int x, y, z, op;
cin >> op;
if (op == 2) {
cin >> x >> y;
cout << que(1, x, y) << endl;
} else {
cin >> x >> y >> z;
add(1, x, y, z);
}
}
}
int main() {
IO
int __T = 1;
// cin >> __T;
while (__T--)slove();
return 0;
}
本文来自博客园,作者:肆月初陸丶,转载请注明原文链接:https://www.cnblogs.com/zarttic/p/16518485.html

代码模板,但是没啥用处
浙公网安备 33010602011771号