fork download
  1. /*
  2. * @Author: hungeazy
  3. * @Date: 2025-11-11 14:07:38
  4. * @Last Modified by: hungeazy
  5. * @Last Modified time: 2025-11-11 14:32:09
  6. */
  7. #include <bits/stdc++.h>
  8. #include <ext/pb_ds/assoc_container.hpp>
  9. #include <ext/pb_ds/tree_policy.hpp>
  10. // #pragma GCC optimize("O3")
  11. // #pragma GCC optimize("unroll-loops")
  12. // #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
  13. using namespace std;
  14. using namespace __gnu_pbds;
  15. bool M1;
  16. #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  17. #define int long long
  18. #define ll long long
  19. #define ull unsigned long long
  20. #define sz(x) x.size()
  21. #define sqr(x) (1LL * (x) * (x))
  22. #define all(x) x.begin(), x.end()
  23. #define fill(f,x) memset(f,x,sizeof(f))
  24. #define FOR(i,l,r) for(int i=l;i<=r;i++)
  25. #define FOD(i,r,l) for(int i=r;i>=l;i--)
  26. #define debug(x) cout << #x << " = " << x << '\n'
  27. #define ii pair<int,int>
  28. #define iii pair<int,ii>
  29. #define di pair<ii,ii>
  30. #define vi vector<int>
  31. #define vii vector<ii>
  32. #define mii map<int,int>
  33. #define fi first
  34. #define se second
  35. #define pb push_back
  36. #define MOD 1000000007
  37. #define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
  38. #define YES cout << "YES\n"
  39. #define NO cout << "NO\n"
  40. #define MASK(i) (1LL << (i))
  41. #define c_bit(i) __builtin_popcountll(i)
  42. #define BIT(x,i) ((x) & MASK(i))
  43. #define SET_ON(x,i) ((x) | MASK(i))
  44. #define SET_OFF(x,i) ((x) & ~MASK(i))
  45. #define oo 1e18
  46. #define name "ANCTEXT"
  47. #define endl '\n'
  48. #define memory() cerr << abs(&M2-&M1)/1024.0/1024 << " MB" << endl
  49. #define time() cerr << endl << "-------------Time:" << 1000.0 * clock() / CLOCKS_PER_SEC << "ms." << endl
  50. template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
  51. template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
  52. template <class T> using ordered_set = tree <T, null_type, less_equal <T>, rb_tree_tag,tree_order_statistics_node_update>;
  53. const int N = (int)5e5+10;
  54. string s;
  55. int n,q;
  56. pair<int,string> mem[N];
  57. // VO20 - Bài 3
  58. namespace hungeazy {
  59.  
  60. const int base = 256;
  61. int pw[N],hashA[N],sorted[N],cnt=0;
  62.  
  63. int hashing(int hashVal, char h) {
  64. return (hashVal*base%MOD+h)%MOD;
  65. }
  66.  
  67. int getHash(int l, int r) {
  68. return (hashA[r]-hashA[l-1]*pw[r-l+1]%MOD+sqr(MOD))%MOD;
  69. }
  70.  
  71. struct Node {
  72. int h,endIdx,hashVal;
  73. Node *child[26], *par[20];
  74. char cur;
  75. Node() {
  76. h = hashVal = 0;
  77. endIdx = -1;
  78. FOR(i,0,19) par[i] = NULL;
  79. FOR(i,0,25) child[i] = NULL;
  80. cur = '?';
  81. }
  82. } *endNode[N];
  83.  
  84. struct Trie {
  85. Node *root;
  86.  
  87. Trie() {
  88. root = new Node();
  89. FOR(i,0,19) root->par[i] = root;
  90. endNode[0] = root;
  91. }
  92.  
  93. void insert(Node *p, int idx, string &s)
  94. {
  95. for (char c : s)
  96. {
  97. int pos = c-'a';
  98. if (p->child[pos] == NULL)
  99. {
  100. p->child[pos] = new Node();
  101. p->child[pos]->hashVal = (p->hashVal*base%MOD+c)%MOD;
  102. p->child[pos]->h = p->h+1;
  103. p->child[pos]->cur = c;
  104. p->child[pos]->par[0] = p;
  105. FOR(i,1,19)
  106. p->child[pos]->par[i] = (p->child[pos]->par[i-1])->par[i-1];
  107. }
  108. p = p->child[pos];
  109. }
  110. if (p->endIdx == -1) p->endIdx = idx;
  111. endNode[idx] = p;
  112. }
  113.  
  114. void DFS(Node *u)
  115. {
  116. if (u->endIdx != -1) sorted[++cnt] = u->endIdx;
  117. FOR(pos,0,25)
  118. if (u->child[pos] != NULL)
  119. DFS(u->child[pos]);
  120. }
  121. } trie;
  122.  
  123. bool check(int pos, int L, int R)
  124. {
  125. Node *p = endNode[pos];
  126. int lenS = R-L+1;
  127. if (p->h <= lenS and p->hashVal == getHash(L,L+p->h-1)) return true;
  128. FOD(i,19,0)
  129. {
  130. int lenT = p->par[i]->h;
  131. if (lenT > lenS or p->par[i]->hashVal != getHash(L,L+lenT-1))
  132. p = p->par[i];
  133. }
  134. return (p->h <= lenS and p->cur < s[L+p->h-1]);
  135. }
  136.  
  137. void solve(void)
  138. {
  139. int len = sz(s);
  140. s = "#"+s;
  141. pw[0] = 1;
  142. FOR(i,1,len)
  143. {
  144. pw[i] = pw[i-1]*base%MOD;
  145. hashA[i] = hashing(hashA[i-1],s[i]);
  146. }
  147. FOR(i,1,n)
  148. {
  149. auto [par,st] = mem[i];
  150. trie.insert(endNode[par],i,st);
  151. }
  152. trie.DFS(trie.root);
  153. while (q--)
  154. {
  155. int l,r;
  156. cin >> l >> r;
  157. int L = 1, R = cnt, ans = -1;
  158. while (L <= R)
  159. {
  160. int mid = (L+R)>>1;
  161. if (check(sorted[mid],l,r)) ans = sorted[mid], L = mid+1;
  162. else R = mid-1;
  163. }
  164. cout << ans << endl;
  165. }
  166. }
  167.  
  168. }
  169.  
  170. bool M2;
  171. signed main()
  172. {
  173. fast;
  174. if (fopen(name".inp","r"))
  175. {
  176. freopen(name".inp","r",stdin);
  177. freopen(name".out","w",stdout);
  178. }
  179. int subtask;
  180. cin >> subtask >> s >> n;
  181. FOR(i,1,n)
  182. {
  183. int par; string st;
  184. cin >> par >> st;
  185. mem[i] = {par,st};
  186. }
  187. cin >> q;
  188. hungeazy::solve();
  189. time();
  190. memory();
  191. return 0;
  192. }
  193. // ██░ ██ █ ██ ███▄ █ ▄████
  194. //▓██░ ██▒ ██ ▓██▒ ██ ▀█ █ ██▒ ▀█▒
  195. //▒██▀▀██░▓██ ▒██░▓██ ▀█ ██▒▒██░▄▄▄░
  196. //░▓█ ░██ ▓▓█ ░██░▓██▒ ▐▌██▒░▓█ ██▓
  197. //░▓█▒░██▓▒▒█████▓ ▒██░ ▓██░░▒▓███▀▒
  198. // ▒ ░░▒░▒░▒▓▒ ▒ ▒ ░ ▒░ ▒ ▒ ░▒ ▒
  199. // ▒ ░▒░ ░░░▒░ ░ ░ ░ ░░ ░ ▒░ ░ ░
  200. // ░ ░░ ░ ░░░ ░ ░ ░ ░ ░ ░ ░ ░
  201. // ░ ░ ░ ░ ░ ░
Success #stdin #stdout #stderr 0.01s 27344KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
-------------Time:7.77ms.
34.3331 MB