/* * Super slow (exponential) solution. Just for testing. */ #include #include #include #include using namespace std ; using ll = long long ; using ull = unsigned long long ; ll X, n, m ; vector> both ; vector pairs ; ll dist(ll a, ll b) { return min((a-b+X)%X, (b-a+X)%X) ; } const ll MOD = 998244353 ; const int CNT = 1<<20 ; int main() { cin >> X >> n >> m ; both.resize(n+m) ; pairs.resize(n+m) ; char tmp; for (auto &v: both) { cin >> v.first >> tmp; v.second = tmp == 'P'; } sort(both.begin(), both.end()) ; for (int dir=-1; dir<=1; dir += 2) { vector hstack ; for (int need=n, at=0; need > 0; at=(at+dir+n+m)%(n+m)) { if (both[at].second == 1) { hstack.push_back(at) ; } else if (hstack.size()) { int p = hstack[hstack.size()-1] ; hstack.pop_back() ; if (pairs[p] / CNT * 2 == dir + 1) { pairs[p] += CNT + at ; pairs[at] += CNT + p ; need-- ; } } } } ll r = 1, rs = 0 ; for (int i=0; i