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. 错误1919,配置ODBC数据源MS Access Database时发生错误ODEC错误

    WIN7 64位旗舰版安装OFFICE2003 提示:“错误1919,配置ODBC数据源MS Access Database时发生错误ODEC错误” 在64位系统上,32位软件的注册表的信息不是直接在 ...

  2. Using FileUpload

    Using FileUpload FileUpload can be used in a number of different ways, depending upon the requiremen ...

  3. m3u8直播测试地址

    调试m3u8的时候需要测试地址 找了几个,备用一下 安徽卫视 http://stream2.ahtv.cn/ahws/cd/live.m3u8经济生活 http://stream2.ahtv.cn/j ...

  4. freetype相关总结

    FreeType-2.7 API : https://www.freetype.org/freetype2/docs/reference/ft2-basic_types.html ---------- ...

  5. leetcode 234 回文链表 Palindrome Linked List

    要求用O(n)时间,和O(1)空间,因此思路是用本身链表进行判断,既然考虑回文,本方法思想是先遍历一次求链表长度,然后翻转前半部分链表:然后同时对前半部分链表和后半部分链表遍历,来判断对应节点的值是否 ...

  6. ubuntu用mentohust连接ruijie

    32位  http://download.csdn.net/detail/yan456jie/8720395 64位 http://download.csdn.net/detail/yan456jie ...

  7. 网站模板-AdminLTE:AdminLTE

    ylbtech-网站模板-AdminLTE:AdminLTE 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://adminlte.io/ 1. ...

  8. java dwg转svg

    package com.example.demo.dxf2svg; import com.aspose.cad.InterpolationMode; import com.aspose.cad.Smo ...

  9. 浅谈防火墙对FTP的影响及故障排除

    本文转载自:http://www.cnblogs.com/emanlee/archive/2013/01/07/2849680.html  向作者致敬! TP是常见的基于TCP的网络服务,它使用了两个 ...

  10. Pycharm激活方法(license server方法)

    pycharm所有版本 http://www.jetbrains.com/pycharm/download/previous.html 打开激活窗口 选择 Activate new license w ...