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. CDH5.7Hadoop集群搭建(离线版)

    用了一周多的时间终于把CDH版Hadoop部署在了测试环境(部分组件未安装成功),本文将就这个部署过程做个总结. 一.Hadoop版本选择. Hadoop大致可分为Apache Hadoop和第三方发 ...

  3. HDFS你一定要知道,要考的

    你肯定听过Hadoop,对就是那头奔跑的小象. Hadoop作为大数据时代代表性的解决方案被大家所熟知,它主要包含两部分内容: HDFS分布式文件存储 MapReduce分布式计算框架 前面我们分析存 ...

  4. C#学习-程序集和反射

    准备项目 1.新建一个空的解决方案MyProj.sln 2.在该解决方案下,建一个控制台项目P01.csproj 3.在该项目下,自己新建一个类MyFirstClass.cs 查看解决方案MyProj ...

  5. sql 列集合转换成逗号分隔的字符类型

    CREATE function [dbo].[getGroupPath](@groupId int) returns nvarchar(2000) as begin declare @path nva ...

  6. 【java基础】java中重载与重写的区别

    重载(Overloading) (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型.重载Overloading是一个类中多态性的一种表现. ( ...

  7. Android开发中常用的ListView列表的优化方式ViewHolder

    在Android开发中难免会遇到大量的数据加载到ListView中进行显示, 然后其中最重要的数据传递桥梁Adapter适配器是常用的,随着市场的需 求变化ListView'条目中的内容是越来越多这就 ...

  8. android接收mjpg-streamer软件视频流

    [代码]主要实现代码 package cn.dong.mjpeg; import java.io.InputStream; import java.net.HttpURLConnection; imp ...

  9. java_遍历文件目录

    package util; import java.io.File; import java.io.IOException; //列出File的一些常用操作 public class util { / ...

  10. swift class protocol-限定协议只能由类实现

    protocol GameMode:class “You can limit protocol adoption to class types (and not structures or enume ...