QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#77301#5504. Flower GardenrniyaAC ✓3779ms163416kbC++177.4kb2023-02-13 23:53:512023-02-13 23:53:52

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-13 23:53:52]
  • 评测
  • 测评结果:AC
  • 用时:3779ms
  • 内存:163416kb
  • [2023-02-13 23:53:51]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ALL(x) (x).begin(), (x).end()
#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) void(0)
#endif

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T> void mkuni(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
template <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }

const int INF = (1 << 30) - 1;
const long long IINF = (1LL << 60) - 1;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MOD = 998244353;
// const int MOD = 1000000007;

#include <algorithm>
#include <cassert>
#include <vector>

struct StronglyConnectedComponents {
    std::vector<std::vector<int>> G;
    std::vector<int> comp;  // component id vertex v belongs to

    StronglyConnectedComponents(int n) : G(n), comp(n, -1), n(n), time(0), group_num(0), ord(n, -1), low(n) {}

    void add_edge(int u, int v) {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        G[u].emplace_back(v);
    }

    int add_vertex() {
        G.emplace_back();
        comp.emplace_back(-1);
        ord.emplace_back(-1);
        low.emplace_back();
        return n++;
    }

    std::vector<std::vector<int>> build() {
        for (int i = 0; i < n; i++) {
            if (ord[i] < 0) {
                dfs(i);
            }
        }
        for (int& x : comp) x = group_num - 1 - x;
        std::vector<std::vector<int>> groups(group_num);
        for (int i = 0; i < n; i++) groups[comp[i]].emplace_back(i);
        return groups;
    }

    std::vector<std::vector<int>> make_graph() {
        std::vector<std::vector<int>> dag(group_num);
        for (int v = 0; v < n; v++) {
            for (int& u : G[v]) {
                if (comp[v] != comp[u]) {
                    dag[comp[v]].emplace_back(comp[u]);
                }
            }
        }
        for (auto& to : dag) {
            std::sort(to.begin(), to.end());
            to.erase(unique(to.begin(), to.end()), to.end());
        }
        return dag;
    }

    int operator[](int v) const { return comp[v]; }

private:
    int n, time, group_num;
    std::vector<int> ord, low, visited;

    void dfs(int v) {
        ord[v] = low[v] = time++;
        visited.emplace_back(v);
        for (int& u : G[v]) {
            if (ord[u] == -1) {
                dfs(u);
                low[v] = std::min(low[v], low[u]);
            } else if (comp[u] < 0) {
                low[v] = std::min(low[v], ord[u]);
            }
        }
        if (ord[v] == low[v]) {
            while (true) {
                int u = visited.back();
                visited.pop_back();
                comp[u] = group_num;
                if (u == v) break;
            }
            group_num++;
        }
    }
};

void solve() {
    int n, q;
    cin >> n >> q;
    n *= 3;
    StronglyConnectedComponents SCC(3 * n);
    auto add_edge = [&](int u, int v) { SCC.add_edge((3 * n <= u ? u - 2 * n : u), v); };
    auto add_edge_range = [&](int l1, int r1, int l2, int r2) {
        int add = SCC.add_vertex();
        for (l1 += n, r1 += n; l1 < r1; l1 >>= 1, r1 >>= 1) {
            if (l1 & 1) add_edge((l1++) + 2 * n, add);
            if (r1 & 1) add_edge((--r1) + 2 * n, add);
        }
        for (l2 += n, r2 += n; l2 < r2; l2 >>= 1, r2 >>= 1) {
            if (l2 & 1) SCC.add_edge(add, l2++);
            if (r2 & 1) SCC.add_edge(add, --r2);
        }
    };
    for (int i = 1; i < n; i++) {
        int l = i << 1 | 0, r = i << 1 | 1;
        add_edge(i, l);
        add_edge(i, r);
        add_edge(l + 2 * n, i + 2 * n);
        add_edge(r + 2 * n, i + 2 * n);
    }
    for (; q--;) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        add_edge_range(--a, b, --c, d);
    }

    auto groups = SCC.build();
    int N = groups.size();
    vector<vector<int>> ngroups(N);
    for (int i = 0; i < N; i++) {
        for (int& v : groups[i]) {
            if (n <= v and v < 2 * n) {
                ngroups[i].emplace_back(v - n);
            }
        }
    }
    swap(groups, ngroups);
    auto G = SCC.make_graph();
    vector<vector<int>> rG(N);
    for (int v = 0; v < N; v++) {
        for (int& u : G[v]) {
            rG[u].emplace_back(v);
        }
    }

    vector<int> comp;
    vector<bool> alive(N, true);
    auto check = [&](int v) {
        vector<bool> reach(N, false);
        reach[v] = true;
        int sum = 0;
        for (int i = v; i < N; i++) {
            if (not reach[i]) continue;
            sum += groups[i].size();
            for (int& j : G[i]) reach[j] = true;
        }
        assert(n / 3 <= sum);
        if (sum <= n / 3 * 2) {
            for (int i = v; i < N; i++) {
                if (reach[i]) {
                    comp.emplace_back(i);
                }
            }
            return;
        }
        for (int i = v; i >= 0; i--) {
            if (not reach[i]) continue;
            alive[i] = false;
            for (int& j : rG[i]) reach[j] = true;
        }
    };
    for (int i = N - 1; i >= 0; i--) {
        if (not alive[i]) continue;
        if ((int)groups[i].size() < n / 3 + 2) continue;
        if (comp.empty()) check(i);
    }

    if (comp.empty()) {
        int sum = 0;
        for (int i = N - 1; i >= 0; i--) {
            if (not alive[i]) continue;
            sum += groups[i].size();
            comp.emplace_back(i);
            if (n / 3 <= sum) break;
        }
        if (sum < n / 3) {
            cout << "NIE" << '\n';
            return;
        }
    }

    string ans(n, 'R');
    for (int& i : comp) {
        for (int& j : groups[i]) {
            ans[j] = 'F';
        }
    }
    cout << "TAK" << '\n';
    cout << ans << '\n';
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int z;
    cin >> z;
    for (; z--;) solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3416kb

input:

2
1 3
1 1 2 2
1 2 3 3
1 1 3 3
1 3
1 1 2 2
2 2 3 3
3 3 1 1

output:

TAK
RRF
NIE

result:

ok good!

Test #2:

score: 0
Accepted
time: 2825ms
memory: 74116kb

input:

10
33333 100000
28701 40192 93418 95143
95902 97908 78378 78461
36823 44196 22268 23996
23977 24786 33315 48829
83965 90411 4923 8445
20235 21177 32543 47454
29598 35414 72477 73049
2014 12632 42163 46466
64305 65518 98825 99552
32331 41625 92772 96224
26500 54122 76990 77126
18249 20335 31165 36080...

output:

NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE
NIE

result:

ok good!

Test #3:

score: 0
Accepted
time: 3491ms
memory: 95060kb

input:

10
33333 100000
15207 33614 66276 66276
97173 97173 67589 73960
19673 36626 65207 65207
89825 98169 27079 27079
56067 56966 7560 7560
18170 35477 18752 18752
32621 36748 34460 34460
61595 61700 14117 14117
32395 36710 9064 9064
13172 13172 1728 4640
40462 41878 47171 47171
76965 82414 5767 5767
9225...

output:

TAK
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...

result:

ok good!

Test #4:

score: 0
Accepted
time: 3230ms
memory: 86444kb

input:

10
33333 100000
2646 2646 6430 6446
82226 82231 15128 15132
877 877 85831 88474
51389 79196 37573 37573
38030 38030 14248 14280
63032 68489 81074 81074
46468 46468 7403 7487
19864 20058 97979 97979
71640 71833 8558 8558
12121 12434 82481 82481
32901 32901 1899 2162
65312 77886 75969 75974
87983 8798...

output:

TAK
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFRRFFFFRFFRRFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...

result:

ok good!

Test #5:

score: 0
Accepted
time: 1617ms
memory: 3540kb

input:

87005
1 3
1 1 2 2
1 2 3 3
1 1 3 3
1 3
1 1 2 2
2 2 3 3
3 3 1 1
1 1
1 3 2 3
1 2
1 1 2 2
2 2 1 1
1 2
1 3 1 1
1 1 1 3
4 20
3 5 6 12
4 5 7 11
1 1 2 2
3 4 7 12
3 5 10 10
3 5 8 8
4 4 9 11
4 4 7 7
1 1 9 10
3 4 6 9
3 5 11 12
3 3 7 9
3 5 2 2
4 5 2 2
1 1 7 11
1 1 10 10
3 5 7 8
4 4 2 2
1 1 2 2
4 5 8 10
4 12
11 ...

output:

TAK
RRF
NIE
TAK
RFF
TAK
FFR
NIE
TAK
RRRRRFFFFRRR
TAK
RFRRFFFRRRRR
NIE
TAK
RFFFRRRRRFRFRRR
TAK
FRRRRRFFFRRR
TAK
FFRRRRRRRRFF
TAK
RFFFFRRRFRRRRRR
TAK
RRRRRRFFFFRR
TAK
FFRRFFRRRFRRRRR
NIE
TAK
RFFFFRRRRRRR
NIE
TAK
RRFFFRRFF
NIE
TAK
FFFRRRRRR
TAK
RRRRFFFFRRRR
TAK
RRFFRFRFFFFFRFF
TAK
RRRRRRFFFFRR
NIE
TAK
...

result:

ok good!

Test #6:

score: 0
Accepted
time: 3779ms
memory: 163416kb

input:

10
33333 99999
60859 99999 1 60858
78724 99999 1 78723
42986 99999 1 42985
35108 99999 1 35107
63158 99999 1 63157
86977 99999 1 86976
13576 99999 1 13575
48277 99999 1 48276
89102 99999 1 89101
92657 99999 1 92656
25093 99999 1 25092
2353 99999 1 2352
81748 99999 1 81747
51216 99999 1 51215
75815 9...

output:

NIE
TAK
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

result:

ok good!