我是 ---> .
标准快读
C++#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace IO {
template<typename T> inline void read(T &x) {
bool f = 1;
x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')f = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
x = f ? x : -x;
}
template<typename T> inline void write(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(('0' + x % 10));
}
template <typename T, typename ...Args> inline
void read(T &x, Args &...args) {read(x),read(args...);}
template<typename T> inline void write(T x, char c) {write(x), putchar(c);}
}
using namespace IO;
const int INF=1e18;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
return 0;
}
C++namespace Fread {
const int SIZE = 1 << 21;
char buf[SIZE], *S, *T;
inline char getchar() {
if (S == T) {
T = (S = buf) + fread(buf, 1, SIZE, stdin);
if (S == T) return EOF;
}
return *S++;
}
} // namespace Fread
namespace Fwrite {
const int SIZE = 1 << 21;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() {
fwrite(buf, 1, S - buf, stdout);
S = buf;
}
inline void putchar(char c) {
*S++ = c;
if (S == T) flush();
}
struct NTR {
~ NTR() { flush(); }
}ztr;
} // namespace Fwrite
#define getchar Fread :: getchar
#define putchar Fwrite :: putchar
namespace Fastio {
struct Reader {
template<typename T>
Reader& operator >> (T& x) {
char c = getchar();
T f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = getchar();
}
x *= f;
return *this;
}
Reader& operator >> (char& c) {
c = getchar();
while (c == '\n' || c == ' ') c = getchar();
return *this;
}
Reader& operator >> (char* str) {
int len = 0;
char c = getchar();
while (c == '\n' || c == ' ') c = getchar();
while (c != '\n' && c != ' ') {
str[len++] = c;
c = getchar();
}
str[len] = '\0';
return *this;
}
Reader(){}
}cin;
const char endl = '\n';
struct Writer {
template<typename T>
Writer& operator << (T x) {
if (x == 0) { putchar('0'); return *this; }
if (x < 0) { putchar('-'); x = -x; }
static int sta[45];
int top = 0;
while (x) { sta[++top] = x % 10; x /= 10; }
while (top) { putchar(sta[top] + '0'); --top; }
return *this;
}
Writer& operator << (char c) {
putchar(c);
return *this;
}
Writer& operator << (char* str) {
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer& operator << (const char* str) {
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer(){}
}cout;
} // namespace Fastio
#define cin Fastio :: cin
#define cout Fastio :: cout
#define endl Fastio :: endl
线段树
C++#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mid (h+t)/2
const int N=1e6+10;
int a[N],n,m;
struct sgt{
int v[N*4],lazy[N*4];
//向下降点
void down(int x,int h,int t){
v[x*2]+=lazy[x]*(mid-h+1); v[x*2+1]+=lazy[x]*(t-mid);
lazy[x*2]+=lazy[x]; lazy[x*2+1]+=lazy[x];
lazy[x]=0;
}
//修改
void g(int x,int h,int t,int h1,int t1,int vv){
if (h1<=h&&t<=t1){
v[x]+=(t-h+1)*vv;
lazy[x]+=vv;
return;
}
down(x,h,t);
if (h1<=mid) g(x*2,h,mid,h1,t1,vv);
if (mid<t1) g(x*2+1,mid+1,t,h1,t1,vv);
v[x]=v[x*2]+v[x*2+1];
}
//区间求和
int q(int x,int h,int t,int h1,int t1){
if (h1<=h&&t<=t1){
return v[x];
}
down(x,h,t);
int ans=0;
if (h1<=mid) ans+=q(x*2,h,mid,h1,t1);
if (mid<t1) ans+=q(x*2+1,mid+1,t,h1,t1);
return ans;
}
}S;
signed main(){
ios::sync_with_stdio(false);
cin>>n>>m;
for (int i=1;i<=n;i++) {
cin>>a[i];
S.g(1,1,n,i,i,a[i]);
}
for (int i=1;i<=m;i++) {
int k,x,y,z;
cin>>k;
if (k==1){
cin>>x>>y>>z;
S.g(1,1,n,x,y,z);
} else {
cin>>x>>y;
cout<<S.q(1,1,n,x,y)<<endl;
}
}
return 0;
}
最小生成树
C++#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node {
int u, v, w;
node(int u, int v, int w) : u(u), v(v), w(w) {}
bool operator<(const node& o) const { return w < o.w; }
};
struct DSU {
vector<int> f, r;
DSU(int n) : f(n+1), r(n+1, 0) {
for (int i = 1; i <= n; ++i) f[i] = i;
}
int find(int x) {
if (f[x] != x) f[x] = find(f[x]);
return f[x];
}
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
if (r[x] < r[y]) f[x] = y;
else {
f[y] = x;
if (r[x] == r[y]) r[x]++;
}
return true;
}
};
树状数组
C++#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace IO {
template<typename T> inline void read(T &x) {
bool f = 1;
x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
x = f ? x : -x;
}
template<typename T> inline void write(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(('0' + x % 10));
}
template <typename T, typename ...Args> inline
void read(T &x, Args &...args) { read(x); read(args...); }
template<typename T> inline void write(T x, char c) { write(x); putchar(c); }
}
using namespace IO;
const int INF = 1e18;
const int M=5e5+7;
struct Fenwick {
int n;
vector<int> bit;
Fenwick(int _n = 0) { init(_n); }
void init(int _n) { n = _n; bit.assign(n + 1, 0); }
void add(int idx, int val) {
for (; idx <= n; idx += idx & -idx) bit[idx] += val;
}
int sumPrefix(int idx) {
if (idx <= 0) return 0;
if (idx > n) idx = n;
int res = 0;
for (; idx > 0; idx -= idx & -idx) res += bit[idx];
return res;
}
int sumRange(int l, int r) {
if (r < l) return 0;
return sumPrefix(r) - sumPrefix(l - 1);
}
};