Educational Codeforces Round 26 G. Functions On The Segments 主席树

来源:互联网 发布:sharpdesk软件下载 编辑:程序博客网 时间:2024/06/05 15:57

题目链接:http://codeforces.com/contest/837/problem/G

题意:给了n个分段函数,然后给m个查询,每次查询查[l,r]的f(x)的和,强制在线。

解法:关键是找到一个可以求前缀信息的数据结构,主席树显然满足,这题就是主席树的应用。


#include <bits/stdc++.h>using namespace std;typedef long long LL;const int mod = 1000000000;const int N = 2e5+5;struct node{    int l,r;    LL A,B,Y1,Y2;}T[40*N];int root[75010], cnt;int n,m,x,l,r,AA,BB,YY1,YY2;void pushup(int x){    int L = T[x].l, R = T[x].r;    T[x].A = T[L].A + T[R].A;    T[x].B = T[L].B + T[R].B;    T[x].Y1 = T[L].Y1 + T[R].Y1;    T[x].Y2 = T[L].Y2 + T[R].Y2;}void update(int &x, int y, int l, int r, int pos){    x = ++cnt, T[x] = T[y];    if(l == r){        T[x].A += AA, T[x].B += BB, T[x].Y1 += YY1, T[x].Y2 += YY2;        return;    }    int mid = (l+r)/2;    if(pos<=mid) update(T[x].l, T[y].l, l, mid, pos);    else update(T[x].r, T[y].r, mid+1, r, pos);    pushup(x);}LL query(int rt, int l, int r, int L, int R, int x){    if(L <= l && r <= R){        return T[rt].A*x + T[rt].B + T[rt].Y1 + T[rt].Y2;    }    int mid = (l+r)/2;    if(R<=mid) return query(T[rt].l, l, mid, L, R, x);    else if(L>mid) return query(T[rt].r, mid+1, r, L, R, x);    else{        return query(T[rt].l, l, mid, L, mid, x) + query(T[rt].r, mid+1, r, mid+1, R, x);    }}int main(){    scanf("%d", &n);    int x1, x2, y1, a, b, y2;    for(int i=1; i<=n; i++){        scanf("%d %d %d %d %d %d", &x1,&x2,&y1,&a,&b,&y2);        AA = 0, BB = 0, YY1 = y1, YY2 = 0;        update(root[i], root[i-1], 1, N, 1);        AA = a, BB = b, YY1 = -y1, YY2 = 0;        update(root[i], root[i], 1, N, x1+1);        AA = -a, BB = -b, YY1 = 0, YY2 = y2;        update(root[i], root[i], 1, N, x2+1);    }    scanf("%d", &m);    LL ans = 0;    while(m--)    {        scanf("%d %d %d", &l,&r,&x);        x = (x + ans)%mod;        LL t1 = query(root[r], 1, N, 1, x, x), t2 = query(root[l-1], 1, N, 1, x, x);        ans = t1 - t2;        printf("%lld\n", ans);    }    return 0;}


阅读全文
0 0