結果

問題 No.5010 Better Mo's Algorithm is Needed!! (Unweighted)
ユーザー komori3komori3
提出日時 2022-12-17 11:21:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,921 ms / 5,000 ms
コード長 11,950 bytes
コンパイル時間 3,387 ms
実行使用メモリ 6,952 KB
スコア 19,094,352,396
最終ジャッジ日時 2022-12-17 11:31:56
合計ジャッジ時間 637,967 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4,917 ms
6,516 KB
testcase_01 AC 4,917 ms
6,596 KB
testcase_02 AC 4,917 ms
6,504 KB
testcase_03 AC 4,917 ms
6,672 KB
testcase_04 AC 4,918 ms
6,516 KB
testcase_05 AC 4,917 ms
6,596 KB
testcase_06 AC 4,917 ms
6,636 KB
testcase_07 AC 4,918 ms
6,600 KB
testcase_08 AC 4,917 ms
6,636 KB
testcase_09 AC 4,917 ms
6,616 KB
testcase_10 AC 4,916 ms
6,680 KB
testcase_11 AC 4,916 ms
6,596 KB
testcase_12 AC 4,916 ms
6,504 KB
testcase_13 AC 4,915 ms
6,832 KB
testcase_14 AC 4,917 ms
6,836 KB
testcase_15 AC 4,915 ms
6,632 KB
testcase_16 AC 4,917 ms
6,848 KB
testcase_17 AC 4,917 ms
6,952 KB
testcase_18 AC 4,916 ms
6,844 KB
testcase_19 AC 4,918 ms
6,824 KB
testcase_20 AC 4,915 ms
6,580 KB
testcase_21 AC 4,917 ms
6,840 KB
testcase_22 AC 4,917 ms
6,620 KB
testcase_23 AC 4,916 ms
6,532 KB
testcase_24 AC 4,917 ms
6,576 KB
testcase_25 AC 4,916 ms
6,520 KB
testcase_26 AC 4,917 ms
6,596 KB
testcase_27 AC 4,916 ms
6,532 KB
testcase_28 AC 4,917 ms
6,532 KB
testcase_29 AC 4,916 ms
6,672 KB
testcase_30 AC 4,917 ms
6,528 KB
testcase_31 AC 4,917 ms
6,500 KB
testcase_32 AC 4,918 ms
6,568 KB
testcase_33 AC 4,916 ms
6,848 KB
testcase_34 AC 4,918 ms
6,532 KB
testcase_35 AC 4,916 ms
6,596 KB
testcase_36 AC 4,917 ms
6,836 KB
testcase_37 AC 4,917 ms
6,680 KB
testcase_38 AC 4,918 ms
6,532 KB
testcase_39 AC 4,917 ms
6,732 KB
testcase_40 AC 4,916 ms
6,628 KB
testcase_41 AC 4,917 ms
6,516 KB
testcase_42 AC 4,916 ms
6,616 KB
testcase_43 AC 4,917 ms
6,532 KB
testcase_44 AC 4,917 ms
6,684 KB
testcase_45 AC 4,917 ms
6,516 KB
testcase_46 AC 4,918 ms
6,528 KB
testcase_47 AC 4,916 ms
6,532 KB
testcase_48 AC 4,921 ms
6,592 KB
testcase_49 AC 4,917 ms
6,532 KB
testcase_50 AC 4,916 ms
6,636 KB
testcase_51 AC 4,917 ms
6,588 KB
testcase_52 AC 4,916 ms
6,592 KB
testcase_53 AC 4,917 ms
6,536 KB
testcase_54 AC 4,917 ms
6,636 KB
testcase_55 AC 4,918 ms
6,676 KB
testcase_56 AC 4,917 ms
6,632 KB
testcase_57 AC 4,916 ms
6,528 KB
testcase_58 AC 4,917 ms
6,588 KB
testcase_59 AC 4,917 ms
6,520 KB
testcase_60 AC 4,916 ms
6,516 KB
testcase_61 AC 4,917 ms
6,836 KB
testcase_62 AC 4,917 ms
6,532 KB
testcase_63 AC 4,917 ms
6,632 KB
testcase_64 AC 4,917 ms
6,592 KB
testcase_65 AC 4,917 ms
6,596 KB
testcase_66 AC 4,916 ms
6,628 KB
testcase_67 AC 4,916 ms
6,632 KB
testcase_68 AC 4,918 ms
6,728 KB
testcase_69 AC 4,916 ms
6,592 KB
testcase_70 AC 4,917 ms
6,596 KB
testcase_71 AC 4,918 ms
6,596 KB
testcase_72 AC 4,916 ms
6,632 KB
testcase_73 AC 4,917 ms
6,504 KB
testcase_74 AC 4,917 ms
6,504 KB
testcase_75 AC 4,917 ms
6,532 KB
testcase_76 AC 4,918 ms
6,592 KB
testcase_77 AC 4,916 ms
6,840 KB
testcase_78 AC 4,918 ms
6,836 KB
testcase_79 AC 4,917 ms
6,504 KB
testcase_80 AC 4,917 ms
6,848 KB
testcase_81 AC 4,917 ms
6,548 KB
testcase_82 AC 4,918 ms
6,836 KB
testcase_83 AC 4,916 ms
6,516 KB
testcase_84 AC 4,917 ms
6,528 KB
testcase_85 AC 4,917 ms
6,580 KB
testcase_86 AC 4,916 ms
6,528 KB
testcase_87 AC 4,916 ms
6,536 KB
testcase_88 AC 4,918 ms
6,528 KB
testcase_89 AC 4,915 ms
6,592 KB
testcase_90 AC 4,917 ms
6,524 KB
testcase_91 AC 4,916 ms
6,684 KB
testcase_92 AC 4,917 ms
6,732 KB
testcase_93 AC 4,916 ms
6,732 KB
testcase_94 AC 4,917 ms
6,692 KB
testcase_95 AC 4,917 ms
6,576 KB
testcase_96 AC 4,917 ms
6,520 KB
testcase_97 AC 4,917 ms
6,584 KB
testcase_98 AC 4,917 ms
6,516 KB
testcase_99 AC 4,917 ms
6,816 KB
testcase_100 AC 4,918 ms
6,684 KB
testcase_101 AC 4,916 ms
6,692 KB
testcase_102 AC 4,918 ms
6,680 KB
testcase_103 AC 4,917 ms
6,500 KB
testcase_104 AC 4,916 ms
6,632 KB
testcase_105 AC 4,917 ms
6,628 KB
testcase_106 AC 4,916 ms
6,636 KB
testcase_107 AC 4,918 ms
6,500 KB
testcase_108 AC 4,916 ms
6,508 KB
testcase_109 AC 4,917 ms
6,512 KB
testcase_110 AC 4,916 ms
6,812 KB
testcase_111 AC 4,916 ms
6,680 KB
testcase_112 AC 4,917 ms
6,684 KB
testcase_113 AC 4,914 ms
6,512 KB
testcase_114 AC 4,916 ms
6,612 KB
testcase_115 AC 4,918 ms
6,820 KB
testcase_116 AC 4,916 ms
6,500 KB
testcase_117 AC 4,917 ms
6,636 KB
testcase_118 AC 4,917 ms
6,552 KB
testcase_119 AC 4,917 ms
6,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_NONSTDC_NO_WARNINGS
#define _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING
#include <bits/stdc++.h>
#include <random>
#include <unordered_set>
#include <array>
#include <optional>
#ifdef _MSC_VER
#include <opencv2/core.hpp>
#include <opencv2/core/utils/logger.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <conio.h>
#include <ppl.h>
#include <filesystem>
#include <intrin.h>
/* g++ functions */
int __builtin_clz(unsigned int n) { unsigned long index; _BitScanReverse(&index, n); return 31 - index; }
int __builtin_ctz(unsigned int n) { unsigned long index; _BitScanForward(&index, n); return index; }
namespace std { inline int __lg(int __n) { return sizeof(int) * 8 - 1 - __builtin_clz(__n); } }
/* enable __uint128_t in MSVC */
//#include <boost/multiprecision/cpp_int.hpp>
//using __uint128_t = boost::multiprecision::uint128_t;
#else
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

/** compro io **/
namespace aux { 
    template<typename T, unsigned N, unsigned L> struct tp { static void output(std::ostream& os, const T& v) { os << std::get<N>(v) << ", "; tp<T, N + 1, L>::output(os, v); } };
    template<typename T, unsigned N> struct tp<T, N, N> { static void output(std::ostream& os, const T& v) { os << std::get<N>(v); } };
}
template<typename... Ts> std::ostream& operator<<(std::ostream& os, const std::tuple<Ts...>& t) { os << '['; aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t); return os << ']'; } // tuple out
template<class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, const Container& x); // container out (fwd decl)
template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p) { return os << "[" << p.first << ", " << p.second << "]"; } // pair out
template<class S, class T> std::istream& operator>>(std::istream& is, std::pair<S, T>& p) { return is >> p.first >> p.second; } // pair in
std::ostream& operator<<(std::ostream& os, const std::vector<bool>::reference& v) { os << (v ? '1' : '0'); return os; } // bool (vector) out
std::ostream& operator<<(std::ostream& os, const std::vector<bool>& v) { bool f = true; os << "["; for (const auto& x : v) { os << (f ? "" : ", ") << x; f = false; } os << "]"; return os; } // vector<bool> out
template<class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, const Container& x) { bool f = true; os << "["; for (auto& y : x) { os << (f ? "" : ", ") << y; f = false; } return os << "]"; } // container out
template<class T, class = decltype(std::begin(std::declval<T&>())), class = typename std::enable_if<!std::is_same<T, std::string>::value>::type> std::istream& operator>>(std::istream& is, T& a) { for (auto& x : a) is >> x; return is; } // container in
template<typename T> auto operator<<(std::ostream& out, const T& t) -> decltype(out << t.stringify()) { out << t.stringify(); return out; } // struct (has stringify() func) out
/** io setup **/
struct IOSetup { IOSetup(bool f) { if (f) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } std::cout << std::fixed << std::setprecision(15); } }
iosetup(true); // set false when solving interective problems
/** string formatter **/
template<typename... Ts> std::string format(const std::string& f, Ts... t) { size_t l = std::snprintf(nullptr, 0, f.c_str(), t...); std::vector<char> b(l + 1); std::snprintf(&b[0], l + 1, f.c_str(), t...); return std::string(&b[0], &b[0] + l); }
/** dump **/
#define DUMPOUT std::cerr
std::ostringstream DUMPBUF;
#define dump(...) do{DUMPBUF<<"  ";DUMPBUF<<#__VA_ARGS__<<" :[DUMP - "<<__LINE__<<":"<<__FUNCTION__<<"]"<<std::endl;DUMPBUF<<"    ";dump_func(__VA_ARGS__);DUMPOUT<<DUMPBUF.str();DUMPBUF.str("");DUMPBUF.clear();}while(0);
void dump_func() { DUMPBUF << std::endl; }
template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) { DUMPBUF << head; if (sizeof...(Tail) == 0) { DUMPBUF << " "; } else { DUMPBUF << ", "; } dump_func(std::move(tail)...); }
/** timer **/
class Timer {
    double t = 0, paused = 0, tmp;
public:
    Timer() { reset(); }
    static double time() {
#ifdef _MSC_VER
        return __rdtsc() / 2.3e9;
#else
        unsigned long long a, d;
        __asm__ volatile("rdtsc"
            : "=a"(a), "=d"(d));
        return (d << 32 | a) / 2.3e9;
#endif
    }
    void reset() { t = time(); }
    void pause() { tmp = time(); }
    void restart() { paused += time() - tmp; }
    double elapsed_ms() const { return (time() - t - paused) * 1000.0; }
};
/** rand **/
struct Xorshift {
    static constexpr uint64_t M = INT_MAX;
    static constexpr double e = 1.0 / M;
    uint64_t x = 88172645463325252LL;
    Xorshift() {}
    Xorshift(uint64_t seed) { reseed(seed); }
    inline void reseed(uint64_t seed) { x = 0x498b3bc5 ^ seed; for (int i = 0; i < 20; i++) next(); }
    inline uint64_t next() { x = x ^ (x << 7); return x = x ^ (x >> 9); }
    inline int next_int() { return next() & M; }
    inline int next_int(int mod) { return next() % mod; }
    inline int next_int(int l, int r) { return l + next_int(r - l + 1); }
    inline double next_double() { return next_int() * e; }
};
/** shuffle **/
template<typename T> void shuffle_vector(std::vector<T>& v, Xorshift& rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); std::swap(v[i], v[r]); } }
/** split **/
std::vector<std::string> split(std::string str, const std::string& delim) { for (char& c : str) if (delim.find(c) != std::string::npos) c = ' '; std::istringstream iss(str); std::vector<std::string> parsed; std::string buf; while (iss >> buf) parsed.push_back(buf); return parsed; }
/** misc **/
template<typename A, size_t N, typename T> inline void Fill(A(&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } // fill array
template<typename T, typename ...Args> auto make_vector(T x, int arg, Args ...args) { if constexpr (sizeof...(args) == 0)return std::vector<T>(arg, x); else return std::vector(arg, make_vector<T>(x, args...)); }
template<typename T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }
template<typename T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
/** using **/
using ll = long long;
using ld = long double;
//using ld = boost::multiprecision::cpp_bin_float_quad;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using std::cin, std::cout, std::cerr, std::endl, std::string, std::vector, std::array;


double ltbl[0x10000];

void initialize() {
    Xorshift rnd;
    for(int i = 0; i < 1000; i++) rnd.next_int();
    for(int i = 0; i < 0x10000; i++) ltbl[i] = log(rnd.next_double());

    //int ctr[] = { 0,64,32,16,8,4,2,1,1 };
    //vector<int> arr;
    //for (int c = 0; c <= 9; c++) for (int k = 0; k < ctr[c]; k++) arr.push_back(c);
    //dump(arr);
    //exit(1);
}

int64_t hilbert_order(int x, int y) {
    static constexpr int maxn = 1 << 18;
    int64_t rx, ry, d = 0;
    for (int64_t s = maxn >> 1; s; s >>= 1) {
        rx = (x & s) > 0, ry = (y & s) > 0;
        d += s * s * ((rx * 3) ^ ry);
        if (ry) continue;
        if (rx) {
            x = maxn - 1 - x;
            y = maxn - 1 - y;
        }
        std::swap(x, y);
    }
    return d;
}

struct MeanVar {
    ll ctr = 0;
    ll sum = 0;
    ll sqsum = 0;
    ll hist[101] = {};
    void inc(ll val) {
        ctr++;
        sum += val;
        sqsum += val * val;
        if(val <= 100) hist[val]++;
    }
    void dec(ll val) {
        ctr--;
        sum -= val;
        sqsum -= val * val;
        if (val <= 100) hist[val]--;
    }
    double mean() const {
        return (double)sum / ctr;
    }
    double var() const {
        double m = mean();
        return (double)sqsum / ctr - m * m;
    }
    void print_hist() const {
        auto max_val = *std::max_element(hist, hist + 101);
        for (int i = 0; i <= 100; i++) cerr << format("%3d: %7d ", i, hist[i]) << string(hist[i] * 100 / max_val, '*') << '\n';
    }
};

struct State {

    static constexpr int N = 200000;
    static constexpr int Q = 200000;

    Xorshift rnd;
    
    int ST;
    int L[Q], R[Q];

    int P[Q];

    int64_t cost;

    State(std::istream& in) {

        in >> ST >> ST >> ST >> ST;
        for (int i = 0; i < N; i++) in >> L[0]; // ignore weights
        for (int i = 0; i < Q; i++) in >> L[i] >> R[i];

        std::iota(P, P + Q, 0);
        if (true) {
            //int bs = std::max<int>(1, 1.0 * N / std::max<double>(1.0, sqrt(Q * 2.0 / 3.0)));
            int bs = 1000;
            //dump(bs);
            std::sort(P, P + Q, [&](int a, int b) {
                int ablock = L[a] / bs, bblock = L[b] / bs;
                if (ablock != bblock) return ablock < bblock;
                return (ablock & 1) ? R[a] > R[b] : R[a] < R[b];
                });
        }
        else {
            int64_t e[Q];
            for (int i = 0; i < Q; i++) e[i] = hilbert_order(L[i], R[i]);
            std::sort(P, P + Q, [&](int i, int j) { return e[i] < e[j]; });
        }

        cost = R[P[0]] - L[P[0]] + 1;
        for (int k = 0; k < Q - 1; k++) cost += calc_adj_cost(k);

    }

    void run(double duration) {
        
        Timer timer;
        size_t loop = 0;
        MeanVar mv;

        auto get_temp = [](double start_temp, double end_temp, double now_time, double end_time) {
            return end_temp + (start_temp - end_temp) * (end_time - now_time) / end_time;
        };

        constexpr double start_temp = 100.0, end_temp = 0.0;
        double now_time = timer.elapsed_ms();
        double temp = get_temp(start_temp, end_temp, now_time, duration);
        while(true) {
            loop++;
            //int l = rnd.next_int(Q - 1), r = std::min(Q - 1, l + rnd.next_int(1, 10));
            int l = loop % (Q - 1), r = std::min(Q - 1, l + rnd.next_int(1, 10));

            int diff = calc_rev_cost(l, r);
#if 1
            if (-diff > temp * ltbl[loop & 0xFFFF]) {
#else
            if(diff < 0) {
#endif
                std::reverse(P + l, P + r + 1);
                cost += diff;
            }

            if (!(loop & 0xFFFFFF)) {
                dump(loop, cost);
            }
            if (!(loop & 0xFFFF)) {
                now_time = timer.elapsed_ms();
                if (now_time > duration) break;
                temp = get_temp(start_temp, end_temp, now_time, duration);
            }
        }

    }

    inline int calc_adj_cost(int k) const {
        return abs(L[P[k]] - L[P[k + 1]]) + abs(R[P[k]] - R[P[k + 1]]);
    }

    int calc_rev_cost(int l, int r) {
        int diff = 0;
        diff -= l ? calc_adj_cost(l - 1) : (R[P[0]] - L[P[0]] + 1);
        diff -= (r + 1 < Q) ? calc_adj_cost(r) : 0;
        std::swap(P[l], P[r]);
        diff += l ? calc_adj_cost(l - 1) : (R[P[0]] - L[P[0]] + 1);
        diff += (r + 1 < Q) ? calc_adj_cost(r) : 0;
        std::swap(P[l], P[r]);
        return diff;
    }

    int64_t eval() const {
        return std::min((ll)std::round(1e16 / cost), (ll)1e16);
    }

    void output(std::ostream& out) const {
        out << P[0] + 1;
        for (int i = 1; i < Q; i++) out << ' ' << P[i] + 1;
        out << '\n';
    }

};

int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) {

    Timer timer;

#ifdef HAVE_OPENCV_HIGHGUI
    cv::utils::logging::setLogLevel(cv::utils::logging::LogLevel::LOG_LEVEL_SILENT);
#endif

#ifdef _MSC_VER
    std::ifstream ifs("../tools/input1/0001.txt");
    std::istream& in = ifs;
    std::ofstream ofs("../tools/output1/0001.txt");
    std::ostream& out = ofs;
#else
    std::istream& in = cin;
    std::ostream& out = cout;
#endif

    initialize();

    State state(in);
    dump(state.cost, state.cost);
    state.run(4900 - timer.elapsed_ms());
    dump(state.cost, state.cost);
    state.output(out);

    return 0;
}
0