cf div2 234 E

时间:2022-03-28 10:48:37
E. Inna and Binary Logic
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Inna is fed up with jokes about female logic. So she started using binary logic instead.

Inna has an array of n elements a1[1], a1[2], ..., a1[n]. Girl likes to train in her binary logic, so she does an exercise consisting of nstages: on the first stage Inna writes out all numbers from array a1, on the i-th (i ≥ 2) stage girl writes all elements of array ai, which consists of n - i + 1 integers; the k-th integer of array ai is defined as follows: ai[k] = ai - 1[kAND ai - 1[k + 1]. Here AND is bit-wise binary logical operation.

Dima decided to check Inna's skill. He asks Inna to change array, perform the exercise and say the sum of all cf div2 234 E elements she wrote out during the current exercise.

Help Inna to answer the questions!

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 105) — size of array a1 and number of Dima's questions. Next line contains nintegers a1[1], a1[2], ..., a1[n] (0 ≤ ai ≤ 105) — initial array elements.

Each of next m lines contains two integers — Dima's question description. Each question consists of two integers pi, vi (1 ≤ pi ≤ n; 0 ≤ vi ≤ 105). For this question Inna should make a1[pi] equals vi, and then perform the exercise. Please, note that changes are saved from question to question.

Output

For each question print Inna's answer on a single line.

Sample test(s)
input
3 4
1 1 1
1 1
2 2
3 2
1 2
output
6
4
7
12 原来以为要各种优化,谁知道只要直接暴力就可以了,汗!
首先把这个序列拆成二进制数,一位一位的看。
对于每个p v ,对于每个二进制位,从 p - 1 往下搜连续的1 的个数, 从 p + 1 往上搜连续的1 的个数,最后代公式
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll; #define maxn 100005 int n,m,p,v;
int a[maxn];
ll ans = ; int main () {
// freopen("sw.in","r",stdin); scanf("%d%d",&n,&m); for(int i = ; i <= n; ++i) {
scanf("%d",&a[i]);
} for(int dig = ; dig < ; ++dig) {
for(int i = ; i <= n; ++i) {
if(a[i] >> dig & ) {
int j;
for(j = i; j <= n && ((a[j] >> dig) & ); ++j ) ;
ans += ( << dig) * (ll)(j - i) * (j - i + ) / ;
i = j;
} } } for(int i = ; i <= m; ++i) {
scanf("%d%d",&p,&v);
ll l = , r = ;
for(int dig = ; dig < ; ++dig) {
int j;
for(j = p - ; ((a[j] >> dig) & ) && j >= ; --j);
// printf(" j = %d\n",j);
l = p - - j;
for(j = p + ; ((a[j] >> dig) & ) && j <= n; ++j);
r = j - (p + );
//printf("dig = %d l = %d r = %d\n",dig,l,r);
if((a[p] >> dig & ) ^ (v >> dig & )) {
ll t = (a[p] >> dig & ) ? : -;
ans += ( << dig) * t * (l * (l + ) / + r * (r + ) / -
(l + r + ) * (l + r + ) / );
}
}
a[p] = v;
printf("%I64d\n",ans); }
return ;
}
计算变化的值以修改 ans.