传送门:https://www.luogu.org/problemnew/show/P2801

参考:http://hzwer.com/2784.html  感觉思路无比清晰;)

ps:我在洛谷A的,BZOJ要权限;

题意:区间查询有多少个比K的数;

思路:分块,两边暴力更新与查询,中间查询是用二分计数;每次更新,如有必要,要记得重新sort(区间对应的另一个数组);

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
const int maxn = ;
const int bmaxn = ;
ll a[maxn],b[maxn],add[bmaxn];
int n,m; int belong[maxn];
int num,l[bmaxn],r[bmaxn]; //块个数;i这块的左端;i这块的右端
int block; //块大小 void reset(int id)
{
int le = l[id],ri = r[id];
for(int i=le; i<=ri; i++)b[i] = a[i];
sort(b+le,b+ri+);
} void build()
{
block = sqrt(n);
num = n/block;if(n%block)num++;
for(int i=; i<=num; i++)
l[i]=(i-)*block+,r[i] = i * block;
r[num] = n;
for(int i=; i<=n; i++)
belong[i] = (i-)/block + ;
for(int i=; i <= num; i++)
reset(i);
} void update(int lx,int rx,ll val)
{
if(belong[lx]==belong[rx])
{
for(int i=lx;i<=rx;i++)a[i]+=val;
reset(belong[lx]);
}
else
{
int li = r[belong[lx]],ri = l[belong[rx]];
for(int i = lx; i<=li; i++)a[i]+=val;
reset(belong[lx]);
for(int i = ri; i<=rx; i++)a[i]+=val;
reset(belong[rx]);
for(int i = belong[lx] + ;i < belong[rx]; i++)add[i]+=val;
}
} ll query(int lx,int rx,ll k)
{
ll res = ;
if(belong[lx]==belong[rx])
{
for(int i=lx;i<=rx;i++)if(a[i] >= k - add[belong[lx]])res++;
}
else
{
int li = r[belong[lx]],ri = l[belong[rx]];
// cout<<li<<" "<<ri<<endl;
for(int i = lx; i <= li; i++) if(a[i] >= k-add[belong[lx]])res++;
for(int i = ri; i <= rx; i++) if(a[i] >= k-add[belong[rx]])res++;
for(int i = belong[lx] + ; i<belong[rx]; i++)
{
int le = l[i],ri = r[i];
while(le <= ri)
{
int mid = (le+ri)>>;
if(b[mid] < k - add[i])
le = mid + ;
else ri = mid - ;
}
// printf("%d\n",le);
// cout<<r[i]-le+1<<endl;
res+=r[i] - le + ;
}
}
return res;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++)
scanf("%lld", &a[i]);
build();
for(int i=; i<=m; i++)
{
char s[];
int x,y;
ll v;
scanf("%s%d%d%lld",s,&x,&y,&v);
if(s[]=='M')
{
update(x,y,v);
}
else
{
ll ans = query(x,y,v);
printf("%lld\n",ans);
}
}
return ;
}

最新文章

  1. cocos2d-x 开头配置(Windows 平台)
  2. LInux : du命令
  3. php 随机生成
  4. PigSPS: a database for pig SNPs and signatures of positive selection
  5. ViewPager撤消左右滑动切换功能
  6. BZOJ2694: Lcm
  7. sql server实用工具sql prompt的安装与注册
  8. C#判断输入的是否是汉字
  9. 解决vsftpd 2.2.2读取目录列表失败的问题
  10. windows ntp安装及调试
  11. 判别linux机器字节序为大端还是小端
  12. SQL Server 2000/2005 分页SQL — 单条SQL语句
  13. 正式生产环境下hadoop集群的DNS+NFS+ssh免password登陆配置
  14. centos7,yum安装的redis用systemctl无法启动
  15. 放弃antd table,基于React手写一个虚拟滚动的表格
  16. Python字符串的格式化,看这一篇就够了
  17. 关于 python中的转义字符
  18. poj1256(贪心+并查集)
  19. C# System.IO.Path
  20. gj2 python中一切皆对象

热门文章

  1. ubuntu/deepin 下下载wxpython
  2. 如何确定FPGA电路中DDR4的Speed bin 是否兼容?
  3. sqoop 密码别名模式 --password-alias
  4. LeetCode 85. 冗余连接 II
  5. linux装OpenOffice后传---中文乱码的解决
  6. python面试总结1(基础章节)
  7. 洛谷 P2158 [SDOI2008]仪仗队
  8. 夯实Java基础(十七)——注解(Annotation)
  9. python骚操作---Print函数用法
  10. 关于在taro使用wx.parse那些事