【BZOJ3236】【AHOI2013】作业 线段树 分治 树状数组
题目描述
给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出1.区间\([l,r]\)有多少范围在\([a,b]\)的数;2.区间\([l,r]\)有多少范围在\([a,b]\)的权值。
\(n\leq 100000,m\leq 1000000\)
题解
这道题莫队可以水过。
这里讲一个更优秀的算法。
建一棵权值线段树。每一个点存它代表的范围内所有数的下标。
一个询问对应权值线段树中的一些点。每个点要求出\([l,r]\)内的数的个数和不同的数的个数。第一问可以乱搞。第二问直接排序后用树状数组维护每个数最后一次出现的位置然后离线乱搞。
每个点会在根到这个点的路径上各插入一次,所以总的点数是\(n\log n\)。
总的时间复杂度是\(O((n+m)\log^2 n)\)
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void sort(int &a,int &b)
{
if(a>b)
swap(a,b);
}
void open(const char *s)
{
#ifndef ONLINE_JUDGE
char str[100];
sprintf(str,"%s.in",s);
freopen(str,"r",stdin);
sprintf(str,"%s.out",s);
freopen(str,"w",stdout);
#endif
}
struct ques
{
int l,r,a,b,id;
};
vector<ques> q[200010];
vector<pii> c[200010];
struct node
{
int l,r,ls,rs;
};
node a[200010];
int cnt;
int rt;
void build(int &p,int l,int r)
{
p=++cnt;
a[p].l=l;
a[p].r=r;
if(l==r)
return;
int mid=(l+r)>>1;
build(a[p].ls,l,mid);
build(a[p].rs,mid+1,r);
}
void build2(int p)
{
if(a[p].l==a[p].r)
return;
int mid=(a[p].l+a[p].r)>>1;
for(vector<pii>::iterator x=c[p].begin();x!=c[p].end();x++)
if(x->second<=mid)
c[a[p].ls].push_back(*x);
else
c[a[p].rs].push_back(*x);
build2(a[p].ls);
build2(a[p].rs);
}
void add(int &p,ques &v)
{
if(v.a<=a[p].l&&v.b>=a[p].r)
{
q[p].push_back(v);
return;
}
int mid=(a[p].l+a[p].r)>>1;
if(v.a<=mid)
add(a[p].ls,v);
if(v.b>mid)
add(a[p].rs,v);
}
int ans[1000010];
int ans2[1000010];
int d[100010];
int e[100010];
int f[100010];
int n,m;
int bt[100010];
int bt2[100010];
int last[100010];
void add(int x,int v)
{
for(;x<=n;x+=x&-x)
bt[x]+=v;
}
int sum(int x)
{
int s=0;
for(;x;x-=x&-x)
s+=bt[x];
return s;
}
void add2(int x,int v)
{
for(;x<=n;x+=x&-x)
bt2[x]+=v;
}
int sum2(int x)
{
int s=0;
for(;x;x-=x&-x)
s+=bt2[x];
return s;
}
void rd(int &s)
{
int c;
while((c=getchar())<'0'||c>'9');
s=c-'0';
while((c=getchar())>='0'&&c<='9')
s=s*10+c-'0';
}
int cmp(ques a,ques b)
{
return a.r<b.r;
}
int main()
{
open("bzoj3236");
scanf("%d%d",&n,&m);
int i,x;
build(rt,1,n);
for(i=1;i<=n;i++)
{
rd(x);
c[1].push_back(pii(i,x));
}
build2(rt);
ques v;
for(i=1;i<=m;i++)
{
rd(v.l);
rd(v.r);
rd(v.a);
rd(v.b);
v.id=i;
add(rt,v);
}
int j;
for(i=1;i<=cnt;i++)
{
int sz2=q[i].size();
if(!sz2)
continue;
int sz=c[i].size();
int k=1;
for(j=1;j<=sz;j++)
{
d[j]=c[i][j-1].first;
f[j]=c[i][j-1].second;
}
sort(q[i].begin(),q[i].end(),cmp);
while(k<=sz2&&q[i][k-1].r<d[1])
k++;
for(j=1;j<=sz;j++)
{
e[j]=last[f[j]];
last[f[j]]=j;
if(e[j])
add(e[j],-1);
add(j,1);
add2(j,1);
while(k<=sz2&&(j==sz||q[i][k-1].r<d[j+1]))
{
int l=lower_bound(d+1,d+sz+1,q[i][k-1].l)-d;
ans[q[i][k-1].id]+=sum(j)-sum(l-1);
ans2[q[i][k-1].id]+=sum2(j)-sum2(l-1);
k++;
}
}
for(j=1;j<=sz;j++)
{
if(last[f[j]])
add(last[f[j]],-1);
last[f[j]]=0;
add2(j,-1);
}
}
for(i=1;i<=m;i++)
printf("%d %d\n",ans2[i],ans[i]);
return 0;
}
【BZOJ3236】【AHOI2013】作业 线段树 分治 树状数组的更多相关文章
- BZOJ3236 [Ahoi2013]作业 【莫队 + 树状数组】
题目链接 BZOJ3236 题解 没想到这题真的是如此暴力 #include<algorithm> #include<iostream> #include<cstring ...
- [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业
[bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...
- BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块
题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...
- [BZOJ3236][AHOI2013]作业:树套树/莫队+分块
分析 第一问随便搞,直接说第二问. 令原数列为\(seq\),\(pre_i\)为\(seq_i\)这个值上一个出现的位置,于是可以简化询问条件为: \(l \leq i \leq r\) \(a \ ...
- [BZOJ3236]:[Ahoi2013]作业(莫队+分块)
题目传送门 题目描述 此时已是凌晨两点,刚刚做了$Codeforces$的小$A$掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是一个小时可与做完的数学作业,接下来是分别都是一个小时可 ...
- 树分治&树链剖分相关题目讨论
预备知识 树分治,树链剖分 poj1741 •一棵有n个节点的树,节点之间的边有长度.方方方想知道,有多少个点对距离不超过m 题解 点分治模板题.详见我早上写的http://www.cnblogs ...
- BZOJ3236:[AHOI2013]作业(莫队,分块)
Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 ...
- 【莫队算法】【权值分块】bzoj3236 [Ahoi2013]作业
莫队显然.然后维护转移的时候如果用树状数组,则很容易TLE.所以用权值分块维护转移. 总复杂度O(m*sqrt(n)). #include<cstdio> #include<algo ...
- bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...
随机推荐
- Pointcut 笔记
教程 https://blog.csdn.net/kkdelta/article/details/7441829 http://www.cnblogs.com/youse/p/6564524.html ...
- python2中reload(sys)后设置编码
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't deco ...
- UnderWater+SDN论文之五
Underwater Sensor Networks with Mobile Agents: Experience from the Field Source: LNICST 2013 论文是来自两个 ...
- Day7 Ubantu学习(一)
Linux是多用户操作系统 Ubantu学习参考网址:https://www.cnblogs.com/resn/p/5800922.html 1.虚拟机网络类型的理解 bridged(桥接模式) : ...
- Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)
当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...
- ES7的新特性
ES7的新特性 ES7 特性: 1.Array.prototype.includes2.Exponentiation Operator(求幂运算) 一,Array.prototype.includes ...
- Git的配置与使用
Git的配置与使用 一,未配置过git 1.1,安装Git https://git-for-windows.github.io/ 1,2,鼠标右键点击Git Bash Here 1.3,输入命令 cd ...
- Day 5-<补充> 类的的继承和查找顺序
类的继承于查找顺序: 在py2中,不继承object的类为经典类,经典类继承查找:深度优先. 在py3中,默认继承object,所以python3中都是新式类,新式类的继承查找:广度优先. 类的特殊属 ...
- windows环境下protobuf的java操作{编译,序列化,反序列化}
google protocol buffer的使用和原理 概况: Protocol Buffers(也就是protobuf)是谷歌的语言中立的.平台中立的.可扩展的用于序列化结构化的数据: windo ...
- RedHat 安装RabbitMQ
(以下均以root用户执行) 1.安装配置epel源rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noar ...