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. 阿里巴巴Java开发规约插件全球首发!(转)

    https://mp.weixin.qq.com/s?__biz=MzI0NTE4NjA0OQ==&mid=2658355901&idx=1&sn=3169172bfc6819 ...

  2. 转:centos7 安装与使用 postgreSQL

    一. 安装与基本说明都非常的详细. https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql ...

  3. Eclipse中GitLab的配置和使用入门

    一.Eclipse中配置GitLab的前提条件 1.1:安装Git客户端 去官网https://git-scm.com/downloads下载合适的版本即可,一般开发环境是windows的就下载win ...

  4. ZOJ 3456 Traveler Nobita 最小生成树

    Traveler Nobita Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita used a time machin ...

  5. Spring+hibernate+struts错题集

    1.严重: Exception starting filter struts2 java.lang.ClassNotFoundException: org.apache.struts2.dispatc ...

  6. jQuery.event详细解析

    介绍之前先介绍jQuery的一个方法 jQuery.event.fix(event || window.event); 此方法个浏览器的event对象转换为 jQuery.event; 如果您的事件是 ...

  7. ZH奶酪:Python按行读取文件

    1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break ...

  8. scoop - 初次使用

    scoop也是包管理工具,不过是含着金钥匙出生的(正巧碰上微软支持开源,并且拥抱开源生态圈),此后的Win10 powershell 3.x+也就不会像Win7 powershell 2.x那样沉默了 ...

  9. vcenter建立cluster

    参见: VMware vSphere 5.1 学习系列之六:vCenter Server 主机管理(2) http://bbs.watchstor.com/thread-166222-1-2.html ...

  10. ubuntu中apache2的安装与卸载

    一.装apache2 安装命令:sudo apt-get install apache2 启动/停止/重启apache2: service apache2 start/stop/restart 二. ...