Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by willj on 11/2/2019.
- //
- #include <iostream>
- #include <fstream>
- #define MAX_N 100000
- using namespace std;
- pair<long long, long long> count = make_pair(0, 0);
- struct YNode {
- int val;
- YNode *c[2];
- YNode() {
- val = 0;
- c[0] = c[1] = NULL;
- }
- YNode* getC(int i) {
- if (!c[i]) {
- c[i] = new YNode();
- }
- return c[i];
- }
- void add(int y, int v, int a = 0, int b = MAX_N + 10) {
- count.second++;
- val += v;
- if (a == b) return;
- int mid = (a + b) / 2;
- if (y <= mid) getC(0)->add(y, v, a, mid);
- else getC(1)->add(y, v, mid + 1, b);
- }
- int query(int y1, int y2, int a = 0, int b = MAX_N + 10) {
- count.first++;
- if (a >= y1 && b <= y2) {
- return val;
- }
- if (a > y2 || b < y1) return 0;
- int mid = (a + b) / 2;
- return (c[0] != NULL ? c[0]->query(y1, y2, a, mid) : 0) +
- (c[1] != NULL ? c[1]->query(y1, y2, mid + 1, b) : 0);
- }
- };
- struct XNode {
- YNode* val;
- XNode* c[2];
- XNode() {
- val = new YNode();
- c[0] = c[1] = NULL;
- }
- XNode* getC(int i) {
- if (!c[i]) {
- c[i] = new XNode();
- }
- return c[i];
- }
- void add(int x, int y, int v, int a = 0, int b = MAX_N + 10) {
- count.second++;
- val->add(y, v);
- if (a == b) return;
- int mid = (a + b) / 2;
- if (x <= mid) getC(0)->add(x, y, v, a, mid);
- else getC(1)->add(x, y, v, mid + 1, b);
- }
- int query(int x1, int y1, int x2, int y2, int a = 0, int b = MAX_N + 10) {
- count.first++;
- if (a >= x1 && b <= x2) return val->query(y1, y2);
- if (a > x2 || b < x1) return 0;
- int mid = (a + b) / 2;
- return (c[0] != NULL ? c[0]->query(x1, y1, x2, y2, a, mid) : 0) +
- (c[1] != NULL ? c[1]->query(x1, y1, x2, y2, mid + 1, b) : 0);
- }
- };
- int cow[MAX_N + 1][2];
- int pos[MAX_N + 1][2];
- int main() {
- ios_base::sync_with_stdio(false);
- ifstream cin("src/competition/test.in");
- ofstream cout("src/competition/test.out");
- int n, k; cin >> n >> k;
- for (int j = 0; j < 2; j++) {
- for (int i = 0; i < n; i++) {
- cin >> cow[i][j]; cow[i][j]--;
- pos[cow[i][j]][j] = i;
- }
- }
- int tot = 0;
- XNode root;
- for (int i = 0; i < n; i++) {
- tot += root.query(0, pos[i][1], pos[i][0], n + 1);
- tot += root.query(pos[i][0], 0, n + 1, pos[i][1]);
- root.add(pos[i][0], pos[i][1], 1);
- }
- for (int i = 0; i < n; i++) {
- root.add(pos[i][0], pos[i][1], -1);
- }
- int res = 0;
- for (int i = 0; i < n; i++) {
- res += root.query(0, pos[i][1], pos[i][0], n + 1);
- res += root.query(pos[i][0], 0, n + 1, pos[i][1]);
- root.add(pos[i][0], pos[i][1], 1);
- if (i - k >= 0) root.add(pos[i - k][0], pos[i - k][1], -1);
- }
- cout << tot - res << endl;
- cout << count.first << " " << count.second << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement