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. Python简单的制作图片验证码

    -人人可以学Python--这里示范的验证码都是简单的,你也可以把字符扭曲 人人可以学Python.png Python第三方库无比强大,PIL 是python的一个d第三方图片处理模块,我们也可以使 ...

  2. css3属性书写的时候带的一些前缀的意思

    使用css3属性时,大部分都要带这些识别前缀,早期点的浏览器才能识别.现在最新版的浏览器基本都支持css3 基本都不用写前缀 ,写前缀是为了向前兼容老版本的浏览器而已. -ms-transform: ...

  3. 堆--LogN的数据结构

    我们这里的堆是指用来表示元素集合的一种数据结构 一个二叉树是一个堆是由堆的两个性质决定的(以小根堆为例) 1:任何节点的值都小于或等于其子节点的值 2:该二叉树最多在两层上具有叶节点,其中最底层的叶节 ...

  4. Java单线程多实例和多线程多实例

    最近写了一个程序,是采用多线程往redis里面写入数据,想统计一下一共写了多少条数据,于是用了一个static的全局变量count来累加,这块代码抽象出来就是这样的: public class Mul ...

  5. 提高C#编程水平的50个要诀

    一篇旧时的文章,看后觉得还可以,特别贴出来. 提高C#编程水平的50个要点: .总是用属性 (Property) 来代替可访问的数据成员 .在 readonly 和 const 之间,优先使用 rea ...

  6. hdu 1806(线段树区间合并)

    Frequent values Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. SQL Server 用链接服务器 同步SqlServer与MySQL

    =======================================================================================SQL SERVER链接S ...

  8. 【JBPM4】流程部署

    示例代码: ProcessEngine processEngine = Configuration.getProcessEngine(); RepositoryService repositorySe ...

  9. Codeforces 1099 B. Squares and Segments-思维(Codeforces Round #530 (Div. 2))

    B. Squares and Segments time limit per test 1 second memory limit per test 256 megabytes input stand ...

  10. 转:深入了解Windows句柄

    深入了解Windows句柄到底是什么 转:http://blog.csdn.net/wenzhou1219/article/details/17659485 总是有新入门的Windows程序员问我Wi ...