博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[BZOJ 3211]花神游历各国(并查集+树状数组)
阅读量:7104 次
发布时间:2019-06-28

本文共 1464 字,大约阅读时间需要 4 分钟。

Description

Solution

树状数组单点修改区间查询

我们知道一个数n最多修改loglogn次就会变为1

并查集维护每个数右边第一个不为1的位置

#include
#include
#include
#include
#include
#define MAXN 100005using namespace std;typedef long long LL; int n,m,delta[MAXN],father[MAXN];LL c[MAXN]; int read(){ int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){ if(c=='-')f=-1;c=getchar(); } while(c>='0'&&c<='9'){ x=x*10+c-'0';c=getchar(); } return x*f;}int lowbit(int x){
return x&-x;}void add(int pos,int x){ while(pos<=n) { c[pos]+=x; pos+=lowbit(pos); }}LL query(int pos){ LL res=0; while(pos>0) { res+=c[pos]; pos-=lowbit(pos); } return res;}int find(int x){ if(x==father[x])return x; father[x]=find(father[x]); return father[x];}int main(){ n=read(); for(int i=1;i<=n;i++) { delta[i]=read(); add(i,delta[i]); father[i]=i; } m=read(); for(int i=1;i<=m;i++) { int x=read(),l=read(),r=read(); if(x==1) printf("%lld\n",query(r)-query(l-1)); else if(x==2) { for(int j=l;j<=r&&j;j=find(j+1)) { add(j,(int)sqrt(delta[j])-delta[j]); delta[j]=(int)sqrt(delta[j]); if(delta[j]<=1)father[j]=j+1; } } } return 0;}

 

转载于:https://www.cnblogs.com/Zars19/p/6871921.html

你可能感兴趣的文章
前端基础-CSS
查看>>
软件版本说明 转
查看>>
[Spring入门学习笔记][maven]
查看>>
java运行时could not open ........jvm.cfg问题的解决
查看>>
Java - 集合框架
查看>>
C6000系列之C6455 DSP的EMIFA接口
查看>>
2-9
查看>>
从键盘上连续录入一批整数,比较并输出其中的最大值和最小值,当输入数字0时结束循环...
查看>>
2018焦作区域赛E. Resistors in Parallel
查看>>
html--特殊字符过滤
查看>>
Linux中断(interrupt)子系统之一:中断系统基本原理【转】
查看>>
SOA会不会造成IT黑洞
查看>>
查询存储过程所需参数
查看>>
HTML5 Web app开发工具Kendo UI Web教程:如何配置Kendo UI Calendar
查看>>
vue Element动态设置el-menu导航当前选中项
查看>>
session的使用
查看>>
Centos6.8通过yum安装mysql5.7
查看>>
NCBI通过氨基酸位置查看相邻SNP
查看>>
CAS SSO单点登录框架学习
查看>>
好书推荐——《启动大脑》
查看>>