STL的find函数竟然不是二分查找!
binary_search竟然返回的是bool!
还是lower_bound/upper_bound好用。。
binary_search竟然返回的是bool!
还是lower_bound/upper_bound好用。。
#ifdef _DEBUG
#pragma warning(disable : 4996)
#endif
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define CLOSE() ios::sync_with_stdio(false)
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define PF(a) printf("%d\n", a)
#define SF(a) scanf("%d", &a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define maxn 100005
#define maxm 10005
#define MOD 2000000000
#define INF 10007
using namespace std;
//-------------------------CHC------------------------------//
//计蒜客 Div2 Math of the Ninja
//树状数组 + 离散化
#define lowbit(x) x & -x
int n;
struct Node {
int x, y;
bool operator<(const Node &r) const {
return x < r.x;
}
}node[maxn];
int a[maxn];
int bit[maxn];
void Update(int i) {
while (i <= n) {
bit[i]++;
i += lowbit(i);
}
}
int sum(int i) {
int ret = 0;
while (i) {
ret += bit[i];
i -= lowbit(i);
}
return ret;
}
void Compress() {
vector<int> v;
FOR(i, 0, n) v.push_back(node[i].y);
sort(v.begin(), v.end());
FOR(i, 0, n) a[i] = lower_bound(v.begin(), v.end(), node[i].y) - v.begin() + 1;
}
int main() {
int T, kase = 1;
SF(T);
while (T--) {
CLEAR(bit, 0);
SF(n);
FOR(i, 0, n) SFF(node[i].x, node[i].y);
sort(node, node + n);
Compress();
//FOR(i, 0, n) printf("a = %d", a[i]);
LL ans = 0;
FOR(i, 0, n) {
int t = a[i];
ans += i - sum(t);
Update(t);
}
printf("Case #%d: %lld\n", kase++, ans);
}
return 0;
}
equal_range 用法
// equal_range example
#include <iostream> // std::cout
#include <algorithm> // std::equal_range, std::sort
#include <vector> // std::vector
bool mygreater (int i,int j) { return (i>j); }
int main () {
int myints[] = {10,20,30,30,20,10,10,20};
std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
std::pair<std::vector<int>::iterator,std::vector<int>::iterator> bounds;
// using default comparison:
std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
bounds=std::equal_range (v.begin(), v.end(), 20); // ^ ^
// using "mygreater" as comp:
std::sort (v.begin(), v.end(), mygreater); // 30 30 20 20 20 10 10 10
bounds=std::equal_range (v.begin(), v.end(), 20, mygreater); // ^ ^
std::cout << "bounds at positions " << (bounds.first - v.begin());
std::cout << " and " << (bounds.second - v.begin()) << '\n';
return 0;
}
归并排序求逆序数
#ifdef _DEBUG
#pragma warning(disable : 4996)
#endif
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define CLOSE() ios::sync_with_stdio(false)
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define PF(a) printf("%d\n", a)
#define SF(a) scanf("%d", &a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define maxn 100005
#define maxm 10005
#define MOD 2000000000
#define INF 10007
using namespace std;
//-------------------------CHC------------------------------//
struct Node {
int x, y;
bool operator<(const Node &r) const {
return x < r.x;
}
}node[maxn];
int a[maxn], t[maxn];
LL cnt;
void Merge(int l, int mid, int r) {
int i = l, j = mid, k = 0;
while (i < mid && j < r) {
if (a[i] <= a[j]) t[k++] = a[i++];
else t[k++] = a[j++], cnt += mid - i;
}
while (i < mid) t[k++] = a[i++];
while (j < r) t[k++] = a[j++];
FOR(i, 0, k) a[l + i] = t[i];
}
void MergeSort(int l, int r) {
if (r - l > 1) {
int mid = (r + l) >> 1;
MergeSort(l, mid);
MergeSort(mid, r);
Merge(l, mid, r);
}
}
int main() {
int T, kase = 1;
SF(T);
while (T--) {
int n;
SF(n);
FOR(i, 0, n) SFF(node[i].x, node[i].y);
sort(node, node + n);
FOR(i, 0, n) a[i] = node[i].y;
cnt = 0;
MergeSort(0, n);
printf("Case #%d: %lld\n", kase++, cnt);
}
return 0;
}