답안 #1084204

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084204 2024-09-05T15:42:23 Z yoav_s Crossing (JOI21_crossing) C++17
100 / 100
3021 ms 31996 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef pair<ll, ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;

#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()

const ll INF = 1e18;
const ll mod = 1e9 + 7;

v process(v &a, v &b)
{
    ll N = a.size();
    v res(N);
    for (ll i = 0; i < N; i++)
    {
        if (a[i] == b[i]) res[i] = a[i];
        else res[i] = 3 - a[i] - b[i];
    }
    return res;
}

ll toNumber(v &a)
{
    ll cur = 1;
    ll res = 0;
    for (ll x: a)
    {
        res += x * cur;
        cur *= 3;
    }
    return res;
}

vb getReachable(vp &classes)
{
    ll n = classes.size();
    vv reachable;
    reachable.pb(v(n, 0));
    v a(n), b(n);
    for (ll i = 0; i < n; i++)
    {
        a[i] = classes[i].f;
        b[i] = classes[i].s;
    }
    reachable.pb(a);
    reachable.pb(b);
    ll maxi = 1;
    for (ll i = 0; i < n; i++) maxi *= 3;
    vb visited(maxi, false);
    for (auto x : reachable) visited[toNumber(x)] = true;
    for (ll i = 0; i < reachable.size(); i++)
    {
        for (ll j = 0; j < i; j++)
        {
            v cur = process(reachable[i], reachable[j]);
            if (visited[toNumber(cur)]) continue;
            visited[toNumber(cur)] = true;
            reachable.pb(cur);
        }
    }
    return visited;
}

ll getAmt(ll l, ll r, v &arr)
{
    return upper_bound(all(arr), r) - lower_bound(all(arr), l);
}

vv getInvalid(ll l, ll r, vvv &classExpectation, ll equalTo)
{
    ll classCnt = classExpectation.size();
    vv res(classCnt, v(3, 0));
    for (ll i = 0; i < classCnt; i++)
    {
        v actAmt(3);
        for (ll j = 0; j < 3; j++) actAmt[j] = getAmt(l, r, classExpectation[i][j]);
        for (ll j = 0; j < 3; j++)
        {
            for (ll k = 0; k < 3; k++)
            {
                if (j != (equalTo + k) % 3) res[i][j] += actAmt[k];
            }
        }
    }
    return res;
}

void add(vv &a, vv b)
{
    for (ll i = 0; i < a.size(); i++)
    {
        for (ll j = 0; j < a[0].size(); j++)
        {
            a[i][j] += b[i][j];
        }
    }
}

void substract(vv &a, vv b)
{
    for (ll i = 0; i < a.size(); i++)
    {
        for (ll j = 0; j < a[0].size(); j++)
        {
            a[i][j] -= b[i][j];
        }
    }
}

bool checkValidity(vv &notMatchingCnt, vb &reachable)
{
    ll n = notMatchingCnt.size();
    v chosen(n, -1);
    for (ll i = 0; i < n; i++)
    {
        for (ll j = 0; j < 3; j++) if (notMatchingCnt[i][j] == 0) chosen[i] = j;
        if (chosen[i] == -1) return false;
    }
    return reachable[toNumber(chosen)];
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll N;
    cin >> N;
    string sA, sB, sC;
    cin >> sA >> sB >> sC;
    map<char, ll> conv{{'J',0},{'O',1},{'I',2}};
    v a(N), b(N), c(N);
    for (ll i = 0; i < N; i++)
    {
        a[i] = conv[sA[i]];
        b[i] = conv[sB[i]];
        c[i] = conv[sC[i]];
    }
    v aNormalized(N), bNormalized(N), cNormalized(N);
    v offset(N);
    for (ll i = 0; i < N; i++)
    {
        aNormalized[i] = 0;
        bNormalized[i] = (b[i] - a[i] + 3) % 3;
        cNormalized[i] = (c[i] - a[i] + 3) % 3;
        offset[i] = (3 - a[i]) % 3;
    }
    map<p, ll> toClass;
    vp classes;
    ll cur = 0;
    for (ll i = 0; i < N; i++)
    {
        p c(bNormalized[i], cNormalized[i]);
        if (toClass.count(c) > 0) continue;
        toClass[c] = cur++;
        classes.pb(c);
    }
    ll classCnt = cur;
    v myClass(N);
    vv byClass(classCnt);
    for (ll i = 0; i < N; i++)
    {
        myClass[i] = toClass[p(bNormalized[i], cNormalized[i])];
        byClass[myClass[i]].pb(i);
    }
    vb reachable = getReachable(classes);
    vvv classExpectation(classCnt, vv(3));
    for (ll i = 0; i < N; i++)
    {
        classExpectation[myClass[i]][offset[i]].pb(i);
    }
    ll Q; cin >> Q;
    string scmp; cin >> scmp;
    v cmp(N);
    for (ll i = 0; i < N; i++) cmp[i] = conv[scmp[i]];
    vector<set<p>> ranges(3);
    ll last = 0;
    for (ll i = 1; i < N; i++)
    {
        if (cmp[i] != cmp[last])
        {
            ranges[cmp[last]].emplace(last, i - 1);
            last = i;
        }
    }
    ranges[cmp[last]].emplace(last, N - 1);
    vv notMatchingCnt(classCnt, v(3, 0));
    for (ll i = 0; i < N; i++)
    {
        for (ll j = 0; j < 3; j++)
        {
            if (j != (cmp[i] + offset[i]) % 3) notMatchingCnt[myClass[i]][j]++;
        }
    }
    bool isValid = checkValidity(notMatchingCnt, reachable);
    if (isValid) cout << "Yes\n";
    else cout << "No\n";
    while (Q--)
    {
        ll l, r;
        char asChar;
        cin >> l >> r >> asChar; l--; r--;
        ll val = conv[asChar];
        for (ll i = 0; i < 3; i++)
        {
            auto ptr = ranges[i].lower_bound(p(l, -INF));
            if (ptr != ranges[i].begin())
            {
                ptr--;
                if ((*ptr).s > r)
                {
                    substract(notMatchingCnt, getInvalid(l, r, classExpectation, i));
                    ll start=  (*ptr).f;
                    ll end = (*ptr).s;
                    ranges[i].erase(ptr);
                    ranges[i].emplace(start, l - 1);
                    ranges[i].emplace(r + 1, end);
                    continue;
                }
                else if ((*ptr).s >= l)
                {
                    substract(notMatchingCnt, getInvalid(l, (*ptr).s, classExpectation, i));
                    ll start = (*ptr).f;
                    ranges[i].erase(ptr);
                    ranges[i].emplace(start, l - 1);
                }
            }
            while (true)
            {
                auto ptr = ranges[i].lower_bound(p(l, -INF));
                if (ptr == ranges[i].end() || (*ptr).f > r) break;
                substract(notMatchingCnt, getInvalid((*ptr).f, min((*ptr).s, r), classExpectation, i));
                if ((*ptr).s > r)
                {
                    ll e = (*ptr).s;
                    ranges[i].erase(ptr);
                    ranges[i].emplace(r + 1, e);
                    break;
                }
                else
                {
                    ranges[i].erase(ptr);
                }
            }
        }
        ranges[val].emplace(l, r);
        add(notMatchingCnt, getInvalid(l, r, classExpectation, val));
        bool isValid = checkValidity(notMatchingCnt, reachable);
        if (isValid) cout << "Yes\n";
        else cout << "No\n";
    }
}

Compilation message

Main.cpp: In function 'vb getReachable(vp&)':
Main.cpp:71:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for (ll i = 0; i < reachable.size(); i++)
      |                    ~~^~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void add(vv&, vv)':
Main.cpp:110:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |     for (ll i = 0; i < a.size(); i++)
      |                    ~~^~~~~~~~~~
Main.cpp:112:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |         for (ll j = 0; j < a[0].size(); j++)
      |                        ~~^~~~~~~~~~~~~
Main.cpp: In function 'void substract(vv&, vv)':
Main.cpp:121:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |     for (ll i = 0; i < a.size(); i++)
      |                    ~~^~~~~~~~~~
Main.cpp:123:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |         for (ll j = 0; j < a[0].size(); j++)
      |                        ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 183 ms 2276 KB Output is correct
2 Correct 205 ms 2384 KB Output is correct
3 Correct 152 ms 2404 KB Output is correct
4 Correct 178 ms 2388 KB Output is correct
5 Correct 178 ms 2384 KB Output is correct
6 Correct 177 ms 2384 KB Output is correct
7 Correct 194 ms 2384 KB Output is correct
8 Correct 186 ms 2384 KB Output is correct
9 Correct 186 ms 2384 KB Output is correct
10 Correct 184 ms 2348 KB Output is correct
11 Correct 191 ms 2384 KB Output is correct
12 Correct 185 ms 2384 KB Output is correct
13 Correct 189 ms 2384 KB Output is correct
14 Correct 183 ms 2384 KB Output is correct
15 Correct 188 ms 2388 KB Output is correct
16 Correct 206 ms 2424 KB Output is correct
17 Correct 190 ms 2640 KB Output is correct
18 Correct 129 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 183 ms 2276 KB Output is correct
2 Correct 205 ms 2384 KB Output is correct
3 Correct 152 ms 2404 KB Output is correct
4 Correct 178 ms 2388 KB Output is correct
5 Correct 178 ms 2384 KB Output is correct
6 Correct 177 ms 2384 KB Output is correct
7 Correct 194 ms 2384 KB Output is correct
8 Correct 186 ms 2384 KB Output is correct
9 Correct 186 ms 2384 KB Output is correct
10 Correct 184 ms 2348 KB Output is correct
11 Correct 191 ms 2384 KB Output is correct
12 Correct 185 ms 2384 KB Output is correct
13 Correct 189 ms 2384 KB Output is correct
14 Correct 183 ms 2384 KB Output is correct
15 Correct 188 ms 2388 KB Output is correct
16 Correct 206 ms 2424 KB Output is correct
17 Correct 190 ms 2640 KB Output is correct
18 Correct 129 ms 2388 KB Output is correct
19 Correct 629 ms 31600 KB Output is correct
20 Correct 534 ms 31588 KB Output is correct
21 Correct 654 ms 29228 KB Output is correct
22 Correct 644 ms 26940 KB Output is correct
23 Correct 434 ms 4688 KB Output is correct
24 Correct 413 ms 4560 KB Output is correct
25 Correct 643 ms 31600 KB Output is correct
26 Correct 733 ms 31976 KB Output is correct
27 Correct 542 ms 23628 KB Output is correct
28 Correct 575 ms 24176 KB Output is correct
29 Correct 505 ms 22900 KB Output is correct
30 Correct 376 ms 4432 KB Output is correct
31 Correct 517 ms 23548 KB Output is correct
32 Correct 563 ms 21772 KB Output is correct
33 Correct 411 ms 4608 KB Output is correct
34 Correct 637 ms 31096 KB Output is correct
35 Correct 622 ms 24228 KB Output is correct
36 Correct 410 ms 4688 KB Output is correct
37 Correct 424 ms 4692 KB Output is correct
38 Correct 279 ms 30704 KB Output is correct
39 Correct 276 ms 31716 KB Output is correct
40 Correct 538 ms 21864 KB Output is correct
41 Correct 350 ms 31164 KB Output is correct
42 Correct 141 ms 30456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 183 ms 2276 KB Output is correct
2 Correct 205 ms 2384 KB Output is correct
3 Correct 152 ms 2404 KB Output is correct
4 Correct 178 ms 2388 KB Output is correct
5 Correct 178 ms 2384 KB Output is correct
6 Correct 177 ms 2384 KB Output is correct
7 Correct 194 ms 2384 KB Output is correct
8 Correct 186 ms 2384 KB Output is correct
9 Correct 186 ms 2384 KB Output is correct
10 Correct 184 ms 2348 KB Output is correct
11 Correct 191 ms 2384 KB Output is correct
12 Correct 185 ms 2384 KB Output is correct
13 Correct 189 ms 2384 KB Output is correct
14 Correct 183 ms 2384 KB Output is correct
15 Correct 188 ms 2388 KB Output is correct
16 Correct 206 ms 2424 KB Output is correct
17 Correct 190 ms 2640 KB Output is correct
18 Correct 129 ms 2388 KB Output is correct
19 Correct 577 ms 2556 KB Output is correct
20 Correct 491 ms 2448 KB Output is correct
21 Correct 279 ms 2428 KB Output is correct
22 Correct 251 ms 2384 KB Output is correct
23 Correct 286 ms 2388 KB Output is correct
24 Correct 270 ms 2388 KB Output is correct
25 Correct 281 ms 2492 KB Output is correct
26 Correct 254 ms 2332 KB Output is correct
27 Correct 315 ms 2384 KB Output is correct
28 Correct 260 ms 2388 KB Output is correct
29 Correct 288 ms 2644 KB Output is correct
30 Correct 242 ms 2388 KB Output is correct
31 Correct 545 ms 2516 KB Output is correct
32 Correct 503 ms 2440 KB Output is correct
33 Correct 537 ms 2388 KB Output is correct
34 Correct 472 ms 2408 KB Output is correct
35 Correct 524 ms 2384 KB Output is correct
36 Correct 538 ms 2444 KB Output is correct
37 Correct 518 ms 2520 KB Output is correct
38 Correct 530 ms 2380 KB Output is correct
39 Correct 523 ms 2536 KB Output is correct
40 Correct 532 ms 2644 KB Output is correct
41 Correct 529 ms 2396 KB Output is correct
42 Correct 525 ms 2556 KB Output is correct
43 Correct 496 ms 2392 KB Output is correct
44 Correct 537 ms 2416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 183 ms 2276 KB Output is correct
2 Correct 205 ms 2384 KB Output is correct
3 Correct 152 ms 2404 KB Output is correct
4 Correct 178 ms 2388 KB Output is correct
5 Correct 178 ms 2384 KB Output is correct
6 Correct 177 ms 2384 KB Output is correct
7 Correct 194 ms 2384 KB Output is correct
8 Correct 186 ms 2384 KB Output is correct
9 Correct 186 ms 2384 KB Output is correct
10 Correct 184 ms 2348 KB Output is correct
11 Correct 191 ms 2384 KB Output is correct
12 Correct 185 ms 2384 KB Output is correct
13 Correct 189 ms 2384 KB Output is correct
14 Correct 183 ms 2384 KB Output is correct
15 Correct 188 ms 2388 KB Output is correct
16 Correct 206 ms 2424 KB Output is correct
17 Correct 190 ms 2640 KB Output is correct
18 Correct 129 ms 2388 KB Output is correct
19 Correct 629 ms 31600 KB Output is correct
20 Correct 534 ms 31588 KB Output is correct
21 Correct 654 ms 29228 KB Output is correct
22 Correct 644 ms 26940 KB Output is correct
23 Correct 434 ms 4688 KB Output is correct
24 Correct 413 ms 4560 KB Output is correct
25 Correct 643 ms 31600 KB Output is correct
26 Correct 733 ms 31976 KB Output is correct
27 Correct 542 ms 23628 KB Output is correct
28 Correct 575 ms 24176 KB Output is correct
29 Correct 505 ms 22900 KB Output is correct
30 Correct 376 ms 4432 KB Output is correct
31 Correct 517 ms 23548 KB Output is correct
32 Correct 563 ms 21772 KB Output is correct
33 Correct 411 ms 4608 KB Output is correct
34 Correct 637 ms 31096 KB Output is correct
35 Correct 622 ms 24228 KB Output is correct
36 Correct 410 ms 4688 KB Output is correct
37 Correct 424 ms 4692 KB Output is correct
38 Correct 279 ms 30704 KB Output is correct
39 Correct 276 ms 31716 KB Output is correct
40 Correct 538 ms 21864 KB Output is correct
41 Correct 350 ms 31164 KB Output is correct
42 Correct 141 ms 30456 KB Output is correct
43 Correct 577 ms 2556 KB Output is correct
44 Correct 491 ms 2448 KB Output is correct
45 Correct 279 ms 2428 KB Output is correct
46 Correct 251 ms 2384 KB Output is correct
47 Correct 286 ms 2388 KB Output is correct
48 Correct 270 ms 2388 KB Output is correct
49 Correct 281 ms 2492 KB Output is correct
50 Correct 254 ms 2332 KB Output is correct
51 Correct 315 ms 2384 KB Output is correct
52 Correct 260 ms 2388 KB Output is correct
53 Correct 288 ms 2644 KB Output is correct
54 Correct 242 ms 2388 KB Output is correct
55 Correct 545 ms 2516 KB Output is correct
56 Correct 503 ms 2440 KB Output is correct
57 Correct 537 ms 2388 KB Output is correct
58 Correct 472 ms 2408 KB Output is correct
59 Correct 524 ms 2384 KB Output is correct
60 Correct 538 ms 2444 KB Output is correct
61 Correct 518 ms 2520 KB Output is correct
62 Correct 530 ms 2380 KB Output is correct
63 Correct 523 ms 2536 KB Output is correct
64 Correct 532 ms 2644 KB Output is correct
65 Correct 529 ms 2396 KB Output is correct
66 Correct 525 ms 2556 KB Output is correct
67 Correct 496 ms 2392 KB Output is correct
68 Correct 537 ms 2416 KB Output is correct
69 Correct 2988 ms 27168 KB Output is correct
70 Correct 3021 ms 31292 KB Output is correct
71 Correct 757 ms 4688 KB Output is correct
72 Correct 775 ms 4812 KB Output is correct
73 Correct 700 ms 4688 KB Output is correct
74 Correct 1098 ms 26580 KB Output is correct
75 Correct 693 ms 4684 KB Output is correct
76 Correct 1292 ms 31508 KB Output is correct
77 Correct 1216 ms 27264 KB Output is correct
78 Correct 722 ms 4668 KB Output is correct
79 Correct 761 ms 4852 KB Output is correct
80 Correct 2511 ms 23216 KB Output is correct
81 Correct 1407 ms 4688 KB Output is correct
82 Correct 2601 ms 31420 KB Output is correct
83 Correct 2722 ms 30328 KB Output is correct
84 Correct 1379 ms 5080 KB Output is correct
85 Correct 1376 ms 4664 KB Output is correct
86 Correct 1970 ms 24648 KB Output is correct
87 Correct 2267 ms 23620 KB Output is correct
88 Correct 1330 ms 4620 KB Output is correct
89 Correct 2065 ms 21708 KB Output is correct
90 Correct 2256 ms 24576 KB Output is correct
91 Correct 1306 ms 4560 KB Output is correct
92 Correct 2164 ms 24808 KB Output is correct
93 Correct 1329 ms 4688 KB Output is correct
94 Correct 1345 ms 4844 KB Output is correct
95 Correct 1366 ms 4948 KB Output is correct
96 Correct 239 ms 22600 KB Output is correct
97 Correct 482 ms 31708 KB Output is correct
98 Correct 1940 ms 22056 KB Output is correct
99 Correct 1644 ms 31996 KB Output is correct
100 Correct 510 ms 30968 KB Output is correct