题意:
题面挺绕口的,还是看原题比较好。
大概的意思就是让你从给定的区间中选择一个以左端点为起点的一个上升子序列,让后将这些下标存下来,在
b
b
b中将这些位置拿出来后,如果相邻一对在模
2
2
2的意义下不相同贡献就加一。
有三个操作,第一个是修改
a
a
a数组,第二个是将
b
b
b数组中
[
l
,
r
]
[l,r]
[l,r]的位置都
+
1
+1
+1,第三个是询问
[
l
,
r
]
[l,r]
[l,r]的上述过程的贡献。
思路:
比较明显的要用线段树区间合并来写,之前写过一个求全局的题楼房重建,这个题的重点就是
c
a
l
c
calc
calc函数,我们需要魔改一下他的函数实现即可。
先不考虑
b
b
b数组的贡献,先考虑如何求以
l
l
l为起点,
[
l
,
r
]
[l,r]
[l,r]中的最长非严格上升序列长度。
考虑函数
c
a
l
c
(
u
,
m
x
)
calc(u,mx)
calc(u,mx)表示
u
u
u这个子树中最大值为
m
x
mx
mx的时候的答案,显然如果左区间的最大值
<
m
x
<mx
<mx的话,直接返回右区间即可。否则说明两边的区间都可能有贡献,首先需要将答案加上
c
a
l
c
(
L
,
m
x
)
calc(L,mx)
calc(L,mx),如果暴力计算的话,我们还需要加上
c
a
l
c
(
R
,
t
r
[
L
]
.
m
x
)
calc(R,tr[L].mx)
calc(R,tr[L].mx),但是我们在
p
u
s
h
u
p
pushup
pushup过程中可以记一个
l
e
n
len
len,代表当前区间的答案,就是以当前区间左端点为起点的最长上升子序列长度。这个时候
c
a
l
c
(
R
,
t
r
[
L
]
.
m
x
)
=
t
r
[
u
]
.
l
e
n
−
t
r
[
L
]
.
l
e
n
calc(R,tr[L].mx)=tr[u].len-tr[L].len
calc(R,tr[L].mx)=tr[u].len−tr[L].len,这是为什么呢?可以发现不管
c
a
l
c
(
L
,
m
x
)
calc(L,mx)
calc(L,mx)计算的是多少,我们左区间选的最后一个数一定是左区间最大值,而我们的
t
r
[
u
]
.
l
e
n
=
t
r
[
L
]
.
l
e
n
+
c
a
l
c
(
R
,
t
r
[
L
]
.
m
x
)
tr[u].len=tr[L].len+calc(R,tr[L].mx)
tr[u].len=tr[L].len+calc(R,tr[L].mx),所以直接移项即可。
这个时候已经成功一大半了,我们只需要在
c
a
l
c
calc
calc函数中计算一下
b
b
b数组的贡献即可。所以维护一个
f
l
a
g
flag
flag表示当前区间的最后一个位置的
b
b
b是多少,让后在
c
a
l
c
calc
calc函数到底的时候计算贡献即可,这个就比较简单了。
没做出来这个题还是对之前哪个题的
c
a
l
c
calc
calc函数有点误解,不然也不会被卡在怎么查询的问题上。或者是自己实现能力太差了,没想到可以直接引用参数让后在
c
a
l
c
calc
calc函数里面就可以修改。
// Problem: xay loves monotonicity
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/11258/B
// Memory Limit: 524288 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,m;
int a[N],b[N];
struct Node {
int l,r;
int mx,cnt,flag,lazy;
}tr[N<<2];
void pushdown(int u) {
if(tr[u].lazy) {
tr[L].flag^=1; tr[L].lazy^=1;
tr[R].flag^=1; tr[R].lazy^=1;
tr[u].lazy=0;
}
}
int query(int u,int &mx,int &flag) {
if(tr[u].l==tr[u].r) {
if(tr[u].mx>=mx) {
int now=flag!=tr[u].flag;
if(mx==-1) now=0;
mx=tr[u].mx;
flag=tr[u].flag;
return now;
}
return 0;
}
int ans=0;
pushdown(u);
if(tr[L].mx>=mx) {
ans=query(L,mx,flag)+tr[u].cnt-tr[L].cnt;
// ans=query(L,l,Mid,mx,flag)+query(R,Mid+1,r,mx,flag);
mx=tr[u].mx; flag=tr[u].flag;
return ans;
} else return query(R,mx,flag);
}
void pushup(int u) {
tr[u].mx=tr[L].mx; tr[u].flag=tr[L].flag;
tr[u].cnt=tr[L].cnt+query(R,tr[u].mx,tr[u].flag);
}
void build(int u,int l,int r) {
tr[u]={l,r};
if(l==r) {
tr[u].mx=a[l]; tr[u].flag=b[l];
return;
}
build(L,l,Mid); build(R,Mid+1,r);
pushup(u);
}
void change(int u,int pos,int x) {
if(tr[u].l==tr[u].r) {
tr[u].mx=x;
return;
}
pushdown(u);
if(pos<=Mid) change(L,pos,x);
else change(R,pos,x);
pushup(u);
}
void filp(int u,int l,int r) {
if(tr[u].l>=l&&tr[u].r<=r) {
tr[u].lazy^=1; tr[u].flag^=1;
return;
}
pushdown(u);
if(l<=Mid) filp(L,l,r);
if(r>Mid) filp(R,l,r);
pushup(u);
}
int aquery(int u,int l,int r,int &mx,int &flag) {
if(tr[u].l>=l&&tr[u].r<=r) return query(u,mx,flag);
pushdown(u);
int ans=0;
if(l<=Mid) ans+=aquery(L,l,r,mx,flag);
if(r>Mid) ans+=aquery(R,l,r,mx,flag);
return ans;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
build(1,1,n);
scanf("%d",&m);
while(m--) {
int op,t1,t2;
scanf("%d%d%d",&op,&t1,&t2);
if(op==1) change(1,t1,t2);
else if(op==2) filp(1,t1,t2);
else {
int mx=-1,flag=0;
printf("%d\n",aquery(1,t1,t2,mx,flag));
}
}
return 0;
}
/*
*/