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. YV12和NV12格式

    害怕搞忘 直接保存图片

  2. 一元回归1_基础(python代码实现)

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  3. 集成微信支付的代码。兼容小程序,扫码,app,公众号。h5 支付 ,以及 服务商提现

      /** * 因为微信总是很多个商户号很多和appid.很多个密钥文件,所以全部改成手动传值的方式,就可以支持多商户调用 * * @param appId 商户的appid * @param mch ...

  4. 【C++进阶:atoi()与itoa()】

    两种函数: atoi 把字符串转为整形: itoa 整形转为字符串: https://www.cnblogs.com/bluestorm/p/3168719.html

  5. python-接口开发flask模块(一)工具类准备

    我们常常听说测试http接口.测试java接口,测试socket接口等等:那么python这么强大的语言当然也可以用来开发接口了. flask模块介绍: python中用来开发接口的模块:flask, ...

  6. Delphi XE2 之 FireMonkey 入门(34) - 控件基础: TFmxObject: 克隆对象

    Delphi XE2 之 FireMonkey 入门(34) - 控件基础: TFmxObject: 克隆对象 有两个和克隆相关的方法: Clone().CloneChildFromStream(). ...

  7. Unity UI —Text

    Character Text 文本字体的编辑 Font Style 字体格式可以自行下载也可在windows自带字体中查找 Font Size 字体尺寸 Line Spacing 行距 Rich Te ...

  8. 彻底理解RSA算法原理

    1. 什么是RSA RSA算法是现今使用最广泛的公钥密码算法,也是号称地球上最安全的加密算法.在了解RSA算法之前,先熟悉下几个术语 根据密钥的使用方法,可以将密码分为对称密码和公钥密码 对称密码:加 ...

  9. [转帖]Intel Xeon路线图:7nm处理器要上DDR5、PCIe 5.0

    Intel Xeon路线图:7nm处理器要上DDR5.PCIe 5.0 https://www.cnbeta.com/articles/tech/849631.htm 在月初的投资者会议上,Intel ...

  10. Python内置函数compile

    英文文档: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) Compile the source i ...