답안 #409510

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
409510 2021-05-21T02:21:44 Z abc864197532 무지개나라 (APIO17_rainbow) C++17
100 / 100
1193 ms 442472 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define info() cerr << __PRETTY_FUNCTION__ << ": " << __LINE__ << endl
void abc() {cerr << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cerr << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.X >> a.Y;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.X << ", " << a.Y << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}
template <typename T> struct vv : vector <vector <T>> {
    vv(int n, int m, int v = 0) : vector <vector <T>> (n, vector <T>(m, v)) {}
    vv() {}
};
template <typename T> struct vvv : vector <vv <T>> {
    vvv(int n, int m, int k, int v = 0) : vector <vv <T>> (n, vv <T>(m, k, v)) {}
    vvv() {}
};
#ifdef Doludu
#define test(args...) info(), abc("[" + string(#args) + "]", args)
#define owo
#include "rainbow-grader.cpp"
#else
#define test(args...)
#define owo ios::sync_with_stdio(false); cin.tie(0)
#endif
const int mod = 998244353, N = 200001, logN = 20, K = 111, NN = 4e7;

struct Seg {
    static Seg mem[NN], *pt;
    int l, r, m, sum;
    Seg* ch[2];
    Seg () = default;
    Seg (int _l, int _r) : l(_l), r(_r), m(l + r >> 1) {
        if (r - l > 1) {
            ch[0] = new (pt++) Seg(l, m);
            ch[1] = new (pt++) Seg(m, r);
        } else {
            sum = 0;
        }
    }
    Seg* modify(int p) {
        Seg *now = new (pt++) Seg(*this);
        now->sum++;
        if (r - l == 1) return now;
        else now->ch[p >= m] = now->ch[p >= m]->modify(p);
        return now;
    }
    int query(int a, int b) {
        if (a >= b) return 0;
        if (a <= l && r <= b) return sum;
        int ans = 0;
        if (a < m) ans += ch[0]->query(a, b);
        if (m < b) ans += ch[1]->query(a, b);
        return ans;
    }
} Seg::mem[NN], *Seg::pt = mem;

vector <Seg*> vertex, seg_row, seg_col, cell;
int mn_r = N, mx_r = 0, mn_c = N, mx_c = 0;

void init(int n, int m, int r, int c, int k, char *s) {
    r--, c--;
    vertex.pb(new (Seg::pt++) Seg(0, m + 1));
    seg_row.pb(new (Seg::pt++) Seg(0, m));
    seg_col.pb(new (Seg::pt++) Seg(0, m + 1));
    cell.pb(new Seg(0, m));
    vector <int> v[n + 1], sr[n + 1], sc[n], cc[n];
    auto add = [&](int r, int c) {
        v[r].pb(c);
        v[r].pb(c + 1);
        v[r + 1].pb(c);
        v[r + 1].pb(c + 1);
        sr[r].pb(c);
        sr[r + 1].pb(c);
        sc[r].pb(c);
        sc[r].pb(c + 1);
        cc[r].pb(c);
        mn_r = min(mn_r, r);
        mx_r = max(mx_r, r);
        mn_c = min(mn_c, c);
        mx_c = max(mx_c, c);
    };
    add(r, c);
    for (int i = 0; i < k; ++i) {
        if (s[i] == 'W') c--;
        if (s[i] == 'E') c++;
        if (s[i] == 'N') r--;
        if (s[i] == 'S') r++;
        add(r, c);
    }
    {
        for (int i = 0; i < n + 1; ++i) {
            sort(all(v[i]));
            v[i].resize(unique(all(v[i])) - v[i].begin());
            Seg* now = vertex.back();
            for (int j : v[i]) {
                now = now->modify(j);
            }
            vertex.pb(now);
        }
    }
    {
        for (int i = 0; i < n + 1; ++i) {
            sort(all(sr[i]));
            sr[i].resize(unique(all(sr[i])) - sr[i].begin());
            Seg* now = seg_row.back();
            for (int j : sr[i]) {
                now = now->modify(j);
            }
            seg_row.pb(now);
        }
    }
    {
        for (int i = 0; i < n; ++i) {
            sort(all(sc[i]));
            sc[i].resize(unique(all(sc[i])) - sc[i].begin());
            Seg* now = seg_col.back();
            for (int j : sc[i]) {
                now = now->modify(j);
            }
            seg_col.pb(now);
        }
    }
    {
        for (int i = 0; i < n; ++i) {
            sort(all(cc[i]));
            cc[i].resize(unique(all(cc[i])) - cc[i].begin());
            Seg* now = cell.back();
            for (int j : cc[i]) {
                now = now->modify(j);
            }
            cell.pb(now);
        }
    }
}

int colour(int l1, int r1, int l2, int r2) {
    l1--, r1--;
    // F = E - V + 1 + C
    // ans = E - V + C - A, A -> cell
    int V = vertex[l2]->query(r1 + 1, r2) - vertex[l1 + 1]->query(r1 + 1, r2) + (l2 - l1 + 1) * 2 + (r2 - r1 + 1) * 2 - 4;
    int E1 = seg_row[l2]->query(r1, r2) - seg_row[l1 + 1]->query(r1, r2) + (r2 - r1) * 2;
    int E2 = seg_col[l2]->query(r1 + 1, r2) - seg_col[l1]->query(r1 + 1, r2) + (l2 - l1) * 2;
    int C = (l1 < mn_r && mx_r + 1 < l2 && r1 < mn_c && mx_c +1 < r2 ? 2 : 1);
    int A = cell[l2]->query(r1, r2) - cell[l1]->query(r1, r2);
    return E1 + E2 - V + C - A;
}

/*
6 4 9 4
3 3
NWESSWEWS
2 3 2 3
3 2 4 4
5 3 6 4
1 2 5 3
 */

Compilation message

rainbow.cpp: In constructor 'Seg::Seg(int, int)':
rainbow.cpp:54:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   54 |     Seg (int _l, int _r) : l(_l), r(_r), m(l + r >> 1) {
      |                                            ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 4 ms 1356 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 3 ms 588 KB Output is correct
5 Correct 4 ms 1612 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 3 ms 692 KB Output is correct
12 Correct 3 ms 1100 KB Output is correct
13 Correct 4 ms 1996 KB Output is correct
14 Correct 5 ms 2508 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 933 ms 263592 KB Output is correct
4 Correct 1152 ms 405944 KB Output is correct
5 Correct 1188 ms 402832 KB Output is correct
6 Correct 1024 ms 322984 KB Output is correct
7 Correct 988 ms 318532 KB Output is correct
8 Correct 695 ms 55088 KB Output is correct
9 Correct 1157 ms 405608 KB Output is correct
10 Correct 1193 ms 402988 KB Output is correct
11 Correct 1041 ms 322852 KB Output is correct
12 Correct 449 ms 381380 KB Output is correct
13 Correct 473 ms 405648 KB Output is correct
14 Correct 491 ms 402796 KB Output is correct
15 Correct 437 ms 323656 KB Output is correct
16 Correct 997 ms 302664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 316 ms 432480 KB Output is correct
3 Correct 327 ms 438900 KB Output is correct
4 Correct 304 ms 435248 KB Output is correct
5 Correct 264 ms 347888 KB Output is correct
6 Correct 128 ms 146736 KB Output is correct
7 Correct 170 ms 213224 KB Output is correct
8 Correct 255 ms 289324 KB Output is correct
9 Correct 228 ms 249204 KB Output is correct
10 Correct 75 ms 60292 KB Output is correct
11 Correct 162 ms 172680 KB Output is correct
12 Correct 314 ms 432572 KB Output is correct
13 Correct 326 ms 438972 KB Output is correct
14 Correct 309 ms 435332 KB Output is correct
15 Correct 268 ms 347920 KB Output is correct
16 Correct 122 ms 134360 KB Output is correct
17 Correct 170 ms 213680 KB Output is correct
18 Correct 317 ms 432864 KB Output is correct
19 Correct 311 ms 432924 KB Output is correct
20 Correct 310 ms 432708 KB Output is correct
21 Correct 260 ms 289356 KB Output is correct
22 Correct 230 ms 249092 KB Output is correct
23 Correct 67 ms 60296 KB Output is correct
24 Correct 162 ms 172620 KB Output is correct
25 Correct 310 ms 432596 KB Output is correct
26 Correct 318 ms 438968 KB Output is correct
27 Correct 305 ms 435332 KB Output is correct
28 Correct 269 ms 347940 KB Output is correct
29 Correct 125 ms 134516 KB Output is correct
30 Correct 169 ms 213672 KB Output is correct
31 Correct 300 ms 432796 KB Output is correct
32 Correct 309 ms 432904 KB Output is correct
33 Correct 310 ms 432680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 4 ms 1356 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 3 ms 588 KB Output is correct
5 Correct 4 ms 1612 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 3 ms 692 KB Output is correct
12 Correct 3 ms 1100 KB Output is correct
13 Correct 4 ms 1996 KB Output is correct
14 Correct 5 ms 2508 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 749 ms 113416 KB Output is correct
19 Correct 256 ms 8772 KB Output is correct
20 Correct 163 ms 4344 KB Output is correct
21 Correct 188 ms 5060 KB Output is correct
22 Correct 202 ms 6080 KB Output is correct
23 Correct 258 ms 8580 KB Output is correct
24 Correct 165 ms 4932 KB Output is correct
25 Correct 194 ms 5808 KB Output is correct
26 Correct 216 ms 6704 KB Output is correct
27 Correct 379 ms 93584 KB Output is correct
28 Correct 351 ms 49872 KB Output is correct
29 Correct 414 ms 86156 KB Output is correct
30 Correct 524 ms 215300 KB Output is correct
31 Correct 3 ms 332 KB Output is correct
32 Correct 589 ms 93796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 4 ms 1356 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 3 ms 588 KB Output is correct
5 Correct 4 ms 1612 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 3 ms 692 KB Output is correct
12 Correct 3 ms 1100 KB Output is correct
13 Correct 4 ms 1996 KB Output is correct
14 Correct 5 ms 2508 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 749 ms 113416 KB Output is correct
19 Correct 256 ms 8772 KB Output is correct
20 Correct 163 ms 4344 KB Output is correct
21 Correct 188 ms 5060 KB Output is correct
22 Correct 202 ms 6080 KB Output is correct
23 Correct 258 ms 8580 KB Output is correct
24 Correct 165 ms 4932 KB Output is correct
25 Correct 194 ms 5808 KB Output is correct
26 Correct 216 ms 6704 KB Output is correct
27 Correct 379 ms 93584 KB Output is correct
28 Correct 351 ms 49872 KB Output is correct
29 Correct 414 ms 86156 KB Output is correct
30 Correct 524 ms 215300 KB Output is correct
31 Correct 3 ms 332 KB Output is correct
32 Correct 589 ms 93796 KB Output is correct
33 Correct 316 ms 432480 KB Output is correct
34 Correct 327 ms 438900 KB Output is correct
35 Correct 304 ms 435248 KB Output is correct
36 Correct 264 ms 347888 KB Output is correct
37 Correct 128 ms 146736 KB Output is correct
38 Correct 170 ms 213224 KB Output is correct
39 Correct 255 ms 289324 KB Output is correct
40 Correct 228 ms 249204 KB Output is correct
41 Correct 75 ms 60292 KB Output is correct
42 Correct 162 ms 172680 KB Output is correct
43 Correct 314 ms 432572 KB Output is correct
44 Correct 326 ms 438972 KB Output is correct
45 Correct 309 ms 435332 KB Output is correct
46 Correct 268 ms 347920 KB Output is correct
47 Correct 122 ms 134360 KB Output is correct
48 Correct 170 ms 213680 KB Output is correct
49 Correct 317 ms 432864 KB Output is correct
50 Correct 311 ms 432924 KB Output is correct
51 Correct 310 ms 432708 KB Output is correct
52 Correct 260 ms 289356 KB Output is correct
53 Correct 230 ms 249092 KB Output is correct
54 Correct 67 ms 60296 KB Output is correct
55 Correct 162 ms 172620 KB Output is correct
56 Correct 310 ms 432596 KB Output is correct
57 Correct 318 ms 438968 KB Output is correct
58 Correct 305 ms 435332 KB Output is correct
59 Correct 269 ms 347940 KB Output is correct
60 Correct 125 ms 134516 KB Output is correct
61 Correct 169 ms 213672 KB Output is correct
62 Correct 300 ms 432796 KB Output is correct
63 Correct 309 ms 432904 KB Output is correct
64 Correct 310 ms 432680 KB Output is correct
65 Correct 933 ms 263592 KB Output is correct
66 Correct 1152 ms 405944 KB Output is correct
67 Correct 1188 ms 402832 KB Output is correct
68 Correct 1024 ms 322984 KB Output is correct
69 Correct 988 ms 318532 KB Output is correct
70 Correct 695 ms 55088 KB Output is correct
71 Correct 1157 ms 405608 KB Output is correct
72 Correct 1193 ms 402988 KB Output is correct
73 Correct 1041 ms 322852 KB Output is correct
74 Correct 449 ms 381380 KB Output is correct
75 Correct 473 ms 405648 KB Output is correct
76 Correct 491 ms 402796 KB Output is correct
77 Correct 437 ms 323656 KB Output is correct
78 Correct 997 ms 302664 KB Output is correct
79 Correct 1191 ms 289404 KB Output is correct
80 Correct 1158 ms 249304 KB Output is correct
81 Correct 515 ms 62084 KB Output is correct
82 Correct 756 ms 172752 KB Output is correct
83 Correct 1161 ms 432624 KB Output is correct
84 Correct 1130 ms 442472 KB Output is correct
85 Correct 1114 ms 437524 KB Output is correct
86 Correct 1034 ms 349220 KB Output is correct
87 Correct 852 ms 136664 KB Output is correct
88 Correct 909 ms 213844 KB Output is correct
89 Correct 1032 ms 433000 KB Output is correct
90 Correct 1167 ms 433864 KB Output is correct
91 Correct 968 ms 433852 KB Output is correct