区间合并
Posted __AiR_H
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了区间合并相关的知识,希望对你有一定的参考价值。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <stack> 9 #include <map> 10 #include <set> 11 #include <cmath> 12 #include <cctype> 13 #include <ctime> 14 #include <bitset> 15 16 using namespace std; 17 18 #define REP(i, n) for (int i = 0; i < (n); ++i) 19 #define eps 1e-9 20 #define lc id << 1 21 #define rc id << 1 | 1 22 #define lson low, mid, lc 23 #define rson mid + 1, high, rc 24 25 typedef long long ll; 26 typedef pair<int, int> pii; 27 28 const int INF = 0x7fffffff; 29 const int maxn = 2e5 + 10; 30 int n, q, opt, x, d; 31 int sum[maxn], lsum[maxn], rsum[maxn], lazy[maxn]; 32 33 void build(int low, int high, int id) { 34 sum[id] = lsum[id] = rsum[id] = high - low + 1; 35 lazy[id] = -1; if (low == high) { return; } 36 int mid = (low + high) / 2; build(lson); build(rson); 37 } 38 void push_down(int id, int low, int mid, int high) { 39 if (lazy[id] == -1) { return; } 40 lazy[lc] = lazy[rc] = lazy[id]; 41 sum[lc] = lsum[lc] = rsum[lc] = !lazy[id] ? mid - low + 1 : 0; 42 sum[rc] = lsum[rc] = rsum[rc] = !lazy[id] ? high - mid : 0; 43 lazy[id] = -1; 44 } 45 void push_up(int id, int low, int mid, int high) { 46 lsum[id] = lsum[lc]; rsum[id] = rsum[rc]; 47 if (lsum[lc] == mid - low + 1) { lsum[id] += lsum[rc]; } 48 if (rsum[rc] == high - mid) { rsum[id] += rsum[lc]; } 49 sum[id] = max(max(sum[lc], sum[rc]), rsum[lc] + lsum[rc]); 50 } 51 void update(int l, int r, int k, int low, int high, int id) { 52 if (l == low && r == high) { 53 sum[id] = lsum[id] = rsum[id] = !k ? high - low + 1 : 0; 54 lazy[id] = k; return; 55 } 56 int mid = (low + high) / 2; push_down(id, low, mid, high); 57 if (r <= mid) { update(l, r, k, lson); } 58 else if (l >= mid + 1) { update(l, r, k, rson); } 59 else { update(l, mid, k, lson); update(mid + 1, r, k, rson); } 60 push_up(id, low, mid, high); 61 } 62 int query(int k, int low, int high, int id) { 63 if (low == high) { return low; } 64 int mid = (low + high) / 2; push_down(id, low, mid, high); 65 if (sum[lc] >= k) { return query(k, lson); } 66 else if (rsum[lc] + lsum[rc] >= k) { return mid - rsum[lc] + 1; } 67 else { return query(k, rson); } 68 } 69 70 int main() { 71 #ifdef __AiR_H 72 freopen("in.txt", "r", stdin); 73 // freopen("out.txt", "w", stdout); 74 #endif // __AiR_H 75 scanf("%d %d", &n, &q); int ans; build(1, n, 1); 76 while (q--) { 77 scanf("%d", &opt); 78 if (opt == 1) { 79 scanf("%d", &d); 80 if (sum[1] < d) { printf("0\n"); continue; } 81 ans = query(d, 1, n, 1); printf("%d\n", ans); 82 update(ans, ans + d - 1, 1, 1, n, 1); 83 } else { 84 scanf("%d %d", &x, &d); 85 update(x, x + d - 1, 0, 1, n, 1); 86 } 87 } 88 return 0; 89 }
以上是关于区间合并的主要内容,如果未能解决你的问题,请参考以下文章