2025年线段树回顾

线段树回顾一 单点更新 1 hdu1166 敌兵布阵 单点增减 区间求和 include stdlib h include stdio h include string h include math h include algorithm include lt algorithm math h string h stdio h stdlib h

大家好,我是讯享网,很高兴认识大家。

一、单点更新

1.hdu1166 敌兵布阵
单点增减,区间求和
#include<stdlib.h> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<ctype.h> #include<iostream> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define se second #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; typedef pair<ll, ll> pii; typedef pair<pii, ll> Pii; template<class T> inline void read(T &x){ 
    x=0; char c=getchar(); int f=1; while (!isdigit(c)) { 
   if (c=='-') f=-1; c=getchar();} while (isdigit(c)) { 
   x=x*10+c-'0'; c=getchar();} x*=f; } const int N = , maxn = ; void FAST(){ 
   ios::sync_with_stdio(false);cin.tie(0);} const ll mod = ; int a[N], sum[N]; void build(int l, int r, int rt) { 
    if(l == r) { 
    sum[rt] = a[l]; return ; } int mid = (l+r)>>1; build(l, mid, rt<<1); build(mid+1, r, rt<<1|1); sum[rt] = sum[rt<<1]+sum[rt<<1|1]; } void update(int dis, int add, int l,int r, int rt) { 
    if(l == r) { 
    sum[rt] += add; return ; } int m = (l+r)>>1; if(dis <= m) update(dis, add, l, m, rt<<1); else update(dis, add, m+1, r, rt<<1|1); sum[rt] = sum[rt<<1]+sum[rt<<1|1]; } void init(int n) { 
    for(int i = 0; i <= 4*n; ++i) sum[i] = 0; } void solve() { 
    int n;scanf("%d", &n);init(n); for(int i = 1; i <= n; ++i) scanf("%d", &a[i]); static int k = 0; printf("Case %d:\n", ++k); build(1, n, 1); char s[10]; while(scanf(" %s", s), s[0]!='E') { 
    if(s[0] == 'Q') { 
    int l, r;scanf("%d%d", &l, &r); int ans = query(l, r, 1, n, 1); printf("%d\n", ans); } else if(s[0] == 'A') { 
    int add, dis; scanf("%d%d", &dis, &add); update(dis, add, 1, n, 1); } else { 
    int add, dis; scanf("%d%d", &dis, &add); update(dis, -1*add, 1, n, 1); } } } int main() { 
    int t;scanf("%d", &t);while(t--) solve(); return 0; } 

讯享网

2.hdu1754 I Hate It

单点修改加区间最大
讯享网#include<stdlib.h> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<ctype.h> #include<iostream> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define se second #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; typedef pair<ll, ll> pii; typedef pair<pii, ll> Pii; template<class T> inline void read(T &x){ 
    x=0; char c=getchar(); int f=1; while (!isdigit(c)) { 
   if (c=='-') f=-1; c=getchar();} while (isdigit(c)) { 
   x=x*10+c-'0'; c=getchar();} x*=f; } const int N = , maxn = ; void FAST(){ 
   ios::sync_with_stdio(false);cin.tie(0);} const ll mod = ; int a[maxn], _max[maxn], n, m; void Push_up(int x) { 
    _max[x] = max(_max[x<<1], _max[x<<1|1]); } void build(int l, int r, int rt) { 
    if(l == r) { 
    _max[rt] = a[l]; return; } int m = (l+r)>>1; build(l, m, rt<<1); build(m+1, r, rt<<1|1); Push_up(rt); } void update(int p, int tihuan, int l, int r, int rt) { 
    if(l == r) { 
    _max[rt] = tihuan;return; } int m = (l+r)>>1; if(m >= p) update(p, tihuan, l, m, rt<<1); else update(p, tihuan, m+1, r, rt<<1|1); Push_up(rt); } int query(int L, int R, int l, int r, int rt) { 
    if(l>=L&&r<=R) return _max[rt]; int m = (l+r)>>1; int ans = -inf; if(L <= m) ans = max(ans, query(L, R, l, m, rt<<1)); if(R > m) ans = max(ans, query(L, R, m+1, r, rt<<1|1)); return ans; } void init() { 
    for(int i = 0; i <4*n; ++i) _max[i] = 0; } void solve() { 
    init(); for(int i = 1; i <= n; ++i) scanf("%d", &a[i]); build(1, n, 1); while(m--) { 
    char s[5]; int l, r; scanf(" %s%d%d", s, &l, &r); if(s[0]=='Q') printf("%d\n", query(l, r, 1, n, 1)); else update(l, r, 1, n, 1); } } int main() { 
    //int t;scanf("%d", &t);while(t--) while(~scanf("%d%d", &n, &m)) solve(); return 0; } 

3.hdu1394Minimum Inversion Number

很巧妙的求解方法,先求原始序列的逆序对个数,然后再枚举每一个数的移动。比如第一数为x,因为都是0到n-1之间的不同的数,很容易发现,比x小的个数变成了x,比x大的数变成了n-(x+1);总的就是n-2*x-1
#include<stdlib.h> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<ctype.h> #include<iostream> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define se second #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; typedef pair<ll, ll> pii; typedef pair<pii, ll> Pii; template<class T> inline void read(T &x){ 
    x=0; char c=getchar(); int f=1; while (!isdigit(c)) { 
   if (c=='-') f=-1; c=getchar();} while (isdigit(c)) { 
   x=x*10+c-'0'; c=getchar();} x*=f; } const int N = , maxn = ; void FAST(){ 
   ios::sync_with_stdio(false);cin.tie(0);} const ll mod = ; int a[N], sum[N]; void Push_up(int rt) { 
    sum[rt] = sum[rt<<1]+sum[rt<<1|1]; } void build(int l, int r, int rt) { 
    if(l == r) { 
    sum[rt] = 0; return; } int m = (l+r)>>1; build(lson); build(rson); Push_up(rt); } void update(int p, int l, int r, int rt) { 
    if(l == r) { 
    sum[rt]++; return; } int m = (l+r)>>1; if(p <= m) update(p, lson); else update(p, rson); Push_up(rt); } int query(int L, int R, int l, int r,int rt) { 
    if(L <= l&&R>=r) return sum[rt]; int m = (l+r)>>1; int ans = 0; if(L<=m) ans += query(L, R, lson); if(R>m) ans += query(L,R,rson); return ans; } int n; void init() { 
    for(int i = 0; i <=4*n; ++i) sum[i] = 0; } void solve() { 
    //init(n); build(0, n-1, 1); int sum = 0; for(int i = 1; i <= n; ++i) { 
    scanf("%d", &a[i]); sum += query(a[i], n-1, 0, n-1, 1); update(a[i], 0, n-1, 1); } int ans = sum; for(int i = 1; i <= n; ++i) { 
    sum += n-2*a[i]-1; ans = min(sum, ans); } printf("%d\n", ans); } int main() { 
    //int t;scanf("%d", &t);while(t--) while(~scanf("%d", &n)) solve(); return 0; } 

4.hdu2795 Billboard

经典题目,考虑一下大于n的层数是无意义的,然后就变成了区间最大,单点修改。线段树查询区间最大,然后考虑左右子树,左子树优先就能更高。
讯享网#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <vector> #include<queue> #include<string.h> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define se second #define SZ(x) (int)x.size() #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; const int N = , maxn = ; const ll mod = inf; int h, w, n; int _max[maxn]; void Push_up(int rt) { 
    _max[rt] = max(_max[rt<<1], _max[rt<<1|1]); } void build(int l, int r, int rt) { 
    if(l == r) { 
    _max[rt] = w; return; } int m = (l+r)>>1; build(l, m, rt<<1); build(m+1, r, rt<<1|1); Push_up(rt);// } void update(int l, int r, int rt, int k, int &ans) { 
    if(l == r&&_max[rt] >= k) { 
    _max[rt] -= k; ans = l; return ; } int m = (l+r)>>1; if(_max[rt<<1]>=k) update(l, m, rt<<1, k, ans); else update(m+1, r, rt<<1|1, k, ans); Push_up(rt); } void solve() { 
    int t = min(n, h);//注意如果n大于h就不用用h的高度了,因为根本用不到 build(1, t, 1); while(n--){ 
    int k, ans = -1; scanf("%d", &k); if(_max[1] >= k) { 
    update(1, t, 1, k, ans); } printf("%d\n", ans); } } int main() { 
    //int T;scanf("%d", &T);while(T--) while(~scanf("%d%d%d", &h, &w, &n)) solve(); return 0; } 

5.poj2828 BuyTickets

逆向维护,除了位置0是0,其他初始为1,然后进行查找,更新。单点更新,通过和找区间。
#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <vector> #include<queue> #include<string.h> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define se second #define SZ(x) (int)x.size() #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; const int N = , maxn = ; const ll mod = inf; int n; struct node { 
    int pos, val; }a[maxn]; int sum[maxn]; void Push_up(int rt) { 
    sum[rt] = sum[rt<<1]+sum[rt<<1|1]; } void build(int l, int r, int rt) { 
    if(l == r) { 
    if(l == 0) sum[rt] = 0; else sum[rt] = 1;return ; } int m = (l+r)>>1; build(lson);build(rson);Push_up(rt); } void update(int p, int l, int r, int rt) { 
    if(l == r) { 
    sum[rt] = 0; return ; } int m = (l+r)>>1; if(m >= p) update(p, lson); else update(p, rson); Push_up(rt); } int query(int l, int r, int rt, int val) { 
    if(l == r) return l; int m = (l+r)>>1; if(sum[rt<<1] > val) return query(lson, val); else return query(rson, val-sum[rt<<1]); } int ans[N]; void solve() { 
    build(0, n, 1); for(int i = 1;i <= n; ++i) scanf("%d%d", &a[i].pos, &a[i].val); for(int i = n; i >= 1; --i) { 
    int pos = query(0, n, 1, a[i].pos); ans[pos] = a[i].val; update(pos, 0, n, 1); } for(int i = 1; i <= n; ++i) printf("%d%c", ans[i], " \n"[i==n]); } int main() { 
    //int T;scanf("%d", &T);while(T--) while(~scanf("%d", &n)) solve(); return 0; } 

二、区间更新

1.hdu1698 Just a Hook

区间修改,最后求和

讯享网//https://codeforces.ml/contest/1326/standings/friends/true #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <vector> #include<queue> #include<string.h> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define se second #define SZ(x) (int)x.size() #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; const int N = , maxn = ; const ll mod = inf; ll sum[maxn], lazy[maxn]; void Push_up(int rt) { 
    sum[rt] = sum[rt<<1]+sum[rt<<1|1]; } void Push_down(int rt, int m) { 
    if(lazy[rt]) { 
    lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt]; sum[rt<<1] = (m-(m>>1))*lazy[rt]; sum[rt<<1|1] = (m>>1)*lazy[rt]; lazy[rt] = 0; } } void build(int l, int r, int rt) { 
    lazy[rt] = 0; if(l == r) { 
    sum[rt] = 1; lazy[rt] = 0; return ; } int m = (l+r)>>1; build(lson);build(rson);Push_up(rt); } void update(int L, int R, int c, int l, int r, int rt) { 
    if(l>=L&&r<=R) { 
    lazy[rt] = c; sum[rt] = c*(r-l+1); return; } Push_down(rt, r-l+1); int m = (l+r)>>1; if(L<=m) update(L, R, c, lson); if(R>m) update(L,R,c,rson); Push_up(rt); } void solve() { 
    static int k = 0; int n, m;scanf("%d%d", &n, &m); build(1, n, 1); while(m--) { 
    int a, b, c; scanf("%d%d%d", &a, &b, &c); update(a, b, c, 1, n, 1); } printf("Case %d: The total value of the hook is %d.\n", ++k, sum[1]); } int main() { 
    int T;scanf("%d", &T);while(T--) solve(); return 0; } 

2 poj3468 A Simple Problem with Integers

线段树功能:update:成段增减 query:区间求和
#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <vector> #include<queue> #include<string.h> #include<set> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define se second #define SZ(x) (int)x.size() #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; const int N = , maxn = ; const ll mod = inf; int n, q; ll sum[N<<2], lazy[N<<2], a[N]; void pushup(int rt) { 
    sum[rt] = sum[rt<<1]+sum[rt<<1|1]; } void pushdown(int rt, int m) { 
    if(lazy[rt]) { 
    lazy[rt<<1] += lazy[rt]; lazy[rt<<1|1] += lazy[rt]; sum[rt<<1] += lazy[rt]*(m-(m>>1)); sum[rt<<1|1] += lazy[rt]*(m>>1); lazy[rt] = 0; } } void build(int l, int r,int rt) { 
    lazy[rt] = 0; if(l == r) { 
    sum[rt] = a[l]; 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) { 
    lazy[rt] += c; sum[rt] += (ll)c*(r-l+1); return ; } pushdown(rt, r-l+1); 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 sum[rt]; pushdown(rt, r-l+1); 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; } void solve() { 
    for(int i = 1; i <= n; ++i) scanf("%lld", &a[i]); build(1, n, 1); while(q--) { 
    char s[10]; int l, r, c; scanf(" %s%d%d", s, &l, &r); if(s[0] == 'Q') printf("%lld\n", query(l, r, 1, n, 1)); else { 
    scanf("%d", &c); update(l, r, c, 1, n, 1); } } } int main() { 
    // int T;scanf("%d", &T);while(T--) scanf("%d%d", &n, &q); solve(); return 0; } 

P3373 【模板】线段树 2

区间乘和区间加模板题
讯享网#include<bits/stdc++.h> #define fi first #define se second #define ALL(a) a.begin(), a.end() #define pb push_back #define MEM(a, b) memset(a, b, sizeof(a)) #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define int long long #define gg cout<<"---+++++----\n"; #define db1(x) cout<<#x<<" = "<<x<<endl #define db2(x, y) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl; using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<pii, int> piii; const int N = 1e5+10, maxn = 1e6+10; //const ll mod = 1e9+7; ll mod; int n, m, k; int a[N]; struct segment { #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define ls rt<<1 #define rs rt<<1|1 ll sum[N<<2], lazy1[N<<2], lazy2[N<<2]; void push_up(int rt) { sum[rt] = (sum[ls]+sum[rs])%mod; } void push_down(int rt, int m) { ll lz1 = lazy1[rt]; ll lz2 = lazy2[rt]; if(lz1!=1) { lazy1[ls] = (lazy1[ls]*lz1)%mod; lazy1[rs] = (lazy1[rs]*lz1)%mod; lazy2[ls] = (lazy2[ls]*lz1)%mod; lazy2[rs] = (lazy2[rs]*lz1)%mod; sum[ls] = (sum[ls]*lz1)%mod; sum[rs] = (sum[rs]*lz1)%mod; lazy1[rt] = 1; } if(lz2) { lazy2[ls] = (lazy2[ls]+lz2)%mod; lazy2[rs] = (lazy2[rs] + lz2)%mod; sum[ls] = (sum[ls]+1ll*((m-(m>>1))*lz2)%mod)%mod; sum[rs] = (sum[rs]+1ll*((m>>1)*lz2)%mod)%mod; lazy2[rt] = 0; } } void build(int l=1, int r=n, int rt=1) { sum[rt] = lazy2[rt] = 0; lazy1[rt] = 1; if(l == r) { sum[rt] = a[l]; return ; } int m = (l+r)>>1; build(lson); build(rson); push_up(rt); } void update(int op, int L, int R,int k, int l=1, int r=n,int rt=1) { if(l>=L&&r<=R) { if(op == 1) { lazy1[rt] = (lazy1[rt]*k)%mod; lazy2[rt] = (lazy2[rt]*k)%mod; sum[rt] = (sum[rt]*k)%mod; } else { lazy2[rt] = (lazy2[rt]+k)%mod; sum[rt] = ((sum[rt]+1ll*(r-l+1)*k%mod))%mod; } return ; } push_down(rt, r-l+1); int m = (l+r)>>1; if(L <= m) update(op, L, R, k, lson); if(R > m) update(op, L, R, k, rson); push_up(rt); } ll query(int L, int R, int l=1, int r=n, int rt=1) { if(l>=L&&r<=R) return sum[rt]; push_down(rt, r-l+1); 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%mod; } }T; void solve() { scanf("%lld%lld%lld", &n, &m, &mod); for(int i = 1; i <= n; ++i) scanf("%lld", &a[i]); T.build(); while(m--) { int op, x, y, k; scanf("%lld%lld%lld", &op, &x, &y); if(op!=3) scanf("%lld", &k); if(op == 3) printf("%lld\n", T.query(x, y)); else T.update(op, x, y, k); } } signed main() { // while(~scanf("%d%d", &n, &m)) // scanf("%d%d", &n, &m); // int _;scanf("%d", &_); while(_--) solve(); // return 0; } 

3 poj2528 Mayor’s posters

离散化,经典区间覆盖问题。
#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <vector> #include<queue> #include<string.h> #include<set> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define se second #define SZ(x) (int)x.size() #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; const int N = 66666, maxn = ; const ll mod = inf; int n, m; vector<int> ss; struct node { 
    int l, r; }a[N], hs[N]; int Find(int x) { 
    return lower_bound(ss.begin(), ss.end(), x)-ss.begin()+1; } int col[N], ans; bool flag[N]; void Push_down(int rt) { 
    if(col[rt]) { 
    col[rt<<1] = col[rt<<1|1] = col[rt]; col[rt] = 0; } } void build(int l, int r, int rt) { 
    col[rt] = 0; if(l == r) return ; int m = (l+r)>>1; build(lson); build(rson); } void update(int L, int R, int c, int l, int r,int rt) { 
    if(l>=L&&r<=R) { 
    col[rt] = c; return; } Push_down(rt); int m = (l+r)>>1; if(L<=m) update(L, R, c, lson); if(R>m) update(L, R, c, rson); } void query(int l, int r, int rt) { 
    int x = col[rt]; if(x) { 
    if(!flag[x]) ans++; flag[x] = 1; return; } if(l == r) return ; int m = (l+r)>>1; query(lson); query(rson); } void init() { 
    scanf("%d", &n); ss.clear(); memset(flag, 0, sizeof(flag)); for(int i = 0; i < n; ++i) { 
    scanf("%d%d", &a[i].l, &a[i].r); ss.pb(a[i].l); ss.pb(a[i].r); } sort(ss.begin(), ss.end()); ss.erase(unique(ss.begin(), ss.end()), ss.end()); int ss_siz = SZ(ss); for(int i = 0; i < ss_siz-1; ++i) { 
    if(ss[i+1]-ss[i]>1) ss.pb(ss[i]+1); } sort(ss.begin(), ss.end()); ss.erase(unique(ss.begin(), ss.end()), ss.end()); for(int i = 0; i < n; ++i) { 
    hs[i].l = Find(a[i].l); hs[i].r = Find(a[i].r); } } void solve() { 
    init(); int ss_siz = SZ(ss); build(1, ss_siz, 1); for(int i = 0; i < n; ++i) { 
    update(hs[i].l, hs[i].r, i+1, 1, ss_siz, 1); } ans = 0; query(1, ss_siz, 1); printf("%d\n", ans); } int main() { 
    // freopen("in.txt", "r", stdin); int T;scanf("%d", &T);while(T--) solve(); return 0; } 

4 poj3225 Help with Intervals

题意:区间操作,交,并,补等
思路:

我们一个一个操作来分析:(用0和1表示是否包含区间,-1表示该区间内既有包含又有不包含)

U:把区间[l,r]覆盖成1

I:把[-∞,l)(r,∞]覆盖成0

D:把区间[l,r]覆盖成0

C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换

S:[l,r]区间0/1互换

成段覆盖的操作很简单,比较特殊的就是区间0/1互换这个操作,我们可以称之为异或操作


讯享网

很明显我们可以知道这个性质:当一个区间被覆盖后,不管之前有没有异或标记都没有意义了

所以当一个节点得到覆盖标记时把异或标记清空

而当一个节点得到异或标记的时候,先判断覆盖标记,如果是0或1,直接改变一下覆盖标记,不然的话改变异或标记

开区间闭区间只要数字乘以2就可以处理(偶数表示端点,奇数表示两端点间的区间)

线段树功能:update:成段替换,区间异或 query:简单hash
讯享网#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <vector> #include<queue> #include<string.h> #include<set> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define se second #define SZ(x) (int)x.size() #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; const int N = 65536*2, maxn = ; const ll mod = ; bool hs[N]; int cover[N<<2], XOR[N<<2]; void FXOR(int rt) { 
    if(cover[rt] != -1) cover[rt] ^= 1; else XOR[rt] ^= 1; } void PushDown(int rt) { 
    if(cover[rt] != -1) { 
    cover[rt<<1] = cover[rt<<1|1] = cover[rt]; XOR[rt<<1|1] = XOR[rt<<1] = 0; cover[rt] = -1; } if(XOR[rt]) { 
    FXOR(rt<<1); FXOR(rt<<1|1); XOR[rt] = 0; } } void update(char op, int L, int R, int l, int r, int rt) { 
    if(l>=L&&r<=R){ 
    if(op == 'U') cover[rt] = 1, XOR[rt] = 0; else if(op == 'D') cover[rt] = XOR[rt] = 0; else if(op == 'C'||op == 'S') FXOR(rt); return ; } PushDown(rt); int m = (l+r)>>1; if(L<=m) update(op, L, R, lson); else if(op == 'I'||op == 'C') XOR[rt<<1] = cover[rt<<1] = 0; if(R>m) update(op, L, R, rson); else if(op == 'I'||op == 'C') XOR[rt<<1|1] = cover[rt<<1|1] = 0; } void query(int l, int r, int rt) { 
    if(cover[rt] == 1) { 
    for(int i = l; i <= r; ++i) hs[i] = 1; return ; } if(l == r) return ; PushDown(rt); int m = (l+r)>>1; query(lson);query(rson); } int main() { 
    // freopen("in.txt", "r", stdin); // int T;scanf("%d", &T);while(T--) char op, l, r; int a, b; while(~scanf(" %c %c%d,%d%c", &op, &l, &a, &b,&r)) { 
    a<<=1;b<<=1; if(l == '(') a++; if(r == ')') b--; if(a > b) { 
    if(op == 'C'||op == 'I') cover[1] = XOR[1] = 0; } else update(op, a, b, 0, N, 1); } query(0, N, 1); bool flag = false; int s = -1, e; for(int i = 0; i <= N; ++i) { 
    if(hs[i]) { 
    if(s == -1) s = i; e = i; } else { 
    if(s != -1) { 
    if(flag) printf(" "); flag = true; printf("%c%d,%d%c", s&1?'(':'[', s>>1,(e+1)>>1, e&1?')':']'); s = -1; } } } if(!flag) printf("empty set"); puts(""); return 0; } 

5 poj3667 Hotel

题意:1 a:询问是不是有连续长度为a的空房间,有的话住进最左边,没有输出0 2 a b:将[a,a+b-1]的房间清空
思路:记录区间中最长的空房间
线段树操作:update:区间替换 query:询问满足条件的最左断点
#include<cstdio> #include<cstdlib> #include<algorithm> #include<iostream> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define se second #define SZ(x) (int)x.size() #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; const int N = , maxn = ; const ll mod = ; int msum[N], lsum[N], rsum[N], cover[N]; void PushDown(int rt, int m) { 
    if(cover[rt]!=-1) { 
    cover[rt<<1] = cover[rt<<1|1] = cover[rt]; msum[rt<<1] = lsum[rt<<1] = rsum[rt<<1] = cover[rt]?0:(m-(m>>1)); msum[rt<<1|1] = lsum[rt<<1|1] = rsum[rt<<1|1] = cover[rt]?0:(m>>1); cover[rt] = -1; } } void PushUP(int rt, int m) { 
    lsum[rt] = lsum[rt<<1]; rsum[rt] = rsum[rt<<1|1]; if(lsum[rt]==(m-(m>>1))) lsum[rt] += lsum[rt<<1|1]; if(rsum[rt]==(m>>1)) rsum[rt] += rsum[rt<<1]; msum[rt] = max(max(msum[rt<<1], msum[rt<<1|1]), rsum[rt<<1]+lsum[rt<<1|1]); } void build(int l, int r, int rt) { 
    msum[rt] = lsum[rt] = rsum[rt] = r-l+1; cover[rt] = -1; if(l == r) return; int m = (l+r)>>1; build(lson);build(rson); } void update(int L, int R, int c, int l, int r, int rt) { 
    if(l>=L&&r<=R) { 
    msum[rt] = lsum[rt] = rsum[rt] = c?0:r-l+1; cover[rt] = c; return; } int m = (l+r)>>1; PushDown(rt, r-l+1); if(L<=m) update(L, R, c, lson); if(R>m) update(L, R, c, rson); PushUP(rt, r-l+1); } int query(int c, int l, int r, int rt) { 
    if(l == r) return l; //if(lsum[rt] >= c) return l; int m = (l+r)>>1; PushDown(rt, r-l+1); if(msum[rt<<1] >= c) return query(c, lson); if(rsum[rt<<1]+lsum[rt<<1|1] >= c) return m-rsum[rt<<1]+1; return query(c, rson); } void solve() { 
    int n, m; scanf("%d%d", &n, &m); build(1, n, 1); while(m--) { 
    int op;scanf("%d", &op); if(op == 1) { 
    int cnt;scanf("%d", &cnt); int ans = 0; if(msum[1] >= cnt) ans = query(cnt, 1, n, 1), update(ans, ans+cnt-1, 1, 1, n, 1); printf("%d\n", ans); } else { 
    int l, r; scanf("%d%d", &l, &r); r = l+r-1; update(l, r, 0, 1, n, 1); } } } int main() { 
    //int T;scanf("%d", &T);while(T--) solve(); return 0; } 

Atlantis HDU - 1542

参考博客

扫描线
讯享网#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include<vector> #include<queue> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<x<<" "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define fi first #define SZ(x) (int)x.size() #define se second #define pb push_back #define inf 0x3f3f3f3f3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<pii, ll> Pii; template<class T> inline void read(T &x){ 
    x=0; char c=getchar(); int f=1; while (!isdigit(c)) { 
   if (c=='-') f=-1; c=getchar();} while (isdigit(c)) { 
   x=x*10+c-'0'; c=getchar();} x*=f; } const int N = 2e5+10, maxn = 1e6+5; void FAST(){ 
   ios::sync_with_stdio(false);cin.tie(0);} const ll mod = 1e9+7; int n, m; int cnt[maxn]; double sum[maxn], fy[maxn]; struct node2 { 
    double x, y1, y2; int op; bool operator <(const node2 &oth) const { 
    return x < oth.x; } }line[maxn]; void PushUp(int rt, int l, int r) { 
    if(cnt[rt]>0) { 
    sum[rt] = fy[r+1]-fy[l]; } else sum[rt] = sum[rt<<1]+sum[rt<<1|1]; } void build(int l, int r, int rt) { 
    sum[rt] = 0.0; cnt[rt] = 0; if(l == r) return ; int m = (l+r)>>1; build(lson); build(rson); } void update(int L, int R, int c, int l, int r, int rt) { 
    if(l>=L&&r<=R) { 
    cnt[rt]+=c; PushUp(rt, l, r); return ; } int m = (l+r)>>1; if(L<=m) update(L, R, c, lson); if(R>m) update(L, R, c, rson); PushUp(rt, l, r); } int fy_siz; int Find(double x) { 
    return lower_bound(fy+1, fy+fy_siz+1, x)-fy; } void solve() { 
    static int t = 1; for(int i = 1, j = 1; i <= n; ++i) { 
    double x1, x2, y1, y2; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); line[j].x = x1, line[j].y1 = y1, line[j].y2 = y2, line[j].op = 1; fy[j++] = y1; line[j].x = x2, line[j].y1 = y1, line[j].y2 = y2, line[j].op = -1; fy[j++] = y2; } sort(line+1, line+1+2*n); sort(fy+1, fy+1+2*n); fy_siz = unique(fy+1, fy+1+2*n)-fy-1; build(1, fy_siz-1, 1); double ans = 0; for(int i = 1; i <= 2*n; ++i) { 
    ans += sum[1]*(line[i].x-line[i-1].x); int l = Find(line[i].y1), r = Find(line[i].y2); update(Find(line[i].y1), Find(line[i].y2)-1, line[i].op, 1, fy_siz-1, 1); } printf("Test case #%d\nTotal explored area: %.2lf\n\n", t++, ans); } int main() { 
    // int _; scanf("%d", &_); while(_--) // while(~scanf("%d%d", &n, &d)) while(scanf("%d", &n)&&n) solve(); return 0; } 

Picture POJ - 1177

扫描线求轮廓周长

产考博客

#include<cstdio> #include<algorithm> #include<iostream> #include<string> #include<cstring> #include<map> #include<vector> #include<queue> #include<math.h> #include<climits> #include<set> #define debug(x) cout <<#x<<" = "<<x<<endl #define debug2(x, y) cout<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<endl #define gg cout <<"---------------QAQ---------------"<<endl #define rep(i,n) for(int i=1;i<=n;i++) #define fi first #define SZ(x) (int)x.size() #define se second #define pb push_back #define inf 0x3f3f3f3f #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI acos(-1) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<pii, ll> Pii; template<class T> inline void read(T &x){ 
    x=0; char c=getchar(); int f=1; while (!isdigit(c)) { 
   if (c=='-') f=-1; c=getchar();} while (isdigit(c)) { 
   x=x*10+c-'0'; c=getchar();} x*=f; } const int N = 2e5+10, maxn = 1e6+5; void FAST(){ 
   ios::sync_with_stdio(false);cin.tie(0);} const ll mod =  ; struct Edge { 
    int l, r; //左右端点的横坐标 int h; //这条线的高度,y int f; }e[N]; bool cmp(Edge a, Edge b) { 
    return a.h < b.h; } struct Node { 
    int l, r; int len; //区间被覆盖长度 int s;//区间被覆盖几次 bool lc, rc; //左右端点是否被覆盖,(0,1) int num; //区间线段数 // len来计算横线,num来计算竖线 }q[N]; void PushUp(int rt) { 
    if(q[rt].s) { 
    //增高区间被覆盖 q[rt].len = q[rt].r - q[rt].l+1; q[rt].lc = q[rt].rc = 1; q[rt].num = 1; } else if(q[rt].l == q[rt].r) //这是一个点而不是一条线段 { 
    q[rt].len = 0; q[rt].lc = q[rt].rc = 0; q[rt].num = 0; } else //是一条没有整个区间被覆盖的线段,合并左右子树的信息 { 
    q[rt].len = q[rt<<1].len + q[rt<<1|1].len; q[rt].lc = q[rt<<1].lc; q[rt].rc = q[rt<<1|1].rc; q[rt].num = q[rt<<1].num+q[rt<<1|1].num-(q[rt<<1].rc&q[rt<<1|1].lc); //如果左子的右端点和右子的左端点都被覆盖,二合一 } } void build(int l, int r, int rt) { 
    q[rt].l = l; q[rt].r = r; q[rt].s = q[rt].len = 0; q[rt].lc = q[rt].rc = q[rt].num = 0; if(l == r) return ; int m = (q[rt].l + q[rt].r)>>1; build(lson); build(rson); } void update(int xx, int l, int r, int rt) { 
    if(l==q[rt].l&&q[rt].r==r) { 
    q[rt].s += xx; PushUp(rt); return ; } int m = (q[rt].l+q[rt].r)>>1; if(r<=m) update(xx, l, r, rt<<1); else if(l>m) update(xx, l, r, rt<<1|1); else { 
    update(xx, lson); update(xx, rson); } PushUp(rt); } int n; void solve() { 
    int x1, x2, y1, y2, mx = -inf, mn = inf; int tot = 0; for(int i = 0; i < n; ++i) { 
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2); mx = max(mx, max(x1, x2)); mn = min(mn, min(x1, x2)); Edge & t1 = e[tot++]; Edge & t2 = e[tot++]; t1.l = t2.l = x1; t1.r = t2.r = x2; t1.h = y1; t2.h = y2; t1.f = 1; t2.f = -1; } sort(e, e+tot, cmp); int ans = 0;//计算周长 int last = 0; //保存上一次总区间的被覆盖长度 build(mn, mx-1, 1); for(int i = 0; i < tot; ++i) { 
    update(e[i].f, e[i].l, e[i].r-1, 1); //横线:现在这次总区间被覆盖的程度和上一次总区间被覆盖长度之差的绝对值 ans += abs(q[1].len-last); //竖线:(下一条横线的高度-现在这条横线的高度)*2*num ans += (e[i+1].h-e[i].h)*2*q[1].num; last = q[1].len; } printf("%d\n", ans); } int main() { 
    // int _; scanf("%d", &_); while(_--) while(~scanf("%d", &n)) // while(scanf("%d", &n)&&n) solve(); return 0; } 
小讯
上一篇 2025-01-25 21:23
下一篇 2025-01-28 15:17

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/112341.html