Hdu4638:group (line segment tree offline processing)

Source: Internet
Author: User

Problem Descriptionthere was n men, every man had an ID (1..N). The their ID is unique. Whose ID is I and i-1 am friends, whose ID is I and i+1 is friends. These n men stand on line. Now we select a 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 a interval of men and want to know there should being how many groups so the value of interval is max.
Inputfirst line was T indicate the case number.
For each case first line is N, m (1<=n, m<=100000) indicate there be n men and M query.
Then a line has n number indicate the ID of the men from left to right.
Next m Line have a number l,r (1<=l<=r<=n), mean we want to know the answer of [l,r].

Outputfor every query output a number indicate there should be what many group so and the sum of value is max.
Sample Input
15 23 1 2 5 41 52 4

Sample Output
12 Test instructions: Give an array, each query l,r, see the interval can be divided into several consecutive series of ideas: Offline processing all queries
#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 rank1const 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&LT;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", &AMP;S[I].L,&AMP;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;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu4638:group (line segment tree offline processing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.