hdu 4638 Group(莫队算法)
来源:互联网 发布:java获取一个月的天数 编辑:程序博客网 时间:2024/06/05 00:37
Problem Description
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.
Input
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].
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].
Output
For every query output a number indicate there should be how many group so that the sum of value is max.
Sample Input
15 23 1 2 5 41 52 4
Sample Output
12solution:
莫队算法模板题,只需要自己修改O(1)修改的地方。
#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>using namespace std;const int maxn = 100050;int unit;int a[maxn];int tmp, ans[maxn];bool num[maxn];struct Query{ int id, l, r; bool operator<(const Query &q)const { if (l / unit == q.l / unit) return r < q.r; return l / unit < q.l / unit; }}query[maxn];void insert(int x){ if (num[x-1] == 0 && num[x+1] == 0)tmp++; else if (num[x-1] && num[x+1])tmp--; num[x] = 1;}void remove(int x){ if (num[x-1] == 0 && num[x+1] == 0)tmp--; else if (num[x-1] == 1 && num[x+1] == 1)tmp++; num[x] = 0;}int main(){ int n, m, t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); unit = (int)sqrt((double)n); for (int i = 0; i < m; ++i) { query[i].id = i; scanf("%d%d", &query[i].l, &query[i].r); } sort(query, query + m); int l = 1, r = 0; tmp = 0; memset(num, 0, sizeof(num)); for (int i = 0; i<m; ++i) { while (r < query[i].r) insert(a[++r]); while (r > query[i].r) remove(a[r--]); while (l < query[i].l) remove(a[l++]); while (l > query[i].l) insert(a[--l]); ans[query[i].id] = tmp; } for (int i = 0; i <m; ++i) printf("%d\n", ans[i]); } return 0;}
0 0
- HDU 4638 Group(莫队算法)
- hdu 4638 Group(莫队算法)
- HDU 4638 Group 莫队算法
- HDU 4638 Group(莫队算法)
- hdu 4638 Group (莫队算法 || 离线线段树)
- hdu 4638 Group(莫队算法|离线线段树)
- HDU 4638 Group (莫队算法||线段树离散查询)
- hdu 4638 Group(莫队算法+分块)
- 文章标题 HDU 4638 : Group (莫队算法)
- HDU 4638 Group 【树状数组,分块乱搞(莫队算法?)】
- 离线处理(线段树|树状数组)| 莫对算法 —— HDU 4638 Group
- hdu 4638 Group
- HDU 4638 Group
- hdu 4638 Group
- HDU-4638-Group
- hdu 4638 Group
- hdu 4638 Group(莫队)
- HDU 4638 Group 【莫队】
- android 自定义View事件触发顺序
- epoll详解
- AOP 那点事儿
- 【Android】Android之创建活动
- 编程小记
- hdu 4638 Group(莫队算法)
- asp.net中的Gridview控件添加序号列
- 最小生成树之Prim算法---POJ1258---Agri-Net
- 112. Path Sum
- Linux常用快捷键
- 使用GridView以表格形式显示多张图片
- HDOJ 1116 Play on Words
- Android studio的genymotion的安装
- java--web.xml中url-pattern的映射规则