HDU 1166 敌兵布阵
PS:线段树维护区间和
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pushup(rt) t[rt] = t[rt<<1] + t[rt<<1|1];
const int maxn = 100000+10;
ll t[maxn<<2],lazy[maxn<<2],a[maxn];
void build(int l,int r,int rt) {
lazy[rt] = 0;
if(l==r) {
scanf("%lld",&t[rt]);
return ;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void pushdown(int l,int r,int rt) {
if(lazy[rt]) {
lazy[rt<<1] += lazy[rt];
lazy[rt<<1|1] += lazy[rt];
t[rt<<1] += l*lazy[rt];
t[rt<<1|1] += r*lazy[rt];
lazy[rt] = 0;
}
}
void update(int L,int R,int C,int l,int r,int rt) {
if(L<=l&&r<=R) {
t[rt] += (r-l+1)*C;
lazy[rt] += C;
return ;
}
int m = (l+r)>>1;
pushdown(m-l+1,r-m,rt);
if(L<=m) update(L,R,C,lson);
if(R>m) update(L,R,C,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt) {
if(L<=l&&r<=R) return t[rt];
int m = (l+r)>>1;
pushdown(m-l+1,r-m,rt);
ll ans = 0;
if(L<=m) ans += query(L,R,lson);
if(R>m) ans += query(L,R,rson);
return ans;
}
int main(){
int t=1, T, n;
char str[10];
scanf("%d", &T);
while(T--){
scanf("%d", &n);
build(1,n,1);
int l, r;
printf("Case %d:\n", t++);
while(scanf("%s", &str)){
if(str[0]=='E') break;
scanf("%d %d", &l, &r);
if(str[0]=='A') update(l,l,r,1,n,1);
if(str[0]=='S') update(l,l,-r,1,n,1);
if(str[0]=='Q') printf("%d\n", query(l,r,1,n,1));
}
}
}
HDU 1754 I Hate It
PS:线段树维护区间最大值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pushup(rt) t[rt] = max(t[rt<<1],t[rt<<1|1]);
const int maxn = 200000+10;
ll t[maxn<<2],lazy[maxn<<2],a[maxn];
void build(int l,int r,int rt) {
lazy[rt] = 0;
if(l==r) {
scanf("%lld",&t[rt]);
return ;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void pushdown(int l,int r,int rt) {
if(lazy[rt]) {
lazy[rt<<1] += lazy[rt];
lazy[rt<<1|1] += lazy[rt];
t[rt<<1] += l*lazy[rt];
t[rt<<1|1] += r*lazy[rt];
lazy[rt] = 0;
}
}
void update(int L,int R,int C,int l,int r,int rt) {
if(L<=l&&r<=R) {
t[rt] = C;
return ;
}
int m = (l+r)>>1;
//pushdown(m-l+1,r-m,rt);
if(L<=m) update(L,R,C,lson);
if(R>m) update(L,R,C,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt) {
if(L<=l&&r<=R) return t[rt];
int m = (l+r)>>1;
//pushdown(m-l+1,r-m,rt);
ll ans = 0;
if(L<=m) ans = max(ans, query(L,R,lson));
if(R>m) ans = max(ans, query(L,R,rson));
return ans;
}
int main(){
int n,op;
char str[10];
while(~scanf("%d", &n)){
scanf("%d", &op);
build(1,n,1);
int l, r;
while(op--){
scanf("%s", &str);
scanf("%d %d", &l, &r);
if(str[0]=='U') update(l,l,r,1,n,1);
if(str[0]=='Q') printf("%d\n", query(l,r,1,n,1));
}
}
}
HDU 1394 Minimum Inversion Number
大意:
0
0
0 ~
n
−
1
n-1
n−1组成的序列,每次将队首的数字移到队尾,求形成的
n
n
n个序列中最小逆序对数目
PS:线段树求逆序对数目、维护区间和
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pushup(rt) t[rt] = t[rt<<1] + t[rt<<1|1];
const int maxn = 100000+10;
ll t[maxn<<2],lazy[maxn<<2],a[maxn];
void build(int l,int r,int rt) {
lazy[rt] = 0;
if(l==r) {
t[rt] = 0;
return ;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int L,int R,int C,int l,int r,int rt) {
if(L<=l&&r<=R) {
t[rt]++;
return ;
}
int m = (l+r)>>1;
if(L<=m) update(L,R,C,lson);
if(R>m) update(L,R,C,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt) {
if(L<=l&&r<=R) return t[rt];
int m = (l+r)>>1;
ll ans = 0;
if(L<=m) ans += query(L,R,lson);
if(R>m) ans += query(L,R,rson);
return ans;
}
int main(){
int n;
while(~scanf("%d", &n)){
build(0,n-1,1);
int sum=0, ans=INT_MAX;
for(int i=0; i<n; i++){
scanf("%d", &a[i]);
sum += query(a[i],n-1,0,n-1,1);
update(a[i],a[i],1,0,n-1,1);
}
for(int i=0; i<n; i++){
sum = sum - a[i] + (n-a[i]-1);
ans = min(ans, sum);
}
printf("%d\n", ans);
}
}
HDU 2795 Billboard
大意:hw的公告牌,每次放进一个1w_i的公告,尽量往上,同一高度尽量靠左,求第n个公告的行数
PS:线段树维护区间最大值,叶子结点为该行的剩余空间
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pushup(rt) t[rt] = max(t[rt<<1],t[rt<<1|1]);
const int maxn = 200000+10;
ll t[maxn<<2],lazy[maxn<<2],a[maxn];
void build(int l,int r,int rt,int w) {
if(l==r) {
t[rt]=w;
return ;
}
int m = (l+r)>>1;
build(lson,w);
build(rson,w);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt,int wi) {
if(l==r){
t[rt] -= wi;
return l;
}
int m = (l+r)>>1;
ll ans = 0;
if(L<=t[rt*2]) ans = query(L,R,lson,wi);
else ans = query(L,R,rson,wi);
pushup(rt);
return ans;
}
int main(){
int h,w,n,wi;
while(~scanf("%d%d%d", &h, &w, &n)){
h = min(h, n);
build(1,h,1,w);
while(n--){
scanf("%d", &wi);
if(t[1]<wi) printf("-1\n");
else printf("%d\n", query(wi,wi,1,h,1,wi));
}
}
}
练习
POJ 2828 Buy Tickets
P
O
J
POJ
POJ
2828
2828
2828题解:这里~~
POJ 2886 Who Gets the Most Candies?
P O J POJ POJ 2886 2886 2886题解:这里~~
区间更新
POJ 3468 A Simple Problem with Integers
PS:区间修改的模板题
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pushup(rt) t[rt] = t[rt<<1] + t[rt<<1|1];
const int maxn = 100000+10;
ll t[maxn<<2],lazy[maxn<<2],a[maxn];
void build(int l,int r,int rt) {
lazy[rt] = 0;
if(l==r) {
scanf("%lld",&t[rt]);
return ;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void pushdown(int l,int r,int rt) {
if(lazy[rt]) {
lazy[rt<<1] += lazy[rt];
lazy[rt<<1|1] += lazy[rt];
t[rt<<1] += l*lazy[rt];
t[rt<<1|1] += r*lazy[rt];
lazy[rt] = 0;
}
}
void update(int L,int R,int C,int l,int r,int rt) {
if(L<=l&&r<=R) {
t[rt] += (r-l+1)*C;
lazy[rt] += C;
return ;
}
int m = (l+r)>>1;
pushdown(m-l+1,r-m,rt);
if(L<=m) update(L,R,C,lson);
if(R>m) update(L,R,C,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt) {
if(L<=l&&r<=R) return t[rt];
int m = (l+r)>>1;
pushdown(m-l+1,r-m,rt);
ll ans = 0;
if(L<=m) ans += query(L,R,lson);
if(R>m) ans += query(L,R,rson);
return ans;
}
int main() {
int n, q;
char str[10];
scanf("%d%d", &n, &q);
build(1,n,1);
int l, r, z;
while(q--) {
scanf("%s", str);
if(str[0]=='C') {
scanf("%d %d %d", &l, &r, &z);
update(l,r,z,1,n,1);
}
if(str[0]=='Q') {
scanf("%d %d", &l, &r);
printf("%lld\n", query(l,r,1,n,1));
}
}
}
HDU 1698 Just a Hook
PS:区间修改值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pushup(rt) t[rt] = t[rt<<1] + t[rt<<1|1];
const int maxn = 100000+10;
ll t[maxn<<2],lazy[maxn<<2],a[maxn];
void build(int l,int r,int rt) {
lazy[rt] = 0;
if(l==r) {
t[rt] = 1;
return ;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void pushdown(int l,int r,int rt) {
if(lazy[rt]) {
lazy[rt<<1] = lazy[rt];
lazy[rt<<1|1] = lazy[rt];
t[rt<<1] = l*lazy[rt];
t[rt<<1|1] = r*lazy[rt];
lazy[rt] = 0;
}
}
void update(int L,int R,int C,int l,int r,int rt) {
if(L<=l&&r<=R) {
t[rt] = (r-l+1)*C;
lazy[rt] = C;
return ;
}
int m = (l+r)>>1;
pushdown(m-l+1,r-m,rt);
if(L<=m) update(L,R,C,lson);
if(R>m) update(L,R,C,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt) {
if(L<=l&&r<=R) return t[rt];
int m = (l+r)>>1;
pushdown(m-l+1,r-m,rt);
ll ans = 0;
if(L<=m) ans += query(L,R,lson);
if(R>m) ans += query(L,R,rson);
return ans;
}
int main(){
int cas=1, T, n, q;
char str[10];
scanf("%d", &T);
while(T--){
scanf("%d %d", &n, &q);
build(1,n,1);
int l, r, z;
while(q--){
scanf("%d %d %d", &l, &r, &z);
update(l,r,z,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n", cas++, query(1,n,1,n,1));
}
}
POJ 2528 Mayor’s posters
PS:区间更新 + 离散化
注意,这里离散化不能用普通的
若是成段的离散化,对距离大于 1 的相邻结点,中间需再插入一个点
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 100000+10;
ll t[maxn<<2], lazy[maxn<<2], ls[maxn], vis[maxn], ans;
struct Node{
int l, r;
} a[maxn];
void build(int l,int r,int rt) {
t[rt] = -1;
if(l==r) return ;
int m = (l+r)>>1;
build(lson);
build(rson);
}
void pushdown(int rt) {
if(t[rt]!=-1) {
t[rt<<1] = t[rt];
t[rt<<1|1] = t[rt];
t[rt] = -1;
}
}
void update(int L,int R,int C,int l,int r,int rt) {
if(L<=l&&r<=R) {
t[rt] = C;
return ;
}
int m = (l+r)>>1;
pushdown(rt);
if(L<=m) update(L,R,C,lson);
if(R>m) update(L,R,C,rson);
}
void query(int l,int r,int rt) {
if(!vis[t[rt]] && t[rt]!=-1){
vis[t[rt]] = 1;
ans++;
return; //加不加好像都很快
}
if(l==r) return;
int m = (l+r)>>1;
pushdown(rt);
query(lson);
query(rson);
}
int main() {
int n, q, T;
scanf("%d", &T);
while(T--) {
int tot = 0;
memset(vis, 0, sizeof(vis));
scanf("%d", &q);
for(int i=1; i<=q; i++) {
scanf("%d %d", &a[i].l, &a[i].r);
ls[tot++] = a[i].l;
ls[tot++] = a[i].r;
}
sort(ls, ls+tot);
tot = unique(ls, ls+tot) - ls;
int sum = tot;
for(int i=1; i<sum; i++)
if(ls[i] != ls[i-1]+1) ls[tot++] = ls[i-1]+1;
sort(ls, ls+tot);
build(1,tot,1);
for(int i=1; i<=q; i++) {
int x = lower_bound(ls, ls+tot, a[i].l) - ls + 1;
int y = lower_bound(ls, ls+tot, a[i].r) - ls + 1;
update(x,y,i,1,tot,1);
}
ans = 0;
query(1,tot,1);
printf("%d\n", ans);
}
}
POJ 3225 Help with Intervals
区间的交并补
0表示没有、1表示有,-1表示可能存在异或操作
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 65536+10;
int t[maxn<<2],rev[maxn<<2],num[maxn<<2];
void build(int l,int r,int rt) {
t[rt] = rev[rt] = 0;
if(l==r) return;
int m = (l+r)>>1;
build(lson);
build(rson);
}
void reverse(int rt){
if(t[rt]!=-1) t[rt] ^= 1;
else rev[rt] ^= 1;
}
void pushdown(int rt) {
if(t[rt]!=-1) {
t[rt<<1] = t[rt<<1|1] = t[rt];
t[rt] = -1;
rev[rt<<1] = rev[rt<<1|1] = 0;
}
if(rev[rt]) {
reverse(rt<<1);
reverse(rt<<1|1);
rev[rt] = 0;
}
}
void update(int L,int R,int C,int l,int r,int rt, char op) {
if(L<=l&&r<=R) {
if(C==1 && (op=='C' || op=='S')) reverse(rt);
else t[rt] = C, rev[rt] = 0;
return ;
}
pushdown(rt);
int m = (l+r)>>1;
if(L<=m) update(L,R,C,lson,op);
if(R>m) update(L,R,C,rson,op);
}
void query(int l,int r,int rt) {
if(t[rt]==1){
for(int i=l; i<=r; i++)
num[i] = 1;
return;
}
if(t[rt]==0) return;
if(l==r) return;
pushdown(rt);
int m = (l+r)>>1;
query(lson);
query(rson);
}
int main() {
int l, r, n = 65536*2;
char op, lc, rc;
build(0,n,1);
while(~scanf(" %c %c%d,%d%c", &op, &lc, &l, &r, &rc)) {
l<<=1; r<<=1;
if(lc=='(') l++;
if(rc==')') r--;
if(l>r) continue;
if(op=='U') update(l,r,1,0,n,1,op);
if(op=='I') {
if(l>0) update(0,l-1,0,0,n,1,op);
if(r<n) update(r+1,n,0,0,n,1,op);
}
if(op=='D') update(l,r,0,0,n,1,op);
if(op=='C') {
if(l>0) update(0,l-1,0,0,n,1,op);
if(r<n) update(r+1,n,0,0,n,1,op);
update(l,r,1,0,n,1,op);
}
if(op=='S') update(l,r,1,0,n,1,op);
}
memset(num,0,sizeof(num));
query(0,n,1);
l=0, r=-1;
for(int i=1; i<=n; i++){
if(num[i] && !num[i-1]) l = i;
if(num[i-1] && !num[i]) {
r = i-1;
if(l&1) printf("(%d,", l/2);
else printf("[%d,", l/2);
if(r&1) printf("%d) ", r/2+1);
else printf("%d] ", r/2);
}
}
if(l>r) printf("empty set\n");
else printf("\n");
}
练习
POJ 1436 Horizontally Visible Segments
P
O
J
POJ
POJ
1436
1436
1436题解:这里~~
POJ 2991 Crane
P
O
J
POJ
POJ
2991
2991
2991题解:无(有点毒,懒得写)
UESTC 1425 Another LCIS
U
E
S
T
C
UESTC
UESTC
1425
1425
1425题解:这里~~
UESTC 1546 Bracket Sequence
U
E
S
T
C
UESTC
UESTC
1546
1546
1546题解:这里~~