QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#456783#2266. Colorful Rectanglegarlic_MWTWA 58ms8340kbC++178.0kb2024-06-28 13:27:092024-06-28 13:27:10

Judging History

你现在查看的是最新测评结果

  • [2024-06-28 13:27:10]
  • 评测
  • 测评结果:WA
  • 用时:58ms
  • 内存:8340kb
  • [2024-06-28 13:27:09]
  • 提交

answer

#include <bits/stdc++.h>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
typedef long long ll;
const int INF = 5e8, MINF = -5e8, CEIL = 1e8;
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define each(x, a) for (auto& x: a)

#define debug if constexpr (xdebug) cout << "[DEBUG] "

#ifndef ONLINE_JUDGE
constexpr bool xdebug = true;
const int SZ = 1 << 4;
#else
constexpr bool xdebug = false;
const int SZ = 1 << 17;
#endif

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>

void solve(int testcase){
    int N;
    cin >> N;
    vector<int> x, y, c;
    vector<vector<int>> ymap(3);
    vector<ordered_multiset> y_vals(3);
    vector<int> pts_sorted, pts_sorted_revy;
    rep(i, 0, N) {
        int temp_x, temp_y, temp_c;
        cin >> temp_x >> temp_y >> temp_c;
        x.push_back(temp_x), y.push_back(temp_y), c.push_back(temp_c);
        ymap[temp_c].push_back(i);
        y_vals[temp_c].insert(temp_y);
        pts_sorted.push_back(i);
        pts_sorted_revy.push_back(i);
    }
    
    auto comp_ymap = [&y](int a, int b) {
        return y[a] < y[b];
    };
    auto comp_pts_sorted = [&x, &y](int a, int b) {
        if(x[a] != x[b]) return x[a] < x[b];
        else return y[a] < y[b];
    };
    auto comp_pts_sorted_revy = [&x, &y](int a, int b) {
        if(x[a] != x[b]) return x[a] < x[b];
        else return y[a] > y[b];
    };
    rep(i, 0, 3) sort(all(ymap[i]), comp_ymap);
    sort(all(pts_sorted), comp_pts_sorted);
    sort(all(pts_sorted_revy), comp_pts_sorted_revy);
    vector<int> ymap_idx(N);
    rep(i, 0, 3) rep(j, 0, ymap[i].size()) ymap_idx[ymap[i][j]] = j;
    if(xdebug) {
        rep(i, 0, 3) {
            debug << "ymap[" << i << "] :";
            each(item, ymap[i]) cout << " (" << x[item] << ", " << y[item] << ", " << c[item] << ')';
            cout << '\n';
        }
        debug << "pts_sorted :";
        each(item, pts_sorted) cout << " (" << x[item] << ", " << y[item] << ", " << c[item] << ')';
        cout << '\n';
    }
    
    int ans = INF;
    vector<int> x1_maxtree(SZ << 1, MINF), x1py2_maxtree(SZ << 1, MINF), y2_lazyprop(SZ << 1, MINF);
    auto prop = [&x1_maxtree, &x1py2_maxtree, &y2_lazyprop](int node) {
        if(y2_lazyprop[node] == MINF) return;
        x1py2_maxtree[node] = max(x1py2_maxtree[node], x1_maxtree[node] + y2_lazyprop[node]);
        if(node >= SZ) {y2_lazyprop[node] = MINF; return;}
        y2_lazyprop[node << 1] = max(y2_lazyprop[node << 1], y2_lazyprop[node]);
        y2_lazyprop[node << 1 | 1] = max(y2_lazyprop[node << 1 | 1], y2_lazyprop[node]);
        y2_lazyprop[node] = MINF;
    };
    auto update_x1 = [&x1_maxtree, &prop](int loc, int val) {
        int temp;
        stack<int> stk;
        for(temp = loc | SZ; temp; temp >>= 1) stk.push(temp);
        while(!stk.empty()) {
            temp = stk.top();
            stk.pop();
            prop(temp);
        }
        x1_maxtree[temp] = val;
        for(temp >>= 1; temp; temp >>= 1) {
            x1_maxtree[temp] = max(x1_maxtree[temp << 1], x1_maxtree[temp << 1 | 1]);
        }
    };
    auto update_x1py2_range = [&x1py2_maxtree, &y2_lazyprop, &prop](int loc, int val) {
        int node = 1;
        for(int r = SZ, step = SZ; loc + step != r; step >>= 1) {
            prop(node);
            if(loc + step > r) {node <<= 1; node |= 1; continue;}
            y2_lazyprop[node<<1|1] = max(y2_lazyprop[node<<1|1], val);
            prop(node << 1 | 1);
            r -= step, node <<= 1;
        }
        y2_lazyprop[node] = max(y2_lazyprop[node], val);
        prop(node);
        for(; node > 1; node >>= 1) {
            x1py2_maxtree[node>>1] = max(x1py2_maxtree[node>>1], x1py2_maxtree[node]);
        }
    };
    auto query_type1 = [&x1py2_maxtree, &prop](int loc) {
        int node = 1, ret = MINF;
        for(int l = 0, step = SZ; l + step != loc; step >>= 1) {
            prop(node);
            if(l + step > loc) {node <<= 1; continue;}
            prop(node << 1);
            ret = max(ret, x1py2_maxtree[node<<1]);
            l += step, node <<= 1;
            node |= 1;
        }
        prop(node);
        ret = max(ret, x1py2_maxtree[node]);
        return ret;
    };
    vector<int> x1py1_maxtree(SZ << 1, MINF);
    map<int, int> max_x1py1_under_y1;
    auto update_x1py1 = [&x1py1_maxtree](int loc, int val) {
        int temp = loc | SZ;
        x1py1_maxtree[temp] = val;
        for(temp >>= 1; temp; temp >>= 1) {
            x1py1_maxtree[temp] = max(x1py1_maxtree[temp<<1], x1py1_maxtree[temp<<1|1]);
        }
    };
    auto query_x1py1 = [&x1py1_maxtree](int loc) {
        int node = 1, ret = MINF;
        for(int l = 0, step = SZ; l + step != loc; step >>= 1) {
            if(l + step > loc) {node <<= 1; continue;}
            ret = max(ret, x1py1_maxtree[node<<1]);
            l += step, node <<= 1;
            node |= 1;
        }
        ret = max(ret, x1py1_maxtree[node]);
        return ret;
    };
    auto update_max_x1py1_under_y1 = [&max_x1py1_under_y1](int y, int val) {
        auto [ptr, succ] = max_x1py1_under_y1.emplace(y, val);
        if(!succ) ptr->second = val;
        for(ptr = next(ptr); ptr != max_x1py1_under_y1.end();) {
            if(ptr->second > val) break;
            ptr = next(ptr);
            max_x1py1_under_y1.erase(prev(ptr));
        }
    };
    auto query_type2 = [&max_x1py1_under_y1](int y) {
        auto ptr = max_x1py1_under_y1.upper_bound(y);
        if(ptr == max_x1py1_under_y1.begin()) return MINF;
        return prev(ptr)->second;
    };
    
    vector<int> c_ord = {0, 1, 2};
    do {
        bool x_rev = false;
        do {
            bool y_rev = false;
            do {
                int M = ymap[c_ord[1]].size();
                rep(it, 0, N) {
                    int idx = x_rev ? N - 1 - it : it;
                    int i = (x_rev ^ y_rev) ? pts_sorted_revy[idx] : pts_sorted[idx];
                    if(c[i] == c_ord[1]) {
                        update_x1(y_rev ? M - 1 - ymap_idx[i] : ymap_idx[i], x_rev ? CEIL - x[i] : x[i]);
                        if(!y_rev) update_x1py1(ymap_idx[i], (x_rev ? CEIL - x[i] : x[i]) + y[i]);
                    }
                    else {
                        int temp_idx = y_rev ? M - y_vals[c_ord[1]].order_of_key(y[i]) : y_vals[c_ord[1]].order_of_key(y[i] + 1);
                        if(c[i] == c_ord[0]) {
                            if(!temp_idx) continue;
                            int ret = query_type1(temp_idx);
                            if(!y_rev) ret = max(ret, query_type2(y[i]));
                            if(ret >= 0) ans = min(ans, (x_rev ? CEIL - x[i] : x[i]) + (y_rev ? CEIL - y[i] : y[i]) - ret);
                        }
                        else {
                            if(temp_idx != M) update_x1py2_range(temp_idx, y_rev ? CEIL - y[i] : y[i]);
                            if(!y_rev && temp_idx) update_max_x1py1_under_y1(y[i], query_x1py1(temp_idx));
                        }
                    }
                }
                debug << c_ord[0] << ' ' << c_ord[1] << ' ' << c_ord[2] << ' ' << x_rev << ' ' << y_rev << ' ' << ans << '\n';
                fill(all(x1_maxtree), MINF), fill(all(x1py2_maxtree), MINF), fill(all(y2_lazyprop), MINF);
                fill(all(x1py1_maxtree), MINF), max_x1py1_under_y1.clear();
                y_rev = !y_rev;
            }
            while(y_rev);
            x_rev = !x_rev;
        } while(x_rev);
    } while(next_permutation(all(c_ord)));
    
    cout << (ans << 1);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    clock_t st = clock();
    int t = 1;
    // cin >> t;
    rep(tc, 0, t) solve(tc);
    clock_t ed = clock();
    if(xdebug) {cout << '\n'; debug << ed - st << "ms\n";}
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 7496kb

input:

10
9991473 83825681 1
26476526 51616963 1
50765942 43355004 0
53028333 5604344 2
57100206 5782798 0
80628150 92688632 2
82964896 73929713 2
85102330 11439534 1
86076990 82286008 0
89626190 52420216 0

output:

75818374

result:

ok single line: '75818374'

Test #2:

score: 0
Accepted
time: 6ms
memory: 7548kb

input:

150
90758267 21234402 1
21737107 45944411 2
71064827 33646685 1
15732041 80099984 2
59366384 89336101 1
23463874 1772178 1
63300491 91439944 2
55016570 76385018 2
68263153 41801574 2
87098378 47936087 1
52162678 88263752 2
33679612 20590713 2
75242487 92720661 1
1669174 61465572 2
99532428 10613104 ...

output:

29171440

result:

ok single line: '29171440'

Test #3:

score: 0
Accepted
time: 5ms
memory: 7440kb

input:

10
4093976 78512657 0
27609174 62042588 1
31354091 61870386 0
35151441 36807411 1
37547440 25518220 0
44055162 7821981 2
66673981 41182270 0
83484785 58963611 1
83713705 24676214 2
88603397 80197017 0

output:

75778302

result:

ok single line: '75778302'

Test #4:

score: -100
Wrong Answer
time: 58ms
memory: 8340kb

input:

10000
12096 65562074 1
14562 60486739 1
20187 50242814 1
35859 51060918 0
50463 52231435 1
56216 44100657 2
68431 98864745 1
73973 62323865 1
74745 22912751 2
100382 29322594 2
106833 31933585 2
123956 66437573 2
124095 72164704 2
130151 80006173 1
149897 26940530 1
150544 42049736 2
154249 83266583...

output:

2618512

result:

wrong answer 1st lines differ - expected: '476190', found: '2618512'