P3377 【模板】左偏树(可并堆)
C++#include<bits/stdc++.h>
#define l(p) tr[p].lt
#define r(p) tr[p].rt
#define val(p) tr[p].val
#define dst(p) tr[p].dist
#define fa(p) tr[p].fa
using namespace std;
const int Maxn = 1e5+100;
struct teap{int fa, lt, rt, val, dist;}tr[Maxn];
int n, m;
int find(int x){return fa(x) == x ? x :fa(x) = find(fa(x));}
int merge(int x,int y){
if(!x | !y) return x | y;
if(val(x) > val(y)) swap(x, y);
r(x) = merge(r(x), y);
if(dst(l(x)) < dst(r(x))) swap(l(x), r(x));
dst(x) = dst(r(x)) + 1;
fa(l(x)) = fa(r(x)) = fa(x) = x;
return x;
}
int pop(int x){val(x) = -1;fa(l(x)) = l(x),fa(r(x)) = r(x);fa(x) = merge(l(x), r(x));return 0;}
int main(){
scanf("%d%d",& n,& m);
for(int i = 1;i <= n;++ i)
fa(i) = i,scanf("%d", & val(i));
int t, x, y;
for(int i = 1;i <= m;++ i){
scanf("%d%d",& t,& x);
if(t == 1){
scanf("%d",& y);
if(val(x) == -1 || val(y) == -1) continue;
int l = find(x),r = find(y);
if(l != r) fa(l) = fa(r) = merge(l, r);
}
else{
if(val(x) == -1) printf("-1\n");
else printf("%d\n",val(find(x))),pop(find(x));
}
}
return 0;
}
P2713 罗马游戏
C++#include<bits/stdc++.h>
#define l(p) tr[p].lt
#define r(p) tr[p].rt
#define val(p) tr[p].val
#define fa(p) tr[p].fa
#define dis(p) tr[p].dist
using namespace std;
const int Maxn = 1e6+10;
struct node{int lt, rt, val, fa, dist;}tr[Maxn];
int n, m;
int find(int x) {return fa(x) == x ? x : fa(x) = find(fa(x));}
int merge(int x,int y){
if(!x || !y) return x | y;
if(val(x) > val(y)) swap(x, y);
r(x) = merge(r(x), y);
if(dis(l(x)) < dis(r(x))) swap(l(x), r(x));
dis(x) = dis(r(x)) + 1;
fa(l(x)) = fa(r(x)) = fa(x) = x;
return x;
}
void pop(int x){val(x) = -1;fa(l(x)) = l(x),fa(r(x)) = r(x);fa(x) = merge(l(x), r(x));}
int main(){
scanf("%d",& n);
for(int i = 1;i <= n;++ i)
fa(i) = i,scanf("%d",& val(i));
scanf("%d",& m);
int x, y;char c;
for(int i = 1;i <= m;++ i){
c = getchar();
while(c != 'M' && c != 'K') c = getchar();
if(c == 'M'){
scanf("%d%d",& x,& y);
if(val(x) == -1 || val(y) == -1) continue;
int l = find(x),r = find(y);
if(l != r) fa(l) = fa(r) = merge(l, r);
}
else{
scanf("%d",& x);
if(val(x) == -1) printf("0\n");
else printf("%d\n",val(find(x))),pop(find(x));
}
}
return 0;
}
P1456 Monkey King
C++#include<bits/stdc++.h>
#define l(p) tr[p].lt
#define r(p) tr[p].rt
#define val(p) tr[p].val
#define dis(p) tr[p].dist
#define fa(p) tr[p].fa
using namespace std;
const int Maxn = 1e5+100;
struct node{int lt, rt, val, dist, fa;void clear(){lt=rt=val=dist=fa=0;}}tr[Maxn];
int n, m, x, y;
int find(int x){return fa(x) == x ? x : fa(x) = find(fa(x));}
int merge(int x,int y){
if(!x || !y) return x | y;
if(val(x) < val(y)) swap(x, y);
r(x) = merge(r(x), y);
if(dis(l(x)) < dis(r(x))) swap(l(x), r(x));
dis(x) = dis(r(x)) + 1;
fa(x) = fa(l(x)) = fa(r(x)) = x;
return x;
}
int pop(int x){
val(x) = -1;
fa(l(x)) = l(x),fa(r(x)) = r(x);fa(x) = merge(l(x), r(x));
return x;
}
int weak(int x){
val(x) >>= 1;
int cnt = merge(l(x), r(x));
l(x) = r(x) = dis(x) = 0;
return fa(cnt) = fa(x) = merge(cnt, x);
}
void fight(int x,int y){
if(find(x) == find(y)) {printf("-1\n");return;}
int l = weak(find(x)),r = weak(find(y));
fa(l) = fa(r) = merge(l, r);
printf("%d\n", val(find(l)));
}
int main(){
while(~scanf("%d",& n)){
for(int i = 1;i <= n;++ i)
fa(i) = i,scanf("%d",& val(i));
scanf("%d",& m);
while(m --){
scanf("%d%d",& x,& y);
if(find(x) == find(y)) printf("-1\n");
else fight(x, y);
}
for(int i = 1;i <= n;++ i) tr[i].clear();
}
return 0;
}
P3261 [JLOI2015]城池攻占
C++#include<bits/stdc++.h>
#define int long long
#define l(p) tr[p].lt
#define r(p) tr[p].rt
#define dst(p) tr[p].dist
#define val(p) tr[p].val
#define add(p) tr[p].add
#define mul(p) tr[p].mul
#define h(i) c[i].h
#define f(i) c[i].f
#define a(i) c[i].a
#define v(i) c[i].v
#define d(i) c[i].die
#define cit(p) tr[p].cit
#define die(p) tr[p].die
using namespace std;
const int Maxn = 3e5+10;
struct node{int lt, rt, val, add, mul, dist, cit, die;node(){mul=1;}}tr[Maxn];
struct city{int h, f, a, v, die;}c[Maxn];
int n, m, root[Maxn], dep[Maxn], ans[Maxn];
void pushDown(int x){
if(add(x) == 0 && mul(x) == 1) return;
if(l(x)){
val(l(x)) *= mul(x);
val(l(x)) += add(x);
mul(l(x)) *= mul(x);
add(l(x)) *= mul(x);
add(l(x)) += add(x);
}
if(r(x)){
val(r(x)) *= mul(x);
val(r(x)) += add(x);
mul(r(x)) *= mul(x);
add(r(x)) *= mul(x);
add(r(x)) += add(x);
}
add(x) = 0,mul(x) = 1;
}
int merge(int x,int y){
if(!x || !y) return x | y;
if(val(x) > val(y)) swap(x, y);
pushDown(x),pushDown(y);
r(x) = merge(r(x), y);
if(dst(l(x)) < dst(r(x))) swap(l(x), r(x));
dst(x) = dst(r(x)) + 1;
return x;
}
int pop(int x){return merge(l(x),r(x));}
int read(){
int x = 0;bool f = 0;char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = 1;c = getchar();}
while(c >= '0' && c <= '9') {x = (x << 3) + (x << 1) + c - '0';c = getchar();}
return f ? -x : x;
}
signed main(){
n = read(),m = read();
memset(root, -1, sizeof(root));
for(int i = 1;i <= n;++ i) h(i) = read();
dep[1] = 1;dst(0) = -1;
for(int i = 2;i <= n;++ i)
f(i) = read(),a(i) = read(),v(i) = read(),
dep[i] = dep[f(i)] + 1;
for(int i = 1;i <= m;++ i){
val(i) = read(),cit(i) = read(),mul(i) = 1;
if(root[cit(i)] == -1) root[cit(i)] = i;
else root[cit(i)] = merge(root[cit(i)], i);
}
for(int x = n;x >= 1;-- x){
while(root[x] != -1 && h(x) > val(root[x])){
die(root[x]) = x;++ d(x);
pushDown(root[x]);
if(!l(root[x])) root[x] = -1;
else root[x] = pop(root[x]);
}
if(x == 1) break;;
if(root[x] != -1){
if(a(x)) mul(root[x]) *= v(x),add(root[x]) *= v(x),val(root[x]) *= v(x);
else val(root[x]) += v(x),add(root[x]) += v(x);
pushDown(root[x]);
if(root[f(x)] == -1) root[f(x)] = root[x];
else root[f(x)] = merge(root[f(x)], root[x]);
}
}
for(int i = 1;i <= n;++ i)
printf("%lld\n", d(i));
for(int i = 1;i <= m;++ i)
printf("%lld\n", dep[cit(i)] - dep[die(i)]);
return 0;
}
P1552 [APIO2012] 派遣
C++#include<bits/stdc++.h>
#define val(i) tr[i].val
#define fa(i) tr[i].fa
#define lis(i) tr[i].lis
#define l(i) tr[i].lt
#define r(i) tr[i].rt
#define dist(i) tr[i].dist
#define int long long
using namespace std;
const int Maxn = 1e5+100;
int n, M, root[Maxn], sum[Maxn], ans, siz[Maxn];
struct node{int dist, val, fa, lis, lt, rt;}tr[Maxn];
int merge(int x,int y){
if(!x || !y) return x | y;
if(val(x) < val(y)) swap(x, y);
r(x) = merge(r(x), y);
if(dist(l(x)) < dist(r(x))) swap(l(x), r(x));
dist(x) = dist(r(x)) + 1;
return x;
}
int pop(int x){return merge(l(x), r(x));}
signed main(){
scanf("%lld%lld",& n,& M);
for(int i = 1;i <= n;++ i)
scanf("%lld%lld%lld",& fa(i),& val(i),& lis(i));
for(int i = 1;i <= n;++ i)
root[i] = i,sum[i] = val(i),siz[i] = 1;
for(int i = n;i >= 1;-- i){
while(sum[i] > M){
sum[i] -= val(root[i]);-- siz[i];
root[i] = pop(root[i]);
if(!siz[i]){root[i] = -1;break;}
}
if(root[i] != -1){
ans = max(ans, lis(i) * siz[i]);
if(i == 1) continue;
sum[fa(i)] += sum[i];siz[fa(i)] += siz[i];
root[fa(i)] = merge(root[fa(i)], root[i]);
}
}
printf("%lld\n", ans);
return 0;
}
P3273 [SCOI2011]棘手的操作
C++#include<bits/stdc++.h>
#define l(p) tr[p].lt
#define r(p) tr[p].rt
#define val(p) tr[p].val
#define dist(p) tr[p].dist
#define fa(p) tr[p].fa
#define add(p) tr[p].add
using namespace std;
const int Maxn = 3e5+10;
string s;
int n, Q, lzy;
int root;
struct node{int lt,rt,val,dist,fa,add;};
struct left{
node tr[Maxn];
int find(int x){return !fa(x) ? x : find(fa(x));}
void pushDown(int x){
if(!add(x)) return;
if(l(x)){
val(l(x)) += add(x);
add(l(x)) += add(x);
}
if(r(x)){
val(r(x)) += add(x);
add(r(x)) += add(x);
}
add(x) = 0;
}
int merge(int x,int y){
if(!x || !y) return x | y;
if(val(x) < val(y)) swap(x, y);
pushDown(x),pushDown(y);
r(x) = merge(r(x), y);fa(r(x)) = x;
if(dist(l(x)) < dist(r(x))) swap(l(x), r(x));
dist(x) = dist(r(x)) + 1;
return x;
}
int pop(int x){
pushDown(x);
int fx = fa(x),rt = merge(l(x),r(x));
fa(rt) = fx;
if(fx){
if(x == l(fx)) l(fx) = rt;
else r(fx) = rt;
}
while(fx){
if(dist(l(fx)) < dist(r(fx))) swap(l(fx), r(fx));
if(dist(fx) == dist(r(fx)) + 1) return root;
dist(fx) = dist(r(fx)) + 1;
rt = fx;
fx = fa(fx);
}
return rt;
}
int build(){
queue <int> q;
for(int i = 1;i <= n;++ i) q.push(i);
while(q.size() > 1){
int x = q.front();q.pop();
int y = q.front();q.pop();
q.push(merge(x, y));
}
return q.front();
}
int lzy(int x){
int sum = 0;
while(x = fa(x))
sum += add(x);
return sum;
}
void clear(int x){
l(x) = r(x) = fa(x) = 0;
}
int point(int x,int y){
int fx = find(x);
if(fx == x){
if(l(x) || r(x)){
if(l(x)) fx = l(x);
else fx = r(x);
}
else {val(x) += y;return x;}
}
pop(x);
val(x) += y + lzy(x);
clear(x);
return merge(find(fx), x);
}
}T,H;
int read(){
int x = 0;bool f = 0;char c = getchar();
while(c < '0' || c > '9'){if(c == '-')f = 1;c = getchar();}
while(c >= '0' && c <= '9'){x = (x << 3) + (x << 1) + c - '0';c = getchar();}
return f ? -x : x;
}
int main(){
n = read();T.tr[0].dist = H.tr[0].dist = -1;
for(int i = 1;i <= n;++ i) T.tr[i].val = H.tr[i].val = read();
root = H.build();
int x, y;
Q = read();
while(Q --){
cin >> s;
if(s == "U") {
x = read();y = read();
int fx = T.find(x),fy = T.find(y);
if(fx == fy) continue;
int tmp = T.merge(fx,fy);
if(tmp == fx) root = H.pop(fy);
else root = H.pop(fx);
}
else if(s == "A1"){
x = read();y = read();
root = H.pop(T.find(x));
int tmp = T.point(x, y);
H.tr[tmp].val = T.tr[tmp].val;
H.clear(tmp);
root = H.merge(root, tmp);
}
else if(s == "A2"){
x = read();y = read();
int fx = T.find(x);
root = H.pop(fx);
T.tr[fx].val += y,T.tr[fx].add += y;
H.tr[fx].val = T.tr[fx].val;
H.clear(fx);
root = H.merge(root, fx);
}
else if(s == "A3"){
x = read();
lzy += x;
}
else if(s == "F1"){
x = read();
printf("%d\n", T.tr[x].val + lzy + T.lzy(x));
}
else if(s == "F2"){
x = read();
printf("%d\n", T.tr[T.find(x)].val + lzy);
}
else{
printf("%d\n", H.tr[root].val + lzy);
}
}
return 0;
}