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) ...
随机推荐
- 制作Windows10政府版的小白教程
制作Windows10政府版的小白教程 https://03k.org/make10entg.html 首先,宿主系统要比操作的系统新,因为低版本dism操作不了: 当然也可以单独下载ADK,提取最新 ...
- [POI2007]ATR-Tourist Attractions
题目大意:一个无向图,从$1$到$n$,要求必须经过$2,3,\dots,k+1$,给出一些限制关系,要求在经过$v\leq k+1$之前必须经过$u\leq k+1$,求最短路 题解:预处理出$1\ ...
- ext大法好啊
http://www.cnblogs.com/keshuqi/p/6257895.html
- BZOJ day1
十题击破 1051108811921432195119682242245624632761
- [poj 3252]数位dp前导0的处理
通过这个题对于数位dp中前导0的处理有了新的认识. 题目链接:http://poj.org/problem?id=3252 //http://poj.org/problem?id=3252 #incl ...
- 如何用listview显示服务端数据
https://www.cnblogs.com/caobotao/p/5061627.html
- 在线输入RGB更改背景色
HTML: <!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" co ...
- 普通table表格样式及代码大全
普通table表格样式及代码大全(全)(一) 单实线边框表格 <table style="border-collapse: collapse" borderColor=#0 ...
- Spring学习--基于 XML 的配置声明切面
正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...
- 利用vue-cli创建Vue项目
1.安装node.js:Node.js安装包及源码下载地址为:https://nodejs.org/en/download/. 配置参考:http://www.runoob.com/nodejs/no ...