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的更多相关文章

  1. 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个数. 给出一 ...

  2. cf div2 234 E

    E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...

  3. CodeForces 400

    A - Inna and Choose Options Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d &a ...

  4. 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 ...

  5. Java TreeMap 源码解析

    继上篇文章介绍完了HashMap,这篇文章开始介绍Map系列另一个比较重要的类TreeMap. 大家也许能感觉到,网络上介绍HashMap的文章比较多,但是介绍TreeMap反而不那么多,这里面是有原 ...

  6. Method, apparatus, and system for speculative abort control mechanisms

    An apparatus and method is described herein for providing robust speculative code section abort cont ...

  7. 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 ...

  8. Logic BIST

    Logic BIST is crucial for many applications, in particular for life-critical and mission-critical ap ...

  9. [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 ...

随机推荐

  1. 文件上传~Uploadify上传控件~续(多文件上传)

    对于Uploadify文件上传之前已经讲过一次(文件上传~Uploadify上传控件),只不过没有涉及到多文件的上传,这回主要说一下多个文件的上传,首先,我们要清楚一个概念,多文件上传前端Upload ...

  2. C# 打开PPT文件另存为PPTX

    /// <summary> /// rename PPT /// </summary> private static void renamePPT() { //add refe ...

  3. android 21 隐式意图启动系统预定义activity

    Intent intent=new Intent(LoginActivity.this, MainActivity.class);//显示意图启动,显示从一个activity到另一个activity, ...

  4. 3 - SQL Server 2008 之 使用SQL语句删除约束条件

    基本语法为: ALTER TABLE 表名 DROP CONSTRAINT 约束名1,约束名2…… 如果删除一个约束,不需要逗号后的约束名,如果删除两个及两个以上的约束,必须以逗号隔开. 使用上一节中 ...

  5. 如何为 setTimeout() 方法传参

    现有如下JavaScript代码: function printApple(apple){ console.log(apple, "is a kind of healthy fruit&qu ...

  6. laydate时间组件在火狐浏览器下有多时间输入框时只能给第一个输入框赋值的问题

    遇到的问题: laydate时间组件在火狐浏览器下有多时间输入框时只能给第一个输入框赋值的问题(safari下也有同样问题); 解决办法: 给laydate绑定id; 解决前代码: <input ...

  7. 无软驱加载raid驱动安装windows2003及其他微软操作系统

    [转载]http://blog.zol.com.cn/2650/article_2649199.html [另一篇]http://www.blue1000.com/bkhtml/c159/2013-0 ...

  8. 部分A+B_1

    正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...

  9. 数据库导出导入操作(expdp,impdp)

    EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. 命令行: sqlplus/nolog connect username/password as sysd ...

  10. 在往oracle中插数据时,如何处理excel读取的时间空值

    //若从excel中读取的时间值为空值时,做如下转换 string YDKGSJ = string.Empty; if (dbdata.Rows[i]["约定开工时间"].ToSt ...