답안 #939385

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
939385 2024-03-06T10:15:40 Z vjudge1 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
921 ms 54580 KB
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #include <x86intrin.h>

#include <bits/stdc++.h>
#include <chrono>
#include <random>

// @author: Vlapos

namespace operators
{
    template <typename T1, typename T2>
    std::istream &operator>>(std::istream &in, std::pair<T1, T2> &x)
    {
        in >> x.first >> x.second;
        return in;
    }

    template <typename T1, typename T2>
    std::ostream &operator<<(std::ostream &out, std::pair<T1, T2> x)
    {
        out << x.first << " " << x.second;
        return out;
    }

    template <typename T1>
    std::istream &operator>>(std::istream &in, std::vector<T1> &x)
    {
        for (auto &i : x)
            in >> i;
        return in;
    }

    template <typename T1>
    std::ostream &operator<<(std::ostream &out, std::vector<T1> &x)
    {
        for (auto &i : x)
            out << i << " ";
        return out;
    }

    template <typename T1>
    std::ostream &operator<<(std::ostream &out, std::set<T1> &x)
    {
        for (auto &i : x)
            out << i << " ";
        return out;
    }
}

// name spaces
using namespace std;
using namespace operators;
// end of name spaces

// defines
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define uint unsigned int
#define all(vc) vc.begin(), vc.end()
// end of defines

// usefull stuff

void boost()
{
    ios_base ::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

inline int getbit(int &x, int &bt) { return (x >> bt) & 1; }

const int dx4[4] = {-1, 0, 0, 1};
const int dy4[4] = {0, -1, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[8] = {-1, -0, 1, -1, 1, -1, 0, 1};

const ll INF = (1e18) + 500;
const int BIG = (1e9) * 2 + 100;
const int MAXN = (1e5) + 5;
const int MOD7 = (1e9) + 7;
const int MOD9 = (1e9) + 9;
const uint MODFFT = 998244353;

#define int ll

struct test
{
    void solve(int testcase)
    {
        boost();

        int n, q;
        cin >> n >> q;
        int mxBit = n;
        n = 1 << n;
        vector<int> cost(n);

        vector<int> overMask(n), subMask(n);
        for (int i = 0; i < n; ++i)
        {
            char x;
            cin >> x;
            cost[i] = x - '0';
            overMask[i] = subMask[i] = cost[i];
        }

        for (int i = 0; i < mxBit; ++i)
        {
            for (int mask = n - 1; mask > 0; --mask)
                if (((mask >> i) & 1))
                    subMask[mask] += subMask[mask ^ (1 << i)];

            for (int mask = 0; mask < n; ++mask)
                if (!((mask >> i) & 1))
                    overMask[mask] += overMask[mask ^ (1 << i)];
        }

        // for (int i = 0; i < n; ++i)
        //     cout << i << ": " << overMask[i] << "\n";

        for (int i = 0; i < q; ++i)
        {
            int mask0 = 0, mask1 = 0, maskq = 0;
            int c0 = 0, c1 = 0, cq = 0;

            string T;
            cin >> T;
            reverse(all(T));
            for (int p = 0; p < mxBit; ++p)
            {
                char x = T[p];
                if (x == '0')
                    c0++, mask0 += (1 << p);
                if (x == '1')
                    c1++, mask1 += (1 << p);
                if (x == '?')
                    cq++, maskq += (1 << p);
            }
            int res = 0;

            if (min({c0, c1, cq}) == cq)
            {
                for (int mask = maskq;; mask = (mask - 1) & maskq)
                {
                    res += cost[mask1 | mask];

                    if (mask == 0)
                        break;
                }
            }
            else if (min({c0, c1, cq}) == c0)
            {
                for (int mask = mask0;; mask = (mask - 1) & mask0)
                {
                    res += ((__builtin_popcount(mask) % 2) ? -1 : 1) * overMask[mask | mask1];

                    if (mask == 0)
                        break;
                }
            }
            else
            {
                int cur = mask1 | maskq;
                for (int mask = mask1;; mask = (mask - 1) & mask1)
                {
                    res += ((__builtin_popcount(mask) % 2) ? -1 : 1) * subMask[cur ^ mask];

                    if (mask == 0)
                        break;
                }
            }

            cout << res << '\n';
        }
    }
};

main()
{
    boost();
    int q = 1;
    // cin >> q;
    for (int i = 0; i < q; i++)
    {
        test t;
        t.solve(i);
    }
    return 0;
}
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
//                                                                                    //
//                               Coded by Der_Vlἀpos                                  //
//                                                                                    //
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//

Compilation message

snake_escaping.cpp:187:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  187 | main()
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 179 ms 15220 KB Output is correct
12 Correct 176 ms 14664 KB Output is correct
13 Correct 196 ms 13908 KB Output is correct
14 Correct 233 ms 14212 KB Output is correct
15 Correct 183 ms 15312 KB Output is correct
16 Correct 206 ms 14364 KB Output is correct
17 Correct 206 ms 14164 KB Output is correct
18 Correct 138 ms 16284 KB Output is correct
19 Correct 163 ms 13120 KB Output is correct
20 Correct 181 ms 14732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 179 ms 15220 KB Output is correct
12 Correct 176 ms 14664 KB Output is correct
13 Correct 196 ms 13908 KB Output is correct
14 Correct 233 ms 14212 KB Output is correct
15 Correct 183 ms 15312 KB Output is correct
16 Correct 206 ms 14364 KB Output is correct
17 Correct 206 ms 14164 KB Output is correct
18 Correct 138 ms 16284 KB Output is correct
19 Correct 163 ms 13120 KB Output is correct
20 Correct 181 ms 14732 KB Output is correct
21 Correct 195 ms 18144 KB Output is correct
22 Correct 197 ms 18260 KB Output is correct
23 Correct 233 ms 17428 KB Output is correct
24 Correct 214 ms 17296 KB Output is correct
25 Correct 218 ms 19228 KB Output is correct
26 Correct 244 ms 17764 KB Output is correct
27 Correct 241 ms 17616 KB Output is correct
28 Correct 146 ms 20276 KB Output is correct
29 Correct 182 ms 16208 KB Output is correct
30 Correct 209 ms 18256 KB Output is correct
31 Correct 236 ms 18256 KB Output is correct
32 Correct 267 ms 18260 KB Output is correct
33 Correct 203 ms 17268 KB Output is correct
34 Correct 240 ms 17300 KB Output is correct
35 Correct 247 ms 17880 KB Output is correct
36 Correct 146 ms 16212 KB Output is correct
37 Correct 185 ms 18216 KB Output is correct
38 Correct 194 ms 16292 KB Output is correct
39 Correct 224 ms 17336 KB Output is correct
40 Correct 217 ms 17232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 85 ms 27216 KB Output is correct
12 Correct 69 ms 27368 KB Output is correct
13 Correct 76 ms 27116 KB Output is correct
14 Correct 85 ms 27220 KB Output is correct
15 Correct 73 ms 27216 KB Output is correct
16 Correct 94 ms 27224 KB Output is correct
17 Correct 92 ms 27108 KB Output is correct
18 Correct 69 ms 27480 KB Output is correct
19 Correct 83 ms 27124 KB Output is correct
20 Correct 71 ms 27216 KB Output is correct
21 Correct 78 ms 27368 KB Output is correct
22 Correct 87 ms 27220 KB Output is correct
23 Correct 69 ms 27220 KB Output is correct
24 Correct 96 ms 27220 KB Output is correct
25 Correct 94 ms 27216 KB Output is correct
26 Correct 73 ms 26988 KB Output is correct
27 Correct 79 ms 27216 KB Output is correct
28 Correct 73 ms 27216 KB Output is correct
29 Correct 75 ms 27220 KB Output is correct
30 Correct 73 ms 27256 KB Output is correct
31 Correct 87 ms 27216 KB Output is correct
32 Correct 99 ms 27216 KB Output is correct
33 Correct 88 ms 27220 KB Output is correct
34 Correct 71 ms 27220 KB Output is correct
35 Correct 91 ms 27328 KB Output is correct
36 Correct 83 ms 27256 KB Output is correct
37 Correct 86 ms 27220 KB Output is correct
38 Correct 96 ms 27216 KB Output is correct
39 Correct 98 ms 27128 KB Output is correct
40 Correct 82 ms 27124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 179 ms 15220 KB Output is correct
12 Correct 176 ms 14664 KB Output is correct
13 Correct 196 ms 13908 KB Output is correct
14 Correct 233 ms 14212 KB Output is correct
15 Correct 183 ms 15312 KB Output is correct
16 Correct 206 ms 14364 KB Output is correct
17 Correct 206 ms 14164 KB Output is correct
18 Correct 138 ms 16284 KB Output is correct
19 Correct 163 ms 13120 KB Output is correct
20 Correct 181 ms 14732 KB Output is correct
21 Correct 195 ms 18144 KB Output is correct
22 Correct 197 ms 18260 KB Output is correct
23 Correct 233 ms 17428 KB Output is correct
24 Correct 214 ms 17296 KB Output is correct
25 Correct 218 ms 19228 KB Output is correct
26 Correct 244 ms 17764 KB Output is correct
27 Correct 241 ms 17616 KB Output is correct
28 Correct 146 ms 20276 KB Output is correct
29 Correct 182 ms 16208 KB Output is correct
30 Correct 209 ms 18256 KB Output is correct
31 Correct 236 ms 18256 KB Output is correct
32 Correct 267 ms 18260 KB Output is correct
33 Correct 203 ms 17268 KB Output is correct
34 Correct 240 ms 17300 KB Output is correct
35 Correct 247 ms 17880 KB Output is correct
36 Correct 146 ms 16212 KB Output is correct
37 Correct 185 ms 18216 KB Output is correct
38 Correct 194 ms 16292 KB Output is correct
39 Correct 224 ms 17336 KB Output is correct
40 Correct 217 ms 17232 KB Output is correct
41 Correct 85 ms 27216 KB Output is correct
42 Correct 69 ms 27368 KB Output is correct
43 Correct 76 ms 27116 KB Output is correct
44 Correct 85 ms 27220 KB Output is correct
45 Correct 73 ms 27216 KB Output is correct
46 Correct 94 ms 27224 KB Output is correct
47 Correct 92 ms 27108 KB Output is correct
48 Correct 69 ms 27480 KB Output is correct
49 Correct 83 ms 27124 KB Output is correct
50 Correct 71 ms 27216 KB Output is correct
51 Correct 78 ms 27368 KB Output is correct
52 Correct 87 ms 27220 KB Output is correct
53 Correct 69 ms 27220 KB Output is correct
54 Correct 96 ms 27220 KB Output is correct
55 Correct 94 ms 27216 KB Output is correct
56 Correct 73 ms 26988 KB Output is correct
57 Correct 79 ms 27216 KB Output is correct
58 Correct 73 ms 27216 KB Output is correct
59 Correct 75 ms 27220 KB Output is correct
60 Correct 73 ms 27256 KB Output is correct
61 Correct 87 ms 27216 KB Output is correct
62 Correct 99 ms 27216 KB Output is correct
63 Correct 88 ms 27220 KB Output is correct
64 Correct 71 ms 27220 KB Output is correct
65 Correct 91 ms 27328 KB Output is correct
66 Correct 83 ms 27256 KB Output is correct
67 Correct 86 ms 27220 KB Output is correct
68 Correct 96 ms 27216 KB Output is correct
69 Correct 98 ms 27128 KB Output is correct
70 Correct 82 ms 27124 KB Output is correct
71 Correct 361 ms 51536 KB Output is correct
72 Correct 368 ms 51740 KB Output is correct
73 Correct 505 ms 50512 KB Output is correct
74 Correct 650 ms 50676 KB Output is correct
75 Correct 368 ms 52472 KB Output is correct
76 Correct 886 ms 51000 KB Output is correct
77 Correct 786 ms 50920 KB Output is correct
78 Correct 257 ms 54580 KB Output is correct
79 Correct 330 ms 48532 KB Output is correct
80 Correct 346 ms 51760 KB Output is correct
81 Correct 472 ms 51444 KB Output is correct
82 Correct 635 ms 50608 KB Output is correct
83 Correct 348 ms 49960 KB Output is correct
84 Correct 691 ms 50768 KB Output is correct
85 Correct 860 ms 50632 KB Output is correct
86 Correct 259 ms 48468 KB Output is correct
87 Correct 403 ms 51404 KB Output is correct
88 Correct 429 ms 48492 KB Output is correct
89 Correct 533 ms 50268 KB Output is correct
90 Correct 507 ms 50560 KB Output is correct
91 Correct 379 ms 49768 KB Output is correct
92 Correct 921 ms 50916 KB Output is correct
93 Correct 767 ms 50792 KB Output is correct
94 Correct 225 ms 48468 KB Output is correct
95 Correct 647 ms 50980 KB Output is correct
96 Correct 712 ms 50664 KB Output is correct
97 Correct 655 ms 50976 KB Output is correct
98 Correct 668 ms 50464 KB Output is correct
99 Correct 660 ms 50516 KB Output is correct
100 Correct 635 ms 50772 KB Output is correct