题目链接

Description

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, … from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.

Input

The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.

Output

For each test case write one line on the standard output:
Test case (case number): (number of crossings)

Sample Input

1

3 4 4

1 4

2 3

3 2

3 1

Sample Output

Test case 1: 5

题意

日本有东西两个海岸,东海岸有N个城市,西海岸有M个城市。城市之间一共修了K条路,问一共有几个交点。
当两条路A、B,A的左端点小于B的左端点,A的右端点大于B的右端点时相交。
所以先按照左端点排序,剩下的就是求逆序数。

AC

  • 树状数组(记得开long long)
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <map>
#include <bitset>
#include <set>
#include <string.h>
#include <cmath>
#include <queue>
#include <algorithm>
#define N 1005
#define P pair<int,int>
#define ll long long
#define lowbit(a) a&(-a)
#define pb(a) push_back(a)
#define mk(a, b) make_pair(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
#define read(a) scanf("%d", &a)
#define print(a) printf("%d\n", a)
using namespace std;
ll c[N];
ll getsum(int x) {ll ret = 0;while (x) {ret += c[x];x -= lowbit(x);}return ret;
}
void update(int x) {while (x < N) {c[x]++;x += lowbit(x);}
}
int main(){
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifint t;scanf("%d", &t);int Case =  1;while (t--) {mem(c, 0);int n, m, k;scanf("%d%d%d", &n, &m, &k);vector<P> a(k);for (int i = 0; i < k; ++i) {scanf("%d%d", &a[i].first, &a[i].second);}// 按照左端点升序,左端点相同又端点升序sort(a.begin(), a.end());ll ans = 0;for (int i = 0; i < k; ++i) {ans += getsum(N) - getsum(a[i].second);update(a[i].second);}printf("Test case %d: %lld\n", Case++, ans);}return 0;
}
  • 线段树
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <map>
#include <bitset>
#include <set>
#include <string.h>
#include <cmath>
#include <queue>
#include <algorithm>
#define N 4005
#define P pair<int,int>
#define ll long long
#define lowbit(a) a&(-a)
#define mk(a, b) make_pair(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;struct node{ll val;
}segtree[N];
void build(int root, int start, int end) {if (start == end)segtree[root].val = 0;else {int mid = (start + end) / 2;build(root * 2 + 1, start, mid);build(root * 2 + 2, mid + 1, end);segtree[root].val = segtree[root * 2 + 1].val + segtree[root * 2 + 2].val;}
}void update(int root, int start, int end, int index) {if (index > end || index < start)return;if (start == end) {if (start == index) {segtree[root].val++;}return;}int mid = (start + end) / 2;update(root * 2 + 1, start, mid, index);update(root * 2 + 2, mid + 1, end, index);segtree[root].val = segtree[root * 2 + 1].val + segtree[root * 2 + 2].val;
}
ll query(int root, int start, int end, int qstart, int qend) {if (qstart > end || qend < start)return 0;if (start >= qstart && end <= qend) {return segtree[root].val;}int mid = (start + end) / 2;return query(root * 2 + 1, start, mid, qstart, qend) + query(root * 2 + 2, mid + 1, end, qstart, qend);
}int main(){
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifint t;scanf("%d", &t);int Case =  1;while (t--) {int n, m, k;scanf("%d%d%d", &n, &m, &k);vector<P> a(k);for (int i = 0; i < k; ++i) {scanf("%d%d", &a[i].first, &a[i].second);}// 按照左端点升序,左端点相同又端点升序sort(a.begin(), a.end());build(0, 0, m + 1);ll ans = 0;for (int i = 0; i < k; ++i) {// 如果查询当前点,可能会被前面的区间包含ans += query(0, 0, m + 1, a[i].second + 1, m + 1);update(0, 0, m + 1, a[i].second);}printf("Test case %d: %lld\n", Case++, ans);}return 0;
}

POJ-3067 Japan(树状数组、线段树)相关推荐

  1. D-query SPOJ - DQUERY(求区间不同数的个数)(树状数组||线段树+离散)(主席树+在线)

    English Vietnamese Given a sequence of n numbers a1, a2, -, an and a number of d-queries. A d-query ...

  2. HDU 1556 前缀和 树状数组 线段树

    解法一: a[i]表示以 i作为起点,对 i-n的气球全部上色的次数  对(start,end)区间上色 ++a[start] --a[end+1]抵消掉 end+1-n的部分 问题转换为求 a的前缀 ...

  3. jzoj3854-分组【树状数组,线段树】

    正题 题目链接:https://jzoj.net/senior/#contest/show/2990/2 题目大意 一个小队满足要求 队长的地位最高 所有队员和队长的年龄差不超过kkk 给出nnn个人 ...

  4. 模板三连击:树状数组+线段树+主席树

    没事儿干,复习模板...... 1.树状数组 本来不想写这个的,但是反正就几分钟就打完了,所以就写了,水AC数. 洛谷 P3374 [模板]树状数组 1 1 #include<cstdio> ...

  5. 51nod 1680区间求和 (dp+树状数组/线段树)

    不妨考虑已知一个区间[l,r]的k=1.k=2....k=r-l+1这些数的答案ans(只是这一个区间,不包含子区间) 那么如果加入一个新的数字a[i](i = r+1) 则新区间[l, i]的答案为 ...

  6. CCF201709-5 除法(100分)【树状数组+线段树】

    试题编号: 201709-5 试题名称: 除法 时间限制: 10.0s 内存限制: 256.0MB 问题描述: 问题描述 小葱喜欢除法,所以他给了你N个数a1, a2, ⋯, aN,并且希望你执行M次 ...

  7. POJ2182 HDU2711 Lost Cows【树状数组+线段树】

    Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17113 Accepted: 10664 Descripti ...

  8. P2357 守墓人(树状数组/线段树)

    题目描述 在一个荒凉的墓地上 有一个令人尊敬的守墓人, 他看守的墓地从来 没有被盗过, 所以人们很放心的把自己的先人的墓 安顿在他那 守墓人能看好这片墓地是必然而不是偶然..... 因为....守墓人 ...

  9. 差分+树状数组 线段树【P2357】 守墓人

    题目描述-->p2357 守墓人 敲了一遍线段树,水过. 树状数组分析 主要思路: 差分 简单介绍一下差分(详细概念太麻烦,看下面. 给定一个数组 7 8 6 5 1 8 18 20 35 // ...

  10. [CSP-S模拟测试]:影魔(树状数组+线段树合并)

    题目背景 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万. 千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄. 每一个灵魂,都有着 ...

最新文章

  1. HashSet 详解
  2. git clone github_GitHub为什么连接缓慢以及解决方法
  3. 洛谷 - P2754 [CTSC1999]家园(最大流+并查集)
  4. SAP UI5 json model load data的原理
  5. 真神器!在家也能控制公司的电脑了
  6. 为什么我选择工作很难做选择_为什么公开工作(即使很难)
  7. 更新Sogou代理服务器程序,支持HTTPS
  8. vue-cli 2.6.9 安装卸载及创建一个工程
  9. Storm Player 字幕加载
  10. linux下载的安装包位置,及下载安装包到本地
  11. 这件小事,我坚持了 200 天
  12. Outlook邮箱设置签名
  13. Total Phase Data Center介绍
  14. 有原函数,可积、变限积分
  15. 红孩儿编辑器的模块设计6
  16. 如何将视频的每一帧提取成图片
  17. APK瘦身优化检测工具-Matrix ApkChecker 使用
  18. Android开发辅助工具类 Utils
  19. Chrome自带滚动截图
  20. 嗨格式数据恢复的 10 种最佳替代方法

热门文章

  1. 格式化输出,运算符,编码,字符串(索引,切片,大小写转换等等)
  2. java中日期与字符串之间的转换
  3. 2015年第六届蓝桥杯本科B组C++省赛个人题解
  4. typedef 与 define
  5. .Net 应用框架设计系列(二)
  6. php 接口继承接口
  7. 在Activity的Title中加入进度条
  8. 前台如何解析json格式 以及后台如何生成json格式
  9. CSDN修改博客皮肤模板
  10. 1031:反向输出一个三位数