Fenwick tree + Euler descending



Title Description

To a sequence length, m n of operations, each operation:
1. interval [L, R & lt] [L, R & lt] [ L , R & lt ] plus XX X
2. For the interval [L, R & lt] [L, R & lt] [ L , R & lt ], query a [l] a [l + 1] a [l + 2] ...... a [l] ^ {a [L +. 1] A ^ {[L + 2]}} ...... A [ L ] A [ L + . 1 ] A [ L + 2 ] . . . . . . modmod m O PP P, until AA A- rr r
Please note that each of the different modulus.

Enter a description:

The first line of two integers n, m denotes the sequence length and the number of operations 

next line, n-integers, indicates that the sequence of

the next line m may be one of the following two operations:

Operation 1: interval [l, r] plus the x

operation 2: query value interval [l, r] mod p to that formula

Output Description:

For each inquiry, the output of a number indicates the answer
Example 1

Entry

copy
6 4
1 2 3 4 5 6
2 1 2 10000007
2 2 3 5
1 1 4 1
2 2 4 10

Export

copy
1
3
1

Remarks:

n, m <= 500000 

sequence number in each [1,2e9] within, x <= 2e9, p < = 2e7

#include<bits/stdc++.h>
#include<tr1/unordered_map>
typedef long long ll;
using namespace std;
const int maxn=2e7+9;
const int MAX=1e5+9; 
bool vis[maxn];
int cnt,prim[maxn],phi[maxn],n;
ll a[maxn];
typedef pair<ll,int>P;

inline int lowbit(int x)
{
	return x&(-x);
}

inline void update(int l,int k)
{
	while(l<=n)
	{
		a[l]+=k;
		l+=lowbit(l);
	}
}

inline ll query(int l)
{
	ll ans=0;
	while(l)
	{
		ans+=a[l];
		l-=lowbit(l);
	}
	return ans;
}

inline ll read()
{
	ll res=0;
	char c=getchar();
	while(!isdigit(c))c=getchar();
	while(isdigit(c))res=(res<<1)+(res<<3)+c-48,c=getchar();
	return res;
}

inline void init()
{
	ll i,j;
	vis[1]=phi[1]=1;
	for(i=2;i<=maxn-9;i++)
	{
		if(!vis[i])
		{
			prim[++cnt]=i;
			phi[i]=i-1;
		}
		for(j=1;j<=cnt&&i*prim[j]<=maxn-9;++j)
		{
			vis[i*prim[j]]=1;
			if(i%prim[j]==0)
			{
				phi[i*prim[j]]=phi[i]*prim[j];break;
			}
			phi[i*prim[j]]=phi[i]*(prim[j]-1);
		}
	}
}

P Pow(ll a,ll b,ll p)
{
	ll ans=1,temp=1,i;
	int flag=0;
	for(i=1;i<=b;i++)
	{
		temp=temp*a;
		if(temp>=p)
		{
			flag=1;break;
		}
	}
	a%=p;
	while(b)
	{
		if(b&1)ans=ans*a%p;
		a=a*a%p;
		b>>=1;
	}
	return P(ans,flag);
}

inline P getans(ll l,ll r,ll p)
{
	ll a=query(l);
	if(a==1||l==r||p==1)return P(a%p,a>=p?1:0);
	P b=getans(l+1,r,phi[p]);
	if(b.second)b.first+=phi[p];
	return Pow(a,b.first,p);
}

int main()
{
	int m,i;
	scanf("%d%d",&n,&m);
	init();
	for(i=1;i<=n;i++)
	{
		ll a;
		scanf("%lld",&a);
		update(i,a);
		update(i+1,-a);
	}
	while(m--)
	{
		int opt;
		ll l,r,x;
		scanf("%d%lld%lld%lld",&opt,&l,&r,&x);
		if(opt==1)
		{
			update(l,x);
			update(r+1,-x);
		}
		else
		{
			printf("%lld\n",getans(l,r,x).first);
		}
	}
}

Links: https://ac.nowcoder.com/acm/problem/17190
Source: Cattle-off network

Guess you like

Origin www.cnblogs.com/HHHEN/p/11782694.html