cf div2 234 E
3 seconds
256 megabytes
standard input
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[k] AND 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 elements she wrote out during the current exercise.
Help Inna to answer the questions!
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.
For each question print Inna's answer on a single line.
3 4
1 1 1
1 1
2 2
3 2
1 2
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.
cf div2 234 E的更多相关文章
- cf div2 234 D
D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 离线dfs CF div2 707 D
http://codeforces.com/contest/707/problem/D 先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理 ...
- cf div2 239 D
D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- cf div2 236 D
D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- cf div2 237 D
D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- cf div2 238 D
D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- cf div2 238 c
C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- cf div2 235 D
D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...
- CF div2 D BFS
http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- 济南学习 Day 3 T1 am
NP(np)Time Limit:1000ms Memory Limit:64MB题目描述LYK 喜欢研究一些比较困难的问题,比如 np 问题.这次它又遇到一个棘手的 np 问题.问题是这个样子的:有 ...
- Codevs 1097 校门外的树
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米 ...
- Winfrom皮肤样式的使用
IrisSkin类库提供了可供我们使用的设置窗体皮肤的类,简单地说,就是给我们提供了一个皮肤引擎,通过设置皮肤引擎来达到我们想要的窗体界面. 具体的开发步骤: (1)引入IrisSkin.dll文件 ...
- 【风马一族_Android】Android学习
如果是色彩盲,请自行收藏这个网站:Material Design Color Palette Generator 所有书上的UI部分扫一遍就行了,我相信现在还没有书按material design标准 ...
- 正则匹配 去掉 多余的js和html标签
$reg17 = '/><strong>公司介绍<\/strong><\/td>([\S\s*]+?)<\/div>/'; $this->d ...
- IDEA笔记
快捷键: 查找类:ctrl + shif + R (eclipse)查找文件:double shift查找文件中的变量名和方法:ctrl + H (eclipse)system.out:输入 sout ...
- 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- python之字串
python字串声明: 单引('), 双引("), 三引(''' 或 """"). python字串前缀: r表示原生字串, 字串内容: (1)不能包 ...
- mysql语句大全
转自:http://www.cnblogs.com/yunf/archive/2011/04/12/2013448.html 1.说明:创建数据库 CREATE DATABASE database ...
- 【UIScrollView】基本方法+基本描述
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )]; scrollView.backgroundColor = [ ...