hdu 4638 Group(莫队算法+分块)
2016-04-26 19:59
381 查看
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1966 Accepted Submission(s): 1012
[align=left]Problem Description[/align]
There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interval of men to make some group. K men in a group
can create K*K value. The value of an interval is sum of these value of groups. The people of same group's id must be continuous. Now we chose an interval of men and want to know there should be how many groups so the value of interval is
max.
[align=left]Input[/align]
First line is T indicate the case number.
For each case first line is n, m(1<=n ,m<=100000) indicate there are n men and m query.
Then a line have n number indicate the ID of men from left to right.
Next m line each line has two number L,R(1<=L<=R<=n),mean we want to know the answer of [L,R].
[align=left]Output[/align]
For every query output a number indicate there should be how many group so that the sum of value is max.
[align=left]Sample Input[/align]
1 5 2 3 1 2 5 4 1 5 2 4
[align=left]Sample Output[/align]
1 2
[align=left]Source[/align]
2013 Multi-University Training Contest 4
[align=left]Recommend[/align]
zhuyuanchen520 | We have carefully selected several similar problems for you: 5674 5673 5672 5671 5670
Statistic | Submit | Discuss
| Note
题解:莫队算法+分块
脑残错误害死人啊。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define N 100003 using namespace std; int n,m,t; int ans1 ,num ,b ,p ,vis ,belong ,a ; struct data { int l,r,num; }; data a1 ; int cmp1(data a,data b) { if (belong[a.l]==belong[b.l]) return a.r<b.r; return belong[a.l]<belong[b.l]; } int main() { scanf("%d",&t); for (int k=1;k<=t;k++) { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%d",&a[i]); for (int i=1;i<=m;i++) scanf("%d%d",&a1[i].l,&a1[i].r),a1[i].num=i; int size=ceil(sqrt(n)); for (int i=1;i<=n;i++) belong[i]=(i-1)/size+1; sort(a1+1,a1+m+1,cmp1); memset(vis,0,sizeof(vis)); memset(ans1,0,sizeof(ans1)); int l=1; int r=0; int ans=0; for (int i=1;i<=m;i++) { while (r<a1[i].r) { r++; vis[a[r]]=1; if (vis[a[r]-1]&&vis[a[r]+1]) ans--; if (!vis[a[r]-1]&&!vis[a[r]+1]) ans++; } while (r>a1[i].r) { vis[a[r]]=0; if (vis[a[r]-1]&&vis[a[r]+1]) ans++; if (!vis[a[r]-1]&&!vis[a[r]+1]) ans--; r--; } while (l>a1[i].l) { l--; if (vis[a[l]-1]&&vis[a[l]+1]) ans--; if (!vis[a[l]-1]&&!vis[a[l]+1]) ans++; vis[a[l]]=1; } while (l<a1[i].l) { vis[a[l]]=0; if (vis[a[l]-1]&&vis[a[l]+1]) ans++; if (!vis[a[l]-1]&&!vis[a[l]+1]) ans--; l++; } ans1[a1[i].num]=ans; } for (int i=1;i<=m;i++) printf("%d\n",ans1[i]); } }
相关文章推荐
- TCP协议三次握手过程分析
- 《C++ Primer》习题
- 【BZOJ4516】【Sdoi2016】生成魔咒 后缀数组 线段树
- linux编程问题记录
- 防止XSS攻击过滤工具类
- Java线程之interrupt终止线程,Thread中的主要方法(五)
- Elasticsearch调优
- [HDU4638]Group(莫队)
- Pascal位运算基础(1)
- 读书
- ie9渲染的bug
- 简单的流布局实现
- 剑指offer(29):字符串的排列
- linux命令之scp
- PHP之Mysql常用SQL语句示例的深入分析
- #leetcode#231. Power of Two
- MySQL出现中文乱码
- 学习笔记(八)数据存储
- 从客户端(content1=" ...")中检测到有潜在危险的 Request.Form 值
- 从客户端(content1="<table> <tbody> ...")中检测到有潜在危险的 Request.Form 值