BZOJ - 4810 [Ynoi2017]由乃的玉米田 莫队算法 + bitset 大暴力

9 篇文章 0 订阅
5 篇文章 0 订阅

大家都很强, 可与之共勉 。

4810: [Ynoi2017]由乃的玉米田

Time Limit: 30 Sec Memory Limit: 256 MB

Description

由乃在自己的农田边散步,她突然发现田里的一排玉米非常的不美。这排玉米一共有N株,它们的高度参差不齐。
由乃认为玉米田不美,所以她决定出个数据结构题

这个题是这样的:
给你一个序列a,长度为n,有m次操作,每次询问一个区间是否可以选出两个数它们的差为x,或者询问一个区间是否可以选出两个数它们的和为x,或者询问一个区间是否可以选出两个数它们的乘积为x ,这三个操作分别为操作1
,2,3选出的这两个数可以是同一个位置的数

Input

第一行两个数n,m
后面一行n个数表示ai
后面m行每行四个数opt l r x
opt表示这个是第几种操作,l,r表示操作的区间,x表示这次操作的x
定义c为每次的x和ai中的最大值,ai >= 0,每次的x>=2n,m,c <= 100000
Output

对于每个询问,如果可以,输出yuno,否则输出yumi
Sample Input

5 5

1 1 2 3 4

2 1 1 2

1 1 2 2

3 1 1 1

3 5 5 16

1 2 3 4
Sample Output

yuno

yumi

yuno

yuno

yumi

HINT

Source

By 佚名提供

莫队的时间限制好像都尼玛很长……

这道题可以用bitset来暴力check权值是否存在

考虑直接bitset会MLE,我们用莫队来优化空间,不用每个点都开一个。

  维护一个c数组,表示每种值的出现次数,那么我可以用bitset维护每种权值是否出现过。

  考虑乘法操作的话,直接利用cnt数组,每次暴枚所有因子,check一下就好了;

  对于减法操作,相当于是查询是否存在i,j满足a[i]−a[j]=xa[i]−a[j]=x的形式,那么bitset右移x位与原来的&一下看新的二进制数是否为00(用bitset自带的count就好了);
此处的右移相当于是做减法
  加法类似,不过需要维护的是a[i]+a[j]=x的形式,那么我用c(c是所有数中的最大值)去减a[j],即变成a[i]=(c−a[j])+x−c的形式,另外维护一个bitset,右移(c−x)位查询就好了。所以总复杂度就是O(n√n+n^2/32)。
  

# include <cmath>
# include <bitset>
# include <cctype>
# include <cstdio>
# include <algorithm>

const int N = 200005 ;

std :: bitset < 100005 > A, B, T ;

int n, m ;
int a [N] ;
int cnt ( 100005 ) , b [N] ;
int c [N] ;
int pos [N], sq ;
bool answer [N << 2] ;

struct Query  {
    int opt, l, r, x, id ;
    inline short operator < ( const Query& rhs )  const  {
        return ( pos [l] < pos [rhs.l] ) || ( pos [l] == pos [rhs.l] && r < rhs.r ) ;
    }
} q [N] ;

inline void Update ( int x, int delta )  {
    c [a [x]] += delta ;
    if ( delta ^ -1 )  {
        if ( c [a [x]] == 1 )  A [a [x]] = B [b [x]] = 1 ;
        return ;
    }
    if ( ! c [a [x]] )  A [a [x]] = B [b [x]] = 0 ;
}

inline bool Query ( Query& Q )  {
    if ( Q.opt == 1 )  {
            T = A ;  // a [i] - a[j] == x ;
            T >>= Q.x ;
            T &= A ;
            if ( T.count ( ) )  return true ;
            return false ;
    }
    if ( Q.opt == 2 )  {
        T = B ;
        T >>= ( cnt - Q.x ) ;  // a [i] + a[j] == x ;
        T &= A ;
        if ( T.count ( ) )  return true ;
        return false ;
    }
    int cur = sqrt ( Q.x + 1 ) ;
    for ( int i = 1 ; i <= cur ; ++ i )
        if ( Q.x % i == 0 && c [i] && c [Q.x / i] )  return true ;
    return false ;
}

void Solve ( )  {
    for( register int i = 1, l = 1, r = 0; i <= m ; ++ i )  {
        while ( r < q [i].r )  
            Update ( ++ r, 1 ) ;
        while ( r > q [i].r )
            Update ( r --, -1 ) ;
        while ( l < q [i].l )
            Update ( l ++, -1 ) ;
        while ( l > q [i].l )
            Update ( -- l, 1 ) ;
        answer [q [i].id] = Query ( q [i] ) ;
    }
}

int main ( )  {
    scanf ( "%d%d", & n, & m ) ;
    for ( int i = 1 ; i <= n ; ++ i )   scanf ( "%d", a + i ) ;
    for ( int i = 1 ; i <= n ; ++ i )   b [i] = cnt - a [i] ;
    sq = sqrt ( n ) ;
    for ( int i = 1 ; i <= n ; ++ i )   pos [i] = ( i - 1 ) / sq + 1 ;
    for ( int i = 1 ; i <= m ; ++ i )  {
        scanf ( "%d%d%d%d", & q [i].opt, & q [i].l, & q [i].r, & q [i].x ) ;
        q [i].id = i ;
    }
    std :: sort ( q + 1, q + 1 + m ) ;
    Solve ( ) ;
    for ( int i = 1 ; i <= m ; ++ i )
        puts ( answer [i] ? "yuno" : "yumi" ) ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值