Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef long double ld;
  6. typedef pair<int,int> pi;
  7. typedef vector<int> vi;
  8. typedef vector<pi> vpi;
  9.  
  10. #define f first
  11. #define s second
  12. #define sz(x) (int)x.size()
  13. #define all(x) begin(x), end(x)
  14. #define rsz resize
  15. #define bk back()
  16. #define pb push_back
  17.  
  18. #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
  19. #define F0R(i,a) FOR(i,0,a)
  20. #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
  21. #define R0F(i,a) ROF(i,0,a)
  22. #define trav(a,x) for (auto& a: x)
  23.  
  24. const int MOD = 1e9+7;
  25. const ld PI = acos((ld)-1);
  26. template<class T> bool ckmin(T& a, const T& b) {
  27. return b < a ? a = b, 1 : 0; }
  28. mt19937 rng; // or mt19937_64
  29.  
  30. void DBG() { cerr << "]" << endl; }
  31. template<class H, class... T> void DBG(H h, T... t) {
  32. cerr << to_string(h); if (sizeof...(t)) cerr << ", ";
  33. DBG(t...); }
  34. #ifdef LOCAL // compile with -DLOCAL
  35. #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  36. #else
  37. #define dbg(...) 42
  38. #endif
  39.  
  40. typedef string str;
  41.  
  42. int xm,ym;
  43. vector<pair<pair<pi,pi>,int>> rect;
  44.  
  45. bool inter(pi a, pi b) {
  46. if (a.s < b.f || b.s < a.f) return 0;
  47. return 1;
  48. }
  49.  
  50. bool inter(pair<pi,pi> a, pair<pi,pi> b) {
  51. return inter(a.f,b.f) && inter(a.s,b.s);
  52. }
  53.  
  54. int cnt = 0;
  55. str com;
  56.  
  57. void BAD(str a) {
  58. cout << "Command " << cnt << ": " << com << " - " << a << "\n";
  59. }
  60.  
  61. bool open(pair<pi,pi> todo, int id) {
  62. bool bad = 0;
  63. if (todo.f.s >= xm || todo.f.f < 0 || todo.s.s >= ym || todo.s.f < 0) bad = 1;
  64. trav(t,rect) if (inter(t.f,todo)) bad = 1;
  65. if (bad) {
  66. BAD("window does not fit");
  67. return 0;
  68. }
  69. rect.pb({todo,id}); return 1;
  70. }
  71.  
  72. int x,y,dx,dy;
  73.  
  74. void ad() {
  75. assert(dx > 0); sort(all(rect));
  76. pair<pi,pi> todo = {{x,x},{y,y}};
  77. int ind = -1;
  78. F0R(i,sz(rect)) if (inter(rect[i].f,todo)) ind = i;
  79. if (ind == -1) {
  80. BAD("no window at given position");
  81. return;
  82. }
  83. vi dis(sz(rect),MOD);
  84. int mx = dx;
  85. dis[ind] = 0;
  86. F0R(i,sz(rect)) {
  87. FOR(j,i+1,sz(rect)) {
  88. if (inter(rect[i].f.s,rect[j].f.s))
  89. ckmin(dis[j],dis[i]+rect[j].f.f.f-rect[i].f.f.s-1);
  90. }
  91. ckmin(mx,dis[i]+xm-1-rect[i].f.f.s);
  92. }
  93. if (mx < dx) BAD("moved "+to_string(mx)+" instead of "+to_string(dx));
  94. F0R(i,sz(rect)) {
  95. int len = max(mx-dis[i],0);
  96. rect[i].f.f.f += len, rect[i].f.f.s += len;
  97. }
  98. }
  99.  
  100. void flipx() {
  101. trav(t,rect) t.f.f = {xm-1-t.f.f.s,xm-1-t.f.f.f};
  102. x = xm-1-x; dx *= -1;
  103. }
  104.  
  105. void swapxy() {
  106. swap(xm,ym);
  107. swap(x,y), swap(dx,dy);
  108. trav(t,rect) swap(t.f.f,t.f.s);
  109. }
  110.  
  111. int main() {
  112. ios_base::sync_with_stdio(0); cin.tie(0);
  113. cin >> xm >> ym;
  114. while (cin >> com) {
  115. cnt ++;
  116. if (com == "OPEN") {
  117. int x,y,w,h; cin >> x >> y >> w >> h;
  118. pair<pi,pi> todo = {{x,x+w-1},{y,y+h-1}};
  119. open(todo,cnt);
  120. } else if (com == "RESIZE") {
  121. int x,y,w,h; cin >> x >> y >> w >> h;
  122. pair<pi,pi> todo = {{x,x},{y,y}};
  123. int ind = -1;
  124. F0R(i,sz(rect)) if (inter(rect[i].f,todo)) ind = i;
  125. if (ind == -1) {
  126. BAD("no window at given position");
  127. continue;
  128. }
  129. auto p = rect[ind]; rect.erase(begin(rect)+ind);
  130. auto P = p.f; P.f.s = P.f.f+w-1, P.s.s = P.s.f+h-1;
  131. if (!open(P,p.s)) rect.pb(p);
  132. } else if (com == "CLOSE") {
  133. int x,y; cin >> x >> y;
  134. pair<pi,pi> todo = {{x,x},{y,y}};
  135. int ind = -1;
  136. F0R(i,sz(rect)) if (inter(rect[i].f,todo)) ind = i;
  137. if (ind == -1) {
  138. BAD("no window at given position");
  139. continue;
  140. }
  141. rect.erase(begin(rect)+ind);
  142. } else if (com == "MOVE") {
  143. cin >> x >> y >> dx >> dy;
  144. if (dx > 0) {
  145. ad();
  146. } else if (dx < 0) {
  147. flipx();
  148. ad();
  149. flipx();
  150. } else if (dy > 0) {
  151. swapxy();
  152. ad();
  153. swapxy();
  154. } else if (dy < 0) {
  155. swapxy();
  156. flipx();
  157. ad();
  158. flipx();
  159. swapxy();
  160. }
  161. }
  162. }
  163. sort(all(rect),[](pair<pair<pi,pi>,int> a, pair<pair<pi,pi>,int> b) {
  164. return a.s < b.s;
  165. });
  166. cout << sz(rect) << " window(s):\n";
  167. trav(t,rect) {
  168. cout << t.f.f.f << " " << t.f.s.f << " " << (t.f.f.s-t.f.f.f+1) << " " << (t.f.s.s-t.f.s.f+1) << "\n";
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement