【Codeforces Round #433 (Div. 1) C】Boredom(二维线段树)
【链接】我是链接
【题意】
【题解】
【错的次数】
【反思】
【代码】
#include <bits/stdc++.h>
using namespace std; const int N = 2e5, M = 2e5 * 20; int n, q;
int root[N + 10], ls[M + 10], rs[M + 10], sum[M+10],tot = 0;
long long ans = 0; void updata(int &x, int y, int l, int r, int pos) {
x = ++tot;
ls[x] = ls[y], rs[x] = rs[y];
sum[x] = sum[y] + 1;
int m = (l + r) >> 1;
if (l == r) return;
if (pos <= m)
updata(ls[x], ls[y], l, m, pos);
else
updata(rs[x], rs[y], m + 1, r, pos);
} int Q(int x, int L, int R, int l, int r) {
if (!x) return 0;
if (L <= l && r <= R) return sum[x];
int m = (l + r) >> 1;
int sum = 0;
if (L <= m) sum += Q(ls[x], L, R, l, m);
if (m < R) sum += Q(rs[x], L, R, m + 1, r);
return sum;
} long long C(long long x) {
return x*(x - 1)/2;
} long long ask(int x1, int y1, int x2, int y2) {
return C(Q(root[x2], y1, y2, 1, n) - Q(root[x1 - 1], y1, y2, 1, n));
} int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> q;
for (int i = 1,p; i <= n; i++) {
cin >> p;
updata(root[i],root[i-1],1,n,p);
}
for (int i = 1, l, d, r, u; i <= q; i++) {
cin >> l >> d >> r >> u;
ans = ask(1, 1, l - 1, n) + ask(1, 1, n, d - 1) + ask(r + 1, 1, n, n) + ask(1, u + 1, n, n);
ans = ans - ask(1, 1, l - 1, d - 1) - ask(1, u + 1, l - 1, n) - ask(r + 1, u + 1, n, n) - ask(r + 1, 1, n, d - 1);
cout << C(n) - ans << endl;
}
return 0;
}
【Codeforces Round #433 (Div. 1) C】Boredom(二维线段树)的更多相关文章
- Codeforces Round #619 (Div. 2)E思维+二维RMQ
题:https://codeforces.com/contest/1301/problem/E 题意:给个n*m的图形,q个询问,每次询问问询问区间最大的合法logo的面积是多少 分析:由于logo是 ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
随机推荐
- 【Tika基础教程之中的一个】Tika基础教程
一.高速入门 1.Tika是一个用于文本解释的框架.其本身并不提供不论什么的库用于解释文本,而是调用各种各样的库,如POI,PDFBox等. 使用Tika.能够提取文件里的作者.标题.创建时间.正文等 ...
- ToString DateTime 操作
来源:网络 字符型转换为字符串// C 货币 2.5.ToString("C"); // ¥2.50 // D 10进制数 25.ToString("D5"); ...
- Oracle定义变量、常量
1 定义变量 declare var_countryname varchar2(50):='中国'; 2 定义常量 con_day constant integer:=365;
- Linux 设置文件默认打开方式
比如说我安装了一个绿色版的sublime(.tar解压出来的不是.deb) 但是现在我右键不能打开,不能添加为默认打开方式...这个时候就比较尴尬了... 我总不能每次都cd到安装目录下然后termi ...
- pstree---树状图的方式展现进程
pstree命令以树状图的方式展现进程之间的派生关系,显示效果比较直观. 语法 pstree(选项) 选项 -a:显示每个程序的完整指令,包含路径,参数或是常驻服务的标示: -c:不使用精简标示法: ...
- [ReasonML] Workshops code
/* list of strings */ let _ = ["example-1", "example-2", "example-3"]; ...
- css大会站点顶部的一个特效
看到http://css.w3ctech.com/ 上一个效果认为挺赞的. 然后学些了一下. demo地址:http://codepen.io/tianzi77/pen/mJaLWq html结构非常 ...
- inflater-布局转化实现
通俗的说,inflate就相当于将一个xml中定义的布局找出来.因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件 ...
- 不用浏览器,直接用代码发送文件给webservices所在服务器 并且可以周期行的发送
package com.toic.test; import java.io.DataInputStream; import java.io.DataOutputStream; import java. ...
- vector转数组
vector转数组 由于vector内部的数据是存放在连续的存储空间,vector转数组事实上只需要获取vector中第一个数据的地址和数据的长度即可.如果仅仅是传参,无需任何操作,直接传地址即可,如 ...