ACM: NBUT 1646 Internet of Lights and Switches - 二进制+map+vector
Time Limit:5000MS Memory Limit:65535KB 64bit IO Format:
You are a fan of "Internet of Things"(IoT, 物联网), so you build a nice Internet of Lights and Switches in your huge mansion. Formally, there are n lights and m switches, each switch controls one or more lights, i.e. pressing that switch flips the status of those lights (on->off, off->on).
Initially, all the lights are on. Your task is to count the number of ways to turn off all the lights by pressing some consecutive switches. Each switch should not be pressed more than once. There is only one restriction: the number of switches you pressed should be between a and b (inclusive).
Input
There will be at most 20 test cases. Each test case begins with a line containing four integers n, m, a, b (2<=n<=50, 1<=a<=b<=m<=300000). Each of the following m lines contains a 01 string of length n. The i-th character is 1 if and only if that switch controls the i-th light. The size of the whole input file does not exceed 8MB.
Output
For each test case, print the case number, and the number of ways to turn off all the lights.
Sample Input
2 4 1 4 01 10 11 00 2 4 3 3 01 10 11 00 6 3 1 3 101001 010110 101001
Sample Output
Case 1: 3 Case 2: 0 Case 3: 2
#include"algorithm" #include"iostream" #include"cstring" #include"cstdlib" #include"cstdio" #include"string" #include"vector" #include"queue" #include"cmath" #include"map" using namespace std; typedef long long LL ; #define memset(x,y) memset(x,y,sizeof(x)) #define memcpy(x,y) memcpy(x,y,sizeof(x)) #define FK(x) cout<<"["<<x<<"]\n" #define bigfor(T) for(int qq=1;qq<= T ;qq++) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 LL light_now[300006]; int main() { char s[123]; int n,m,a,b; int qq=1; LL light_change; while(~scanf("%d%d%d%d",&n,&m,&a,&b)) { map< LL , vector<int> > mp; // LL 保存开关的状态,vector 保存 能够按到这个状态的所有起点。 mp.clear(); light_now[0]=0; mp[0].push_back(0); for(int i=1; i<=m; i++) { scanf("%s",s); // FK(s); light_change=0; for(int j=0; j<n; j++) { if(s[j]=='1')light_change+=(1ll<<j); //按照50位数来保存灯的状态 } // FK(light_change); light_now[i]=light_change^light_now[i-1]; //取反,现在开关的状态。 // FK(light_now[i]); mp[light_now[i]].push_back(i); //把每一个需要被按下的键,能够按到这个状态的所有起点保存。 } int ans=0; for(int i=1; i<=m; i++) { LL t=((~light_now[i])&((1ll<<n)-1)); //需要被按下的键 if(!mp[t].empty()) { // int x=mp[t].front(); // mp[t].pop(); // if(x>=a&&x<=b)ans++; int l=lower_bound(mp[t].begin(),mp[t].end(),i-b)-mp[t].begin(); //查找下边界 int r=upper_bound(mp[t].begin(),mp[t].end(),i-a)-mp[t].begin(); //查找上边界 ans+=r-l; //记录所有的可能性。 } } printf("Case %d: ",qq++); printf("%d\n",ans); } return 0; } /*/ 2 4 1 4 01 10 11 00 2 4 3 3 01 10 11 00 6 3 1 3 101001 010110 101001 /*/