|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "题解:[HEOI2016/TJOI2016] 排序" |
| 4 | +subtitle: "洛谷P2824 | ODT 维护。" |
| 5 | +date: 2025-8-18 |
| 6 | +author: "TH911" |
| 7 | +header-img: "img/2024/10/006.jpeg" |
| 8 | +header-mask: 0.4 |
| 9 | +tags: |
| 10 | + - 题解 |
| 11 | + - 提高+/省选− |
| 12 | + - ODT 珂朵莉树 |
| 13 | + - 线段树 |
| 14 | +words: |
| 15 | +--- |
| 16 | + |
| 17 | +> [题目传送门](https://www.luogu.com.cn/problem/P2824) |
| 18 | +
|
| 19 | +# 问题转化 |
| 20 | + |
| 21 | +发现询问有且仅有一次,且仅询问**一个位置**。 |
| 22 | + |
| 23 | +因此无需得出整个序列,得出排序后的 $a_q$ 即可。区间排序实在不好做,因此考虑能否找到一种方式转化。 |
| 24 | + |
| 25 | +答案与 $a_q$ 正相关,其余其实并不重要。因此不妨有: |
| 26 | + |
| 27 | +$$ |
| 28 | +a_i\leftarrow |
| 29 | +\begin{cases} |
| 30 | +1&a_i\geq a_q\\ |
| 31 | +0&a_i<a_q |
| 32 | +\end{cases} |
| 33 | +$$ |
| 34 | + |
| 35 | +这样,排序即可视为区间赋值。 |
| 36 | + |
| 37 | +枚举答案 $a_q=x$,可以 $\mathcal O(m\log n)$ 维护区间赋值,若最后 $a_q=1$,则说明 $x$ 是可能的解,且真实的 $a_q\geq x$。 |
| 38 | + |
| 39 | +因此可以二分。 |
| 40 | + |
| 41 | +时间复杂度 $\mathcal O(m\log^2n)$。 |
| 42 | + |
| 43 | +# AC 代码 |
| 44 | + |
| 45 | +只有一次查询,因此使用 ODT 实现。时间复杂度同线段树。 |
| 46 | + |
| 47 | +```cpp |
| 48 | +//#include<bits/stdc++.h> |
| 49 | +#include<algorithm> |
| 50 | +#include<iostream> |
| 51 | +#include<cstring> |
| 52 | +#include<iomanip> |
| 53 | +#include<cstdio> |
| 54 | +#include<string> |
| 55 | +#include<vector> |
| 56 | +#include<cmath> |
| 57 | +#include<ctime> |
| 58 | +#include<deque> |
| 59 | +#include<queue> |
| 60 | +#include<stack> |
| 61 | +#include<list> |
| 62 | +#include<set> |
| 63 | +using namespace std; |
| 64 | +constexpr const int N=1e5,M=1e5; |
| 65 | +int n,m,q,backup[N+1],a[N+1]; |
| 66 | +struct operation{ |
| 67 | + int op,l,r; |
| 68 | +}op[M+1]; |
| 69 | +struct ODT{ |
| 70 | + struct node{ |
| 71 | + int l,r; |
| 72 | + mutable int value; |
| 73 | + }; |
| 74 | + friend bool operator <(node a,node b){ |
| 75 | + return a.l<b.l; |
| 76 | + } |
| 77 | + set<node>t; |
| 78 | + void build(int l,int r){ |
| 79 | + t.insert({l,r+1}); |
| 80 | + } |
| 81 | + void clear(){ |
| 82 | + t.clear(); |
| 83 | + } |
| 84 | + auto split(int x){ |
| 85 | + auto p=t.lower_bound({x}); |
| 86 | + if(p!=t.end()&&p->l==x){ |
| 87 | + return p; |
| 88 | + } |
| 89 | + p=prev(p); |
| 90 | + int l=p->l,r=p->r,value=p->value; |
| 91 | + t.erase(p); |
| 92 | + t.insert({l,x-1,value}); |
| 93 | + return t.insert({x,r,value}).first; |
| 94 | + } |
| 95 | + void assign(int l,int r,int value){ |
| 96 | + auto pr=split(r+1),pl=split(l); |
| 97 | + t.erase(pl,pr); |
| 98 | + t.insert({l,r,value}); |
| 99 | + } |
| 100 | + void ascending(int l,int r){ |
| 101 | + auto pr=split(r+1),pl=split(l); |
| 102 | + int cnt=0; |
| 103 | + for(auto i=pl;i!=pr;i=next(i)){ |
| 104 | + if(i->value){ |
| 105 | + cnt+=i->r - i->l + 1; |
| 106 | + } |
| 107 | + } |
| 108 | + assign(l,r-cnt,0); |
| 109 | + assign(r-cnt+1,r,1); |
| 110 | + } |
| 111 | + void descending(int l,int r){ |
| 112 | + auto pr=split(r+1),pl=split(l); |
| 113 | + int cnt=0; |
| 114 | + for(auto i=pl;i!=pr;i=next(i)){ |
| 115 | + if(i->value){ |
| 116 | + cnt+=i->r - i->l + 1; |
| 117 | + } |
| 118 | + } |
| 119 | + assign(l,l+cnt-1,1); |
| 120 | + assign(l+cnt,r,0); |
| 121 | + } |
| 122 | + int perform(int x){ |
| 123 | + auto pr=split(x+1),pl=split(x); |
| 124 | + return pl->value; |
| 125 | + } |
| 126 | +}t; |
| 127 | +bool check(int mid){ |
| 128 | + t.clear(); |
| 129 | + t.build(1,n); |
| 130 | + for(int i=1;i<=n;i++){ |
| 131 | + t.assign(i,i,a[i]>=mid); |
| 132 | + } |
| 133 | + for(int i=1;i<=m;i++){ |
| 134 | + switch(op[i].op){ |
| 135 | + case 0: |
| 136 | + t.ascending(op[i].l,op[i].r); |
| 137 | + break; |
| 138 | + case 1: |
| 139 | + t.descending(op[i].l,op[i].r); |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + return t.perform(q); |
| 144 | +} |
| 145 | +int main(){ |
| 146 | + /*freopen("test.in","r",stdin); |
| 147 | + freopen("test.out","w",stdout);*/ |
| 148 | + |
| 149 | + ios::sync_with_stdio(false); |
| 150 | + cin.tie(0);cout.tie(0); |
| 151 | + |
| 152 | + cin>>n>>m; |
| 153 | + for(int i=1;i<=n;i++){ |
| 154 | + cin>>a[i]; |
| 155 | + } |
| 156 | + for(int i=1;i<=m;i++){ |
| 157 | + cin>>op[i].op>>op[i].l>>op[i].r; |
| 158 | + } |
| 159 | + cin>>q; |
| 160 | + int l=0,r=n; |
| 161 | + while(l<r){ |
| 162 | + int mid=l+r+1>>1; |
| 163 | + if(check(mid)){ |
| 164 | + l=mid; |
| 165 | + }else{ |
| 166 | + r=mid-1; |
| 167 | + } |
| 168 | + } |
| 169 | + cout<<l<<'\n'; |
| 170 | + |
| 171 | + cout.flush(); |
| 172 | + |
| 173 | + /*fclose(stdin); |
| 174 | + fclose(stdout);*/ |
| 175 | + return 0; |
| 176 | +} |
| 177 | +``` |
0 commit comments