Group(区间查询 莫队)hdu 4638
Group
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3056 Accepted Submission(s): 1530
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
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3056 Accepted Submission(s): 1530
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
https://www.cnblogs.com/Paul-Guderian/p/6933799.html
题意:在每个查询之后输出有几个连续的序列。
#include<stdio.h> #include<algorithm> #include<iostream> #include<math.h> #include<cstring> #define go(i,a,b) for(int i=a;i<=b;i++) #define mem(a,b) memset(a,b,sizeof(a)) #define ll long long using namespace std; const int N=100005; int x[N]; int vis[N]; struct Mo{int l,r,ID;}q[N]; //ll GCD(ll a,ll b){while(b^=a^=b^=a%=b);return a;} int n,m,col[N],unit,Be[N];int ans; bool cmp(Mo a,Mo b){return Be[a.l]==Be[b.l]?a.r<b.r:a.l<b.l;} int main() { int t; scanf("%d",&t); while(t--){mem(vis,0);mem(col,0);mem(q,0);mem(Be,0); scanf("%d%d",&n,&m);unit=sqrt(n); for(int i=1;i<=n;i++){scanf("%d",&col[i]);Be[i]=i/unit+1;} for(int i=1;i<=m;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].ID=i;} sort(q+1,q+m+1,cmp); int l=1,r=0;ans=0; for(int i=1;i<=m;i++) { while(l>q[i].l){ if(vis[col[l-1]+1]==1&&vis[col[l-1]-1]==1) ans--; if(vis[col[l-1]+1]==0&&vis[col[l-1]-1]==0) ans++; vis[col[l-1]]=1; l--;} while(r<q[i].r){ if(vis[col[r+1]+1]==1&&vis[col[r+1]-1]==1) ans--; if(vis[col[r+1]+1]==0&&vis[col[r+1]-1]==0) ans++; vis[col[r+1]]=1; r++;} while(r>q[i].r){ if(vis[col[r]+1]==1&&vis[col[r]-1]==1) ans++; if(vis[col[r]+1]==0&&vis[col[r]-1]==0) ans--; vis[col[r]]=0; r--;} while(l<q[i].l){ if(vis[col[l]+1]==0&&vis[col[l]-1]==0) ans--; if(vis[col[l]+1]==1&&vis[col[l]-1]==1) ans++; vis[col[l]]=0; l++; } x[q[i].ID]=ans; } for(int i=1;i<=m;i++)printf("%d\n",x[i]); } return 0; }