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个数。
给出一种操作, 将a[1][x]这个位置的数换为y, 然后求换完之后的这n(n-1)/2个数的和。
很有意思的题, 这个题应该按位建线段树, 这样需要建18棵, 因为a[1][i]的最大值为1e5。
然后我们来看具体如何做, 我们来看一组数据:3 6 7, 换为二进制之后是下面这个样子。
0 1 1
1 1 0
1 1 1
我们先来算一算这三个数的值为多少, 3+6+7+2+6+2 = 26。
我们发现第一个数和第二个数&之后剩余的是2, 第二个数和第三个数&之后剩余的是6, 我们来观察二进制, 发现第一个数和第二个数的第二位的2个1是相邻的, 并且这一位换成10进制正好是2, 而第二个数和第三个数的第一位和第二位的两个1是相邻的, 换成10进制之后是6。
由此可以推断出, 如果是一个单独的1, 那么贡献就是1, 两个相邻的1贡献是2*3/2 = 3,n个相邻的1贡献就是n(n+1)/2。
那么我们建立线段树的时候就应该保存一个sum, prefix, 以及suffix, 具体的看代码。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e5+;
ll cal(ll val) {
return 1LL*val*(val+)/;
}
struct SegmentTree
{
ll sum[maxn<<], pre[maxn<<], suf[maxn<<];
void pushUp(int rt, int m) {
sum[rt] = sum[rt<<|] + sum[rt<<];
if(pre[rt<<|]&&suf[rt<<]) {
sum[rt] += cal(pre[rt<<|]+suf[rt<<])-cal(suf[rt<<])-cal(pre[rt<<|]);
}
pre[rt] = pre[rt<<];
suf[rt] = suf[rt<<|];
if(pre[rt] == m-(m>>)) {
pre[rt] += pre[rt<<|];
}
if(suf[rt] == m>>) {
suf[rt] += suf[rt<<];
}
}
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
sum[rt] = pre[rt] = suf[rt] = val;
return ;
}
int m = l+r>>;
if(p<=m)
update(p, val, lson);
else
update(p, val, rson);
pushUp(rt, r-l+);
}
}tree[];
int main()
{
int cnt, n, m, x, y, a[];
cin>>n>>m;
for(int i = ; i<=n; i++) {
scanf("%d", &x);
cnt = ;
while(x) {
a[cnt++] = x&;
x>>=;
}
for(int j = ; j<cnt; j++) {
tree[j].update(i, a[j], , n, );
}
}
while(m--) {
scanf("%d%d", &x, &y);
cnt = ;
mem(a);
while(y) {
a[cnt++] = y&;
y>>=;
}
ll ans = , mul = ;
for(int i = ; i<; i++) {
tree[i].update(x, a[i], , n, );
ans += tree[i].sum[]*mul;
mul*=;
}
cout<<ans<<endl;
}
return ;
}
codeforces 400E. Inna and Binary Logic 线段树的更多相关文章
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- Codeforces 444 C. DZY Loves Colors (线段树+剪枝)
题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...
- Codeforces Gym 100513F F. Ilya Muromets 线段树
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
- Codeforces 834D The Bakery【dp+线段树维护+lazy】
D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...
- codeforces 1017C - Cloud Computing 权值线段树 差分 贪心
https://codeforces.com/problemset/problem/1070/C 题意: 有很多活动,每个活动可以在天数为$[l,r]$时,提供$C$个价格为$P$的商品 现在从第一天 ...
- Codeforces 1045. A. Last chance(网络流 + 线段树优化建边)
题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...
- Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]
洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...
随机推荐
- 【OpenCV新手教程之十二】OpenCV边缘检測:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/25560901 作者:毛星云(浅墨) ...
- Struts2复习(四)防止表单反复提交
1.採取请求转发的方式完毕表单内容的加入会造成内容的反复插入. 2.採取重定向的方式实现数据的加入不会导致数据的反复插入. 3.防止表单反复提交的两种方式 1) 通过重定向 2) 通过Sessi ...
- 从零开始学习UNITY3D(GUI篇 2)
复合控件极其使用,toolbar,selectgrid 先看效果图: toolbar可以看作是一个button按钮的集合,一次只能点击一个按钮, selectgrid又可以堪称是一个toolbar的集 ...
- 关于html5之canvas的那些事
何为canvas <canvas> 标签只是图形容器,您必须使用脚本来绘制图形.默认情况下该矩形区域宽为300像素,高为150像素,设置宽高必须在canvas标签内部,不能加单位px. 大 ...
- 关于matlab鼠标响应
今天看了一下Matlab中响应鼠标的事件,整理如下, (1)函数WindowButtonMotionFcn,当鼠标在窗口上运动的时候就会相应此函数,于是在此函数中可以设置运动时想要的代码,如:改变鼠标 ...
- C++中的栈和队列
使用标准库的栈和队列时,先包含相关的头文件 #include<stack> #include<queue> 定义栈如下: stack<int> stk; 定义队列如 ...
- Unity3D移植到自己的Android程序
用Unity3D开发需要把动画效果移植到现有的APP上面.Unity for Android 比较特殊,Unity for IOS 打包是将XCODE工程直接交给开发者,开发者可以在工程的基础上继续添 ...
- java面向对象之 类和对象
OOP:Object Oriented Programming(面向对象编程) 面向对象: 1:将复杂的事情简单化. 2:面向对象将以前的过程中的执行者,变成了指挥者. 3:面向对象这种思想是符合现在 ...
- [C语言练习]学生学籍管理系统
/** * @copyright 2012 Chunhui Wang * * wangchunhui@wangchunhui.cn * * 学生学籍管理系统(12.06) */ #include &l ...
- SQL Server 数据库备份到域中别的机器上
backup database dbName to disk = '\\SV2\D\dbbackup\dbName.bak' with init,compression;