HDU 4638 莫隊算法
阿新 • • 發佈:2017-06-19
total n) desc align struct hose nbsp reat some Now we chose an interval of men and want to know there should be how many groups so the value of interval is max.
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].
Source
2013 Multi-University Training Contest 4
題意:t組數據 給你一個長度為n的序列 m個詢問[l,r] 問 l到r 的值可以組成多少個連續的段
題解:例如序列3 1 2 5 4
查詢 [1,5] 值為{1,2,3,4,5} 只有一個連續的段
查詢 [2,4] 值為{1,2} {5} 有兩個連續的段
莫隊處理;
Group
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2483 Accepted Submission(s): 1272
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.
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
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<map> 7 #include<queue> 8 #include<stack> 9 #include<vector> 10 #include<set> 11 #define ll __int64 12 using namespace std; 13 int n,m; 14 struct node 15 { 16 int l,r,id; 17 } N[100005]; 18 int p[100005]; 19 int block; 20 int a[100005]; 21 int x[100005]; 22 int mp[100005]; 23 ll ans=0; 24 ll re[100005]; 25 int cmp(struct node aa,struct node bb) 26 { 27 if(p[aa.l]==p[bb.l]) 28 return aa.r<bb.r; 29 else 30 return p[aa.l]<p[bb.l]; 31 } 32 void update(int w,int h) 33 { 34 if(h==1) 35 { 36 mp[a[w]]=1; 37 if(mp[a[w]-1]==0&&mp[a[w]+1]==0) 38 ans++; 39 if(mp[a[w]-1]==1&&mp[a[w]+1]==1) 40 ans--; 41 } 42 else 43 { 44 mp[a[w]]=0; 45 if(mp[a[w]-1]==1&&mp[a[w]+1]==1) 46 ans++; 47 if(mp[a[w]-1]==0&&mp[a[w]+1]==0) 48 ans--; 49 } 50 } 51 int t; 52 int main() 53 { 54 scanf("%d",&t); 55 for(int o=1;o<=t;o++) 56 { 57 for(int i=0;i<100004;i++) 58 mp[i]=0; 59 scanf("%d %d",&n,&m); 60 for(int i=1; i<=n; i++) 61 scanf("%d",&a[i]); 62 for(int i=1; i<=m; i++) 63 { 64 scanf("%d %d",&N[i].l,&N[i].r); 65 N[i].id=i; 66 } 67 block=(int)sqrt((double)n); 68 for(int i=1; i<=n; i++) 69 p[i]=(i-1)/block+1; 70 sort(N+1,N+1+m,cmp); 71 ans=0; 72 for(int i=1,l=1,r=0; i<=m; i++) 73 { 74 for(; r<N[i].r; r++) update(r+1,1); 75 for(; l>N[i].l; l--) update(l-1,1); 76 for(; r>N[i].r; r--) update(r,-1); 77 for(; l<N[i].l; l++) update(l,-1); 78 re[N[i].id]=ans; 79 } 80 for(int i=1; i<=m; i++) 81 printf("%I64d\n",re[i]); 82 } 83 return 0; 84 }
HDU 4638 莫隊算法