题意:
给你一些平行于x轴的线段,每次询问坐标x上方的的前k个线段的高度和。题目是强制要求在线的。
思路:
首先要会可持久化线段树(又称主席树和函数式线段树),不会的可以去做下POJ 2104。
把所有线段高度离散化,作为结点建线段树。建两颗主席树,一颗是把线段的左端点的高度插入主席树,一颗是把线段的右端点的高度插入主席树,然后询问坐标x的时候,实际要询问的就是第一颗主席树减去第二颗主席树后的树的前k大高度和,不懂的可以自己在纸上画画每颗树表示的是什么然后就会就懂了。
#include
#include
#include
using namespace std;
typedef __int64 ll;
const int N = 100000+5;
struct preSegtree {
int tot;
int cnt[N*35], ls[N*35], rs[N*35];
ll sum[N*35];
void up(int rt) {
cnt[rt] = cnt[ls[rt]] + cnt[rs[rt]];
sum[rt] = sum[ls[rt]] + sum[rs[rt]];
}
void build(int &rt, int l, int r) {
rt = tot++;
cnt[rt] = sum[rt] = 0;
if(l == r) {
return ;
}
int mid = l+r>>1;
build(ls[rt], l, mid);
build(rs[rt], mid+1, r);
up(rt);
}
void update(int &rt, int l, int r, int pre, int x, int y) {
rt = tot++;
if(l == r) {
cnt[rt] = cnt[pre] + 1;
sum[rt] = sum[pre] + y;
return ;
}
int mid = l+r>>1;
if(x <= mid) {
update(ls[rt], l, mid, ls[pre], x, y);
rs[rt] = rs[pre];
}
else {
update(rs[rt], mid+1, r, rs[pre], x, y);
ls[rt] = ls[pre];
}
up(rt);
}
ll query(int rt, int l, int r, int pre, int k) {
if(k == 0) return 0;
if(l == r) {
int Cnt = cnt[rt]-cnt[pre];
ll Sum = sum[rt]-sum[pre];
if(Cnt >= k) return Sum/Cnt*k;
else return Sum;
}
int mid = l+r>>1;
int cur = cnt[ls[rt]] - cnt[ls[pre]];
if(cur <= k)
return sum[ls[rt]]-sum[ls[pre]] + query(rs[rt], mid+1, r, rs[pre], k-cur);
else
return query(ls[rt], l, mid, ls[pre], k);
}
void print(int rt, int l, int r) {
printf("rt = %d l = %d r = %d cnt = %d sum = %I64d\n", rt, l, r, cnt[rt], sum[rt]);
if(l == r) return ;
int mid = l+r>>1;
print(ls[rt], l, mid);
print(rs[rt], mid+1, r);
}
}tree;
struct PP{
int l, r, d;
bool operator < (const PP&a)const {
return l < a.l;
}
}a[N];
bool cmp(const PP& a, const PP&b) {
return a.r < b.r;
}
int hd1[N], hd2[N], td[N], sd1[N], sd2[N];
int n, m, X, P, top;
void input() {
tree.tot = 0;
top = 0;
for(int i = 1;i <= n; i++) {
scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].d);
td[++top] = a[i].d;
}
sort(td+1, td+top+1);
top = unique(td+1, td+top+1) - (td+1);
sort(a+1, a+n+1);
tree.build(hd1[0], 1, top);
sd1[0] = 0;
int tmp = 1;
for(int i = 1;i <= n; i++) {
while(tmp <= X && tmp < a[i].l) sd1[tmp++] = i-1;
int pos = lower_bound(td+1, td+top+1, a[i].d) - td;
tree.update(hd1[i], 1, top, hd1[i-1], pos, td[pos]);
}
//tree.print(hd1[0], 1, top);
//tree.print(hd1[1], 1, top);
while(tmp <= X) sd1[tmp++] = n;
sort(a+1, a+n+1, cmp);
tree.build(hd2[0], 1, top);
sd2[0] = 0;
tmp = 1;
for(int i = 1;i <= n; i++) {
while(tmp <= X && tmp < a[i].r) sd2[tmp++] = i-1;
int pos = lower_bound(td+1, td+top+1, a[i].d) - td;
tree.update(hd2[i], 1, top, hd2[i-1], pos, td[pos]);
}
while(tmp <= X) sd2[tmp++] = n;
}
void solve() {
ll pre = 1;
for(int i = 1;i <= m; i++) {
int x, aa, bb, cc;
scanf("%d%d%d%d", &x, &aa, &bb, &cc);
int k = (pre%cc*aa+bb)%cc;
ll cur = tree.query(hd1[sd1[x]], 1, top, hd2[sd2[x-1]], k);
if(pre > P && i > 1) cur *= 2;
pre = cur;
printf("%I64d\n", pre);
}
}
int main() {
while(scanf("%d%d%d%d", &n, &m, &X, &P) == 4) {
input();
solve();
}
return 0;
}