1. 程式人生 > >P3709 大爺的字符串題(50分)

P3709 大爺的字符串題(50分)

etc 數據 () void 出了 ace == con 說明

題目背景

在那遙遠的西南有一所學校

/*被和諧部分*/

然後去參加該省省選虐場

然後某蒟蒻不會做,所以也出了一個字符串題:

題目描述

給你一個字符串a,每次詢問一段區間的貢獻

貢獻定義:

每次從這個區間中隨機拿出一個字符x,然後把x從這個區間中刪除,你要維護一個集合S

如果S為空,你rp減1

如果S中有一個元素不小於x,則你rp減1,清空S

之後將x插入S

由於你是大爺,平時做過的題考試都會考到,所以每次詢問你搞完這段區間的字符之後最多還有多少rp?rp初始為0

詢問之間不互相影響~

輸入輸出格式

輸入格式:

第一行兩個數n,m,表示字符串長度與詢問次數

之後一行n個數,表示字符串

由於你是大爺,所以字符集1e9

之後m行每行兩個數,表示詢問的左右區間

輸出格式:

m行,每行一個數表示答案

輸入輸出樣例

輸入樣例#1:
3 3
3 3 3
3 3
3 3
3 3
輸出樣例#1:
-1
-1
-1

說明

前4個點1s,後面的點4s

對於10%的數據,是樣例

對於另外10%的數據,n,m <= 100

對於另外10%的數據,n,m <= 1000

對於另外10%的數據,n,m <= 10000

對於另外10%的數據,n,m <= 100000

對於100%的數據,n,m <= 200000

保證數據向某省省選day1T2一樣sb,大家盡情用暴力水過題吧!

沒事,你只要在一個好學校,就算這題只能拿到10分,也可以進隊了

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<cstdlib>
  6 #include<algorithm>
  7 #include<vector>
  8 #define hh 10
  9 using namespace std;
 10 const int MAXN=2000001
; 11 void read(int & n) 12 { 13 char c=+;int x=0;bool flag=0; 14 while(c<0||c>9) 15 {c=getchar();if(c==-)flag=1;} 16 while(c>=0&&c<=9) 17 {x=x*10+(c-48);c=getchar();} 18 flag==1?n=-x:n=x; 19 } 20 int n,m; 21 int pos[MAXN]; 22 int base; 23 int rp=0; 24 struct node 25 { 26 int l,r,id; 27 }q[MAXN]; 28 struct dr 29 { 30 int a,p; 31 }a[MAXN]; 32 int comp(const node & a,const node &b) 33 { 34 if(pos[a.l]==pos[b.l]) 35 return a.r<b.r; 36 else 37 return pos[a.l]<pos[b.l]; 38 } 39 int cop(const dr &a,const dr &b) 40 { 41 return (a.a<b.a)||(a.a==b.a&&a.p<b.p); 42 } 43 int happen[MAXN];// 記錄i出現了多少次 44 int cnt[MAXN];// 記錄出現次數為j的有多少 45 int out[MAXN]; 46 void dele(int p) 47 { 48 if(rp==happen[p]&&cnt[happen[p]+hh]==1) 49 rp--; 50 cnt[happen[p]+hh]--; 51 cnt[happen[p]+hh-1]++; 52 happen[p]--; 53 } 54 void add(int p) 55 { 56 if(rp==happen[p]) 57 rp++; 58 cnt[happen[p]+hh]--; 59 cnt[happen[p]+hh+1]++; 60 happen[p]++; 61 } 62 int ls[MAXN]; 63 void modui() 64 { 65 int ll=1,rr=0; 66 for(int i=1;i<=m;i++) 67 { 68 for(;ll<q[i].l;ll++) 69 dele(ls[ll]); 70 for(;ll>q[i].l;ll--) 71 add(ls[ll-1]); 72 for(;rr>q[i].r;rr--) 73 dele(ls[rr]); 74 for(;rr<q[i].r;rr++) 75 add(ls[rr+1]); 76 out[q[i].id]=-rp; 77 } 78 for(int i=1;i<=m;i++) 79 printf("%d\n",out[i]); 80 } 81 int main() 82 { 83 read(n);read(m); 84 base=sqrt(n); 85 for(int i=1;i<=n;i++) 86 read(a[i].a),a[i].p=i; 87 sort(a+1,a+n+1,cop); 88 int j; 89 for(int i=1,j=0;i<=n;i++) 90 { 91 if (i==1||a[i].a!=a[i-1].a) j++; 92 ls[a[i].p]=j; 93 } 94 //for(int i=1;i<=n;i++) 95 // pos[i]=(i-1)/base+1; 96 int sqt=0; 97 for ( sqt=1; sqt*sqt<=n; sqt++); 98 99 for (int i=1; i<=n; i++) pos[i]=i/sqt; 100 101 cnt[0]=j; 102 for(int i=1;i<=m;i++) 103 { 104 read(q[i].l); 105 read(q[i].r); 106 q[i].id=i; 107 } 108 sort(q+1,q+m+1,comp); 109 modui(); 110 return 0; 111 }

P3709 大爺的字符串題(50分)