POJ3667 Hotel

Hotel
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 18195   Accepted: 7897

Description

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ XiN-Di+1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (b) Three space-separated integers representing a check-out: 2, Xi, and Di

Output

* Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

Sample Input

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

Sample Output

1
4
7
0
5

Source

 
【题解】
这题拖得时间太长,有一些错忘记了。主要错误是   下穿标记的时候忘了改ma
还有就是 sum我手残也修改了 导致判断left[o <<1]能否接上left[o <<1 | 1]出
了偏差
 
思路就是维护前缀/后缀  最长连续序列  和 区间内最长连续序列
 
查询的时候根据  区间内最长连续序列额 分情况即可
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #define max(a, b) ((a) > (b) ? (a) :(b))
  6 #define min(a, b) ((a) < (b) ? (a) : (b)) 
  7 
  8 inline void read(int &x)
  9 {
 10     x = 0;char ch = getchar(), c = ch;
 11     while(ch < '0' || ch > '9')c = ch, ch = getchar();
 12     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
 13     if(c == '-')x = -x;
 14 }
 15 
 16 inline void swap(int &a, int &b)
 17 {
 18     int tmp = a;a = b;b = tmp;
 19 }
 20 
 21 const int MAXN = 100000 + 10;
 22 
 23 int n,m,left[MAXN << 2], right[MAXN << 2], ma[MAXN << 2], sum[MAXN << 2], lazy[MAXN << 2];
 24 
 25 void build(int o, int l, int r)
 26 {
 27     lazy[o] = -1;
 28     if(l == r)
 29     {
 30         left[o] = right[o] = ma[o] = sum[o] = 1;
 31         return;
 32     }
 33     int mid = (l + r) >> 1;
 34     build(o << 1, l, mid);
 35     build(o << 1 | 1, mid + 1, r);
 36     
 37     left[o] = left[o << 1];
 38     if(left[o << 1] == sum[o << 1])left[o] += left[o << 1 | 1];
 39     right[o] = right[o << 1 | 1];
 40     if(right[o << 1 | 1] == sum[o << 1 | 1])right[o] += right[o << 1];
 41     ma[o] = max(left[o], max(left[o << 1 | 1] + right[o << 1],right[o]));    
 42     ma[o] = max(ma[o], max(ma[o << 1], ma[o << 1 | 1]));
 43     sum[o] = sum[o << 1] + sum[o << 1 | 1];
 44 }
 45 
 46 inline void pushdown(int o, int l, int r)
 47 {
 48     if(lazy[o] == -1)return;
 49     register int mid = (l + r) >> 1;
 50     register int k = lazy[o];
 51     lazy[o << 1] = lazy[o << 1 | 1] = k;
 52     left[o << 1] = right[o << 1] = ma[o << 1] = k * (mid - l + 1);
 53     left[o << 1 | 1] = right[o << 1 | 1] = ma[o << 1 | 1] = k * (r - mid);
 54     lazy[o] = -1;
 55 }
 56 
 57 void modify(int ll, int rr, int k, int o, int l, int r)
 58 {
 59     pushdown(o, l, r);
 60     if(ll <= l && rr >= r)
 61     {
 62         left[o] = right[o] = ma[o] = k * (r - l + 1);
 63         lazy[o] = k;
 64         return;
 65     }
 66     int mid = (l + r) >> 1;
 67     if(mid >= ll)modify(ll, rr, k, o << 1, l, mid);
 68     if(mid < rr) modify(ll, rr, k, o << 1 | 1, mid + 1, r); 
 69     
 70     left[o] = left[o << 1];
 71     if(left[o << 1] == sum[o << 1])left[o] += left[o << 1 | 1];
 72     right[o] = right[o << 1 | 1];
 73     if(right[o << 1 | 1] == sum[o << 1 | 1])right[o] += right[o << 1];
 74     ma[o] = max(left[o], max(left[o << 1 | 1] + right[o << 1],right[o]));    
 75     ma[o] = max(ma[o], max(ma[o << 1], ma[o << 1 | 1]));
 76     return;
 77 }
 78 
 79 struct Node
 80 {
 81     int left, sum;
 82     Node(int _left, int _sum){left = _left; sum = _sum;}
 83     Node(){left = sum = 0;}
 84 };
 85 
 86 int ask(int p, int o, int l, int r)
 87 {
 88     pushdown(o, l, r);
 89     if(l == r && left[l])return l;
 90     int mid = (l + r) >> 1;
 91     if(ma[o << 1] >= p)return ask(p, o << 1, l, mid);
 92     else
 93     {
 94         if(right[o << 1] + left[o << 1 | 1] >= p) return mid - right[o << 1] + 1;
 95         else if(ma[o << 1 | 1] >= p)return ask(p, o << 1 | 1, mid + 1, r);
 96         else return 0;
 97     }
 98 }
 99 
100 int main()
101 {
102     read(n), read(m);
103     register int tmp1, tmp2, tmp3;
104     memset(lazy, -1, sizeof(lazy));
105     build(1,1,n);
106     for(register int i = 1;i <= m;++ i)
107     {
108         read(tmp1);
109         if(tmp1 == 1) 
110         {
111             read(tmp2);
112             tmp3 = ask(tmp2, 1, 1, n);
113             printf("%d\n", tmp3);
114             if(tmp3)modify(tmp3, tmp3 + tmp2 - 1, 0, 1, 1, n);    
115         }
116         else
117         {
118             read(tmp2), read(tmp3);
119             modify(tmp2, tmp2 + tmp3 - 1, 1, 1, 1, n);
120         }
121     }
122     return 0;
123 } 
POJ3667

 

posted @ 2017-09-06 23:30  嘒彼小星  阅读(189)  评论(0编辑  收藏  举报