Little Q is very sleepy, and he really needs some impenetrable hard problems coffee to make him awake.
At this time, Little L brings a pot to Little Q, and he states the pot as follows.
For a prime number p, if p m |n and p m+1 6 |n, we say pot p (n) = m.
The pot is very special that it can make everyone awake immediately.
Now Little L provides n (1 ≤ n ≤ 10 5 ) integers a 1 ,a 2 ,··· ,a n to Little Q, each of which is 1 initially.
After that, Little L shows 2 types of queries:
• MULTIPLY l r x : For every i ∈ [l,r] (1 ≤ l ≤ r ≤ n), multiply a i by x (2 ≤ x ≤ 10).
• MAX l r : Calculate the value of
max
l≤i≤r
?
max
p|a i
? pot
p (a i )
? ?
(1 ≤ l ≤ r ≤ n),
where p is prime.
Now you need to perform q (1 ≤ q ≤ 10 5 ) queries of these two types of queries described above.
If you perform a “MULTIPLY” query, you don’t need to output anything.
If you perform a “MAX” query, you need to output a line like “ANSWER y”, where y the value you’ve
calculated.
Input
The first line contains two integers n (1 ≤ n ≤ 10 5 ) and q (1 ≤ q ≤ 10 5 ), the number of integers and the
number of queries.
Each of the next q lines contains one type of query described above.
Output
For each “MAX” query, output one line in the format of “ANSWER y”, where y the value you have calculated.
Example
standard input standard output
5 6
MULTIPLY 3 5 2
MULTIPLY 2 5 3
MAX 1 5
MULTIPLY 1 4 2
MULTIPLY 2 5 5
MAX 3 5
ANSWER 1
ANSWER 2
Note
If m and n are non-zero integers, or more generally, non-zero elements of an integral domain, it is said
that m divides n if there exists an integer k, or an element k of the integral domain, such that m×k = n,
and this is written as m | n.
想了想还是纪念一下这道题吧。毕竟是在现场赛敲的线段树。
其实这道题目我们读懂,队友读的题意。给我说了说之后,我就想到用线段树维护四个值,因为四个值不多,4nlogn完全可以。类似于今年蓝桥国赛的一道题目。但是wa了两发,query函数写错了。日。这道题目就是线段树的基本的区间更新区间求最值。但是不是修改一个值,而是修改四个值。(没有提交试验所以不一定对。。)
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e5+100;
struct node{
int l;
int r;
int max5,max2,max3,max7;
int lazy5,lazy2,lazy3,lazy7;
int _max;
}p[maxx<<2];
int n,m;
inline void pushdown(int cur)
{
if(p[cur].lazy2)
{
p[cur<<1].lazy2+=p[cur].lazy2;
p[cur<<1|1].lazy2+=p[cur].lazy2;
p[cur<<1].max2+=p[cur].lazy2;
p[cur<<1|1].max2+=p[cur].lazy2;
}
if(p[cur].lazy3)
{
p[cur<<1].lazy3+=p[cur].lazy3;
p[cur<<1|1].lazy3+=p[cur].lazy3;
p[cur<<1].max3+=p[cur].lazy3;
p[cur<<1|1].max3+=p[cur].lazy3;
}
if(p[cur].lazy5)
{
p[cur<<1].lazy5+=p[cur].lazy5;
p[cur<<1|1].lazy5+=p[cur].lazy5;
p[cur<<1].max5+=p[cur].lazy5;
p[cur<<1|1].max5+=p[cur].lazy5;
}
if(p[cur].lazy7)
{
p[cur<<1].lazy7+=p[cur].lazy7;
p[cur<<1|1].lazy7+=p[cur].lazy7;
p[cur<<1].max7+=p[cur].lazy7;
p[cur<<1|1].max7+=p[cur].lazy7;
}
p[cur<<1]._max=max(max(p[cur<<1].max2,p[cur<<1].max3),max(p[cur<<1].max5,p[cur<<1].max7));
p[cur<<1|1]._max=max(max(p[cur<<1|1].max2,p[cur<<1|1].max3),max(p[cur<<1|1].max5,p[cur<<1|1].max7));
p[cur].lazy2=p[cur].lazy3=p[cur].lazy5=p[cur].lazy7=0;
}
inline void pushup(int cur)
{
p[cur].max3=max(p[cur<<1].max3,p[cur<<1|1].max3);
p[cur].max2=max(p[cur<<1].max2,p[cur<<1|1].max2);
p[cur].max5=max(p[cur<<1].max5,p[cur<<1|1].max5);
p[cur].max7=max(p[cur<<1].max7,p[cur<<1|1].max7);
p[cur]._max=max(max(p[cur].max2,p[cur].max3),max(p[cur].max5,p[cur].max7));
}
inline void build(int l,int r,int cur)
{
p[cur].l=l;
p[cur].r=r;
p[cur].max7=p[cur].max2=p[cur].max3=p[cur].max5=0;
p[cur].lazy7=p[cur].lazy2=p[cur].lazy3=p[cur].lazy5=0;
if(l==r) return ;
int mid=l+r>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
}
inline void update(int l,int r,int cur,int x2,int x3,int x5,int x7)
{
int L=p[cur].l;
int R=p[cur].r;
if(l<=L&&R<=r)
{
p[cur].lazy2+=x2;
p[cur].lazy3+=x3;
p[cur].lazy5+=x5;
p[cur].lazy7+=x7;
p[cur].max2+=x2;
p[cur].max3+=x3;
p[cur].max5+=x5;
p[cur].max7+=x7;
p[cur]._max=max(max(p[cur].max2,p[cur].max3),max(p[cur].max5,p[cur].max7));
return ;
}
pushdown(cur);
int mid=L+R>>1;
if(r<=mid) update(l,r,cur<<1,x2,x3,x5,x7);
else if(l>mid) update(l,r,cur<<1|1,x2,x3,x5,x7);
else
{
update(l,mid,cur<<1,x2,x3,x5,x7);
update(mid+1,r,cur<<1|1,x2,x3,x5,x7);
}
pushup(cur);
}
inline int query(int l,int r,int cur)
{
int L=p[cur].l;
int R=p[cur].r;
if(l<=L&&R<=r) return p[cur]._max;
pushdown(cur);
int mid=L+R>>1;
if(r<=mid) return query(l,r,cur<<1);
else if(l>mid) return query(l,r,cur<<1|1);
else return max(query(l,mid,cur<<1),query(mid+1,r,cur<<1|1));
}
int main()
{
int l,r,x;
scanf("%d%d",&n,&m);
build(1,n,1);
char s[10];
while(m--)
{
scanf("%s",s);
if(s[1]=='U')
{
scanf("%d%d%d",&l,&r,&x);
int x5=0,x2=0,x3=0,x7=0;//因为x是在2~10的,所以2~10的素数只有4个,2,3,5,7.将给定的数字分解为各个素数的乘积。
if(x==2) x2++;
else if(x==3) x3++;
else if(x==4) x2+=2;
else if(x==5) x5++;
else if(x==6) x2++,x3++;
else if(x==7) x7++;
else if(x==8) x2+=3;
else if(x==9) x3+=2;
else if(x==10) x2++,x5++;
update(l,r,1,x2,x3,x5,x7);
}
else
{
scanf("%d%d",&l,&r);
printf("ANSWER %d\n",query(l,r,1));
}
}
return 0;
}
银川结果不好不坏,虽然都说银川水,我也没感觉出哪儿水,可能是太菜了。尽自己努力了,结果不管如何也笑着接受了。acm真的结束了!!泪目
努力加油a啊,(o)/~