0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

2141. 排队

https://hydro.ac/d/bzoj/record/63a45898cf75a716a1672f72
This commit is contained in:
Baoshuo Ren 2022-12-22 21:16:58 +08:00
parent 86d5635288
commit 08b27afab5
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
41 changed files with 267 additions and 0 deletions

147
BZOJ/2141/2141.cpp Normal file
View File

@ -0,0 +1,147 @@
#include <iostream>
#include <algorithm>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 2e4 + 5;
int n, m, h[N], ans;
std::vector<int> nums;
class SegmentTree {
private:
struct node {
int l, r;
int sum;
node *lchild, *rchild;
node(const int &_l = 0, const int &_r = 0)
: l(_l), r(_r), sum(0), lchild(nullptr), rchild(nullptr) {}
void pushup() {
sum = 0;
if (lchild != nullptr) sum += lchild->sum;
if (rchild != nullptr) sum += rchild->sum;
}
};
node *root;
void modify(node *&cur, int l, int r, int pos, int val) {
if (cur == nullptr) cur = new node(l, r);
if (l == r) {
cur->sum += val;
return;
}
int mid = l + r >> 1;
if (pos <= mid) modify(cur->lchild, l, mid, pos, val);
else modify(cur->rchild, mid + 1, r, pos, val);
cur->pushup();
}
int query(node *cur, int l, int r, int ql, int qr) {
if (cur == nullptr) return 0;
if (ql <= l && r <= qr) return cur->sum;
int mid = l + r >> 1,
res = 0;
if (ql <= mid) res += query(cur->lchild, l, mid, ql, qr);
if (qr > mid) res += query(cur->rchild, mid + 1, r, ql, qr);
return res;
}
public:
SegmentTree()
: root(nullptr) {}
void modify(int pos, int val) {
modify(root, 1, n, pos, val);
}
int query(int ql, int qr) {
return query(root, 1, n, ql, qr);
}
} tr[N];
int lowbit(int x) {
return x & -x;
}
void add(int x, int y, int z) {
for (; x <= n; x += lowbit(x)) tr[x].modify(y, z);
}
int sum(int x, int l, int r) {
int res = 0;
for (; x; x -= lowbit(x)) res += tr[x].query(l, r);
return res;
}
int get(int x) {
return sum(x - 1, h[x] + 1, n) + sum(n, 1, h[x] - 1) - sum(x - 1, 1, h[x] - 1);
}
int find(int x) {
return std::lower_bound(nums.begin(), nums.end(), x) - nums.begin() + 1;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
nums.emplace_back(h[i]);
}
std::sort(nums.begin(), nums.end());
std::transform(h + 1, h + 1 + n, h + 1, find);
for (int i = 1; i <= n; i++) {
add(i, h[i], 1);
ans += sum(i - 1, h[i] + 1, n);
}
cout << ans << endl;
cin >> m;
while (m--) {
int x, y;
cin >> x >> y;
if (x > y) std::swap(x, y);
if (h[x] > h[y]) ans++;
ans -= get(x) + get(y);
add(x, h[x], -1);
add(y, h[y], -1);
std::swap(h[x], h[y]);
if (h[x] > h[y]) ans--;
add(x, h[x], 1);
add(y, h[y], 1);
ans += get(x) + get(y);
cout << ans << endl;
}
return 0;
}

BIN
BZOJ/2141/data/1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/11.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/11.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/12.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/12.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/13.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/13.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/14.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/14.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/15.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/15.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/16.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/16.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/17.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/17.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/18.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/18.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/19.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/19.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/20.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/20.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
BZOJ/2141/data/9.out (Stored with Git LFS) Normal file

Binary file not shown.