Inna and Binary Logic
Codeforces Round #234 (Div. 2) E:http://codeforces.com/problemset/problem/400/E
题意:给你n个数,然后每相邻的两个数可以通过and运算生成一个新的数,然后这些新生成的n-1个数每相邻的两个数又通过and运算成n-2个数,最后只会剩下一个数,然后让你求这n(n+1)/2个数的和,然后每一次会更新最底层的某个数,然后操作之后,输出刚才的总和。
题解:这一题,虽然是看题解,然后自己敲出来的,但是还是有点成就感和收获。首先,这一题的思路很巧妙。如果所有的数都是1或者0,加入说序列是1110001,通过计算,发现其实和就是(3+1)*3/2+(1+1)*1/2==7,与连续1的个数有关,加入连续1的个数是x,那么这连续的x个1,形成的和就是(x+1)*x/2;总和就是把所有连续的1和相加。想到这里,就可以知道,数的范围是1
e5,最多是2^17,所以可以把每个数拆成17位,每一位要么是0或者是1,这样只要统计底层的连续1有多少就可以了。num[i][j]表示第j个数的第i位,一开始我们可以计算出总和,然后更新时,如果更新数的这一位和原来相同则这一位不用变化,否则,如果是1要0,可以把ans先减去原来连续个一所形成的和,然后加上这个数左边连续1的和以及右边连续1的和,然后把这一位变成0,如果是0变1,则相反。这一要注意数据范围,在过程中有可能爆int,所以要用long long,并且在求和过程中使用的局部变量也要用long long,由于自己没有注意到这样的问题结果wa 2发,int和long long之间转换出了问题。也许,有人会问这样会不会t,首先事实上没有t,而且跑的很快。从理论上讲,也不会,因为要出现很长的连续的1是很难的,必须保证这个连续的数在某些位上都是1,并且连续,很难,这样的数据很难。所以很快。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+;
long long num[][N];
long long ans;
long long a[N],v;
int n,m,p;
long long tmp[];
int main(){
while(~scanf("%d%d",&n,&m)){
memset(num,,sizeof(num));
memset(a,,sizeof(a));
memset(tmp,,sizeof(tmp));
for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
for(int j=;j<=;j++){
num[j][i]=(a[i]&);
a[i]/=;
}
}
ans=;
for(int j=;j<=;j++){
long long temp=,tp=;
for(int i=;i<=n;i++){
if(num[j][i]==){
tp+=temp*(temp+)/;
temp=;
}
else{
temp++;
}
if(i==n){
tp+=temp*(temp+)/;
temp=;
}
}
ans+=(tp<<j);
}
for(int i=;i<=m;i++){
scanf("%d%I64d",&p,&v);
for(int j=;j<=;j++){
tmp[j]=(v&);
v/=;
}
for(int j=;j<=;j++){
long long sum=;
int tt=p;
if(tmp[j]==num[j][p])continue;
long long lnum=,rnum=;
while(num[j][--tt])
lnum++;
tt=p;
while(num[j][++tt])
rnum++;
if(tmp[j]==&&num[j][p]==){
sum-=(lnum+rnum+)*(lnum+rnum+)/;
sum+=(lnum+)*lnum/;
sum+=(rnum+)*rnum/;
num[j][p]=;
}
else{
sum-=(lnum+)*lnum/;
sum-=(rnum+)*rnum/;
sum+=(lnum+rnum+)*(lnum+rnum+)/;
num[j][p]=;
}
ans+=(sum<<j);
}
printf("%I64d\n",ans);
}
} }
Inna and Binary Logic的更多相关文章
- codeforces 400E. Inna and Binary Logic 线段树
题目链接 给出n个数, 定义a[1][i]为这初始的n个数, 然后a[i][j] = a[i-1][j]&a[i-1][j-1], 这样就可以得到一个三角形一共n*(n-1)/2个数. 给出一 ...
- cf div2 234 E
E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...
- CodeForces 400
A - Inna and Choose Options Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- A trip through the Graphics Pipeline 2011_09_Pixel processing – “join phase”
Welcome back! This post deals with the second half of pixel processing, the “join phase”. The pre ...
- Java TreeMap 源码解析
继上篇文章介绍完了HashMap,这篇文章开始介绍Map系列另一个比较重要的类TreeMap. 大家也许能感觉到,网络上介绍HashMap的文章比较多,但是介绍TreeMap反而不那么多,这里面是有原 ...
- Method, apparatus, and system for speculative abort control mechanisms
An apparatus and method is described herein for providing robust speculative code section abort cont ...
- Satisfying memory ordering requirements between partial reads and non-snoop accesses
A method and apparatus for preserving memory ordering in a cache coherent link based interconnect in ...
- Logic BIST
Logic BIST is crucial for many applications, in particular for life-critical and mission-critical ap ...
- [LeetCode#156] Binary Tree Upside Down
Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left ...
随机推荐
- C#-Mdi多文档窗体及其子窗体的排列 ---ShinePans
MdiLayout枚举成员及说明 Casecade s全部Mdi层叠在父窗体 TileHorizontal 水平平铺 TitleVertical 垂直平铺 Form1.cs (mdi) using S ...
- mount命令以及mount ntfs硬盘权限权限与显示的问题 分类: shell ubuntu 2014-11-08 18:29 148人阅读 评论(0) 收藏
sudo mount -t 文件系统类型 -o 可设置选项 设备路经 访问路经 #常用文件类型如下: iso9660 光驱文件系统, vfat fat/fat32分区, ntfs ntfs分区, sm ...
- android 27 ListView
效果: 上图中ArrarAdapter是数组的适配器,CursorAdapter是游标适配器,用于操作数据库的数据. ListView是垂直列表,数据源是集合或者数组,这些View都是安卓里的Adap ...
- Driving the Activity Lifecycle
Before Robolectric 2.2, most tests created Activities by calling constructors directly, (new MyActiv ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 二进制编码的个体解码操作 decode.c
种群解码函数 decode_pop 为包装函数, 核心调用函数为 decode_ind , 对每个个体进行解码. /* Routines to decode the population */ ...
- HDFS的Java客户端操作代码(查看HDFS下所有的文件存储位置信息)
1.查看HDFS下所有的文件存储位置信息 package Hdfs; import java.net.URI; import org.apache.hadoop.conf.Configuration; ...
- HTML5移动开发中的input输入框类型
HTML5规范引入了许多新的input输入框类型 在HTML5移动开发中,通过这些新的输入框类型来显示定制后的键盘布局,用户体验更好,更容易填写各种表单 本文中,实测手机为肾4S与米4 数字类型num ...
- Java-Android 之短信发送
file:///F:/workspace3/Android_ver2.5/src/cn/szy/com/MainActivity.java package cn.szy.com; import jav ...
- 图论——读书笔记(基于BFS广度优先算法的广度优先树)
广度优先树 对于一个图G=(V,E)在跑过BFS算法的过程中会创建一棵广度优先树. 形式化一点的表示该广度 优先树的形成过程是这样的: 对于图G=(V,E)是有向图或是无向图, 和图中的源结点s, 我 ...
- ios第三方工具
1. DXPopover 带尖叫的提示框 2. MMDrawerController 最好的抽屉效果 3.MMProgressHUD 提示框 4.Reachability :网络链接检测 UIBu ...