-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsample.cpp
More file actions
109 lines (103 loc) · 2.2 KB
/
sample.cpp
File metadata and controls
109 lines (103 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
class Node
{
public:
string val;
int self_uid;
map<int,int> uid;
bool isLock()
{
return is_locked;
}
bool Lock(int ud,map<string,Node *> &mp)
{
if((int)des.size()>0)
return false;
Node* parent=_parent;
while(parent)
{
if(parent->isLock())
return false;
parent=parent->_parent;
}
parent=_parent;
while(parent)
{
parent->des.insert(val);
parent->uid[ud]++;
parent=parent->_parent;
}
self_uid=ud;
is_locked=true;
return true;
}
bool unlock(int ud,map<string,Node*> &mp)
{
if(isLock() and self_uid==ud)
{
Node* parent=_parent;
while(parent)
{
parent->des.erase((val));
parent->uid[ud]--;
if(parent->uid[ud]==0)
parent->uid.erase(ud);
parent=parent->_parent;
}
is_locked=false;
return true;
}
return false;
}
bool upgrade(int ud,map<string,Node*> &mp)
{
if(uid.size()==1 and uid.begin()->first == ud)
{
bool ans=true;
for(auto i:des)
{
ans=ans && mp[i]->unlock(ud,mp);
}
return Lock(ud,mp);
}
return false;
}
};
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
map<string,Node*> mp;
int n,m,k;
cin>>n>>m>>k;
vector<Node> A(n);
for(auto &i:A) cin>>i.val;
for(int i=0;i<n;i++) mp[A[i].val]=&A[i];
for(int i=0;m*i<n;i++)
{
for(int j=m*i+1;j<=m*i+m and j<n;j++)
{
A[i].children.push_back(&A[j]);
A[j]._parent=&A[i];
}
}
for(int i=0,a,c;i<k;i++)
{
string b;
cin>>a>>b>>c;
if(a==1)
{
cout<<(mp[b]->Lock(c,mp)?"true":"false")<<"\n";
}
else if(a==2)
{
cout<<(mp[b]->unlock(c,mp)?"true":"false")<<"\n";
}
else{
cout<<(mp[b]->upgrade(c,mp)?"true":"false")<<"\n";
}
}
}