QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#582950#7108. Couleurxuanbac05ML 2ms9780kbC++175.5kb2024-09-22 17:50:452024-09-22 17:50:51

Judging History

你现在查看的是最新测评结果

  • [2024-09-22 17:50:51]
  • 评测
  • 测评结果:ML
  • 用时:2ms
  • 内存:9780kb
  • [2024-09-22 17:50:45]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
 
using namespace std;
const int mxN = 5e5 + 227;
const int mxM = 3e6 + 7;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
    if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
    if (a < b) {a = b; return true;} return false;
}
 
struct Node{
    int left, right;
    ll sum;
    Node() {
        left = right = -1;
        sum = 0;
    }
};

struct Segtree{
    int n;
    int timer = 0;
    vector<Node> t;
    vector<int> root;
    Segtree(){}
    Segtree(int _n) {
        n = _n;
        timer = 0;
        int lg2 = log2(n) + 5;
        int offset = 1;
        while(offset <= n * lg2) offset *= 2;
        t.resize(offset * 2 + 7, Node());
    }

    void buildTree() {
        ++timer;
        root.push_back(timer);
    }

    void update(int cur, int tl, int tr, int pos, int val) {
        if(tl == tr) {
            t[cur].sum += val;
        }
        else {
            int tm = (tl + tr) / 2;
            if(pos <= tm) {
                if(t[cur].left == -1) t[cur].left = ++timer;
                update(t[cur].left, tl, tm, pos, val);
            }
            else {
                if(t[cur].right == -1) t[cur].right = ++timer;
                update(t[cur].right, tm + 1, tr, pos, val);
            }
            t[cur].sum = 0;
            if(t[cur].left != -1) t[cur].sum += t[t[cur].left].sum;
            if(t[cur].right != -1) t[cur].sum += t[t[cur].right].sum;
        }
    }

    ll getsum(int cur, int tl, int tr, int l, int r) {
        if(cur == -1) return 0;
        if(l > r) return 0;
        if(tl == l && tr == r) return t[cur].sum;
        int tm = (tl + tr) / 2;
        ll m1 = getsum(t[cur].left, tl, tm, l, min(r, tm));
        ll m2 = getsum(t[cur].right, tm + 1, tr, max(l, tm + 1), r);
        return m1 + m2;
    }

    void update(int id, int pos, int val) {
        update(root[id], 1, n, pos, val);
    }

    ll getsum(int id, int l, int r) {
        return getsum(root[id], 1, n, l, r);
    }
} segtree;
 
int n;
int a[mxN];
ll p[mxN];
int idx[mxN];
ll sum[mxM];
int timer = 0;

void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> p[i];
    
    segtree = Segtree(n);
    segtree.buildTree();
    timer = 0;
    multiset<ll> ms;
    multiset<pair<int, int>> segment;
    for (int i = 1; i <= n; i++) idx[i] = 0;
    for (int i = 1; i <= n; i++) {
        sum[0] += segtree.getsum(0, a[i] + 1, n);
        segtree.update(0, a[i], 1);
    }
    ms.insert(sum[0]);
    segment.insert(make_pair(1, n));
    for(int i = 1; i <= n; i++) {
        ll z = *--ms.end();
        cout << z;
        if(i < n) cout << ' ';

        z ^= p[i];

        // cerr << "Z :" << z << endl;
        auto it = segment.upper_bound(make_pair(z, inf));
        // assert(it != segment.begin());
        --it;

        int left = (*it).fi;
        int right = (*it).se;
        int middle = z;

        
        int l1 = left, r1 = middle - 1, l2 = middle + 1, r2 = right;
        // cerr << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << endl;
        int cur = idx[middle];
        ++timer;
        segtree.buildTree();
        int next = timer;
        ms.erase(ms.find(sum[cur]));
        segment.erase(it);
        // cerr << sum[cur] << endl;
        if(r1 - l1 + 1 <= r2 - l2 + 1) {
            for(int i = l1; i <= r1 + 1; i++) {
                segtree.update(cur, a[i], -1);
                sum[cur] -= segtree.getsum(cur, 1, a[i] - 1);

                if(i <= r1) {
                    idx[i] = next;
                    sum[next] += segtree.getsum(next, a[i] + 1, n);
                    segtree.update(next, a[i], 1);
                }
            }

            if(l2 <= r2) ms.insert(sum[cur]);
            if(l1 <= r1) ms.insert(sum[next]);
            if(l1 <= r1) segment.insert(make_pair(l1, r1));
            if(l2 <= r2) segment.insert(make_pair(l2, r2));
        }
        else {
            for(int i = r2; i >= l2 - 1; i--) {
                segtree.update(cur, a[i], -1);
                sum[cur] -= segtree.getsum(cur, a[i] + 1, n);

                if(i >= l2) {
                    idx[i] = next;
                    sum[next] += segtree.getsum(next, 1, a[i] - 1);
                    segtree.update(next, a[i], 1);
                }
            }


            // cerr << sum[cur] << ' ' << sum[next] << endl;
            if(l1 <= r1) ms.insert(sum[cur]);
            if(l2 <= r2) ms.insert(sum[next]);
            if(l1 <= r1) segment.insert(make_pair(l1, r1));
            if(l2 <= r2) segment.insert(make_pair(l2, r2));
        }
        
    }
    cout << endl;
    for(int i = 0; i <= timer; i++) sum[i] = 0; 
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    // freopen(TASK".inp" , "r" , stdin);
    // freopen(TASK".out" , "w" , stdout);
 
    int tc = 1;
    cin >> tc;
    while(tc--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9780kb

input:

3
5
4 3 1 1 1
5 4 5 3 1
10
9 7 1 4 7 8 5 7 4 8
21 8 15 5 9 2 4 5 10 6
15
4 8 8 1 12 1 10 14 7 14 2 9 13 10 3
37 19 23 15 7 2 10 15 2 13 4 5 8 7 10

output:

7 0 0 0 0
20 11 7 2 0 0 0 0 0 0
42 31 21 14 14 4 1 1 1 0 0 0 0 0 0

result:

ok 3 lines

Test #2:

score: -100
Memory Limit Exceeded

input:

11116
10
10 5 10 3 6 4 8 5 9 8
31 27 24 11 12 3 0 2 3 1
10
8 2 7 2 8 10 1 10 9 10
6 5 2 13 2 1 0 1 3 1
10
7 10 7 6 1 3 10 6 7 9
21 18 10 1 6 5 4 8 9 10
10
2 10 4 8 8 5 7 2 6 7
20 10 9 1 15 0 4 2 9 7
10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
10
1 2 3 4 5 6 7 8 9 10
6 3 5 2 7 10 9 1 4 8
10
1 10 1 3...

output:

21 18 16 12 10 6 4 1 1 0
12 12 10 10 4 4 4 2 1 0
20 16 9 5 3 3 3 0 0 0
22 14 8 8 5 5 2 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
19 12 7 4 4 2 2 1 0 0
20 18 8 3 1 1 0 0 0 0
45 21 21 10 3 3 3 0 0 0
17 11 8 2 1 1 1 0 0 0
13 4 1 0 0 0 0 0 0 0
29 27 22 15 9 7 4 3 1 0
26 16 9 2 1 1 1 1 1 0
0 0 0 0 0 ...

result: