답안 #276510

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
276510 2020-08-20T13:20:15 Z spdskatr Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
323 ms 224096 KB
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#define SZ (1<<21)
#define fi first
#define se second
using namespace std;
struct entry {
	int i, l, r, f, a;
};

int N, M, ppos[100005], spos[100005], ans[100005];
string S[100005], P[100005], Q[100005];
vector<int> sw[2000005];
vector<entry> qu;
char bruh[] = { 'A', 'C', 'G', 'U' };

struct Trie {
	int ch[2000005][4], cnt = 2, sz[2000005], ind[2000005], cnt2;
	vector<int> occ[2000005];
	void build(int s) {
		int cur = 1;
		for (char c : S[s]) {
			for (int i = 0; i < 4; i++) {
				if (c == bruh[i]) {
					if (ch[cur][i] == 0) {
						ch[cur][i] = cnt++;
					}
					cur = ch[cur][i];
				}
			}
		}
		occ[cur].push_back(s);
	}
	void dfs(int x, int *p) {
		ind[x] = cnt2++;
		sz[x] = 1;
		for (int v : occ[x]) p[v] = ind[x];
		for (int i = 0; i < 4; i++) {
			if (ch[x][i]) {
				dfs(ch[x][i], p);
				sz[x] += sz[ch[x][i]];
			}
		}
	}
	int query(const string &s) {
		int cur = 1;
		for (char c : s) {
			for (int i = 0; i < 4; i++) {
				if (c == bruh[i]) {
					if (ch[cur][i] == 0) {
						return 0;
					}
					cur = ch[cur][i];
				}
			}
		}
		return cur;
	}
} tp, ts;

struct Segtree {
	int st[1<<22];
	void seg_inc(int i) {
		for (st[i += SZ] += 1; i > 1; i >>= 1) st[i>>1] = st[i] + st[i^1];
	}
	int seg_sum(int l, int r) {
		int res = 0;
		for (l += SZ, r += SZ; l < r; l >>= 1, r >>= 1) {
			if (l&1) res += st[l++];
			if (r&1) res += st[--r];
		}
		return res;
	}
} st1;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N >> M;
	for (int i = 0; i < N; i++) {
		cin >> S[i];
		tp.build(i);
		reverse(S[i].begin(), S[i].end());
		ts.build(i);
	}
	tp.dfs(1, ppos);
	ts.dfs(1, spos);
	for (int i = 0; i < N; i++) {
		//printf("Point at (%d, %d)\n", ppos[i], spos[i]);
		sw[ppos[i]].push_back(spos[i]);
	}
	for (int i = 0; i < M; i++) {
		cin >> P[i] >> Q[i];
		reverse(Q[i].begin(), Q[i].end());
		int pn = tp.query(P[i]);
		int sn = ts.query(Q[i]);
		int pl = tp.ind[pn];
		int pr = tp.ind[pn] + tp.sz[pn];
		int sl = ts.ind[sn];
		int sr = ts.ind[sn] + ts.sz[sn];
		//printf("Query %d: [%d, %d), [%d, %d)\n", i, pl, pr, sl, sr);
		qu.push_back({ pl-1, sl, sr, -1, i });
		qu.push_back({ pr-1, sl, sr, 1, i });
	}
	sort(qu.begin(), qu.end(), [](entry l, entry r) {
		return l.i < r.i;
	});
	int cur = 0;
	for (entry e : qu) {
		while (cur <= e.i) {
			for (int x : sw[cur]) {
				//printf("Incrementing %d, %d\n", cur, x);
				st1.seg_inc(x);
			}
			cur++;
		}
		ans[e.a] += e.f * st1.seg_sum(e.l, e.r);
	}
	for (int i = 0; i < M; i++) {
		cout << ans[i] << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 150816 KB Output is correct
2 Correct 105 ms 150828 KB Output is correct
3 Correct 119 ms 150776 KB Output is correct
4 Correct 119 ms 150776 KB Output is correct
5 Correct 115 ms 150760 KB Output is correct
6 Correct 116 ms 150776 KB Output is correct
7 Correct 119 ms 150776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 312 ms 215628 KB Output is correct
2 Correct 279 ms 215548 KB Output is correct
3 Correct 259 ms 203496 KB Output is correct
4 Correct 297 ms 201328 KB Output is correct
5 Correct 323 ms 223056 KB Output is correct
6 Correct 300 ms 224096 KB Output is correct
7 Correct 150 ms 159208 KB Output is correct
8 Correct 238 ms 198996 KB Output is correct
9 Correct 239 ms 192640 KB Output is correct
10 Correct 232 ms 189012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 143 ms 154968 KB Output is correct
2 Correct 130 ms 153600 KB Output is correct
3 Correct 151 ms 154864 KB Output is correct
4 Correct 144 ms 154584 KB Output is correct
5 Correct 126 ms 153560 KB Output is correct
6 Correct 135 ms 154884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 150816 KB Output is correct
2 Correct 105 ms 150828 KB Output is correct
3 Correct 119 ms 150776 KB Output is correct
4 Correct 119 ms 150776 KB Output is correct
5 Correct 115 ms 150760 KB Output is correct
6 Correct 116 ms 150776 KB Output is correct
7 Correct 119 ms 150776 KB Output is correct
8 Correct 312 ms 215628 KB Output is correct
9 Correct 279 ms 215548 KB Output is correct
10 Correct 259 ms 203496 KB Output is correct
11 Correct 297 ms 201328 KB Output is correct
12 Correct 323 ms 223056 KB Output is correct
13 Correct 300 ms 224096 KB Output is correct
14 Correct 150 ms 159208 KB Output is correct
15 Correct 238 ms 198996 KB Output is correct
16 Correct 239 ms 192640 KB Output is correct
17 Correct 232 ms 189012 KB Output is correct
18 Correct 143 ms 154968 KB Output is correct
19 Correct 130 ms 153600 KB Output is correct
20 Correct 151 ms 154864 KB Output is correct
21 Correct 144 ms 154584 KB Output is correct
22 Correct 126 ms 153560 KB Output is correct
23 Correct 135 ms 154884 KB Output is correct
24 Correct 267 ms 209236 KB Output is correct
25 Correct 280 ms 211080 KB Output is correct
26 Correct 299 ms 208016 KB Output is correct
27 Correct 250 ms 196368 KB Output is correct
28 Correct 273 ms 172412 KB Output is correct
29 Correct 208 ms 160384 KB Output is correct