2020威海-Caesar Cipher-(线段树+hash修改)

G

题意:
就是给你一个数组,然后有两种操作,第一个就是让一段区间加1,然后如果这个数>65535了就变成0,然后第二个就是查询两端区间序列是否相同。

思考:
很明显的线段树,然后对数组维护一个hash,对了取模的话自己定义一个取模,不要用ull,因为出题人想卡你就卡你。现在难点就是维护hash的同时如何操作一段区间加1?其实发现∑ a[i] * base ^ i,现在每个a都加1,那么变化值就是​∑ base ^ i,所以维护一个pre对bas进行前缀和就行了。然后还有一个点就是对于如果这个数>65535了,就变成0。因为我们一直在%mod,所以大于了可能显示没大于,所以再维护一个最大值,然后专门写一个函数去处理哪些maxn>65535的点就行了。
其实和之前做过的线段树维护hash都是一样的:
科学幻想
进制

代码:

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define node_l node<<1
#define node_r node<<1|1

using namespace std;
const int mod = 1e9+7,inf = 65536;
const int N = 2e6+10,M = 5e6+10;

struct node{
	int L,R;
	int laz;
	int sum;
	int maxn;
}t[4*N];

int T,n,m,k;
int va[M];
int bas[M],pre[M],p = 131;

void pushup(int node)
{
	int l = t[node].L,r = t[node].R,mid = (l+r)>>1;
	t[node].sum = (t[node_l].sum*bas[r-mid]%mod+t[node_r].sum)%mod;
	t[node].maxn = max(t[node_l].maxn,t[node_r].maxn);
}

void pushdown(int node)
{
	int laz = t[node].laz;
	int l = t[node].L,r = t[node].R,mid = (l+r)>>1;
	if(laz)
	{
		t[node_l].sum = (t[node_l].sum+laz*pre[mid-l]%mod)%mod;
		t[node_r].sum = (t[node_r].sum+laz*pre[r-mid-1]%mod)%mod;
		t[node_l].maxn += laz;
		t[node_r].maxn += laz;
		t[node_l].laz += laz;
		t[node_r].laz += laz;
		t[node].laz = 0;
	}
}

void build(int node,int l,int r)
{
	t[node].L = l,t[node].R = r;
	if(l==r)
	{
		t[node].sum = va[l];
		t[node].maxn = va[l];
		return ;
	}
	int mid = (l+r)>>1;
	build(node_l,l,mid);build(node_r,mid+1,r);
	pushup(node);
}

void update(int node,int l,int r,int value)
{
	if(t[node].L>=l&&t[node].R<=r)
	{
		t[node].laz += value;
		t[node].sum = (t[node].sum+pre[r-l])%mod;
		t[node].maxn += value;
		return ;
	}
	pushdown(node);
	int mid = (t[node].L+t[node].R)>>1;
	if(r<=mid) update(node_l,l,r,value);
	else if(l>mid) update(node_r,l,r,value);
	else update(node_l,l,mid,value),update(node_r,mid+1,r,value);
	pushup(node);
}

int query(int node,int l,int r)
{
	if(t[node].L>=l&&t[node].R<=r) return t[node].sum%mod;
	pushdown(node);
	int mid = (t[node].L+t[node].R)>>1;
	if(r<=mid) return query(node_l,l,r);
	else if(l>mid) return query(node_r,l,r);
	else return (query(node_l,l,mid)*bas[r-mid]%mod+query(node_r,mid+1,r))%mod;
	pushup(node);
}

void update_inf(int node)
{
	int l = t[node].L,r = t[node].R,mid = (l+r)>>1;
	if(t[node].maxn<inf) return ;
	if(l==r)
	{
		t[node].sum -= inf;
		t[node].maxn -= inf;
		return ;
	}
	pushdown(node);
	if(t[node_l].maxn>=inf) update_inf(node_l);
	if(t[node_r].maxn>=inf) update_inf(node_r);
	pushup(node);
}

void init(int x)
{
	bas[0] = pre[0] = 1;
	for(int i=1;i<=x;i++) bas[i] = bas[i-1]*p%mod;
	for(int i=1;i<=x;i++) pre[i] = (pre[i-1]+bas[i])%mod;
}

signed main()
{
	IOS;
	init(5e6+5);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>va[i];
	build(1,1,n);
	while(m--)
	{
		int op,a,b,c;
		cin>>op;
		if(op==1)
		{
			cin>>a>>b;
			update(1,a,b,1);
			update_inf(1);
		}
		else
		{
			cin>>a>>b>>c;
			int l1 = a,r1 = a+c-1;
			int l2 = b,r2 = b+c-1;
			if(query(1,l1,r1)==query(1,l2,r2)) cout<<"yes\n";
			else cout<<"no\n";
		}
	}
	return 0;
}

总结:
多多积累经验,多多思考。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值