Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
gzshawnliang committed Jan 26, 2019
1 parent 78dd3b3 commit 743a9c1
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Lib/SegmentTree/fenwickTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using namespace std;

//1-index的树去movieCollection_UVA1513.cpp查看

class fenwickTree
{
private:
Expand Down
2 changes: 1 addition & 1 deletion myCpps/!-OJcreater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using namespace std;

//***************************************
const string CPPfile = "potentiometers_UVA12086"; //*
const string CPPfile = "movieCollection_UVA1513"; //*
//***************************************

ifstream fin(CPPfile + ".cpp");
Expand Down
117 changes: 117 additions & 0 deletions myCpps/movieCollection_UVA1513.cpp
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;
}
61 changes: 61 additions & 0 deletions myCpps/movieCollection_UVA1513.in

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions myCpps/movieCollection_UVA1513.out

Large diffs are not rendered by default.

119 changes: 119 additions & 0 deletions myCpps/movieCollection_UVA1513OJ.cpp
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;
}

0 comments on commit 743a9c1

Please sign in to comment.