标签:man contains other diff query static cos set lock
Time Limit: 2000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 858 Accepted Submission(s): 256
题目链接:HDU 5790
就是问你[L,R]中的字符串的前缀一共有多少种,那么我们可以把每一个字符串的前缀标号,然后记录这种字符串有几种前缀并更新到主席树上,每一次问[L,R]就变成询问[presum_kind[L-1]+1, presum_kind[R]]之间有几个不同的前缀标号,比如题目样例给前缀标号,就是[1,2,3][1,2,4][5,6,7]。
代码:
#include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f #define LC(ql) (ql<<1) #define RC(ql) ((ql<<1)+1) #define MID(ql,qr) ((ql+qr)>>1) #define fin(name) freopen(name,"r",stdin) #define fout(name) freopen(name,"w",stdout) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const int N = 100010; struct Trie { int nxt[26]; int id; void reset() { fill(nxt, nxt + 26, 0); id = 0; } }; struct seg { int lson, rson; int cnt; void reset() { lson = rson = 0; cnt = 0; } }; Trie L[N]; seg T[N * 36]; int sz, tot, tid, arr[N], Tot; int lenth[N], last[N], root[N]; char s[N]; void init() { sz = 1; tot = 0; tid = 0; Tot = 0; CLR(last, 0); L[0].reset(); } void update(int &cur, int ori, int l, int r, int pos, int flag) { cur = ++tot; T[cur] = T[ori]; T[cur].cnt += flag; if (l == r) return ; int mid = MID(l, r); if (pos <= mid) update(T[cur].lson, T[ori].lson, l, mid, pos, flag); else update(T[cur].rson, T[ori].rson, mid + 1, r, pos, flag); } int query(int S, int E, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return T[E].cnt - T[S].cnt; else { int ret = 0; int mid = MID(l, r); if (ql <= mid) ret += query(T[S].lson, T[E].lson, l, mid, ql, qr); if (qr > mid) ret += query(T[S].rson, T[E].rson, mid + 1, r, ql, qr); return ret; } } int insert(int id, char s[]) { int len = strlen(s); int u = 0; for (int i = 0; i < len; ++i) { int v = s[i] - ‘a‘; if (!L[u].nxt[v]) { L[sz].reset(); L[u].nxt[v] = sz++; } u = L[u].nxt[v]; if (L[u].id == 0) L[u].id = ++tid; arr[++Tot] = L[u].id; } lenth[id] = Tot; return len; } int main(void) { int n, m, i, l, r; while (~scanf("%d%d", &n, &m)) { init(); for (i = 0; i < n; ++i) { scanf("%s", s); insert(i, s); } for (i = 1; i <= Tot; ++i) { if (!last[arr[i]]) update(root[i], root[i - 1], 1, Tot, i, 1); else { int tmp; update(tmp, root[i - 1], 1, Tot, last[arr[i]], -1); update(root[i], tmp, 1, Tot, i, 1); } last[arr[i]] = i; } scanf("%d", &m); int ans = 0; while (m--) { int L, R; scanf("%d%d", &L, &R); l = min((L + ans) % n, (R + ans) % n); r = max((L + ans) % n, (R + ans) % n); L = l - 1 >= 0 ? lenth[l - 1] : 0; R = lenth[r]; printf("%d\n", ans = query(root[L], root[R], 1, Tot, L + 1, R)); } } return 0; }
标签:man contains other diff query static cos set lock
原文地址:http://www.cnblogs.com/Blackops/p/7228484.html