hdu4553约会安排(线段树区间合并)

链接

poj3667的加强版 当时的题解

这里只不过对于女神需要另开算,DS的占用的时间不加在女神身上,女神的时间都要加,清空的时候也都要算。

  1 #include <iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #include<stdlib.h>
6 #include<vector>
7 #include<cmath>
8 #include<queue>
9 #include<set>
10 using namespace std;
11 #define N 100010
12 #define LL long long
13 #define INF 0xfffffff
14 const double eps = 1e-8;
15 const double pi = acos(-1.0);
16 const double inf = ~0u>>2;
17 int lm[N<<2],ll[N<<2],lr[N<<2];
18 int nlm[N<<2],nll[N<<2],nlr[N<<2];
19 int lz[N<<2],nlz[N<<2];
20 void up(int w,int m)
21 {
22 ll[w] = ll[w<<1]==(m-m/2)?ll[w<<1|1]+ll[w<<1]:ll[w<<1];
23 lr[w] = lr[w<<1|1]==(m/2)?lr[w<<1]+lr[w<<1|1]:lr[w<<1|1];
24 lm[w] = max(max(lm[w<<1],lm[w<<1|1]),lr[w<<1]+ll[w<<1|1]);
25 nll[w] = nll[w<<1]==(m-m/2)?nll[w<<1|1]+nll[w<<1]:nll[w<<1];
26 nlr[w] = nlr[w<<1|1]==(m/2)?nlr[w<<1]+nlr[w<<1|1]:nlr[w<<1|1];
27 nlm[w] = max(max(nlm[w<<1],nlm[w<<1|1]),nlr[w<<1]+nll[w<<1|1]);
28 }
29 void down(int w,int m)
30 {
31 if(lz[w]!=-1)
32 {
33 ll[w<<1] = lr[w<<1] = lm[w<<1] = lz[w]?(m-m/2):0;
34 ll[w<<1|1] = lr[w<<1|1] = lm[w<<1|1] = lz[w]?(m/2):0;
35 lz[w<<1] = lz[w<<1|1] = lz[w];
36 lz[w] = -1;
37 }
38 if(nlz[w]!=-1)
39 {
40 nll[w<<1] = nlr[w<<1] = nlm[w<<1] = nlz[w]?(m-m/2):0;
41 nll[w<<1|1] = nlr[w<<1|1] = nlm[w<<1|1] = nlz[w]?(m/2):0;
42 nlz[w<<1] = nlz[w<<1|1] = nlz[w];
43 nlz[w] = -1;
44 }
45 }
46 void build(int l,int r,int w)
47 {
48 if(l==r)
49 {
50 lm[w] = ll[w] = lr[w] = 1;
51 nlm[w] = nll[w] = nlr[w] = 1;
52 return ;
53 }
54 int m = (l+r)>>1;
55 build(l,m,w<<1);
56 build(m+1,r,w<<1|1);
57 up(w,r-l+1);
58 }
59 void update(int a,int b,int d,int flag,int l,int r,int w)
60 {
61 if(a<=l&&b>=r)
62 {
63 if(flag)
64 {
65 nlm[w] = nll[w] = nlr[w] = d*(r-l+1);
66 nlz[w] = d;
67 }
68 lm[w] = ll[w] = lr[w] = d*(r-l+1);
69 lz[w] = d;
70 return ;
71 }
72 down(w,r-l+1);
73 int m = (l+r)>>1;
74 if(a<=m)
75 update(a,b,d,flag,l,m,w<<1);
76 if(b>m)
77 update(a,b,d,flag,m+1,r,w<<1|1);
78 up(w,r-l+1);
79 }
80 int find(int k,int f,int l,int r,int w)
81 {
82 if(l==r)
83 {
84 return l;
85 }
86 int m = (l+r)>>1;
87 down(w,r-l+1);
88 if(f)
89 {
90 if(lm[w<<1]>=k)
91 return find(k,f,l,m,w<<1);
92 else if(lr[w<<1]+ll[w<<1|1]>=k)
93 return m-lr[w<<1]+1;
94 else return find(k,f,m+1,r,w<<1|1);
95 }
96 else
97 {
98 if(nlm[w<<1]>=k)
99 return find(k,f,l,m,w<<1);
100 else if(nlr[w<<1]+nll[w<<1|1]>=k)
101 return m-nlr[w<<1]+1;
102 else return find(k,f,m+1,r,w<<1|1);
103 }
104 }
105 int main()
106 {
107 int n,kk=0,t,q;
108 int x,y;
109 char s[20];
110 scanf("%d",&t);
111 while(t--)
112 {
113 memset(lz,-1,sizeof(lz));
114 memset(nlz,-1,sizeof(nlz));
115 scanf("%d%d",&n,&q);
116 build(1,n,1);
117 printf("Case %d:\n",++kk);
118 while(q--)
119 {
120 scanf("%s%d",s,&x);
121 if(s[0]==‘D‘)
122 {
123 if(lm[1]<x)
124 puts("fly with yourself");
125 else
126 {
127 int k = find(x,1,1,n,1);
128 update(k,k+x-1,0,0,1,n,1);
129 printf("%d,let‘s fly\n",k);
130 }
131 }
132 else if(s[0]==‘N‘)
133 {
134 if(lm[1]>=x)
135 {
136 int k = find(x,1,1,n,1);
137 printf("%d,don‘t put my gezi\n",k);
138 update(k,k+x-1,0,1,1,n,1);
139 }
140 else if(nlm[1]>=x)
141 {
142 int k = find(x,0,1,n,1);
143 printf("%d,don‘t put my gezi\n",k);
144 update(k,k+x-1,0,1,1,n,1);
145 }
146 else puts("wait for me");
147 }
148 else
149 {
150 scanf("%d",&y);
151 update(x,y,1,1,1,n,1);
152 printf("I am the hope of chinese chengxuyuan!!\n");
153 }
154 }
155 }
156 return 0;
157 }

hdu4553约会安排(线段树区间合并),布布扣,bubuko.com

时间: 2024-10-10 10:51:49

hdu4553约会安排(线段树区间合并)的相关文章

hdu 4453 约会安排(线段树区间合并)

约会安排 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 433    Accepted Submission(s): 145 Problem Description 寒假来了,又到了小明和女神们约会的季节. 小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会.与此同时,也有

hdu4553约会安排 线段树

//DS QT  找一段最靠前的长度为QT的空间 //NS QT  找一段最靠前的长度为QT的空间,如果没找到可以将DS占据的空间当做空闲空间,找一段最靠前的空间 //STUDY!! L R  清空L ,R的空间 //用两个线段树,一个处理DS的情况,一个处理NS的情况 //线段树维护,ma_l , ma_r , ma 区间[l,r]的左边的空闲区间,右边的以及最大的空闲区间 #include<cstdio> #include<cstring> #include<iostre

HDU 3911 Black And White(线段树区间合并)

Problem Description There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she wan

HDU 3308 LCIS (线段树区间合并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[mid + 1] > a[mid],写的细心点就好了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int MAXN = 1

HDU 5316 Magician(线段树区间合并入门)

本文纯属原创,转载请注明出处谢谢.http://blog.csdn.net/zip_fan. 题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5316 Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Fantasy magicians usually gain their ability

poj3667 线段树 区间合并

1 //Accepted 3728 KB 1079 ms 2 //线段树 区间合并 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <queue> 7 #include <cmath> 8 #include <algorithm> 9 using namespace std; 10 /** 11 * This is a document

hdu3911 线段树 区间合并

1 //Accepted 3911 750MS 9872K 2 //线段树 区间合并 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <queue> 7 #include <cmath> 8 #include <algorithm> 9 using namespace std; 10 /** 11 * This is a documen

线段树 区间合并

poj3667 Hotel 区间合并入门题,照着代码打的, 题意:1 a:询问是不是有连续长度为a的空房间,有的话住进最左边       2 a b:将[a,a+b-1]的房间清空思路:记录区间中最长的空房间,开三个数组,msum[rt]表示节点rt内连续的1的个数的最大值,lsum[rt]表示从节点rt左端点开始连续1的个数,rsum[rt]表示从节点rt右端点开始连续1的个数..线段树操作:update:区间替换 query:询问满足条件的最左端点 1 #include<iostream>

【BZOJ3638】Cf172 k-Maximum Subsequence Sum 线段树区间合并(模拟费用流)

[BZOJ3638]Cf172 k-Maximum Subsequence Sum Description 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少.1 ≤ n ≤ 105,1 ≤ m ≤ 105,1 ≤ l ≤ r ≤ n, 1 ≤ k ≤ 20 Sample Input 9 9 -8 9 -1 -1 -1 9 -8 9 3 1 1 9 1 1 1 9 2 1 4 6 3 Sample Output 17 25