1782E - Rectangle Shrinking - CodeForces Solution


brute force data structures greedy implementation sortings *2300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define fp(i, a, b) for (int i = a, i##_ = int(b); i <= i##_; ++i)
#define fd(i, a, b) for (int i = a, i##_ = int(b); i >= i##_; --i)

using namespace std;
using ll = long long;
const int N = 2e5 + 5;
int n;
struct rtg {
    int l, r, u, d, id;
    bool operator<(const rtg &b)const {
        return l < b.l;
    }
} ans[N], rec[N], cur[3];
void Clear() {
    fp(i, 1, n)
        ans[i].l = ans[i].r = ans[i].u = ans[i].d = ans[i].id = 0;
    memset(cur, 0, sizeof(cur));
}
void Solve() {
    scanf("%d", &n);
    fp(i, 1, n)
        scanf("%d%d%d%d", &rec[i].d, &rec[i].l, &rec[i].u, &rec[i].r), rec[i].id = i;
    sort(rec + 1, rec + n + 1);
    fp(i, 1, n) {
        rec[i].l = max(rec[i].l, min(cur[1].r, cur[2].r) + 1);
        if (rec[i].r <= cur[1].r) rec[i].d = 2;
        if (rec[i].r <= cur[2].r) rec[i].u = 1;
        if (rec[i].l > rec[i].r || rec[i].d > rec[i].u) continue;
        fp(j, rec[i].d, rec[i].u) {
            if (cur[j].r >= rec[i].l) rec[cur[j].id].r = rec[i].l - 1;
            cur[j].r = rec[i].r, cur[j].id = i;
        }
    }
    int sum = 0;
    fp(i, 1, n) {
        int id = rec[i].id;
        if (rec[i].l <= rec[i].r && rec[i].d <= rec[i].u)
            ans[id] = rec[i], sum += (rec[i].u - rec[i].d + 1) * (rec[i].r - rec[i].l + 1);
    }
    printf("%d\n", sum);
    fp(i, 1, n) printf("%d %d %d %d\n", ans[i].d, ans[i].l, ans[i].u, ans[i].r);
}
int main() {
    int t = 1;
    scanf("%d", &t);
    while (t--) Solve(), Clear();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure