Submission #57128

# Submission time Handle Problem Language Result Execution time Memory
57128 2018-07-14T06:03:17 Z 윤교준(#1654) Selling RNA Strands (JOI16_selling_rna) C++11
100 / 100
407 ms 169444 KB
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[(sz(V)-2)])
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define clv(V) (V).clear()
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define cb(x) (x)=(!(x))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int __intarr[22000022], *__intarrp = __intarr;
inline int* newint(const unsigned int SZ) {
	__intarrp += SZ;
	return __intarrp - SZ;
}

const bool debug = 0;
const int MAXN = 100005;
const int MAXM = 100005;

struct SOD {
	SOD() : l(NULL), r(NULL), dt(0) {}
	SOD *l, *r;
	int dt;
} *nil;

void makeTree(SOD *bp, SOD *p, int S, int E, int X) {
	p -> dt = (bp -> dt) + 1;
	if(S == E) return;

	int m = (S+E)/2;

	if(X <= m) {
		p -> l = new SOD();
		p -> r = bp -> r;
		makeTree(bp -> l, p -> l, S, m, X);
	} else {
		p -> r = new SOD();
		p -> l = bp -> l;
		makeTree(bp -> r, p -> r, m+1, E, X);
	}
}
int getTree(SOD *v, int s, int e, int p, int q) {
	if(!(v -> dt) || q < p || e < p || e < p || q < s) return 0;
	if(p <= s && e <= q) return v -> dt;
	int m = (s+e)/2;
	return getTree(v->l, s, m, p, q) + getTree(v->r, m+1, e, p, q);
}

struct NOD {
	NOD() {
		for(int i = 0; i < 4; i++) p[i] = NULL;
		dt = s = e = c = 0;
	}
	NOD *p[4];
	int dt, s, e, c;
} *rt1, *rt2;

SOD *PST[MAXN];

int O[MAXN], T[MAXN], RO[MAXN];

int *A[MAXN], An[MAXN];
int *B[MAXM], *C[MAXM], Bn[MAXM], Cn[MAXM];

int N, M;

void makeTrie(NOD *p, int *A, int L, int i) {
	if(L < i) {
		(p -> dt)++;
		return;
	}
	if(NULL == (p -> p[A[i]]))
		p -> p[A[i]] = new NOD();
	makeTrie(p -> p[A[i]], A, L, i+1);
}
void structTrie(NOD *p, int &c) {
	if(NULL == p) return;
	p -> s = c;
	c += p -> dt;
	for(int i = 0; i < 4; i++)
		structTrie(p -> p[i], c);
	p -> e = c;
}
int getIndex(NOD *p, int *A, int L, int i) {
	if(L < i) return (p -> s) + ((p -> c)++);
	return getIndex(p -> p[A[i]], A, L, i+1);
}
pii getRange(NOD *p, int *A, int L, int i) {
	if(L < i) return pii(p -> s, p -> e);
	if(p -> p[A[i]]) return getRange(p -> p[A[i]], A, L, i+1);
	return pii(-1, -1);
}

void parseString(char *S, int *&A, int &L) {
	L = int(strlen(S+1));
	A = newint(L+1);
	for(int i = 1; S[i]; i++) {
		if('A' == S[i]) A[i] = 0;
		else if('C' == S[i]) A[i] = 1;
		else if('G' == S[i]) A[i] = 2;
		else A[i] = 3;
	}
}

void input() {
	char str[100005];

	scanf("%d%d", &N, &M);
	for(int i = 1; i <= N; i++) {
		scanf(" %s", str+1);
		parseString(str, A[i], An[i]);
	}
	for(int i = 1; i <= M; i++) {
		scanf(" %s", str+1);
		parseString(str, B[i], Bn[i]);
		scanf(" %s", str+1);
		parseString(str, C[i], Cn[i]);
	}
}

int main() {
	input();

	rt1 = new NOD();
	rt2 = new NOD();

	for(int i = 1; i <= N; i++) {
		makeTrie(rt1, A[i], An[i], 1);
		reverse(A[i]+1, A[i]+An[i]+1);
		makeTrie(rt2, A[i], An[i], 1);
		reverse(A[i]+1, A[i]+An[i]+1);
	}

	{ int c = 0; structTrie(rt1, c); }
	{ int c = 0; structTrie(rt2, c); }

	for(int i = 1; i <= N; i++) {
		O[i] = getIndex(rt1, A[i], An[i], 1) + 1;
		reverse(A[i]+1, A[i]+An[i]+1);
		T[i] = getIndex(rt2, A[i], An[i], 1) + 1;
		reverse(A[i]+1, A[i]+An[i]+1);
	}

	if(debug) {
	for(int i = 1; i <= N; i++)
		printf("%d ; %d %d\n", i, O[i], T[i]);
	}
	
	nil = new SOD();
	nil -> l = nil -> r = nil;
	PST[0] = nil;

	for(int i = 1; i <= N; i++) RO[O[i]] = i;
	for(int oi = 1, i; oi <= N; oi++) {
		i = RO[oi];
		PST[oi] = new SOD();
		makeTree(PST[oi-1], PST[oi], 0, N+1, T[i]);
	}

	for(int i = 1; i <= M; i++) reverse(C[i]+1, C[i]+Cn[i]+1);
	for(int i = 1; i <= M; i++) {
		int s, e, p, q;
		tie(s, e) = getRange(rt1, B[i], Bn[i], 1);
		tie(p, q) = getRange(rt2, C[i], Cn[i], 1);
		s++; e++; p++; q++;

		if(debug) printf("%d ; %d %d / %d %d\n", i, s, e, p, q);

		if(s >= e || p >= q) {
			puts("0");
			continue;
		}

		int ret = getTree(PST[e-1], 0, N+1, p, q-1);
		ret -= getTree(PST[s-1], 0, N+1, p, q-1);

		printf("%d\n", ret);
	}
	return 0;
}

Compilation message

selling_rna.cpp: In function 'void input()':
selling_rna.cpp:118:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
selling_rna.cpp:120:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %s", str+1);
   ~~~~~^~~~~~~~~~~~~~
selling_rna.cpp:124:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %s", str+1);
   ~~~~~^~~~~~~~~~~~~~
selling_rna.cpp:126:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %s", str+1);
   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 620 KB Output is correct
3 Correct 3 ms 620 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 688 KB Output is correct
6 Correct 3 ms 688 KB Output is correct
7 Correct 2 ms 688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 291 ms 142356 KB Output is correct
2 Correct 289 ms 142356 KB Output is correct
3 Correct 292 ms 142356 KB Output is correct
4 Correct 290 ms 142356 KB Output is correct
5 Correct 373 ms 166988 KB Output is correct
6 Correct 407 ms 169444 KB Output is correct
7 Correct 111 ms 169444 KB Output is correct
8 Correct 307 ms 169444 KB Output is correct
9 Correct 278 ms 169444 KB Output is correct
10 Correct 216 ms 169444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 169444 KB Output is correct
2 Correct 56 ms 169444 KB Output is correct
3 Correct 70 ms 169444 KB Output is correct
4 Correct 55 ms 169444 KB Output is correct
5 Correct 54 ms 169444 KB Output is correct
6 Correct 72 ms 169444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 620 KB Output is correct
3 Correct 3 ms 620 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 688 KB Output is correct
6 Correct 3 ms 688 KB Output is correct
7 Correct 2 ms 688 KB Output is correct
8 Correct 291 ms 142356 KB Output is correct
9 Correct 289 ms 142356 KB Output is correct
10 Correct 292 ms 142356 KB Output is correct
11 Correct 290 ms 142356 KB Output is correct
12 Correct 373 ms 166988 KB Output is correct
13 Correct 407 ms 169444 KB Output is correct
14 Correct 111 ms 169444 KB Output is correct
15 Correct 307 ms 169444 KB Output is correct
16 Correct 278 ms 169444 KB Output is correct
17 Correct 216 ms 169444 KB Output is correct
18 Correct 80 ms 169444 KB Output is correct
19 Correct 56 ms 169444 KB Output is correct
20 Correct 70 ms 169444 KB Output is correct
21 Correct 55 ms 169444 KB Output is correct
22 Correct 54 ms 169444 KB Output is correct
23 Correct 72 ms 169444 KB Output is correct
24 Correct 284 ms 169444 KB Output is correct
25 Correct 303 ms 169444 KB Output is correct
26 Correct 339 ms 169444 KB Output is correct
27 Correct 284 ms 169444 KB Output is correct
28 Correct 384 ms 169444 KB Output is correct
29 Correct 217 ms 169444 KB Output is correct