BZOJ4303:数列
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=4303
把每个元素看成点\((i,a_i)\)即可,然后裸的正交范围打标记和查询。由于膜的是\(2^{29}\),所以任由其自然溢出最后与\(2^{29}-1\)按位和输出即可。
时间复杂度:\(O(n\sqrt{n})\)
空间复杂度:\(O(n)\)
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=5e4+5,inf=2e9,mo=(1<<29)-1;
int n,m,pps,opt,l,r,x,y,ans;
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct kd_tree {
int root;
struct TAG {
int mul,add;
TAG() {}
TAG(int _mul,int _add) {
mul=_mul,add=_add;
}
TAG operator+(const TAG &a)const {
return TAG(mul*a.mul,add*a.mul+a.add);
}
};
struct point {
TAG tag;
int c[2],mn[2],mx[2];
int val,sum,cnt,ls,rs;
bool operator<(const point &a)const {
return c[pps]<a.c[pps];
}
}p[maxn];
int build(int l,int r,int d) {
int mid=(l+r)>>1,u=mid;pps=d;
nth_element(p+l,p+mid,p+r+1);
if(l<mid)p[u].ls=build(l,mid-1,d^1);
if(r>mid)p[u].rs=build(mid+1,r,d^1);
p[u].tag=TAG(1,0);
int ls=p[u].ls,rs=p[u].rs;
p[u].cnt=p[ls].cnt+1+p[rs].cnt;
for(int i=0;i<2;i++) {
int mn=min(p[ls].mn[i],p[rs].mn[i]);
p[u].mn[i]=min(p[u].c[i],mn);
int mx=max(p[ls].mx[i],p[rs].mx[i]);
p[u].mx[i]=max(p[u].c[i],mx);
}
return u;
}
void prepare() {
p[0].mn[0]=p[0].mn[1]=inf;
p[0].mx[0]=p[0].mx[1]=-inf;
for(int i=1;i<=n;i++)
p[i].c[0]=i,p[i].c[1]=read();
root=build(1,n,0);
}
void update(int u) {
int ls=p[u].ls,rs=p[u].rs;
p[u].sum=p[ls].sum+p[u].val+p[rs].sum;
}
void make_tag(int u,TAG a) {
p[u].tag=p[u].tag+a;
p[u].val=p[u].val*a.mul+a.add;
p[u].sum=p[u].sum*a.mul+p[u].cnt*a.add;
}
void push_down(int u) {
if(p[u].tag.mul==1&&p[u].tag.add==0)return;
if(p[u].ls)make_tag(p[u].ls,p[u].tag);
if(p[u].rs)make_tag(p[u].rs,p[u].tag);
p[u].tag=TAG(1,0);
}
void change(int u) {
if(r<p[u].mn[opt]||l>p[u].mx[opt])return;
if(l<=p[u].mn[opt]&&p[u].mx[opt]<=r) {
make_tag(u,TAG(x,y));return;
}
push_down(u);
if(l<=p[u].c[opt]&&p[u].c[opt]<=r)
p[u].val=p[u].val*x+y;
if(p[u].ls)change(p[u].ls);
if(p[u].rs)change(p[u].rs);
update(u);
}
void query(int u) {
if(r<p[u].mn[opt]||l>p[u].mx[opt])return;
if(l<=p[u].mn[opt]&&p[u].mx[opt]<=r) {
ans+=p[u].sum;return;
}
push_down(u);
if(l<=p[u].c[opt]&&p[u].c[opt]<=r)ans+=p[u].val;
if(p[u].ls)query(p[u].ls);
if(p[u].rs)query(p[u].rs);
}
}T;
int main() {
n=read(),m=read();
T.prepare();
for(int i=1;i<=m;i++) {
ans=0,opt=read(),l=read(),r=read();
if(opt<2)x=read(),y=read(),T.change(T.root);
else opt-=2,T.query(T.root),printf("%d\n",ans&mo);
}
return 0;
}
BZOJ4303:数列的更多相关文章
- BZOJ4303 : 数列
将每个点看成二维坐标点$(i,a_i)$,那么每次操作的范围都是一个矩形. 于是建立KD-Tree,通过打标记支持操作即可. 时间复杂度$O(m\sqrt{n})$. #include<cstd ...
- C#求斐波那契数列第30项的值(递归和非递归)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- BZOJ1500[NOI2005]维修数列
Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一 ...
- PAT 1049. 数列的片段和(20)
给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1 ...
- 斐波拉契数列加强版——时间复杂度O(1),空间复杂度O(1)
对于斐波拉契经典问题,我们都非常熟悉,通过递推公式F(n) = F(n - ) + F(n - ),我们可以在线性时间内求出第n项F(n),现在考虑斐波拉契的加强版,我们要求的项数n的范围为int范围 ...
- fibonacci数列(五种)
自己没动脑子,大部分内容转自:http://www.jb51.net/article/37286.htm 斐波拉契数列,看起来好像谁都会写,不过它写的方式却有好多种,不管用不用的上,先留下来再说. 1 ...
- js中的斐波那契数列法
//斐波那契数列:1,2,3,5,8,13…… //从第3个起的第n个等于前两个之和 //解法1: var n1 = 1,n2 = 2; for(var i=3;i<101;i++){ var ...
- 洛谷 P1182 数列分段Section II Label:贪心
题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成M(M≤N)段,并要求每段连续,且每段和的最大值最小. 关于最大值最小: 例如一数列4 2 4 5 1要分成3段 将其如下分段: [4 ...
- 剑指Offer面试题:8.斐波那契数列
一.题目:斐波那契数列 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项.斐波那契数列的定义如下: 二.效率很低的解法 很多C/C++/C#/Java语言教科书在讲述递归函数的时 ...
随机推荐
- java 程序的使用
Java程序可以在任何安装有Java平台的系统上运行,运行的时候语法如下: java -jar <program.jar> -jar这个参数是必须有的,后面跟你的java程序,例如我们 ...
- linux 无密码登录
环境:Linux 脚本:Python 功能:批量IP,远程执行命令.拷贝文件 运行:./ssh_scp.py iplist.txt 脚本内容: #!/usr/bin/env python# -*- c ...
- ubuntu ssh免密码登录
目前很多服务(ceph,openstack等)都需要用到SSH使用ssh-key进行登录,而不能使用密码进行登录. 下面是配置步骤: 一.在SSH Client生成ssh key pair root@ ...
- cuffdiff problem--Performed 0 isoform-level transcription difference tests
如何解决cuffdiff运行结果中表达量为0的情况? cuffdiff -o cdiffout -b ref.fasta -u -p 15 --library-type fr-firststrand ...
- C#遍历指定文件夹中的所有文件
DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);//遍历文件夹foreach(DirectoryInfo NextFolder in ...
- MapReduce-shuffle过程详解
Shuffle map端 map函数开始产生输出时,并不是简单地将它写到磁盘.这个过程很复杂,它利用缓冲的方式写到内存并出于效率的考虑进行预排序.每个map任务都有一个环形内存缓冲区用于存储任务输出. ...
- keystone cache
http://docs.openstack.org/juno/config-reference/content/section_keystone.conf.html http://docs.opens ...
- InnoDB并发事务
目录 1.行锁:索引加锁 2.意向锁 3.间隙锁 4.MVCC机制 行锁 InnoDB通过多版本并发控制MVCC来支持事务 InnoDB的设计是为了在处理大数据量的时候得到最好的性能.InnoDB存 ...
- androidstudio与unity进行交互
参考地址:http://blog.csdn.net/yangxuan0261/article/details/52427119 http://www.360doc.com/content/17/032 ...
- .netframe work的操作系统支持版本
https://msdn.microsoft.com/zh-cn/library/8z6watww.aspx