Topic meaning:
Link: https://www.nowcoder.com/acm/contest/84/E
Given n numbers a 1 , a 2 , ..., a n .
Define f(l, r) = a l | a l+1 | ... | a r .
Now enumerate (1 <= l <= r <= n) and ask how many different f values there are.
Idea: In the case of determining an l, there are only more than 20 different values from l at most, and it is good to update violently from the back to the front.
1 #include<bits/stdc++.h> 2 #define LL long long 3 #define pii pair<int,int> 4 #define mk make_pair 5 #define fi first 6 #define se second 7 using namespace std; 8 9 const int N=1e5+7; 10 const int M=2e6+7; 11 const int inf=0x3f3f3f3f; 12 const LL INF=0x3f3f3f3f3f3f3f3f; 13 const int mod=1e9+7; 14 15 int n, a[N], ans; 16 bool vis[M]; 17 set<int> st; 18 vector<int> v[N]; 19 int main() { 20 scanf("%d", &n); 21 for(int i = 1; i <= n; i++) 22 scanf("%d", &a[i]); 23 vis[a[n]] = true; 24 v[n].push_back(a[n]); 25 ans++; 26 for(int i = n - 1; i >= 1; i--) { 27 if(!vis[a[i]]) { 28 vis[a[i]] = true; 29 ans++; 30 } 31 st.insert(a[i]); 32 for(int u : v[i + 1]) { 33 if(!vis[a[i] | u]) { 34 vis[a[i] | u] = true; 35 ans++; 36 } 37 st.insert(a[i] | u); 38 } 39 40 for(int u : st) 41 v[i].push_back(u); 42 st.clear(); 43 } 44 printf("%d\n", ans); 45 return 0; 46 } 47 /* 48 */