【链接】我是链接


【题意】



【题解】


这里讲一种用类似二维线段树的方法求矩形区域内点的个数的方法。
我们可以把n个正方形用n棵线段树来维护。
第i棵线段树维护的是正方形的前i列的各行之间的点数之和。
也即前i列,第[x..y]行之间点的个数(也即一个(y-x+1)*i的矩形区域的点的个数);
因为每一列只有一个点。
所以,我们在输入的时候,当前的“行区间l..r”的点数和,直接加上1就好。
然后看看输入的点所在的行区间在l..m还是m+1..r
如果是在l..m,则右区间直接用前一列的线段树的相应节点就好。不用重新建树(因为点数就和前i-1列的一样)。
左区间还是一样,在前一列的基础上点数和递增1.
询问(x1,y1)~(x2,y2)这个矩形区域的时候。
只要在第x1-1列的线段树里面求和第y1..y2行的点个数sum2。
在第x2列的线段树里面也求和第y1..y2行的点个数sum1;
则sum1-sum2就是这个矩形区域内的点的个数了。
其他的和上一篇文章类似。

【错的次数】


0

【反思】


在这了写反思

【代码】

#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(二维线段树)的更多相关文章

  1. Codeforces Round #619 (Div. 2)E思维+二维RMQ

    题:https://codeforces.com/contest/1301/problem/E 题意:给个n*m的图形,q个询问,每次询问问询问区间最大的合法logo的面积是多少 分析:由于logo是 ...

  2. 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. ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  6. 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. 这题 ...

  7. 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 ...

  8. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  9. 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所 ...

随机推荐

  1. 【Tika基础教程之中的一个】Tika基础教程

    一.高速入门 1.Tika是一个用于文本解释的框架.其本身并不提供不论什么的库用于解释文本,而是调用各种各样的库,如POI,PDFBox等. 使用Tika.能够提取文件里的作者.标题.创建时间.正文等 ...

  2. ToString DateTime 操作

    来源:网络 字符型转换为字符串// C 货币 2.5.ToString("C"); // ¥2.50 // D 10进制数 25.ToString("D5"); ...

  3. Oracle定义变量、常量

    1 定义变量 declare var_countryname varchar2(50):='中国'; 2 定义常量 con_day constant integer:=365;

  4. Linux 设置文件默认打开方式

    比如说我安装了一个绿色版的sublime(.tar解压出来的不是.deb) 但是现在我右键不能打开,不能添加为默认打开方式...这个时候就比较尴尬了... 我总不能每次都cd到安装目录下然后termi ...

  5. pstree---树状图的方式展现进程

    pstree命令以树状图的方式展现进程之间的派生关系,显示效果比较直观. 语法 pstree(选项) 选项 -a:显示每个程序的完整指令,包含路径,参数或是常驻服务的标示: -c:不使用精简标示法: ...

  6. [ReasonML] Workshops code

    /* list of strings */ let _ = ["example-1", "example-2", "example-3"]; ...

  7. css大会站点顶部的一个特效

    看到http://css.w3ctech.com/ 上一个效果认为挺赞的. 然后学些了一下. demo地址:http://codepen.io/tianzi77/pen/mJaLWq html结构非常 ...

  8. inflater-布局转化实现

    通俗的说,inflate就相当于将一个xml中定义的布局找出来.因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件 ...

  9. 不用浏览器,直接用代码发送文件给webservices所在服务器 并且可以周期行的发送

    package com.toic.test; import java.io.DataInputStream; import java.io.DataOutputStream; import java. ...

  10. vector转数组

    vector转数组 由于vector内部的数据是存放在连续的存储空间,vector转数组事实上只需要获取vector中第一个数据的地址和数据的长度即可.如果仅仅是传参,无需任何操作,直接传地址即可,如 ...