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 ...
随机推荐
- [RxJS] Transformation operator: bufferToggle, bufferWhen
bufferToggle(open: Observable, () => close: Observalbe : Observalbe<T[]>) bufferToggle take ...
- android 53 ContentProvider内容提供者
ContentProvider内容提供者:像是一个中间件一样,一个媒介一样,可以以标准的增删改差操作对手机的文件.数据库进行增删改差.通过ContentProvider查找sd卡的音频文件,可以提供标 ...
- mybatis0205 一对多查询 复杂
查询所有用户信息,关联查询订单及订单明细信息及商品信息,订单明细信息中关联查询商品信息 1.1sql 主查询表:用户信息 关联查询:订单.订单明细,商品信息 SELECT orders.*, user ...
- android 14 进度条和拖动条
进度条: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l ...
- C开发之----#if、#ifdef、#if defined之间的区别
#if的使用说明 #if的后面接的是表达式 #if (MAX==10)||(MAX==20) code... #endif 它的作用是:如果(MAX==10)||(MAX==20)成立,那么编译器就会 ...
- switch-case参数类型
switch语句用法: 0. switch语句由一个控制表达式和多个case标签组成 1. switch控制表达式支持的类型有byte.short.char.int.enum(JDK5).String ...
- Junit简介和常用API
测试几个的概念 白盒测试——把测试对象看作一个打开的盒子,程序内部的逻辑结构和其他信息对测试人员是公开的. 回归测试——软件或环境的修复或更正后的“再测试”,自动测试工具对这类测试尤其有用. 单元测试 ...
- html px em pt长度单位(像素 相对长度 点)知识(转)
html px em pt单位区 一.PX\EM\PT单位介绍 px单位名称为像素,相对长度单位,像素(px)是相对于显示器屏幕分辨率而言的国内推荐:em单位名称为相对长度单位.相对于当前对象内文本的 ...
- asp.net利用ajax和jquery-ui实现进度条
前台用ajax不停进行查询,直到任务完成.进度条用的是jquery-ui.后台用一般处理程序处理相应,进度信息保存在HttpContext.Application中. 代码作为简单示例,实际应用时应对 ...
- CSS3新增Hsl、Hsla、Rgba色彩模式以及透明属性(转)
CSS2中色彩模式只有RGB色彩模式(RGB即RED.Green.BLue)和十六进制(Hex)模式,为了能支持 透明opacity 的Alpha值,CSS3又增加了RGBA色彩模式(RGBA即RED ...