fork download
  1. // ~~ icebear ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define BIT(S, i) (((S) >> (i)) & 1)
  27. #define mp make_pair
  28. #define pb push_back
  29. #define fi first
  30. #define se second
  31. #define all(x) x.begin(), x.end()
  32. #define task "anctext"
  33. /*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN VOI26 */
  34.  
  35. const int MOD = 1e9 + 2277;
  36. const int inf = 1e9 + 27092008;
  37. const ll INF = 1e18 + 27092008;
  38. const int N = 5e5 + 5;
  39. const int base = 256;
  40. string str;
  41. int numStr, lenStr, numQuery, power[N], hs[N];
  42. int sorted[N], numDiff;
  43.  
  44. struct Node {
  45. int depth, end_index, hashValue;
  46. Node *child[26], *par[20];
  47. char current_char;
  48. Node() {
  49. depth = hashValue = 0;
  50. end_index = -1;
  51. REP(j, 20) par[j] = NULL;
  52. REP(j, 26) child[j] = NULL;
  53. current_char = '?';
  54. }
  55. } *end_node[N];
  56.  
  57. struct Trie {
  58. Node *root;
  59. Trie () {
  60. root = new Node();
  61. REP(j, 20) root -> par[j] = root;
  62. end_node[0] = root;
  63. }
  64.  
  65. void addStr(Node *p, int index, string &s) {
  66. for(char c : s) {
  67. int pos = c - 'a';
  68. if (p -> child[pos] == NULL) {
  69. p -> child[pos] = new Node();
  70.  
  71. p -> child[pos] -> hashValue = (1LL * (p -> hashValue) * base + c) % MOD;
  72. p -> child[pos] -> depth = p -> depth + 1;
  73. p -> child[pos] -> current_char = c;
  74.  
  75. p -> child[pos] -> par[0] = p;
  76. FOR(j, 1, 19) p -> child[pos] -> par[j] = (p -> child[pos] -> par[j - 1]) -> par[j - 1];
  77. }
  78. p = p -> child[pos];
  79. }
  80.  
  81. if (p -> end_index == -1) p -> end_index = index;
  82. end_node[index] = p;
  83. }
  84.  
  85. void DFS(Node *p) {
  86. if (p -> end_index != -1) sorted[++numDiff] = p -> end_index;
  87. REP(pos, 26) if (p -> child[pos] != NULL)
  88. DFS(p -> child[pos]);
  89. }
  90. } trie;
  91.  
  92. int getHash(int l, int r) {
  93. return (hs[r] - 1LL * hs[l - 1] * power[r - l + 1] % MOD + MOD) % MOD;
  94. }
  95.  
  96. bool check(int pos, int L, int R) {
  97. Node *p = end_node[pos];
  98. int lenS = R - L + 1;
  99. if (p -> depth <= lenS && p -> hashValue == getHash(L, L + p -> depth - 1)) return true;
  100.  
  101. RED(j, 20) {
  102. int lenT = p -> par[j] -> depth;
  103. if (lenT > lenS || ((p -> par[j] -> hashValue) != getHash(L, L + lenT - 1))) p = p -> par[j];
  104. }
  105.  
  106. return (p -> depth <= lenS && p -> current_char < str[L + p -> depth - 1]);
  107. }
  108.  
  109. void init(void) {
  110. int subtask; cin >> subtask;
  111. cin >> str >> numStr;
  112. lenStr = (int)str.size();
  113. str = " " + str;
  114. power[0] = 1;
  115. FOR(i, 1, lenStr) {
  116. hs[i] = (1LL * hs[i - 1] * base + str[i]) % MOD;
  117. power[i] = 1LL * power[i - 1] * base % MOD;
  118. }
  119. }
  120.  
  121. void process(void) {
  122. FOR(i, 1, numStr) {
  123. int parent; string more;
  124. cin >> parent >> more;
  125. trie.addStr(end_node[parent], i, more);
  126. }
  127.  
  128. trie.DFS(trie.root);
  129.  
  130. cin >> numQuery;
  131. while(numQuery--) {
  132. int l, r;
  133. cin >> l >> r;
  134. int low = 1, high = numDiff, res = -1;
  135. while(low <= high) {
  136. int mid = (low + high) >> 1;
  137. if (check(sorted[mid], l, r)) res = sorted[mid], low = mid + 1;
  138. else high = mid - 1;
  139. }
  140. cout << res << '\n';
  141. }
  142. }
  143.  
  144. int main() {
  145. ios_base::sync_with_stdio(0);
  146. cin.tie(0); cout.tie(0);
  147. if (fopen(task".inp", "r")) {
  148. freopen(task".inp", "r", stdin);
  149. freopen(task".out", "w", stdout);
  150. }
  151. int tc = 1;
  152. // cin >> tc;
  153. while(tc--) {
  154. init();
  155. process();
  156. }
  157. return 0;
  158. }
  159.  
Success #stdin #stdout 0.01s 5552KB
stdin
Standard input is empty
stdout
Standard output is empty