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) ...
随机推荐
- BZOJ4531 && BJOI2014 trace
#include<cstdio> #include<cctype> using namespace std ; struct state { int len ; int p ; ...
- bootstrap-table 增加序号列(支持分页)
columns: [ { checkbox: true }, { title: '序号', align: 'center', halign: 'center', formatter: function ...
- 洛谷 [SDOI2015]约数个数和 解题报告
[SDOI2015]约数个数和 题目描述 设\(d(x)\)为\(x\)的约数个数,给定\(N,M\),求$ \sum\limits^N_{i=1}\sum\limits^M_{j=1}d(ij)$ ...
- last_query_cost
The total cost of the last compiled query as computed by the query optimizer. This is useful for com ...
- Idea 怎么远程debug
注意的问题:远程debug别人的服务器只能开一个debug,所以当你的同事比你先远程debug同一台服务器时,你应该报Error running 某某ip地址 .unable to open debu ...
- hdu 6223 Infinite Fraction Path
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223 题意:给定长度为n的一串数字S,现在要按照一种规则寻找长度为n的数字串,使得该数字串的字典序最大 ...
- Codeforces 950E Data Center Maintenance 强连通分量
题目链接 题意 有\(n\)个信息中心,每个信息中心都有自己的维护时间\((0\leq t\lt h)\),在这个时刻里面的信息不能被获得. 每个用户的数据都有两份备份,放在两个相异的信息中心(维护时 ...
- centOS 7 部署samba
部署samba **每个用户有自己的目录,可以浏览内容,也可以删除** 清空防火墙规则 [root@bogon ~]# iptables -F 安装samba [root@bogon ~]# yum ...
- 百度面试题——top K算法
需求 从一亿个数据中,找出其中最小的10个数. 分析 最笨的方法就是将这一亿个数据,按从小到大进行排序,然后取前10个.这样的话,即使使用时间复杂度为nlogn的快排或堆排,由于元素会频繁的移动,效率 ...
- Launcher3自定义壁纸旋转后拉伸无法恢复
MTK8382/8121平台. 描述:将自定义图片设置成壁纸后,横屏显示时,旋转为竖屏,图片由于分辨率过小,会拉伸:再旋转为横屏,拉伸不恢复. 这两天正在解这个问题,研究了很久,走了不少弯路,最后发现 ...