Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main {
- static long SumofAndOfSubarrays(int[] arr, int n){
- long sum = 0;
- for(int i=0; i<18; i++){ //since 1e5 don;t cross 18 bits
- int contiguousOnes = 0;
- int currContiguousOnes = 0;
- for(int j=0; j<n; j++){
- if((arr[j]&(1<<i)) != 0) {
- currContiguousOnes++;
- contiguousOnes+=currContiguousOnes;
- } else {
- currContiguousOnes= 0;
- }
- }
- long SubarraysWithIthPlaceAsOne = contiguousOnes;
- sum += SubarraysWithIthPlaceAsOne*(1 << i);
- }
- return sum;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int Q = sc.nextInt();
- while(Q-- > 0){
- int n = sc.nextInt();
- int[] arr = new int[n];
- for(int i=0; i<n; i++) arr[i] = sc.nextInt();
- System.out.println(SumofAndOfSubarrays(arr, n));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment