QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#377884#5504. Flower Gardenckiseki#AC ✓6001ms541840kbC++204.2kb2024-04-05 19:14:292024-04-05 19:14:30

Judging History

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

  • [2024-04-05 19:14:30]
  • 评测
  • 测评结果:AC
  • 用时:6001ms
  • 内存:541840kb
  • [2024-04-05 19:14:29]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
  cerr << "\e[1;32m(" << s << ") = (";
  int f = 0;
  (..., (cerr << (f++ ? ", " : "") << a));
  cerr << ")\e[0m\n";
}
#include <experimental/iterator>
void orange_(auto s, auto L, auto R) {
  cerr << "\e[1;33m[ " << s << " ] = [ ";
  using namespace experimental;
  copy(L, R, make_ostream_joiner(cerr, ", "));
  cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

class SCC {
protected:
  int n, dfc, nscc;
  vector<vector<int>> G;
  vector<int> vis, low, idx, stk;
  void dfs(int i) {
    vis[i] = low[i] = ++dfc;
    stk.push_back(i);
    for (int j : G[i])
      if (!vis[j])
        dfs(j), low[i] = min(low[i], low[j]);
      else if (vis[j] != -1)
        low[i] = min(low[i], vis[j]);
    if (low[i] == vis[i])
      for (idx[i] = nscc++; vis[i] != -1;) {
        int x = stk.back();
        stk.pop_back();
        idx[x] = idx[i]; vis[x] = -1;
      }
  }
public:
  SCC(int n_) : n(n_), dfc(0), nscc(0), G(n), vis(n), low(n), idx(n) {}
  void add_edge(int u, int v) { G[u].push_back(v); }
  void solve() {
    for (int i = 0; i < n; i++) if (!vis[i]) dfs(i);
  }
  int get_id(int x) { return idx[x]; }
  int count() { return nscc; }
};

void solve() {
  int n, q;
  cin >> n >> q;


  const int SZ = n * 3;
  SCC scc(SZ * 3);

  vector<array<int, 2>> es;
  auto add_edge = [&](int l, int r) {
    scc.add_edge(l, r);
    es.push_back({l, r});
  };

  auto gao = [SZ](int l, int r) {
    vector<int> res;
    for (l += SZ, r += SZ; l < r; l >>= 1, r >>= 1) {
      if (l & 1) res.push_back(l++);
      if (r & 1) res.push_back(--r);
    }
    return res;
  };

  vector<int> up(SZ * 2), dn(SZ * 2);
  for (int i = 0; i < SZ; i++)
    up[i + SZ] = dn[i + SZ] = i;
  for (int i = SZ - 1; i > 0; i--) {
    up[i] = i + SZ;
    add_edge(up[i << 1], up[i]);
    add_edge(up[i << 1 | 1], up[i]);
  }
  for (int i = SZ - 1; i > 0; i--) {
    dn[i] = i + SZ * 2;
    add_edge(dn[i], dn[i << 1]);
    add_edge(dn[i], dn[i << 1 | 1]);
  }

  for (int x = 0; x < q; x++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    --a;
    --c;
    
    for (int i : gao(a, b))
      for (int j : gao(c, d)) {
        add_edge(up[i], dn[j]);
      }
  }

  scc.solve();

  const int m = scc.count();
  vector<vector<int>> S(m);
  vector<vector<int>> g(m);

  for (auto [x, y] : es) {
    x = scc.get_id(x);
    y = scc.get_id(y);
    assert(x >= y);
    if (x != y)
      g[x].emplace_back(y);
  }

  for (int i = 0; i < n * 3; i++) {
    debug(i+1, scc.get_id(i));
    S[scc.get_id(i)].push_back(i);
  }

  vector<int> vis(m);

  vector<int> big;
  for (int i = 0; i < m; i++) {
    if (S[i].size() * 3 >= SZ) {
      big.push_back(i);

      vis.assign(m, false);

      vector<int> cur;
      auto dfs = [&](auto self, int u) {
        if (vis[u]) return;
        vis[u] = true;

        for (int z : S[u])
          cur.push_back(z);
        for (int v : g[u]) {
          self(self, v);
        }
      };

      dfs(dfs, i);

      assert(cur.size() * 3 >= SZ);

      orange(all(cur));
      if (cur.size() * 3 <= SZ * 2) {
        string ans(n * 3, 'R');
        for (int x : cur)
          ans[x] = 'F';
        cout << "TAK\n";
//#ifndef CKISEKI
        cout << ans << '\n';
//#endif
        safe;
        debug("take big, OK");
        return;
      }

    }
  }

  vector<int> cant_take(m);

  for (int x : big)
    cant_take[x] = true;

  for (int x = 0; x < m; x++) {
    for (int y : g[x]) {
      cant_take[x] |= cant_take[y];
    }
  }

  vector<int> cur;
  for (int x = 0; x < m; x++) {
    if (cant_take[x]) continue;
    for (int z : S[x]) {
      cur.push_back(z);
    }
    
    if (cur.size() * 3 >= SZ) {
      assert(cur.size() * 3 <= SZ * 2);
      string ans(n * 3, 'R');
      for (int y : cur)
        ans[y] = 'F';
      cout << "TAK\n";
//#ifndef CKISEKI
      cout << ans << '\n';
//#endif
      orange(all(cur));
      return;
    }
  }

  cout << "NIE\n";
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  int t;
  cin >> t;
  while (t--)
    solve();

  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3620kb

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: 5456ms
memory: 255648kb

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: 1801ms
memory: 79124kb

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: 1947ms
memory: 78572kb

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: 682ms
memory: 3756kb

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
RFRRRRRRFFFR
TAK
FFRRFFRRRRRR
NIE
TAK
FFFFRRRRRRRFRRR
TAK
FRRRRRRRFFFR
TAK
FFFRRRRRRRRF
TAK
FFFFFRRRRRRRRRR
TAK
FFRRRRRRRRFF
TAK
FFRRFFRRRFRRRRR
NIE
TAK
FFFFRRRRRRRR
NIE
TAK
RRFFFRRFF
NIE
TAK
FFFRRRRRR
TAK
RRRRRRFFFRRF
TAK
RRFFRFRFFFFFRFF
TAK
RRRRRRRFFFFR
NIE
TAK
...

result:

ok good!

Test #6:

score: 0
Accepted
time: 6001ms
memory: 541840kb

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!