Group

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

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
 
 
 
队友的思路!!
 
先按区间右端点r排升序!!
 
下面是操作:(若之前有相邻的,就把那位上减1,相当于把之前的组归到当前的组!!!)
 
如    3          1          2         5        4
 
3 in: 1
 
1 in: 1                1
 
2 in: 1-1=0     1-1=0      1
 
5 in:    0       0       1       1
 
4 in:   0-1=-1        0       1                      1-1=0            1
 
 
然后再区间求和即可!!!
 
 
 
 #include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
#define lowbit(x) (x&(-x))
const int N=;
int C[N],n; void add(int x,int inc){
while(x<=n){
C[x]+=inc;
x+=lowbit(x);
}
}
int sum(int x){
int res=;
while(x){
res+=C[x];
x-=lowbit(x);
}
return res;
} int a[N];
struct node
{
int l,r,id,ans;
}query[N];
struct ppp
{
int x[],cc;
void init()
{
memset(x,,sizeof(x));
cc=;
}
}b[N];//b[i]用来储存与i相邻的数的下标且 这个相邻的数在i之前!!
bool cmpR(node a,node b)
{
return a.r<b.r;
}
bool cmpID(node a,node b)
{
return a.id<b.id;
}
int main()
{
int i,m,T;
scanf("%d",&T);
while(T--)
{ memset(C,,sizeof(C));
for(i=;i<N;i++)b[i].init();
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=m;i++)scanf("%d%d",&query[i].l,&query[i].r),query[i].id=i;
sort(query+,query+m+,cmpR);
int cnt=;
for(i=;i<=n;i++)
{
int tmp=a[i];
if(b[tmp].x[])add(b[tmp].x[],-);//将之前出现过与之相邻的数减1,单点更新!!
if(b[tmp].x[])add(b[tmp].x[],-);
if(tmp->=)
{
b[tmp-].x[++b[tmp-].cc]=i;
}
if(tmp+<=n)
{
b[tmp+].x[++b[tmp+].cc]=i;
}
add(i,);
while(i==query[cnt].r)
{
query[cnt].ans=sum(query[cnt].r)-sum(query[cnt].l-);
cnt++;
}
}
sort(query+,query+m+,cmpID);
for(i=;i<=m;i++)printf("%d\n",query[i].ans);
}
}
 
 

hdu 4638 Group(离线+树状数组)的更多相关文章

  1. HDU 4638 group(离线+树状数组)

    此题实质上是询问每段区间[l,r]内的数字能组成多少段连续的数字. 不大好用线段树合并区间来写. 考虑离线,类似于HH的项链的话,对询问按右端点排序,对于右端点的每一次右移,则对于询问,实际上是求的后 ...

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

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

  3. 区间的关系的计数 HDU 4638 离线+树状数组

    题目大意:给你n个人,每个人都有一个id,有m个询问,每次询问一个区间[l,r],问该区间内部有多少的id是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了 ...

  4. hdu 4605 Magic Ball Game (在线主席树/离线树状数组)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4605 题意: 有一颗树,根节点为1,每一个节点要么有两个子节点,要么没有,每个节点都有一个权值wi .然后,有一个球,附带值x . 球 ...

  5. HDU 2852 KiKi's K-Number(离线+树状数组)

    题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...

  6. HDU - 4777 离线树状数组

    离线树状数组搞一搞. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #de ...

  7. HDU 4417 离线+树状数组

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. POJ 3416 Crossing --离线+树状数组

    题意: 给一些平面上的点,然后给一些查询(x,y),即以(x,y)为原点建立坐标系,一个人拿走第I,III象限的点,另一个人拿II,IV象限的,点不会在任何一个查询的坐标轴上,问每次两人的点数差为多少 ...

  9. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

  10. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)

    转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...

随机推荐

  1. 【c++进阶:c++ 顺序容器vector,string,deque,list,forward_list,array常用性质】

    常用5种顺序容器性质: https://blog.csdn.net/oil_you/article/details/82821833 关于deque https://www.cnblogs.com/L ...

  2. MYSQL常见安装错误集:[ERROR] --initialize specified but the data directory has files in it. Abort

    1.[ERROR] --initialize specified but the data directory has files in it. Abort [错误] -初始化指定,但数据目录中有文件 ...

  3. lua 十进制转二进制

    -- Converts a byte to a string of 0s and 1s. function byte2bin(n) local t = {} for i=7,0,-1 do t[#t+ ...

  4. Java 语言特性之 Annotation 注解

    利用 Java 的反射机制,可以在运行时获取 Java 类的注解信息. 注解 注解的特性 注解是 Java 5 的一个新特性,是插入代码中的一种注释或者说是元数据.注解并不是程序代码,可以对程序作出解 ...

  5. QA的工作职责是什么?

    目前不知道,后续一点一点查资料补充吧 QA不管做什么的类型的测试,最基础的功能测试,需要搭建测试环境:进阶部分的性能压力测试,对搭建环境的要求更高:接口功能测试,搭建测试环境,和功能测试的差不多: 测 ...

  6. Jmeter 接口测试 响应结果中文是Unicode转为中文

    1.增加一个后置处理器:BeanShell PostProcessor 内容如下: //获取响应代码Unicode编码的        String s2=new String(prev.getRes ...

  7. cocos2dx基础篇(10) 按钮控件CCControlButton

    [3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)按钮事件回调依旧为 cccontrol_selector ,没有使用 CC_CALLBACK_2 (4)按钮状态  ...

  8. Lesson 5 The facts

    go to extremes走极端 provide... with...向..提供.. go to press付印 suspicious,adj. 可疑的:怀疑的:多疑的 fired---同义词--- ...

  9. mysql数据库自带数据库介绍

    show databases:查看mysql自带数据库有information_schema,mysql, performance_schema, test information_schema数据库 ...

  10. [每天一课] 今天就讲一讲关于vue-cli 脚手架里 如何调用API

    既然vue-resource停更了,就不讲了,但是效果也是差不多了 今天主要讲一下关于axios的方式来调用API,按照vue-cli的模式,这个当然得先引入一个axios这个模块了.那当然得走一遍终 ...