Submission #840062

# Submission time Handle Problem Language Result Execution time Memory
840062 2023-08-31T03:51:24 Z Thanhs Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
164 ms 194772 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
 
#define int long long
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef stack<int> si;
typedef queue<int> qi;
typedef deque<int> di;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

#define endl '\n'
#define pb push_back
#define FOR(i,a,b) for(int i = a; i <= b; i++)
#define BACK(i,a,b) for(int i = a; i >= b; i--)
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define fi first
#define se second
#define gcd __gcd
#define setmin(x,y) x=min((x),(y))
#define setmax(x,y) x=max((x),(y))

const int MAXN=2e5+5;
const long long inf=1e18;
const int MOD=1e9+7;

struct node
{
	int l,r;
	node *nxt[4];
	vector<int> idx;
};

struct Op
{
	int x,y,idx;
	Op(int x,int y,int idx):x(x),y(y),idx(idx){}
	bool operator <(const Op& o) {return x<o.x;}
};

struct FT
{
	int n;
	vector<int> data;
	FT(int n):n(n),data(n+1){}
	void upd(int idx,int val) {for(;idx<=n;idx+=idx&-idx) data[idx]+=val;}
	int get(int idx) {int res=0; for(;idx;idx-=idx&-idx) res+=data[idx]; return res;}
};

array<int,2> points[MAXN];
int n,q,ans[MAXN],enc[256],cc;
node *root1=new node();
node *root2=new node();

void add(string &s,int idx)
{
	node *cur=root1;
	for(char c:s)
	{
		int ch=enc[(int)c];
		if(!cur->nxt[ch]) cur->nxt[ch]=new node();
		cur=cur->nxt[ch];
	}
	cur->idx.emplace_back(idx);
	reverse(s.begin(),s.end());
	cur=root2;
	for(char c:s)
	{
		int ch=enc[(int)c];
		if(!cur->nxt[ch]) cur->nxt[ch]=new node();
		cur=cur->nxt[ch];
	}
	cur->idx.emplace_back(idx);
}

void dfs(node *cur,int idx)
{
	cur->l=cc;
	for(int val:cur->idx) points[val][idx]=cc++;
	FOR(i,0,3) if(cur->nxt[i]) dfs(cur->nxt[i],idx);
	cur->r=cc-1;
}

pii get(string &s,node *cur)
{
	for(char c:s)
	{
		int ch=enc[(int)c];
		if(!cur->nxt[ch]) return {-1,-1};
		cur=cur->nxt[ch];
	}
	return {cur->l,cur->r};
}

signed main()
{
	fastIO
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	enc['A']=0; enc['C']=1; enc['G']=2; enc['U']=3;
	cin>>n>>q;
	FOR(i,1,n)
	{
		string s; cin>>s;
		add(s,i);
	}
	cc=1; dfs(root1,0);
	cc=1; dfs(root2,1);
	sort(points+1,points+n+1);
	vector<Op> op;
	auto addrange = [&](int x1,int x2,int y1,int y2,int idx) -> void
	{
		op.emplace_back(x2,y2,idx);
		op.emplace_back(x1-1,y2,-idx);
		op.emplace_back(x2,y1-1,-idx);
		op.emplace_back(x1-1,y1-1,idx);
	};
	FOR(i,1,q)
	{
		string pfx,sfx;
		cin>>pfx>>sfx;
		reverse(sfx.begin(),sfx.end());
		pii x=get(pfx,root1);
		pii y=get(sfx,root2);
		if(x.fi==-1||y.fi==-1) continue;
		addrange(x.fi,x.se,y.fi,y.se,i);
	}
	sort(op.begin(),op.end());
	FT fw(n);
	int ptr=1;
	for(Op t:op)
	{
		int x=t.x,y=t.y,idx=t.idx;
		while(ptr<=n&&points[ptr][0]<=x) fw.upd(points[ptr++][1],1);
		ans[abs(idx)]+=fw.get(y)*(idx>0?1:-1);
	}
	FOR(i,1,q) cout<<ans[i]<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 155180 KB Output is correct
2 Correct 127 ms 147524 KB Output is correct
3 Correct 128 ms 153580 KB Output is correct
4 Correct 121 ms 146184 KB Output is correct
5 Correct 162 ms 191916 KB Output is correct
6 Correct 164 ms 194772 KB Output is correct
7 Correct 34 ms 1628 KB Output is correct
8 Correct 125 ms 113260 KB Output is correct
9 Correct 119 ms 95236 KB Output is correct
10 Correct 85 ms 92412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 8336 KB Output is correct
2 Correct 17 ms 5236 KB Output is correct
3 Correct 23 ms 8316 KB Output is correct
4 Correct 18 ms 7840 KB Output is correct
5 Correct 18 ms 5304 KB Output is correct
6 Correct 23 ms 8444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 125 ms 155180 KB Output is correct
9 Correct 127 ms 147524 KB Output is correct
10 Correct 128 ms 153580 KB Output is correct
11 Correct 121 ms 146184 KB Output is correct
12 Correct 162 ms 191916 KB Output is correct
13 Correct 164 ms 194772 KB Output is correct
14 Correct 34 ms 1628 KB Output is correct
15 Correct 125 ms 113260 KB Output is correct
16 Correct 119 ms 95236 KB Output is correct
17 Correct 85 ms 92412 KB Output is correct
18 Correct 23 ms 8336 KB Output is correct
19 Correct 17 ms 5236 KB Output is correct
20 Correct 23 ms 8316 KB Output is correct
21 Correct 18 ms 7840 KB Output is correct
22 Correct 18 ms 5304 KB Output is correct
23 Correct 23 ms 8444 KB Output is correct
24 Correct 115 ms 129876 KB Output is correct
25 Correct 127 ms 132920 KB Output is correct
26 Correct 121 ms 126936 KB Output is correct
27 Correct 115 ms 128320 KB Output is correct
28 Correct 98 ms 33296 KB Output is correct
29 Correct 66 ms 16328 KB Output is correct