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和相加。想到这里,就可以知道,数的范围是1e5,最多是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# richTextBox 重下往上依次查找关键字
private void richTextBox1_SelectionChanged(object sender, EventArgs e) { pos = richTextBox1.Sele ...
- GDB调试总结__1
该博客旨在分享IT技术心得和实际工作中遇到问题的解决方法,下面是新浪博客地址http://blog.sina.com.cn/qianyumolu,则为分享经济.行业趋势.心灵文章等,有兴趣的朋友能够踩 ...
- Apache SSL服务器配置SSL详解(转)
1.安装必要的软件 引用 我用的是apahce2.0.61版,可以直接官方提供的绑定openssl的apache. 文件名是:apache_2.0.61-win32-x86-openssl-0.9.7 ...
- 泰晓科技 +兰大开源社区 +程序动态分析---LINUX内核网站
http://www.tinylab.org/ http://linux-talents.tinylab.org/lzuoss/ http://www.tinylab.org/source-code- ...
- Not enough free space on disks! linux
Not enough free space on disks!100多G未分配空间也装不了Linux ------------------------------------------------- ...
- laravel扩展包开发步骤总结
1. 创建包 php artisan workbench vendor/package --resources 注: vendor:开发商名 package:包名 2.修改下包里co ...
- css(动画,过渡,转换)
css3动画 @keyframes 规定动画,必须定义动画的名称,动画时长的百分比,一个或多个css样式属性 以百分比来规定改变发生的时间,或者通过关键词"from"和" ...
- jQuery作用
jquery是前端里面比较总要的,是很强大的一个选择器. 表单: 1.$(":input") 查找所有的input元素 2.$("text") 匹配所有的 ...
- 第一篇、Apache和Tomcat的整合
1.web架构 首先上图,解释web通用架构 通常情况下分为三大块 : ★ Web server : 通常情况下由 Apache Http Server . IBM Http Server .I ...
- angular.js 字符串1
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...