题解:P7707 「Wdsr-2.7」百花齐放的太阳花田
考虑线段树。对于每一个结点代表的区间内的每一个花的高度
上文的 pushup 复杂度时
查询操作是简单的。对于线段树上的每个被询问区间,二分出最大的满足要求的花的高度,再将信息合并。时间复杂度是
::::success[代码]
#include <bits/stdc++.h>
using namespace std;
struct node {
int l, r, sum = 0, h;
};
const int N = 1e6 + 7;
int n, m, k, h[N], t[N];
vector<node> tr[N << 2];
bool operator <(const node &a, const node &b) {
return a.h < b.h;
}
node operator +(const node &a, const node &b) {
if(!a.sum) return b;
if(!b.sum) return a;
node ret;
ret.h = max(a.h, b.h);
ret.l = a.l, ret.r = b.r;
ret.sum = a.sum + b.sum - (a.r == b.l);
return ret;
}
void pushup(int p) {
int t1 = 0, t2 = 0;
while(t1 < tr[p << 1].size() || t2 < tr[p << 1 | 1].size()) {
if(t2 == tr[p << 1 | 1].size() || (t1 < tr[p << 1].size() && tr[p << 1][t1] < tr[p << 1 | 1][t2])) {
if(t2) tr[p].push_back(tr[p << 1][t1] + tr[p << 1 | 1][t2 - 1]);
else tr[p].push_back(tr[p << 1][t1]);
t1 ++;
} else {
if(t1) tr[p].push_back(tr[p << 1][t1 - 1] + tr[p << 1 | 1][t2]);
else tr[p].push_back(tr[p << 1 | 1][t2]);
t2 ++;
}
}
}
void update(int p, int l, int r, int pos, int x, int y) {
if(l == r) {
tr[p].push_back({y, y, 1, x});
return ;
}
int mid = l + r >> 1;
if(pos <= mid) update(p << 1, l, mid, pos, x, y);
else update(p << 1 | 1, mid + 1, r, pos, x, y);
if(r == pos) pushup(p);
}
node query(int p, int l, int r, int lq, int rq, int x) {
if(lq <= l && rq >= r) {
if(tr[p][0].h > x) return node{0, 0, 0, 0};
int pos = upper_bound(tr[p].begin(), tr[p].end(), node{0, 0, 0, x}) - tr[p].begin() - 1;
return tr[p][pos];
}
int mid = l + r >> 1;
if(rq <= mid) return query(p << 1, l, mid, lq, rq, x);
if(lq > mid) return query(p << 1 | 1, mid + 1, r, lq, rq, x);
return query(p << 1, l, mid, lq, rq, x) + query(p << 1 | 1, mid + 1, r, lq, rq, x);
}
int main() {
ios :: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> k;
for(int i = 1; i <= n; i ++) cin >> h[i];
for(int i = 1; i <= n; i ++) cin >> t[i];
for(int i = 1; i <= n; i ++) update(1, 1, n + m, i, h[i], t[i]);
int lst = 0, cnt = n;
int m1 = m;
while(m --) {
int op; cin >> op;
if(op == 1) {
int l, r, x; cin >> l >> r >> x;
l ^= k * lst, r ^= k * lst, x ^= k * lst;
lst = query(1, 1, n + m1, l, r, x).sum;
cout << lst << '\n';
} else {
int x, y; cin >> x >> y;
x ^= k * lst, y ^= k * lst;
update(1, 1, n + m1, ++ cnt, x, y);
}
}
return 0;
}
::::