UOJ#515. 【UR #19】前进四
题意
给定序列 ,维护以下操作:
- 单点修改
- 询问 的不同的后缀最小值个数。
一组简单样例
input
代码 3 6 1 3 3 2 1 1 2 2 2 1 1 3 1 2 1 2 2output
代码 2 3 1 1
一开始觉得这个转换很神仙,根本想不到,实际上往图上一画还是很好想的。
知识点:区间最值线段树,扫描线
首先可以发现一些关键点:
- 每次查询是查询一个序列后缀,也就是对位置 的修改会影响到所有查询位置小于 的询问。
- 对位置 修改只会影响到从该修改的时刻开始,到位置 的下一次修改的时刻为止的一个时间段中的询问。
我们以时间和下标建一个坐标系,把每个修改的影响范围画出来,并把查询也画上去。
每个矩形是一个修改,黑三角是询问
将所有操作离线,对时间轴建立线段树,考虑一条从上往下的扫描线,遇到修改就将区间所有数对该数取 ,遇到询问就查询该点的更新次数,可以用线段树很方便的维护。
总共 次操作,只有区间 的吉司机线段树单次操作 ,总时间复杂度为
CODE
// #pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
bool Mbe;
//#define int long long
#define pb emplace_back
typedef long long ll;
const int mod=1e9+7;
const int N=1e6+50;
const int inf=INT_MAX;
int n,m,q,k,res,sum;
#define lc (u<<1)
#define rc (lc|1)
#define mid (l+r>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
struct segtreebeat{
int mf,ms,cnt;
int tag1,tag2;
}t[N<<2];
inline void pushup(int u){
t[u].mf=max(t[lc].mf,t[rc].mf);
if (t[lc].mf==t[rc].mf){t[u].ms=max(t[lc].ms,t[rc].ms);}
else if(t[lc].mf>t[rc].mf){t[u].ms=max(t[lc].ms,t[rc].mf);}
else if(t[lc].mf<t[rc].mf){t[u].ms=max(t[lc].mf,t[rc].ms);}
}
void build(int u,int l,int r){
t[u].tag1=t[u].tag2=0;
if(l==r){
t[u].mf=inf,t[u].ms=-inf;
return ;
}
build(ls);
build(rs);
pushup(u);
}
inline void work(int u,int v1,int v2){
t[u].mf+=v1,t[u].cnt+=v2;
t[u].tag1+=v1,t[u].tag2+=v2;
}
inline void pushdown(int u){
int maxi=max(t[lc].mf,t[rc].mf);
if(t[lc].mf==maxi)work(lc,t[u].tag1,t[u].tag2);
if(t[rc].mf==maxi)work(rc,t[u].tag1,t[u].tag2);
t[u].tag1=t[u].tag2=0;
}
void update(int u,int l,int r,int s,int e,int v){
// cerr<<u<<" "<<l<<" "<<r<<" "<<s<<" "<<e<<" "<<v<<"\n";
if(s<=l&&r<=e){
if(t[u].mf<=v)return ;
if(t[u].ms<v){
work(u,v-t[u].mf,1);
return ;
}
}
pushdown(u);
if(s<=mid)update(ls,s,e,v);
if(mid<e)update(rs,s,e,v);
pushup(u);
}
int query(int u,int l,int r,int x){
// cerr<<u<<" "<<l<<" "<<r<<" "<<x<<"\n";
if(l==r)return t[u].cnt;
pushdown(u);
if(x<=mid)return query(ls,x);
else return query(rs,x);
}
namespace FastIO {
char buf[1 << 21], *p1 = buf, *p2 = buf;
#define getchar() (p1 == p2 && (p1 = buf, p2 = (p1 + fread(buf, 1, 1 << 21, stdin))) == p1 ? EOF : *p1++)
template <typename T> inline T read() { T x = 0, w = 0; char ch = getchar(); while (ch < '0' || ch > '9') w |= (ch == '-'), ch = getchar(); while ('0' <= ch && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar(); return w ? -x : x; }
template <typename T> inline void write(T x) { if (!x) return; write<T>(x / 10), putchar((x % 10) ^ '0'); }
template <typename T> inline void print(T x) { if (x > 0) write<T>(x); else if (x < 0) putchar('-'), write<T>(-x); else putchar('0'); }
template <typename T> inline void print(T x, char en) { print<T>(x), putchar(en); }
}; using namespace FastIO;
#undef getchar()
struct Data{
int x,v,f,l,r,id;
}Q[N<<1];
inline bool cmp(Data x,Data y){
return x.x!=y.x?(x.x>y.x):x.f<y.f;
}
int lat[N],ans[N];
int cnt;
bool Med;
signed main(){
// cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";
//ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
freopen("a.out","w",stdout);
#endif
//freopen("UOJ515.cpp.in","r",stdin);
//freopen("UOJ515.cpp.out","w",stdout);
n=read<int>(),q=read<int>();
for(int i=1;i<=n;++i)
Q[i].v=read<int>(),Q[i].x=i,Q[i].f=1,Q[i].l=1,lat[i]=i;
for(int i=2;i<=q+1;++i){
int p=n+i-1;
Q[p].f=read<int>();
Q[p].l=i;
if(Q[p].f==1)Q[p].x=read<int>(),Q[p].v=read<int>(),Q[lat[Q[p].x]].r=i-1,lat[Q[p].x]=p;
if(Q[p].f==2)Q[p].x=read<int>(),Q[p].id=++cnt;
}
for(int i=1;i<=n;++i)Q[lat[i]].r=q+1;
sort(Q+1,Q+1+q+n,cmp);
build(1,1,q+1);
for(int i=1;i<=n+q;++i){
if(Q[i].f==1) update(1,1,q+1,Q[i].l,Q[i].r,Q[i].v);
else ans[Q[i].id]=query(1,1,q+1,Q[i].l);
}
for(int i=1;i<=cnt;++i)print(ans[i],'\n');
// cerr<<"\n"<<clock()*1.0/CLOCKS_PER_SEC*1000<<"ms\n";
return 0;
}
/*
*/
/*
文件名: UOJ515.cpp
创建时间: 2025-03-24 09:29:13
作者: nnn233
*/