线段树的简单题目,做一个离散化,O(lgn)可以找到id。RE了一晚上,额,后来找到了原因。
1 /* 555C */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 #include <functional> 19 #include <iterator> 20 #include <iomanip> 21 using namespace std; 22 //#pragma comment(linker,"/STACK:102400000,1024000") 23 24 #define sti set<int> 25 #define stpii set<pair<int, int> > 26 #define mpii map<int,int> 27 #define vi vector<int> 28 #define pii pair<int,int> 29 #define vpii vector<pair<int,int> > 30 #define rep(i, a, n) for (int i=a;i<n;++i) 31 #define per(i, a, n) for (int i=n-1;i>=a;--i) 32 #define clr clear 33 #define pb push_back 34 #define mp make_pair 35 #define fir first 36 #define sec second 37 #define all(x) (x).begin(),(x).end() 38 #define SZ(x) ((int)(x).size()) 39 #define lson l, mid, rt<<1 40 #define rson mid+1, r, rt<<1|1 41 42 typedef struct { 43 int x, y; 44 char op[4]; 45 } node_t; 46 47 const int maxn = 2e5+5; 48 const int maxm = 4e5+5; 49 node_t Q[maxn]; 50 int rn[maxm]; 51 int mxr[maxm<<2]; 52 int mxc[maxm<<2]; 53 int rL[maxm<<2]; 54 int cT[maxm<<2]; 55 int L, R; 56 57 void build(int l, int r, int rt) { 58 rL[rt] = 1; 59 cT[rt] = 1; 60 if (l == r) 61 return ; 62 63 int mid = (l+r)>>1; 64 build(lson); 65 build(rson); 66 } 67 68 inline void PushDownR(int rt) { 69 int lb = rt<<1; 70 int rb = lb | 1; 71 72 if (mxr[rt]) { 73 rL[rt] = max(rL[rt], mxr[rt]); 74 mxr[lb] = max(mxr[lb], mxr[rt]); 75 mxr[rb] = max(mxr[rb], mxr[rt]); 76 mxr[rt] = 0; 77 } 78 } 79 80 inline void PushDownC(int rt) { 81 int lb = rt<<1; 82 int rb = lb | 1; 83 84 if (mxc[rt]) { 85 cT[rt] = max(cT[rt], mxc[rt]); 86 mxc[lb] = max(mxc[lb], mxc[rt]); 87 mxc[rb] = max(mxc[rb], mxc[rt]); 88 mxc[rt] = 0; 89 } 90 } 91 92 inline void PushDown(int rt) { 93 int lb = rt<<1; 94 int rb = lb | 1; 95 96 if (mxr[rt]) { 97 rL[rt] = max(rL[rt], mxr[rt]); 98 mxr[lb] = max(mxr[lb], mxr[rt]); 99 mxr[rb] = max(mxr[rb], mxr[rt]); 100 mxr[rt] = 0; 101 } 102 103 if (mxc[rt]) { 104 cT[rt] = max(cT[rt], mxc[rt]); 105 mxc[lb] = max(mxc[lb], mxc[rt]); 106 mxc[rb] = max(mxc[rb], mxc[rt]); 107 mxc[rt] = 0; 108 } 109 } 110 111 void updateR(int delta, int l, int r, int rt) { 112 if (L<=l && R>=r) { 113 mxr[rt] = max(mxr[rt], delta); 114 return ; 115 } 116 117 int mid = (l+r)>>1; 118 119 PushDownR(rt); 120 if (mid >= R) { 121 updateR(delta, lson); 122 } else if (mid < L) { 123 updateR(delta, rson); 124 } else { 125 updateR(delta, lson); 126 updateR(delta, rson); 127 } 128 } 129 130 void updateC(int delta, int l, int r, int rt) { 131 if (L<=l && R>=r) { 132 mxc[rt] = max(mxc[rt], delta); 133 return ; 134 } 135 136 int mid = (l+r)>>1; 137 138 PushDownC(rt); 139 if (mid >= R) { 140 updateC(delta, lson); 141 } else if (mid < L) { 142 updateC(delta, rson); 143 } else { 144 updateC(delta, lson); 145 updateC(delta, rson); 146 } 147 } 148 149 pii query(int l, int r, int rt) { 150 if (l == r) { 151 rL[rt] = max(rL[rt], mxr[rt]); 152 cT[rt] = max(cT[rt], mxc[rt]); 153 mxr[rt] = 0; 154 mxc[rt] = 0; 155 156 pii p(rL[rt], cT[rt]); 157 return p; 158 } 159 int mid = (l+r)>>1; 160 161 PushDown(rt); 162 if (mid >= R) { 163 return query(lson); 164 } else { 165 return query(rson); 166 } 167 } 168 169 int main() { 170 ios::sync_with_stdio(false); 171 #ifndef ONLINE_JUDGE 172 freopen("data.in", "r", stdin); 173 freopen("data.out", "w", stdout); 174 #endif 175 176 int n, q; 177 mpii tb; 178 sti st; 179 180 scanf("%d %d", &n, &q); 181 rep(i, 0, q) { 182 scanf("%d %d %s", &Q[i].y, &Q[i].x, Q[i].op); 183 st.insert(Q[i].x); 184 st.insert(Q[i].y); 185 } 186 st.insert(1); 187 188 int m = 0; 189 int i, j, k; 190 191 for (sti::iterator siter=st.begin(); siter!=st.end(); ++siter) { 192 k = *siter; 193 ++m; 194 rn[m] = k; 195 tb[k] = m; 196 } 197 198 int x, y, idx, idy; 199 int ans; 200 pii p; 201 202 build(1, m, 1); 203 rep(i, 0, q) { 204 x = Q[i].x; 205 y = Q[i].y; 206 if (Q[i].op[0] == ‘U‘) { 207 idy = tb[y]; 208 L = R = idy; 209 p = query(1, m, 1); 210 updateC(x+1, 1, m, 1); 211 ans = x - p.sec + 1; 212 idx = tb[x]; 213 k = lower_bound(rn+1, rn+1+m, p.sec) - rn; 214 L = k; 215 R = idx; 216 if (L <= R) 217 updateR(y+1, 1, m, 1); 218 } else { 219 idx = tb[x]; 220 L = R = idx; 221 p = query(1, m, 1); 222 updateR(y+1, 1, m, 1); 223 ans = y - p.fir + 1; 224 idy = tb[y]; 225 k = lower_bound(rn+1, rn+1+m, p.fir) - rn; 226 L = k; 227 R = idy; 228 if (L <= R) 229 updateC(x+1, 1, m, 1); 230 } 231 #ifndef ONLINE_JUDGE 232 printf("L = %d, R = %d\n", L, R); 233 #endif 234 if (ans < 0) 235 ans = 0; 236 printf("%d\n", ans); 237 } 238 239 #ifndef ONLINE_JUDGE 240 printf("time = %d.\n", (int)clock()); 241 #endif 242 243 return 0; 244 }
时间: 2024-10-18 20:15:49