Sets-STL C++
Problem Statement :
Sets are a part of the C++ STL. Sets are containers that store unique elements following a specific order. Here are some of the frequently used member functions of sets: Declaration: set<int>s; //Creates a set of integers. Size: int length=s.size(); //Gives the size of the set. Insert: s.insert(x); //Inserts an integer x into the set s. Erasing an element: s.erase(val); //Erases an integer val from the set s. Finding an element: set<int>::iterator itr=s.find(val); //Gives the iterator to the element val if it is found otherwise returns s.end() . Ex: set<int>::iterator itr=s.find(100); //If 100 is not present then it==s.end(). To know more about sets click Here. Coming to the problem, you will be given Q queries. Each query is of one of the following three types: 1 x: Add an element x to the set. 2 x: Delete an element x from the set. (If the number x is not present in the set, then do nothing). 3 x: If the number x is present in the set, then print "Yes"(without quotes) else print "No"(without quotes). Input Format The first line of the input contains Q where Q is the number of queries. The next Q lines contain 1 query each. Each query consists of two integers y and x where y is the type of the query and x is an integer. Constraints 1 <= Q <= 10^5 1 <= y <= 3 1 <= x <= 10^9 Output Format For queries of type 3 print "Yes"(without quotes) if the number x is present in the set and if the number is not present, then print "No"(without quotes). Each query of type 3 should be printed in a new line.
Solution :
Solution in C :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int main() {
int c,t,n;
scanf("%d",&n);
set<int> ns;
while(n--) {
scanf("%d %d",&c,&t);
switch(c) {
case 1:
ns.insert(t);
break;
case 2:
ns.erase(t);
break;
case 3:
if (ns.find(t)!=ns.end())
cout << "Yes"<<endl;
else
cout <<"No"<<endl;
break;
default:
cout<<"invalid switch value: "<<c<<endl;
}
}
return 0;
}
View More Similar Problems
No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →Direct Connections
Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do
View Solution →Subsequence Weighting
A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =
View Solution →Kindergarten Adventures
Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti
View Solution →Mr. X and His Shots
A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M
View Solution →