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. 自动化测试===uiautomator2类似appium

    项目地址:https://github.com/openatx/uiautomator2 http://mp.weixin.qq.com/s/YKxwW-pL603Fll3QIWapVw https: ...

  2. java===java基础学习(2)---运算符,三元操作符,数学函数

    主要介绍运算符,和数学函数以及三元运算符: package testbotoo; public class test1 { public static void main(String[] args) ...

  3. 安全测试===Web 安全渗透方面的学习路线

    作者:向生李链接:https://www.zhihu.com/question/21914899/answer/39344435来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  4. 【模板】解决二分图匹配的强力算法——Hopcroft-Karp算法

    详细解释 参见:http://blog.csdn.net/wall_f/article/details/8248373 简要过程 HK算法可以当成是匈牙利算法的优化版,和dinic算法的思想比较类似. ...

  5. [hadoop][基本原理]zookeeper场景使用

    代码:https://github.com/xufeng79x/ZkClientTest 1. 简介 zookeeper的特性决定他适用到某些场景非常合适,比如典型的应用场景: 1.集群管理(Grou ...

  6. JS页面之间传值

    父页面与子页面之间有多种传值的方式: 第一种,通过window.open的方法打开一个新的页面,在新的页面里面通过window.opener来获取对象,以下为实例 父页面: function open ...

  7. IE中部分版本的浏览器对Select标签设置innerHTML无效的问题

    这样写的代码:document.getElementById('data_list').innerHTML = data;//data是ajax返回的数据 结果发现在ie10的兼容模式下面下拉框没有内 ...

  8. 用eclipse运行项目时怎么设置虚拟机内存大小

    方法一: 打开eclipse,选择Window--Preferences...在对话框左边的树上双击Java,再双击InstalledJREs,在右边选择前面有对勾的JRE,再单击右边的“Edit”按 ...

  9. 《java并发编程实战》读书笔记7--线程池的使用

    第8章 线程池的使用 8.1 在任务与执行策略之间的隐性耦合 虽然Executor框架为制定和修改执行策略都提供了相当大的灵活性,但并非所有的任务都适用所有的执行策略.有些类型的任务需要明确地指明执行 ...

  10. window下线程同步之(Semaphores(信号量))

    HANDLE WINAPI CreateSemaphore( _In_opt_ LPSECURITY_ATTRIBUTES lpSemaphoreAttributes _In_ LONG lIniti ...