首页 > 学院 > 开发设计 > 正文

中国(北方)大学生程序设计训练赛(第一周)(Problem F: 等差区间-线段树+等差数列平方和公式)

2019-11-06 06:04:36
字体:
来源:转载
供稿:网友

对一长度为105的静态序列进行q次询问,每次询问某段区间元素是否排序后能成为等差数列

正解: 考虑2种情况 公差为0, 公差不为0,此时求区间gcd,区间内无相同元素 且 相邻两项差构成的数列的GCD *(R-L) = (区间最大值-区间最小值)

#include <iostream>#include <cmath>#include <algorithm>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <map>#include <functional>#include <cstdlib>#include <queue>#include <stack>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=PRe[x];p;p=Next[p])#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,0x3f,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define MEMx(a,b) memset(a,b,sizeof(a));#define INF (0x3f3f3f3f)#define F (1000000007)#define pb push_back#define mp make_pair #define fi first#define se second#define vi vector<int> #define pi pair<int,int>#define SI(a) ((a).size())#define Pr(kcase) printf("Case %d:/n",kcase);#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;#define PRi2D(a,n,m) For(i,n) { / For(j,m-1) cout<<a[i][j]<<' ';/ cout<<a[i][m]<<endl; / } #pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef long double ld;typedef unsigned long long ull;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return ((a-b)%F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}int read(){ int 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 x*f;} const int maxn =1000000;ull sum[maxn],a[maxn],minv[maxn],maxv[maxn],a2[maxn],sum2[maxn];void pushup(int o) { sum[o]=sum[Lson]+sum[Rson]; sum2[o]=sum2[Lson]+sum2[Rson]; maxv[o]=max(maxv[Lson],maxv[Rson]); minv[o]=min(minv[Lson],minv[Rson]);}void build(int l,int r,int o) { if (l==r) { minv[o]=maxv[o]=sum[o]=a[l]; sum2[o]=(ull)a[l]*a[l]; return ; } int m=(l+r)>>1; build(l,m,Lson),build(m+1,r,Rson); pushup(o);}ull mi1,ma1,s1,s2;void query(int l,int r,int o,int L,int R) { if(L<=l && r<=R ) { mi1=min(mi1,minv[o]); ma1=max(ma1,maxv[o]); s1+=sum[o]; s2+=sum2[o]; return; } int m=(l+r)>>1; if(L<=m) query(l,m,Lson,L,R); if(m<R) query(m+1,r,Rson,L,R); }int main(){// freopen("F.in","r",stdin);// freopen(".out","w",stdout); int n,q; while(scanf("%d%d",&n,&q)==2) { For(i,n) a[i]=read(); build(1,n,1); For(i,q) { int l=read(),r=read(); mi1=INF,ma1=0,s1=s2=0; query(1,n,1,l,r); ull len=r-l+1; bool flag=0; if (len==1) flag=1; else { if ((ma1-mi1)%(len-1)!=0) flag=0; else { ull d=ma1-mi1; d/=len-1; ull ss1=(mi1+ma1)*len; if (ss1!=s1*2) {flag=0;} else { ull ss2=len*mi1*mi1; ss2+=len*(len-1)/2*(2*len-1)/3*d*d; ss2+=len*(len-1)*d*mi1; if (ss2==s2) flag=1;else flag=0; } } } puts(flag? "Yes":"No"); } } return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表