首页 > 代码库 > hdu4553约会安排(线段树区间合并)

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 }
View Code