답안 #1108852

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108852 2024-11-05T11:06:35 Z AverageAmogusEnjoyer Joker (BOI20_joker) C++17
41 / 100
474 ms 29984 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> bool cmin(T &i, T j) { return i > j ? i=j,true:false; }
template<class T> bool cmax(T &i, T j) { return i < j ? i=j,true:false; }

constexpr int nax = 200200,block_size = 450;
vector<tuple<int,int,int>> queries[block_size]; // r,l,idx
vector<bool> ans;
int edges[nax][2];
int n,m;

struct dsu_save {
    int x,y,lzx,lzy,szx,szy,parx,pary;
    dsu_save(int x,int y,int lzx,int lzy,int szx,int szy,int parx,int pary): x(x), y(y), lzx(lzx), lzy(lzy), szx(szx), szy(szy), parx(parx), pary(pary) {}
    void print() {
        cout << "x: " << x + 1 << endl;
        cout << "y: " << y + 1 << endl;
        cout << "lzx: " << lzx << ", lzy: " << lzy << ", szx: " << szx << ", szy: " << szy << ", parx: " << parx << ", pary: " << pary << endl;
    }
};

struct DSU {
    int lz[nax];
    int par[nax];
    int sz[nax];
    stack<dsu_save> stk;
    DSU(int N) {
        for (int i = 0; i < N; i++)
            lz[i] = 0, par[i] = i, sz[i] = 1;
    }
    int get(int x,int &cx) {
        cx ^= lz[x];
        return (x == par[x] ? x : get(par[x],cx));
    }
    bool need_reset = 0;
    bool unite(int x,int y) {
        assert(!need_reset);

        // cout << "Adding edge: " << x + 1 << " " << y + 1 << endl;

        int cx = 0,cy = 0;
        int px = get(x,cx), py = get(y,cy);

        // cout << px + 1 << " " << py + 1 << " " << cx << " " << cy << endl;
        if (sz[px] < sz[py]) 
            swap(px,py), swap(cx,cy);
        stk.emplace(px,py,lz[px],lz[py],sz[px],sz[py],par[px],par[py]);
        if (cx == cy) {
            if (px == py) {
                need_reset = 1;
                return 1;
            } else {
                lz[py] ^= 1;
                sz[px] += sz[py];
                par[py] = px;
            }
        } else {
            if (px != py) {
                sz[px] += sz[py];
                par[py] = px;
            }
        }
        return 0;
    }
    void rollback(int k) {
        need_reset = 0;
        // do stuff
        assert(stk.size() >= k);
        while(k--) {
            auto c = stk.top(); stk.pop();
            // c.print();
            lz[c.x] = c.lzx,lz[c.y] = c.lzy,par[c.x] = c.parx,par[c.y] = c.pary,sz[c.x] = c.szx,sz[c.y] = c.szy;
        }
    }
};

DSU dsu(nax); 

bool solve_block(int block) {
    int current_r = m - 1;
    for (int i = max(0,(block - 1) * block_size); i < min(m,block * block_size); i++) {
        // cout << "edge index: " << i + 1 << endl;
        if (dsu.unite(edges[i][0],edges[i][1])) {
            for (int B = block; B < block_size; B++) 
                for (auto &[r,l,idx]: queries[B])
                    ans[idx] = 1;
            return 1;
        }
    }
    for (int i = 0; i < queries[block].size(); i++) {
        auto &[r,l,idx] = queries[block][i];
        while(current_r >= r) {
            // cout << "edge index: " << current_r + 1 << endl;
            if (dsu.unite(edges[current_r][0],edges[current_r][1])) {
                for (int j = i; j < queries[block].size(); j++) {
                    int idx2 = get<2>(queries[block][j]);
                    ans[idx2] = 1;
                }
                dsu.rollback(m - current_r); // potrebbero essere necessari dei +- 1
                return 0;
            }
            current_r--;
        }
        --l;
        int inserted = 0;
        while(l >= block * block_size) {
            // devo inserirlo
            inserted++;
            // cout << "edge index: " << l + 1 << endl;
            if (dsu.unite(edges[l][0],edges[l][1])) {
                ans[idx] = 1;
                break;
            }
            l--;
        }
        dsu.rollback(inserted);
    }
    return 0;
}

vector<bool> trova_cicli(int N, int M, int Q, vector<int> A, vector<int> B, vector<int> L, vector<int> R) {
    n = N, m = M;
    for (int i = 0; i < M; i++)
        edges[i][0] = A[i],edges[i][1] = B[i];
    ans.resize(Q);
    for (int i = 0; i < Q; i++) 
        queries[L[i] / block_size].emplace_back(R[i],L[i],i);
    for (int i = 0; i < block_size; i++) 
        sort(queries[i].rbegin(),queries[i].rend());
    for (int i = 0; i < block_size; i++) 
        if (solve_block(i))
            break;
    return ans;
}

int main() {

    int N,M,Q;
    cin >> N >> M >> Q;
    vector<int> A(M),B(M),L(Q),R(Q);
    for (int i = 0; i < M; i++) {
        cin >> A[i] >> B[i];
        --A[i],--B[i];
    }
    for (int i = 0; i < Q; i++) {
        cin >> L[i] >> R[i];
        --L[i];
    }
    auto res = trova_cicli(N,M,Q,A,B,L,R);
    for (int i = 0; i < Q; i++) 
        cout << (res[i] ? "YES" : "NO") << "\n";

}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from Joker.cpp:1:
Joker.cpp: In member function 'void DSU::rollback(int)':
Joker.cpp:69:27: warning: comparison of integer expressions of different signedness: 'std::stack<dsu_save>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   69 |         assert(stk.size() >= k);
      |                ~~~~~~~~~~~^~~~
Joker.cpp: In function 'bool solve_block(int)':
Joker.cpp:91:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |     for (int i = 0; i < queries[block].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:96:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |                 for (int j = i; j < queries[block].size(); j++) {
      |                                 ~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2640 KB Output is correct
2 Correct 1 ms 2640 KB Output is correct
3 Correct 2 ms 2808 KB Output is correct
4 Correct 2 ms 2640 KB Output is correct
5 Correct 1 ms 2640 KB Output is correct
6 Correct 2 ms 2892 KB Output is correct
7 Correct 1 ms 2640 KB Output is correct
8 Correct 2 ms 2640 KB Output is correct
9 Correct 2 ms 2808 KB Output is correct
10 Correct 2 ms 2640 KB Output is correct
11 Correct 2 ms 2640 KB Output is correct
12 Correct 2 ms 2640 KB Output is correct
13 Correct 2 ms 2640 KB Output is correct
14 Correct 2 ms 2640 KB Output is correct
15 Correct 2 ms 2640 KB Output is correct
16 Correct 2 ms 2828 KB Output is correct
17 Correct 2 ms 2640 KB Output is correct
18 Correct 1 ms 2640 KB Output is correct
19 Correct 2 ms 2640 KB Output is correct
20 Correct 2 ms 2640 KB Output is correct
21 Correct 2 ms 2640 KB Output is correct
22 Correct 2 ms 2640 KB Output is correct
23 Correct 2 ms 2640 KB Output is correct
24 Correct 2 ms 2640 KB Output is correct
25 Correct 2 ms 2824 KB Output is correct
26 Correct 2 ms 2640 KB Output is correct
27 Correct 2 ms 2640 KB Output is correct
28 Correct 2 ms 2640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2640 KB Output is correct
2 Correct 1 ms 2640 KB Output is correct
3 Correct 2 ms 2808 KB Output is correct
4 Correct 2 ms 2640 KB Output is correct
5 Correct 1 ms 2640 KB Output is correct
6 Correct 2 ms 2892 KB Output is correct
7 Correct 1 ms 2640 KB Output is correct
8 Correct 2 ms 2640 KB Output is correct
9 Correct 2 ms 2808 KB Output is correct
10 Correct 2 ms 2640 KB Output is correct
11 Correct 2 ms 2640 KB Output is correct
12 Correct 2 ms 2640 KB Output is correct
13 Correct 2 ms 2640 KB Output is correct
14 Correct 2 ms 2640 KB Output is correct
15 Correct 2 ms 2640 KB Output is correct
16 Correct 2 ms 2828 KB Output is correct
17 Correct 2 ms 2640 KB Output is correct
18 Correct 1 ms 2640 KB Output is correct
19 Correct 2 ms 2640 KB Output is correct
20 Correct 2 ms 2640 KB Output is correct
21 Correct 2 ms 2640 KB Output is correct
22 Correct 2 ms 2640 KB Output is correct
23 Correct 2 ms 2640 KB Output is correct
24 Correct 2 ms 2640 KB Output is correct
25 Correct 2 ms 2824 KB Output is correct
26 Correct 2 ms 2640 KB Output is correct
27 Correct 2 ms 2640 KB Output is correct
28 Correct 2 ms 2640 KB Output is correct
29 Correct 8 ms 2896 KB Output is correct
30 Correct 7 ms 2988 KB Output is correct
31 Correct 6 ms 2896 KB Output is correct
32 Correct 5 ms 2896 KB Output is correct
33 Correct 7 ms 3064 KB Output is correct
34 Correct 6 ms 2896 KB Output is correct
35 Correct 7 ms 2896 KB Output is correct
36 Correct 6 ms 2872 KB Output is correct
37 Incorrect 9 ms 2896 KB Output isn't correct
38 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2640 KB Output is correct
2 Correct 1 ms 2640 KB Output is correct
3 Correct 143 ms 16296 KB Output is correct
4 Correct 172 ms 26016 KB Output is correct
5 Correct 174 ms 18872 KB Output is correct
6 Correct 185 ms 16296 KB Output is correct
7 Correct 159 ms 16316 KB Output is correct
8 Correct 157 ms 16292 KB Output is correct
9 Correct 159 ms 16324 KB Output is correct
10 Correct 158 ms 16300 KB Output is correct
11 Correct 164 ms 16292 KB Output is correct
12 Correct 201 ms 16288 KB Output is correct
13 Correct 171 ms 16292 KB Output is correct
14 Correct 193 ms 16316 KB Output is correct
15 Correct 174 ms 16292 KB Output is correct
16 Correct 181 ms 16312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2640 KB Output is correct
2 Correct 1 ms 2640 KB Output is correct
3 Correct 2 ms 2808 KB Output is correct
4 Correct 2 ms 2640 KB Output is correct
5 Correct 1 ms 2640 KB Output is correct
6 Correct 2 ms 2892 KB Output is correct
7 Correct 1 ms 2640 KB Output is correct
8 Correct 2 ms 2640 KB Output is correct
9 Correct 2 ms 2808 KB Output is correct
10 Correct 2 ms 2640 KB Output is correct
11 Correct 2 ms 2640 KB Output is correct
12 Correct 2 ms 2640 KB Output is correct
13 Correct 2 ms 2640 KB Output is correct
14 Correct 2 ms 2640 KB Output is correct
15 Correct 2 ms 2640 KB Output is correct
16 Correct 2 ms 2828 KB Output is correct
17 Correct 2 ms 2640 KB Output is correct
18 Correct 1 ms 2640 KB Output is correct
19 Correct 2 ms 2640 KB Output is correct
20 Correct 2 ms 2640 KB Output is correct
21 Correct 2 ms 2640 KB Output is correct
22 Correct 2 ms 2640 KB Output is correct
23 Correct 2 ms 2640 KB Output is correct
24 Correct 2 ms 2640 KB Output is correct
25 Correct 2 ms 2824 KB Output is correct
26 Correct 2 ms 2640 KB Output is correct
27 Correct 2 ms 2640 KB Output is correct
28 Correct 2 ms 2640 KB Output is correct
29 Correct 143 ms 16296 KB Output is correct
30 Correct 172 ms 26016 KB Output is correct
31 Correct 174 ms 18872 KB Output is correct
32 Correct 185 ms 16296 KB Output is correct
33 Correct 159 ms 16316 KB Output is correct
34 Correct 157 ms 16292 KB Output is correct
35 Correct 159 ms 16324 KB Output is correct
36 Correct 158 ms 16300 KB Output is correct
37 Correct 164 ms 16292 KB Output is correct
38 Correct 201 ms 16288 KB Output is correct
39 Correct 171 ms 16292 KB Output is correct
40 Correct 193 ms 16316 KB Output is correct
41 Correct 174 ms 16292 KB Output is correct
42 Correct 181 ms 16312 KB Output is correct
43 Correct 452 ms 20504 KB Output is correct
44 Correct 463 ms 29984 KB Output is correct
45 Correct 474 ms 23992 KB Output is correct
46 Correct 339 ms 20408 KB Output is correct
47 Correct 364 ms 20404 KB Output is correct
48 Correct 202 ms 20152 KB Output is correct
49 Correct 218 ms 20496 KB Output is correct
50 Correct 394 ms 20408 KB Output is correct
51 Correct 395 ms 20580 KB Output is correct
52 Correct 426 ms 21176 KB Output is correct
53 Correct 408 ms 20408 KB Output is correct
54 Correct 439 ms 20492 KB Output is correct
55 Correct 456 ms 20600 KB Output is correct
56 Correct 461 ms 20836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2640 KB Output is correct
2 Correct 1 ms 2640 KB Output is correct
3 Correct 2 ms 2808 KB Output is correct
4 Correct 2 ms 2640 KB Output is correct
5 Correct 1 ms 2640 KB Output is correct
6 Correct 2 ms 2892 KB Output is correct
7 Correct 1 ms 2640 KB Output is correct
8 Correct 2 ms 2640 KB Output is correct
9 Correct 2 ms 2808 KB Output is correct
10 Correct 2 ms 2640 KB Output is correct
11 Correct 2 ms 2640 KB Output is correct
12 Correct 2 ms 2640 KB Output is correct
13 Correct 2 ms 2640 KB Output is correct
14 Correct 2 ms 2640 KB Output is correct
15 Correct 2 ms 2640 KB Output is correct
16 Correct 2 ms 2828 KB Output is correct
17 Correct 2 ms 2640 KB Output is correct
18 Correct 1 ms 2640 KB Output is correct
19 Correct 2 ms 2640 KB Output is correct
20 Correct 2 ms 2640 KB Output is correct
21 Correct 2 ms 2640 KB Output is correct
22 Correct 2 ms 2640 KB Output is correct
23 Correct 2 ms 2640 KB Output is correct
24 Correct 2 ms 2640 KB Output is correct
25 Correct 2 ms 2824 KB Output is correct
26 Correct 2 ms 2640 KB Output is correct
27 Correct 2 ms 2640 KB Output is correct
28 Correct 2 ms 2640 KB Output is correct
29 Correct 8 ms 2896 KB Output is correct
30 Correct 7 ms 2988 KB Output is correct
31 Correct 6 ms 2896 KB Output is correct
32 Correct 5 ms 2896 KB Output is correct
33 Correct 7 ms 3064 KB Output is correct
34 Correct 6 ms 2896 KB Output is correct
35 Correct 7 ms 2896 KB Output is correct
36 Correct 6 ms 2872 KB Output is correct
37 Incorrect 9 ms 2896 KB Output isn't correct
38 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2640 KB Output is correct
2 Correct 1 ms 2640 KB Output is correct
3 Correct 2 ms 2808 KB Output is correct
4 Correct 2 ms 2640 KB Output is correct
5 Correct 1 ms 2640 KB Output is correct
6 Correct 2 ms 2892 KB Output is correct
7 Correct 1 ms 2640 KB Output is correct
8 Correct 2 ms 2640 KB Output is correct
9 Correct 2 ms 2808 KB Output is correct
10 Correct 2 ms 2640 KB Output is correct
11 Correct 2 ms 2640 KB Output is correct
12 Correct 2 ms 2640 KB Output is correct
13 Correct 2 ms 2640 KB Output is correct
14 Correct 2 ms 2640 KB Output is correct
15 Correct 2 ms 2640 KB Output is correct
16 Correct 2 ms 2828 KB Output is correct
17 Correct 2 ms 2640 KB Output is correct
18 Correct 1 ms 2640 KB Output is correct
19 Correct 2 ms 2640 KB Output is correct
20 Correct 2 ms 2640 KB Output is correct
21 Correct 2 ms 2640 KB Output is correct
22 Correct 2 ms 2640 KB Output is correct
23 Correct 2 ms 2640 KB Output is correct
24 Correct 2 ms 2640 KB Output is correct
25 Correct 2 ms 2824 KB Output is correct
26 Correct 2 ms 2640 KB Output is correct
27 Correct 2 ms 2640 KB Output is correct
28 Correct 2 ms 2640 KB Output is correct
29 Correct 8 ms 2896 KB Output is correct
30 Correct 7 ms 2988 KB Output is correct
31 Correct 6 ms 2896 KB Output is correct
32 Correct 5 ms 2896 KB Output is correct
33 Correct 7 ms 3064 KB Output is correct
34 Correct 6 ms 2896 KB Output is correct
35 Correct 7 ms 2896 KB Output is correct
36 Correct 6 ms 2872 KB Output is correct
37 Incorrect 9 ms 2896 KB Output isn't correct
38 Halted 0 ms 0 KB -