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

思路:因为没有改动,所以就想到了莫队算法。然后我写了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. DecimalFormat

    public class TestDemo { public static void main(String[] args) { String format = new DecimalFormat(& ...

  2. 公布AppStore被拒绝的经历

    我们知道IOS发布的版本有很多原因是苹果拒绝发表,我总结了这里3手头上做的原因,他拒绝游,包括同事.朋友拒绝的理由,IOS app参考朋友. 1. 使用非公开API该计划将被拒绝 2. beta版.d ...

  3. JSP路径出现故障

    1.错误叙述性说明 2.错误原因 <%@ page language="java" import="java.util.*" pageEncoding=& ...

  4. JAVA实现Shell排序

    Shell排序可以理解为插入排序的变种,它充分利用了插入排序的两个特点: (1). 当数据规模小的时候非常高效. (2). 当给定数据已经有序时的时间代价为O(N) 所以,Shell排序每次把数据分成 ...

  5. HDU 4815 背包

    标题的含义给出N问题.和概率P,然后给予相应的分数为每个问题x(每个问题只有两种选择,纠正错误). 两个人来回答.一个人是随机选择的答案,问:还有一个人的至少一些点的能力有保证P概率不会失败. 01背 ...

  6. Debian7.6安装过程中遇到的问题

    一 sudo命令不能用 1 使用su切换到root用户,命令: su 2 使用名:vim /etc/sudoers加入sudoer用户,命令: vim /etc/sudoers 找到root=(ALL ...

  7. shufe前辈名师

    前辈名师 姓名 现职/原职 郭秉文 中国现代大学之父.国立东南大学校长.哥伦比亚大学教育学博士,该校第一任校长.为了纪念郭秉文先生,勉励优秀学子,郭夏瑜女士在上海财经大学等校设立了“郭秉文奖学金” 马 ...

  8. wamp5中的apache不能启动,80端口被占用

    在wamp中apache中的httpd.conf文件中 端口文件设置为8080 #Listen 12.34.56.78:8080Listen 8080

  9. DWR异步产生的问题

    默认情况下,DRW是异步的.当数据量大的时候,数据还未加载完就已经提交了.这样会照成数据丢失.为了解决这个问题应该改变DWR的数据加载方式,改为同步的.这样就不会照成数据丢失. DWREngine.s ...

  10. HDU 1171 Big Event in HDU (多重背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...