CF1004F Sonya and Bitwise OR

Solution

感觉比较套路。

序列的前缀ororor有一个性质:最多变换logloglog次。

所以直接建一个线段树,每个区间对于前缀、后缀分别存下O(log)O(log)O(log)个断点、ororor值以及ansansans,这样就能够很容易地合并以及统计答案。

时间复杂度O(nlgn)O(nlgn)O(nlgn)。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se secondusing namespace std;template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=200005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{int f=1,x=0; char c=getchar();while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }return x*f;
}
int n,m,MIN,a[MAXN];
struct Node
{ll ans;vector<PR> L,R;void clear() { ans=0,L.clear(),R.clear(); }Node() { clear(); }
};
Node operator + (Node x,Node y)
{Node z;z.L=x.L;for (int i=0,now=z.L.back().se;i<y.L.size();i++){PR t=y.L[i];if ((t.se|now)!=now) now|=t.se,z.L.PB(MP(t.fi,now));}z.R=y.R; reverse(z.R.begin(),z.R.end());for (int i=x.R.size()-1,now=z.R.back().se;i>=0;i--){PR t=x.R[i];if ((t.se|now)!=now) now|=t.se,z.R.PB(MP(t.fi,now));}reverse(z.R.begin(),z.R.end());z.ans=x.ans+y.ans;for (int i=0,now=0;i<y.L.size();i++){while (now<x.R.size()&&(x.R[now].se|y.L[i].se)>=MIN) now++;int sl=(!now?0:x.R[now-1].fi-x.L[0].fi+1);int sr=(i+1==y.L.size()?y.R.back().fi-y.L[i].fi+1:y.L[i+1].fi-y.L[i].fi);z.ans+=1ll*sl*sr;}return z;
}struct Segment_Tree
{Node tree[MAXN<<2];void up(int x) { tree[x]=tree[x<<1]+tree[x<<1|1]; }void build(int x,int l,int r){tree[x].clear();if (l==r){tree[x].L.PB(MP(l,a[l])),tree[x].R.PB(MP(l,a[l]));tree[x].ans=(a[l]>=MIN);return;}int mid=(l+r)>>1;build(x<<1,l,mid);build(x<<1|1,mid+1,r);up(x);}void change(int x,int l,int r,int y,int z){if (l==r){tree[x].clear();tree[x].L.PB(MP(y,z)),tree[x].R.PB(MP(y,z));tree[x].ans=(z>=MIN);return;}int mid=(l+r)>>1;if (y<=mid) change(x<<1,l,mid,y,z);else change(x<<1|1,mid+1,r,y,z);up(x);}Node query(int x,int l,int r,int L,int R){if (l>=L&&r<=R) return tree[x];int mid=(l+r)>>1;if (R<=mid) return query(x<<1,l,mid,L,R);else if (L>mid) return query(x<<1|1,mid+1,r,L,R);else return query(x<<1,l,mid,L,mid)+query(x<<1|1,mid+1,r,mid+1,R);;}
} segment;
int main()
{n=read(),m=read(),MIN=read();for (int i=1;i<=n;i++) a[i]=read();segment.build(1,1,n);for (int i=1;i<=m;i++){int opt=read(),x=read(),y=read();if (opt==2) printf("%I64d\n",segment.query(1,1,n,x,y).ans);else segment.change(1,1,n,x,y);}return 0;
}

CF1004F Sonya and Bitwise OR相关推荐

  1. CF1004F Sonya and Bitwise OR(线段树平衡复杂度+or 前缀性质)

    CF1004F Sonya and Bitwise OR 有一个长度为 \(n\) 的数组 \(\{a\}\),有 \(m\) 次操作,又给定一个数 \(x\),有两类操作: 1 i y 将 \(a_ ...

  2. 27.CF1004F Sonya and Bitwise OR 区间合并线段树

    27.CF1004F Sonya and Bitwise OR 区间合并线段树 个人Limitの线段树题单题解主目录:Limitの线段树题单 题解目录_HeartFireY的博客-CSDN博客 给定序 ...

  3. 线段树 ---- CF1004F Sonya and Bitwise OR(线段树上分治合并区间信息 + or 前缀和的log性质)

    题目链接 题目大意: 解题思路: 考虑只有一次询问时怎么做. 分治.每次考虑LLL位于左半边,RRR位于右半边的情况(也就是"跨过中点"的答案).再分别递归左.右两边.计算跨过中点 ...

  4. Codeforces 1004F Sonya and Bitwise OR (线段树)

    题目链接 https://codeforces.com/contest/1004/problem/F 题解 这种水题都不会做了怎么.. 考虑一个序列的前缀 \(\text{or}\) 值只会变化 \( ...

  5. LeetCode Bitwise AND of Numbers Range(位操作)

    问题:给出一个区间[m,n],求这些数的位与结果 思路: 一种是直接根据定义,遍历[m,n],当m或者遍历过程等于Integer.MAX_VALUE,直接返回. 第二种方式是当m不等于n时,从最低位开 ...

  6. Sonya and Queries CodeForces - 714C

    Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an i ...

  7. Linux Mint 18.2 Sonya 将于 6 月上旬发布公测版本

    Linux Mint 18.2"Sonya"beta 即将于本月上旬推出,不过还是比原计划的五月发布晚了一些. Mint 团队领导人 Clement Lefebvre 宣布,距离 ...

  8. JavaScript Bitwise NOT Operator

    心血来潮地跑到Upworks做了个JavaScript Test,结果当然是惨不忍睹,发现自己对不少JavaScript的基础知识的理解是模模糊糊,甚至是错的. 比如这题: ~-(2+"2& ...

  9. leetcode 201. Bitwise AND of Numbers Range(位运算,dp)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

最新文章

  1. html5父子页面数据传递,使用iframe标签嵌套页面时 如何进行父子页面通讯/传值...
  2. android版多功能日历,欢迎大家测试
  3. SAP系统上线后的变化
  4. HTML5-Canvas 图形变换+状态保存
  5. HTML+CSS+JS实现燃烧的火焰火花动画特效
  6. linux 编译字符设备驱动错误,linux字符设备驱动框架及编写流程
  7. 李彦宏:百度智能汽车预计2023年和大家见面
  8. spring3: AOP 之 6.2 AOP的HelloWorld
  9. 基于 snowNLP的微博评论数据情感分析
  10. Linux 内核软中断(softirq)执行分析
  11. deepin安装maven
  12. mybatis 文档 学习
  13. 你以为你在利用碎片化时间,实际上你的时间被碎片化了
  14. Python实战:导出聊天记录分析你和你的对象聊了什么
  15. 在Keil4中新建51单片机工程模板详细步骤
  16. J-Link RTT使用
  17. 音频转换器怎么将ogg转换mp3格式
  18. sap文档服务器,SAP程序文档管理方案
  19. ASP.Net Core The type initializer for 'Gdip' threw an exception
  20. NullPointerException案例

热门文章

  1. ​怎么用藏头诗向女友表白......
  2. 恐龙的丁丁长什么样?它们是怎么啪啪啪的?这项研究网友看完直呼涨姿势.........
  3. 少女为什么会身上香香的?
  4. 美女晕倒怎么办?二哈:这不是一滋就醒......
  5. 太漂亮了!66个高颜值的矿物晶体,吸引到你的目光了吗
  6. 女生心中的理想男生!这些条件你符合几条?
  7. google的api key调用次数是多少_Sprint Boot如何基于Redis发布订阅实现异步消息系统的同步调用?...
  8. matlab生成有向网络,matlab ode45和矩阵生成有向网络图
  9. 点歌软件测试自学,实际歌唱对比测试
  10. 计算数字的出现次数 java_关于Java:如何计算数字在.txt文件中出现的次数