AC's String HDU3973

55 篇文章 0 订阅
22 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=3973

线段树维护HASH

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>
#include <bitset>
#include <iomanip>
//#pragma comment(linker, "/STACK:102400000,102400000")

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
using std::ios;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<ULL, ULL> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;

const int MAXN(100010);
const int MAXM(310);
const int MAXE(310);
const int MAXK(6);
const int HSIZE(131313);
const int SIGMA_SIZE(26);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> inline void checkmax(T &a, T b){if(b > a) a = b;}
template<typename T> inline void checkmin(T &a, T b){if(b < a) a = b;}
template<typename T> inline T ABS(const T &a){return a < 0? -a: a;}

struct NODE
{
    ULL val;
    int key;
    NODE *ch[2];
    void init(ULL v, NODE *c)
    {
        val = v;
        ch[0] = ch[1] = c;
        key = rand();
    }
};

struct TREAP
{
    NODE pool[10010], *rear, *root, *NIL;
    void rot(NODE *&rt, int f)
    {
        NODE *t = rt->ch[f];
        rt->ch[f] = t->ch[!f];
        t->ch[!f] = rt;
        rt = t;
    }
    void insert(NODE *&rt, ULL x)
    {
        if(rt == NIL)
        {
            rt = rear++;
            rt->init(x, NIL);
            return;
        }
        int f = x > rt->val;
        insert(rt->ch[f], x);
        if(rt->ch[f]->key < rt->key) rot(rt, f);
    }
    void Delete(NODE *&rt, ULL x)
    {
        if(rt == NIL) return;
        if(rt->val == x)
        {
            int f = rt->ch[1]->key < rt->ch[0]->key;
            if(rt->ch[f] == NIL)
            {
                rt = NIL;
                return;
            }
            rot(rt, f);
            Delete(rt->ch[!f], x);
        }
        else
            Delete(rt->ch[x>rt->val], x);
    }
    NODE *search(NODE *rt, ULL x)
    {
        if(rt == NIL) return NIL;
        if(rt->val == x) return rt;
        return search(rt->ch[x > rt->val], x);
    }    
    void init()
    {
        root = NIL = pool;
        NIL->key = INFI;
        rear = pool+1;
    }
    void ins(ULL x){insert(root, x);}
    NODE *sea(ULL x){return search(root, x);}
    void del(ULL x){Delete(root, x);}
} trp;

char opt[5];
char str[2000010];
ULL XP[MAXN];

struct SEG_TREE
{
    ULL H[MAXN << 2];
    inline int lson(int rt){return rt << 1;}
    inline int rson(int rt){return (rt << 1)|1;}
    void build(int l, int r, int rt)
    {
        if(l == r)
        {
            H[rt] = str[l]-'a'+1;
            return;
        }
        int m = (l+r) >> 1;
        build(l, m, lson(rt));
        build(m+1, r, rson(rt));
        H[rt] = H[lson(rt)]*XP[r-m]+H[rson(rt)];
    }
    void updata(int l, int r, int rt, int qi, int qv)
    {
        if(l == r)
        {
            H[rt] = qv;
            return;
        }
        int m = (l+r) >> 1;
        if(qi <= m) updata(l, m, lson(rt), qi, qv);
        else updata(m+1, r, rson(rt), qi, qv);
        H[rt] = H[lson(rt)]*XP[r-m]+H[rson(rt)];
    }
    ULL query(int l, int r, int rt, int ql, int qr)
    {
        if(ql <= l && qr >= r) return H[rt];
        int m = (l+r) >> 1;
        if(qr <= m) return query(l, m, lson(rt), ql, qr);
        if(ql > m) return query(m+1, r, rson(rt), ql, qr);
        return query(l, m, lson(rt), ql, m)*XP[qr-m]+query(m+1, r, rson(rt), m+1, qr);
    }
} sgt;

int main()
{
    srand(time(0));
    XP[0] = 1;
    for(int i = 1; i <= 100000; ++i) XP[i] = XP[i-1]*BASE; 
    int TC, n_case(0);
    scanf("%d", &TC);
    while(TC--)
    {
        printf("Case #%d:\n", ++n_case);
        int n;
        scanf("%d", &n);
        ULL temp;
        trp.init();
        for(int i = 0; i < n; ++i)
        {
            scanf("%s", str);
            temp = 0;
            for(char *sp = str; *sp; ++sp) temp = temp*BASE+*sp-'a'+1;
            trp.ins(temp);
        }
        scanf("%s", str);
        int len = strlen(str);
        sgt.build(0, len-1, 1);
        int m;
        scanf("%d", &m);
        for(int i = 0; i < m; ++i)
        {
            int l, r;
            scanf("%s", opt);
            if(opt[0] == 'Q')
            {
                scanf("%d%d", &l, &r);
                temp = sgt.query(0, len-1, 1, l, r);
                if(trp.sea(temp) != trp.NIL) printf("Yes\n");
                else printf("No\n");
            }
            else
            {
                char c;
                scanf("%d %c", &l, &c);
                sgt.updata(0, len-1, 1, l, c-'a'+1);
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值