首页 > 其他分享 >AT_joisc2016_d 雇用計画

AT_joisc2016_d 雇用計画

时间:2024-09-26 21:25:33浏览次数:1  
标签:計画 joisc2016 ch puts int le 雇用 include define

题意

有 \(n\) 个数 \(a_i\),\(q\) 次操作,每次操作会单点修改 \(a_i\),查询所有 \(\ge b\) 的所有数形成的连通块个数。

\(n,q\le 2\times10^5,1\le a_i\le 10^9\)

分析

存在一个 \(O(n\sqrt n)\) 的分块做法,但是需要精细实现(否则复杂度可能退化成 \(O(n\sqrt n\log n)\),不过应该也能过)。

但是存在一个非常简单的 \(O(n\log n)\) 做法。

考虑拆贡献,把每个连通块的 \(1\) 的贡献在连通块的开头计算,就相当于要计算 \(a_{i-1}<b,a_i\ge b\) 的 \(i\) 个数。开一个值域线段树/树状数组维护 \(b=i\) 的答案,那么 \(i\) 会在 \(b\in (a_{i-1},a_i]\) 时被算进答案,对这段区间加 1 即可。单点修改是平凡的。

注意 \(a_i\le 10^9\),所以需要动态开点或者离散化,由于离散化树状数组好写又快,所以为什么不写它呢?

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define IOS ios::sync_with_stdio(false)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define pc putchar
#define pb emplace_back
#define un using namespace
#define popc __builtin_popcountll
#define all(x) x.begin(),x.end()
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
using pii=pair<int,int>;
using i64=long long;
using u64=unsigned long long;
template<typename T>bool greating(T x,T y){return x>y;}
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-48;ch=getchar();}return x*f;
}
template<typename T>
inline void write(T x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=2e5+5,maxm=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,Q,a[maxn];
int b[maxn<<1],m;
struct BIT{
	int c[maxn<<1];
	void add(int x,int y){while(x<=m)c[x]+=y,x+=lowbit(x);}
	void upd(int l,int r,int x){if(l<=r)add(l,x),add(r+1,-x);}
	int qry(int x){int res=0;while(x)res+=c[x],x-=lowbit(x);return res;}
} T;
struct Query{
	int op,x,y;
}q[maxn];
void lsh(){
	sort(b+1,b+m+1),m=unique(b+1,b+m+1)-(b+1);
	rep(i,1,n)a[i]=lower_bound(b+1,b+m+1,a[i])-b;
	rep(i,1,Q)q[i].op==1?q[i].x=lower_bound(b+1,b+m+1,q[i].x)-b:q[i].y=lower_bound(b+1,b+m+1,q[i].y)-b;
}
void mdf(int x,int v){
	if(x==n+1)return;
	T.upd(a[x-1]+1,a[x],v);
}
inline void solve_the_problem(){
	n=rd(),Q=rd();
	rep(i,1,n)a[i]=rd(),b[++m]=a[i];
	rep(i,1,Q)(q[i].op=rd())==1?(q[i].x=rd(),b[++m]=q[i].x):(q[i].x=rd(),q[i].y=rd(),b[++m]=q[i].y);
	lsh();
	rep(i,1,n)mdf(i,1);
	rep(i,1,Q){
		if(q[i].op==1){
			write(T.qry(q[i].x),10);
		}else{
			mdf(q[i].x,-1),mdf(q[i].x+1,-1);
			a[q[i].x]=q[i].y;
			mdf(q[i].x,1),mdf(q[i].x+1,1);
		}
	}
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=1;
	while(_--)solve_the_problem();
}
/*

*/

标签:計画,joisc2016,ch,puts,int,le,雇用,include,define
From: https://www.cnblogs.com/dcytrl/p/18434406

相关文章

  • 渲染指南:自建渲染农场vs雇用云渲染农场服务
    许多设计师和动画制作人在面临一些对成本和时间敏感的渲染项目时,常常会选择使用云渲染服务。尤其是在截止日期迫近的时候,云渲染服务提供了一种快速和有效的解决方案。另一方面,对于有持续渲染需求的个人或机构,可能会考虑建立自己的渲染服务器群。在做出使用云渲染服务或是自建渲染......
  • JOISC2016 题解
    仍然是没有做通信题。JOISC2016Day1Matryoshka俄罗斯套娃转化错了,转化成上升子序列了,然后就变成了区间LIS。实际上是LDS,那么就可以直接做了。https://qoj.ac/submission/99648JOISC2016Day1Memory2神经衰弱我们对数进行两两配对,然后把每一对都问出来。如果不存在相......