BZOJ3236:[AHOI2013]作业(莫队,分块)
Description
.jpg)
Input

Output

Sample Input
1 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3
Sample Output
1 1
3 2
2 1
HINT
N=100000,M=1000000
Solution
首先有一个比较显然的做法就是用莫队加树状数组……然而这样的话复杂度是$n\sqrt nlog$。
因为树状数组的修改和查询都是$log$的,所以我们用一个修改$O(1)$,查询$O(\sqrt n)$的分块代替树状数组,那么总的复杂度就是$n\sqrt n$了。
Code
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define N (100009)
#define M (1000009)
#define S (359)
using namespace std; int n,m,a[N],l,r,x,y,ans2,Keg[N],q_num;
int ID[N],L[S],R[S],Val[N][],Sum[N][];
struct Que
{
int l,r,a,b,id,ans1,ans2;
bool operator < (const Que &a) const
{
if (ID[l]==ID[a.l]) return r<a.r;
return ID[l]<ID[a.l];
}
}Q[M];
bool cmp(Que a,Que b) {return a.id<b.id;} inline int read()
{
int x=,w=; char c=getchar();
while (c<'' || c>'') {if (c=='-') w=-; c=getchar();}
while (c>='' && c<='') x=x*+c-'', c=getchar();
return x*w;
} void Build()
{
int unit=sqrt(n);
int num=n/unit+(n%unit!=);
for (int i=; i<=num; ++i)
L[i]=(i-)*unit+, R[i]=i*unit;
R[num]=n;
for (int i=; i<=num; ++i)
for (int j=L[i]; j<=R[i]; ++j) ID[j]=i;
} int Query(int l,int r,int opt)
{
int ans=;
if (ID[l]==ID[r])
{
for (int i=l; i<=r; ++i) ans+=Val[i][opt];
return ans;
}
for (int i=l; i<=R[ID[l]]; ++i) ans+=Val[i][opt];
for (int i=L[ID[r]]; i<=r; ++i) ans+=Val[i][opt];
for (int i=ID[l]+; i<=ID[r]-; ++i) ans+=Sum[i][opt];
return ans;
} void Del(int p)
{
Val[a[p]][]--; Sum[ID[a[p]]][]--;
if (!Val[a[p]][]) Val[a[p]][]--, Sum[ID[a[p]]][]--;
} void Ins(int p)
{
Val[a[p]][]++; Sum[ID[a[p]]][]++;
if (Val[a[p]][]==) Val[a[p]][]++, Sum[ID[a[p]]][]++;
} int main()
{
n=read(); m=read();
Build();
for (int i=; i<=n; ++i) a[i]=read();
for (int i=; i<=m; ++i)
{
l=read(); r=read(); x=read(); y=read();
Q[++q_num]=(Que){l,r,x,y,i};
}
sort(Q+,Q+m+);
int l=,r=;
for (int i=; i<=m; ++i)
{
while (l<Q[i].l) Del(l++);
while (l>Q[i].l) Ins(--l);
while (r<Q[i].r) Ins(++r);
while (r>Q[i].r) Del(r--);
Q[i].ans1=Query(Q[i].a,Q[i].b,);
Q[i].ans2=Query(Q[i].a,Q[i].b,);
}
sort(Q+,Q+m+,cmp);
for (int i=; i<=m; ++i)
printf("%d %d\n",Q[i].ans1,Q[i].ans2);
}
BZOJ3236:[AHOI2013]作业(莫队,分块)的更多相关文章
- bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...
- [AHOI2013]作业 (莫队+分块)
[AHOI2013]作业 (莫队+分块) 题面 给定了一个长度为n的数列和若干个询问,每个询问是关于数列的区间[l,r],首先你要统计该区间内大于等于a,小于等于b的数的个数,其次是所有大于等于a,小 ...
- 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 ...
- Bzoj 3236: [Ahoi2013]作业 莫队,分块
3236: [Ahoi2013]作业 Time Limit: 100 Sec Memory Limit: 512 MBSubmit: 1113 Solved: 428[Submit][Status ...
- bzoj 3236: 洛谷 P4396: [AHOI2013]作业 (莫队, 分块)
题目传送门:洛谷P4396. 题意简述: 给定一个长度为\(n\)的数列.有\(m\)次询问,每次询问区间\([l,r]\)中数值在\([a,b]\)之间的数的个数,和数值在\([a,b]\)之间的不 ...
- 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
[BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
- BZOJ 3236: [Ahoi2013]作业( 莫队 + BIT )
莫队..用两个树状数组计算.时间复杂度应该是O(N1.5logN). 估计我是写残了...跑得很慢... ----------------------------------------------- ...
随机推荐
- c# 跨域api
前端 ajax get请求 $.ajax({ url: "API地址", type: 'get', dataType: 'jsonp', async: true, processD ...
- POJ3281(KB11-B 最大流)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19170 Accepted: 8554 Descripti ...
- POJ3268(KB4-D spfa)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23426 Accepted: 1069 ...
- 大数据java基础day01
day01笔记 1.==操作符和equals方法 equals方法存在于Object类中,所有类的equals方法都继承于Object 2.String类的常用方法 ①.replace()替换字符串 ...
- WebForms开发方式以及优缺点,来源《ASP.NET MVC企业级实战》
WebForms有以下3种开发方式 1.服务器端控件 2.一般处理程序+HTML静态页+Ajax 3.一般处理程序+HTML模板 WebForms的请求的是具体的某一个文件.具体的一个类,由客户端发送 ...
- crontab -让服务器执行定时任务
1.启动服务 一般启动服务用 "/sbin/service crond start"就可以了,如果是root用户就是 "sudo service crond start& ...
- JavaScript:作用域与作用域链
1.什么是作用域(scope)? 简单来讲,作用域(scope)就是变量访问规则的有效范围. 作用域外,无法引用作用域内的变量: 离开作用域后,作用域的变量的内存空间会被清除,比如执行完函数或者关闭浏 ...
- css实现自适应正方形的方法
页面布局中,我们经常需要用百分比来实现宽度自适应,但是如果这时候高度要跟宽度呈固定比例变化,该怎么办呢? 很简单,我们可以利用元素的padding或margin的百分比值是参照父元素的宽度这一特性来实 ...
- JS--我发现,原来你是这样的JS(一)(初识,历史)
一.前言: 前段时间看红宝书(JavaScript高级程序设计),但没有计划的去看,也没有做详细的笔记,读了之后有点空虚,感觉不对劲啊,学的东西很难记住,印象不深啊,有种挫败感. 作前端的js都学不好 ...
- PeopleSoft面试题(服务器相关)
如何配置app服务器与web服务器的负载均衡?请详细说明. App Server: 在配置App Server负载均衡时候,通过webserv目录下的configuration.properties文 ...