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 28, 2019
1 parent 3427674 commit a6b7eaf
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 2 deletions.
58 changes: 57 additions & 1 deletion Lib/SegmentTree/fenwickTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,63 @@

using namespace std;

//1-index的树去movieCollection_UVA1513.cpp查看
class fenwickTree1index
{
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);
}
};

class fenwickTree
{
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 = "theSkylineProblem_UVA105"; //*
const string CPPfile = "pingPong_UVA1428"; //*
//***************************************

ifstream fin(CPPfile + ".cpp");
Expand Down
101 changes: 101 additions & 0 deletions myCpps/pingPong_UVA1428.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <bits/stdc++.h>

using namespace std;

ifstream fin("pingPong_UVA1428.in");
ofstream fout("pingPong_UVA1428.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);
}

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; fin >> n;

vector<int> s(100001, 0);
fenwickTree treeL(s), treeR(s);

vector<int> a(n, 0), l(n, 0), r(n, 0);
for (int i = 0; i <= n - 1; ++i)
{
fin >> a[i];
}

for (int i = 0; i <= n - 1; ++i)
{
treeL.update(a[i], 1);
l[i] = treeL.query(a[i]);
}

for (int i = n - 1; i >= 0; --i)
{
treeR.update(a[i], 1);
r[i] = treeR.query(a[i]);
}

long long ans = 0;
for (int i = 0; i <= n - 1; ++i)
{
int minL = l[i] - 1, minR = r[i] - 1;
ans += (minL * (n - i - 1 - minR) + (i - minL) * minR);
}

fout << ans << '\n';
}

return 0;
}
2 changes: 2 additions & 0 deletions myCpps/pingPong_UVA1428.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
3 1 2 3
1 change: 1 addition & 0 deletions myCpps/pingPong_UVA1428.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
103 changes: 103 additions & 0 deletions myCpps/pingPong_UVA1428OJ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <bits/stdc++.h>

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);
}

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; cin >> n;

vector<int> s(100001, 0);
fenwickTree treeL(s), treeR(s);

vector<int> a(n, 0), l(n, 0), r(n, 0);
for (int i = 0; i <= n - 1; ++i)
{
cin >> a[i];
}

for (int i = 0; i <= n - 1; ++i)
{
treeL.update(a[i], 1);
l[i] = treeL.query(a[i]);
}

for (int i = n - 1; i >= 0; --i)
{
treeR.update(a[i], 1);
r[i] = treeR.query(a[i]);
}

long long ans = 0;
for (int i = 0; i <= n - 1; ++i)
{
int minL = l[i] - 1, minR = r[i] - 1;
ans += (minL * (n - i - 1 - minR) + (i - minL) * minR);
}

cout << ans << '\n';
}

cout.flush();
return 0;
}

0 comments on commit a6b7eaf

Please sign in to comment.