B. Applejack and Storages

This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).

Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend’s company. Initially, the company storehouse has n
planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack’s friend can provide her with information about each operation. For convenience, he will give her information according to the following format:
+x: the storehouse received a plank with length x
−x: one plank with length x was removed from the storehouse (it is guaranteed that the storehouse had some planks with length x).

Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!
We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.

Input
The first line contains a single integer n (1≤n≤105): the initial amount of planks at the company’s storehouse, the second line contains n integers a1,a2,…,an (1≤ai≤105): the lengths of the planks.
The third line contains a single integer q (1≤q≤105): the number of events in the company. Each of the next q lines contains a description of the events in a given format: the type of the event (a symbol + or −) is given first, then goes the integer x (1≤x≤105).
Output
After every event in the company, print “YES” if two storages of the required shape can be built from the planks of that company’s set, and print “NO” otherwise. You can print each letter in any case (upper or lower).

Example


Input

6
1 1 1 2 1 1
6
+ 2
+ 1
- 1
+ 2
- 1
+ 2

Output

NO
YES
NO
NO
NO
YES

Note

After the second event Applejack can build a rectangular storage using planks with lengths 1
, 2, 1, 2 and a square storage using planks with lengths 1, 1, 1, 1.

After the sixth event Applejack can build a rectangular storage using planks with lengths 2, 2, 2, 2 and a square storage using planks with lengths 1, 1, 1, 1.

感觉很厉害的一个题,也可能是我太弱了

数组 a 存长度为 m 的木棒个数,数组 b 存木棒个数为 a[m] 的有多少种木棒,但是一个长度为4的木棒如果有3个的话,数组 b 里面木棒个数为1存了这个木棒,木棒个数为2也存了这个木棒,木棒个数为3也存了这个木棒,所以这也解释了为什么 if(x==’+’) a[m]++,b[a[m]]++; 为什么b[a[m]] 不减去 1,因为如果一个木棒有八个的话,那么在算有七个木棒的个数的时候这个木棒也是算上的

这个也解释了下面判断条件if(b[8]>0 || (b[4]>=2) || (b[4]>0 && b[2]>=3) || (b[6]>0 && b[2]>=2)) cout<<“YES”<

#include 
using namespace std;
#define ll long long
const int l=100005;
int main()
{
       
	int a[l],b[l],n,m;
    char x;
    cin>>n;
    while(n--)
    {
     
       cin>>m;
       a[m]++;
       b[a[m]]++;
    }
    cin>>n;
    while(n--)
    {
     
    	cin>>x>>m;
    	if(x=='+') a[m]++,b[a[m]]++;
    	else b[a[m]]--,a[m]--;
 	    if(b[8]>0 || (b[4]>=2) || (b[4]>0 && b[2]>=3) || (b[6]>0 && b[2]>=2)) cout<<"YES"<<endl;
		else cout<<"NO"<<endl; 
	}  
}
 

你可能感兴趣的:(B. Applejack and Storages)