cf920f(线段树+数论)

昨天不造为啥脑残去把操作几次后的状态都维护出来了,简直智障啊qaq多好的一次上分机会就这么没了qaq

这个和区间开方同个道理。。直接判断区间每个数是不是都是1或2就行,因为该操作对1和2无效。。然后对任意一个数操作最多目测不超过10次,所以可以不用担心TLE。。

那个d函数直接暴力筛,nlogn足够了。。

然后这个写起来贼简单。。推掉重写不用10分钟A。。窝好恨啊。。

补充:d函数还可以欧拉筛。。复杂度为o(n),不过快不了多少就是。。



#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf 1e9
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define mid (x+y>>1)
#define NM 1000005
#define nm 100498
#define pi 3.141592653
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}




int b[NM],n,m,_x,_y,_t;
struct node{
	ll s;int x,y;bool tag;
	node*l,*r;
	void upd(){s=l->s+r->s;tag=l->tag&&r->tag;}
	node(int x,int y,int s,node*l=0,node*r=0):tag(s<3),s(s),x(x),y(y),l(l),r(r){if(l)upd();}
	void mod(){
		if(_y<x||y<_x)return;
		if(_x<=x&&y<=_y&&tag)return;
		if(x==y){s=b[s];tag=(s<3);return;}
		l->mod();r->mod();upd();
	}
	ll query(){
		if(_y<x||y<_x)return 0;
		if(_x<=x&&y<=_y)return s;
		return l->query()+r->query();
	}
}*root;
node*build(int x,int y){return x==y?new node(x,y,read(),0,0):new node(x,y,0,build(x,mid),build(mid+1,y));}


int main(){
	n=read();m=read();
	inc(i,1,1e6)for(int j=1;i*j<=1e6;j++)b[i*j]++;
	root=build(1,n);
	while(m--){
		_t=read();_x=read();_y=read();
		if(_t==1)root->mod();
		else printf("%I64d\n",root->query());
	}
	return 0;
}


 

加欧拉筛后:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf 1e9
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define mid (x+y>>1)
#define NM 1000005
#define nm 100498
#define pi 3.141592653
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}

int b[NM],n,m,_x,_y,_t,c[NM],tot,prime[NM];
bool v[NM];
struct node{
	ll s;int x,y;bool tag;
	node*l,*r;
	void upd(){s=l->s+r->s;tag=l->tag&&r->tag;}
	node(int x,int y,int s,node*l=0,node*r=0):tag(s<3),s(s),x(x),y(y),l(l),r(r){if(l)upd();}
	void mod(){
		if(_y<x||y<_x)return;
		if(_x<=x&&y<=_y&&tag)return;
		if(x==y){s=b[s];tag=(s<3);return;}
		l->mod();r->mod();upd();
	}
	ll query(){
		if(_y<x||y<_x)return 0;
		if(_x<=x&&y<=_y)return s;
		return l->query()+r->query();
	}
}*root;
node*build(int x,int y){return x==y?new node(x,y,read(),0,0):new node(x,y,0,build(x,mid),build(mid+1,y));}


void Euler(){
	b[1]=1;
	inc(i,2,1e6){
		if(!v[i]){
			b[i]=2;c[i]=1;prime[++tot]=i;
		}
		inc(j,1,tot){
			if(i*prime[j]>1e6)break;
			v[i*prime[j]]=true;
			if(i%prime[j]==0){
				c[i*prime[j]]=c[i]+1;
				b[i*prime[j]]=b[i]/(c[i]+1)*(c[i*prime[j]]+1);
				break;
			}else{
				b[i*prime[j]]=b[i]*2;c[i*prime[j]]=1;
			}
		}
	}
}

int main(){
	n=read();m=read();
	Euler();
	root=build(1,n);
	while(m--){
		_t=read();_x=read();_y=read();
		if(_t==1)root->mod();
		else printf("%I64d\n",root->query());
	}
	return 0;
}




F. SUM and REPLACE
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let D(x) be the number of positive divisors of a positive integerx. For example, D(2) = 2 (2 is divisible by1 and 2), D(6) = 4 (6 is divisible by 1, 2, 3 and 6).

You are given an array a of n integers. You have to process two types of queries:

  1. REPLACE lr — for every replaceai withD(ai);
  2. SUM lr — calculate .

Print the answer for each SUM query.

Input

The first line contains two integers n andm (1 ≤ n, m ≤ 3·105) — the number of elements in the array and the number of queries to process, respectively.

The second line contains n integers a1, a2, ...,an (1 ≤ ai ≤ 106) — the elements of the array.

Then m lines follow, each containing 3 integers ti,li,ri denotingi-th query. If ti = 1, theni-th query is REPLACE liri, otherwise it'sSUM liri (1 ≤ ti ≤ 2,1 ≤ li ≤ ri ≤ n).

There is at least one SUM query.

Output

For each SUM query print the answer to it.

Example
Input
7 6
6 4 1 10 3 2 4
2 1 7
2 4 5
1 3 5
2 4 4
1 5 7
2 1 7
Output
30
13
4
22



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值