CF2247D2 XOR Sorting (Hard Version) Solution
在 Print 之前
赛后 D2 第七发 AC,你就知道到底多绝望了。
继续掉分。
题解
在 D1 中我们知道答案肯定是
发现我们每次枚举的
考虑转化一下判定条件。
发现对于
我们考虑在枚举到
当在某一层中这个数字会归到
再次思考后发现这个的处理似乎可以归到树上结构去解决。
这里最开始只觉得是一颗普通的满二叉树,结果代码打到一半才发现实际上就是一颗线段树。
我们考虑序列扔到线段树上,维护区间最小最大值以及答案。
合并两个区间只需要用上述的判定方法判定即可。
查询时可以手写一个函数二进制求解。
看了眼其他人的代码和官方题解,发现自己好像写复杂了?还是败在了代码实现上啊。
Code
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#ifdef _WIN32
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#else
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define pll pair<ll,ll>
#define pld pair<ld,ld>
typedef long long ll;
typedef long double ld;
typedef __int128 i128;
namespace io {
using namespace std;
template<typename T> void debug (T x) {
cerr<<x<<'\n';
}
template<typename T> void debuglen (T x) {
cerr<<x<<' ';
}
template<typename T,typename...Args> void debug (T x,Args...args) {
cerr<<x<<' ';
debug(args...);
}
template<typename T> void debug (T *lt,T *rt) {
ll len=rt-lt;
for (ll i=0;i<len;i++) {
debuglen(*(lt+i));
}
cerr<<'\n';
}
inline ll read () {
char x=getchar();
ll ans=0,f=1;
while (x<'0'||x>'9') {
if (x=='-') {
f=-1;
}
x=getchar();
}
while (x>='0'&&x<='9') {
ans=(ans<<1)+(ans<<3);
ans+=(x^'0');
x=getchar();
}
return ans*f;
}
void print (ll x) {
if (x<0) {
x=-x;
putchar('-');
}
if (x>=10) {
print(x/10);
}
putchar(x%10+'0');
}
}
using namespace io;
const ll N=4e6+5,mod=1e9+7,inf=2e18;
const ld eps=1e-6;
ll n,q,a[N],lg[N],num[N];
struct info {
ll mx,mn;
} t[N<<1];
bool vis[N<<1];
inline void init () {
lg[1]=0;
for (ll i=2;i<=N-5;i++) {
lg[i]=lg[i/2]+1;
}
}
inline bool ck (ll x) {
ll d=lg[x];
if (x==(1<<(d+1))-1) {
return false;
}
return t[x].mx>t[x+1].mn;
}
inline ll get_ans (ll pl) {
for (ll i=pl;i>=0;i--) {
if (num[i]>0) {
return (1<<i);
}
}
return 0;
}
inline void solve () {
n=read(),q=read();
for (ll i=0;i<n;i++) {
a[i]=read();
}
ll pl=0,sz=1;
while (sz<n) {
sz<<=1,pl++;
}
for (ll i=0;i<=pl;i++) {
num[i]=0;
}
for (ll i=0;i<sz;i++) {
ll u=sz+i;
if (i<n) {
t[u]={a[i],a[i]};
}
else {
t[u]={-inf,inf};
}
}
for (ll i=sz-1;i>=1;i--) {
t[i]={max(t[i<<1].mx,t[i<<1|1].mx),min(t[i<<1].mn,t[i<<1|1].mn)};
}
for (ll i=1;i<2*sz;i++) {
vis[i]=ck(i);
if (vis[i]) {
ll pos=pl-lg[i];
num[pos]++;
}
}
print(get_ans(pl));
puts("");
while (q--) {
ll posl=read(),x=read();
ll pos=sz+posl,fl=pos;
while (fl>=1) {
ll d=lg[fl],ql=pl-d;
if (fl>(1<<d)) {
num[ql]-=vis[fl-1];
}
if (fl<(1<<(d+1))-1) {
num[ql]-=vis[fl];
}
if (fl==pos) {
t[fl]={x,x};
}
else {
t[fl]={max(t[fl<<1].mx,t[fl<<1|1].mx),min(t[fl<<1].mn,t[fl<<1|1].mn)};
}
if (fl>(1<<d)) {
vis[fl-1]=ck(fl-1);
num[ql]+=vis[fl-1];
}
if (fl<(1<<(d+1))-1) {
vis[fl]=ck(fl);
num[ql]+=vis[fl];
}
fl>>=1;
}
print(get_ans(pl));
puts("");
}
}
int main () {
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
init();
ll T=1;
T=read();
while (T--) {
solve();
}
return 0;
}