Bzoj 3207: Flower God's taunt plan Ⅰ (hash + persistent segment tree)

Source: Internet
Author: User

O (NK) brute force a hash of all the substrings, and then discretization of the hash value line tree, is the Chairman tree Classic practice. Total time Complexity O (nk+ (n+q) logn)

--------------------------------------------------------------------------

#include <cstdio>#include <algorithm>#include <cstring>#include <cctype>using namespace std;typedef unsigned long long ull;const int MAXN = 100009;const ULL P = 1000000007ULL;inline int Read () {char C = getchar ();int ret = 0;For (;!isdigit (c); c = GetChar ());For (; IsDigit (c); c = GetChar ())ret = ret * + C-' 0 ';return ret;}int SEQ[MAXN], R[MAXN];int N, Q, K, N;ull H[MAXN], HASH[MAXN];void Init () {N = read (); Q = Read (); K = Read ();for (int i = 1; I <= N; i++)Seq[i] = read ();n = n-k + 1;for (int i = 1; I <= n; i++) {H[i] = 0;For (int j = 0; J < K; J + +)(H[i] *= P) + = Seq[i + j];Hash[i] = h[i];}sort (H + 1, H + n + 1);for (int i = 1; I <= n; i++)R[i] = Lower_bound (H + 1, H + N + 1, hash[i])-H;}struct Node {Node *l, *r;int cnt;} pool[5000000], *pt, *ROOT[MAXN], *null;void Init_sgt () {pt = pool;Null = pt++;null->l = Null->r = Null;}int Val;node* Modify (node* t, int l, int r) {node* h = pt++;if (L = = r)h->cnt = t->cnt + 1;else {int m = (L + r) >> 1;if (Val <= m) {h->l = Modify (T->l, L, m);h->r = t->r;} else {h->l = t->l;h->r = Modify (t->r, M + 1, R);}}return h;}int Query (node* t, int l, int r) {if (L = = r)return t->cnt;int m = (L + r) >> 1;if (Val <= m)return Query (T->l, L, m);return Query (T->r, M + 1, R);}void Solve () {Init_sgt ();root[0] = Null;for (int i = 1; I <= n; i++) {Val = r[i];Root[i] = Modify (Root[i-1], 1, n);}While (q--) {int L = Read (), r = Read ()-K + 1;ull hash_val = 0;for (int i = 0; i < K; i++)(Hash_val *= P) + = Read ();Val = Lower_bound (H + 1, H + N + 1, hash_val)-H;if (L > R | | H[val]! = hash_val) {puts ("Yes");continue;}puts (query (Root[r], 1, N) > Query (Root[l-1], 1, N)? "No": "Yes");}}int main () {Init ();Solve ();return 0;}

--------------------------------------------------------------------------

3207: Flower God's taunt plan Ⅰ time limit: ten Sec Memory Limit: MB
Submit: 1132 Solved: 412
[Submit] [Status] [Discuss] Description background Flower God is God, a big hobby is to taunt Big J, for example: "Hey you silly not silly!" "HQZ: Big Stupid J" "" This problem has been J excrement!! "J how this program runs so fast!" J's going to hit the counter! ”...... Describe the day the DJ is giving us all the Konjac konjac topic, the flower God on the side to do the bored, ran to the side with our Konjac Konjac together listen. The following is a partial excerpt: 1. " J What are you talking about! "I'm talking about xxx!. "Ah you silly not silly!" So troublesome, direct xxx and XXX will be good! "" ... "2." J, have you spoken to xxx yet? "" "..." "and that's what you're talking about?" Hey, you're not stupid! "" ... "the DJ was very silent about the situation, and the DJ was very embarrassed when it happened. After the Konjac Konjac study, DJ before the lecture there will be a length of n program, we can think of it as a sequence; Similarly, the flower God will have a taunt before the lecture, there are M, each time in the X to Y to start the taunt, in order to reduce the difficulty, the length of each taunt program is certain, for K. The flower God taunt DJ makes the DJ awkward in need of conditions: in X~y time the DJ did not talk about the flower God's taunt scheme, that is, J's lecture plan x~y no flower God's taunt scheme "so that the flower God will taunt J will not say." After the public Konjac Konjac efforts, before a lecture to get the flowers God taunt each of the programs, DJ learned this news after Joy Unceasingly, DJ want to know whether every taunt of flower God will let DJ embarrassed "say no words to". Input 1th row 3 number n,m,k, 2nd row n number, meaning above, 3rd line to 3+m-1 line, each line k+2 number, first two number is X, y, then K number, meaning above; Outputfor every taunt to make a reply will be embarrassed output ' Yes ' otherwise output ' No 'Sample Input8 5 3
1 2 3 4 5 6 7 8
2 5 2) 3 4
1 8 3) 2 1
5 7 4) 5 6
2 5 1) 2 3
1 7 3) 4 5
Sample OutputNo
Yes
Yes
Yes
No
HINT

All data in the problem is not more than 2*10^9; ensure that each digit of the scheme sequence <=n
There are 2 3 4 schemes in the 2~5, output no, which means the DJ won't be embarrassed
There is no 3 2 1 scheme in 1~8, output Yes, which means the DJ will be embarrassed
There is no 4 5 6 scheme in 5~7, output Yes, which means the DJ will be embarrassed
There is no 1 2 3 scheme in 2~5, output Yes, which means the DJ will be embarrassed
There are 3 4 5 schemes in the 1~7, output no, which means the DJ won't be embarrassed

Source

Original Memphis

Bzoj 3207: Flower God's taunt plan Ⅰ (hash + persistent segment tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.