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. [干货]Kaggle热门 | 用一个框架解决所有机器学习难题

    新智元推荐 来源:LinkedIn 作者:Abhishek Thakur 译者:弗格森 [新智元导读]本文是数据科学家Abhishek Thakur发表的Kaggle热门文章.作者总结了自己参加100 ...

  2. 【python3】集合set (转)

    https://www.cnblogs.com/onepeace/p/4791578.html set原理 Python 还 包 含 了 一 个 数 据 类 型—— set ( 集 合 ) . 集 合 ...

  3. Pandas对行情数据的预处理

    库里是过去抓取的行情数据,间隔6秒,每分钟8-10个数据不等,还有开盘前后的一些数据,用Pandas可以更加优雅地进行处理. 需要把当前时间设置为index df=df.set_index('time ...

  4. [Spring boot] Read values from external properties file

    Let's say we have a extral app.proporites file which contains some extra configuration: // resources ...

  5. Python requests如何将第一个请求得到的 cookie 通过 POST 提交给第二个请求

    #coding=utf-8 import requests import json url_login, url_test = "http://192.168.0.162/login&quo ...

  6. (纪录片)现代生活的秘密规则:算法 The Secret Rules of Modern Living: Algorithms

    简介: The Secret Rules of Modern Living: Algorithms (2015) 导演: David Briggs主演: Marcus du Sautoy类型: 纪录片 ...

  7. CSS3实现的苹果网站搜索框效果

    在线演示 本地下载 用CSS3相关属性生成的动态搜索框效果.

  8. Office办公 如何给WPS 的文字添加黑框

    双击文本框,弹出的设置对象格式中选择线条颜色和样式即可   要做成虚线框也是可以的   弄完之后字体也可以改大一点   还有一种需要时文本不在黑框的中间位置,比如顶部的某个位置,可以设置文字锁定点是顶 ...

  9. android中NavigationView(Design Support)的使用

    NavigationView可以实现美观的菜单功能展示,下面看一下怎么使用NavigationView 先是主Activity activity_main.xml: <?xml version= ...

  10. Redhat、Centos等系统配置进行网络配置的方法

    配置文件(Redhat) /etc/sysconfig/network-scripts/* : 网卡/etc/sysconfig/network-scripts/ifcfg-eth0/etc/reso ...