[Codeforces 297E]Mystic Carvings
Description
Solution
这里给出主席树的版本。主席树维护直线的一个端点在前 \(i\) 个端点中,另一个端点在区间内的个数。
Code
//It is made by Awson on 2018.3.21
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 2e5;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); }
int n, a[N+5], b[N+5], to[N+5], len;
struct Segment_tree {
int root[N+5], ch[N*40+5][2], key[N*40+5], pos;
int cpynode(int o) {++pos; ch[pos][0] = ch[o][0], ch[pos][1] = ch[o][1], key[pos] = key[o]; return pos; }
void insert(int &o, int l, int r, int loc) {
o = cpynode(o); ++key[o];
if (l == r) return; int mid = (l+r)>>1;
if (loc <= mid) insert(ch[o][0], l, mid, loc);
else insert(ch[o][1], mid+1, r, loc);
}
int query(int o, int l, int r, int a, int b) {
if (!o || (a <= l && r <= b)) return key[o]; int mid = (l+r)>>1, c1 = 0, c2 = 0;
if (a <= mid) c1 = query(ch[o][0], l, mid, a, b);
if (b > mid) c2 = query(ch[o][1], mid+1, r, a, b);
return c1+c2;
}
int query(int a, int b, int l, int r) {
if (r < l || a > b) return 0;
return query(root[b], 1, len, l, r)-query(root[a-1], 1, len, l, r);
}
}T;
void work() {
read(n);
for (int i = 1; i <= n; i++) {
read(a[i]), read(b[i]); to[a[i]] = b[i], to[b[i]] = a[i];
if (a[i] > b[i]) Swap(a[i], b[i]);
}
len = (n<<1);
for (int i = 1; i <= len; i++) T.root[i] = T.root[i-1], T.insert(T.root[i], 1, len, to[i]);
LL ans = 1ll*n*(n-1)*(n-2)/2/3, t = 0;
for (int i = 1; i <= n; i++) {
int x = T.query(a[i]+1, b[i]-1, 1, a[i]-1)+T.query(a[i]+1, b[i]-1, b[i]+1, len);
t += 1ll*x*(n-1-x);
}
ans -= t/2;
for (int i = 1; i <= n; i++) {
int x = T.query(a[i]+1, b[i]-1, a[i]+1, b[i]-1)/2;
int y = (T.query(1, a[i]-1, 1, a[i]-1)+T.query(1, a[i]-1, b[i]+1, len)+T.query(b[i]+1, len, 1, a[i]-1)+T.query(b[i]+1, len, b[i]+1, len))/2;
ans -= 1ll*x*y;
}
writeln(ans);
}
int main() {work(); return 0; }
[Codeforces 297E]Mystic Carvings的更多相关文章
- codefroces 297E Mystic Carvings
problem:一个圆上依次有1~2*n的数字.每个数字都有且只有另一个数字与他相连.选出三条线,使得每条线的两端之间隔的最少点(只包括被选择的6个点)的个数相等.输入输出格式输入格式: The fi ...
- Codeforces Round #180 (Div. 1 + Div. 2)
A. Snow Footprints 如果只有L或者只有R,那么起点和终点都在边界上,否则在两者的边界. B. Sail 每次根据移动后的曼哈顿距离来判断是否移动. C. Parity Game 如果 ...
- 【题解】Codeforces Round #798 (Div. 2)
本篇为 Codeforces Round #798 (Div. 2) 也就是 CF1689 的题解,因本人水平比较菜,所以只有前四题 A.Lex String 题目描述 原题面 给定两个字符串 \(a ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- 20162320刘先润第三周Bag类测试
前言 以下内容是本周Bag代码的课后作业,要求是完成伪代码.产品代码和测试代码,为了书写方便我将伪代码以注释的形式写在了产品代码的后面 测试步骤 1.首先对Bag类引用BagInterface的代码进 ...
- Beta冲刺计划---Day0
Beta阶段报告---Day0 1.需要改进完善的功能 我们上一阶段开发由于开发时间匆忙,对于爬虫耗时的优化没有考虑.优化的空间我在Alpha阶段的总结报告里说过,具体看下图. 这张图显示出爱 ...
- day-6 机器学习概念及应用
学习玩Python基础语法,今天开始进行机器学习,首先了解下机器学习和深度学习的一些基本概念和术语: 1. 机器学习概念及应用 2. 深度学习概念及应用 3. 机器学习基本术语及举例 4. 机 ...
- Lock(三)查看是谁把表给锁了
查看是谁把表给锁了 select se1.inst_id as 被阻塞的会话节点, se2.inst_id as 罪魁祸首节点, se1.sid as 被阻塞的会话ID, ob.object_name ...
- Nginx动静分离架构
Nginx动静分离简单来说就将动态与静态资源分开,不能理解成只是单纯的把动态页面和静态页面物理分离,严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat,Res ...
- 初次面对c++
第一次实验 2-4源码: #include<iostream> using namespace std; int main() { int day; cin>>day; swi ...
- Spark快速入门
Spark 快速入门 本教程快速介绍了Spark的使用. 首先我们介绍了通过Spark 交互式shell调用API( Python或者scala代码),然后演示如何使用Java, Scala或者P ...
- python入门(13)获取函数帮助和调用函数
Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs,只有一个参数.可以直接从Python的官方网站查看文档: http://doc ...
- Spring Security入门(2-1)Spring Security - 重要的过滤器
1.自定义的filter机制 如果要对Web资源进行保护,最好的办法莫过于Filter,要想对方法调用进行保护,最好的办法莫过于AOP. Acegi对Web资源的保护,就是靠Filter实现的.Ace ...
- SQL Server数据库优化的10多种方法
巧妙优化sql server数据库的几种方法,在实际操作中导致查询速度慢的原因有很多,其中最为常见有以下的几种:没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷). I/O吞吐量小, ...