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
题意:给出一个数组。每次查询l,r。看区间内能分成几个连续的数列
思路:离线处理全部查询
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std; #define ls 2*i
#define rs 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 100005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define rank rank1
const int mod = 10007; struct node
{
int l,r,val;
} a[N*4],s[N]; int num[N],pos[N],vis[N],ans[N];
int n,m; int cmp(node a,node b)
{
return a.r<b.r;
} void build(int l,int r,int i)
{
a[i].l = l;
a[i].r = r;
a[i].val = 0;
if(l==r) return;
int mid = (l+r)/2;
build(l,mid,ls);
build(mid+1,r,rs);
} void updata(int i,int pos,int v)
{
a[i].val+=v;
if(a[i].l==a[i].r) return;
int mid = (a[i].l+a[i].r)/2;
if(pos<=mid) updata(ls,pos,v);
else updata(rs,pos,v);
} int query(int l,int r,int i)
{
if(a[i].l==l&&a[i].r==r)
{
return a[i].val;
}
int mid = (a[i].l+a[i].r)/2;
if(r<=mid) return query(l,r,ls);
if(l>mid) return query(l,r,rs);
return query(l,mid,ls)+query(mid+1,r,rs);
} int main()
{
int t,i,j,k,cnt;
scanf("%d",&t);
while(t--)
{
MEM(vis,0);
scanf("%d%d",&n,&m);
for(i = 1; i<=n; i++)
{
scanf("%d",&num[i]);
pos[num[i]] = i;
}
for(i = 1; i<=m; i++)
{
scanf("%d%d",&s[i].l,&s[i].r);
s[i].val = i;
}
sort(s+1,s+1+m,cmp);
build(1,n,1);
cnt = 1;
for(i = 1; i<=n&&cnt<=m; i++)
{
updata(1,i,1);
vis[num[i]]=1;
if(vis[num[i]-1]) updata(1,pos[num[i]-1],-1);
if(vis[num[i]+1]) updata(1,pos[num[i]+1],-1);
while(s[cnt].r==i&&cnt<=m)
{
ans[s[cnt].val] = query(s[cnt].l,s[cnt].r,1);
cnt++;
}
}
for(i = 1; i<=m; i++)
printf("%d\n",ans[i]);
} return 0;
}



HDU4638:Group(线段树离线处理)的更多相关文章

  1. 线段树+离线 hdu5654 xiaoxin and his watermelon candy

    传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...

  2. 牛客练习赛53 E-老瞎眼pk小鲜肉(思维+线段树+离线)

    前言 听说是线段树离线查询?? 做题做着做着慢慢对离线操作有点感觉了,不过也还没参透,等再做些题目再来讨论离线.在线操作. 这题赛后看代码发现有人用的树状数组,$tql$.当然能用树状数组写的线段树也 ...

  3. HDU 4638-Group(线段树+离线处理)

    题意: 给n个编号,m个查询每个查询l,r,求下标区间[l,r]中能分成标号连续的组数(一组内的标号是连续的) 分析: 我们认为初始,每个标号为一个组(线段树维护区间组数),从左向右扫序列,当前标号, ...

  4. HDU 4630-No Pain No Game(线段树+离线处理)

    题意: 给你n个数的序列a,q个询问,每个询问给l,r,求在下标i在[l,r]的区间任意两个数的最大公约数中的最大值 分析: 有了hdu3333经验,我们从左向右扫序列,如果当前数的约数在前面出现过, ...

  5. HDU 4288 Coder 【线段树+离线处理+离散化】

    题意略. 离线处理,离散化.然后就是简单的线段树了.需要根据mod 5的值来维护.具体看代码了. /* 线段树+离散化+离线处理 */ #include <cstdio> #include ...

  6. SPOJ--K-query (线段树离线) 离线操作解决一些问题

    K-query Given a sequence of n numbers a1, a2, ..., an and a number of k- queries. A k-query is a tri ...

  7. lca 欧拉序+rmq(st) 欧拉序+rmq(线段树) 离线dfs 倍增

    https://www.luogu.org/problemnew/show/P3379 1.欧拉序+rmq(st) /* 在这里,对于一个数,选择最左边的 选择任意一个都可以,[left_index, ...

  8. 51nod 1463 找朋友(线段树+离线处理)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1463 题意: 思路: 好题! 先对所有查询进行离线处理,按照右区间排序, ...

  9. 玲珑oj 1117 线段树+离线+离散化,laz大法

    1117 - RE:从零开始的异世界生活 Time Limit:1s Memory Limit:256MByte Submissions:438Solved:68 DESCRIPTION 486到了异 ...

随机推荐

  1. day02_12/12/2016_bean的实例化之构造器方式

  2. 涨知识---IV

    1.如何减少换页错误? A.进程倾向于占用CPU. B.访问局部性(locality of reference)满足进程要求. C.进程倾向于占用I/O. D.使用基于最短剩余时间(shortest ...

  3. 涨知识 - II

    计算机网络相关 1.在无盘工作站向服务器申请IP地址时,使用的是(     )协议. A.ARP B.RARP C.ICMP D.IGMP 解析: ARP(地址解析协议)是设备通过自己知道的IP地址来 ...

  4. 设置靠近 水平居中的主体内容Div 的 左侧位置固定的Div

    示例效果: 1.主体内容的divMain 水平居中: 2.divLeft 靠近divMain ,位置固定,不随垂直滚动条而动: 相关代码: <html> <head runat=&q ...

  5. 【转】Java 集合系列12之 TreeMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对TreeMap进行学习.我们先对TreeMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用TreeMap.内容包括:第1部分 TreeMap介绍第2部分 TreeMa ...

  6. python 根据数组生成图片

    array = np.asarray(allBigPng, dtype=np.uint8)image = Image.fromarray(array, 'RGBA') image.save(outpu ...

  7. 精美对UI设计界面赏析

    最美的UI设计界面赏析 . 喜欢就关注我吧

  8. 开源业务规则引擎JBoss Drools

    Drools 是什么? 规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策.接受数据输入,解释业务规则,并根据业务规 ...

  9. [Windows Server 2008] SQL Server 2008 数据库还原方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:还原SQL S ...

  10. [Windows Server 2012] IIS自带FTP配置方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:IIS自带FT ...