B. T-shirt buying*
time limit per test 3 seconds
memory limit per test 256 megabytes
input standard input
output standard output
A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers pi, ai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.
m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won’t buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts.
Input
The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.
The following line contains sequence of integers p1, p2, …, pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.
The following line contains sequence of integers a1, a2, …, an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.
The following line contains sequence of integers b1, b2, …, bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.
The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.
The following line contains sequence c1, c2, …, cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.
Output
Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won’t buy anything, print -1.
Examples
input
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
output
200 400 300 500 911 -1
input
2
1000000000 1
1 1
1 2
2
2 1
output
1 1000000000
#include<bits/stdc++.h>
using namespace std;
int n,m;
#define N 210001
#define PER(i,a,b) for(int i=a;i<=b;i++)
int a[N],b[N];int p[N];int c[N];bool pg[4][4];int t[4][4][2*N];int size[4][4];
void up(int x,int y,int k)
{
while(k>1&&t[x][y][k]<t[x][y][k/2])
{
swap(t[x][y][k],t[x][y][k/2]);
k/=2;
}
}
void down(int x,int y,int k)
{
int j;
int le=k*2,ri=le+1;
while(le<=size[x][y])
{
if(le==size[x][y]||t[x][y][le]<t[x][y][ri]) j=le;else j=ri;
if(t[x][y][j]<t[x][y][k]) swap(t[x][y][j],t[x][y][k]);
else return;
k=j;le=k*2,ri=le+1;
}
return;
}
void add(int x,int y,int p)
{
int num=++size[x][y];
t[x][y][num]=p;up(x,y,num);
}
int co;
int back,on[4];
int QwQ;
int bb,cc;
int main()
{
scanf("%d",&n); PER(i,1,n) scanf("%d",&p[i]);
PER(i,1,3)PER(j,1,3)PER(xx,1,n+1) t[i][j][xx]=2e9;
PER(i,1,n)scanf("%d",&a[i]);PER(i,1,n)scanf("%d",&b[i]);
PER(i,1,n) if(a[i]>b[i]) swap(a[i],b[i]);
PER(i,1,n) add(a[i],b[i],p[i]); scanf("%d",&m);
PER(i,1,m)
{
scanf("%d",&co);
PER(j,1,3)
{
bb=co;cc=j;
if(bb>cc) swap(bb,cc);
on[j]=t[bb][cc][1];
}
QwQ=2e9;
PER(j,1,3) {if(QwQ>on[j]) {back=j;QwQ=on[j];}}if(back==0||QwQ>1e9+4001) QwQ=-1;
printf("%d ",QwQ);
bb=co;cc=back;if(bb>cc) swap(bb,cc);t[bb][cc][1]=2e9+5;down(bb,cc,1);
}
}
本文介绍了一个关于T恤购买的问题,需要根据买家喜爱的颜色为每位买家匹配价格最低且颜色符合要求的T恤。通过构建特定的数据结构来高效地解决这个问题。
438

被折叠的 条评论
为什么被折叠?



