题目链接

给出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 线段树的更多相关文章

  1. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  2. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  3. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  4. Codeforces 444 C. DZY Loves Colors (线段树+剪枝)

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

  5. Codeforces Gym 100513F F. Ilya Muromets 线段树

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

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

  7. codeforces 1017C - Cloud Computing 权值线段树 差分 贪心

    https://codeforces.com/problemset/problem/1070/C 题意: 有很多活动,每个活动可以在天数为$[l,r]$时,提供$C$个价格为$P$的商品 现在从第一天 ...

  8. Codeforces 1045. A. Last chance(网络流 + 线段树优化建边)

    题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...

  9. Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]

    洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...

随机推荐

  1. 接着上一个版本在上一个分离access-token和ticket的版本

    上代码: 本次修改将获取token和ticket分离出来,分别封装在函数中: 每个函数最后一个参数是一个回调参数: 回调函数的参数,是这一步中需要处理的结果; 结果怎么处理,根据传递进去的函数: va ...

  2. windows 7 memcached报failed to install service or service already installed的解决方案

    今天心血来潮捣鼓一下memcache,由于系统是windows 7,我参考了 Windows下安装Memcache 使用memcached for Win32. 在运行memcached.exe -d ...

  3. C++ 清空消息队列

    在button等被禁用后,可能须要它在禁用期间不去响应不论什么消息. 能够使用以下的语句片段: MSG msg; //消耗掉消息队列中的全部消息 while(::PeekMessage(&ms ...

  4. javascript DOM,它到底是什么-------Day32

    这一晚上看的我是头疼不已啊,为什么呢? 终究是半路出家,我对javascript的理解仅仅停留在:调用javascript,改变页面样式,元素和实现一些事件的响应,尽管须要的时候可能会用,可是到底使用 ...

  5. css3 动画(animation)-简单入门

    css3之动画(animation) css3中我们可以使用动画,由于取代以前的gif图片,flash动画,以及部分javascript代码(相信有很多同学都用过jquery中的animate方法来做 ...

  6. js生成验证码并且判断

       <style type="text/css">        .code        {            font-family: Arial;      ...

  7. 解决Adobe Acrobat “正在纠偏图像,正在旋转图像,正在分解页面”问题

    笔者最近遇到的一个问题:用acrobat Pro X 打开pdf显示“正在纠偏图像,正在旋转图像,正在分解页面”,此时acrobat没有响应,要等待其完成,出现就得等一会儿,总出现,总得停顿,看一篇文 ...

  8. Oracle中的Truncate和Delete语句

    Oracle中的Truncate和Delete语句   首先讲一下,truncate命令:   语法:TRUNCATE  TABLE  table; 表格里的数据被清空,存储空间被释放. 运行后会自动 ...

  9. Unity5UGUI 官方教程学习笔记(四)UI Image

    Image Source image:源图片  需要显示的图片 Color:颜色  会与图片进行颜色的混合 Material:材质 Image Type:  Simple   精灵只会延伸到适合Rec ...

  10. Html页面操作json串

    System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaSc ...