hdu 4368 樹狀陣列 離線維護
http://acm.hdu.edu.cn/showproblem.php?pid=4638
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].
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
/**
hdu 4368 樹狀陣列
題目大意:給定1~n的一個排列,每次查詢一個區間(l,r),將所求區間內的數進行分組,每一組內的數的值必須是連著的,該組的價值為元素個數的平方和
問在區間價值最大的情況下,要分成幾組
解題思路:用樹狀陣列離線維護,把詢問按照右區間遞增排序,然後從1~n維護,維護到i時注意找尋前i個是不是有a[i]-1,和a[i]+1,有的話直接去掉,因為
這兩個數必定要和a[i]分到一組,留一個就好了
*/
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
using namespace std;
const int maxn=100100;
int n,m,a[maxn],p[maxn],ans[maxn];
struct note
{
int l,r,id;
bool operator < (const note &other)const
{
return r<other.r;
}
}que[maxn];
int c[maxn];
int lowbit(int x)
{
return x&(-x);
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<=n)
{
c[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
p[a[i]]=i;
}
for(int i=0;i<m;i++)
{
scanf("%d%d",&que[i].l,&que[i].r);
que[i].id=i;
}
sort(que,que+m);
memset(c,0,sizeof(c));
int j=0;
for(int i=1;i<=n;i++)
{
add(i,1);
if(a[i]>1&&p[a[i]-1]<i)add(p[a[i]-1],-1);
if(a[i]<n&&p[a[i]+1]<i)add(p[a[i]+1],-1);
while(j<m&&que[j].r==i)
{
ans[que[j].id]=sum(que[j].r)-sum(que[j].l-1);
j++;
}
}
for(int i=0;i<m;i++)
{
printf("%d\n",ans[i]);
}
}
return 0;
}
相關文章
- hdu4325 樹狀陣列+離散化陣列
- hdu 4836 The Query on the Tree(線段樹or樹狀陣列)陣列
- hdu 3874 樹狀陣列陣列
- HDU 5862 Counting Intersections(樹狀陣列+掃描線+離散化)陣列
- HDU 1556 Color the ball(線段樹|樹狀陣列)陣列
- hdu 5147 樹狀陣列陣列
- HDU 6274 Master of Sequence(思維+樹狀陣列+二分)AST陣列
- 二維樹狀陣列陣列
- SPOJ DQUERY (離線數狀陣列||線上主席樹)陣列
- HDU4991 Ordered Subsequence (dp+樹狀陣列+離散化)陣列
- HDU 1166 敵兵佈陣 (樹狀陣列)陣列
- HDU 1166 敵兵佈陣(樹狀陣列)陣列
- HDU 2689 Sort it【樹狀陣列求逆序對】陣列
- HDU 1541 & POJ 2352 Stars (樹狀陣列)陣列
- HDU2689 Sort it (樹狀陣列求逆序數)陣列
- HDU4843Wow! Such Sequence!(樹狀陣列寫法)陣列
- 樹狀陣列陣列
- 線段樹+差分——【模板】樹狀陣列2陣列
- 【二維樹狀陣列】poj 2155 Matrix陣列
- 二維樹狀陣列-poj2155陣列
- poj 1195 二維樹狀陣列陣列
- HDU5419Victor and Toys(樹狀陣列+數學期望)陣列
- HDU 1394 Minimum Inversion Number (樹狀陣列求逆序數)陣列
- AC dreamoj 1011 樹狀陣列+hash維護字串的字首和陣列字串
- 解析樹狀陣列陣列
- 線段樹維護區間等差數列
- 二維樹狀陣列--poj1195陣列
- 維護樹狀資料
- HDU 1556【區間更新+單點查詢 樹狀陣列】陣列
- [php]運用變數引用實現一維陣列轉多維樹狀陣列PHP變數陣列
- hdu4288 離線處理線段樹
- POJ 1195 Mobile phones(二維樹狀陣列)陣列
- HDU5147 Sequence II(樹狀陣列+字首和+字尾和)陣列
- 樹狀陣列詳解陣列
- 樹狀陣列基礎陣列
- poj 2481 樹狀陣列陣列
- 【樹狀陣列 單點修改,區間求值】hdu 1166 敵兵佈陣陣列
- light oj 1080 線段樹和樹狀陣列陣列