QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#457038#2266. Colorful Rectanglegarlic_MWTWA 522ms15316kbC++178.6kb2024-06-28 22:34:352024-06-28 22:34:36

Judging History

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

  • [2024-06-28 22:34:36]
  • 评测
  • 测评结果:WA
  • 用时:522ms
  • 内存:15316kb
  • [2024-06-28 22:34:35]
  • 提交

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 << 3;
#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) {
        if(!loc) {
            y2_lazyprop[1] = max(y2_lazyprop[1], val);
            prop(1);
            return;
        }
        int node = 1;
        for(int r = SZ, step = SZ >> 1; ; step >>= 1) {
            prop(node);
            if(loc + step >= r) {
                node <<= 1; node |= 1;
                if(loc + step == r) break;
                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) {
        // if(loc == SZ - 1) {
        //     prop(1);
        //     return x1py2_maxtree[1];
        // }
        int node = 1, ret = MINF;
        for(int l = 0, step = SZ >> 1; ; step >>= 1) {
            prop(node);
            if(l + step >= loc) {
                node <<= 1;
                if(l + step == loc) break;
                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) {
        // if(loc == SZ - 1) return x1py1_maxtree[1];
        int node = 1, ret = MINF;
        for(int l = 0, step = SZ >> 1; ; step >>= 1) {
            if(l + step >= loc) {
                node <<= 1;
                if(l + step == loc) break;
                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.lower_bound(y + 1);
        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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 7420kb

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: 7496kb

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: 0ms
memory: 7544kb

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: 0
Accepted
time: 57ms
memory: 8272kb

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:

476190

result:

ok single line: '476190'

Test #5:

score: 0
Accepted
time: 7ms
memory: 7428kb

input:

600
46864911 65058066 1
43812689 67844083 1
47624523 65356242 1
65763113 65439718 2
66870643 65931362 0
100000000 43232094 2
99773659 8651677 1
66502329 65775578 0
67130062 61467562 2
41297284 85249873 0
45570122 61586875 1
68626502 64903738 2
44727214 64595373 1
69383055 64974526 2
50960869 6495575...

output:

29384768

result:

ok single line: '29384768'

Test #6:

score: 0
Accepted
time: 78ms
memory: 8160kb

input:

10000
2177 6599212 0
3313 13493229 1
8624 80455572 2
10635 33135945 0
13266 17210177 0
21252 67913127 0
25630 44096615 0
26868 61301695 0
35959 34225877 2
40034 86139028 1
49019 16335976 0
56879 37023369 1
58406 27475381 2
65029 74490416 1
76280 94487503 0
78480 69430131 0
79030 23340728 0
79320 803...

output:

529732

result:

ok single line: '529732'

Test #7:

score: 0
Accepted
time: 177ms
memory: 11776kb

input:

60000
56904392 34119842 0
56860702 34345199 0
56863596 34223670 0
56833507 34167094 0
69029799 88014623 1
56701555 34308096 0
56818682 34376693 0
56834926 34330417 0
56949550 34257853 0
56684748 34297211 0
56900683 34127043 0
69073935 88044683 1
57002769 34170885 0
56645259 34209545 0
56949514 34101...

output:

214547970

result:

ok single line: '214547970'

Test #8:

score: 0
Accepted
time: 76ms
memory: 8132kb

input:

10000
5943 40201737 0
14879 96360008 1
84275 93764821 1
88316 65975310 2
92537 83169863 2
120913 54444955 1
122906 99179164 2
129216 52068348 1
138852 89877942 2
141123 97415909 1
155989 59760984 2
169805 6529653 2
170898 51961777 2
189693 18175483 1
198839 91060856 0
200187 19004619 0
207702 916481...

output:

392858

result:

ok single line: '392858'

Test #9:

score: 0
Accepted
time: 344ms
memory: 15144kb

input:

99999
65554710 12155679 0
78502621 86698549 1
66034198 11853186 0
78908439 86997239 1
75690302 59078887 2
75917845 58838788 2
78706695 87224574 1
75278920 59317406 2
75811752 58764827 2
65449462 11911322 0
79138515 86744879 1
65343865 11801595 0
65831220 12681966 0
78208917 86344984 1
78233596 86692...

output:

167810038

result:

ok single line: '167810038'

Test #10:

score: 0
Accepted
time: 76ms
memory: 8204kb

input:

10000
16453 91339702 2
17091 38182701 1
20936 29620225 2
47682 28151688 0
63221 76101571 0
65233 11392954 2
66284 74135634 2
86624 70956583 1
108068 98693936 1
114014 84167404 1
114265 18545899 1
120274 72079572 1
120373 96300499 2
123059 39784349 2
157658 37801703 0
170521 81004158 1
176351 5250039...

output:

352820

result:

ok single line: '352820'

Test #11:

score: 0
Accepted
time: 316ms
memory: 15316kb

input:

100000
36557383 100000000 1
53846533 5947338 0
24566151 89955356 2
23273552 91215468 2
36969432 98598695 1
26107898 89329321 2
53542808 6884678 0
37879687 99187185 1
52969807 5797175 0
53372131 4763020 0
36301035 100000000 1
53325023 6481729 0
37569840 100000000 1
24536890 89611841 2
24005966 893013...

output:

219386438

result:

ok single line: '219386438'

Test #12:

score: 0
Accepted
time: 77ms
memory: 8216kb

input:

10000
913 71695869 0
44727 80986550 2
53187 92127111 2
76157 87371194 1
80315 95062820 2
87064 48749409 1
87394 92128884 1
112372 46500690 2
120966 66525629 0
125652 92792157 1
136750 86862566 1
143481 96898726 2
151343 34996186 0
159085 62728567 2
164963 39895589 0
174996 82892797 2
184024 52133875...

output:

431976

result:

ok single line: '431976'

Test #13:

score: 0
Accepted
time: 505ms
memory: 14712kb

input:

97932
52284988 92138589 2
29206507 82522141 1
52909858 87999226 2
45692188 80211744 1
35493727 86238397 1
34822632 93781153 2
40346454 81255316 1
49468709 92105158 2
31027254 82650917 1
97094988 41517207 0
27125320 68205834 1
56699871 91310811 2
90235937 15919826 0
35696832 76242003 1
47376311 94592...

output:

60926458

result:

ok single line: '60926458'

Test #14:

score: 0
Accepted
time: 78ms
memory: 8128kb

input:

10000
4121 45276640 2
35568 51336364 0
47620 84075629 1
63330 76418369 0
63958 45357799 1
79664 74393497 0
88739 8368773 1
98705 64915375 0
100114 16259520 1
108749 92262958 0
116309 59859481 0
125921 4595658 2
131545 17676231 2
146925 83052695 0
154543 16882767 1
166321 43160274 0
174342 3499817 0
...

output:

506892

result:

ok single line: '506892'

Test #15:

score: 0
Accepted
time: 522ms
memory: 14932kb

input:

100000
39798904 68828678 2
56937242 58014952 0
56758306 57451114 0
38746499 51964627 2
56532042 58143844 0
52939689 63344810 2
45049586 65171055 2
20057743 3432569 1
51529728 57602014 0
62773199 68566788 2
51377179 65794879 2
34174168 63457598 2
52814256 69625370 2
58610225 67089086 2
56575100 58165...

output:

34188812

result:

ok single line: '34188812'

Test #16:

score: 0
Accepted
time: 78ms
memory: 8292kb

input:

10000
5737 37220377 0
5803 36404658 2
9249 6747714 2
25732 55571376 1
61246 30922073 0
61836 42608265 1
81477 70348885 0
107589 8738354 0
111307 8040637 2
143560 54882437 2
152517 76825039 2
176017 43369316 0
180092 87823847 0
184947 94495058 0
197882 37492026 0
217517 77700615 2
222701 67543247 1
2...

output:

181554

result:

ok single line: '181554'

Test #17:

score: 0
Accepted
time: 380ms
memory: 15156kb

input:

100000
17622444 8000033 0
30835380 8000033 1
52663741 8000033 0
70031205 8000033 0
54407389 8000033 2
82444091 8000033 1
79610268 8000033 1
53930150 8000033 0
28541791 8000033 2
43252170 8000033 2
53546829 8000033 2
3516948 8000033 0
51935542 8000033 1
55023622 8000033 0
65885269 8000033 2
89745547 ...

output:

40

result:

ok single line: '40'

Test #18:

score: -100
Wrong Answer
time: 78ms
memory: 8128kb

input:

10000
797 75327956 0
1739 7223559 1
9535 32305593 0
11449 21913826 0
37076 67597268 1
44781 99118529 2
46837 19559579 1
53721 80491438 1
58502 76311712 0
62494 46799879 0
66912 46495165 2
72642 37596193 0
95419 70508156 0
98774 68665733 0
99049 63085873 1
100851 60379638 1
102269 54262558 0
124039 4...

output:

621566

result:

wrong answer 1st lines differ - expected: '234328', found: '621566'