Educational Codeforces Round 37 (Rated for Div. 2) F. SUM and REPLACE(线段树)

题目链接:http://codeforces.com/contest/920/problem/F


线段树,每次暴力更新,如果发现更新后和更新前一样的话,就停止更新,理性分析一下,其实每个点不会更新几次,预处理出来即可


代码:

#include<bits/stdc++.h>
#define xx first
#define yy second
using namespace std;
typedef long long ll;
const int MAXN=3e5+5;
const int M=1e6+5;
struct node
{
	int pos,val;
	node(int _pos=0,int _val=0):pos(_pos),val(_val){}
	bool operator < (const node &o)const
	{
		return pos<o.pos;
	}
};
set<node> s;
int a[MAXN];
struct seg
{
	#define lson l,mid,rt<<1
	#define rson mid+1,r,rt<<1|1
	ll tr[MAXN<<2];
	void push_up(int rt)
	{
		tr[rt]=tr[rt<<1]+tr[rt<<1|1];
	}
	void build(int l,int r,int rt)
	{
		if(l==r)
		{
			tr[rt]=a[l];
			return ;
		}
		int mid=(l+r)>>1;
		build(lson);
		build(rson);
		push_up(rt);
	}
	ll query(int L,int R,int l,int r,int rt)
	{
		if(L<=l&&r<=R)
			return tr[rt];
		int mid=(l+r)>>1;
		ll ret=0;
		if(L<=mid)
			ret+=query(L,R,lson);
		if(mid<R)
			ret+=query(L,R,rson);
		return ret;
	}
	void update(int pos,ll val,int l,int r,int rt)
	{
		if(l==r)
		{
			tr[rt]=val;
			return ;
		}
		int mid=(l+r)>>1;
		if(pos<=mid)
			update(pos,val,lson);
		if(mid<pos)
			update(pos,val,rson);
		push_up(rt);
	}
}se;
inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void rea(int &x)
{
	char c=nc();x=0;
	for(;c>'9'||c<'0';c=nc());for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc());
}
int D[M];
void init()
{
	for(int i=1;i<M;i++)
	{
		for(int j=i;j<M;j+=i)
		{
			D[j]++;
		}
	}
}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int n,m;
	init();
	rea(n);rea(m);
	//scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		rea(a[i]);
		//scanf("%d",&a[i]);
		s.insert(node(i,a[i]));
	}
	se.build(1,n,1);
	while(m--)
	{
		int op,l,r;
		rea(op);rea(l);rea(r);
		//scanf("%d%d%d",&op,&l,&r);
		if(op==1)
		{
			auto it=s.lower_bound(node(l,0));
			while(it!=s.end()&&it->pos<=r)
			{
				int pos=it->pos;
				int val=it->val;
				if(val==D[val])
				{
					it=s.erase(it);
				}
				else
				{
					s.erase(it);
					it=s.insert(node(pos,D[val])).xx;
					se.update(pos,D[val],1,n,1);
					it++;
				}
			}
		}
		if(op==2)
		{
			printf("%lld\n",se.query(l,r,1,n,1));
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值