답안 #751002

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
751002 2023-05-30T18:27:48 Z joelgun14 Crossing (JOI21_crossing) C++17
100 / 100
1403 ms 37620 KB
// header file
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// pragma
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// macros
#define endl "\n"
#define ll long long
#define mp make_pair
#define ins insert
#define lb lower_bound
#define pb push_back
#define ub upper_bound
#define lll __int128
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
string operate(string a, string b) {
    for(int it = 0; it < a.size(); ++it) {
        if(a[it] == b[it])
            continue;
        bool j = 0, o = 0, i = 0;
        if(a[it] == 'J')
            j = 1;
        else if(a[it] == 'O')
            o = 1;
        else
            i = 1;
        if(b[it] == 'J')
            j = 1;
        else if(b[it] == 'O')
            o = 1;
        else
            i = 1;
        if(!j)
            a[it] = 'J';
        else if(!o)
            a[it] = 'O';
        else
            a[it] = 'I';
    }
    return a;
}
int to_num(string s) {
    if(s == "J")
        return 0;
    else if(s == "O")
        return 1;
    else
        return 2;
}
int to_num(char x) {
    if(x == 'J')
        return 0;
    else if(x == 'O')
        return 1;
    else
        return 2;
}
const int lim = 2e5 + 5;
// buat bit 27 state 1 nya implicit disimpan di segment bawahnya
// remove segment bawahnya per segment gtu
// nanti cnt cari mutually exclusive segment
// char akhirnya berapa
// char 1, 2, 3, dan akhir
// 4 pertama -> quadruplet 1
// 4 kedua -> quadruplet 2
bool compatible[3][3][3][3][3][3][3][3];
bool possible[3][3][3][3];
int pref[lim][3][3][3];
int cnt[3][3][3][3];
vector<vector<int>> masks[3][3][3][3];
// precompute itu incompatible pairsnya gmn aja
int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    int n;
    cin >> n;
    // coba cek tiap possibility pengambilan
    // max pengambilan cmn 3?
    int e[n + 1][3];
    for(int i = 0; i < 3; ++i) {
        for(int j = 1; j <= n; ++j) {
            char x;
            cin >> x;
            e[j][i] = to_num(x);
        }
    }
    for(int i = 1; i <= n; ++i) {
        for(int j = 0; j < 3; ++j) {
            for(int k = 0; k < 3; ++k) {
                for(int l = 0; l < 3; ++l) {
                    pref[i][j][k][l] += pref[i - 1][j][k][l];
                }
            }
        }
        pref[i][e[i][0]][e[i][1]][e[i][2]]++;
    }
    int q;
    cin >> q;
    set<pair<pair<int, int>, int>> segments;
    for(int i = 1; i <= n; ++i) {
        char x;
        cin >> x;
        for(int j = 0; j < 3; ++j) {
            for(int k = 0; k < 3; ++k) {
                for(int l = 0; l < 3; ++l) {
                    cnt[j][k][l][to_num(x)] += pref[i][j][k][l] - pref[i - 1][j][k][l];
                }
            }
        }
        segments.insert(make_pair(make_pair(i, i), to_num(x)));
    }
    string choice[3] = {"J", "O", "I"};
    vector<vector<int>> triplets;
    for(int i = 0; i < 3; ++i) {
        for(int j = 0; j < 3; ++j) {
            for(int k = 0; k < 3; ++k) {
                triplets.push_back({i, j, k});
            }
        }
    }
    // utk tiap triple consider tiap pengambilan dengan triple itu sendiri, apa bisa buat char tertentu?
    for(auto p : triplets) {
        for(int i = 0; i < 3; ++i) {
            string tmp = choice[p[i]];
            for(int j = 0; j < 3; ++j) {
                tmp = operate(tmp, choice[p[j]]);
                for(int k = 0; k < 3; ++k) {
                    tmp = operate(tmp, choice[p[k]]);
                    possible[p[0]][p[1]][p[2]][to_num(tmp)] = 1;
                    masks[p[0]][p[1]][p[2]][to_num(tmp)].push_back({i, j, k});
                }
            }
        }
    }
    for(int trip = 0; trip < triplets.size(); ++trip) {
        for(int trip2 = 0; trip2 < triplets.size(); ++trip2) {
            vector<int> p1 = triplets[trip], p2 = triplets[trip2];
            for(int i = 0; i < 3; ++i) {
                string tmp1 = choice[p1[i]];
                string tmp2 = choice[p2[i]];
                for(int j = 0; j < 3; ++j) {
                    tmp1 = operate(tmp1, choice[p1[j]]);
                    tmp2 = operate(tmp2, choice[p2[j]]);
                    for(int k = 0; k < 3; ++k) {
                        tmp1 = operate(tmp1, choice[p1[k]]);
                        tmp2 = operate(tmp2, choice[p2[k]]);
                        compatible[p1[0]][p1[1]][p1[2]][to_num(tmp1)][p2[0]][p2[1]][p2[2]][to_num(tmp2)] = 1;
                    }
                }
            }
        }
    }
    // coba permutasi size 1, 2, 3
    // given same sequence tp resultnya beda -> pasti gabisa -> O(81Q) -> 1.6e7
    // klo misal ga ada yg beda, berarti max ada 27 aja
    // klo 27, nanti cek O(27^2*Q) -> 1.4e8
    // total O(81Q)
    bool ans = 1;
    vector<vector<int>> a;
    for(int i = 0; i < 3; ++i) {
        for(int j = 0; j < 3; ++j) {
            for(int k = 0; k < 3; ++k) {
                for(int l = 0; l < 3; ++l) {
                    if(cnt[i][j][k][l] && !possible[i][j][k][l])
                        ans = 0;
                    if(cnt[i][j][k][l])
                        a.push_back({i, j, k, l});
                }
            }
        }
    }
    int res[3][3][3];
    memset(res, 0, sizeof(res));
    for(int i = 0; i < a.size(); ++i) {
        for(auto j : masks[a[i][0]][a[i][1]][a[i][2]][a[i][3]]) {
            res[j[0]][j[1]][j[2]] += cnt[a[i][0]][a[i][1]][a[i][2]][a[i][3]];
        }
    }
    int mx = 0;
    for(int i = 0; i < 3; ++i) {
        for(int j = 0; j < 3; ++j) {
            for(int k = 0; k < 3; ++k)
                mx = max(mx, res[i][j][k]);
        }
    }
    //cout << mx << endl;
    if(ans && mx == n)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    while(q--) {
        // jalankan kode update
        int l, r; char x;
        cin >> l >> r >> x;
        while(segments.lower_bound(mp(make_pair(l, 0), 0)) != segments.end() && (*segments.lower_bound(mp(make_pair(l, 0), 0))).fi.se <= r) {
            pair<pair<int, int>, int> tmp = *segments.lower_bound(mp(mp(l, 0), 0));
            int cl = tmp.fi.se, cr = tmp.fi.fi, type = tmp.se;
            segments.erase(tmp);
            if(cl < l) {
                segments.ins(mp(mp(l - 1, cl), type));
            }
            if(cr > r) {
                segments.ins(mp(mp(cr, r + 1), type));
            }
            cl = max(l, cl);
            cr = min(r, cr);
            for(int i = 0; i < 3; ++i) {
                for(int j = 0; j < 3; ++j) {
                    for(int k = 0; k < 3; ++k) {
                        cnt[i][j][k][type] -= pref[cr][i][j][k] - pref[cl - 1][i][j][k];
                        cnt[i][j][k][to_num(x)] += pref[cr][i][j][k] - pref[cl - 1][i][j][k];
                        // cek mask yg bs buat i, j, k jadi type?
                    }
                }
            }
        }
        segments.ins(mp(mp(r, l), to_num(x)));
        // kode cek answer
        bool ans = 1;
        vector<vector<int>> a;
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < 3; ++j) {
                for(int k = 0; k < 3; ++k) {
                    for(int l = 0; l < 3; ++l) {
                        if(cnt[i][j][k][l] && !possible[i][j][k][l])
                            ans = 0;
                        if(cnt[i][j][k][l])
                            a.push_back({i, j, k, l});
                    }
                }
            }
        }
        if(!ans) {
            cout << "No" << endl;
            continue;
        }
        // cari subset yg compatiblenya terbanyak?
        int res[3][3][3];
        memset(res, 0, sizeof(res));
        for(int i = 0; i < a.size(); ++i) {
            for(auto j : masks[a[i][0]][a[i][1]][a[i][2]][a[i][3]]) {
                res[j[0]][j[1]][j[2]] += cnt[a[i][0]][a[i][1]][a[i][2]][a[i][3]];
            }
        }
        int mx = 0;
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < 3; ++j) {
                for(int k = 0; k < 3; ++k)
                    mx = max(mx, res[i][j][k]);
            }
        }
        if(ans && mx == n)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}

Compilation message

Main.cpp: In function 'std::string operate(std::string, std::string)':
Main.cpp:24:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int it = 0; it < a.size(); ++it) {
      |                     ~~~^~~~~~~~~~
Main.cpp:27:28: warning: variable 'i' set but not used [-Wunused-but-set-variable]
   27 |         bool j = 0, o = 0, i = 0;
      |                            ^
Main.cpp: In function 'int main()':
Main.cpp:141:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |     for(int trip = 0; trip < triplets.size(); ++trip) {
      |                       ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:142:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |         for(int trip2 = 0; trip2 < triplets.size(); ++trip2) {
      |                            ~~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:180:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  180 |     for(int i = 0; i < a.size(); ++i) {
      |                    ~~^~~~~~~~~~
Main.cpp:246:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  246 |         for(int i = 0; i < a.size(); ++i) {
      |                        ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 250 ms 928 KB Output is correct
2 Correct 257 ms 932 KB Output is correct
3 Correct 188 ms 924 KB Output is correct
4 Correct 237 ms 924 KB Output is correct
5 Correct 243 ms 1364 KB Output is correct
6 Correct 233 ms 1340 KB Output is correct
7 Correct 237 ms 1360 KB Output is correct
8 Correct 245 ms 1372 KB Output is correct
9 Correct 249 ms 1268 KB Output is correct
10 Correct 243 ms 1308 KB Output is correct
11 Correct 247 ms 1448 KB Output is correct
12 Correct 243 ms 1368 KB Output is correct
13 Correct 264 ms 1344 KB Output is correct
14 Correct 242 ms 1356 KB Output is correct
15 Correct 251 ms 1372 KB Output is correct
16 Correct 241 ms 1332 KB Output is correct
17 Correct 246 ms 1324 KB Output is correct
18 Correct 157 ms 1388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 250 ms 928 KB Output is correct
2 Correct 257 ms 932 KB Output is correct
3 Correct 188 ms 924 KB Output is correct
4 Correct 237 ms 924 KB Output is correct
5 Correct 243 ms 1364 KB Output is correct
6 Correct 233 ms 1340 KB Output is correct
7 Correct 237 ms 1360 KB Output is correct
8 Correct 245 ms 1372 KB Output is correct
9 Correct 249 ms 1268 KB Output is correct
10 Correct 243 ms 1308 KB Output is correct
11 Correct 247 ms 1448 KB Output is correct
12 Correct 243 ms 1368 KB Output is correct
13 Correct 264 ms 1344 KB Output is correct
14 Correct 242 ms 1356 KB Output is correct
15 Correct 251 ms 1372 KB Output is correct
16 Correct 241 ms 1332 KB Output is correct
17 Correct 246 ms 1324 KB Output is correct
18 Correct 157 ms 1388 KB Output is correct
19 Correct 461 ms 37424 KB Output is correct
20 Correct 399 ms 37312 KB Output is correct
21 Correct 615 ms 35184 KB Output is correct
22 Correct 541 ms 31696 KB Output is correct
23 Correct 364 ms 3276 KB Output is correct
24 Correct 362 ms 3164 KB Output is correct
25 Correct 629 ms 37488 KB Output is correct
26 Correct 596 ms 37488 KB Output is correct
27 Correct 509 ms 37360 KB Output is correct
28 Correct 562 ms 37368 KB Output is correct
29 Correct 509 ms 36472 KB Output is correct
30 Correct 345 ms 3300 KB Output is correct
31 Correct 519 ms 37364 KB Output is correct
32 Correct 533 ms 34144 KB Output is correct
33 Correct 368 ms 3312 KB Output is correct
34 Correct 557 ms 37324 KB Output is correct
35 Correct 554 ms 28308 KB Output is correct
36 Correct 358 ms 3116 KB Output is correct
37 Correct 364 ms 3144 KB Output is correct
38 Correct 306 ms 37324 KB Output is correct
39 Correct 471 ms 37420 KB Output is correct
40 Correct 551 ms 24880 KB Output is correct
41 Correct 518 ms 37452 KB Output is correct
42 Correct 370 ms 37512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 250 ms 928 KB Output is correct
2 Correct 257 ms 932 KB Output is correct
3 Correct 188 ms 924 KB Output is correct
4 Correct 237 ms 924 KB Output is correct
5 Correct 243 ms 1364 KB Output is correct
6 Correct 233 ms 1340 KB Output is correct
7 Correct 237 ms 1360 KB Output is correct
8 Correct 245 ms 1372 KB Output is correct
9 Correct 249 ms 1268 KB Output is correct
10 Correct 243 ms 1308 KB Output is correct
11 Correct 247 ms 1448 KB Output is correct
12 Correct 243 ms 1368 KB Output is correct
13 Correct 264 ms 1344 KB Output is correct
14 Correct 242 ms 1356 KB Output is correct
15 Correct 251 ms 1372 KB Output is correct
16 Correct 241 ms 1332 KB Output is correct
17 Correct 246 ms 1324 KB Output is correct
18 Correct 157 ms 1388 KB Output is correct
19 Correct 623 ms 1304 KB Output is correct
20 Correct 381 ms 1372 KB Output is correct
21 Correct 348 ms 1476 KB Output is correct
22 Correct 305 ms 1228 KB Output is correct
23 Correct 348 ms 1292 KB Output is correct
24 Correct 342 ms 1356 KB Output is correct
25 Correct 360 ms 1356 KB Output is correct
26 Correct 323 ms 1396 KB Output is correct
27 Correct 353 ms 1268 KB Output is correct
28 Correct 338 ms 1220 KB Output is correct
29 Correct 354 ms 1436 KB Output is correct
30 Correct 326 ms 1220 KB Output is correct
31 Correct 708 ms 1360 KB Output is correct
32 Correct 677 ms 1472 KB Output is correct
33 Correct 821 ms 1520 KB Output is correct
34 Correct 565 ms 1248 KB Output is correct
35 Correct 702 ms 1392 KB Output is correct
36 Correct 674 ms 1392 KB Output is correct
37 Correct 660 ms 1416 KB Output is correct
38 Correct 653 ms 1448 KB Output is correct
39 Correct 676 ms 1284 KB Output is correct
40 Correct 643 ms 1292 KB Output is correct
41 Correct 681 ms 1436 KB Output is correct
42 Correct 654 ms 1448 KB Output is correct
43 Correct 780 ms 1356 KB Output is correct
44 Correct 673 ms 1396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 250 ms 928 KB Output is correct
2 Correct 257 ms 932 KB Output is correct
3 Correct 188 ms 924 KB Output is correct
4 Correct 237 ms 924 KB Output is correct
5 Correct 243 ms 1364 KB Output is correct
6 Correct 233 ms 1340 KB Output is correct
7 Correct 237 ms 1360 KB Output is correct
8 Correct 245 ms 1372 KB Output is correct
9 Correct 249 ms 1268 KB Output is correct
10 Correct 243 ms 1308 KB Output is correct
11 Correct 247 ms 1448 KB Output is correct
12 Correct 243 ms 1368 KB Output is correct
13 Correct 264 ms 1344 KB Output is correct
14 Correct 242 ms 1356 KB Output is correct
15 Correct 251 ms 1372 KB Output is correct
16 Correct 241 ms 1332 KB Output is correct
17 Correct 246 ms 1324 KB Output is correct
18 Correct 157 ms 1388 KB Output is correct
19 Correct 461 ms 37424 KB Output is correct
20 Correct 399 ms 37312 KB Output is correct
21 Correct 615 ms 35184 KB Output is correct
22 Correct 541 ms 31696 KB Output is correct
23 Correct 364 ms 3276 KB Output is correct
24 Correct 362 ms 3164 KB Output is correct
25 Correct 629 ms 37488 KB Output is correct
26 Correct 596 ms 37488 KB Output is correct
27 Correct 509 ms 37360 KB Output is correct
28 Correct 562 ms 37368 KB Output is correct
29 Correct 509 ms 36472 KB Output is correct
30 Correct 345 ms 3300 KB Output is correct
31 Correct 519 ms 37364 KB Output is correct
32 Correct 533 ms 34144 KB Output is correct
33 Correct 368 ms 3312 KB Output is correct
34 Correct 557 ms 37324 KB Output is correct
35 Correct 554 ms 28308 KB Output is correct
36 Correct 358 ms 3116 KB Output is correct
37 Correct 364 ms 3144 KB Output is correct
38 Correct 306 ms 37324 KB Output is correct
39 Correct 471 ms 37420 KB Output is correct
40 Correct 551 ms 24880 KB Output is correct
41 Correct 518 ms 37452 KB Output is correct
42 Correct 370 ms 37512 KB Output is correct
43 Correct 623 ms 1304 KB Output is correct
44 Correct 381 ms 1372 KB Output is correct
45 Correct 348 ms 1476 KB Output is correct
46 Correct 305 ms 1228 KB Output is correct
47 Correct 348 ms 1292 KB Output is correct
48 Correct 342 ms 1356 KB Output is correct
49 Correct 360 ms 1356 KB Output is correct
50 Correct 323 ms 1396 KB Output is correct
51 Correct 353 ms 1268 KB Output is correct
52 Correct 338 ms 1220 KB Output is correct
53 Correct 354 ms 1436 KB Output is correct
54 Correct 326 ms 1220 KB Output is correct
55 Correct 708 ms 1360 KB Output is correct
56 Correct 677 ms 1472 KB Output is correct
57 Correct 821 ms 1520 KB Output is correct
58 Correct 565 ms 1248 KB Output is correct
59 Correct 702 ms 1392 KB Output is correct
60 Correct 674 ms 1392 KB Output is correct
61 Correct 660 ms 1416 KB Output is correct
62 Correct 653 ms 1448 KB Output is correct
63 Correct 676 ms 1284 KB Output is correct
64 Correct 643 ms 1292 KB Output is correct
65 Correct 681 ms 1436 KB Output is correct
66 Correct 654 ms 1448 KB Output is correct
67 Correct 780 ms 1356 KB Output is correct
68 Correct 673 ms 1396 KB Output is correct
69 Correct 1131 ms 31368 KB Output is correct
70 Correct 1280 ms 37456 KB Output is correct
71 Correct 480 ms 3196 KB Output is correct
72 Correct 474 ms 3244 KB Output is correct
73 Correct 475 ms 3148 KB Output is correct
74 Correct 645 ms 31252 KB Output is correct
75 Correct 456 ms 3116 KB Output is correct
76 Correct 749 ms 37340 KB Output is correct
77 Correct 644 ms 31476 KB Output is correct
78 Correct 464 ms 3180 KB Output is correct
79 Correct 460 ms 3192 KB Output is correct
80 Correct 1182 ms 26944 KB Output is correct
81 Correct 1055 ms 3212 KB Output is correct
82 Correct 1403 ms 37340 KB Output is correct
83 Correct 1282 ms 35212 KB Output is correct
84 Correct 1057 ms 3112 KB Output is correct
85 Correct 1051 ms 3056 KB Output is correct
86 Correct 1109 ms 28700 KB Output is correct
87 Correct 1246 ms 37536 KB Output is correct
88 Correct 1030 ms 3236 KB Output is correct
89 Correct 1173 ms 33032 KB Output is correct
90 Correct 1290 ms 37328 KB Output is correct
91 Correct 1036 ms 3144 KB Output is correct
92 Correct 1251 ms 28356 KB Output is correct
93 Correct 1044 ms 3116 KB Output is correct
94 Correct 1069 ms 3276 KB Output is correct
95 Correct 1075 ms 3184 KB Output is correct
96 Correct 333 ms 37296 KB Output is correct
97 Correct 1128 ms 37580 KB Output is correct
98 Correct 988 ms 24876 KB Output is correct
99 Correct 984 ms 37620 KB Output is correct
100 Correct 818 ms 37412 KB Output is correct