Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- typedef pair<int,int> pi;
- typedef vector<int> vi;
- typedef vector<pi> vpi;
- #define f first
- #define s second
- #define sz(x) (int)x.size()
- #define all(x) begin(x), end(x)
- #define rsz resize
- #define bk back()
- #define pb push_back
- #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
- #define F0R(i,a) FOR(i,0,a)
- #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
- #define R0F(i,a) ROF(i,0,a)
- #define trav(a,x) for (auto& a: x)
- const int MOD = 1e9+7;
- const ld PI = acos((ld)-1);
- template<class T> bool ckmin(T& a, const T& b) {
- return b < a ? a = b, 1 : 0; }
- mt19937 rng; // or mt19937_64
- void DBG() { cerr << "]" << endl; }
- template<class H, class... T> void DBG(H h, T... t) {
- cerr << to_string(h); if (sizeof...(t)) cerr << ", ";
- DBG(t...); }
- #ifdef LOCAL // compile with -DLOCAL
- #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
- #else
- #define dbg(...) 42
- #endif
- typedef string str;
- int xm,ym;
- vector<pair<pair<pi,pi>,int>> rect;
- bool inter(pi a, pi b) {
- if (a.s < b.f || b.s < a.f) return 0;
- return 1;
- }
- bool inter(pair<pi,pi> a, pair<pi,pi> b) {
- return inter(a.f,b.f) && inter(a.s,b.s);
- }
- int cnt = 0;
- str com;
- void BAD(str a) {
- cout << "Command " << cnt << ": " << com << " - " << a << "\n";
- }
- bool open(pair<pi,pi> todo, int id) {
- bool bad = 0;
- if (todo.f.s >= xm || todo.f.f < 0 || todo.s.s >= ym || todo.s.f < 0) bad = 1;
- trav(t,rect) if (inter(t.f,todo)) bad = 1;
- if (bad) {
- BAD("window does not fit");
- return 0;
- }
- rect.pb({todo,id}); return 1;
- }
- int x,y,dx,dy;
- void ad() {
- assert(dx > 0); sort(all(rect));
- pair<pi,pi> todo = {{x,x},{y,y}};
- int ind = -1;
- F0R(i,sz(rect)) if (inter(rect[i].f,todo)) ind = i;
- if (ind == -1) {
- BAD("no window at given position");
- return;
- }
- vi dis(sz(rect),MOD);
- int mx = dx;
- dis[ind] = 0;
- F0R(i,sz(rect)) {
- FOR(j,i+1,sz(rect)) {
- if (inter(rect[i].f.s,rect[j].f.s))
- ckmin(dis[j],dis[i]+rect[j].f.f.f-rect[i].f.f.s-1);
- }
- ckmin(mx,dis[i]+xm-1-rect[i].f.f.s);
- }
- if (mx < dx) BAD("moved "+to_string(mx)+" instead of "+to_string(dx));
- F0R(i,sz(rect)) {
- int len = max(mx-dis[i],0);
- rect[i].f.f.f += len, rect[i].f.f.s += len;
- }
- }
- void flipx() {
- trav(t,rect) t.f.f = {xm-1-t.f.f.s,xm-1-t.f.f.f};
- x = xm-1-x; dx *= -1;
- }
- void swapxy() {
- swap(xm,ym);
- swap(x,y), swap(dx,dy);
- trav(t,rect) swap(t.f.f,t.f.s);
- }
- int main() {
- ios_base::sync_with_stdio(0); cin.tie(0);
- cin >> xm >> ym;
- while (cin >> com) {
- cnt ++;
- if (com == "OPEN") {
- int x,y,w,h; cin >> x >> y >> w >> h;
- pair<pi,pi> todo = {{x,x+w-1},{y,y+h-1}};
- open(todo,cnt);
- } else if (com == "RESIZE") {
- int x,y,w,h; cin >> x >> y >> w >> h;
- pair<pi,pi> todo = {{x,x},{y,y}};
- int ind = -1;
- F0R(i,sz(rect)) if (inter(rect[i].f,todo)) ind = i;
- if (ind == -1) {
- BAD("no window at given position");
- continue;
- }
- auto p = rect[ind]; rect.erase(begin(rect)+ind);
- auto P = p.f; P.f.s = P.f.f+w-1, P.s.s = P.s.f+h-1;
- if (!open(P,p.s)) rect.pb(p);
- } else if (com == "CLOSE") {
- int x,y; cin >> x >> y;
- pair<pi,pi> todo = {{x,x},{y,y}};
- int ind = -1;
- F0R(i,sz(rect)) if (inter(rect[i].f,todo)) ind = i;
- if (ind == -1) {
- BAD("no window at given position");
- continue;
- }
- rect.erase(begin(rect)+ind);
- } else if (com == "MOVE") {
- cin >> x >> y >> dx >> dy;
- if (dx > 0) {
- ad();
- } else if (dx < 0) {
- flipx();
- ad();
- flipx();
- } else if (dy > 0) {
- swapxy();
- ad();
- swapxy();
- } else if (dy < 0) {
- swapxy();
- flipx();
- ad();
- flipx();
- swapxy();
- }
- }
- }
- sort(all(rect),[](pair<pair<pi,pi>,int> a, pair<pair<pi,pi>,int> b) {
- return a.s < b.s;
- });
- cout << sz(rect) << " window(s):\n";
- trav(t,rect) {
- 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";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement