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的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- CentOS 7.2 无法生成 coredump文件
CentOS版本 cat /etc/centos-release CentOS Linux release 7.2.1511 (Core) 设置ulimit -c ulimited 依旧无法生成co ...
- MySql安装时在Start Service处失败
安装MySql时在最后Start Service时失败: 首先先卸载掉MySql. 查看MySql服务有没有启动,若启动,则先停止MySql服务. 打开注册表,到HKEY_LOCAL_MACHIN ...
- HTML5应用开发:JavaScript库iScroll教程
目录 1. iScroll介绍 2. 安装和使用 3. 简单的iScroll例子 4. Pinch & Zoom 5. Snap to element 6. iScroll 详细参数 1. i ...
- 运行yum报错Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
今天给Centos通过rpm -Uvh装了个epel的扩展后,执行yum就开始报错: Error: Cannot retrieve metalink for repository: epel. Ple ...
- 安装gitolite,并ssh公钥无密码登录
安装gitolite,并ssh公钥无密码登录 gitolite是管理git版本库的一种方案,它将git版本库的管理信息放在了一个特殊git版本库里.gitolite与linux操作系统集成了,需要使用 ...
- 一款jQuery立体感动态下拉导航菜单特效
一款jQuery立体感动态下拉导航菜单特效,鼠标经过,在菜单栏上方下拉出一个背景图片,效果十分不错的一款jquery特效. 对IE6都是兼容的,希望大家好好研究研究. 适用浏览器:IE6.IE7.IE ...
- 2013-07-24 IT 要闻速记快想
### ========================= ###凡客有闹钟?从凡客的角度来讲,闹钟等工具类应用是为推广品牌和产品服务,通过工具类产品给大众一个对凡客品牌的认知.而选择推出工具类的产品 ...
- pandas库学习笔记(一)Series入门学习
Pandas基本介绍: pandas is an open source, BSD-licensed (permissive free software licenses) library provi ...
- Linux驱动开发之开篇--HelloWorld
Linux驱动的编写,大致分为两个过程,第一个过程为测试阶段,即为某一具体的设备,添加必要的驱动模块,为了节省编译时间,需要将代码单独放在一处,在编译时,只需要要调用内核的头文件即可:第二个过程为布置 ...
- 把数组排成最小的数/1038. Recover the Smallest Number
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. Give ...