結果

問題 No.1087 転倒数の転倒数
ユーザー ミドリムシミドリムシ
提出日時 2020-06-19 23:45:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 11,916 bytes
コンパイル時間 2,039 ms
コンパイル使用メモリ 186,008 KB
実行使用メモリ 7,528 KB
最終ジャッジ日時 2024-07-03 16:03:39
合計ジャッジ時間 16,881 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 107 ms
7,332 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 3 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 106 ms
7,528 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 87 ms
6,948 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 8 ms
6,944 KB
testcase_25 AC 81 ms
6,940 KB
testcase_26 AC 73 ms
6,940 KB
testcase_27 AC 48 ms
6,940 KB
testcase_28 AC 43 ms
6,940 KB
testcase_29 AC 60 ms
6,940 KB
testcase_30 AC 29 ms
6,940 KB
testcase_31 AC 79 ms
6,940 KB
testcase_32 AC 80 ms
6,940 KB
testcase_33 AC 50 ms
6,944 KB
testcase_34 AC 106 ms
7,456 KB
testcase_35 AC 105 ms
7,384 KB
testcase_36 AC 105 ms
7,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, r) for(int i = int(l); i < int(r); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, r) for(int i = int(r) - 1; i >= int(l); i--)
#define SZ(x) int(x.size())
constexpr int inf9 = 1e9; constexpr lint inf18 = 1e18;
inline void YES(bool condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
inline void Yes(bool condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
inline void assertNO(bool condition){ if(!condition){ cout << "NO" << endl; exit(0); } }
inline void assertNo(bool condition){ if(!condition){ cout << "No" << endl; exit(0); } }
inline void assertm1(bool condition){ if(!condition){ cout << -1 << endl; exit(0); } }
lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int y, x; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } string to_string(string x){ return x; }
template<class T> string to_string(complex<T> x){ return to_string(make_pair(x.real(), x.imag())); }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) cout << (i == start ? "" : " ") << (*i); if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(b) return gcd(b, a % b); else return a; }
template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint P(int n, int r){ if(r < 0 || n < r) return 0; return (fact[n] * inv[n - r] % mod); } lint C(int p, int q){ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };
template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; }
template<class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2){ itr now = last; itr2 now2 = last2; while(now != first){ now--, now2--; (*now)++; if((*now) == (*now2)){ (*now) = 0; }else{ return true; } } return false; }
template<class T> bool chmax(T &a, const T &b){ if(a < b){ a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b){ if(b < a){ a = b; return 1; } return 0; }
inline int at(lint i, int j){ return (i >> j) & 1; }
random_device rnd;
bool is_in_board(lint y, lint x, lint H, lint W){ return (0 <= y && y < H && 0 <= x && x < W); }

template< class Monoid, class OperatorMonoid = Monoid >
struct RandomizedBinarySearchTree
{
    using F = function< Monoid(Monoid, Monoid) >;
    using G = function< Monoid(Monoid, OperatorMonoid) >;
    using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;
    using P = function< OperatorMonoid(OperatorMonoid, int) >;
    
    inline int xor128()
    {
        static int x = 123456789;
        static int y = 362436069;
        static int z = 521288629;
        static int w = 88675123;
        int t;
        
        t = x ^ (x << 11);
        x = y;
        y = z;
        z = w;
        return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    }
    
    struct Node
    {
        Node *l, *r;
        int cnt;
        Monoid key, sum;
        OperatorMonoid lazy;
        
        Node() {}
        
        Node(const Monoid &k, const OperatorMonoid &p) : cnt(1), key(k), sum(k), lazy(p), l(nullptr), r(nullptr) {}
    };
    
    vector< Node > pool;
    int ptr;
    
    const Monoid M1;
    const OperatorMonoid OM0;
    const F f;
    const G g;
    const H h;
    const P p;
    
    RandomizedBinarySearchTree(int sz, const F &f, const Monoid &M1) :
    pool(sz), ptr(0), f(f), g(G()), h(H()), p(P()), M1(M1), OM0(OperatorMonoid()) {}
    
    RandomizedBinarySearchTree(int sz, const F &f, const G &g, const H &h, const P &p,
                               const Monoid &M1, const OperatorMonoid &OM0) :
    pool(sz), ptr(0), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {}
    
    inline Node *alloc(const Monoid &key) { return &(pool[ptr++] = Node(key, OM0)); }
    
    virtual Node *clone(Node *t) { return t; }
    
    inline int count(const Node *t) { return t ? t->cnt : 0; }
    
    inline Monoid sum(const Node *t) { return t ? t->sum : M1; }
    
    inline Node *update(Node *t)
    {
        t->cnt = count(t->l) + count(t->r) + 1;
        t->sum = f(f(sum(t->l), sum(t->r)), t->key);
        return t;
    }
    
    Node *propagete(Node *t)
    {
        t = clone(t);
        if(t->lazy != OM0) {
            t->key = g(t->key, t->lazy);
            if(t->l) {
                t->l = clone(t->l);
                t->l->lazy = h(t->l->lazy, t->lazy);
                t->l->sum = f(t->l->sum, p(t->lazy, count(t->l)));
            }
            if(t->r) {
                t->r = clone(t->r);
                t->r->lazy = h(t->r->lazy, t->lazy);
                t->r->sum = f(t->r->sum, p(t->lazy, count(t->r)));
            }
            t->lazy = OM0;
        }
        return update(t);
    }
    
    Node *merge(Node *l, Node *r)
    {
        if(!l || !r) return l ? l : r;
        if(xor128() % (l->cnt + r->cnt) < l->cnt) {
            l = propagete(l);
            l->r = merge(l->r, r);
            return update(l);
        } else {
            r = propagete(r);
            r->l = merge(l, r->l);
            return update(r);
        }
    }
    
    pair< Node *, Node * > split(Node *t, int k)
    {
        if(!t) return {t, t};
        t = propagete(t);
        if(k <= count(t->l)) {
            auto s = split(t->l, k);
            t->l = s.second;
            return {s.first, update(t)};
        } else {
            auto s = split(t->r, k - count(t->l) - 1);
            t->r = s.first;
            return {update(t), s.second};
        }
    }
    
    Node *build(int l, int r, const vector< Monoid > &v)
    {
        if(l + 1 >= r) return alloc(v[l]);
        return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));
    }
    
    Node *build(const vector< Monoid > &v)
    {
        ptr = 0;
        return build(0, (int) v.size(), v);
    }
    
    void dump(Node *r, typename vector< Monoid >::iterator &it)
    {
        if(!r) return;
        r = propagete(r);
        dump(r->l, it);
        *it = r->key;
        dump(r->r, ++it);
    }
    
    vector< Monoid > dump(Node *r)
    {
        vector< Monoid > v((size_t) count(r));
        auto it = begin(v);
        dump(r, it);
        return v;
    }
    
    string to_string(Node *r)
    {
        auto s = dump(r);
        string ret;
        for(int i = 0; i < s.size(); i++) ret += ", ";
        return (ret);
    }
    
    void insert(Node *&t, int k, const Monoid &v)
    {
        auto x = split(t, k);
        t = merge(merge(x.first, alloc(v)), x.second);
    }
    
    void erase(Node *&t, int k)
    {
        auto x = split(t, k);
        t = merge(x.first, split(x.second, 1).second);
    }
    
    Monoid query(Node *&t, int a, int b)
    {
        auto x = split(t, a);
        auto y = split(x.second, b - a);
        auto ret = sum(y.first);
        t = merge(x.first, merge(y.first, y.second));
        return ret;
    }
    
    void set_propagate(Node *&t, int a, int b, const OperatorMonoid &p)
    {
        auto x = split(t, a);
        auto y = split(x.second, b - a);
        y.first->lazy = h(y.first->lazy, p);
        t = merge(x.first, merge(propagete(y.first), y.second));
    }
    
    void set_element(Node *&t, int k, const Monoid &x)
    {
        t = propagete(t);
        if(k < count(t->l)) set_element(t->l, k, x);
        else if(k == count(t->l)) t->key = t->sum = x;
        else set_element(t->r, k - count(t->l) - 1, x);
        t = update(t);
    }
    
    
    int size(Node *t)
    {
        return count(t);
    }
    
    bool empty(Node *t)
    {
        return !t;
    }
    
    Node *makeset()
    {
        return (nullptr);
    }
};

template< class T >
struct OrderedMultiSet : RandomizedBinarySearchTree< T >
{
    using RBST = RandomizedBinarySearchTree< T >;
    using Node = typename RBST::Node;
    
    OrderedMultiSet(int sz) : RBST(sz, [&](T x, T y) { return x; }, T()) {}
    
    T kth_element(Node *t, int k)
    {
        if(k < RBST::count(t->l)) return kth_element(t->l, k);
        if(k == RBST::count(t->l)) return t->key;
        return kth_element(t->r, k - RBST::count(t->l) - 1);
    }
    
    virtual void insert_key(Node *&t, const T &x)
    {
        RBST::insert(t, lower_bound(t, x), x);
    }
    
    void erase_key(Node *&t, const T &x)
    {
        if(!count(t, x)) return;
        RBST::erase(t, lower_bound(t, x));
    }
    
    int count(Node *t, const T &x)
    {
        return upper_bound(t, x) - lower_bound(t, x);
    }
    
    int lower_bound(Node *t, const T &x)
    {
        if(!t) return 0;
        if(x <= t->key) return lower_bound(t->l, x);
        return lower_bound(t->r, x) + RBST::count(t->l) + 1;
    }
    
    int upper_bound(Node *t, const T &x)
    {
        if(!t) return 0;
        if(x < t->key) return upper_bound(t->l, x);
        return upper_bound(t->r, x) + RBST::count(t->l) + 1;
    }
};

template< class T >
struct OrderedSet : OrderedMultiSet< T >
{
    using SET = OrderedMultiSet< T >;
    using RBST = typename SET::RBST;
    using Node = typename RBST::Node;
    
    OrderedSet(int sz) : OrderedMultiSet< T >(sz) {}
    
    void insert_key(Node *&t, const T &x) override
    {
        if(SET::count(t, x)) return;
        RBST::insert(t, SET::lower_bound(t, x), x);
    }
};

int main(){
    int N, K;
    cin >> N >> K;
    if(N * (N - 1) < K){
        cout << "No" << endl;
        return 0;
    }
    if(N == 1){
        cout << "Yes\n0" << endl;
        return 0;
    }
    if(N == 2 && K == 1){
        cout << "Yes\n3 2\n0 1" << endl;
        return 0;
    }
    int inversion = (K + 1) / 2;
    OrderedSet<int> inversion_set(N);
    auto root = inversion_set.makeset();
    rep(i, N){
        inversion_set.insert_key(root, i);
    }
    vector<int> inversed_array(N);
    rep(i, N){
        int inv_cnt = min(N - i - 1, inversion);
        int num = inversion_set.kth_element(root, inv_cnt);
        inversed_array[num] = i;
        inversion_set.erase_key(root, num);
        inversion -= inv_cnt;
    }
    int zero_pos = int(find(all(inversed_array), 0) - inversed_array.begin());
    int ans[N][N];
    rep(i, N){
        rep(j, N){
            ans[i][j] = -1;
        }
        ans[i][N - inversed_array[i] - 1] = N * (N - 1) + i;
    }
    int cnt = 0;
    rep(i, N){
        rep(j, N){
            if(ans[i][j] == -1){
                ans[i][j] = cnt;
                cnt++;
            }
        }
    }
    if(K % 2){
        swap(ans[zero_pos][0], ans[zero_pos][1]);
    }
    cout << "Yes" << endl;
    rep(i, N){
        array_output(ans[i], ans[i] + N);
    }
}

0