File size: 4,112 Bytes
ff444f7 |
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 |
#include <iostream>
#include <vector>
using namespace std;
const long long MOD = (int)1e9 + 7;
struct Node {
long long maxval, maxflip;
int idx_of_maxval, idx_of_maxflip;
Node() {}
Node(int i, long long v):
maxval(v), idx_of_maxval(i),
maxflip(((long long)v * (MOD - 1)) % MOD), idx_of_maxflip(i) {}
};
template<class T, class D>
class segment_tree {
static T join_values(const T &a, const T &b) {
Node res;
if (a.maxval > b.maxval ||
(a.maxval == b.maxval && a.idx_of_maxval < b.idx_of_maxval)) {
res.maxval = a.maxval;
res.idx_of_maxval = a.idx_of_maxval;
} else {
res.maxval = b.maxval;
res.idx_of_maxval = b.idx_of_maxval;
}
if (a.maxflip > b.maxflip ||
(a.maxflip == b.maxflip && a.idx_of_maxflip < b.idx_of_maxflip)) {
res.maxflip = a.maxflip;
res.idx_of_maxflip = a.idx_of_maxflip;
} else {
res.maxflip = b.maxflip;
res.idx_of_maxflip = b.idx_of_maxflip;
}
return res;
}
static void join_value_with_delta(T &v, const D &d, int len) {
if (d) { // Flip all values in v.lo .. v.hi.
swap(v.maxval, v.maxflip);
swap(v.idx_of_maxval, v.idx_of_maxflip);
}
}
static D join_deltas(const D &d1, const D &d2) {
return d1 ^ d2;
}
int len;
std::vector<T> value;
std::vector<D> delta;
std::vector<bool> pending;
template<class It>
void build(int i, int lo, int hi, It arr) {
if (lo == hi) {
value[i] = Node(lo, *(arr + lo));
return;
}
int mid = lo + (hi - lo)/2;
build(i*2 + 1, lo, mid, arr);
build(i*2 + 2, mid + 1, hi, arr);
value[i] = join_values(value[i*2 + 1], value[i*2 + 2]);
}
void push_delta(int i, int lo, int hi) {
if (pending[i]) {
join_value_with_delta(value[i], delta[i], hi - lo + 1);
if (lo != hi) {
int l = 2*i + 1, r = 2*i + 2;
delta[l] = pending[l] ? join_deltas(delta[l], delta[i]) : delta[i];
delta[r] = pending[r] ? join_deltas(delta[r], delta[i]) : delta[i];
pending[l] = pending[r] = true;
}
pending[i] = false;
}
}
T query(int i, int lo, int hi, int tgt_lo, int tgt_hi) {
push_delta(i, lo, hi);
if (lo == tgt_lo && hi == tgt_hi) {
return value[i];
}
int mid = lo + (hi - lo)/2;
if (tgt_lo <= mid && mid < tgt_hi) {
return join_values(
query(i*2 + 1, lo, mid, tgt_lo, std::max(tgt_hi, mid)),
query(i*2 + 2, mid + 1, hi, std::max(tgt_lo, mid + 1), tgt_hi));
}
if (tgt_lo <= mid) {
return query(i*2 + 1, lo, mid, tgt_lo, std::min(tgt_hi, mid));
}
return query(i*2 + 2, mid + 1, hi, std::max(tgt_lo, mid + 1), tgt_hi);
}
void update(int i, int lo, int hi, int tgt_lo, int tgt_hi, const D &d) {
push_delta(i, lo, hi);
if (hi < tgt_lo || lo > tgt_hi) {
return;
}
if (tgt_lo <= lo && hi <= tgt_hi) {
delta[i] = d;
pending[i] = true;
push_delta(i, lo, hi);
return;
}
update(2*i + 1, lo, (lo + hi)/2, tgt_lo, tgt_hi, d);
update(2*i + 2, (lo + hi)/2 + 1, hi, tgt_lo, tgt_hi, d);
value[i] = join_values(value[2*i + 1], value[2*i + 2]);
}
public:
template<class It>
segment_tree(It lo, It hi)
: len(hi - lo), value(4*len), delta(4*len), pending(4*len, false) {
build(0, 0, len - 1, lo);
}
int size() const { return len; }
T query(int lo, int hi) { return query(0, 0, len - 1, lo, hi); }
void update(int i, const D &d) { update(0, 0, len - 1, i, i, d); }
void update(int lo, int hi, const D &d) { update(0, 0, len - 1, lo, hi, d); }
};
long long solve() {
int N, Q;
cin >> N;
vector<long long> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
segment_tree<Node, bool> S(A.begin(), A.end());
long long ans = 0;
cin >> Q;
for (int i = 0, l, r; i < Q; i++) {
cin >> l >> r;
l--;
r--;
S.update(l, r, 1);
ans += S.query(0, N - 1).idx_of_maxval+1;
}
return ans;
}
int main() {
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": " << solve() << endl;
}
return 0;
} |