码迷,mamicode.com
首页 > 其他好文 > 详细

CF1320D Reachable Strings [字符串哈希]

时间:2020-03-04 18:57:45      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:unsigned   std   偶数   name   for   %s   double   min   each   

我们发现0是不能跨越区间的,且奇偶性不变,那么我们就分类讨论一下左端点开始的位置
奇数位当0处理,偶数位当1处理QAQ

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define Tp template
using pii = pair<int, int>;
#define fir first
#define sec second
Tp<class T> void cmax(T& x, const T& y) {if (x < y) x = y;} Tp<class T> void cmin(T& x, const T& y) {if (x > y) x = y;}
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
Tp<class T> void sort(vector<T>& v) { sort(all(v)); } Tp<class T> void reverse(vector<T>& v) { reverse(all(v)); }
Tp<class T> void unique(vector<T>& v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
const int SZ = 1 << 23 | 233;
struct FILEIN { char qwq[SZ], *S = qwq, *T = qwq, ch;
#ifdef __WIN64
#define GETC getchar
#else
  char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
#endif
  FILEIN& operator>>(char& c) {while (isspace(c = GETC()));return *this;}
  FILEIN& operator>>(string& s) {while (isspace(ch = GETC())); s = ch;while (!isspace(ch = GETC())) s += ch;return *this;}
  Tp<class T> void read(T& x) { bool sign = 0;while ((ch = GETC()) < 48) sign ^= (ch == 45); x = (ch ^ 48);
    while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48); x = sign ? -x : x;
  }FILEIN& operator>>(int& x) { return read(x), *this; } FILEIN& operator>>(ll& x) { return read(x), *this; }
} in;
struct FILEOUT {const static int LIMIT = 1 << 22 ;char quq[SZ], ST[233];int sz, O;
  ~FILEOUT() { flush() ; }void flush() {fwrite(quq, 1, O, stdout); fflush(stdout);O = 0;}
  FILEOUT& operator<<(char c) {return quq[O++] = c, *this;}
  FILEOUT& operator<<(string str) {if (O > LIMIT) flush();for (char c : str) quq[O++] = c;return *this;}
  Tp<class T> void write(T x) {if (O > LIMIT) flush();if (x < 0) {quq[O++] = 45;x = -x;}
        do {ST[++sz] = x % 10 ^ 48;x /= 10;} while (x);while (sz) quq[O++] = ST[sz--];
  }FILEOUT& operator<<(int x) { return write(x), *this; } FILEOUT& operator<<(ll x) { return write(x), *this; }
} out;
#define int long long

mt19937 rnd(chrono :: steady_clock :: now().time_since_epoch().count()) ;

const int maxn = 2e5 + 52 ;
const int bs = rnd() % 19260817 | 3 ;
const int mod = 1213153189 ;
int n ;
char a[maxn] ;
int p[maxn] ;
int cnt[maxn] , h1[maxn] , h2[maxn] ;
int qry(int l , int r) {
    if(l & 1) 
        return (h1[r] - h1[l - 1] * p[cnt[r] - cnt[l - 1]] % mod + mod) % mod ;
    else
        return (h2[r] - h2[l - 1] * p[cnt[r] - cnt[l - 1]] % mod + mod) % mod ;
}

signed main() {
  // code begin.
    scanf("%lld" , & n) ;
    scanf("%s" , a + 1) ;
    p[0] = 1 ;
    for(int i = 1 ; i <= n ; i ++) {
        p[i] = p[i - 1] * bs % mod ;
        cnt[i] = cnt[i - 1] + (a[i] == '0') ;
        if(a[i] == '0') {
            h1[i] = (h1[i - 1] * bs + (i & 1) + 1) % mod ;
            h2[i] = (h2[i - 1] * bs + (i & 1 ^ 1) + 1) % mod ;
        }
        else {
            h1[i] = h1[i - 1] ;
            h2[i] = h2[i - 1] ;
        }
    }
    int q ;
    scanf("%lld" , & q) ;
    while(q --) {
        int l , r , len ;
        scanf("%lld %lld %lld" , & l , & r , & len) ;
        if(qry(l , l + len - 1) == qry(r , r + len - 1)) 
            out << "Yes" << '\n' ;
        else 
            out << "No" << '\n' ;
    }
    return 0;
  // code end.
}

CF1320D Reachable Strings [字符串哈希]

标签:unsigned   std   偶数   name   for   %s   double   min   each   

原文地址:https://www.cnblogs.com/Isaunoya/p/12411331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!