제출 #562759

#제출 시각아이디문제언어결과실행 시간메모리
562759SSRSSelling RNA Strands (JOI16_selling_rna)C++14
100 / 100
502 ms38508 KiB
#include <bits/stdc++.h>
using namespace std;
struct binary_indexed_tree{
  int N;
  vector<int> BIT;
  binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
  }
  void add(int i){
    i++;
    while (i <= N){
      BIT[i]++;
      i += i & -i;
    }
  }
  int sum(int i){
    int ans = 0;
    while (i > 0){
      ans += BIT[i];
      i -= i & -i;
    }
    return ans;
  }
  int sum(int L, int R){
    return sum(R) - sum(L);
  }
};
int main(){
  int N, M;
  cin >> N >> M;
  vector<string> S(N);
  for (int i = 0; i < N; i++){
    cin >> S[i];
  }
  vector<pair<string, int>> P1(N);
  for (int i = 0; i < N; i++){
    P1[i] = make_pair(S[i], i);
  }
  for (int i = 0; i < N; i++){
    reverse(S[i].begin(), S[i].end());
  }
  vector<pair<string, int>> P2(N);
  for (int i = 0; i < N; i++){
    P2[i] = make_pair(S[i], i);
  }
  sort(P1.begin(), P1.end());
  sort(P2.begin(), P2.end());
  vector<int> X(N), Y(N);
  for (int i = 0; i < N; i++){
    X[P1[i].second] = i;
    Y[P2[i].second] = i;
  }
  vector<int> L1(M), R1(M), L2(M), R2(M);
  for (int i = 0; i < M; i++){
    string P, Q;
    cin >> P >> Q;
    reverse(Q.begin(), Q.end());
    L1[i] = lower_bound(P1.begin(), P1.end(), make_pair(P, 0)) - P1.begin();
    P.back()++;
    R1[i] = lower_bound(P1.begin(), P1.end(), make_pair(P, 0)) - P1.begin();
    L2[i] = lower_bound(P2.begin(), P2.end(), make_pair(Q, 0)) - P2.begin();
    Q.back()++;
    R2[i] = lower_bound(P2.begin(), P2.end(), make_pair(Q, N)) - P2.begin();
  }
  vector<vector<int>> query_add(N + 1), query_sub(N + 1);
  for (int i = 0; i < M; i++){
    query_sub[L1[i]].push_back(i);
    query_add[R1[i]].push_back(i);
  }
  vector<vector<int>> update(N + 1);
  for (int i = 0; i < N; i++){
    update[X[i]].push_back(Y[i]);
  }
  vector<int> ans(M, 0);
  binary_indexed_tree BIT(N);
  for (int i = 0; i <= N; i++){
    for (int j : query_add[i]){
      ans[j] += BIT.sum(L2[j], R2[j]);
    }
    for (int j : query_sub[i]){
      ans[j] -= BIT.sum(L2[j], R2[j]);
    }
    for (int j : update[i]){
      BIT.add(j);
    }
  }
  for (int i = 0; i < M; i++){
    cout << ans[i] << endl;
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...