# HDU4638:Group(线段树离线处理)

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

#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;
}

```

## 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 triple (i, j, k) (1 ≤ i ≤ j ≤ n). For each k-query (i, j, k), you have to return the number of elements greater than k in the subsequence ai, ai+1, ...,

## 51nod 1463 找朋友（线段树+离线处理）

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1463 题意: 思路: 好题! 先对所有查询进行离线处理,按照右区间排序,因为k一共最多只有10个,所有在该区间内的B数组,每次枚举K值,通过这样的方式来得到另外一个B值.但是这样得到的B值它在B数组中的位置必须在当前数的左边.如下图:(j为当前数在B数组中的位置,pos为计算得到的另一个B值在数组中的位置) 这两个数的和记录在pos中,这里pos的位置必须在j的左边,假

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

1117 - RE:从零开始的异世界生活 Time Limit:1s Memory Limit:256MByte Submissions:438Solved:68 DESCRIPTION 486到了异世界,看到了一群可爱的妹子比如蕾姆啊,艾米莉亚啊,拉姆啊,白鲸啊,怠惰啊等等!有一天膜女告诉486说她的能力可能不能再用了,因为膜女在思考一个数据结构题,没心情管486了.486说我来帮你做,膜女说你很棒棒哦! 给一个集合,最开始为空(不是数学上的集合)五个操作: 1.插入x2.把小于x的数变成x3