#include <bits/stdc++.h>using namespace std;
int main() {
int n ,a[100010];
cin >> n;
for (int i = 1;i <= n;++i)cin >> a[i];
sort(a+1 ,a+1+n);
for (int i = 1;i <= n;++i)cout << a[i] << ' ';
}
P1226 【模板】快速幂
C++
#include<iostream>#define int long longusing namespace std;
int Pow(int x,int y,int z){
if(y==0)return 1;
int res=Pow(x,y/2,z);
if(y%2==1)return ((res*res)%z*x)%z;
else return res*res%z;
}
signed main(){
int x,y,z;
cin>>x>>y>>z;
cout<<x<<"^"<<y<<" mod "<<z<<"="<<Pow(x,y,z);
return 0;
}
P1495【模板】中国剩余定理(CRT)/ 曹冲养猪
C++
#include <bits/stdc++.h>using namespace std;
const int N = 1e5 + 10;
typedef __int128 LL;
class FastIO {
private:static const int SIZE = 1 << 20;
char in[SIZE], out[SIZE], *p1, *p2, *pp;
public:FastIO() : p1(in), p2(in), pp(out) {}
inline char getchar() {
if (p1 == p2) p2 = (p1 = in) + fread(in, 1, SIZE, stdin);
return p1 == p2 ? EOF : *p1++;
}
template<typename T>
inline void read(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch - '0');
ch = getchar();
}
x *= f;
}
inline void write_char(char c) {
if (pp - out == SIZE) {
fwrite(out, 1, SIZE, stdout);
pp = out;
}
*pp++ = c;
}
template<typename T>
inline void write(T x) {
if (x < 0) {
write_char('-');
x = -x;
}
if (x > 9) write(x / 10);
write_char(x % 10 + '0');
}
inline void write_string(const char* s) {
while (*s) {
write_char(*s++);
}
}
~FastIO() {
fwrite(out, 1, pp - out, stdout);
}
}d;
LL n, ans = 1, a[N], b[N], sum = 0;
LL exgcd(LL a ,LL b ,LL &x ,LL &y) {
if (!b) {
x = 1 ,y = 0;
return a;
}
int d = exgcd(b ,a % b ,y ,x);
y -= a / b * x;
return d;
}
int main() {
d.read(n);
for (int i = 1; i <= n; ++i) {
d.read(a[i]);
d.read(b[i]);
ans *= a[i];
}
for (int i = 1; i <= n; ++i) {
LL x, y;
exgcd(ans / a[i],a[i], x, y);
sum = sum + ans / a[i] * b[i] * x % ans;
}
d.write((sum % ans + ans) % ans);
return 0;
}
#include <bits/stdc++.h>using namespace std;
const int MAXN = 133335;
const int MAXM = 133335;
const int MAXC = 1000005;
int n, m;
int a[MAXN];
int blockSize;
struct Query {
int l, r, t, id;
int blockL, blockR;
} q[MAXM];
struct Modify {
int pos, val, pre;
} mod[MAXM];
int qcnt = 0, mcnt = 0;
int ans[MAXM];
int cnt[MAXC];
int colorCount = 0;
bool cmp(const Query& a, const Query& b) {
if (a.blockL != b.blockL) return a.blockL < b.blockL;
if (a.blockR != b.blockR) {
return (a.blockL & 1) ? (a.blockR < b.blockR) : (a.blockR > b.blockR);
}
return (a.blockR & 1) ? (a.t < b.t) : (a.t > b.t);
}
void add(int p) {
if (cnt[a[p]] == 0) colorCount++;
cnt[a[p]]++;
}
void del(int p) {
cnt[a[p]]--;
if (cnt[a[p]] == 0) colorCount--;
}
void apply(int p, int val, int l, int r) {
if (l <= p && p <= r) {
cnt[a[p]]--;
if (cnt[a[p]] == 0) colorCount--;
a[p] = val;
cnt[a[p]]++;
if (cnt[a[p]] == 1) colorCount++;
} else {
a[p] = val;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
blockSize = pow(n, 2.0 / 3.0);
for (int i = 1; i <= m; i++) {
char op;
int x, y;
cin >> op >> x >> y;
if (op == 'Q') {
q[++qcnt] = {x, y, mcnt, qcnt, 0, 0};
q[qcnt].blockL = (x - 1) / blockSize;
q[qcnt].blockR = (y - 1) / blockSize;
} else {
mod[++mcnt] = {x, y, a[x]};
a[x] = y;
}
}
for (int i = mcnt; i >= 1; i--) {
a[mod[i].pos] = mod[i].pre;
}
sort(q + 1, q + qcnt + 1, cmp);
int L = 1, R = 0, T = 0;
for (int i = 1; i <= qcnt; i++) {
while (L > q[i].l) add(--L);
while (R < q[i].r) add(++R);
while (L < q[i].l) del(L++);
while (R > q[i].r) del(R--);
while (T < q[i].t) {
T++;
apply(mod[T].pos, mod[T].val, L, R);
}
while (T > q[i].t) {
apply(mod[T].pos, mod[T].pre, L, R);
T--;
}
ans[q[i].id] = colorCount;
}
for (int i = 1; i <= qcnt; i++) {
cout << ans[i] << "\n";
}
return 0;
}
P1919 【模板】高精度乘法 / A*B Problem 升级版
C++
#include<bits/stdc++.h>using namespace std;
const int N=3e6+10;
const double PI=acos(-1.0);
struct C{
double x,y;
C(double a=0,double b=0):x(a),y(b){}
C operator+(const C&b)const{
return C(x+b.x,y+b.y);
}
C operator-(const C&b)const{
return C(x-b.x,y-b.y);
}
C operator*(const C&b)const{
return C(x*b.x-y*b.y,x*b.y+y*b.x);
}
};
void change(C a[],int n){
for(int i=1,j=n/2;i<n-1;i++){
if(i<j)swap(a[i],a[j]);
int k=n/2;
while(j>=k){j-=k;k/=2;}
if(j<k)j+=k;
}
}
void fft(C a[],int n,int t){
change(a,n);
for(int h=2;h<=n;h<<=1){
C wn(cos(2*PI/h),sin(t*2*PI/h));
for(int j=0;j<n;j+=h){
C w(1,0);
for(int k=j;k<j+h/2;k++){
C u=a[k];
C v=w*a[k+h/2];
a[k]=u+v;
a[k+h/2]=u-v;
w=w*wn;
}
}
}
if(t==-1){
for(int i=0;i<n;i++){
a[i].x/=n;
}
}
}
string s1,s2;
C a[N],b[N];
int ans[N];
int main(){
cin>>s1>>s2;
int n1=s1.size(),n2=s2.size();
int n=1;
while(n<n1*2||n<n2*2){
n*=2;
}
for(int i=0;i<n1;i++){
a[i]=C(s1[n1-1-i]-'0',0);
}
for(int i=n1;i<n;i++){
a[i]=C(0,0);
}
for(int i=0;i<n2;i++){
b[i]=C(s2[n2-1-i]-'0',0);
}
for(int i=n2;i<n;i++){
b[i]=C(0,0);
}
fft(a,n,1);
fft(b,n,1);
for(int i=0;i<n;i++){
a[i]=a[i]*b[i];
}
fft(a,n,-1);
for(int i=0;i<n;i++){
ans[i]=(int)(a[i].x+0.5);
}
for(int i=0;i<n;i++){
ans[i+1]+=ans[i]/10;ans[i]%=10;
}
int h=n1+n2-1;
while(h>0&&ans[h]==0){
h--;
}
for(int i=h;i>=0;i--){
cout<<ans[i];
}
cout<<endl;
return 0;
}
P2613 【模板】有理数取余
C++
#include <bits/stdc++.h>#define LL long longusing namespace std;
const int MOD = 19260817;
inline int read() {
LL ret = 0 , f = 0;
char ch = getchar_unlocked();
while (!isdigit(ch)) {
f |= ch == '-' ,ch = getchar_unlocked();
}
while (isdigit(ch)) {
ret = ret * 10 + ch - '0' ,ch = getchar_unlocked();
ret %= MOD;
}
return f ? -ret : ret;
}
LL x, y, a, b;
void exgcd(LL a ,LL b) {
if (!b) {
x = 1 ,y = 0;
return;
}
exgcd(b ,a % b);
LL xx = x;
x = y;
y = xx - a / b * x;
}
int main() {
a = read();
b = read();
if (!b) {
cout << "Angry!\n";
return 0;
}
exgcd(b, MOD);
(x %= MOD) += MOD;
cout << a * x % MOD << endl;
return 0;
}
P2709 【模板】莫队 / 小B的询问
C++
#include <bits/stdc++.h>using namespace std;
typedef long long ll;
const int N = 5e4 + 10;
int n, m, k, a[N], blen;
int cnt[N], lp = 1, rp = 0;
ll ans[N], cur_ans;
struct Qry {
int l, r, id;
bool operator < (Qry p) {
if ((l - 1) / blen == (p.l - 1) / blen) return r < p.r;
return l / blen < p.l / blen;
}
}q[N];
inline void add_cur(int x) {
cur_ans += 2 * cnt[x] + 1;
cnt[x]++;
}
inline void del_cur(int x) {
cur_ans -= 2 * cnt[x] - 1;
cnt[x]--;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
blen = sqrt(n);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= m; ++i) {
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
sort(q + 1, q + 1 + m);
for (int i = 1; i <= m; ++i) {
int L = q[i].l, R = q[i].r;
while (lp > L) add_cur(a[--lp]);
while (rp < R) add_cur(a[++rp]);
while (lp < L) del_cur(a[lp++]);
while (rp > R) del_cur(a[rp--]);
ans[q[i].id] = cur_ans;
}
for (int i = 1; i <= m; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
P3366 【模板】最小生成树
C++
#include <bits/stdc++.h>using namespace std;
const int N = 5e3 + 10;
const int M = 2e5 + 10;
struct Node {
int w, dis;
} tmp;
bool operator < (Node x, Node y) {
return x.dis > y.dis;
}
priority_queue<Node> q;
vector<pair<int, int>> G[N];
int n, m;
int cnt, ans, dis[N];
bool vis[N];
void Prim() {
dis[1] = 0;
tmp.dis = 0, tmp.w = 1;
q.push(tmp);
while (!q.empty()) {
tmp = q.top();
q.pop();
int w = tmp.w;
if (vis[w]) continue;
cnt++, vis[w] = 1, ans += dis[w];
for (auto [v, ww] : G[w]) {
if (dis[v] > ww) {
tmp.w = v;
tmp.dis = dis[v] = ww;
q.push(tmp);
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y, z;
cin >> x >> y >> z;
G[x].push_back({y, z});
G[y].push_back({x, z});
}
memset(dis, 0x3f3f3f3f, sizeof dis);
Prim();
if (cnt == n) {
cout << ans << endl;
}
else {
cout << "orz\n";
}
return 0;
}
P3367 【模板】并查集
C++
#include <bits/stdc++.h>using namespace std;
const int MAXN = 2e5 + 10;
int n ,m;
int fa[MAXN];//fathervoid init() {
for (int i = 1;i <= n;++i) {
fa[i] = i;
}
}
int find(int x) {//find x fatherif (fa[x] == x) {
return x;
}
return fa[x] = find(fa[x]);
}
void merge(int x ,int y) {
int fx = find(x) , fy = find(y);
if (fx == fy) {
return;
}
fa[fx] = fy;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
init();
while (m--) {
int opt ,x ,y;
cin >> opt >> x >> y;
if (opt == 1) {
merge(x ,y);
}
else {
if (find(x) == find(y)) cout << "Y\n";
else cout << "N\n";
}
}
return 0;
}
P3368 【模板】树状数组 2
C++
#include <bits/stdc++.h>using namespace std;
const int N = 5e5 + 10;
int lowbit(int x) {
return x & -x;
}
int n ,m ,a[N] ,tr[N] ,diff[N];
void update(int x ,int k) {
while (x <= N) {
tr[x] += k;
x += lowbit(x);
}
}
int query(int x) {
int res = 0;
while (x) {
res += tr[x];
x -= lowbit(x);
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
update(i ,a[i] - a[i - 1]);
}
while (m--) {
int opt ,x ,y ,z;
cin >> opt;
if (opt == 1) {
cin >> x >> y >> z;
update(x ,z);
update(y + 1 ,-z);
}
else {
cin >> x;
cout << query(x) << '\n';
}
}
return 0;
}
P3369 【模板】普通平衡树
C++
#include <bits/stdc++.h>#include <ext/rope>using namespace std;
using namespace __gnu_cxx;
rope<int> r;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
while (n--) {
int op;
cin >> op;
if (op == 1) {
int x;
cin >> x;
r.insert(lower_bound(r.begin(), r.end(), x) - r.begin(), x);
}
if (op == 2) {
int x;
cin >> x;
r.erase(lower_bound(r.begin(), r.end(), x) - r.begin(), 1);
}
if (op == 3) {
int x;
cin >> x;
cout << lower_bound(r.begin(), r.end(), x) - r.begin() + 1 << endl;
}
if (op == 4) {
int x;
cin >> x;
cout << r[x - 1] << endl;
}
if (op == 5) {
int x;
cin >> x;
cout << r[lower_bound(r.begin(), r.end(), x) - r.begin() - 1] << endl;
}
if (op == 6) {
int x;
cin >> x;
cout << r[upper_bound(r.begin(), r.end(), x) - r.begin()] << endl;
}
}
return 0;
}
#include <bits/stdc++.h>#define int long longusing namespace std;
const int INF = 0x3f3f3f3f3f3f3f3f;
int n, m, s;
vector<pair<int, int>> G[500010];
int dis[500010];
bool vis[500010];
void Dijkstra() {
dis[s] = 0;
while (true) {
int mn = -1;
for (int i = 1; i <= n; ++i) {
if (!vis[i] && (mn == -1 || dis[i] < dis[mn])) {
mn = i;
}
}
if (mn == -1 || dis[mn] == INF) break;
vis[mn] = 1;
for (auto v : G[mn]) {
int to = v.first;
int w = v.second;
if (dis[to] > dis[mn] + w) {
dis[to] = dis[mn] + w;
}
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> s;
for (int i = 1; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back({v, w});
}
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
Dijkstra();
for (int i = 1; i <= n; ++i) {
if (dis[i] == INF) {
cout << INT_MAX << ' ';
} else {
cout << dis[i] << ' ';
}
}
cout << endl;
return 0;
}
P3372 【模板】线段树 1
C++
#include <bits/stdc++.h>using namespace std;
const long long MAXN = 5e5 + 10;
long long n ,m ,a[MAXN];
struct Node {
long long p ,v ,lazy;
}tr[4*MAXN];
void pushup(long long p) {
tr[p].v = tr[p*2].v + tr[p*2+1].v;
}
void pushdown(long long p ,long long l ,long long r) {
long long ls = p<<1 ,rs = p<<1|1 ,mid = l+r>>1;
tr[ls].v += (mid-l+1) * tr[p].lazy;
tr[ls].lazy+=tr[p].lazy;
tr[rs].v += (r-mid) * tr[p].lazy;
tr[rs].lazy+=tr[p].lazy;
tr[p].lazy = 0;
return;
}
void build(long long l ,long long r ,long long p) {
if (l == r) {
tr[p].v = a[l];
return;
}
long long mid = (l+r) / 2;
build(l , mid ,p<<1);
build(mid+1 ,r ,p<<1|1);
pushup(p);
}
long long ask(long long l ,long long r ,long long x,long long y ,long long p) {
if (r < x || l > y) {
return 0;
}
if (l >= x && r <= y) {
return tr[p].v;
}
pushdown(p ,l ,r);
long long mid = (l+r) / 2;
return ask(l ,mid ,x ,y ,p<<1) + ask(mid+1 ,r ,x ,y ,p<<1|1);
}
void add(long long l ,long long r ,long long x,long long y ,long long p ,long long v) {
if (r < x || l > y) {
return;
}
if (l >= x && r <= y) {
tr[p].v += (r-l+1) * v;
tr[p].lazy += v;
return;
}
pushdown(p ,l ,r);
long long mid = (l + r) / 2;
add(l ,mid ,x ,y ,p*2 ,v);
add(mid+1 ,r ,x ,y ,p*2+1 ,v);
pushup(p);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (long long i = 1;i <= n;++i) {
cin >> a[i];
}
build(1 ,n ,1);
while (m--) {
long long opt ,x ,y ,z;
cin >> opt;
if (opt == 1) {
cin >> x >> y >> z;
add(1 ,n ,x ,y ,1 ,z);
}
else {
cin >> x >> y;
cout << ask(1 ,n ,x ,y ,1) << '\n';
}
}
return 0;
}
P3373 【模板】线段树 2
C++
#include <bits/stdc++.h>using namespace std;
const long long MAXN = 5e5 + 10;
long long n ,m ,a[MAXN] ,q;
struct Node {
long long p ,v ,lazy1 ,lazy2;
}tr[4*MAXN];
void pushup(long long p) {
tr[p].v = tr[p*2].v + tr[p*2+1].v;
}
void f(long long x ,long long l ,long long r ,long long k1 ,long long k2){
tr[x].v = (k1 * tr[x].v + (r-l+1) * k2 % m) % m;
(tr[x].lazy1 *= k1) %= m;
tr[x].lazy2 = (k2 + k1*tr[x].lazy2) % m;
}
void pushdown(long long p ,long long l ,long long r) {
long long ls = p<<1 ,rs = p<<1|1 ,mid = l+r>>1;
f(ls ,l, mid ,tr[p].lazy1 ,tr[p].lazy2);
f(rs ,mid+1 ,r ,tr[p].lazy1 ,tr[p].lazy2);
tr[p].lazy1 = 1 , tr[p].lazy2 = 0;
return;
}
void build(long long l ,long long r ,long long p) {
tr[p].lazy1 = 1;
if (l == r) {
tr[p].v = a[l] % m;
return;
}
long long mid = (l+r) / 2;
build(l , mid ,p<<1);
build(mid+1 ,r ,p<<1|1);
pushup(p);
}
long long ask(long long l ,long long r ,long long x,long long y ,long long p) {
if (r < x || l > y) {
return 0;
}
if (l >= x && r <= y) {
return tr[p].v;
}
pushdown(p ,l ,r);
long long mid = (l+r) / 2;
return (ask(l ,mid ,x ,y ,p<<1) + ask(mid+1 ,r ,x ,y ,p<<1|1)) % m;
}
void add(long long l ,long long r ,long long x,long long y ,long long p ,long long v) {
if (r < x || l > y) {
return;
}
if (l >= x && r <= y) {
tr[p].v += (r-l+1) * v;
tr[p].v %= m;
tr[p].lazy2 += v;
tr[p].lazy2 %= m;
return;
}
pushdown(p ,l ,r);
long long mid = (l + r) / 2;
add(l ,mid ,x ,y ,p*2 ,v);
add(mid+1 ,r ,x ,y ,p*2+1 ,v);
pushup(p);
}
void mul(long long l ,long long r ,long long x,long long y ,long long p ,long long v) {
if (r < x || l > y) {
return;
}
if (l >= x && r <= y) {
(tr[p].v *= v) %= m;
(tr[p].lazy1 *= v) %= m;
(tr[p].lazy2 *= v) %= m;
return;
}
pushdown(p ,l ,r);
long long mid = (l + r) / 2;
mul(l ,mid ,x ,y ,p*2 ,v);
mul(mid+1 ,r ,x ,y ,p*2+1 ,v);
pushup(p);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q >> m;
for (long long i = 1;i <= n;++i) {
cin >> a[i];
}
build(1 ,n ,1);
while (q--) {
long long opt ,x ,y ,z;
cin >> opt;
z %= m;
if (opt == 1) {
cin >> x >> y >> z;
mul(1 ,n ,x ,y ,1 ,z);
}
else if (opt == 2){
cin >> x >> y >> z;
add(1 ,n ,x ,y ,1 ,z);
}
else {
cin >> x >> y;
cout << ask(1 ,n ,x ,y ,1) << '\n';
}
}
return 0;
}
P3374 【模板】树状数组 1
C++
#include <bits/stdc++.h>using namespace std;
const int N = 5e5 + 10;
int lowbit(int x) {
return x & -x;
}
int n ,m ,a[N] ,tr[N];
void update(int x ,int k) {
while (x <= N) {
tr[x] += k;
x += lowbit(x);
}
}
int query(int x) {
int res = 0;
while (x) {
res += tr[x];
x -= lowbit(x);
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
update(i ,a[i]);
}
while (m--) {
int opt ,x ,y;
cin >> opt >> x >> y;
if (opt == 1) {
update(x ,y);
}
else {
cout << query(y) - query(x - 1) << '\n';
}
}
return 0;
}
P3375 【模板】KMP
C++
#include <bits/stdc++.h>using namespace std;
using ULL = unsigned long long;
const int MAXN = 1e6 + 10;
int n ,m;
ULL nxt[MAXN];
string s1 ,s2;
void build_nxt(string s) {
int j = 0 , t = nxt[0] = -1;
while (j < m) {
if (t < 0 || s[j] == s[t]) {
nxt[++j] = ++t;
}
else {
t = nxt[t];
}
}
}
void match(string s1 ,string s2) {
build_nxt(s2);
int i = 0 , j = 0;
while (i < n) {
if (j == m) {
cout << i - j + 1 << '\n';
j = nxt[m];
}
else if (j < 0 || s1[i] == s2[j]) {
i++, j++;
}
else {
j = nxt[j];
}
}
if (j == m) {
cout << i - j + 1 << '\n';
}
for (int i = 1;i <= m;++i) {
cout << nxt[i] << ' ';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> s1 >> s2;
// s1 = ' ' + s1; // s2 = ' ' + s2;
n = s1.size() ,m = s2.size();
match(s1 , s2);
return 0;
}
P3378【模板】堆
C++
#include <bits/stdc++.h>using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
int main() {
int n;
cin >> n;
while (n--) {
int opt;
cin >> opt;
if (opt == 1) {
int x;
cin >> x;
q.push(x);
}
else if (opt == 2) {
cout << q.top() << '\n';
}
else q.pop();
}
return 0;
}
P3379【模板】最近公共祖先LCA
C++
#include <bits/stdc++.h>using namespace std;
const int N = 5e5 + 10;
const int LOG = 20;
int n, m, s, f[N][LOG], dep[N];
vector<int> e[N];
void dfs(int u, int fa) {
f[u][0] = fa;
dep[u] = dep[fa] + 1;
for (int i = 1; i < LOG; i++) {
f[u][i] = f[f[u][i - 1]][i - 1];
}
for (auto v : e[u]) {
if (v != fa) {
dfs(v, u);
}
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int diff = dep[u] - dep[v];
for (int i = 0; i < LOG; i++) {
if (diff & (1 << i)) {
u = f[u][i];
}
}
if (u == v) return u;
for (int i = LOG - 1; i >= 0; i--) {
if (f[u][i] != f[v][i]) {
u = f[u][i];
v = f[v][i];
}
}
return f[u][0];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> s;
dep[0] = 0;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
dfs(s, 0);
while (m--) {
int u, v;
cin >> u >> v;
cout << lca(u, v) << '\n';
}
return 0;
}
P3644【模板】拓扑排序 / 家谱树
C++
#include <bits/stdc++.h>using namespace std;
const int N = 1010, M = 10010;
int n, m;
struct edge {
int to, nxt;
};
edge e[M];
int hd[N], ecnt, ind[N];
void add(int u, int v) {
e[++ecnt].to = v;
e[ecnt].nxt = hd[u];
hd[u] = ecnt;
}
queue<int> q;
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
int u;
while (cin >> u && u) {
add(i, u);
ind[u]++;
}
}
for (int i = 1; i <= n; ++i) {
if (!ind[i]) {
q.push(i);
}
}
vector<int> ans;
while (!q.empty()) {
int u = q.front();
q.pop();
ans.push_back(u);
for (int i = hd[u]; i; i = e[i].nxt) {
int v = e[i].to;
ind[v]--;
if (!ind[v]) q.push(v);
}
}
for (auto it : ans) {
cout << it << ' ';
}
cout << endl;
return 0;
}
P3384【模板】重链剖分 / 树链剖分
C++
#include <bits/stdc++.h>using namespace std;
const int N = 1e5 + 10;
int ecnt;
struct edge {
int to, nxt;
};
edge G[N << 1];
int hd[N], dfncnt;
int n, m, r, mod;
void addedge(int u, int v) {
G[++ecnt].to = v;
G[ecnt].nxt = hd[u];
hd[u] = ecnt;
}
int siz[N], fa[N], dep[N], son[N], top[N], dfn[N], dfn2[N];
int a[N];
void dfs1(int u, int f) {
siz[u] = 1, fa[u] = f;
dep[u] = dep[f] + 1;
for (int i = hd[u]; i; i = G[i].nxt) {
int v = G[i].to;
if (v == f) continue;
dfs1(v, u);
siz[u] += siz[v];
if (son[u] == 0 || siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int t) {
top[u] = t;
dfn[u] = ++dfncnt;
dfn2[dfncnt] = u;
if (son[u])
dfs2(son[u], t);
for (int i = hd[u]; i; i = G[i].nxt) {
int v = G[i].to;
if (v == fa[u] || v == son[u])
continue;
dfs2(v, v);
}
}
struct Node {
int v, lzy;
} tr[N << 2];
void pushup(int p) {
tr[p].v = (tr[p << 1].v + tr[p << 1 | 1].v) % mod;
}
void build(int l, int r, int p) {
if (l == r) {
tr[p].v = a[dfn2[l]];
return;
}
int mid = (l + r) / 2;
build(l, mid, p << 1);
build(mid + 1, r, p << 1 | 1);
pushup(p);
}
void pushdown(int p, int l, int r) {
int ls = p << 1 ,rs = p << 1 | 1 ,mid = l + r >> 1;
(tr[ls].v += (mid - l + 1) * tr[p].lzy) %= mod;
(tr[ls].lzy += tr[p].lzy) %= mod;
(tr[rs].v += (r - mid) * tr[p].lzy) %= mod;
(tr[rs].lzy += tr[p].lzy) %= mod;
tr[p].lzy = 0;
return;
}
int query(int l, int r, int x, int y, int p) {
if (r < x || l > y) return 0;
if (l >= x && r <= y) return tr[p].v;
int mid = l + r >> 1;
pushdown(p, l, r);
return (query(l, mid, x, y, p << 1) % mod + query(mid + 1, r, x, y, p << 1 | 1) % mod) % mod;
}
void update(int l, int r, int x, int y, int p, int val) {
if (x > r || y < l) return;
if (x <= l && r <= y) {
(tr[p].v += (r - l + 1) * val) %= mod;
(tr[p].lzy += val) %= mod;
return;
}
int mid = l + r >> 1;
pushdown(p, l, r);
update(l, mid, x, y, p << 1, val);
update(mid + 1, r, x, y, p << 1 | 1, val);
pushup(p);
}
int query_path(int x, int y) {
int res = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
(res += query(1, n, dfn[top[x]], dfn[x], 1)) %= mod;
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
(res += query(1, n, dfn[x], dfn[y], 1)) %= mod;
return res;
}
void update_path(int x, int y, int v) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
update(1, n, dfn[top[x]], dfn[x], 1, v);
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
update(1, n, dfn[x], dfn[y], 1, v);
}
int main() {
cin >> n >> m >> r >> mod;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
addedge(u, v);
addedge(v, u);
}
dfs1(r, 0);
dfs2(r, 0);
build(1, n, 1);
while (m--) {
int op, x, y, z;
cin >> op;
if (op == 1) {
cin >> x >> y >> z;
update_path(x, y, z);
}
if (op == 2) {
cin >> x >> y;
cout << query_path(x, y) % mod << endl;
}
if (op == 3) {
cin >> x >> y;
update(1, n, dfn[x], dfn[x] + siz[x] - 1, 1, y);
}
if (op == 4) {
cin >> x;
cout << query(1, n, dfn[x], dfn[x] + siz[x] - 1, 1) % mod << endl;
}
}
return 0;
}
P3385【模板】负环
C++
#include <bits/stdc++.h>using namespace std;
const int N = 2e3 + 10;
const int M = 3e3 + 10;
int n, m, Gtop;
int dis[N], hd[N], cnt[N];
bool vis[N];
struct Node {
int nxt, to, dis;
};
Node G[M * 2];
void add(int u, int v, int w) {
G[++Gtop].nxt = hd[u];
G[Gtop].to = v;
G[Gtop].dis = w;
hd[u] = Gtop;
}
bool spfa() {
queue<int> q;
memset(dis, 0x3f, sizeof dis);
memset(vis, 0, sizeof vis);
memset(cnt, 0, sizeof cnt);
dis[1] = 0;
q.push(1);
vis[1] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 0;
for (int i = hd[u]; i; i = G[i].nxt) {
int v = G[i].to;
if (dis[v] > dis[u] + G[i].dis) {
dis[v] = dis[u] + G[i].dis;
cnt[v] = cnt[u] + 1;
if (cnt[v] >= n) return 1;
if (!vis[v]) {
vis[v] = 1;
q.push(v);
}
}
}
}
return 0;
}
void solve() {
memset(hd, 0, sizeof hd);
Gtop = 0;
cin >> n >> m;
for (int i = 1, u, v, w; i <= m; ++i) {
cin >> u >> v >> w;
add(u, v, w);
if (w >= 0) add(v, u, w);
}
if (spfa()) cout << "YES\n";
else cout << "NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
P3805【模板】Manacher
C++
#include<bits/stdc++.h>using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
string s;
cin>>s;
string t="!";
for(char c:s){
t+=c;
t+="!";
}
int n=t.size();
vector<int>p(n,0);
int cent=0,maxR=0,ans=0;
for(int i=0;i<n;i++){
if(i<maxR){
int mir=2*cent-i;
p[i]=min(p[mir],maxR-i);
}
int L=i-(p[i]+1),R=i+(p[i]+1);
while(L>=0&&R<n&&t[L]==t[R]){
p[i]++,L--,R++;
}
if(i+p[i]>maxR){
cent=i,maxR=i+p[i];
}
ans=max(ans,p[i]);
}
cout<<ans<<endl;
return 0;
}
P3807【模板】卢卡斯定理 / Lucas 定理
C++
#include <bits/stdc++.h>using namespace std;
typedef long long ll;
ll n, m, p;
ll fac[100010];
ll qpow(ll a, ll b, ll p) {
ll ans = 1, bs = a;
while (b) {
if (b & 1) {
ans = (ans * bs) % p;
}
bs = (bs * bs) % p;
b >>= 1;
}
return ans;
}
ll C(ll n, ll m, ll p) {
if (m > n) return 0;
return fac[n] * qpow(fac[m], p - 2, p) % p * qpow(fac[n - m], p - 2, p) % p;
}
ll lucas(ll n, ll m, ll p) {
if (!m) return 1;
return C(n % p, m % p, p) * lucas(n / p, m /p , p) % p;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n >> m >> p;
fac[0] = 1;
for (int i = 1; i <= p; ++i) {
fac[i] = fac[i - 1] * i % p;
}
cout << lucas(n + m, n, p) << '\n';
}
return 0;
}
P3810【模板】三维偏序 / 陌上花开
C++
#include <bits/stdc++.h>using namespace std;
const int MAXN = 1e5 + 10;
const int MAXK = 2e5 + 10;
int n, k;
int freq[MAXN];
int bit[MAXK];
vector<int> used;
inline int lowbit(int x) { return x & -x; }
void add(int pos, int val) {
while (pos <= k) {
if (bit[pos] == 0) used.push_back(pos);
bit[pos] += val;
pos += lowbit(pos);
}
}
int sum(int pos) {
int res = 0;
while (pos > 0) {
res += bit[pos];
pos -= lowbit(pos);
}
return res;
}
void clearBIT() {
for (int pos : used) bit[pos] = 0;
used.clear();
}
struct Element {
int a, b, c;
} e[MAXN];
struct Node {
int a, b, c;
int cnt;
int ans;
} node[MAXN];
int m;
struct TempNode {
int b, c, cnt;
int idx;
bool isLeft;
};
bool cmpElement(const Element &x, const Element &y) {
if (x.a != y.a) return x.a < y.a;
if (x.b != y.b) return x.b < y.b;
return x.c < y.c;
}
void build() {
sort(e + 1, e + 1 + n, cmpElement);
m = 0;
for (int i = 1; i <= n; ++i) {
if (i > 1 && e[i].a == e[i-1].a && e[i].b == e[i-1].b && e[i].c == e[i-1].c) {
node[m].cnt++;
} else {
node[++m] = {e[i].a, e[i].b, e[i].c, 1, 0};
}
}
}
void cdq(int l, int r) {
if (l == r) return;
int mid = (l + r) >> 1;
cdq(l, mid);
cdq(mid + 1, r);
vector<TempNode> tmp;
for (int i = l; i <= r; ++i) {
tmp.push_back({node[i].b, node[i].c, node[i].cnt, i, i <= mid});
}
sort(tmp.begin(), tmp.end(), [](const TempNode &x, const TempNode &y) {
if (x.b != y.b) return x.b < y.b;
return x.idx < y.idx;
});
for (auto &p : tmp) {
if (p.isLeft) {
add(p.c, p.cnt);
} else {
node[p.idx].ans += sum(p.c);
}
}
clearBIT();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> e[i].a >> e[i].b >> e[i].c;
}
build();
cdq(1, m);
for (int i = 1; i <= m; ++i) {
int fval = node[i].ans + (node[i].cnt - 1);
freq[fval] += node[i].cnt;
}
for (int d = 0; d < n; ++d) {
cout << freq[d] << '\n';
}
return 0;
}
P3865【模板】ST表&RMQ问题
C++
#include <bits/stdc++.h>using namespace std;
const int MAXN = 1e5+10;
int a[MAXN] ,f[MAXN][25] ,logn[MAXN];
int n ,m;
class FastIO {
private:static const int SIZE = 1 << 20;
char in[SIZE], out[SIZE], *p1, *p2, *pp;
public:FastIO() : p1(in), p2(in), pp(out) {}
inline char getchar() {
if (p1 == p2) p2 = (p1 = in) + fread(in, 1, SIZE, stdin);
return p1 == p2 ? EOF : *p1++;
}
template<typename T>
inline void read(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch - '0');
ch = getchar();
}
x *= f;
}
inline void write_char(char c) {
if (pp - out == SIZE) {
fwrite(out, 1, SIZE, stdout);
pp = out;
}
*pp++ = c;
}
template<typename T>
inline void write(T x) {
if (x < 0) {
write_char('-');
x = -x;
}
if (x > 9) write(x / 10);
write_char(x % 10 + '0');
}
inline void write_string(const char* s) {
while (*s) {
write_char(*s++);
}
}
~FastIO() {
fwrite(out, 1, pp - out, stdout);
}
}d;
void init(){
logn[1] = 0;
logn[2] = 1;
for (int i=3;i<=MAXN;i++) {
logn[i] = logn[i / 2] + 1;
}
}
void ST_init() {
int k = logn[n];
for (int j = 1;j <= k;++j) {
for(int i = 1;i <= n - (1 << j) +1 ; ++i) {
f[i][j] = max(f[i][j-1] ,f[i + (1 << (j - 1))][j - 1]);
}
}
}
int query(int l ,int r) {
int k = logn[r-l+1];
return max(f[l][k] ,f[r - (1 << k) + 1][k]);
}
int main(){
d.read(n), d.read(m);
for (int i = 1;i <= n;++i){
d.read(a[i]);
f[i][0] = a[i];
}
init();
ST_init();
while (m--) {
int l ,r;
d.read(l) ,d.read(r);
d.write(query(l , r));
d.write_char('\n');
}
return 0;
}
P4779 【模板】单源最短路径(标准版)
C++
#include <bits/stdc++.h>#define int long longusing namespace std;
const int INF = 0x3f3f3f3f3f3f3f3f;
int n, m, s;
vector<pair<int, int>> G[500010];
int dis[500010];
bool vis[500010];
void Dijkstra() {
dis[s] = 0;
while (true) {
int mn = -1;
for (int i = 1; i <= n; ++i) {
if (!vis[i] && (mn == -1 || dis[i] < dis[mn])) {
mn = i;
}
}
if (mn == -1 || dis[mn] == INF) break;
vis[mn] = 1;
for (auto v : G[mn]) {
int to = v.first;
int w = v.second;
if (dis[to] > dis[mn] + w) {
dis[to] = dis[mn] + w;
}
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> s;
for (int i = 1; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back({v, w});
}
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
Dijkstra();
for (int i = 1; i <= n; ++i) {
if (dis[i] == INF) {
cout << INT_MAX << ' ';
} else {
cout << dis[i] << ' ';
}
}
cout << endl;
return 0;
}
P5357【模板】AC 自动机
C++
#include <bits/stdc++.h>using namespace std;
const int N = 2e5 + 10;
struct TRIE {
int tot = 0;
int tr[N][30], a[N], b[N];
int cnt[N], cnt1[N];
int get(char x) {
return x - 'a' + 1;
}
void ins(string s, int ps) {
int x = 0, n = s.size();
for (int i = 0; i < n; ++i) {
int v = get(s[i]);
if (!tr[x][v]) {
tr[x][v] = ++tot;
}
x = tr[x][v];
}
a[ps] = x;
if (!b[x]) {
b[x] = ps;
}
}
};
struct AC {
TRIE trie;
int fail[26 * N], ind[26 * N], ans[N], cnt[N];
void build_fail () {
queue<int> q;
for (int i = 0; i <= 26; ++i) {
if (trie.tr[0][i]) {
q.push(trie.tr[0][i]);
}
}
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 1; i <= 26; ++i) {
if (trie.tr[u][i]) {
q.push(trie.tr[u][i]);
fail[trie.tr[u][i]] = trie.tr[fail[u]][i];
ind[trie.tr[fail[u]][i]]++;
}
else {
trie.tr[u][i] = trie.tr[fail[u]][i];
}
}
}
}
void query (string s) {
int x = 0;
for (int i = 0; i < s.size(); ++i) {
x = trie.tr[x][trie.get(s[i])];
cnt[x]++;
}
for (int i = trie.tot; i >= 0; --i) {
cnt[fail[i]] += cnt[i];
}
}
}ac;
int n;
string s[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
ac.trie.ins(s[i], i);
}
ac.build_fail();
string s1;
cin >> s1;
ac.query(s1);
for (int i = 1; i <= n; ++i) {
cout << ac.cnt[ac.trie.a[i]] << '\n';
}
return 0;
}
P5788【模板】单调栈
C++
#include <bits/stdc++.h>using namespace std;
const int MAXN = 5e6 + 10;
int n ,a[MAXN] ,ans[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1;i <= n;++i) {
cin >> a[i];
}
stack<int> st;
for (int i = 1;i <= n;++i) {
while (!st.empty() && a[st.top()] < a[i]) {
ans[st.top()] = i;
st.pop();
}
st.push(i);
}
for (int i = 1;i <= n;++i) {
cout << ans[i] << ' ';
}
cout << '\n';
return 0;
}
P6091【模板】原根
C++
#include <bits/stdc++.h>using namespace std;
const int MAXN = 1e6 + 5;
vector<int> primes;
bool is_prime[MAXN];
int phi[MAXN];
bool has_primitive_root[MAXN];
void sieve() {
fill(is_prime, is_prime + MAXN, true);
is_prime[0] = is_prime[1] = false;
phi[1] = 1;
for (int i = 2; i < MAXN; i++) {
if (is_prime[i]) {
primes.push_back(i);
phi[i] = i - 1;
}
for (int p : primes) {
if (i * p >= MAXN) break;
is_prime[i * p] = false;
if (i % p == 0) {
phi[i * p] = phi[i] * p;
break;
} else {
phi[i * p] = phi[i] * (p - 1);
}
}
}
fill(has_primitive_root, has_primitive_root + MAXN, false);
has_primitive_root[2] = has_primitive_root[4] = true;
for (int p : primes) {
if (p == 2) continue;
long long pk = p;
while (pk < MAXN) {
has_primitive_root[pk] = true;
if (pk * 2 < MAXN) {
has_primitive_root[pk * 2] = true;
}
pk *= p;
}
}
}
long long pow_mod(long long a, long long b, long long mod) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
vector<int> get_factors(int n) {
vector<int> factors;
int temp = n;
for (int p : primes) {
if (p * p > temp) break;
if (temp % p == 0) {
factors.push_back(p);
while (temp % p == 0) temp /= p;
}
}
if (temp > 1) factors.push_back(temp);
return factors;
}
bool is_primitive_root(int g, int n, int phi_n, const vector<int>& factors) {
if (__gcd(g, n) != 1) return false;
for (int p : factors) {
if (pow_mod(g, phi_n / p, n) == 1) {
return false;
}
}
return true;
}
int find_min_primitive_root(int n) {
int phi_n = phi[n];
vector<int> factors = get_factors(phi_n);
for (int g = 1; g < n; g++) {
if (is_primitive_root(g, n, phi_n, factors)) {
return g;
}
}
return -1;
}
vector<int> find_all_primitive_roots(int n) {
vector<int> roots;
if (!has_primitive_root[n]) {
return roots;
}
if (n == 2) {
roots.push_back(1);
return roots;
}
if (n == 4) {
roots.push_back(3);
return roots;
}
int g = find_min_primitive_root(n);
if (g == -1) return roots;
int phi_n = phi[n];
int cnt = phi[phi_n];
roots.reserve(cnt);
long long current = g;
for (int k = 1; k <= phi_n; k++) {
if (__gcd(k, phi_n) == 1) {
roots.push_back(current);
}
current = current * g % n;
}
sort(roots.begin(), roots.end());
return roots;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
sieve();
int T;
cin >> T;
while (T--) {
int n, d;
cin >> n >> d;
vector<int> roots = find_all_primitive_roots(n);
int c = roots.size();
cout << c << "\n";
if (c == 0) {
cout << "\n";
continue;
}
vector<int> output;
for (int i = d - 1; i < c; i += d) {
output.push_back(roots[i]);
}
for (int i = 0; i < (int)output.size(); i++) {
cout << output[i];
if (i < (int)output.size() - 1) cout << " ";
}
cout << "\n";
}
return 0;
}
P8306【模板】字典树
C++
#include <bits/stdc++.h>#define int long longusing namespace std;
const int N = 3e6 + 10;
const int M = 63;
int trie[N][M], cnt[N], tot;
int get(char x) {
if (x >= 'A' && x <= 'Z') {
return x - 'A';
}
else if (x >= 'a' && x <= 'z') {
return x - 'a' + 26;
}
else {
return x - '0' + 52;
}
}
void ins(string s) {
int x = 0, n = s.size();
for (int i = 0; i < n; ++i) {
int v = get(s[i]);
if (!trie[x][v]) {
trie[x][v] = ++tot;
}
x = trie[x][v];
cnt[x]++;
}
}
int fnd(string s) {
int x = 0, n = s.size();
for (int i = 0; i < n; ++i) {
int v = get(s[i]);
if (!trie[x][v]) {
return 0;
}
x = trie[x][v];
}
return cnt[x];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
for (cin >> T; T; --T) {
int n, q;
cin >> n >> q;
for (int i = 0; i <= tot; ++i) {
memset(trie[i], 0, sizeof trie[i]);
cnt[i] = 0;
}
tot = 0;
for (int i = 1; i <= n; ++i) {
string s;
cin >> s;
ins(s);
}
while (q--) {
string s;
cin >> s;
cout << fnd(s) << '\n';
}
}
return 0;
}
P10815【模板】快速读入
C++
#include <bits/stdc++.h>using namespace std;
inline int read() {
int ret = 0 , f = 0;
char ch = getchar_unlocked();
while (!isdigit(ch)) {
f |= ch == '-' ,ch = getchar_unlocked();
}
while (isdigit(ch)) {
ret = ret * 10 + ch - '0' ,ch = getchar_unlocked();
}
return f ? -ret : ret;
}
int main() {
int n ,res = 0;
for (n = read() ;n ;n--) {
res += read();
}
cout << res << '\n';
}
P11615【模板】哈希表
C++
#include <bits/stdc++.h>using namespace std;
using ull = unsigned long long;
char buf[1<<23], *p1 = buf, *p2 = buf;
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<21, stdin), p1 == p2) ? EOF : *p1++)
inline ull read_ull() {
ull x = 0;
char ch = gc();
while (!isdigit(ch)) ch = gc();
while (isdigit(ch)) {
x = x * 10 + (ch ^ 48);
ch = gc();
}
return x;
}
const int SIZE = 6e6 + 10;
const ull EMPTY = 0xFFFFFFFFFFFFFFFFULL;
struct Hash {
ull key;
ull value;
bool used;
};
class Map {
private:
Hash* table;
int capacity;
inline int hash(ull key) const {
const ull multiplier = 11400714819323198485ULL;
return (key * multiplier) % capacity;
}
public:Map(int size) : capacity(size) {
table = new Hash[size];
for (int i = 0; i < size; ++i) {
table[i].key = EMPTY;
table[i].used = false;
table[i].value = 0;
}
}
~Map() {
delete[] table;
}
ull query(ull key, ull new_) {
int idx = hash(key);
int start_idx = idx;
while (true) {
if (!table[idx].used && table[idx].key == EMPTY) {
table[idx].key = key;
table[idx].value = new_;
table[idx].used = true;
return 0;
}
if (table[idx].used && table[idx].key == key) {
ull old_ = table[idx].value;
table[idx].value = new_;
return old_;
}
idx = (idx + 1) % capacity;
if (idx == start_idx) {
return 0;
}
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = read_ull();
Map hashmap(SIZE);
ull sum = 0;
for (int i = 1; i <= n; ++i) {
ull x = read_ull();
ull y = read_ull();
ull ans = hashmap.query(x, y);
sum += (ull)i * ans;
}
cout << sum << endl;
return 0;
}
P13825【模板】线段树1.5
C++
#include <bits/stdc++.h>using namespace std;
const unsigned long long MAXN = 2e5 + 10;
unsigned long long n ,m ,tot = 0 ,rt = 0;
struct Node {
unsigned long long ls , rs ,v ,lazy;
}tr[40*MAXN];
unsigned long long calc(unsigned long long l ,unsigned long long r) {
return (l+r) * (r-l+1) / 2;
}
void pushup(unsigned long long p) {
tr[p].v = tr[tr[p].ls].v + tr[tr[p].rs].v;
return;
}
void pushdown(unsigned long long p ,unsigned long long l ,unsigned long long r) {
if (tr[p].lazy== 0) {
return;
}
unsigned long long &ls = tr[p].ls ,&rs = tr[p].rs ,mid = l+r>>1;
if (!ls) ls = ++tot;
if (!rs) rs = ++tot;
tr[ls].v += 1ll * (mid-l+1) * tr[p].lazy;
tr[ls].lazy += tr[p].lazy;
tr[rs].v += 1ll * (r-mid) * tr[p].lazy;
tr[rs].lazy += tr[p].lazy;
tr[p].lazy = 0;
return;
}
unsigned long long ask(unsigned long long l ,unsigned long long r ,unsigned long long x,unsigned long long y ,unsigned long long p) {
if (l >= x && r <= y) {
return tr[p].v;
}
if (r < x || l > y) {
return 0;
}
pushdown(p ,l ,r);
unsigned long long mid = (l+r) / 2;
return ask(l ,mid ,x ,y ,tr[p].ls) + ask(mid+1 ,r ,x ,y ,tr[p].rs);
}
void add(unsigned long long l ,unsigned long long r ,unsigned long long x,unsigned long long y ,unsigned long long v ,unsigned long long &p){
if (p == 0) {
p = ++tot;
}
if (r < x || l > y) {
return;
}
if (l >= x && r <= y) {
tr[p].v += 1ll * (r-l+1) * v;
tr[p].lazy += v;
return;
}
pushdown(p ,l ,r);
unsigned long long mid = (l + r)>>1;
add(l ,mid ,x ,y ,v ,tr[p].ls);
add(mid+1 ,r ,x ,y ,v ,tr[p].rs);
pushup(p);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
while (m--) {
unsigned long long opt ,x ,y ,z;
cin >> opt;
if (opt == 1) {
cin >> x >> y >> z;
add(1 ,n ,x ,y ,z ,rt);
}
else {
cin >> x >> y;
cout << ask(1 ,n ,x ,y ,rt) + calc(x , y) << '\n';
}
}
return 0;
}