-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78dd3b3
commit 743a9c1
Showing
6 changed files
with
330 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
using namespace std; | ||
|
||
//1-index的树去movieCollection_UVA1513.cpp查看 | ||
|
||
class fenwickTree | ||
{ | ||
private: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <fstream> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <list> | ||
#include <string> | ||
#include <vector> | ||
#include <set> | ||
#include <map> | ||
|
||
using namespace std; | ||
|
||
ifstream fin("movieCollection_UVA1513.in"); | ||
ofstream fout("movieCollection_UVA1513.out"); | ||
|
||
class fenwickTree | ||
{ | ||
private: | ||
|
||
int lowbit(int x) | ||
{ | ||
return x & (-x); | ||
} | ||
|
||
public: | ||
|
||
vector<int> a; | ||
vector<int> s; | ||
|
||
fenwickTree(vector<int> & in) | ||
{ | ||
int n = in.size(); | ||
|
||
a.assign(n, 0); | ||
s.assign(n, 0); | ||
|
||
for (int i = 1; i <= n - 1; ++i) | ||
{ | ||
update(i, in[i]); | ||
} | ||
} | ||
|
||
void update(int k, int delta) | ||
{ | ||
int n = s.size() - 1; | ||
|
||
a[k] += delta; | ||
|
||
for (int i = k; i <= n; i += lowbit(i)) | ||
{ | ||
s[i] += delta; | ||
} | ||
} | ||
|
||
int query(int k) | ||
{ | ||
int n = s.size() - 1; | ||
int ans = 0; | ||
|
||
for (int i = k; i >= 1; i -= lowbit(i)) | ||
{ | ||
ans += s[i]; | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
int sum(int i, int j) | ||
{ | ||
return query(j) - query(i - 1); | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
int testCase; fin >> testCase; | ||
for (int t = 1; t <= testCase; ++t) | ||
{ | ||
int n, m; fin >> n >> m; | ||
|
||
vector<int> temp(n + m + 1, 0), pos(n + 1, 0); | ||
|
||
for (int i = m + 1; i <= m + n; ++i) | ||
{ | ||
temp[i] = 1; | ||
} | ||
|
||
for (int i = 1; i <= n; ++i) | ||
{ | ||
pos[i] = m + i; | ||
} | ||
|
||
fenwickTree tree(temp); | ||
|
||
int j = m; | ||
for (int c = 1; c <= m; ++c) | ||
{ | ||
if (c > 1) fout << ' '; | ||
|
||
int r; fin >> r; | ||
|
||
fout << tree.query(pos[r]) - 1; | ||
|
||
tree.update(pos[r], -1); | ||
tree.update(j, 1); | ||
|
||
pos[r] = j; | ||
--j; | ||
} | ||
fout << '\n'; | ||
} | ||
|
||
return 0; | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <fstream> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <list> | ||
#include <string> | ||
#include <vector> | ||
#include <set> | ||
#include <map> | ||
|
||
using namespace std; | ||
|
||
|
||
class fenwickTree | ||
{ | ||
private: | ||
|
||
int lowbit(int x) | ||
{ | ||
return x & (-x); | ||
} | ||
|
||
public: | ||
|
||
vector<int> a; | ||
vector<int> s; | ||
|
||
fenwickTree(vector<int> & in) | ||
{ | ||
int n = in.size(); | ||
|
||
a.assign(n, 0); | ||
s.assign(n, 0); | ||
|
||
for (int i = 1; i <= n - 1; ++i) | ||
{ | ||
update(i, in[i]); | ||
} | ||
} | ||
|
||
void update(int k, int delta) | ||
{ | ||
int n = s.size() - 1; | ||
|
||
a[k] += delta; | ||
|
||
for (int i = k; i <= n; i += lowbit(i)) | ||
{ | ||
s[i] += delta; | ||
} | ||
} | ||
|
||
int query(int k) | ||
{ | ||
int n = s.size() - 1; | ||
int ans = 0; | ||
|
||
for (int i = k; i >= 1; i -= lowbit(i)) | ||
{ | ||
ans += s[i]; | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
int sum(int i, int j) | ||
{ | ||
return query(j) - query(i - 1); | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(false); | ||
std::cin.tie(NULL); | ||
int testCase; cin >> testCase; | ||
for (int t = 1; t <= testCase; ++t) | ||
{ | ||
int n, m; cin >> n >> m; | ||
|
||
vector<int> temp(n + m + 1, 0), pos(n + 1, 0); | ||
|
||
for (int i = m + 1; i <= m + n; ++i) | ||
{ | ||
temp[i] = 1; | ||
} | ||
|
||
for (int i = 1; i <= n; ++i) | ||
{ | ||
pos[i] = m + i; | ||
} | ||
|
||
fenwickTree tree(temp); | ||
|
||
int j = m; | ||
for (int c = 1; c <= m; ++c) | ||
{ | ||
if (c > 1) cout << ' '; | ||
|
||
int r; cin >> r; | ||
|
||
cout << tree.query(pos[r]) - 1; | ||
|
||
tree.update(pos[r], -1); | ||
tree.update(j, 1); | ||
|
||
pos[r] = j; | ||
--j; | ||
} | ||
cout << '\n'; | ||
} | ||
|
||
cout.flush(); | ||
return 0; | ||
} | ||
|