#include <iostream>
#include <unordered_map>
#include <set>
#include <string>
using namespace std;
#define MAXM (10)
#define MAXL (11)
struct meeting{
string name;
int m;
int mem[10];
int st;
int e;
bool deleted;
} pool[10005];
int cnt;
int cntm;
unordered_map <string,int> mem;
set <pair<int,int>> myset[20005];
unordered_map <string, int> meet;
void xoa(int id,int x){
for(int i=x;i<pool[id].m-1;i++){
pool[id].mem[i]=pool[id].mem[i+1];
}
pool[id].m--;
}
void init()
{
cntm=0;
cnt=0;
meet.clear();
mem.clear();
for(int i=0;i<20005;i++){
myset[i].clear();
}
}
bool check_overlap(int id,int st,int e){
pair<int,int> a;
a.first=st;
a.second=0;
auto it=myset[id].lower_bound(a);
if(it!=myset[id].end()) {
if(e>=(pool[it->second]).st) return true;
}
if(it!=myset[id].begin()){
it--;
if((pool[it->second]).e>=st) return true;
}
return false;
}
int addMeeting(char mMeeting[MAXL], int M, char mMemberList[MAXM][MAXL], int mStartTime, int mEndTime)
{
int ans=0;
pool[cnt].name=mMeeting;
int cnt_mem=0;
int dem=0;
pool[cnt].deleted=false;
pool[cnt].st=mStartTime;
pool[cnt].e=mEndTime;
for(int i=0; i<M; i++){
string s=mMemberList[i];
auto it=mem.find(s);
if(it==mem.end()) {
mem[s]=cntm;
pool[cnt].mem[cnt_mem]=cntm;
cnt_mem++;
pair<int, int> a;
a.first=mStartTime;
a.second=cnt;
myset[cntm].insert(a);
cntm++;
}
else{
if(check_overlap(it->second,mStartTime,mEndTime)) dem++;
else{
pool[cnt].mem[cnt_mem]=it->second;
cnt_mem++;
pair<int, int> a;
a.first=mStartTime;
a.second=cnt;
myset[it->second].insert(a);
}
}
}
if(M!=0){
pool[cnt].m=M-dem;
ans=M-dem;
meet[mMeeting]=cnt;
cnt++;
}
return ans;
}
int cancelMeeting(char mMeeting[MAXL])
{
auto it=meet.find(mMeeting);
if(it!=meet.end()){
if(pool[it->second].m==0||pool[it->second].deleted==true) return 0;
pool[it->second].deleted=true;
for(int i=0;i<pool[it->second].m;i++){
int id=pool[it->second].mem[i];
pair<int, int> a;
a.first=pool[it->second].st;
a.second=it->second;
myset[id].erase(a);
}
meet.erase(it);
return 1;
}
return 0;
}
int changeMeetingMember(char mMeeting[MAXL], char mMember[MAXL])
{
auto it1= meet.find(mMeeting);
if(it1==meet.end()) return -1;
else {
if(pool[it1->second].deleted==true||pool[it1->second].m==0) return -1;
auto it2=mem.find(mMember);
if(it2==mem.end()){
mem[mMember]=cntm;
pair<int, int> a;
a.first=pool[it1->second].st;
a.second=it1->second;
myset[cntm].insert(a);
pool[it1->second].mem[pool[it1->second].m]=cntm;
pool[it1->second].m++;
cntm++;
return 1;
}
else{
for(int i=0;i<pool[it1->second].m; i++){
if(pool[it1->second].mem[i]==it2->second){
xoa(it1->second,i);
pair<int,int> c;
c.first=pool[it1->second].st;
c.second=it1->second;
myset[it2->second].erase(c);
pair <int, int > a;
a.first=pool[it1->second].st;
a.second=it1->second;
myset[cntm].erase(a);
if(pool[it1->second].m==0) {
pool[it1->second].deleted=true;
meet.erase(pool[it1->second].name);
}
return 0;
}
}
if(pool[it1->second].m==10) return 2;
if(check_overlap(it2->second,pool[it1->second].st,pool[it1->second].e)) return 2;
pool[it1->second].mem[pool[it1->second].m]=it2->second;
pool[it1->second].m++;
pair<int, int> b;
b.first= pool[it1->second].st;
b.second=it1->second;
myset[it2->second].insert(b);
return 1;
}
}
}
int changeMeeting(char mMeeting[MAXL], int mStartTime, int mEndTime)
{
auto it=meet.find(mMeeting);
if(it!=meet.end()){
if(pool[it->second].deleted==true) return 0;
for(int i=0;i<pool[it->second].m;i++){
pair<int, int> a;
a.first=pool[it->second].st;
a.second=it->second;
myset[pool[it->second].mem[i]].erase(a);
}
pool[it->second].st=mStartTime;
pool[it->second].e=mEndTime;
int mem_save[10];
int dem=0;
for(int i=0; i<pool[it->second].m;i++){
if(!check_overlap(pool[it->second].mem[i],pool[it->second].st,pool[it->second].e)) {
mem_save[dem]=pool[it->second].mem[i];
dem++;
pair<int, int> b;
b.first=pool[it->second].st;
b.second=it->second;
myset[pool[it->second].mem[i]].insert(b);
}
}
if(dem==0)
{
pool[it->second].deleted=true;
meet.erase(mMeeting);
return 0;
}
else {
pool[it->second].m=dem;
for(int i=0;i<dem;i++){
pool[it->second].mem[i]=mem_save[i];
}
return dem;
}
}
}
void checkNextMeeting(char mMember[MAXL], int mTime, char mResult[MAXL])
{
mResult[0]='\0';
auto it=mem.find(mMember);
if(it!=mem.end()) {
int id=it->second;
pair<int,int> a;
a.first=mTime;
a.second=999999;
auto it1=myset[id].upper_bound(a);
if(it1!=myset[id].end()){
int idx=(*it1).second;
string s=pool[idx].name;
for(int i=0;i<s.length();i++){
mResult[i]=s[i];
}
mResult[s.length()]='\0';
}
}
}