「Comet OJ 58D」菜菜种菜

题目链接

ii 个点,给定 xiiyix_i\le i\le y_i 和权值 aia_i

qq 个询问,回答

ai[LiR  xi<L  yi>R] \sum a_i[L\le i \le R\ \wedge\ x_i < L\ \wedge \ y_i>R]

n,q106n,q\le 10^6


首先这肯定不是一个图论问题

把所有土地排成一行,从左到右第 ii 个编号为 ii 。容易发现,一块土地 ii 能不能作为家,只跟他能直接到达的土地中,在它左边最靠右的一块(记作 ,xix_i 不存在则为 00 )和在它右面最靠左的一块(记作 yiy_i ,不存在则为 n+1n+1

询问 L,RL, R ,回答

f(L,R)=ai[LiR  xi<L  yi>R] f(L, R) = \sum a_i[L\le i \le R\ \wedge\ x_i < L\ \wedge \ y_i>R]

s(a,b,c,d,e,f)=ai[aib  cxid  eyif] s(a,b,c,d,e,f)=\sum a_i[a\le i \le b\ \wedge\ c\le x_i \le d\ \wedge\ e\le y_i\le f]\\
f(L,R)=+s(L,R,0,L1,R+1,n+1)=+s(L,R,0,n+1,0,n+1)s(L,R,L,n+1,0,n+1)s(L,R,0,n+1,0,R)+s(L,R,L,n+1,0,R) \begin{aligned} f(L,R) = &+s(L,R,0,L-1,R+1, n+1)\\ = &+s(L,R,0,n+1, 0, n+1)\\ &-s(L, R,L,n+1,0,n+1)\\ &-s(L,R,0,n+1,0,R)\\ &+s(L,R,L,n+1,0,R) \end{aligned}

一个容斥,最后因为 xiL,yiRx_i\ge L, y_i \le R 被减了两次,需要加回来。

xi<i<yix_i<i<y_i

可得

LiR,xiLiR,xiLLiR,yiRiL,yiRLiR,xiL,yiRxiL,yiR \begin{aligned} L\le i\le R, x_i\ge L&\Leftrightarrow i\le R,x_i\ge L\\ L\le i\le R, y_i\le R&\Leftrightarrow i\ge L,y_i\le R\\ L\le i\le R, x_i\ge L,y_i\le R&\Leftrightarrow x_i\ge L, y_i \le R \end{aligned}

所以问题变成了三个二维数点,用扫描线和树状数组即可解决

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("inline")
// #pragma GCC optimize("-fgcse")
// #pragma GCC optimize("-fgcse-lm")
// #pragma GCC optimize("-fipa-sra")
// #pragma GCC optimize("-ftree-pre")
// #pragma GCC optimize("-ftree-vrp")
// #pragma GCC optimize("-fpeephole2")
// #pragma GCC optimize("-ffast-math")
// #pragma GCC optimize("-fsched-spec")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("-falign-jumps")
// #pragma GCC optimize("-falign-loops")
// #pragma GCC optimize("-falign-labels")
// #pragma GCC optimize("-fdevirtualize")
// #pragma GCC optimize("-fcaller-saves")
// #pragma GCC optimize("-fcrossjumping")
// #pragma GCC optimize("-fthread-jumps")
// #pragma GCC optimize("-funroll-loops")
// #pragma GCC optimize("-fwhole-program")
// #pragma GCC optimize("-freorder-blocks")
// #pragma GCC optimize("-fschedule-insns")
// #pragma GCC optimize("inline-functions")
// #pragma GCC optimize("-ftree-tail-merge")
// #pragma GCC optimize("-fschedule-insns2")
// #pragma GCC optimize("-fstrict-aliasing")
// #pragma GCC optimize("-fstrict-overflow")
// #pragma GCC optimize("-falign-functions")
// #pragma GCC optimize("-fcse-skip-blocks")
// #pragma GCC optimize("-fcse-follow-jumps")
// #pragma GCC optimize("-fsched-interblock")
// #pragma GCC optimize("-fpartial-inlining")
// #pragma GCC optimize("no-stack-protector")
// #pragma GCC optimize("-freorder-functions")
// #pragma GCC optimize("-findirect-inlining")
// #pragma GCC optimize("-fhoist-adjacent-loads")
// #pragma GCC optimize("-frerun-cse-after-loop")
// #pragma GCC optimize("inline-small-functions")
// #pragma GCC optimize("-finline-small-functions")
// #pragma GCC optimize("-ftree-switch-conversion")
// #pragma GCC optimize("-foptimize-sibling-calls")
// #pragma GCC optimize("-fexpensive-optimizations")
// #pragma GCC optimize("-funsafe-loop-optimizations")
// #pragma GCC optimize("inline-functions-called-once")
// #pragma GCC optimize("-fdelete-null-pointer-checks")
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, l, r) for (int i = (l); i >= (r); --i)
using std::cerr;
using std::endl;
using std::make_pair;
using std::pair;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;

// #define DEBUG 1 //调试开关
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
IO() : p1(buf), p2(buf), pp(pbuf) {}
~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }
#endif
inline char gc() {
#if DEBUG //调试,可显示字符
return getchar();
#endif
if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
return p1 == p2 ? -1 : *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline void read(T &x) {
register double tmp = 1;
register bool sign = 0;
x = 0;
register char ch = gc();
for (; !isdigit(ch); ch = gc())
if (ch == '-') sign = 1;
for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0');
if (ch == '.')
for (ch = gc(); isdigit(ch); ch = gc()) tmp /= 10.0, x += tmp * (ch - '0');
if (sign) x = -x;
}
inline void read(char *s) {
register char ch = gc();
for (; blank(ch); ch = gc())
;
for (; !blank(ch); ch = gc()) *s++ = ch;
*s = 0;
}
inline void read(char &c) {
for (c = gc(); blank(c); c = gc())
;
}
inline void push(const char &c) {
#if DEBUG //调试,可显示字符
putchar(c);
#else
if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
*pp++ = c;
#endif
}
template <class T>
inline void write(T x) {
if (x < 0) x = -x, push('-'); // 负数输出
static T sta[35];
T top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) push(sta[--top] + '0');
}
inline void write(const char *s) {
while (*s != '\0') push(*(s++));
}
template <class T>
inline void write(T x, char lastChar) {
write(x), push(lastChar);
}
} io;

const int N = 1000010;

struct Node {
ui i, x, y, v;
} p[N];
struct Q {
ui id, l, r;
ll ans;
} ask[N];

bool cmp_l(const Q &a, const Q &b) { return a.l < b.l; }

bool cmp_r(const Q &a, const Q &b) { return a.r < b.r; }

bool cmp_id(const Q &a, const Q &b) { return a.id < b.id; }

bool cmp_y(const Node &a, const Node &b) { return a.y < b.y; }

ll pre[N], c[N + 5];

// #define debug(x) cerr << x << endl
#define debug(x)

inline int lowbit(int x) { return x & (-x); }

void add(int i, ll x) {
++i;
for (; i <= N; i += lowbit(i)) {
debug(i);
c[i] += x;
}
}
ll sum(int i) {
++i;
ll ret = 0;
for (; i > 0; i -= lowbit(i)) ret += c[i];
return ret;
}

ll query(int l, int r) { return sum(r) - sum(l - 1); }

int main() {
#ifdef LOCAL
freopen("input", "r", stdin);
#endif
int n, m, q;
io.read(n), io.read(m), io.read(q);
rep(i, 1, n) io.read(p[i].v), p[i].x = 0, p[i].y = n + 1, p[i].i = i, pre[i] = pre[i - 1] + p[i].v;
while (m--) {
ui u, v;
io.read(u), io.read(v);
if (v < u)
p[u].x = std::max(p[u].x, v);
else
p[u].y = std::min(p[u].y, v);
}
rep(i, 1, q) io.read(ask[i].l), io.read(ask[i].r), ask[i].ans = pre[ask[i].r] - pre[ask[i].l - 1], ask[i].id = i;

debug("read over");

// i <= R, x_i >= L
std::sort(ask + 1, ask + 1 + q, cmp_r);
int ptr = 1;
rep(i, 1, q) {
while (ptr <= n && p[ptr].i <= ask[i].r) add(p[ptr].x, p[ptr].v), ++ptr;
ask[i].ans -= query(ask[i].l, n + 1);
}

debug("i <= R, x_i >= L");

// x >= L, y <= R
std::memset(c, 0, sizeof(c));
ptr = 1;
std::sort(p + 1, p + 1 + n, cmp_y);
rep(i, 1, q) {
while (ptr <= n && p[ptr].y <= ask[i].r) add(p[ptr].x, p[ptr].v), ++ptr;
ask[i].ans += query(ask[i].l, n + 1);
}

// y <= R, i >= L
std::memset(c, 0, sizeof(c));
ptr = 1;
rep(i, 1, q) {
while (ptr <= n && p[ptr].y <= ask[i].r) add(p[ptr].i, p[ptr].v), ++ptr;
ask[i].ans -= query(ask[i].l, n + 1);
}

ll ans = 0;
rep(i, 1, q) ans ^= ask[i].id * ask[i].ans;
io.write(ans);
return 0;
}
作者

Gesrua

发布于

2019-08-17

更新于

2020-11-21

许可协议