堆(heap)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e6 + 10;
struct Heap {
int a[N], size;
void clear() {
memset(a, 0, sizeof(a));
size = 0;
}
void push(int x) {
a[++size] = x;
for(int son = size, fa = son>>1; fa; son = fa, fa = son>>1) {
if(a[son] < a[fa]) swap(a[son], a[fa]);
else break;
}
}
int top() {
return a[1];
}
void pop() {
a[1] = a[size--];
for(int fa = 1, son = fa<<1; son <= size; fa = son, son = fa<<1) {
if(son < size && a[son] > a[son+1]) son++;
if(a[son] < a[fa]) swap(a[son], a[fa]);
else break;
}
}
}heap;
int main() {
int n, op, x;
scanf("%d", &n);
heap.clear();
for(int i = 1; i <= n; ++i) {
scanf("%d", &op);
if(op == 1) {
scanf("%d", &x);
heap.push(x);
} else if(op == 2) {
printf("%d\n", heap.top());
} else if(op == 3) {
heap.pop();
}
}
return 0;
}
并查集
#include<cstdio>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N = 1e4 + 10;
int n, m, op, x, y, f[N];
int find(int x) {
int r = x;
while(r != f[r]) r = f[r];
int i = x, j;
while(f[i] != r) {
j = f[i];
f[i] = r;
i = j;
}
return r;
}
void merge(int x, int y) {
if(rand()%2) f[x] = y;
else f[y] = x;
}
int main() {
srand(time(NULL));
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) f[i] = i;
for(int i = 1; i <= m; ++i) {
scanf("%d%d%d", &op, &x, &y);
int fx = find(x), fy = find(y);
if(op == 1) {
if(fx != fy) merge(fx, fy);
} else if(op == 2) {
if(fx == fy) printf("Y\n");
else printf("N\n");
}
}
return 0;
}
st表(RMQ问题)
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 1e5 + 10;
int n, m, a[N], l, r, d[N][32];
void pre() {
for(int i = 1; i <= 30; ++i)
for(int j = 1; j+(1<<(i-1)) <= n; ++j)
d[j][i] = max(d[j][i-1], d[j+(1<<(i-1))][i-1]);
}
int RMQ(int l, int r) {
int k = log(r-l+1)/log(2);
return max(d[l][k], d[r-(1<<k)+1][k]);
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%d", &d[i][0]);
pre();
for(int i = 1; i <= m; ++i) {
scanf("%d%d", &l, &r);
printf("%d\n", RMQ(l, r));
}
return 0;
}
差分数组(多次修改,一次查询)(借教室
#include<cstdio>
#include<cstring>
const int N = 1e6 + 10;
long long sup[N], d[N];
int needs[N], s[N], t[N];
int n, m;
bool judge(int x) {
memset(needs, 0, sizeof(needs));
for(int i = 1; i <= x; ++i) {
needs[s[i]] += d[i];
needs[t[i]+1] -= d[i];
}
for(int i = 1; i <= n; ++i) {
needs[i] += needs[i-1];
if(needs[i] > sup[i]) {
return false;
}
}
return true;
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf("%d", &sup[i]);
}
for(int i = 1; i <= m; ++i) {
scanf("%d%d%d", &d[i], &s[i], &t[i]);
}
int l = 0, r = m, mid, ans = m;
while(l <= r) {
mid = (l+r)>>1;
if(judge(mid)) {
ans = mid;
l = mid+1;
} else r = mid-1;
}
if(ans != m) printf("-1\n%d\n", ans+1);
else printf("0\n");
return 0;
}
线段树(区间修改,区间查询)
#include<cstdio>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
const int N = 1e5 + 10;
int n, m, op, l, r;
ll sum[N<<2], col[N<<2], a[N], val;
void update(int rt) {
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l, int r, int rt) {
col[rt] = 0;
if(l == r) {
sum[rt] = a[l];
return;
}
int mid = (l+r)>>1;
build(lson); build(rson);
update(rt);
}
void color(int l, int r, int rt, int val) {
col[rt] += val;
sum[rt] += val*(r-l+1);
}
void push_col(int l, int r, int rt) {
if(col[rt]) {
int mid = (l+r)>>1;
color(lson, col[rt]);
color(rson, col[rt]);
col[rt] = 0;
}
}
void modify(int l, int r, int rt, int nowl, int nowr, ll val) {
if(nowl <= l && r <= nowr) {
color(l, r, rt, val);
return;
}
push_col(l, r, rt);
int mid = (l+r)>>1;
if(nowl <= mid) modify(lson, nowl, nowr, val);
if(mid < nowr) modify(rson, nowl, nowr, val);
update(rt);
}
ll query(int l, int r, int rt, int nowl, int nowr) {
if(nowl <= l && r <= nowr) {
return sum[rt];
}
push_col(l, r, rt);
int mid = (l+r)>>1;
ll ans = 0;
if(nowl <= mid) ans += query(lson, nowl, nowr);
if(mid < nowr) ans += query(rson, nowl, nowr);
return ans;
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
}
build(1, n, 1);
for(int i = 1; i <= m; ++i) {
scanf("%d%d%d", &op, &l, &r);
if(op == 1) {
scanf("%lld", &val);
modify(1, n, 1, l, r, val);
} else if(op == 2) {
printf("%lld\n", query(1, n, 1, l, r));
}
}
return 0;
}
树状数组(单点修改,区间查询)
const int N = 500000 + 10;
int z[N], n, m, x, y, op;
void modify(int pos, int val) {
for(; pos <= n; pos += lowbit(pos)) z[pos] += val;
}
int query(int l, int r) {
int ans = 0; l--;
for(; r; r -= lowbit(r)) ans += z[r];
for(; l; l -= lowbit(l)) ans -= z[l];
return ans;
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf("%d", &x);
modify(i, x);
}
for(int i = 1; i <= m; ++i) {
scanf("%d%d%d", &op, &x, &y);
if(op == 1) {
modify(x, y);
} else if(op == 2) {
printf("%d\n", query(x, y));
}
}
return 0;
}