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
- Educational Codeforces Round 26 G. Functions On The Segments 主席树
- Educational Codeforces Round 4 D. The Union of k-Segments
- Educational Codeforces Round 4-D. The Union of k-Segments
- Educational Codeforces Round 20 G. Periodic RMQ Problem(线段树+主席树)
- 【Codeforces837G】Functions On The Segments
- Educational Codeforces Round 10 D. Nested Segments
- Educational Codeforces Round 10 D Nested Segments
- Educational Codeforces Round 10 D.Nested Segments
- Educational Codeforces Round 10-D. Nested Segments
- Codeforces 612D Educational Codeforces Round 4 D. The Union of k-Segments 离散化+map
- Educational Codeforces Round 21-G
- Educational Codeforces Round 4 612D The Union of k-Segments(stl)
- Educational Codeforces Round 4 D. The Union of k-Segments(★)
- Educational Codeforces Round 4 D The Union of k-Segments (扫描线)
- Educational Codeforces Round 26
- Educational Codeforces Round 26
- 初识树状数组 Educational Codeforces Round 10 D - Nested Segments
- Educational Codeforces Round 28 Four Segments 前缀和 + 思维
- Mongodb基础篇 之有话要说
- eclipse常见问题
- 如何申请与对接支付接口
- 区间dp模板
- Android 使用obb扩展App
- Educational Codeforces Round 26 G. Functions On The Segments 主席树
- 死锁-操作系统-程序员面试
- 剑指offer--二进制中1的个数
- HDU
- java中Map,List与Set的区别
- Java高性能集合类 ConcurrentLinkedHashMap demo
- 实现带header和footer功能的RecyclerView——完善篇
- django 1.11通用视图自带分页功能
- 文章标题 HDU 1054 :Strategic game (树形DP)