Group
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1323 Accepted Submission(s): 703
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
Recommend
zhuyuanchen520 | We have carefully selected several similar problems for you: 4996 4995 4994 4993 4992
题意:
给你一个长度为n(1<=n<=100000)的数列。数列中的值互不相同且1<=ai<=n。对于一个给定的区间。[L,R]ans就为
下标[L,R]的数。中取值连续的块的块数。比如。1,3,2,6,8,7块数就为2。1,2,3一块。6,7,8为一块。
思路:
由于知道[L,R]的答案可以得到附近区间的答案。就想用莫队水一发。但是维护区间内有多少块是用线段树维护的。结果不言而喻。。其实不用线段树的。直接开个bool数组。如果i出现了就把vis[i]置为1.判断块数就简单了。加入一个数的时候如果vis[i-1]和vis[i+1]都为1的话那么块数ans就要-1.如果只有一个为1的话ans不变。如果全为0的话ans+1.
删除一个数类似处理就行了。
详细见代码:
#include<algorithm> #include<iostream> #include<string.h> #include<stdio.h> #include<math.h> using namespace std; const int INF=0x3f3f3f3f; const int maxn=100010; typedef long long ll; struct qnode { int l,r,idx; } qu[maxn]; int id[maxn],pos[maxn],ans[maxn],bks; bool vis[maxn]; bool cmp(qnode a,qnode b) { if(pos[a.l]==pos[b.l]) return a.r<b.r; return pos[a.l]<pos[b.l]; } void update(int x,bool d) { vis[x]=d; if(d) bks+=1-vis[x-1]-vis[x+1]; else bks+=vis[x-1]+vis[x+1]-1; } int main() { int t,n,m,i,j,bk,pl,pr,pp; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(vis,0,sizeof vis); bk=ceil(sqrt(1.0*n)); for(i=1;i<=n;i++) { scanf("%d",&id[i]); pos[i]=(i-1)/bk; } for(i=0;i<m;i++) { scanf("%d%d",&qu[i].l,&qu[i].r); qu[i].idx=i; } sort(qu,qu+m,cmp); pl=1,pr=0,bks=0; for(i=0;i<m;i++) { pp=qu[i].idx; if(pr<qu[i].r) { for(j=pr+1;j<=qu[i].r;j++) update(id[j],1); } else { for(j=pr;j>qu[i].r;j--) update(id[j],0); } if(pl<qu[i].l) { for(j=pl;j<qu[i].l;j++) update(id[j],0); } else { for(j=pl-1;j>=qu[i].l;j--) update(id[j],1); } pl=qu[i].l,pr=qu[i].r; ans[pp]=bks; } for(i=0;i<m;i++) printf("%d\n",ans[i]); } return 0; }
开始的线段树代码:
#include<algorithm> #include<iostream> #include<string.h> #include<stdio.h> #include<math.h> using namespace std; const int INF=0x3f3f3f3f; const int maxn=100010; typedef long long ll; #define lson L,mid,ls #define rson mid+1,R,rs struct qnode { int l,r,idx; } qu[maxn]; int id[maxn],bks[maxn<<1],ml[maxn<<1],mr[maxn<<1],pos[maxn],ans[maxn]; bool cmp(qnode a,qnode b) { if(pos[a.l]==pos[b.l]) return a.r<b.r; return pos[a.l]<pos[b.l]; } void build(int L,int R,int rt) { bks[rt]=ml[rt]=mr[rt]=0; if(L==R) return; int ls=rt<<1,rs=ls|1,mid=(L+R)>>1; build(lson); build(rson); } void update(int L,int R,int rt,int p,int d) { if(L==R) { bks[rt]=ml[rt]=mr[rt]=d; return; } int ls=rt<<1,rs=ls|1,mid=(L+R)>>1; if(p<=mid) update(lson,p,d); else update(rson,p,d); bks[rt]=bks[ls]+bks[rs]; ml[rt]=ml[ls]; mr[rt]=mr[rs]; if(mr[ls]&&ml[rs]) bks[rt]--; } int qubks(int L,int R,int rt,int l,int r) { if(l<=L&&R<=r) return bks[rt]; int ls=rt<<1,rs=ls|1,mid=(L+R)>>1,tp=0,ct=0; if(l<=mid) tp+=qubks(lson,l,r),ct++; if(r>mid) tp+=qubks(rson,l,r),ct++; if(ct==2&&mr[ls]&&ml[rs]) tp--; return tp; } int main() { int t,n,m,i,j,bk,pl,pr,pp; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); build(1,n,1); bk=ceil(sqrt(1.0*n)); for(i=1;i<=n;i++) { scanf("%d",&id[i]); pos[i]=(i-1)/bk; } for(i=0;i<m;i++) { scanf("%d%d",&qu[i].l,&qu[i].r); qu[i].idx=i; } sort(qu,qu+m,cmp); pl=1,pr=0; for(i=0;i<m;i++) { pp=qu[i].idx; if(pr<qu[i].r) { for(j=pr+1;j<=qu[i].r;j++) update(1,n,1,id[j],1); } else { for(j=pr;j>qu[i].r;j--) update(1,n,1,id[j],0); } if(pl<qu[i].l) { for(j=pl;j<qu[i].l;j++) update(1,n,1,id[j],0); } else { for(j=pl-1;j>=qu[i].l;j--) update(1,n,1,id[j],1); } pl=qu[i].l,pr=qu[i].r; ans[pp]=qubks(1,n,1,1,n); } for(i=0;i<m;i++) printf("%d\n",ans[i]); } return 0; }
离线线段树的方法还没写。写了再加上。。。,