답안 #557072

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
557072 2022-05-04T17:09:30 Z nutella 무지개나라 (APIO17_rainbow) C++17
12 / 100
298 ms 89636 KB
#include "rainbow.h"

//#define _GLIBCXX_DEBUG

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;

//#include <ext/pb_ds/assoc_container.hpp>
//
//using namespace __gnu_pbds;
//
//template<typename T>
//using ordered_set = tree<T, null_type, less < T>, rb_tree_tag, tree_order_statistics_node_update>;

template<typename T>
using normal_queue = priority_queue<T, vector<T>, greater<>>;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

#define trace(x) cout << #x << ": " << (x) << endl;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define uniq(x) x.resize(unique(all(x)) - begin(x))
#define sz(s) ((int) size(s))
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define int128 __int128
#define pb push_back
#define popb pop_back
#define eb emplace_back
#define fi first
#define se second
#define itn int

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;


template<typename T>
bool ckmn(T &x, T y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}

template<typename T>
bool ckmx(T &x, T y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

int bit(int x, int b) {
    return (x >> b) & 1;
}

int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }


const ll infL = 3e18;
const int infI = 1000000000 + 7;
const int infM = 0x3f3f3f3f; //a little bigger than 1e9
const ll infML = 0x3f3f3f3f3f3f3f3fLL; //4.5e18

template<typename T>
void make_uniq(vector<T> &v) {
    sort(all(v));
    v.resize(unique(all(v)) - begin(v));
}

struct Fenwick {
    vector<vector<ll>> t;
    vector<vector<int>> yy;
    int n;


    Fenwick() = default;

    void init(int a) {
        n = a;
        yy.resize(n);
        t.resize(n);
    }

    void fake_add(int x, int y) {
        for (int i = x; i < n; i |= (i + 1))
            yy[i].pb(y);
    }

    void build() {
        for (int i = 0; i < n; ++i) {
            make_uniq(yy[i]);
            t[i].resize(sz(yy[i]) + 2);
        }
    }

    void add(int x, int y) {
        for (int i = x; i < n; i |= (i + 1))
            for (int j = upper_bound(all(yy[i]), y) - begin(yy[i]) - 1; j < sz(yy[i]); j |= (j + 1))
                ++t[i][j];
    }

    ll get(int x, int y) {
        ll ans = 0;
        for (int i = x; i > -1; i = ((i + 1) & i) - 1)
            for (int j = upper_bound(all(yy[i]), y) - begin(yy[i]) - 1; j > -1; j = ((j + 1) & j) - 1)
                ans += t[i][j];
        return ans;
    }

    ll get(int x1, int y1, int x2, int y2) {
        return get(x2, y2) - get(x1 - 1, y2) - get(x2, y1 - 1) + get(x1 - 1, y1 - 1);
    }

};
const int dx[4] = {0, 0, 1, -1};

const int dy[4] = {1, -1, 0, 0};
int n, m;
vector<pair<int, int>> yy;

Fenwick fx, fy, fv, cv;
int mnx = infI, mny = infI, mxx = -infI, mxy = -infI;

void init(int R, int C, int sr, int sc, int M, char *S) {
    n = R, m = C;
    --sr, --sc;
    fx.init(n), fy.init(n), fv.init(n), cv.init(n);
    yy = {{sr, sc}};
    for (int i = 0; i < M; ++i) {
        if (S[i] == 'N') --sr;
        else if (S[i] == 'S') ++sr;
        else if (S[i] == 'W') --sc;
        else ++sc;
        yy.emplace_back(sr, sc);
    }
    make_uniq(yy);

    auto here = [](int x, int y) -> bool {
        auto it = lower_bound(all(yy), mp(x, y));
        if (it == end(yy)) return false;
        return (*it) == mp(x, y);
    };
    vector<pii > pv, px, py, cvv;
    for (auto [x, y]: yy) {;
        pv.emplace_back(x, y);
        ckmx(mxx, x);
        ckmx(mxy, y);
        ckmn(mnx, x);
        ckmn(mny, y);
        for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if (here(nx, ny)) {
                if (nx == x) {
                    px.emplace_back(x, min(ny, y));
                } else {
                    py.emplace_back(min(nx, x), y);
                }
            }
        }
        if (here(x + 1, y) && here(x, y + 1) && here(x + 1, y + 1)) {
            cvv.emplace_back(x, y);
        }
    }

    make_uniq(pv), make_uniq(px), make_uniq(py), make_uniq(cvv);

    for (auto [x, y] : pv)
        fv.fake_add(x, y);
    for (auto [x, y] : px)
        fx.fake_add(x, y);
    for (auto [x, y] : py)
        fy.fake_add(x, y);
    for (auto [x, y] : cvv)
        cv.fake_add(x, y);

    fv.build(), fx.build(), fy.build(), cv.build();

    for (auto [x, y] : pv)
        fv.add(x, y);
    for (auto [x, y] : px)
        fx.add(x, y);
    for (auto [x, y] : py)
        fy.add(x, y);
    for (auto [x, y] : cvv)
        cv.add(x, y);


}

int colour(int ar, int ac, int br, int bc) {
    int x1 = ar, y1 = ac, x2 = br, y2 = bc;
    --x1, --y1;
    --x2, --y2;
    int C = 1 + (x1 < mnx && mxx < x2 && y1 < mny && mxy < y2);
    ll E = (x2 - x1) * 2 + (y2 - y1) * 2 + fx.get(x1 + 1, y1, x2 - 1, y2 - 1) + fy.get(x1, y1 + 1, x2 - 1, y2 - 1);
    ll V = (x2 - x1 + 1) * 2 + (y2 - y1 - 1) * 2 + fv.get(x1 + 1, y1 + 1, x2 - 1, y2 - 1);
    ll U = cv.get(x1, y1, x2 - 1, y2 - 1);
    ll F = C + 1 + E - V;
    int ans = F - U - 1;
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 464 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 135 ms 4268 KB Output is correct
4 Correct 195 ms 7316 KB Output is correct
5 Correct 208 ms 7496 KB Output is correct
6 Correct 210 ms 8184 KB Output is correct
7 Correct 187 ms 6208 KB Output is correct
8 Correct 63 ms 1936 KB Output is correct
9 Correct 207 ms 7272 KB Output is correct
10 Correct 208 ms 7592 KB Output is correct
11 Correct 217 ms 8120 KB Output is correct
12 Correct 136 ms 6632 KB Output is correct
13 Correct 132 ms 7332 KB Output is correct
14 Correct 158 ms 7420 KB Output is correct
15 Correct 153 ms 8120 KB Output is correct
16 Correct 156 ms 5936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 226 ms 85528 KB Output is correct
3 Correct 173 ms 78020 KB Output is correct
4 Correct 298 ms 89636 KB Output is correct
5 Correct 166 ms 76936 KB Output is correct
6 Correct 147 ms 69968 KB Output is correct
7 Incorrect 226 ms 74308 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 464 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 464 KB Output isn't correct
2 Halted 0 ms 0 KB -