답안 #641440

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
641440 2022-09-16T18:12:40 Z Alexandruabcde Colors (RMI18_colors) C++14
100 / 100
798 ms 70028 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair <int, int> PII;
constexpr int NMAX = 15e4 + 5;

vector <int> G[NMAX];

int N, M;

int A[NMAX];
int B[NMAX];

vector <int> Add[NMAX];
vector <int> Remove[NMAX];

class Dynamic_Conectivity {
private:
    vector <int> arb[4*NMAX];
    int arb_size;

    bool Unlocked[NMAX];
    bool Good[NMAX];
    int Dad[NMAX];
    int Sz[NMAX];
    vector <PII> DSU;

    int Reprezentant (int Node) {
        if (Node == Dad[Node]) return Node;

        return Reprezentant(Dad[Node]);
    }

    void Unite (int x, int y) {
        x = Reprezentant(x);
        y = Reprezentant(y);

        if (x == y) return;

        if (Sz[x] <= Sz[y]) {
            Dad[x] = y;
            Sz[y] += Sz[x];

            DSU.push_back({x, y});
        }
        else {
            Dad[y] = x;
            Sz[x] += Sz[y];

            DSU.push_back({y, x});
        }
    }

    void Delete_Last () {
        if (DSU.empty()) return;

        PII last = DSU.back();
        DSU.pop_back();

        Dad[last.first] = last.first;
        Sz[last.second] -= Sz[last.first];
    }

    void Add_Point (int x) {
        Unlocked[x] = true;

        for (auto it : G[x]) {
            if (!Unlocked[it]) continue;

            Unite(x, it);
        }
    }

    void Init_Tree (int nod, int a, int b) {
        arb[nod].clear();

        if (a == b) return;

        int mij = (a + b) / 2;

        Init_Tree(2*nod, a, mij);
        Init_Tree(2*nod + 1, mij+1, b);
    }

    void Update (int nod, int a, int b, int ua, int ub, int who) {
        if (ua <= a && b <= ub) {
            arb[nod].push_back(who);
            return;
        }

        int mij = (a + b) / 2;

        if (ua <= mij) Update(2*nod, a, mij, ua, ub, who);
        if (mij < ub) Update(2*nod+1, mij+1, b, ua, ub, who);
    }

    bool Query (int nod, int a, int b) {
        int aux_sz = DSU.size();
        for (auto it : arb[nod])
            Add_Point(it);

        bool ans = true;
        if (a == b) {
            for (auto it : Add[a])
                Good[Reprezentant(it)] = true;

            for (auto it : Remove[a])
                ans &= Good[Reprezentant(it)];

            for (auto it : Add[a])
                Good[Reprezentant(it)] = false;

            while (DSU.size() > aux_sz)
                Delete_Last();

            for (auto it : arb[nod])
                Unlocked[it] = false;
            return ans;
        }

        int mij = (a + b) / 2;

        ans = (Query(2*nod, a, mij)&Query(2*nod+1, mij+1, b));

        while (DSU.size() > aux_sz)
            Delete_Last();

        for (auto it : arb[nod])
            Unlocked[it] = false;

        return ans;
    }

public:

    void Initialize (int N) {
        arb_size = N;
        Init_Tree(1, 1, N);

        for (int i = 1; i <= N; ++ i ) {
            Unlocked[i] = false;
            Dad[i] = i;
            Sz[i] = 1;
        }

        DSU.clear();
    }

    void Add_Interval (int st, int dr, int who) {
        Update(1, 1, arb_size, st, dr, who);
    }

    bool Check () {
        return Query(1, 1, arb_size);
    }
};

Dynamic_Conectivity DCC;

void Read () {
    cin >> N >> M;

    for (int i = 1; i <= N; ++ i )
        cin >> A[i];

    for (int i = 1; i <= N; ++ i )
        cin >> B[i];

    for (int i = 1; i <= M; ++ i ) {
        int x, y;
        cin >> x >> y;

        G[x].push_back(y);
        G[y].push_back(x);
    }
}

bool ans;

void Solve () {
    DCC.Initialize(N);
    ans = true;

    for (int i = 1; i <= N; ++ i ) {
        Add[A[i]].push_back(i);
        Remove[B[i]].push_back(i);

        if (B[i] > A[i]) ans = false;

        DCC.Add_Interval(B[i], A[i], i);
    }

    ans &= DCC.Check();

    cout << ans << '\n';
}

void Reset () {
    for (int i = 1; i <= N; ++ i ) {
        Add[i].clear();
        Remove[i].clear();

        G[i].clear();
    }
}

void Do_Testcase () {
    Read();

    Solve();

    Reset();
}

int main () {

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;

    for (; T; -- T ) {
        Do_Testcase();
    }
    return 0;
}

Compilation message

colors.cpp: In member function 'bool Dynamic_Conectivity::Query(int, int, int)':
colors.cpp:114:31: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  114 |             while (DSU.size() > aux_sz)
      |                    ~~~~~~~~~~~^~~~~~~~
colors.cpp:126:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  126 |         while (DSU.size() > aux_sz)
      |                ~~~~~~~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 24932 KB Output is correct
2 Correct 33 ms 25544 KB Output is correct
3 Correct 15 ms 25292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 24924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 25028 KB Output is correct
2 Correct 37 ms 25540 KB Output is correct
3 Correct 17 ms 25200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 25028 KB Output is correct
2 Correct 37 ms 25540 KB Output is correct
3 Correct 17 ms 25200 KB Output is correct
4 Correct 155 ms 28100 KB Output is correct
5 Correct 459 ms 44380 KB Output is correct
6 Correct 798 ms 63532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 24932 KB Output is correct
2 Correct 33 ms 25544 KB Output is correct
3 Correct 15 ms 25292 KB Output is correct
4 Correct 78 ms 25028 KB Output is correct
5 Correct 37 ms 25540 KB Output is correct
6 Correct 17 ms 25200 KB Output is correct
7 Correct 77 ms 26444 KB Output is correct
8 Correct 38 ms 25544 KB Output is correct
9 Correct 16 ms 25212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 146 ms 25168 KB Output is correct
2 Correct 443 ms 44408 KB Output is correct
3 Correct 491 ms 62464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 24916 KB Output is correct
2 Correct 20 ms 25384 KB Output is correct
3 Correct 16 ms 25136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 24932 KB Output is correct
2 Correct 33 ms 25544 KB Output is correct
3 Correct 15 ms 25292 KB Output is correct
4 Correct 55 ms 24924 KB Output is correct
5 Correct 78 ms 25028 KB Output is correct
6 Correct 37 ms 25540 KB Output is correct
7 Correct 17 ms 25200 KB Output is correct
8 Correct 155 ms 28100 KB Output is correct
9 Correct 459 ms 44380 KB Output is correct
10 Correct 798 ms 63532 KB Output is correct
11 Correct 77 ms 26444 KB Output is correct
12 Correct 38 ms 25544 KB Output is correct
13 Correct 16 ms 25212 KB Output is correct
14 Correct 146 ms 25168 KB Output is correct
15 Correct 443 ms 44408 KB Output is correct
16 Correct 491 ms 62464 KB Output is correct
17 Correct 34 ms 24916 KB Output is correct
18 Correct 20 ms 25384 KB Output is correct
19 Correct 16 ms 25136 KB Output is correct
20 Correct 74 ms 27344 KB Output is correct
21 Correct 443 ms 45644 KB Output is correct
22 Correct 665 ms 70028 KB Output is correct