結果
問題 | No.2338 Range AtCoder Query |
ユーザー |
![]() |
提出日時 | 2022-08-22 22:56:21 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 624 ms / 4,000 ms |
コード長 | 2,304 bytes |
コンパイル時間 | 1,713 ms |
コンパイル使用メモリ | 182,756 KB |
実行使用メモリ | 51,680 KB |
最終ジャッジ日時 | 2024-10-04 09:34:05 |
合計ジャッジ時間 | 17,038 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 34 |
ソースコード
#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, int x){i++;while (i <= N){BIT[i] += x;i += i & -i;}}int sum(int i){int ans = 0;while (i > 0){ans += BIT[i];i -= i & -i;}return ans;}};int main(){int N, M, Q;cin >> N >> M >> Q;vector<int> P(N);vector<string> S(N);for (int i = 0; i < N; i++){cin >> P[i] >> S[i];P[i]--;}vector<int> L(Q), R(Q);for (int i = 0; i < Q; i++){cin >> L[i] >> R[i];L[i]--;}vector<vector<int>> ac(M);for (int i = 0; i < N; i++){if (S[i] == "AC"){ac[P[i]].push_back(i);}}vector<vector<pair<int, int>>> add1(N + 1);vector<vector<pair<int, int>>> add2(N + 1);for (int i = 0; i < N; i++){auto itr = lower_bound(ac[P[i]].begin(), ac[P[i]].end(), i);if (S[i] == "AC"){int l = -1;if (itr != ac[P[i]].begin()){l = *(itr - 1);}add1[l + 1].push_back(make_pair(i, 1));add1[i + 1].push_back(make_pair(i, -1));}if (S[i] == "WA"){if (itr != ac[P[i]].end()){int r = *itr;int l = -1;if (itr != ac[P[i]].begin()){l = *(itr - 1);}add2[l + 1].push_back(make_pair(r, 1));add2[i + 1].push_back(make_pair(r, -1));}}}vector<vector<pair<int, int>>> get(N + 1);for (int i = 0; i < Q; i++){get[L[i]].push_back(make_pair(R[i], i));}vector<int> ans1(Q), ans2(Q);binary_indexed_tree BIT1(N);binary_indexed_tree BIT2(N);for (int i = 0; i < N; i++){int cnt1 = add1[i].size();for (int j = 0; j < cnt1; j++){int p = add1[i][j].first;int x = add1[i][j].second;BIT1.add(p, x);}int cnt2 = add2[i].size();for (int j = 0; j < cnt2; j++){int p = add2[i][j].first;int x = add2[i][j].second;BIT2.add(p, x);}int cnt3 = get[i].size();for (int j = 0; j < cnt3; j++){int r = get[i][j].first;int id = get[i][j].second;ans1[id] = BIT1.sum(r);ans2[id] = BIT2.sum(r);}}for (int i = 0; i < Q; i++){cout << ans1[i] << ' ' << ans2[i] << endl;}}