#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const double PI = 4 * atan(1);
#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define vi vector<int>
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define vpi vector<pair<int, int>>
#define vpd vector<pair<double, double>>
#define pd pair<double, double>
#define f0r(i,a) for(int i=0;i<a;i++)
#define f1r(i,a,b) for(int i=a;i<b;i++)
template<typename A, typename B> ostream& operator <<(ostream &cout, pair< A, B> const &p) {return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator <<(ostream &cout, vector<A> const &v){
cout <<"["; for(int i = 0; i<v.size(); i++) {if(i) cout <<", "; cout << v[i];} return cout << "]";
}
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
void io(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fast_io();
}
const int MAX = 1e5 + 5;
const int ALPH = 4;
const int VALS = 2e6;
const int INF = 1e9;
const int MOD = 1e9 + 7;
vector<string> s;
vector<string> p;
vector<string> q;
int n, m;
pii pts[MAX];
pair<pii, pii> queries[MAX];
template<int SZ> struct mstree {
Tree<pii> val[SZ+1]; // for offline queries use vector with binary search instead
void upd(int x, int y, int t = 1) { // x-coordinate between 1 and SZ inclusive
for (int X = x; X <= SZ; X += X&-X) {
if (t == 1) val[X].insert({y,x});
else val[X].erase({y,x});
}
}
int query(int x, int y) {
int t = 0;
for (;x > 0; x -= x&-x) t += val[x].order_of_key({y,MOD});
return t;
}
int query(int lox, int hix, int loy, int hiy) { // query number of elements within a rectangle
return query(hix,hiy)-query(lox-1,hiy)
-query(hix,loy-1)+query(lox-1,loy-1);
}
};
template<int SZ> struct Trie{
int t[SZ][ALPH];
int loc[SZ];
int root = 0;
int vals = 1;
int ok = 0;
unordered_map<int, int> um;
pii bounds[SZ];
vi v;
void build(){
f0r(i, SZ) f0r(j, ALPH) t[i][j] = -1;
// cout << "bad" << endl;
}
int get(char c){
if(c == 'A') return 0;
else if(c == 'U') return 1;
else if (c == 'G') return 2;
return 3;
}
void ins(string s, int idx){
int sz = sz(s);
int cur = root;
f0r(i, sz){
int g = get(s[i]);
if(t[cur][g] == -1){
t[cur][g] = vals;
cur = vals;
vals++;
}
else{
cur = t[cur][g];
}
}
//cout << idx << " " << cur << endl;
um[idx] = cur;
}
void euler(){
dfs(root);
f0r(i, m){
if(ok == 0){
pts[i].f = loc[um[i]];
queries[i].f = bounds[um[i+n]];
}
else{
pts[i].s = loc[um[i]];
queries[i].s = bounds[um[i+n+m]];
}
}
}
int dfs(int src){
v.eb(src);
int val = sz(v) - 1;
loc[src] = val;
int r = val;
f0r(i, ALPH){
if(t[src][i] == -1) continue;
int ret = dfs(t[src][i]);
r = max(r, ret);
}
bounds[src] = mp(val, r);
return r;
}
};
Trie<VALS> f;
Trie<VALS> b;
unordered_map<int, int> xm;
unordered_map<int, int> ym;
mstree<3*MAX> ms;
int main(){
fast_io();
cin >> n >> m;
string temporary;
getline(cin, temporary);
f.build();
b.build();
int cnt = 0;
f0r(i, n){
string si;
cin >> si;
s.eb(si);
f.ins(si, cnt);
reverse(all(si));
b.ins(si, cnt);
cnt++;
}
cnt = 0;
f0r(i, m){
string pi, qi;
cin >> pi >> qi;
p.eb(pi);
q.eb(qi);
f.ins(pi, cnt+n);
reverse(all(qi));
b.ins(qi, cnt+n + m);
cnt++;
}
b.ok = 1;
f.euler();
b.euler();
set<int> ycoords;
set<int> xcoords;
f0r(i, n){
xcoords.insert(pts[i].f);
ycoords.insert(pts[i].s);
}
f0r(i, m){
xcoords.insert(queries[i].f.f); xcoords.insert(queries[i].f.s);
ycoords.insert(queries[i].s.f); ycoords.insert(queries[i].s.s);
}
cnt = 1;
for(auto x: xcoords){
xm[x] = cnt;
cnt++;
}
cnt = 1;
for(auto x: ycoords){
ym[x] = cnt;
cnt++;
}
f0r(i, n){
pts[i].f = xm[pts[i].f];
pts[i].s = ym[pts[i].s];
ms.upd(pts[i].f, pts[i].s);
}
f0r(i, m){
queries[i].f.f = xm[queries[i].f.f];
queries[i].s.f = ym[queries[i].s.f];
queries[i].f.s = xm[queries[i].f.s];
queries[i].s.s = ym[queries[i].s.s];
cout << ms.query(queries[i].f.f, queries[i].f.s, queries[i].s.f, queries[i].s.s) << endl;;
}
return 0;
}
Compilation message
selling_rna.cpp:1:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(linker, "/stack:200000000")
selling_rna.cpp: In function 'void io(std::__cxx11::string)':
selling_rna.cpp:50:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FIN, "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~
selling_rna.cpp:51:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FOUT, "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
116 ms |
122788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
274 ms |
148816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
333 ms |
136980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
116 ms |
122788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |