标签:sel 时间段 使用 首次适应算法 mod 原来 dia inpu 小明
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2705 Accepted Submission(s): 768
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <ctype.h> 5 #include <set> 6 #include <map> 7 #include <queue> 8 #include <stack> 9 using namespace std; 10 #define bug printf("******\n"); 11 const int maxn = 1e5 + 10; 12 #define rtl rt<<1 13 #define rtr rt<<1|1 14 int t, n, m, cas = 1; 15 struct node { 16 int d, n, s; 17 int dls, drs, das; 18 int nls, nrs, nas; 19 } tree[maxn << 2]; 20 void diaosi(int rt) { 21 tree[rt].d = 1; 22 tree[rt].dls = tree[rt].drs = tree[rt].das = 0; 23 } 24 void nvshen(int rt) { 25 tree[rt].d = 0, tree[rt].n = 1; 26 tree[rt].dls = tree[rt].drs = tree[rt].das = 0; 27 tree[rt].nls = tree[rt].nrs = tree[rt].nas = 0; 28 } 29 void studytime(int l, int r, int rt) { 30 tree[rt].s = 1, tree[rt].d = tree[rt].n = 0; 31 tree[rt].dls = tree[rt].drs = tree[rt].das = r - l + 1; 32 tree[rt].nls = tree[rt].nrs = tree[rt].nas = r - l + 1; 33 } 34 void pushdown(int l, int r, int rt) { 35 int mid = (l + r) >> 1; 36 if (tree[rt].s) { 37 studytime(l, mid, rtl); 38 studytime(mid + 1, r, rtr); 39 tree[rt].s = 0; 40 } 41 if (tree[rt].n) { 42 nvshen(rtl); 43 nvshen(rtr); 44 tree[rt].n = 0; 45 } 46 if (tree[rt].d) { 47 diaosi(rtl); 48 diaosi(rtr); 49 tree[rt].d = 0; 50 } 51 } 52 53 void pushup(int l, int r, int rt) { 54 int mid = (l + r) >> 1; 55 tree[rt].das = max(max(tree[rtl].das, tree[rtr].das), tree[rtl].drs + tree[rtr].dls); 56 if (tree[rtl].dls == mid - l + 1) tree[rt].dls = tree[rtl].dls + tree[rtr].dls; 57 else tree[rt].dls = tree[rtl].dls; 58 if (tree[rtr].drs == r - mid) tree[rt].drs = tree[rtl].drs + tree[rtr].drs; 59 else tree[rt].drs = tree[rtr].drs; 60 tree[rt].nas = max(max(tree[rtl].nas, tree[rtr].nas), tree[rtl].nrs + tree[rtr].nls); 61 if (tree[rtl].nls == mid - l + 1) tree[rt].nls = tree[rtl].nls + tree[rtr].nls; 62 else tree[rt].nls = tree[rtl].nls; 63 if (tree[rtr].nrs == r - mid) tree[rt].nrs = tree[rtl].nrs + tree[rtr].nrs; 64 else tree[rt].nrs = tree[rtr].nrs; 65 } 66 void modify(int L, int R, int l, int r, int rt, int f) { 67 if (L == l && r == R) { 68 if (f == 0) diaosi(rt); 69 else nvshen(rt); 70 return ; 71 } 72 pushdown(l, r, rt); 73 int m = (l + r) >> 1; 74 if (R <= m) modify(L, R, l, m, rtl, f); 75 else if (L > m ) modify(L, R, m + 1, r, rtr, f); 76 else { 77 modify(L, m, l, m, rtl, f); 78 modify(m + 1, R, m + 1, r, rtr, f); 79 } 80 pushup(l, r, rt); 81 } 82 83 int query(int rt, int l, int r, int w, int f) { 84 int mid = (l + r) >> 1; 85 if (l == r) return l; 86 pushdown(l, r, rt); 87 if (f == 0) { 88 if (tree[rtl].das >= w) query(rtl, l, mid, w, f); 89 else if (tree[rtl].drs + tree[rtr].dls >= w) return mid - tree[rtl].drs + 1; 90 else query(rtr, mid + 1, r, w, f); 91 } else { 92 if (tree[rtl].nas >= w) query(rtl, l, mid, w, f); 93 else if (tree[rtl].nrs + tree[rtr].nls >= w) return mid - tree[rtl].nrs + 1; 94 else query(rtr, mid + 1, r, w, f); 95 } 96 } 97 void study(int L, int R, int l, int r, int rt) { 98 if (L == l && r == R) { 99 studytime(l, r, rt); 100 return ; 101 } 102 pushdown(l, r, rt); 103 int m = (l + r) >> 1; 104 if (R <= m) study(L, R, l, m, rtl); 105 else if(L > m) study(L, R, m + 1, r, rtr); 106 else { 107 study(L, m, l, m, rtl); 108 study(m + 1, R, m + 1, r, rtr); 109 } 110 pushup(l, r, rt); 111 } 112 int main() { 113 scanf("%d", &t); 114 while(t--) { 115 scanf("%d%d", &n, &m); 116 printf("Case %d:\n", cas++); 117 study(1, n, 1, n, 1); 118 while(m--) { 119 char op[10]; 120 int x, y; 121 scanf("%s", op); 122 if (op[0] == ‘D‘) { 123 scanf("%d", &x); 124 if (tree[1].das < x) printf("fly with yourself\n"); 125 else { 126 int ans = query(1, 1, n, x, 0); 127 modify(ans, ans + x - 1, 1, n, 1, 0); 128 printf("%d,let‘s fly\n", ans); 129 } 130 } else if (op[0] == ‘N‘) { 131 scanf("%d", &x); 132 if (tree[1].das >= x) { 133 int ans = query(1, 1, n, x, 0); 134 modify(ans, ans + x - 1, 1, n, 1, 1); 135 printf("%d,don‘t put my gezi\n", ans); 136 } else { 137 if (tree[1].nas >= x) { 138 int ans = query(1, 1, n, x, 1); 139 modify(ans, ans + x - 1, 1, n, 1, 1); 140 printf("%d,don‘t put my gezi\n", ans); 141 } else printf("wait for me\n"); 142 } 143 } else { 144 scanf("%d%d", &x, &y); 145 study(x, y, 1, n, 1); 146 printf("I am the hope of chinese chengxuyuan!!\n"); 147 } 148 } 149 } 150 return 0; 151 }
标签:sel 时间段 使用 首次适应算法 mod 原来 dia inpu 小明
原文地址:https://www.cnblogs.com/qldabiaoge/p/9383160.html