Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P2163 [SHOI2007]园丁的烦恼

P2163 [SHOI2007]园丁的烦恼

二维数点模板题,注意相同的 $x$ 先修改在查询。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write(Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar(x % 10 + '0');
}
const int N = 5e5 + 10;
vector <int> ws;
int n, m, q, tr[N], ans[N];
struct OP
{
int t, k, id, op;
bool operator < (const OP &_) const
{
return t < _.t || (t == _.t && id < _.id);
}
};
vector <OP> p;
void modify (int x)
{
for (; x <= m; x += x & -x)
tr[x]++;
}
int query (int x)
{
int res = 0;
for (; x; x -= x & -x)
res += tr[x];
return res;
}
int main ()
{
read(n), read(q);
for (int i = 1, x, y; i <= n; i++)
{
read(x), read(y);
p.push_back((OP){x, y, 0, 0});
ws.push_back(y);
}
sort(ws.begin(), ws.end());
m = ws.erase(unique(ws.begin(), ws.end()), ws.end()) - ws.begin();
for (int i = 1, a, b, c, d; i <= q; i++)
{
read(a), read(b), read(c), read(d);
p.push_back((OP){c, d, i, 1});
p.push_back((OP){a - 1, b - 1, i, 1});
p.push_back((OP){c, b - 1, i, -1});
p.push_back((OP){a - 1, d, i, -1});
}
for (OP &i : p)
i.k = upper_bound(ws.begin(), ws.end(), i.k) - ws.begin();
sort(p.begin(), p.end());
for (OP i : p)
i.id ? (ans[i.id] += query(i.k) * i.op) : (modify(i.k), 0);
for (int i = 1; i <= q; i++)
write(ans[i]), puts("");
return 0;
}