ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

D. Bash and a Tough Math Puzzle(思维+线段树+剪枝)

2021-04-13 18:02:01  阅读:150  来源: 互联网

标签:剪枝 cnt Tough LL tree gcd% return include Bash


https://codeforces.com/problemset/problem/914/D


思路:

考虑如何快速判断gcd[l,r]%x是否可以修改一个数得出。如果答案满足,那么其必然是有一个不是x的倍数,或者都是x的倍数。因为最多只能将一个不是x的倍数改成x。或者将都是x的倍数其中一个改成最小的x就是了。

如果这样其实还是暴力了。如果一段区间的gcd是x的倍数,说明这段区间就可以return了。如果不是,看这段区间的左儿子和右儿子,如果两个%x!=0,那么至少要cnt=2;不然就搜其中一个儿子,

那么需要递归到叶子节点看有几个不是x的倍数,cnt++。

同时dfs的过程中设一个全局的flag反应当前是否已经有cnt>=2,有的话直接return完就好了。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e5+1000;
typedef long long LL;
inline 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-48;ch=getchar();}
return x*f;}
struct Tree{
   LL l,r,gcd;
}tree[maxn*4];
void push_up(LL p){
     tree[p].gcd=__gcd(tree[p*2].gcd,tree[p*2+1].gcd);
}
void build(LL p,LL l,LL r){
     tree[p].l=l;tree[p].r=r;tree[p].gcd=0;
     if(l==r){LL x;cin>>x;tree[p].gcd=x;return;}
     LL mid=(tree[p].l+tree[p].r)>>1;
     build(p*2,l,mid);build(p*2+1,mid+1,r);
     push_up(p);
}
void modify(LL p,LL l,LL r,LL d){
     if(l<=tree[p].l&&r>=tree[p].r){
        tree[p].gcd=d;
        return;
     }
     LL mid=(tree[p].l+tree[p].r)>>1;
     if(l<=mid) modify(p*2,l,r,d);
     if(r>mid) modify(p*2+1,l,r,d);
     push_up(p);
}
void query(LL p,LL l,LL r,LL x,LL& cnt){
     if(cnt>=2) return;
     if(tree[p].l==tree[p].r){
        if(tree[p].gcd%x){cnt++;return;}
        return;
     }
     if(l<=tree[p].l&&r>=tree[p].r){///需要递归到叶子节点
          if(tree[p].gcd%x==0) return;
          if(tree[p*2].gcd%x!=0&&tree[p*2+1].gcd%x!=0) {
                cnt+=2;
                return;
          }
          if(tree[p*2].gcd%x!=0) query(p*2,l,r,x,cnt);
          if(tree[p*2+1].gcd%x!=0) query(p*2+1,l,r,x,cnt);
          return;
     }
     LL mid=(tree[p].l+tree[p].r)>>1;
     if(l<=mid) query(p*2,l,r,x,cnt);
     if(r>mid)  query(p*2+1,l,r,x,cnt);
     return;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   build(1,1,n);
   LL q;cin>>q;
   while(q--){
      LL op;cin>>op;
      if(op==1){
         LL l,r,x;cin>>l>>r>>x;
         LL cnt=0;
         query(1,l,r,x,cnt);
         if(cnt>=2){
            cout<<"NO"<<"\n";
         }
         else if(cnt==0||cnt==1){
            cout<<"YES"<<"\n";
         }
      }
      else if(op==2){
         LL i,y;cin>>i>>y;
         modify(1,i,i,y);
      }
   }
   return 0;
}

 

标签:剪枝,cnt,Tough,LL,tree,gcd%,return,include,Bash
来源: https://blog.csdn.net/zstuyyyyccccbbbb/article/details/115674310

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有