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的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- springMvc(三)session、HandlerInterceptorAdapter
仅供参考 设置session值,根据自己的需求设置值 /** * 登入验证 * * @return */ @RequestMapping(value = "/loginCheck.htm&q ...
- Windows 命令大全
打开控制面板的方法:输入control,回车即可打开. 以下是“运行”里常见的命令: gpedit.msc-----组策略 sndrec32-------录音机 Nslookup-------IP地址 ...
- 精简DropDownList用法
ViewBag.TypeID = new SelectList(db.TType, "ID", "Name", model.TypeID); @Html.Dro ...
- change
#include<iostream> using namespace std; int main() { double a; cin>>a; cout<<a< ...
- js动态引入的四种方式
index.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...
- 从源码看java中Integer的缓存问题
在开始详细的说明问题之前,我们先看一段代码 public static void compare1(){ Integer i1 = 127, i2 = 127, i3 = 128, i4 = 128; ...
- 正确编写Designated Initializer的几个原则
Designated Initializer(指定初始化器)在Objective-C里面是很重要的概念,但是在日常开发中我们往往会忽视它的重要性,以至于我们写出的代码具有潜藏的Bug,且不易发现.保证 ...
- SQL语句执行顺寻
SQL语句执行的时候是有一定顺序的.理解这个顺序对SQL的使用和学习有很大的帮助. 1.from 先选择一个表,或者说源头,构成一个结果集. 2.where 然后用where对结果集进行筛选.筛选出需 ...
- How to Fix Missing TortoiseSVN File Status Icons in Windows
For many Windows-based developers, Subversion and TortoiseSVN is a great source control solution. It ...
- 方法的可变长参数 传入参数个数不确定可用(Type ... values)
/** * 可变长的参数. * 有时候,我们传入到方法的参数的个数是不固定的,为了解决这个问题,我们一般采用下面的方法: * 1. 重载,多重载几个方法,尽可能的满足参数的个数.显然这不是什么好办法. ...