其他分享
首页 > 其他分享> > GukiZ and GukiZiana CodeForces - 551E (分块)

GukiZ and GukiZiana CodeForces - 551E (分块)

作者:互联网

大意: 区间加, 查询整个序列元素$x$的最远间隔.

 

时限很大, 无脑$O(n\sqrt{n}logn)$分块.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e6+10;
int n, m, sqn, a[N];
int blo[N], L[N], R[N], tag[N];
vector<int> g[N];

void build(int l, int r, int v, int id) {
	REP(i,L[id],R[id]) a[i]=min(INF,a[i]+tag[id]);
	REP(i,l,r) a[i]=min(INF,a[i]+v);
	g[id].clear(),tag[id] = 0;
	REP(i,L[id],R[id]) g[id].pb(a[i]);
	sort(g[id].begin(),g[id].end());
}

void update(int l, int r, int v) {
	if (blo[l]==blo[r]) build(l,r,v,blo[l]);
	else {
		build(l,R[blo[l]],v,blo[l]);
		build(L[blo[r]],r,v,blo[r]);
		REP(i,blo[l]+1,blo[r]-1) tag[i] = min(INF,tag[i]+v);
	}
}

int query(int x) {
	int qr=0,ql=1;
	REP(i,1,blo[n]) {
		auto t = lower_bound(g[i].begin(),g[i].end(),x-tag[i]);
		if (t!=g[i].end()&&*t==x-tag[i]) {
			build(0,0,0,i);
			REP(j,L[i],R[i]) if (a[j]==x) { 
				ql = j; break;
			}
			break;
		}
	}
	PER(i,1,blo[n]) {
		auto t = upper_bound(g[i].begin(),g[i].end(),x-tag[i]);
		if (t!=g[i].begin()&&*(--t)==x-tag[i]) {
			build(0,0,0,i);
			PER(j,L[i],R[i]) if (a[j]==x) {
				qr = j; break;
			}
			break;
		}
	}
	return qr-ql;
}

int main() {
	scanf("%d%d", &n, &m);
	sqn = pow(n,0.44);
	REP(i,1,n) {
		scanf("%d", a+i);
		blo[i] = (i-1)/sqn+1;
		g[blo[i]].pb(a[i]);
		L[blo[i]] = (blo[i]-1)*sqn+1;
		R[blo[i]] = blo[i]*sqn;
	}
	REP(i,1,blo[n]) sort(g[i].begin(),g[i].end());
	while (m--) {
		int op,l,r,x;
		scanf("%d", &op);
		if (op==1) {
			scanf("%d%d%d", &l, &r, &x);
			update(l,r,x);
		}
		else { 
			scanf("%d", &x);
			printf("%d\n", query(x));
		}
	}
}

 

标签:int,REP,551E,CodeForces,blo,GukiZiana,include,id,define
来源: https://www.cnblogs.com/uid001/p/10950557.html