Group(hdu4638,树状数组)
来源:互联网 发布:macbook pro mac air 编辑:程序博客网 时间:2024/05/21 21:41
http://acm.hdu.edu.cn/showproblem.php?pid=4638
Group
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 145 Accepted Submission(s): 75
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].
Output
For every query output a number indicate there should be how many group so that the sum of value is max.
Sample Input
1
5 2
3 1 2 5 4
1 5
2 4
Sample Output
1
2
Source
2013 Multi-University Training Contest 4
Recommend
zhuyuanchen520
Statistic | Submit | Discuss | Note
解析:
题意:给出一组数据,询问在给定区间里,有多少段连续的数字(位置不一定要连续的)
思路:
1.利用树状数组从组到右一一更新(根据a[i]-1以及a[i]+1位置更新,例如:如果前 i中不含a[i]-1或a[i]+1,段数必然增加)
2.将询问区间按照从起始位置由小到大的顺序排列。然后从左开始删除(是上面的逆过程,区间判断规则同上)
PS:区间更新是难点,开始的时候有思路可是一直就是写不出来
仿KB写的:*/
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>using namespace std;const int maxn=100000+10;int v[maxn],c[maxn];int ans[maxn],a[maxn],n;struct node{int s;int t;int id;}q[maxn];bool cmp(node n1,node n2){return n1.s<n2.s;}int lowbit(int x){return x&(-x);}void add(int i,int d){while(i<=n){c[i]+=d;i+=lowbit(i);}}int sum(int i){ int ret=0;while(i>0){ret+=c[i];i-=lowbit(i);}return ret;}int main(){int T,i,j,m;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);memset(v,0,sizeof(v));memset(c,0,sizeof(c));memset(ans,0,sizeof(ans));for(i=1;i<=n;i++){scanf("%d",&a[i]);v[a[i]]=i;}v[0]=n+n;v[n+1]=n+n;for(i=1;i<=n;i++)//从1--n.更新{if(i>v[a[i]-1]&&i>v[a[i]+1])add(i,-1);else if(i<v[a[i]-1]&&i<v[a[i]+1])add(i,1);}for(i=1;i<=m;i++){scanf("%d%d",&q[i].s,&q[i].t);q[i].id=i;}sort(q+1,q+m+1,cmp);i=1;j=1;while(j<=m){while(i<=n&&q[j].s>i)//将左边的删除{if(i>v[a[i]-1]&&i>v[a[i]+1])add(i,-1);else if(i<v[a[i]-1]&&i<v[a[i]+1]){ add(i,-1);int mi=v[a[i]-1]<v[a[i]+1]? v[a[i]-1]:v[a[i]+1];int ma=v[a[i]-1]>v[a[i]+1]? v[a[i]-1]:v[a[i]+1];add(mi,1);add(ma,1);}else if(i<v[a[i]-1]){ add(i,-1);add(v[a[i]-1],1);}else{ add(i,-1);add(v[a[i]+1],1);}i++;}while(j<=m&&q[j].s<=i){ ans[q[j].id]=sum(q[j].t);j++;}}for(i=1;i<=m;i++)printf("%d\n",ans[i]);}return 0;}
- Group(hdu4638,树状数组)
- hdu4638 Group(树状数组)
- HDU4638——Group(树状数组+离线操作)
- HDU4638【离线树状数组】
- [HDU4638]Group(莫队)
- hdu 4638 Group 树状数组
- HDU 4638 Group(树状数组)
- 【树状数组】 HDOJ 4638 Group
- HDU 4638 Group (树状数组)
- HDU4638:Group(线段树离线处理)
- hdu4638 Group(离线线段树)
- hdu 多校contest4 group(树状数组)
- hdu 4638 Group (树状数组+离线)
- HDU 4638 Group(离线 + 树状数组)
- Hdu 4638 Group 离线+树状数组
- HDU 4638 - Group(树状数组 / 线段树)
- hdu 4638 Group (离线树状数组)
- HDU 4638 Group [离线+树状数组]
- C++ 成员列表初始化
- VS2013中的C++11新特性(转载)
- Fliping game(hdu4642)
- JS
- c++读取ccbi
- Group(hdu4638,树状数组)
- JavaScript学习总结(二)
- 学了三个周的C#小结(1)
- Hehe(hdu4639,斐波拉契数+排列组合)
- 安装wampserver2时提示丢失MSVCR100.dll
- static在C和C++中各代表什么含义?
- 【VB.NET2010】注册表写入方法
- 热备份模式【Begin Backup】(1)--原理理解
- stm32中“拔掉jlink”程序无法正确运行