結果

問題 No.2977 Kth Xor Pair
ユーザー 👑 tute7627tute7627
提出日時 2024-12-02 01:56:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 3,000 ms
コード長 4,065 bytes
コンパイル時間 923 ms
コンパイル使用メモリ 77,712 KB
実行使用メモリ 14,080 KB
最終ジャッジ日時 2024-12-02 01:57:02
合計ジャッジ時間 3,481 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 3 ms
5,248 KB
testcase_03 AC 3 ms
5,248 KB
testcase_04 AC 3 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 23 ms
7,168 KB
testcase_08 AC 37 ms
8,960 KB
testcase_09 AC 35 ms
8,576 KB
testcase_10 AC 36 ms
8,704 KB
testcase_11 AC 36 ms
8,704 KB
testcase_12 AC 36 ms
8,704 KB
testcase_13 AC 38 ms
8,960 KB
testcase_14 AC 23 ms
7,040 KB
testcase_15 AC 33 ms
8,192 KB
testcase_16 AC 39 ms
9,088 KB
testcase_17 AC 38 ms
9,088 KB
testcase_18 AC 39 ms
9,088 KB
testcase_19 AC 39 ms
8,960 KB
testcase_20 AC 38 ms
8,832 KB
testcase_21 AC 39 ms
9,088 KB
testcase_22 AC 69 ms
13,952 KB
testcase_23 AC 67 ms
13,952 KB
testcase_24 AC 68 ms
14,080 KB
testcase_25 AC 42 ms
8,960 KB
testcase_26 AC 69 ms
14,080 KB
testcase_27 AC 27 ms
7,808 KB
testcase_28 AC 24 ms
6,400 KB
testcase_29 AC 26 ms
6,912 KB
testcase_30 AC 27 ms
7,296 KB
testcase_31 AC 26 ms
7,168 KB
testcase_32 AC 18 ms
6,144 KB
testcase_33 AC 18 ms
6,144 KB
testcase_34 AC 18 ms
6,016 KB
testcase_35 AC 18 ms
6,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//: https://codeforces.com/contest/1055/submission/232368836

// LUOGU_RID: 134589272
#include <iostream>
#include <algorithm>

typedef long long ll;
typedef double lf;

// #define DEBUG 1
struct IO
{
    #define MAXSIZE (1 << 20)
    #define isdigit(x) (x >= '0' && x <= '9')
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;
    #if DEBUG
    #else
    IO() : p1(buf), p2(buf), pp(pbuf) {}
    ~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
    #endif
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
    #define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')

    template <typename T>
    void Read(T &x)
    {
        #if DEBUG
        std::cin >> x;
        #else
        bool sign = 0; char ch = gc(); x = 0;
        for (; !isdigit(ch); ch = gc())
            if (ch == '-') sign = 1;
        for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
        if (sign) x = -x;
        #endif
    }
    void Read(char *s)
    {
        #if DEBUG
        std::cin >> s;
        #else
        char ch = gc();
        for (; blank(ch); ch = gc());
        for (; !blank(ch); ch = gc()) *s++ = ch;
        *s = 0;
        #endif
    }
    void Read(char &c) {for (c = gc(); blank(c); c = gc());}

    void Push(const char &c)
    {
        #if DEBUG
        putchar(c);
        #else
        if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
        *pp++ = c;
        #endif
    }
    template <typename T>
    void Write(T x)
    {
        if (x < 0) x = -x, Push('-');
        static T sta[35];
        int top = 0;
        do sta[top++] = x % 10, x /= 10; while (x);
        while (top) Push(sta[--top] ^ 48);
    }
    template <typename T>
    void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)

using namespace std;

const int MAXN = 2e5 + 10;

int n, fa[MAXN];
ll k, w[MAXN], ans;

int top[2];
int l1[2][MAXN], r1[2][MAXN], l2[2][MAXN], r2[2][MAXN], mid1[MAXN], mid2[MAXN];

inline int FindMid(int l, int r, int s)
{
    int mid = l - 1;
    while (mid < r && !((w[mid + 1] >> s) & 1)) mid++;
    return mid;
}

int main()
{
    #if DEBUG
    #else
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    Read(n), Read(k);
    k = n + 2 * k;
    for (int i = 1; i <= n; i++) Read(w[i]);
    sort(w + 1, w + n + 1);
    int p = 0;
    top[0] = 1, l1[0][1] = l2[0][1] = 1, r1[0][1] = r2[0][1] = n;
    for (int s = 30; s >= 0; s--)
    {
        ll sum = 0;
        for (int i = 1; i <= top[p]; i++)
        {
            mid1[i] = FindMid(l1[p][i], r1[p][i], s), mid2[i] = (l2[p][i] == l1[p][i] ? mid1[i] : FindMid(l2[p][i], r2[p][i], s));
            sum += (ll)(mid1[i] - l1[p][i] + 1) * (mid2[i] - l2[p][i] + 1) + (ll)(r1[p][i] - mid1[i]) * (r2[p][i] - mid2[i]);
        }
        int np = p ^ 1; top[np] = 0;
        if (sum >= k)
        {
            for (int i = 1; i <= top[p]; i++)
            {
                if (mid1[i] >= l1[p][i] && mid2[i] >= l2[p][i])
                    top[np]++, l1[np][top[np]] = l1[p][i], r1[np][top[np]] = mid1[i], l2[np][top[np]] = l2[p][i], r2[np][top[np]] = mid2[i];
                if (mid1[i] < r1[p][i] && mid2[i] < r2[p][i])
                    top[np]++, l1[np][top[np]] = mid1[i] + 1, r1[np][top[np]] = r1[p][i], l2[np][top[np]] = mid2[i] + 1, r2[np][top[np]] = r2[p][i];
            }
        }
        else
        {
            k -= sum, ans ^= (1ll << s);
            for (int i = 1; i <= top[p]; i++)
            {
                if (mid1[i] >= l1[p][i] && mid2[i] < r2[p][i])
                    top[np]++, l1[np][top[np]] = l1[p][i], r1[np][top[np]] = mid1[i], l2[np][top[np]] = mid2[i] + 1, r2[np][top[np]] = r2[p][i];
                if (mid1[i] < r1[p][i] && mid2[i] >= l2[p][i])
                    top[np]++, l1[np][top[np]] = mid1[i] + 1, r1[np][top[np]] = r1[p][i], l2[np][top[np]] = l2[p][i], r2[np][top[np]] = mid2[i];
            }
        }
        p = np;
    }
    Write(ans, '\n');
    return 0;
}
0