求助卡常
查看原帖
求助卡常
236866
401rk8楼主2021/9/27 19:04

大佬们帮帮我,蒟蒻写了一天了。。。感激不尽

根据评测机波动能拿 65~79pts

抄的题解,应该不是写假了

#include <bits/stdc++.h>
using namespace std;
#define For(i,x,y,...) for(int i=x,##__VA_ARGS__;i<=int(y);++i)
char buf[1<<22],*p1=buf,*p2=buf,pbuf[1<<22],*pp=pbuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1==p2)?EOF:*p1++)
#define iocl() fwrite(pbuf,1,pp-pbuf,stdout),pp=pbuf,0
#define putchar(x) pp-pbuf==1<<22&&(iocl()),*pp++=x
template<typename T>void read(T &x){
	x=0;char c=getchar();
	for(;!isdigit(c);c=getchar());
	for(;isdigit(c);c=getchar())x=x*10+c-48;
}
template<typename T,typename ...Args>void read(T &x, Args &...args)
	{ read(x),read(args...); }
template<typename T>void write(T x) {
	if(!x)putchar(48);
	else{static int s[21];int l=0;
		for(;x;x/=10)s[l++]=x%10;while(l)putchar(s[--l]|48);}
	putchar(10);
}
template<typename T>void ckmax(T &x,T y) { if( y > x ) x = y; }
template<typename T>void ckmin(T &x,T y) { if( y < x ) x = y; }

const int N = 1e5+5, B = 1e3;
int n,m,a[N*10];
struct Q { int op,l,r,x; } q[N*5];

int le,ri,mx,tag,ql,qr,x,rt[N],fa[N*10],siz[N],lsh[N*10],ans[N*5];

int find(int x) { return fa[x]== x ? x : fa[x]=find(fa[x]); }
void merge(int x,int y) {
	if( rt[y] ) fa[rt[x]] = rt[y];
	else rt[y] = rt[x], lsh[rt[y]] = y;
	siz[y] += siz[x], rt[x] = siz[x] = 0;
}

void build() {
	mx = tag = 0;
	For(i,le,ri) {
		ckmax(mx,a[i]);
		if( !rt[a[i]] ) rt[a[i]] = i, lsh[i] = a[i];
		fa[i] = rt[a[i]], ++siz[a[i]];
	}
}
void modify() {
	if( x*2 <= mx-tag ) {
		For(i,1+tag,x+tag) if( rt[i] ) merge(i,i+x);
		tag += x;
	} else {
		For(i,tag+x+1,mx) if( rt[i] ) merge(i,i-x);
		ckmin(mx,x+tag);
	}
}
void rebuild() {
	For(i,le,ri) a[i] = lsh[find(i)], rt[a[i]] = siz[a[i]] = 0, a[i] -= tag;
	For(i,max(le,ql),ii, ii = min(ri,qr)) if( a[i] > x ) a[i] -= x;
	build();
}
int get() {
	if( x+tag > 1e5+1 ) return 0;
	if( ql <= le && ri <= qr ) return siz[x+tag];
	else {
		int res = 0;
		For(i,max(le,ql),ii, ii = min(ri,qr)) res += lsh[find(i)]-tag==x;
		return res;
	}
}

signed main() {
//  	freopen("a.in","r",stdin);
// 	freopen("a.out","w",stdout);
	read(n,m);
	For(i,1,n) read(a[i]);
	For(i,1,m) read(q[i].op,q[i].l,q[i].r,q[i].x);
	for(le = 1, ri; ri = min(n,le+B-1), le <= n; le = ri+1) {
		memset(rt,0,sizeof rt), memset(siz,0,sizeof siz);
		build();
		For(i,1,m) {
			if( q[i].r < le || ri < q[i].l ) continue;
			qr = q[i].r, ql = q[i].l, x = q[i].x;
			if( q[i].op == 1 ) {
				if( ql <= le && ri <= qr ) modify();
				else rebuild();
			} else ans[i] += get();
		}
	}
	For(i,1,m) if( q[i].op == 2 ) write(ans[i]);
	return iocl();
}
2021/9/27 19:04
加载中...