poj 1823 hotel 线段树

题意:一个hotel,有n个连续的房间,开始时均无人住宿

共有3种操作

1 a b 从a开始连续b个房间全部旅客住宿 [a,a+b-1];

2 a b 从a开始连续b个房间全部旅客离开 [a,a+b-1];

3 查询最长连续空房间

 

思路:线段树,记录每个节点,左边,右边各最多连续空房间lmax,rmax; 以及这个区间内最多空房间max

 

  1 #include<iostream>
2 #include<cmath>
3 using namespace std;
4 #define MAXN 16001
5 struct node
6 {
7 int lmax,rmax,max;
8 int left,right;
9 int flag;
10 };
11 node tree[MAXN*4];
12 void build(int i)
13 {
14 tree[i].lmax=tree[i].rmax=tree[i].max=tree[i].right-tree[i].left+1;
15 tree[i].flag=0;
16 if(tree[i].left==tree[i].right) return ;
17 int mid=(tree[i].left+tree[i].right)/2;
18 tree[2*i].left=tree[i].left; tree[2*i].right=mid;
19 tree[2*i+1].left=mid+1; tree[2*i+1].right=tree[i].right;
20 build(2*i); build(2*i+1);
21 }
22
23 void insert(int i,int x,int y,int c)
24 {
25 //cout<<i<<" "<<x<<" "<<y<<" "<<c<<" "<<tree[i].left<<" "<<tree[i].right<<endl;
26 if(tree[i].left==x&&tree[i].right==y)
27 {
28 tree[i].flag=c;
29 if(c==1) tree[i].lmax=tree[i].rmax=tree[i].max=0;
30 else tree[i].lmax=tree[i].rmax=tree[i].max=tree[i].right-tree[i].left+1;
31 return ;
32 }
33 if(tree[i].flag==0)
34 {
35 tree[2*i].flag=tree[2*i+1].flag=0;
36 tree[2*i].lmax=tree[2*i].rmax=tree[2*i].max=tree[2*i].right-tree[2*i].left+1;
37 tree[2*i+1].lmax=tree[2*i+1].rmax=tree[2*i+1].max=tree[2*i+1].right-tree[2*i+1].left+1;
38 }
39 if(tree[i].flag==1)
40 {
41 tree[2*i].flag=tree[2*i+1].flag=1;
42 tree[2*i].lmax=tree[2*i].rmax=tree[2*i].max=0;
43 tree[2*i+1].lmax=tree[2*i+1].rmax=tree[2*i+1].max=0;
44 }
45 int mid=(tree[i].left+tree[i].right)/2;
46 if(y<=mid) insert(2*i,x,y,c);
47 else if(x>mid) insert(2*i+1,x,y,c);
48 else
49 {
50 insert(2*i,x,mid,c);
51 insert(2*i+1,mid+1,y,c);
52 }
53 if(tree[2*i].flag==0&&tree[2*i+1].flag==0)
54 {
55 tree[i].flag=0;
56 tree[i].lmax=tree[i].rmax=tree[i].max=tree[i].right-tree[i].left+1;
57 }
58 else if(tree[2*i].flag==0&&tree[2*i+1].flag==1)
59 {
60 tree[i].flag=2;
61 tree[i].lmax=tree[i].max=tree[2*i].max;
62 tree[i].rmax=0;
63 }
64 else if(tree[2*i].flag==1&&tree[2*i+1].flag==0)
65 {
66 tree[i].flag=2;
67 tree[i].rmax=tree[i].max=tree[2*i+1].max;
68 tree[i].lmax=0;
69 }
70 else if(tree[2*i].flag==1&&tree[2*i+1].flag==1)
71 {
72 tree[i].flag=1;
73 tree[i].lmax=tree[i].rmax=tree[i].max=0;
74 }
75 else
76 {
77 tree[i].flag=2;
78 tree[i].max=max(tree[2*i].max,tree[2*i+1].max);
79 tree[i].max=max(tree[i].max,tree[2*i].rmax+tree[2*i+1].lmax);
80 if(tree[2*i].flag==0) tree[i].lmax=tree[2*i].max+tree[2*i+1].lmax;
81 else tree[i].lmax=tree[2*i].lmax;
82 if(tree[2*i+1].flag==0) tree[i].rmax=tree[2*i].rmax+tree[2*i+1].max;
83 else tree[i].rmax=tree[2*i+1].rmax;
84 }
85 }
86 int main()
87 {
88 int n,m;
89 scanf("%d%d",&n,&m);
90 tree[1].left=1; tree[1].right=n;
91 build(1);
92 int i;
93 int x,y,c;
94 for(i=1;i<=m;i++)
95 {
96 scanf("%d",&c);
97 if(c==3) printf("%d\n",tree[1].max);
98 if(c==1)
99 {
100 scanf("%d%d",&x,&y);
101 insert(1,x,x+y-1,1);
102 }
103 if(c==2)
104 {
105 scanf("%d%d",&x,&y);
106 insert(1,x,x+y-1,0);
107 }
108 }
109 return 0;
110 }



posted on 2012-02-08 22:06  myoi  阅读(494)  评论(0编辑  收藏  举报

导航