比赛 2025.3.29 评测结果 WWWWWWWWTT
题目名称 排序 最终得分 0
用户昵称 健康铀 运行时间 20.773 s
代码语言 C++ 内存使用 11.09 MiB
提交时间 2025-03-29 11:46:02
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
const int D = 20;
const int M = N * D * 4;

struct Node { int l, r, c; } t[M];
int uid = 0;                       

int mk() { t[++uid] = {0,0,0}; return uid; }

void add(int& u, int L, int R, int x) {
    if (!u) u = mk();
    t[u].c++;
    if (L == R) return;
    int m = (L+R)>>1;
    x <= m ? add(t[u].l, L, m, x) : add(t[u].r, m+1, R, x);
}

int mg(int a, int b) { 
    if (!a || !b) return a|b;
    t[a].c += t[b].c;
    t[a].l = mg(t[a].l, t[b].l);
    t[a].r = mg(t[a].r, t[b].r);
    return a;
}

void splitL(int src, int& a, int& b, int k) { 
    if (!src) { a = b = 0; return; }
    if (k == 0) { a = 0; b = src; return; }
    if (k >= t[src].c) { a = src; b = 0; return; }
    
    a = mk(), b = mk();
    t[a] = t[b] = t[src];
    t[a].c = t[b].c = 0;
    
    int lc = t[t[src].l].c;
    if (lc >= k) {
        splitL(t[src].l, t[a].l, t[b].l, k);
        t[a].c = k;
        t[b].c = t[src].c - k;
        t[b].r = t[src].r;
    } else {
        splitL(t[src].r, t[a].r, t[b].r, k - lc);
        t[a].c = lc + t[t[a].r].c;
        t[b].c = t[src].c - t[a].c;
        t[a].l = t[src].l;
    }
}

void splitR(int src, int& a, int& b, int k) { 
    splitL(src, b, a, t[src].c - k);
}

struct Seg { 
    int l, r, rt; 
    bool asc;       
    Seg(int a=0, int b=0, int c=0, bool d=1) : l(a), r(b), rt(c), asc(d) {}
    bool operator<(const Seg& x) const { return l < x.l; }
};

set<Seg> ss;

void cut(int p) {
    if (p < 1) return;
    auto it = ss.lower_bound(Seg(p+1));
    if (it == ss.begin()) return;
    Seg s = *--it;
    if (s.r < p) return;
    if (s.l == p) return;
    
    ss.erase(it);
    int k = p - s.l;
    int a=0, b=0;
    
    s.asc ? splitL(s.rt, a, b, k) 
          : splitR(s.rt, a, b, k);
    
    if (a) ss.insert(Seg(s.l, p-1, a, s.asc));
    if (b) ss.insert(Seg(p, s.r, b, s.asc));
}

int kth(int u, int L, int R, int k) {
    if (L == R) return L;
    int m = (L+R)>>1;
    int lc = t[t[u].l].c;
    return k <= lc ? kth(t[u].l, L, m, k) 
                  : kth(t[u].r, m+1, R, k - lc);
}

int main() {
	freopen("heoi2016_sort.in","r",stdin);
		freopen("heoi2016_sort.out","w",stdout);
    ios::sync_with_stdio(0), cin.tie(0);
    int n, m, x;
    cin >> n >> m;
    
    for (int i=1; i<=n; ++i) {
        cin >> x;
        int rt = mk();
        add(rt, 1, n, x);
        ss.insert(Seg(i, i, rt, 1));
    }
    
    while (m--) {
        int op, l, r;
        cin >> op >> l >> r;
        cut(l), cut(r+1);
        
        auto it = ss.lower_bound(Seg(l));
        vector<Seg> tmp;
        for (; it != ss.end() && it->l <= r; it = ss.erase(it))
            tmp.push_back(*it);
        
        int comb = 0;
        for (auto& s : tmp) comb = mg(comb, s.rt);
        ss.insert(Seg(l, r, comb, !op));
    }
    
    cin >> x;
    auto it = ss.upper_bound(Seg(x));
    Seg s = *--it;
    int k = x - s.l + 1;
    int ans = s.asc ? kth(s.rt, 1, n, k)
                   : kth(s.rt, 1, n, t[s.rt].c - k + 1);
    
    cout << ans << "\n";
}