[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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- Django+xadmin打造在线教育平台(八)
十一.用户信息 11.1.个人信息展示 (1)新建‘usercenter-bae.html’当母板 {% load staticfiles %} <!DOCTYPE html> <h ...
- IntelliJIDEA中如何使用JavaDoc
IntelliJ IDEA 12.1.6,本身提供了很好的 JavaDoc 生成功能,以及标准 JavaDoc 注释转换功能,其实质是在代码编写过程中,按照标准 JavaDoc 的注释要求,为需要暴露 ...
- spring框架学习笔记5:SpringAOP示例
1.导包: 导入spring中的这两个包 再导入其他包(网上下载): 2.准备目标对象: package service; public class UserServiceImpl implement ...
- python实现k-近邻算法
参考:<机器学习实战>- Machine Learning in Action 一. 必备的包 实现此算法需要准备以下的包: • matplotlib,用于绘图 • numpy,数组处理库 ...
- Twisted 延迟调用
延迟(defer)是twisted框架中实现异步的编程体系,使程序设计可以采用事件驱动的机制 1.基本使用 defer可以看作一个管理回调函数的对象,可以向该对象添加需要的回调函数同时也可以指定该组函 ...
- 码农、黑客和2B程序员之间的区别
码农: 黑客: 2B程序员: 求2的32次方: 码农: System.out.println(Math.pow(2, 32)); 黑客: System.out.println(1L<<32 ...
- Python 列表嵌套多种实现方式
#coding=utf-8 list=[] for i in range(1,101): list.append(i) # print(list) tempList=[] newList=[] whi ...
- c#动态加载卸载DLL
前段时间工作的时候遇到一个问题.就是需要每次启动程序的时候动态替换掉某个dll,所以就百度了这方面的资料.这次记录下来让自己以后可以看. 根据自己的理解,动态卸载dll需要有以下条件: 1:dll在加 ...
- java对象转字节数组,获取泛型类
对象转字节数组,字节数组在恢复成对象 Test.java class Test { public static void main(String args[]) throws IOException, ...
- s遇到错误不要慌,教你方法走四方
我觉得不管是新手还是老手,他们都会出错,有些错误控制台会报错,而有些错误控制台不会报错 面对不会报错的时候,就有一些人烦恼,不知道怎么办了,久而久之,就失去了对学习的乐趣. 所以我在这里说一下对错误处 ...