qiaobaaa / response3

0 stars 0 forks source link

Managing Rectangles #3

Open qiaobaaa opened 1 year ago

qiaobaaa commented 1 year ago

include

int timestamp;

struct Rectangle { int mid, timestamp; int sy, sx, ey, ex; int h, w; }rect[10001];

struct cmp { bool operator()( Rectangle a, Rectangle b) { return a->timestamp > b->timestamp; } };

std::set< Rectangle *, cmp> bucketimestampet[101][101];

void init(int N) { for (int i = 1; i <= 10000; i++) rect[i] = {}; for (int i = 0; i <= 100; i++) for (int j = 0; j <= 100; j++) bucketimestampet[i][j].clear(); }

void Erase(int sy, int ey, int sx, int ex, Rectangle * cur) { for (int i = sy; i <= ey; i++) for (int j = sx; j <= ex; j++) bucketimestampet[i][j].erase(cur); }

void addRect(int mID, int mY, int mX, int mHeight, int mWidth) { int sy = mY, sx = mX, ey = mY + mHeight - 1, ex = mX + mWidth - 1; rect[mID] = { mID, ++timestamp, sy, sx, ey, ex, mHeight, mWidth }; for (int i = sy / 100; i <= ey / 100; i++) for (int j = sx / 100; j <= ex / 100; j++) bucketimestampet[i][j].insert(&rect[mID]); }

int check(int mY, int mX) { for (auto cur : bucketimestampet[mY / 100][mX / 100]) { if (cur->sy <= mY && cur->ey >= mY && cur->sx <= mX && cur->ex >= mX) return cur->mid; } return 0; }

void selectAndMove(int y1, int x1, int y2, int x2) { int mID = check(y1, x1); if (!mID) return; Rectangle * cur = &rect[mID]; Erase(cur->sx / 100, cur->ey / 100, cur->sx / 100, cur->ex / 100, cur); rect[mID] = { mID, cur->timestamp, y2, x2, y2 + cur->h - 1, x2 + cur->w - 1, cur->h, cur->w }; for (int i = cur->sy / 100; i <= cur->ey / 100; i++) for (int j = cur->sx / 100; j <= cur->ex / 100; j++) bucketimestampet[i][j].insert(cur); }

int moveFront(int mID) { if (!rect[mID].mid || !rect[mID].timestamp) return 0; int sx = rect[mID].sx, sy = rect[mID].sy, ex = rect[mID].ex, ey = rect[mID].ey; Erase(sy / 100, ey / 100, sx / 100, ex / 100, &rect[mID]); addRect(mID, sy, sx, rect[mID].h, rect[mID].w);

int r = 0, rid = 0;
for (int i = rect[mID].sy / 100; i <= rect[mID].ey / 100; i++)
    for (int j = rect[mID].sx / 100; j <= rect[mID].ex / 100; j++)
        for (auto cur : bucketimestampet[i][j]) {
            if (&rect[mID] == cur) continue;
            if (rect[mID].sx <= cur->ex && rect[mID].sy <= cur->ey && rect[mID].ex >= cur->sx && rect[mID].ey >= cur->sy) {
                if (r < cur->timestamp) {
                    r = cur->timestamp;
                    rid = cur->mid;
                }
            }
        }
return rid;

}

int selectAndErase(int mY, int mX) { int mID = check(mY, mX); if (mID) { Erase(rect[mID].sy / 100, rect[mID].ey / 100, rect[mID].sx / 100, rect[mID].ex / 100, &rect[mID]); rect[mID].timestamp = 0; } return mID; }

qiaobaaa commented 1 year ago

include

int timestamp;

struct Rectangle { int mid, timestamp; int sy, sx, ey, ex; int h, w; }rect[10001];

struct cmp { bool operator()( Rectangle a, Rectangle b) { return a->timestamp > b->timestamp; } };

std::set< Rectangle *, cmp> bucketimestampet[101][101];

void init(int N) { for (int i = 1; i <= 10000; i++) rect[i] = {}; for (int i = 0; i <= 100; i++) for (int j = 0; j <= 100; j++) bucketimestampet[i][j].clear(); }

void Erase(int sy, int ey, int sx, int ex, Rectangle * cur) { for (int i = sy; i <= ey; i++) for (int j = sx; j <= ex; j++) bucketimestampet[i][j].erase(cur); }

void addRect(int mID, int mY, int mX, int mHeight, int mWidth) { int sy = mY, sx = mX, ey = mY + mHeight - 1, ex = mX + mWidth - 1; rect[mID] = { mID, ++timestamp, sy, sx, ey, ex, mHeight, mWidth }; for (int i = sy / 100; i <= ey / 100; i++) for (int j = sx / 100; j <= ex / 100; j++) bucketimestampet[i][j].insert(&rect[mID]); }

int check(int mY, int mX) { for (auto cur : bucketimestampet[mY / 100][mX / 100]) { if (cur->sy <= mY && cur->ey >= mY && cur->sx <= mX && cur->ex >= mX) return cur->mid; } return 0; }

void selectAndMove(int y1, int x1, int y2, int x2) { int mID = check(y1, x1); if (!mID) return; Rectangle * cur = &rect[mID]; Erase(cur->sx / 100, cur->ey / 100, cur->sx / 100, cur->ex / 100, cur); rect[mID] = { mID, cur->timestamp, y2, x2, y2 + cur->h - 1, x2 + cur->w - 1, cur->h, cur->w }; for (int i = cur->sy / 100; i <= cur->ey / 100; i++) for (int j = cur->sx / 100; j <= cur->ex / 100; j++) bucketimestampet[i][j].insert(cur); }

int moveFront(int mID) { if (!rect[mID].mid || !rect[mID].timestamp) return 0; int sx = rect[mID].sx, sy = rect[mID].sy, ex = rect[mID].ex, ey = rect[mID].ey; Erase(sy / 100, ey / 100, sx / 100, ex / 100, &rect[mID]); addRect(mID, sy, sx, rect[mID].h, rect[mID].w);

int r = 0, rid = 0;
for (int i = rect[mID].sy / 100; i <= rect[mID].ey / 100; i++)
    for (int j = rect[mID].sx / 100; j <= rect[mID].ex / 100; j++)
        for (auto cur : bucketimestampet[i][j]) {
            if (&rect[mID] == cur) continue;
            if (rect[mID].sx <= cur->ex && rect[mID].sy <= cur->ey && rect[mID].ex >= cur->sx && rect[mID].ey >= cur->sy) {
                if (r < cur->timestamp) {
                    r = cur->timestamp;
                    rid = cur->mid;
                }
            }
        }
return rid;

}

int selectAndErase(int mY, int mX) { int mID = check(mY, mX); if (mID) { Erase(rect[mID].sy / 100, rect[mID].ey / 100, rect[mID].sx / 100, rect[mID].ex / 100, &rect[mID]); rect[mID].timestamp = 0; } return mID; }