Group

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

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
 

把查询区间按照左端点排序。

然后逐渐从左边删除数,看对后面的影响。

树状数组实现单点更新和求和

/*
* Author:kuangbin
* 1007.cpp
*/ #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <math.h>
using namespace std;
const int MAXN = ;
int n; int lowbit(int x)
{
return x&(-x);
}
int c[MAXN];
void add(int i,int val)
{
while(i <= n)
{
c[i] += val;
i += lowbit(i);
}
}
int sum(int i)
{
int s = ;
while(i > )
{
s += c[i];
i -= lowbit(i);
}
return s;
}
int a[MAXN];
int num[MAXN]; int ans[MAXN];
struct Node
{
int l,r;
int index;
}node[MAXN];
bool cmp(Node a,Node b)
{
return a.l < b.l;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int m;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(c,,sizeof(c));
for(int i = ;i <= n;i++)
{
scanf("%d",&a[i]);
num[a[i]]=i;
}
num[] = n+;
num[n+] = n+;
for(int i = ;i <= n;i++)
{
if(i < num[a[i]-] && i < num[a[i]+])
add(i,);
else if(i > num[a[i]-] && i > num[a[i]+])
add(i,-);
}
for(int i = ;i < m;i++)
{
scanf("%d%d",&node[i].l,&node[i].r);
node[i].index = i;
}
sort(node,node+m,cmp);
int i = ;
int j = ;
while(j < m)
{
while(i <= n && i < node[j].l)
{
if(i > num[a[i]-] && i > num[a[i]+])
add(i,-);
else if(i < num[a[i]-] && i < num[a[i]+])
{
int Min = min(num[a[i]-],num[a[i]+]);
int Max = max(num[a[i]-],num[a[i]+]);
add(i,-);
add(Min,);
add(Max,);
}
else if(i < num[a[i]-])
{
add(i,-);
add(num[a[i]-],);
}
else
{
add(i,-);
add(num[a[i]+],);
}
i++;
}
while( j < m && node[j].l <= i)
{
ans[node[j].index]= sum(node[j].r);
j++;
}
}
for(int i = ;i < m;i++)
printf("%d\n",ans[i]);
}
return ;
}

HDU 4638 Group (2013多校4 1007 离线处理+树状数组)的更多相关文章

  1. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  2. Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

    题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组  时间限制: 1 s   空间限制: 128000 KB   题目等级 : ...

  3. hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5792 题目描述:给你n个值,每个值用A[i]表示,然后问你能否找到多少组(a,b,c,d)四个编号,四 ...

  4. HDU 4630 No Pain No Game(2013多校3 1010题 离线处理+树状数组求最值)

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. HDU 5618:Jam's problem again(CDQ分治+树状数组处理三维偏序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:…… 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. #includ ...

  6. HDU 4746 莫比乌斯反演+离线查询+树状数组

    题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...

  7. HDU 4918 Query on the subtree(动态点分治+树状数组)

    题意 给定一棵 \(n\) 个节点的树,每个节点有点权.完成 \(q\) 个操作--操作分两种:修改点 \(x\) 的点权.查询与 \(x\) 距离小于等于 \(d\) 的权值总和. \(1 \leq ...

  8. HDU 5618 Jam's problem again (cdq分治+BIT 或 树状数组套Treap)

    题意:给n个点,求每一个点的满足 x y z 都小于等于它的其他点的个数. 析:三维的,第一维直接排序就好按下标来,第二维按值来,第三维用数状数组维即可. 代码如下: cdq 分治: #pragma ...

  9. NOIP 2013 洛谷P1966 火柴排队 (树状数组求逆序对)

    对于a[],b[]两个数组,我们应选取其中一个为基准,再运用树状数组求逆序对的方法就行了. 大佬博客:https://www.cnblogs.com/luckyblock/p/11482130.htm ...

随机推荐

  1. MSCL超级工具类(C#),开发人员必备,开发利器

    MSCL超强工具类库 是基于C#开发的超强工具类集合,涵盖了日常B/S或C/S开发的诸多方面,包含上百个常用封装类(数据库操作类全面支持Mysql.Access.Oracle.Sqlserver.Sq ...

  2. 【Educationcal Codeforces Round 21】

    这场edu我原本以为能清真一点…… 后来发现不仅是七题 还有各种奇奇怪怪的骚操作…… A. 随便枚举 #include<bits/stdc++.h> using namespace std ...

  3. 搭建selenium+python自动化环境

    1.安装python,下载地址:http://python.org---安装版本3.5.1 ps:自带setuptools和pip工具 2.然后,用pip安装开发Web App需要的第三方库:异步框架 ...

  4. English——Unit 1

    meditate  v.沉思,冥想:考虑,谋划 medtiation   n.沉思,冥想:深思熟虑 medium elaborate   adj.精心制作的,详尽的,复杂的:v.精心制作:详述(计划, ...

  5. input标签获取焦点时文本框内提示信息清空背景颜色发生变化

    <input type="text" id="username" onfocus="myFocus(this,'#f4eaf1')" ...

  6. lnmp的安装--mysql

    1.前期准备 创建组:groupadd mysql 创建用户:useradd -r -g mysql mysql 创建mysql文件夹于数据存放文件夹data mkdir -p /usr/www/my ...

  7. 一台服务器支持多少TCP并发链接

    误区一 1.文件句柄---文件描述符 每开一个链接,都要消耗一个文件套接字,当文件描述符用完,系统会返回can't  open so many files 这时你需要明白操作系统对可以打开的最大文件数 ...

  8. hdu 1506(好题+DP或者RMQ)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  9. "GrabCut" - Interactive Foreground Extraction using Iter

    转载自:http://blog.csdn.net/zouxy09/article/details/8534954 有源代码的博客:http://www.cnblogs.com/xrwang/archi ...

  10. Docker for Windows 里的Shared Drives 设置不生效

    原文地址:传送门 问题描述:Docker中的settings里的Shared Drives 选择对应盘符后,点击Apply后无法生效,没办法选择对应盘符进行分享. 解决办法:win+R ,键入gped ...