Group

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 208    Accepted Submission(s): 122

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
 
Recommend
zhuyuanchen520
 
题意:找到区间能,有多少组连续数字串 

(转)思路:显然,我们要使得value最大,就要尽量将连续的ID分在一组,所以问题转化为求一个区间中连续ID区间的个数。我们从左往右扫描,依次考虑右端点为i的询问,设dp[l]为区间[l,i]的连续区间个数,po[i]为i出现的位置,若还未出现,则为0,设我们当前考虑的右端点为a[i],首先我们假设a[i]不能和区间[1,i-1]中的任何一个数分到一组,则我们要将dp[1]到dp[i-1]全部加1,然后考虑po[a[i]+1]是否不为0,若不为0则说明a[i]-1已经在前面出现,则我们需要将dp[1]到dp[po[a[i]+1]]全部减一个1,因为a[i]可以和a[i]+1分为一组,则我们之前加的1是多余的。对于a[i]-1的情况同理。以上操作可以由线段树或者树状数组什么的实现,然后再将询问按照右端点从小到大排序,离线处理即可,以下是代码实现

线段树:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int N=; #define L(rt) (rt<<1)
#define R(rt) (rt<<1|1) struct Tree{
int l,r;
int num; //num记录(l,r)区间内的组数
}tree[N<<]; struct node{
int l,r;
int id;
}f[N]; int cmp(node a,node b){
return a.r<b.r;
} int a[N],loc[N],res[N]; void build(int L,int R,int rt){
tree[rt].l=L;
tree[rt].r=R;
tree[rt].num=;
if(tree[rt].l==tree[rt].r)
return ;
int mid=(L+R)>>;
build(L,mid,L(rt));
build(mid+,R,R(rt));
} void update(int L,int R,int val,int rt){
if(tree[rt].l==L && tree[rt].r==R){
tree[rt].num+=val;
return ;
}
int mid=(tree[rt].l+tree[rt].r)>>;
if(R<=mid)
update(L,R,val,L(rt));
else if(L>=mid+)
update(L,R,val,R(rt));
else{
update(L,mid,val,L(rt));
update(mid+,R,val,R(rt));
}
tree[rt].num=tree[L(rt)].num+tree[R(rt)].num;
} int query(int L,int R,int rt){
if(tree[rt].l==L && tree[rt].r==R)
return tree[rt].num;
int mid=(tree[rt].l+tree[rt].r)>>;
if(R<=mid)
return query(L,R,L(rt));
else if(L>=mid+)
return query(L,R,R(rt));
else{
int a=query(L,mid,L(rt));
int b=query(mid+,R,R(rt));
return a+b;
}
} int main(){ //freopen("input.txt","r",stdin); int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
loc[a[i]]=i;
}
for(int i=;i<=m;i++){
scanf("%d%d",&f[i].l,&f[i].r);
f[i].id=i;
}
sort(f+,f+m+,cmp); //将查找区间以右右区间递增排序,方便在更改线段树的时候查找,不会漏掉
build(,n,);
int i,j=;
for(i=;i<=n;i++){
update(i,i,,); //每次新进的一位数,假设独立,所有已i结尾的区间都+1
if(a[i]<n && loc[a[i]+]<i) //每次删除掉之前加入的与a[i]相邻的数,因为它们在一组里
update(loc[a[i]+],loc[a[i]+],-,);
if(a[i]> && loc[a[i]-]<i)
update(loc[a[i]-],loc[a[i]-],-,);
while(j<=m && f[j].r==i){
res[f[j].id]=query(f[j].l,f[j].r,);
j++;
}
}
for(i=;i<=m;i++)
printf("%d\n",res[i]);
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int N=; int n,m;
int arr[N],a[N],loc[N],res[N]; struct node{
int l,r;
int id;
}f[N]; int cmp(node a,node b){
return a.r<b.r;
} int lowbit(int x){
return x&(-x);
} void update(int i,int val){
while(i<=n){
arr[i]+=val;
i+=lowbit(i);
}
} int sum(int i){
int ans=;
while(i>){
ans+=arr[i];
i-=lowbit(i);
}
return ans;
} int main(){ //freopen("input.txt","r",stdin); int t; //刚开在这里使用 int t,n,m; 这里的局部变量影响了在main函数外面定义的全局变量n(默认值为0),导致我无奈了。。。。。
scanf("%d",&t);
while(t--){ scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
loc[a[i]]=i;
}
for(int i=;i<=m;i++){
scanf("%d%d",&f[i].l,&f[i].r);
f[i].id=i;
}
sort(f+,f+m+,cmp); //将查找区间以右右区间递增排序,方便在更改线段树的时候查找,不会漏掉
memset(arr,,sizeof(arr));
int i,j=; for(i=;i<=n;i++){
update(i,); //每次新进的一位数,假设独立,所有已i结尾的区间都+1
if(a[i]<n && loc[a[i]+]<i) //每次删除掉之前加入的与a[i]相邻的数,因为它们在一组里
update(loc[a[i]+],-);
if(a[i]> && loc[a[i]-]<i)
update(loc[a[i]-],-);
while(j<=m && f[j].r==i){
res[f[j].id]=sum(f[j].r)-sum(f[j].l-);
j++;
}
}
for(i=;i<=m;i++)
printf("%d\n",res[i]);
}
return ;
}

HDU 4638 Group (线段树 | 树状数组 + 离线处理)的更多相关文章

  1. hdu 5057 Argestes and Sequence (数状数组+离线处理)

    题意: 给N个数.a[1]....a[N]. M种操作: S X Y:令a[X]=Y Q L R D P:查询a[L]...a[R]中满足第D位上数字为P的数的个数 数据范围: 1<=T< ...

  2. Necklace HDU - 3874 (线段树/树状数组 + 离线处理)

    Necklace HDU - 3874  Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...

  3. 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...

  4. SPOJ DQUERY树状数组离线or主席树

    D-query Time Limit: 227MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submit Status ...

  5. D-query SPOJ 树状数组+离线

    D-query SPOJ 树状数组+离线/莫队算法 题意 有一串正数,求一定区间中有多少个不同的数 解题思路--树状数组 说明一下,树状数组开始全部是零. 首先,我们存下所有需要查询的区间,然后根据右 ...

  6. HDU 4638 Group ★(树状数组)

    题意 询问一段区间里的数能组成多少段连续的数. 思路 先考虑从左往右一个数一个数添加,考虑当前添加了i - 1个数的答案是x,那么可以看出添加完i个数后的答案是根据a[i]-1和a[i]+1是否已经添 ...

  7. HDU 4638 Group 【树状数组,分块乱搞(莫队算法?)】

    根据题目意思,很容易得出,一个区间里面连续的段数即为最少的group数. 题解上面给的是用树状数组维护的. 询问一个区间的时候,可以一个一个的向里面添加,只需要判断a[i]-1 和 a[i]+1是否已 ...

  8. HDU 4638 Group (2013多校4 1007 离线处理+树状数组)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. HDU 4638 Group 树状数组 + 思路

    实际上就是问这个区间编号连续的段的个数,假如一个编号连续的段有(a+b)个人,我把他们分在同一组能得到的分值为(a+b)^2,而把他们分成人数为a和b的两组的话,得到的分值就是a^2+b^2,显然(a ...

随机推荐

  1. Double-Array Trie分词词典简述

    http://www.xuebuyuan.com/1991441.html 一.TRIE树简介(以下简称T树) TRIE树用于确定词条的快速检索,对于给定的一个字符串a1,a2,a3,…an,则采用T ...

  2. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十四)Structured Streaming:Encoder

    一般情况下我们在使用Dataset<Row>进行groupByKey时,你会发现这个方法最后一个参数需要一个encoder,那么这些encoder如何定义呢? 一般数据类型 static ...

  3. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十一)定制一个arvo格式文件发送到kafka的topic,通过Structured Streaming读取kafka的数据

    将arvo格式数据发送到kafka的topic 第一步:定制avro schema: { "type": "record", "name": ...

  4. matlab中subplot函数的功能 类似python

    原文:http://blog.163.com/my_it_dream_pwj/blog/static/17841430520112294342649/ 和python 中类似 subplot 功能 分 ...

  5. 如何通过 iframe 共享 jQuery $.data?

    index.html <div id="div1"> </div> <iframe src="ajax2.html" name=& ...

  6. 20160205.CCPP体系具体解释(0015天)

    程序片段(01):01.杨辉三角.c 内容概要:杨辉三角 #include <stdio.h> #include <stdlib.h> #define N 10 //01.杨辉 ...

  7. C语言常见问题 如何用Visual Studio编写C语言程序测试

    新建Win32控制台应用程序   勾选控制台空项目   右击源文件,添加cpp文件   输入范例代码,可以正常运行#include <stdio.h> int main(void) { i ...

  8. springboot项目中报错:listener does not currently know of SID given in connect descriptor

    springboot项目中报错:listener does not currently know of SID given in connect descriptor 出现这个问题的原因是SID写错了 ...

  9. Android View的事件分发机制

    准备了一阵子,一直想写一篇事件分发的文章总结一下.这个知识点实在是太重要了. 一个应用的布局是丰富的,有TextView,ImageView,Button等.这些子View的外层还有ViewGroup ...

  10. 微信小程序文本如何换行

    替换<br/>标签 为 \n 使用 css 属性 :white-space:pre-wrap   举个例子: <view style="white-space:pre-wr ...