码迷,mamicode.com
首页 > 其他好文 > 详细

区间合并

时间:2017-10-16 09:49:27      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:onclick   --   blank   txt   targe   class   view   code   logs   


 

 

 

 

 


 

1 POJ 3667 Hotel

技术分享
 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 }
View Code

 


 

区间合并

标签:onclick   --   blank   txt   targe   class   view   code   logs   

原文地址:http://www.cnblogs.com/zhaoyz/p/7675456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!