传送门
文章目录
- 题意:
- 思路:
题意:
思路:
真 调一晚上血压上来了。
考虑第一个操作,块内打个标记,其他的暴力查询即可。
考虑第二个操作,讲块内元素排序之后,直接二分查询。
注意修改元素值的时候需要重新排序,并且在查询开始前需要先将块排序。。以及各种小细节。
// Problem: #6278. 数列分块入门 2
// Contest: LibreOJ
// URL: https://loj.ac/p/6278
// Memory Limit: 256 MB
// Time Limit: 500 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;
LL a[N],b[N];
LL tag[N],id[N];
int block;void add(int l,int r,int c) {if(id[l]==id[r]) {for(int i=l;i<=r;i++) a[i]+=c;int sl=(id[l]-1)*block,sr=id[l]*block; sr=min(sr,n);for(int i=sl+1;i<=sr;i++) b[i]=a[i];sort(b+sl+1,b+sr+1);} else {int sl=id[l]*block,sr=(id[r]-1)*block;for(int i=l;i<=sl;i++) a[i]+=c;for(int i=sr+1;i<=r;i++) a[i]+=c;for(int i=sl-block+1;i<=sl;i++) b[i]=a[i];for(int i=sr+1;i<=sr+block;i++) b[i]=a[i];sort(b+sl-block+1,b+sl+1); sort(b+sr+1,b+min(sr+block,n)+1);for(int i=id[l]+1;i<=id[r]-1;i++) tag[i]+=c;}
}int query(int l,int r,LL c) {int ans=0;if(id[l]==id[r]) {for(int i=l;i<=r;i++) ans+=(a[i]+tag[id[l]])<c;} else {// for(int i=l;i<=r;i++) ans+=(a[i]+tag[id[l]])<=c;int sl=id[l]*block,sr=(id[r]-1)*block;for(int i=l;i<=sl;i++) ans+=(a[i]+tag[id[l]])<c;for(int i=sr+1;i<=r;i++) ans+=(a[i]+tag[id[r]])<c;for(int i=id[l]+1;i<=id[r]-1;i++) {int sl=(i-1)*block,sr=i*block;ans+=lower_bound(b+sl+1,b+sr+1,c-tag[i])-b-sl-1;}}return ans;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0); scanf("%d",&n); block=sqrt(n); for(int i=1;i<=n;i++) scanf("%lld",&a[i]),b[i]=a[i],id[i]=(i-1)/block+1;for(int i=1;i<=id[n];i++) {int sl=(i-1)*block,sr=i*block;sort(b+sl+1,b+sr+1);}for(int i=1;i<=n;i++) {int op,l,r,c; scanf("%d%d%d%d",&op,&l,&r,&c);if(op==0) add(l,r,c);else printf("%d\n",query(l,r,1ll*c*c));}return 0;
}
/**/