Submission #767334

# Submission time Handle Problem Language Result Execution time Memory
767334 2023-06-26T15:55:58 Z jmyszka2007 Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
281 ms 283584 KB
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pi pair<int, int>
#define pb push_back
constexpr int LIM = 4e6;
constexpr int base = (1 << 22);
int gpref[LIM + 10][26];
int gsuf[LIM + 10][26];
int prepref[LIM + 10];
int sizpref[LIM + 10];
int presuf[LIM + 10];
int sizsuf[LIM + 10];
int res[LIM + 10];
int tri[2 * base];
void upd(int v) {
	v += base;
	while(v > 0) {
		tri[v]++;
		v /= 2;
	}
}
int que(int l, int r) {
	l += base;
	r += base;
	int ans = 0;
	while(l <= r) {
		if(l & 1) {
			ans += tri[l];
		}
		if(!(r & 1)) {
			ans += tri[r];
		}
		l = (l + 1) / 2;
		r = (r - 1) / 2;
	}
	return ans;
}
int aktpre = 1;
void dfspref(int v) {
	prepref[v] = aktpre++;
	sizpref[v] = 1;
	for(int i = 0; i < 26; i++) {
		if(gpref[v][i]) {
			dfspref(gpref[v][i]);
			sizpref[v] += sizpref[gpref[v][i]];
		}
	}
}
void dfssuf(int v) {
	presuf[v] = aktpre++;
	sizsuf[v] = 1;
	for(int i = 0; i < 26; i++) {
		if(gsuf[v][i]) {
			dfssuf(gsuf[v][i]);
			sizsuf[v] += sizsuf[gsuf[v][i]];
		}
	}
}
int main() {
	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	int n, t;
	cin >> n >> t;
	int cntpref = 2;
	int cntsuf = 2;
	vector<pair<pi, pi> >zap;
	for(int i = 1; i <= n; i++) {
		string a;
		cin >> a;
		pi pkt;
		int akt = 1;
		for(auto x : a) {
			if(!gpref[akt][x - 'A']) {
				gpref[akt][x - 'A'] = cntpref++;
			}
			akt = gpref[akt][x - 'A'];
		}
		pkt.st = akt;
		reverse(a.begin(), a.end());
		akt = 1;
		for(auto x : a) {
			if(!gsuf[akt][x - 'A']) {
				gsuf[akt][x - 'A'] = cntsuf++;
			}
			akt = gsuf[akt][x - 'A'];
		}
		pkt.nd = akt;
		zap.pb({pkt, {0, 0}});
	}
	for(int i = 1; i <= t; i++) {
		string a, b;
		cin >> a >> b;
		reverse(b.begin(), b.end());
		pi pkt;
		int akt = 1;
		for(auto x : a) {
			if(!gpref[akt][x - 'A']) {
				gpref[akt][x - 'A'] = cntpref++;
			}
			akt = gpref[akt][x - 'A'];
		}
		pkt.st = akt;
		akt = 1;
		for(auto x : b) {
			if(!gsuf[akt][x - 'A']) {
				gsuf[akt][x - 'A'] = cntsuf++;
			}
			akt = gsuf[akt][x - 'A'];
		}
		pkt.nd = akt;
		zap.pb({pkt, {0, i}});
	}
	dfspref(1);
	aktpre = 1;
	dfssuf(1);
	vector<pair<pi, pi> >nzap;
	/*for(int i = 1; i < cntpref; i++) {
		cout << i << ' ' << prepref[i] << ' ' << sizpref[i] << ":\n";
		for(int j = 0; j < 26; j++) {
			if(gpref[i][j]) {
				cout << gpref[i][j] << ' ';
			}
		}
		cout << '\n';
	}*/
	for(int i = 0; i < zap.size(); i++) {
		int a = prepref[zap[i].st.st];
		int b = presuf[zap[i].st.nd];
		//cout << a << ' ' << b << '\n';
		if(!zap[i].nd.nd) {
			nzap.pb({{a, 0}, {0, b}});
		}
		else {
			nzap.pb({{a - 1, b}, {b + sizsuf[zap[i].st.nd] - 1, -zap[i].nd.nd}});
			nzap.pb({{a + sizpref[zap[i].st.st] - 1, b}, {b + sizsuf[zap[i].st.nd] - 1, zap[i].nd.nd}});
		}
	}
	sort(nzap.begin(), nzap.end());
	for(auto x : nzap) {
		//cout << x.st.st << ' ' << x.st.nd << ' ' << x.nd.st << ' ' << x.nd.nd << '\n';
		if(!x.st.nd) {
			upd(x.nd.nd);
		}
		else {
			if(x.nd.nd < 0) {
				res[-x.nd.nd] -= que(x.st.nd, x.nd.st);
			}
			else {
				res[x.nd.nd] += que(x.st.nd, x.nd.st);
			}
		}
	}
	for(int i = 1; i <= t; i++) {
		cout << res[i] << '\n';
	}
}

Compilation message

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:128:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |  for(int i = 0; i < zap.size(); i++) {
      |                 ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 436 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 231752 KB Output is correct
2 Correct 232 ms 223192 KB Output is correct
3 Correct 245 ms 217924 KB Output is correct
4 Correct 224 ms 207492 KB Output is correct
5 Correct 271 ms 279436 KB Output is correct
6 Correct 272 ms 283584 KB Output is correct
7 Correct 104 ms 6724 KB Output is correct
8 Correct 279 ms 168780 KB Output is correct
9 Correct 246 ms 142492 KB Output is correct
10 Correct 186 ms 136148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 6600 KB Output is correct
2 Correct 27 ms 4312 KB Output is correct
3 Correct 37 ms 5280 KB Output is correct
4 Correct 31 ms 4796 KB Output is correct
5 Correct 28 ms 4300 KB Output is correct
6 Correct 36 ms 5260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 436 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 237 ms 231752 KB Output is correct
9 Correct 232 ms 223192 KB Output is correct
10 Correct 245 ms 217924 KB Output is correct
11 Correct 224 ms 207492 KB Output is correct
12 Correct 271 ms 279436 KB Output is correct
13 Correct 272 ms 283584 KB Output is correct
14 Correct 104 ms 6724 KB Output is correct
15 Correct 279 ms 168780 KB Output is correct
16 Correct 246 ms 142492 KB Output is correct
17 Correct 186 ms 136148 KB Output is correct
18 Correct 42 ms 6600 KB Output is correct
19 Correct 27 ms 4312 KB Output is correct
20 Correct 37 ms 5280 KB Output is correct
21 Correct 31 ms 4796 KB Output is correct
22 Correct 28 ms 4300 KB Output is correct
23 Correct 36 ms 5260 KB Output is correct
24 Correct 228 ms 194580 KB Output is correct
25 Correct 281 ms 196264 KB Output is correct
26 Correct 219 ms 191788 KB Output is correct
27 Correct 224 ms 179808 KB Output is correct
28 Correct 204 ms 38836 KB Output is correct
29 Correct 126 ms 15088 KB Output is correct