CF1004F Sonya and Bitwise OR

CF1004F Sonya and Bitwise OR

Solution

感觉比较套路。

序列的前缀 o r or or有一个性质:最多变换 l o g log log次。

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

时间复杂度 O ( n l g n ) 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 second

using 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值