Codeforces gym102152 K.Subarrays OR

传送:http://codeforces.com/gym/102152/problem/K

题意:给定$n(nle10^5)$个数$a_i(a_ile10^9)$,对于任一个子数组中的数进行或操作,问所有的值有多少种?

分析:

假设现在想要求解区间$[1,i]$内的答案,那么需要先处理出$[1,i-1],[2,i-1],[3,i-1]......[i-1,i-1]$。然后将这些答案暴力与$a_i$取$|$。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 # define ll long long
 4 const int maxn = 2e5+100;
 5 int a[maxn];
 6 int main(){
 7     int T;scanf("%d",&T);
 8     while(T--){
 9         int n;scanf("%d",&n);
10         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
11         set<int>ans;set<int>st;set<int>tmp;
12         for(int j=1;j<=n;j++){
13             tmp.clear();tmp.insert(a[j]);
14             for(auto i : st) tmp.insert(i|a[j]);
15             st=tmp;
16             for(auto i: tmp) ans.insert(i);
17         }
18         printf("%d
",ans.size());
19     }
20     return 0;
21 }
原文地址:https://www.cnblogs.com/changer-qyz/p/10673767.html