Appointments HDU - 4553 (segment tree query interval, the interval modify, merge interval)

topic:

Winter came, and went to Xiao Ming and goddesses dating season. 
  Xiao Ming Although the grass root level code farmers, but very active, enthusiastic goddess who often reply "Oh," following a statement by a large segment of the Internet Xiaoming, so, Xiao Ming is a favorite and goddesses dating. At the same time, there are many gay friend asked him to open the black, because the number is too large, how to arrange a time Xiao Ming has become a major mind. 
  There will be many or goddess-based Friends come to us known Xiao Ming Xiao Ming, a total of T idle time period. 
  As an operating system once anger exam 71 points Great God, Xiao Ming think of an algorithm, that is, "first fit algorithm", according to the description of the operating system textbooks, it is to find contiguous space for some of the most forward of the meet the requirements assigned to each request, thus Xiao Ming made a decision: 
  when a gay friend came Xiaoming, according to Xiao Ming on "first-fit algorithm" to find some free time and based Friends of the appointment, if found, say "X, let's fly" (here, X is the start time), otherwise say "fly with yourself";
  when the goddess came to Bob, first use a "first fit algorithm", if not found, Xiao Ming would run the risk of ignoring all wood Jiji Cock silk-based Friends of the convention, again using the "ignore-based Friends of the first-fit algorithm", twice as long as one is found, say "X, do not put my gezi " ( here, X is the start time), otherwise say " wait for me " 
  of course, we know who Bob is not a festival parade negative infinity if and goddesses dating finished, time remaining, and he will be based Friends of the original appointment to dota. (For example: Konishi (Cock wire) Xiaoming appointment play dota within 1 to 5 the unit of time period, at this time, the goddess to Xiaoming reservation length of period 3, the final is 1 to 3, Xiaoming to goddess dating, playing dota at 4-5 and get back Konishi) 
  Xiao Ming occasionally want to learn new knowledge, at this time Xiao Ming will all have a certain predetermined time interval of time to learn all the empty and Shout "I am the hope of chinese chengxuyuan !!", but are generally Xiaoming heat for three minutes, then it was to reserve it, Xiao Ming will be unable to bear alone the time to learn new knowledge dispensed.

Input

CASE input of the first behavior, expressed CASE set of test data; 
each set of data in two integers T, begins N T represents the total time, N is the number of reservation request; 
next N rows, each row represents one or goddess based Friends of the appointment, "NS QT" on behalf of a goddess came Xiaoming about for some length of time the QT, "DS QT" represents a Cock wire length request QT, of course, also possible that Bob wanted to learn the knowledge of, "sTUDY !! LR "all requests within the interval representative of emptying L ~ R. 

[Technical Specification] 
1.. 1 <= the CASE <= 30 
2.. 1 <= T, N <= 100000 
3.. 1 <= QT <= 110000 
4.. 1 <= L <= R & lt <= T

Output

For each Case, the first line of the first output "Case C:" is the first of several representatives Case, then N rows, each row corresponding to a result of the request (see below). 
Output samples (can be copied here): 
"X-, the let apos Fly", "Fly with Yourself", "X-, do Not My Gezi PUT", "the wait for Me", "The Hope of the I AM chinese chengxuyuan !!" 

Sample Input

1
5 6
DS 3
NS 2
NS 4
STUDY!! 1 5
DS 4
NS 2

Sample Output

Case 1:
1,let's fly
4,don't put my gezi
wait for me
I am the hope of chinese chengxuyuan!!
1,let's fly
1,don't put my gezi

:( code analysis longest since freshman ever seen, are 200+ online, he just took me down to the collapse of the 150+, too much trouble ...) this troublesome question is how to find the range of queries from the left the first free zone. See the specific implementation of code >>

Code:

  1 #include <bits / STDC ++. H> ///////////////////// longest ever written code of topics, mygod!
  2  the using  namespace STD;
   . 3  const  int MAXN 1E5 + = 10 ;
   . 4  struct Node
   . 5  {
   . 6      int L, R & lt;
   . 7      int RM, mm, LM; //// Cock wire
   . 8      int RM1, MM1, LM1; //// / goddess
   . 9      void INI () = mm {RM = LM = 0 ;}
 10      void Full () = {RM = R & lt mm LM = - L + . 1 ;}
 . 11      void INI1 () = {RM1 MM1 = LM1 = 0; }
 12     void full1() { rm1 = mm1 = lm1 = r - l + 1; }
 13 }t[maxn << 2];
 14 int n, m;
 15 
 16 void pushdown(int tar)
 17 {
 18     if (t[tar].mm == 0)
 19         t[tar << 1].ini(), t[tar << 1 | 1].ini();
 20     if (t[tar].mm == t[tar].r - t[tar].l + 1)
 21         t[tar << 1].full(), t[tar << 1 | 1].full();
 22     if(t[tar].mm1 == 0)
 23         t[tar << 1].ini1(), t[tar << 1 | 1].ini1();
 24     if (t[tar].mm1 == t[tar].r - t[tar].l + 1)
 25         t[tar << 1].full1(), t[tar << 1 | 1].full1();
 26 }
 27 
 28 void pushup(int tar)
 29 {
 30     t[tar].lm = t[tar << 1].lm;
 31     t[tar].rm = t[tar << 1 | 1].rm;
 32     t[tar].mm = max(t[tar << 1].mm, t[tar << 1 | 1].mm);
 33     t[tar].mm = max(t[tar].mm, t[tar << 1].rm + t[tar << 1 | 1].lm);
 34     if (t[tar << 1].lm == t[tar << 1].r - t[tar << 1].l + 1)
 35         t[tar].lm += t[tar << 1 | 1].lm;
 36     if (t[tar << 1 | 1].rm == t[tar << 1 | 1].r - t[tar << 1 | 1].l + 1)
 37         t[tar].rm += t[tar << 1].rm;
 38 
 39     t[tar].lm1 = t[tar << 1].lm1;
 40     t[tar].rm1 = t[tar << 1 | 1].rm1;
 41     t[tar].mm1 = max(t[tar << 1].mm1, t[tar << 1 | 1].mm1);
 42     t[tar].mm1 = max(t[tar].mm1, t[tar << 1].rm1 + t[tar << 1 | 1].lm1);
 43     if (t[tar << 1].lm1 == t[tar << 1].r - t[tar << 1].l + 1)
 44         t[tar].lm1 += t[tar << 1 | 1].lm1;
 45     if (t[tar << 1 | 1].rm1 == t[tar << 1 | 1].r - t[tar << 1 | 1].l + 1)
 46         t[tar].rm1 += t[tar << 1].rm1;
 47 }
 48 
 49 void build(int l, int r, int tar)
 50 {
 51     t[tar].l = l, t[tar].r = r;
 52     t[tar].full(), t[tar].full1();
 53     if (l == r) return;
 54     int mid = (l + r) >> 1;
 55     build(l, mid, tar << 1);
 56     build(mid + 1, r, tar << 1 | 1);
 57 }
 58 
 59 void update(int kind, int l, int r, int tar)
 60 {
 61     if (l == t[tar].l && r == t[tar].r)
 62     {
 63         if (kind == 1)  t[tar].ini();
 64         else if (kind == 2) t[tar].ini(), t[tar].ini1();
 65         else t[tar].full(), t[tar].full1();
 66         return;
 67     }
 68     pushdown(tar);
 69     int mid = (t[tar].l + t[tar].r) >> 1;
 70     if (r <= mid) update(kind, l, r, tar << 1);
 71     else if (l > mid) update(kind, l, r, tar << 1 | 1);
 72     else update(kind, l, mid, tar << 1), update(kind, mid + 1, r, tar << 1 | 1);
 73     pushup(tar);
 74 }
 75 
 76 int query(int kind, int tar, int v)
 77 {
 78     if (kind == 1)
 79     {
 80         if (t[tar].mm < v) return 0;
 81         if (t[tar].lm >= v) return t[tar].l;
 82         if (t[tar << 1].mm >= v) return query(kind, tar << 1, v);
 83         if (t[tar << 1].rm + t[tar << 1 | 1].lm >= v) return t[tar << 1].r - t[tar << 1].rm + 1;
 84         return query(kind, tar << 1 | 1, v);
 85     }
 86     else
 87     {
 88         if (t[tar].mm1 < v) return 0;
 89         if (t[tar].lm1 >= v) return t[tar].l;
 90         if (t[tar << 1].mm1 >= v) return query(kind, tar << 1, v);
 91         if (t[tar << 1].rm1 + t[tar << 1 | 1].lm1 >= v) return t[tar << 1].r - t[tar << 1].rm1 + 1;
 92         return query(kind, tar << 1 | 1, v);
 93     }
 94 }
 95 
 96 int main()
 97 {
 98     int T; cin >> T;
 99     char dir[100];
100     int x, y;
101     int cases = 0;
102 
103     while (T--)
104     {
105         scanf("%d%d", &n, &m);
106         build(1, n, 1);
107         printf("Case %d:\n", ++cases);
108         while (m--)
109         {
110             scanf("%s", dir);
111             if (dir[0] == 'D')
112             {
113                 scanf("%d", &x);
114                 int l = query(1, 1, x);
115                 //cout << l << endl;
116                 if(!l) printf("fly with yourself\n");
117                 else
118                 {
119                     printf("%d,let's fly\n", l);
120                     update(1, l, l + x - 1, 1);   //kind, l, r, tar
121                 }
122             }
123             else if (dir[0] == 'N')
124             {
125                 scanf("%d", &x);
126                 int l = query(1, 1, x);
127                 //cout << l << endl;
128                 if (l != 0)
129                 {
130                     printf("%d,don't put my gezi\n", l);
131                     update(2, l, l + x - 1, 1);
132                 }
133                 else
134                 {
135                     l = query(2, 1, x);
136                     if (l != 0)
137                     {
138                         printf("%d,don't put my gezi\n", l);
139                         update(2, l, l + x - 1, 1);
140                     }
141                     else printf("wait for me\n");
142                 }
143             }
144             else
145             {
146                 scanf("%d%d", &x, &y);
147                 printf("I am the hope of chinese chengxuyuan!!\n");
148                 update(3, x, y, 1);
149             }
150         }
151     }
152 }

Guess you like

Origin www.cnblogs.com/liuwenhan/p/11356198.html