题目大意:给出一些数,问在一个区间中不同的数值有多少种,和在一个区间中不同的数值有多少个。

思路:因为没有改动,所以就想到了莫队算法。然后我写了5K+的曼哈顿距离最小生成树,然后果断T了。(100s的时限啊,刷status都要刷疯了..,结果最后加了手写读入也没能A)。

后来果断放弃,写了分块版的莫队算法。

84sAC。。。这题卡的。。貌似莫队并非正解。

其有用分块来写莫队就非常easy了。仅仅须要将全部询问的区间排序。左端点所在块作为第一键值,右端点作为第二季键值排序,之后就能够转移了。理论上来时还是曼哈顿距离最小生成树比較快。可是常数实在是太大。随处可见的pair。sort。。

还不太好写。

唉。

时代的眼泪。。。

CODE(84816AC):

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1100010
using namespace std; struct Ask{
int x,y,_id;
int block;
int a,b; bool operator <(const Ask &a)const {
if(block == a.block) return y < a.y;
return block < a.block;
}
void Read(int p) {
scanf("%d%d%d%d",&x,&y,&a,&b);
_id = p;
}
}ask[MAX]; int cnt,asks;
pair<int,int *> xx[MAX << 1];
int src[MAX]; int t;
int fenwick[MAX << 1],_fenwick[MAX];
int num[MAX];
pair<int,int> ans[MAX]; inline int GetSum(int fenwick[],int x)
{
int re = 0;
for(; x; x -= x&-x)
re += fenwick[x];
return re;
} inline void Fix(int fenwick[],int x,int c)
{
for(; x <= t; x += x&-x)
fenwick[x] += c;
} int main()
{
cin >> cnt >> asks;
int temp = 0;
for(int i = 1; i <= cnt; ++i)
scanf("%d",&xx[++temp].first),xx[temp].second = &src[i];
for(int i = 1; i <= asks; ++i) {
ask[i].Read(i);
xx[++temp] = make_pair(ask[i].a,&ask[i].a);
xx[++temp] = make_pair(ask[i].b,&ask[i].b);
}
sort(xx + 1,xx + temp + 1);
for(int i = 1; i <= temp; ++i) {
if(!t || xx[i].first != xx[i - 1].first)
++t;
*xx[i].second = t;
}
int block = static_cast<int>(sqrt(cnt));
for(int i = 1; i <= asks; ++i)
ask[i].block = ask[i].x / block;
sort(ask + 1,ask + asks + 1);
int l = 1,r = 0;
for(int x = 1; x <= asks; ++x) {
while(r < ask[x].y) {
++num[src[++r]];
Fix(fenwick,src[r],1);
if(num[src[r]] == 1) Fix(_fenwick,src[r],1);
}
while(l > ask[x].x) {
++num[src[--l]];
Fix(fenwick,src[l],1);
if(num[src[l]] == 1) Fix(_fenwick,src[l],1);
}
while(r > ask[x].y) {
--num[src[r]];
Fix(fenwick,src[r],-1);
if(!num[src[r]]) Fix(_fenwick,src[r],-1);
--r;
}
while(l < ask[x].x) {
--num[src[l]];
Fix(fenwick,src[l],-1);
if(!num[src[l]]) Fix(_fenwick,src[l],-1);
++l;
}
int p = ask[x]._id;
ans[p].first = GetSum(fenwick,ask[x].b) - GetSum(fenwick,ask[x].a - 1);
ans[p].second = GetSum(_fenwick,ask[x].b) - GetSum(_fenwick,ask[x].a - 1);
}
for(int i = 1; i <= asks; ++i)
printf("%d %d\n",ans[i].first,ans[i].second);
return 0;
}

CODE(TLE):

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1100010
#define INF 0x3f3f3f3f
using namespace std; inline int GetInt()
{
int res=0;
char t=getchar();
for (;t>'9'||t<'0';t=getchar());
for (;t>='0'&&t<='9';t=getchar()) res=res*10+t-'0';
return res;
} struct Ask{
int x,y,_id;
int a,b; bool operator <(const Ask &a)const {
if(x == a.x) return y < a.y;
return x < a.x;
}
void Read() {
x = GetInt();
y = GetInt();
a = GetInt();
b = GetInt();
}
}ask[MAX]; struct Edge{
int x,y,length; Edge(int _,int __,int ___):x(_),y(__),length(___) {}
Edge() {}
bool operator <(const Edge &a)const {
return length < a.length;
}
}edge[MAX << 3]; int cnt,asks;
int src[MAX]; int y_x[MAX],t,gt,edges;
pair<int,int *> xx[MAX << 1];
int fenwick[MAX << 1],_fenwick[MAX]; int father[MAX]; int head[MAX],total;
int next[MAX << 1],aim[MAX << 1]; int num[MAX];
pair<int,int> ans[MAX]; inline int CalcM(const Ask &a,const Ask &b)
{
return abs(a.x - b.x) + abs(a.y - b.y);
} inline int GetPos(int x)
{
int re = 0;
for(; x <= t; x += x&-x)
if(ask[fenwick[x]].x + ask[fenwick[x]].y < ask[re].x + ask[re].y)
re = fenwick[x];
return re;
} inline void Fix(int x,int pos)
{
for(; x; x -= x&-x)
if(ask[fenwick[x]].x + ask[fenwick[x]].y > ask[pos].x + ask[pos].y)
fenwick[x] = pos;
} void MakeGraph()
{
for(int dir = 1; dir <= 4; ++dir) {
if(dir == 2 || dir == 4)
for(int i = 1; i <= asks; ++i)
swap(ask[i].x,ask[i].y);
if(dir == 3)
for(int i = 1; i <= asks; ++i)
ask[i].y *= -1;
sort(ask + 1,ask + asks + 1);
for(int i = 1; i <= asks; ++i)
xx[i] = make_pair(ask[i].y - ask[i].x,&y_x[i]);
sort(xx + 1,xx + asks + 1);
t = 0;
for(int i = 1; i <= asks; ++i) {
if(!t || xx[i].first != xx[i - 1].first)
++t;
*xx[i].second = t;
}
memset(fenwick,0,sizeof(fenwick));
for(int i = asks; i; --i) {
int temp = GetPos(y_x[i]);
if(temp)
edge[++edges] = Edge(ask[temp]._id,ask[i]._id,CalcM(ask[temp],ask[i]));
Fix(y_x[i],i);
}
}
for(int i = 1; i <= asks; ++i)
ask[i].x *= -1;
} int Find(int x)
{
if(father[x] == x) return x;
return father[x] = Find(father[x]);
} inline void Add(int x,int y)
{
next[++total] = head[x];
aim[total] = y;
head[x] = total;
} void MST()
{
sort(edge + 1,edge + edges + 1);
for(int i = 1; i <= edges; ++i) {
int fx = Find(edge[i].x);
int fy = Find(edge[i].y);
if(fx != fy) {
father[fx] = fy;
Add(fx,fy),Add(fy,fx);
}
}
} inline int GetSum(int fenwick[],int x)
{
int re = 0;
for(; x; x -= x&-x)
re += fenwick[x];
return re;
} inline void Fix(int fenwick[],int x,int c)
{
for(; x <= gt; x += x&-x)
fenwick[x] += c;
} void DFS(int x,int last)
{
static int l = 1,r = 0; while(r < ask[x].y) {
++num[src[++r]];
Fix(fenwick,src[r],1);
if(num[src[r]] == 1) Fix(_fenwick,src[r],1);
}
while(l > ask[x].x) {
++num[src[--l]];
Fix(fenwick,src[l],1);
if(num[src[l]] == 1) Fix(_fenwick,src[l],1);
}
while(r > ask[x].y) {
--num[src[r]];
Fix(fenwick,src[r],-1);
if(!num[src[r]]) Fix(_fenwick,src[r],-1);
--r;
}
while(l < ask[x].x) {
--num[src[l]];
Fix(fenwick,src[l],-1);
if(!num[src[l]]) Fix(_fenwick,src[l],-1);
++l;
} int p = ask[x]._id;
ans[p].first = GetSum(fenwick,ask[x].b) - GetSum(fenwick,ask[x].a - 1);
ans[p].second = GetSum(_fenwick,ask[x].b) - GetSum(_fenwick,ask[x].a - 1); for(int i = head[x]; i; i = next[i]) {
if(aim[i] == last) continue;
DFS(aim[i],x); while(r < ask[x].y) {
++num[src[++r]];
Fix(fenwick,src[r],1);
if(num[src[r]] == 1) Fix(_fenwick,src[r],1);
}
while(l > ask[x].x) {
++num[src[--l]];
Fix(fenwick,src[l],1);
if(num[src[l]] == 1) Fix(_fenwick,src[l],1);
}
while(r > ask[x].y) {
--num[src[r]];
Fix(fenwick,src[r],-1);
if(!num[src[r]]) Fix(_fenwick,src[r],-1);
--r;
}
while(l < ask[x].x) {
--num[src[l]];
Fix(fenwick,src[l],-1);
if(!num[src[l]]) Fix(_fenwick,src[l],-1);
++l;
}
}
} int main()
{
ask[0].x = ask[0].y = INF;
cnt = GetInt();
asks = GetInt();
int temp = 0;
for(int i = 1; i <= cnt; ++i) {
xx[++temp].first = GetInt();
xx[temp].second = &src[i];
}
for(int i = 1; i <= asks; ++i) {
ask[i].Read();
xx[++temp] = make_pair(ask[i].a,&ask[i].a);
xx[++temp] = make_pair(ask[i].b,&ask[i].b);
ask[i]._id = i;
father[i] = i;
}
sort(xx + 1,xx + temp + 1);
for(int i = 1; i <= temp; ++i) {
if(!gt || xx[i].first != xx[i - 1].first)
++gt;
*xx[i].second = gt;
}
MakeGraph();
MST();
memset(fenwick,0,sizeof(fenwick));
DFS(1,0);
for(int i = 1; i <= asks; ++i)
printf("%d %d\n",ans[i].first,ans[i].second);
return 0;
}

BZOJ 3236 AHOI 2013 作业 莫队算法的更多相关文章

  1. BZOJ 3236 AHOI 2013 作业 莫队+树状数组

    BZOJ 3236 AHOI 2013 作业 内存限制:512 MiB 时间限制:10000 ms 标准输入输出     题目类型:传统 评测方式:文本比较 题目大意: 此时己是凌晨两点,刚刚做了Co ...

  2. 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...

  3. 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块

    [BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...

  4. bzoj 4540 [HNOI 2016] 序列 - 莫队算法 - Sparse-Table - 单调栈

    题目传送门 传送点I 传送点II 题目大意 给定一个长度为$n$的序列.询问区间$[l, r]$的所有不同的子序列的最小值的和. 这里的子序列是连续的.两个子序列不同当且仅当它们的左端点或右端点不同. ...

  5. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  6. bzoj 3757 苹果树(树上莫队算法)

    [题意] 有若干个询问,询问路径u,v上的颜色总数,另外有要求a,b,意为将a颜色看作b颜色. [思路] vfk真是神系列233. Quote: 用S(v, u)代表 v到u的路径上的结点的集合. 用 ...

  7. bzoj 3236: 洛谷 P4396: [AHOI2013]作业 (莫队, 分块)

    题目传送门:洛谷P4396. 题意简述: 给定一个长度为\(n\)的数列.有\(m\)次询问,每次询问区间\([l,r]\)中数值在\([a,b]\)之间的数的个数,和数值在\([a,b]\)之间的不 ...

  8. bzoj 4358 Permu - 莫队算法 - 链表

    题目传送门 需要高级权限的传送门 题目大意 给定一个全排列,询问一个区间内的值域连续的一段的长度的最大值. 考虑使用莫队算法. 每次插入一个数$x$,对值域的影响可以分成4种情况: $x - 1$, ...

  9. bzoj 2038 A-小Z的袜子[hose] - 莫队算法

    作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只袜子从1到N编号,然后从 ...

随机推荐

  1. LVS的调度算法分析

    LVS调度算法 一.静态调度算法 1.  rr(round robin)轮询调度,即调度器将客户端的请求依次的传递给内部的服务器,从1到N,算法简洁,无须记录状态,但是不考虑每台服务器的性能. 配置如 ...

  2. 下载jdk文件后缀是.gz而不是.tar.gz怎么办

    用chrom浏览器下载了linux版的jdk,发现文件后缀是.gz,没看过这玩意,一打开,还是一个.gz文件,原本以为是新文件后缀呢.那个百度google啊. . ..最后都没发现有这方面的资料啊.. ...

  3. hdu1505(dp求最大子矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1505 分析: 这题是HDU 1506 的加强版,定义一个二维数组,d[i][j]表示第i行j列元素在前 ...

  4. Hama学习总结

    Hama学习笔记 1.       Hama定义 Hama是基于HDFS上的BSP模型实现,其执行不须要MapReduce. 例证例如以下: 在单点调试的Hama系统上,仅仅执行NameNode.Da ...

  5. 让window命令行支持自己主动补全[相似Linux的Tab键]

    打开注冊表,找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor下 项"CompletionChar"(REG_DWO ...

  6. 核心ASP.NET

    1.用于Web应用程序的.NET Framework ASP.NET是.NET Framework的一部分,它可以在通过HTTP请求文档是在WEB服务器上动态创建它们,在.Net Framework中 ...

  7. 安装ArcGIS License 10.1 许可管理器 破解版 服务启动又失败的解决办法

    安装破解文件的提示执行 替换许可管理器Bin下面的service.txt  文件,之后会发现,许可管理器启动不了(有时候又可以,挺郁闷), 经过多次的试验,我找到了一种折中解决的方法,供大家参考 解决 ...

  8. 【Android工具类】Activity管理工具类AppManager

    转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 import java.util.Stack; import android.app.Activity; i ...

  9. 本科非cs菜鸟计算机面试实录

    两年制小硕,本硕期间差不多都打酱油的.本科非cs专业,硕士cs,编程基础一般,专业基础尚可.研究生期间分析分析了pgsql数据库的源码:同时实验室一些杂项目:自己业余为了应试读了些计算机书.自己当时q ...

  10. linux查看CPU和内存信息

    一 先来看看ps命令: 1.查看当前某个时间点的进程:ps命令就是最基本同时也是非常强大的进程查看命令.使用该命令可以确定有哪些进程正在运行和运行的状态.进程是否结束.进程有没有僵死. 哪些进程占用了 ...