void GLrender() { glClear(GL_COLOR_BUFFER_BIT); lines = quadtree.query(Point2D(me.c.p.x,me.c.p.y),me.c); quadtree.render(); me.c.render(col); glFlush(); glutSwapBuffers(); }
int main() { ios::sync_with_stdio(false); cin.tie(NULL); QuadTree<100000, 4000100> qt; int N, Q; cin >> N >> Q >> ws; vector<string> inp(N, ""); Node *ft = new Node(), *bt = new Node(); for (int i = 0; i < N; ++i) { getline(cin, inp[i]); ft->insert(inp[i]); bt->rinsert(inp[i], inp[i].length() - 1); } int fc = 0, bc = 0; ft->leaf_fix(fc); bt->leaf_fix(bc); vii lr(N, {-1, -1}); for (int i = 0; i < N; ++i) { lr[i].first = ft->getleaf(inp[i], 0).first; lr[i].second = bt->getleaf_r(inp[i], inp[i].length()-1).first; // cerr << i << ' ' << lr[i].first << ' '<<lr[i].second << endl; } vii bysize; for (int i = 0; i < N; ++i) bysize.push_back({inp[i].size(), i}); sort(bysize.rbegin(), bysize.rend()); inp.clear(); vector<string> query(Q, ""); vii qbysize; for (int q = 0; q < Q; ++q) { getline(cin, query[q]); qbysize.push_back({query[q].size(), q}); } sort(qbysize.rbegin(), qbysize.rend()); vi ans(Q, 0LL); // Handle queries one by one for (int _q = 0, i = 0; _q < Q; ++_q) { int q = qbysize[_q].second; // Insert all strings that are strictly smaller while (i < N && bysize[i].first + 1 >= qbysize[_q].first) { int s = bysize[i].second; qt.insert(lr[s], 1); ++i; } // Process the query ii prefr = ft->getleaf_ast(query[q], 0); ii suffr = bt->getleaf_rast(query[q], query[q].length()-1); if (prefr.second == -1 || suffr.second == -1) continue; // cerr << q << " "; // cerr << prefr.first << ' ' <<prefr.second << " "; // cerr << suffr.first << ' ' <<suffr.second << endl; --prefr.second; --suffr.second; ans[q] = qt.query(prefr.first, suffr.first, prefr.second, suffr.second); } for (size_t i = 0; i < ans.size(); ++i) cout << ans[i] << '\n'; return 0; }