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. Android 解决ScrollView嵌套RecyclerView导致滑动不流畅的问题

    最近做的项目中遇到了ScrollView嵌套RecyclerView,刚写完功能测试,直接卡出翔了,后来通过网上查找资料和 自己的实践,找出了两种方法解决这个问题. 首先来个最简单的方法: recyc ...

  2. Python3 每次处理一个字符

    """ Python3.4[文本]之每次处理一个字符 """ test_str = "my name is bixiaopeng& ...

  3. Rsync 传输不需要输入密码

    1.背景 1)        一个作为服务器端:VM3(IP: 3.9.8.151) 2)        一个作为客户端:VM2(IP: 3.9.8.157) 3)        服务器端和客户端网络 ...

  4. 详谈java集合框架

    1.为什么使用集合框架 当我们并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象——可以使用Java集合框架 2.Java集合框架包含的内容 接口:(父类)Collection接口下包含Li ...

  5. DECLARE_MESSAGE_MAP( )

    DECLARE_MESSAGE_MAP( ) 说明: 你的程序中的每一个CCmdTarget的派生类都可以提供一个消息映射以处理消息.在你的类声明的末尾使用DECLARE_MESSAGE_MAP宏.然 ...

  6. dotfuscator 如何设置

  7. 关于WEB开发下面DIV层被OCX控件拦住问题

    控件分为有窗口控件与无窗口控件,无窗口控件很好办,如flash控件,可以通过添加wmode属性来解决挡住DIV层这个问题,添加的代码如下: 解决无窗口控件挡住DIV: 1 <param  nam ...

  8. Git学习总结(标签管理)

    在Git中打标签非常简单,首先,切换到需要打标签的分支上: 然后,敲命令git tag <name>就可以打一个新标签: $ git tag v1. 可以用命令git tag查看所有标签: ...

  9. windons共享的一些问题

    有时候访问共享一直说无法打开共享,但是别人确实是开了共享. 其中可能如下: 1.首先确定网络没有问题,win+R输入cmd,ping对方IP地址,保证是网络是通的,如果不通,关闭共享电脑的防火墙. 2 ...

  10. 云计算时代,你为什么一定要学Linux?

    云计算早已不是什么稀奇的概念,它的火爆让Linux运维工程师这个职业越来越重要.在当今各类云平台提供的系统中,Linux系统几乎毫无争议的独占鳌头,市场份额进一步扩张. 这也让Linux运维工程师职位 ...