Bash and a Tough Math Puzzle
可以过100的数据
但是50w就过不了了,具体不明,没看过题解.
没加第二种情况的返回值..
#include <iostream>
#include <vector>
#include <cstdio>
#include <stack>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <set>
using namespace std;
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
const int MAXN = 5e5+17;
int st[MAXN*10];
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
void layback(int rt)
{
st[rt] = gcd(st[rt<<1] ,st[rt<<1|1]);
}
void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&st[rt]);
return ;
}
int m = (l+r)/2;
build(lson);
build(rson);
layback(rt);
}
void update(int l,int r,int rt,int p,int cg)
{
if(l==r)
{
st[rt]=cg;
return ;
}
int m = (l+r)/2;
if(p<=m) update(lson,p,cg);
else update(rson,p,cg);
layback(rt);
}
bool can = true;
bool query(int l,int r,int rt,int x,int y,int gd)
{
//debug(rt);
if(!can) return false;
int m = (l+r)/2;
if(l>=x&&r<=y)
{
if(st[rt]%gd==0)
return true;
else
{
bool lf = true,rg = true;
if(x<=m)
lf = query(lson,x,y,gd);
if(y>=m+1)
rg = query(rson,x,y,gd);
if(lf==false&&rg==false)
can = false;
return false;
}
}
else
{
bool lf = true,rg = true;
if(x<=m)
lf = query(lson,x,y,gd);
if(y>=m+1)
rg = query(rson,x,y,gd);
if(lf==false&&rg==false)
can = false;
}
}
int main(int argc ,char const *argv[])
{
#ifdef GoodbyeMonkeyKing
freopen("Input.txt","r",stdin);freopen("Output.txt","w",stdout);
#endif
int n;
cin>>n;
build(1, n, 1);
int q;
cin>>q;
while(q--)
{
int cmd;
cin>>cmd;
int l,r,c;
if(cmd==1)
{
can = true;
cin>>l>>r>>c;
query(1, n, 1, l, r,c);
if(can)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
else
{
cin>>l>>r;
update(1, n, 1, l,r );
}
}
return 0;
}
总算过了..
一个线段树..
可能并不算难把..
#include <iostream>
#include <vector>
#include <cstdio>
#include <stack>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <set>
using namespace std;
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
const int MAXN = 5e5+17;
int st[MAXN*10];
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
void layback(int rt)
{
st[rt] = gcd(st[rt<<1] ,st[rt<<1|1]);
}
void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&st[rt]);
return ;
}
int m = (l+r)/2;
build(lson);
build(rson);
layback(rt);
}
void update(int l,int r,int rt,int p,int cg)
{
if(l==r)
{
st[rt]=cg;
return ;
}
int m = (l+r)/2;
if(p<=m) update(lson,p,cg);
else update(rson,p,cg);
layback(rt);
}
bool can = true;
bool query(int l,int r,int rt,int x,int y,int gd)
{
if(!can) return false;
int m = (l+r)/2;
if(l>=x&&r<=y)
{
if(st[rt]%gd==0)
{
return true;
}
else
{
bool lf = true,rg = true;
if(x<=m)
lf = query(lson,x,y,gd);
if(y>=m+1)
rg = query(rson,x,y,gd);
if(lf==false&&rg==false)
can = false;
return false;
}
}
else
{
bool lf = true,rg = true;
if(x<=m)
lf = query(lson,x,y,gd);
if(y>=m+1)
rg = query(rson,x,y,gd);
if(lf==false&&rg==false)
can = false;
if(lf==false||rg==false)
return false;
else
return true;
}
}
int main(int argc ,char const *argv[])
{
#ifdef GoodbyeMonkeyKing
freopen("Input.txt","r",stdin);freopen("Output.txt","w",stdout);
#endif
int n;
cin>>n;
build(1, n, 1);
int q;
cin>>q;
while(q--)
{
int cmd;
scanf("%d",&cmd);
int l,r,c;
if(cmd==1)
{
can = true;
scanf("%d%d%d",&l,&r,&c);
query(1, n, 1, l, r,c);
if(can)
puts("YES");
else
puts("NO");
}
else
{
scanf("%d%d",&l,&r);
update(1, n, 1, l,r );
}
}
return 0;
}