Codeforces400E - Inna and Binary Logic - 思维、数学
2018-03-21 10:53
585 查看
E. Inna and Binary Logic
分类:brute force
math
Description
初始给你长度为 n (1≤n≤105)n (1≤n≤105) 大小的数组,现在会执行一种操作持续 nn 轮,每次 ai,j=ai−1,j & ai−1,j+1ai,j=ai−1,j & ai−1,j+1 执行下去,每轮求一个和,直到只剩下一个数,每一轮的答案加起来就是总和答案。现在增加修改操作,总共有 m (1≤m≤105)m (1≤m≤105) 次修改,每次输入 pi vipi vi 表示把第 pipi 个数修改成 vivi 再去执行这个过程,并询问总和答案!
Solution
画出这个过程,我们按位考虑,每一段连续的 11,假设长度为 xx 那么,对答案的贡献容易发现也是个三角形, x(x+1)2x(x+1)2 ,而同样我们修改时候,假设会影响到二进制的第 ii 位,那么也可以顺便统计一下它连续左侧和右侧的 11 ,不妨记为 ll 和 rr,同样可以推出来对答案的贡献就是 (l+r+1+l×r)×2k(l+r+1+l×r)×2k 。
Code
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 5; int a[maxn], n, m; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); ll sum = 0; for (int i = 0; i < 18; i++) { int k = 1 << i; int cnt = 0; for (int j = 1; j <= n; j++) { if (k & a[j]) cnt++; else { sum += 1LL * cnt * (cnt + 1) / 2 * k; cnt = 0; } } if (cnt) sum += 1LL * cnt * (cnt + 1) / 2 * k; } while (m--) { int p, v; scanf("%d%d", &p, &v); for (int i = 0; i < 18; i++) { int k = 1 << i; if ((k & v) == (k & a[p])) continue; int l = 0, r = 0; for (int j = p - 1; j >= 1; j--) { if (a[j] & k) l++; else break; } for (int j = p + 1; j <= n; j++) { if (a[j] & k) r++; else break; } ll tmp = (l + r + 1LL * l * r + 1) * k; if (k & v) sum += tmp; else sum -= tmp; } a[p] = v; printf("%lld\n", sum); } return 0; }
相关文章推荐
- Codeforces 400E Inna and Binary Logic(位运算+暴力)
- !codeforces 400E Inna and Binary Logic-yy-(位运算)
- CodeForces 400E Inna and Binary Logic
- CF 400E Inna and Binary Logic(多维线段树)
- Codeforces-916B:Jamie and Binary Sequence (changed after round)(思维)
- Codeforces 766E Mahmoud and a xor trip [二进制,]【数学+思维】
- codeforces 374A Inna and Pink Pony(数学)
- CodeForces 374 B.Inna and Nine(组合数学)
- Inna and Binary Logic
- Inna and Binary Logic
- codeforces 347 c Alice and Bob(博弈 && 数学)
- Codeforces 559C Gerald and Giant Chess 组合数学 DP
- CodeForces - 361C A - Levko and Array Recovery 思维
- CodeForces 360A - Levko and Array Recovery (思维)
- CodeForces 366A Dima and Guards(结构体,数学)
- B. Inna and Nine----思维题
- codeforces 390D Inna and Sweet Matrix
- Codeforces 916B-Jamie and Binary Sequence
- codeforces 400 C Inna and Huge Candy Matrix【模拟】
- Codeforces 400 C. Inna and Huge Candy Matrix