Codeforces 853C - Boredom
853C - Boredom
题意
给出一个矩阵,每行每列有且仅有一个点。每次询问一个子矩形,问这些点构成的矩形有多少个与给定的矩形相交(两个处于对角线上的点可以组成矩形)。
分析
考虑矩形周围 8 个方向,答案其实就是这些方向上的点的组合。直接去算相交比较麻烦,我们可以考虑去算不相交的矩形的个数,例如上方有 \(x\) 个点,则要减去矩形的个数 \(\frac{x * (x - 1)}{2}\) ,下左右同理,但是这样会多减去左下角、左上角、右上角、右下角四个区域的点组成的矩形的个数,考虑再加回来,那我们实际上就要高效算出这些区域内点的个数。二维平面统计点的个数,上主席树。
再讲讲主席树查询的那部分,和线段树很类似(废话)。为什么它可以统计纵轴方向上某个区间点的个数呢?注意到在插入数据的时候我们是根据值的大小去决定走左边还是右边的,在查询的时候,同样根据值的大小决定走左还是走右(这个值此时是区间端点的值)。
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l, m
#define rson m + 1, r
const int MAXN = 2e5 + 10;
int L[MAXN << 5], R[MAXN << 5], sum[MAXN << 5];
int tot;
int f[MAXN];
int build(int l, int r) {
int rt = ++tot;
sum[rt] = 0;
if(l < r) {
int m = l + r >> 1;
L[rt] = build(lson);
R[rt] = build(rson);
}
return rt;
}
int update(int pre, int l, int r, int x) {
int rt = ++tot;
L[rt] = L[pre]; R[rt] = R[pre]; sum[rt] = sum[pre] + 1;
if(l < r) {
int m = l + r >> 1;
if(x <= m) L[rt] = update(L[pre], lson, x);
else R[rt] = update(R[pre], rson, x);
}
return rt;
}
ll query(int ql, int qr, int l_, int r_, int l, int r) {
if(l >= l_ && r <= r_) return sum[qr] - sum[ql];
int m = (l + r) / 2;
ll res = 0;
if(m >= l_) res += query(L[ql], L[qr], l_, r_, lson);
if(m < r_) res += query(R[ql], R[qr], l_, r_, rson);
return res;
}
ll cal(ll x) { return x * (x - 1) / 2; }
int main() {
tot = 0;
int n, q;
scanf("%d%d", &n, &q);
f[0] = build(1, n);
for(int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
f[i] = update(f[i - 1], 1, n, x);
}
while(q--) {
int l, d, r, u;
scanf("%d%d%d%d", &l, &d, &r, &u);
ll res = cal(n) - cal(l - 1) - cal(n - r) - cal(d - 1) - cal(n - u);
if(d > 1) {
res += cal(query(f[0], f[l - 1], 1, d - 1, 1, n));
res += cal(query(f[r], f[n], 1, d - 1, 1, n));
}
if(u < n) {
res += cal(query(f[0], f[l - 1], u + 1, n, 1, n));
res += cal(query(f[r], f[n], u + 1, n, 1, n));
}
printf("%I64d\n", res);
}
return 0;
}
Codeforces 853C - Boredom的更多相关文章
- [Codeforces Round #433][Codeforces 853C/854E. Boredom]
题目链接:853C - Boredom/854E - Boredom 题目大意:在\(n\times n\)的方格中,每一行,每一列都恰有一个被标记的方格,称一个矩形为漂亮的当且仅当这个矩形有两个角是 ...
- CodeForces 456-C Boredom
题目链接:CodeForces -456C Description Alex doesn't like boredom. That's why whenever he gets bored, he c ...
- CodeForces 455A Boredom (DP)
Boredom 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/G Description Alex doesn't like b ...
- Codeforces 455A - Boredom - [DP]
题目链接:https://codeforces.com/problemset/problem/455/A 题意: 给出一个 $n$ 个数字的整数序列 $a[1 \sim n]$,每次你可以选择一个 $ ...
- Codeforces 445A Boredom(DP+单调队列优化)
题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...
- Codeforces 455A Boredom (线性DP)
<题目链接> 题目大意:给定一个序列,让你在其中挑选一些数,如果你选了x,那么你能够得到x分,但是该序列中所有等于x-1和x+1的元素将全部消失,问你最多能够得多少分. 解题分析:从小到大 ...
- Codeforces 455A Boredom 取数字的dp
题目链接:点击打开链接 给定一个n长的序列 删除x这个数就能获得x * x的个数 的分数,然后x+1和x-1这2个数会消失.即无法获得这2个数的分数 问最高得分. 先统计每一个数出现的次数.然后dp一 ...
- Codeforces Round #260 (Div. 1) A - Boredom DP
A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...
- DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...
随机推荐
- [MySQL] explain执行计划解读
Explain语法 EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可得 ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 在xml文件中引入带有@Configuration的Java类
在xml引入带有@Configuration的Java类,就是把这个带有@Configuration的Java类,当做一个普通的的类用<bean>标签引入: 核心代码如下: @Config ...
- idea中mybatis-plugin破解
Mybatis Plugin 一.Mybatis Plugin插件是什么 提供Mapper接口与配置文件中对应SQL的导航 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及 ...
- javascript简易下拉菜单效果
JS代码: window.onload=function(){ var oDiv=document.getElementById('navMenu'); var aUl=oDiv.getElement ...
- PostgreSQL(Linux)安装、启动、停止、重启
If we don't already have PostgreSQL installed, we must install it. $ sudo apt-get install postgresql ...
- 最短路径算法 SP
dijkstra求最短路径长度 dijkstra求最短路并记录路径 #include<stdio.h> #include<string.h> #include<stack ...
- HDU2571--命运---DP
http://acm.hdu.edu.cn/showproblem.php?pid=2571 #include "iostream" #include "cstdio&q ...
- Python基础(7)闭包函数、装饰器
一.闭包函数 闭包函数:1.函数内部定义函数,成为内部函数, 2.改内部函数包含对外部作用域,而不是对全局作用域名字的引用 那么该内部函数成为闭包函数 #最简单的无参闭包函数 def func1() ...
- final 的作用
1.修饰类 类不能被继承 2.修饰方法 目的有二: 1)禁止子类重写该方法 2)执行效率(JVM相关的东西,不用太关注) 3.修饰变量 final修饰原始类型的变量,该变量不能被修改 final修饰引 ...