2015湖南ACM省赛I题 Internet of Lights and Switches(思维)
https://ac.2333.moe/Problem/view.xhtml?id=1646
本来想用主席树来着,一看内存直接劝退。
貌似只能用vector或map搞一搞了。
1 #define bug(x) cout<<#x<<" is "<<x<<endl 2 #include<iostream> 3 #include<algorithm> 4 #include<iterator> 5 #include<vector> 6 #include<set> 7 #define iter ::iterator 8 using namespace std; 9 #define ll long long 10 const int N=3e5+5; 11 int n,m,L,R; 12 ll a[N],b[N],c[N]; 13 char s[100]; 14 ll cal(){ 15 ll h=1; 16 ll res=0; 17 for(int i=n;i>=1;i--){ 18 if(s[i]=='1'){ 19 res+=h; 20 } 21 h<<=1; 22 } 23 return res; 24 } 25 vector<int>v[N]; 26 int main(){ 27 int kase=0; 28 ll z=1; 29 while(scanf("%d%d%d%d",&n,&m,&L,&R)!=EOF){ 30 ll k=(z<<n)-1; 31 for(int i=1;i<=m;i++){ 32 scanf("%s",s+1); 33 a[i]=cal(); 34 c[i]=a[i]^c[i-1]; 35 b[i]=c[i]; 36 } 37 sort(b+1,b+1+m); 38 int nb=unique(b+1,b+1+m)-b-1; 39 for(int i=1;i<=m;i++){ 40 a[i]=lower_bound(b+1,b+1+nb,c[i])-b; 41 v[a[i]].push_back(i); 42 } 43 for(int i=1;i<=nb;i++){ 44 sort(v[i].begin(),v[i].end()); 45 } 46 ll ans=0; 47 for(int i=0;i<=m;i++){ 48 if(i+L>m)break; 49 ll x=c[i]; 50 x^=k; 51 int id=lower_bound(b+1,b+1+nb,x)-b; 52 if(id>nb)continue; 53 if(b[id]!=x)continue; 54 int res=upper_bound(v[id].begin(),v[id].end(),i+R)-lower_bound(v[id].begin(),v[id].end(),i+L); 55 ans+=res; 56 } 57 printf("Case %d: %lld\n",++kase,ans); 58 for(int i=1;i<=nb;i++)v[i].clear(); 59 } 60 } 61 /* 62 2 4 1 4 63 01 64 10 65 11 66 00 67 2 4 3 3 68 01 69 10 70 11 71 00 72 6 3 1 3 73 101001 74 010110 75 101001 76 */