1. 程式人生 > >luogu3168 [CQOI2015]任務查詢系統

luogu3168 [CQOI2015]任務查詢系統

%d cqoi2015 log pos ++ bit date () class

樹狀數組不用動腦子真爽啊

#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int m, n, b[100005], rem, rot[100005], lson[20000005], rson[20000005], siz[20000005];
int cnt, xi, ai, bi, ci, ki, qque[45], len;
ll sum[20000005], lst=1;
struct Task{
    int fro, too, val;
}tsk[100005];
int
lowbit(int x){ return x&-x; } void update(int &rt, int l, int r, int x, int val){ if(!rt) rt = ++cnt; siz[rt] += val; sum[rt] += val * b[x]; if(l==r) return ; int mid=(l+r)>>1; if(x<=mid) update(lson[rt], l, mid, x, val); if(mid<x) update(rson[rt], mid+1
, r, x, val); } ll query(int l, int r, int k){ if(l==r) return (ll)k*b[l]; int tmp=0; ll tat=0; for(int i=1; i<=len; i++) tmp += siz[lson[qque[i]]], tat += sum[lson[qque[i]]]; int mid=(l+r)>>1; if(k<=tmp){ for(int i=1; i<=len; i++) qque[i] = lson[qque[i]]; return
query(l, mid, k); } else{ for(int i=1; i<=len; i++) qque[i] = rson[qque[i]]; return query(mid+1, r, k-tmp)+tat; } } int main(){ cin>>m>>n; for(int i=1; i<=m; i++){ scanf("%d %d %d", &tsk[i].fro, &tsk[i].too, &tsk[i].val); b[i] = tsk[i].val; } sort(b+1, b+1+m); rem = unique(b+1, b+1+m) - (b + 1); for(int i=1; i<=m; i++){ int tmp=lower_bound(b+1, b+1+rem, tsk[i].val)-b; for(int j=tsk[i].fro; j<=n; j+=lowbit(j)) update(rot[j], 1, rem, tmp, 1); for(int j=tsk[i].too+1; j<=n; j+=lowbit(j)) update(rot[j], 1, rem, tmp, -1); } for(int i=1; i<=n; i++){ scanf("%d %d %d %d", &xi, &ai, &bi, &ci); ki = 1 + ((ll)ai*lst+bi) % ci; int tmp=0; ll ppp=0; for(int i=xi; i; i-=lowbit(i)){ tmp += siz[rot[i]]; ppp += sum[rot[i]]; } if(tmp<ki) lst = ppp; else{ len = 0; for(int i=xi; i; i-=lowbit(i)) qque[++len] = rot[i]; lst = query(1, rem, ki); } printf("%lld\n", lst); } return 0; }

luogu3168 [CQOI2015]任務查詢系統