/*Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1431 Accepted Submission(s): 754 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 Source 2013 Multi-University Training Contest 4 */ #include<stdio.h> #include<stdlib.h> #include<string.h> #define M 100010 struct quer { int l, r, id; } query[M]; int ans[M], adr[M], mark[M], a[M], c[M];/*ans保存答案 adr保存每隔数字的位置 mark用来标记数字是否出现过 a保存输入数据 c是树状数组*/ int sum(int x)//树状数组求和 1~x的和 { int sum = 0; while(x > 0) { sum += c[x]; x -= x&(-x); } return sum; } void update(int i, int e, int n)/*修改树状数组的值*/ { while(i <= n) { c[i] += e; i += i&(-i); } } int cmp(const void*a, const void*b)//按照查询的左端点进行排序 { return (*(struct quer*)a).l - (*(struct quer*)b).l; } int main() { int m, n, T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for( int i = 1; i <= m; i++) { scanf("%d%d", &query[i].l, &query[i].r); query[i].id = i; } memset(mark, 0, sizeof(mark)); memset(c, 0, sizeof(c)); memset(adr, 0, sizeof(adr)); int l1 = 1, l2 = 1; qsort(query+1, m, sizeof(query[0]), cmp);//以左端点从小到大排序 for(int i = 1; i <= m; i++) { while(l2 <= query[i].r)//对1~query【i】.r的块数进行建树 { int e = 1; if( mark[a[l2] - 1] ) e--; if( mark[a[l2] + 1] ) e--; update(l2, e, n); mark[a[l2]] = 1; adr[a[l2]] = l2;//记录位置 l2++; } while(l1 < query[i].l)//去除1~query【i】.l的块数对查询区间的影响 { if( mark[a[l1] - 1] ) update(adr[a[l1] - 1], 1, n); if( mark[a[l1] + 1] ) update(adr[a[l1] + 1], 1, n); update(adr[a[l1]], -1, n); mark[a[l1]] = 0; l1++; } ans[query[i].id] = sum(query[i].r);//保存该查询的答案 } for(int i = 1; i <= m; i++)//按原输入顺序输出答案 printf("%d\n", ans[i]); } return 0; }
题意 :给定一个序列,对于一个数n,n-1,n+1任意两个以上可以组成一个块,前提是必须是相邻的。现在给出一个区间数,求在这个区间内有多少块数。
思路 :首先,将每一个点本身当成是一个块,然后根据输入的顺序,从1~n进行遍历,当第i个位置时,查看a【i】的相邻的两个数的位置在哪,如果a【i】-1和a【i】+1在a【i】的两边,则树状数组不用更新,若是都在左边,则树状数组从当前位置开始-1,若都在右边,说明当前的a【i】为此块的第一个出现的数字,则树状数组从当前位置开始往后+1。这个求出的是区间1~当前位置的块数和,可查询给出的是一个中间区间【l,r】,所以得去除1~l对该区间块数和的影响。
那么 利用反向思维,令i从1~l看a【i】的相邻数字是否出现过,出现过一个就从出现的数字的位置开始更新树状数组往后+1,再令树状数组从a【i】的当前位置往后-1,便能抵消区间左端点之前的数字对区间块数的影响(不懂得话可以自己根据代码推敲一下)。其次离线查询是为了以区间的左端点从小到大开始查询,这样才能使树状数组发挥作用。
关键:必须得想到如何建立块数的树状数组,并且能够想出抵消区间之前的数对区间块数的影响的方法。