题目链接

被自己的sb错误调到自闭。。

主席树的进阶应用。

把\(P_i\)离散化一下,得到每个\(P_i\)的排名,然后建一棵维护\(m\)个位置的主席树,每个结点记录区间总和和正在进行的任务数。

差分一下,主席树维护前缀和,每个时刻一个\(root\)。

然后就是线段树里查前\(k\)小了。

#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 4000010;
struct PT{
int lc, rc, cnt;
long long val;
}t[MAXN << 2];
int rk[MAXN];
struct lsh{
int val, id;
int operator < (const lsh A) const{
return val < A.val;
}
}p[MAXN];
int n, m;
struct Edge{
int next, to, dis;
}e[MAXN];
int head[MAXN], num;
long long pre = 1;
inline void Add(int from, int to, int dis){
e[++num].to = to; e[num].next = head[from]; e[num].dis = dis; head[from] = num;
}
inline int read(){
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
int a[MAXN], b[MAXN], c[MAXN], root[MAXN], cnt, r, A, B, C, X, K;
inline void pushup(int x){
t[x].val = t[t[x].lc].val + t[t[x].rc].val;
t[x].cnt = t[t[x].lc].cnt + t[t[x].rc].cnt;
}
int build(int l, int r){
if(l == r) return ++cnt;
int id = ++cnt, mid = (l + r) >> 1;
t[id].lc = build(l, mid);
t[id].rc = build(mid + 1, r);
return id;
}
int update(int q, int l, int r, int x, int y){
if(l == r){ t[++cnt].val = t[q].val + y; t[cnt].cnt = t[q].cnt + (y > 0 ? 1 : -1); return cnt; }
int id = ++cnt, mid = (l + r) >> 1;
t[id] = t[q];
if(x <= mid) t[id].lc = update(t[q].lc, l, mid, x, y);
else t[id].rc = update(t[q].rc, mid + 1, r, x, y);
pushup(id);
return id;
}
long long query(int q, int l, int r, int x){
if(l == r) return t[q].val;
int mid = (l + r) >> 1;
if(t[t[q].lc].cnt >= x) return query(t[q].lc, l, mid, x);
else return t[t[q].lc].val + query(t[q].rc, mid + 1, r, x - t[t[q].lc].cnt);
}
int main(){
m = read(); n = read();
for(int i = 1; i <= m; ++i){
a[i] = read(); r = max(r, b[i] = read());
p[i].id = i; p[i].val = c[i] = read();;
}
sort(p + 1, p + m + 1);
for(int i = 1; i <= m; ++i)
rk[p[i].id] = i;
for(int i = 1; i <= m; ++i){
Add( a[i] , rk[i], c[i]);
Add(b[i] + 1, rk[i], -c[i]);
}
root[0] = build(1, m);
for(int i = 1; i <= n; ++i){
int tmp = root[i - 1];
for(int j = head[i]; j; j = e[j].next)
tmp = update(tmp, 1, m, e[j].to, e[j].dis);
root[i] = tmp;
}
for(int i = 1; i <= n; ++i){
X = read(); A = read(); B = read(); C = read();
K = ((long long)A * pre + B) % C + 1;
printf("%lld\n", pre = query(root[X], 1, m, K));
}
return 0;
}

【洛谷 P3168】 [CQOI2015]任务查询系统(主席树)的更多相关文章

  1. 洛谷P3168 [CQOI2015]任务查询系统 [主席树,差分]

    题目传送门 任务查询系统 题目描述 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任 ...

  2. 洛谷 P3168 [CQOI2015]任务查询系统 解题报告

    P3168 [CQOI2015]任务查询系统 题目描述 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分. 超级计算机中的任务用三元组\((S_i,E_i,P_i) ...

  3. ●洛谷P3168 [CQOI2015]任务查询系统

    题链: https://www.luogu.org/problemnew/show/P3168题解: 主席树 强制在线? 那就直接对每一个前缀时间建一个线段树(可持久化线段树),线段树维护优先度权值. ...

  4. P3168 [CQOI2015]任务查询系统(主席树)

    题目描述 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第Ei ...

  5. 洛谷P3168 [CQOI2015]任务查询系统

    #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #in ...

  6. BZOJ3932: [CQOI2015]任务查询系统 主席树

    3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4869  Solved: 1652[Submit][St ...

  7. [CQOI2015]任务查询系统 主席树

    [CQOI2015]任务查询系统 LG传送门 以前还没见过主席树的这种写法. 考虑使用差分的思想处理每一个任务,然后所有的东西就都能顺理成章地用主席树维护了,查询的时候和平时的主席树有一点不同,详见代 ...

  8. 【BZOJ3932】[CQOI2015]任务查询系统 主席树

    [BZOJ3932][CQOI2015]任务查询系统 Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si, ...

  9. bzoj 3932: [CQOI2015]任务查询系统 -- 主席树 / 暴力

    3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec  Memory Limit: 512 MB Description 最近实验室正在为其管理的超级计算机编制一套任务管 ...

  10. BZOJ 3932: [CQOI2015]任务查询系统 | 主席树练习题

    题目: 洛谷也能评测 题解: De了好长时间BUG发现是自己sort前面有一行for没删,气死. 题目询问第x秒时候前k小的P值之和. 朴素想法: 我们可以把P值离散化,然后对于每个时刻建一棵定义域是 ...

随机推荐

  1. 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  2. 转---秒杀多线程第五篇 经典线程同步 关键段CS

    上一篇<秒杀多线程第四篇 一个经典的多线程同步问题>提出了一个经典的多线程同步互斥问题,本篇将用关键段CRITICAL_SECTION来尝试解决这个问题. 本文首先介绍下如何使用关键段,然 ...

  3. jmeter3.2生成图形html遇到的问题Error in NonGUIDriver java.lang.IllegalArgumentException: Results file:log is not empty

    遇到Creating summariser <summary> Error in NonGUIDriver java.lang.IllegalArgumentException: Resu ...

  4. 【数据库_Postgresql】数据库主键自增长之加序列和不加序列2种方法

    将表的主键进行序列增加之后可以在数据库层面自动主键id增长 方法如下:先建序列,然后建表关联id主键,然后添加语句,不用考虑id主键 DROP SEQUENCE IF EXISTS "pub ...

  5. 【CF600E】Lomsat gelral(dsu on tree)

    [CF600E]Lomsat gelral(dsu on tree) 题面 洛谷 CF题面自己去找找吧. 题解 \(dsu\ on\ tree\)板子题 其实就是做子树询问的一个较快的方法. 对于子树 ...

  6. 【loj6179】Pyh的求和

    Portal -->loj6179 Solution ​  这题其实有一个式子一喵一样的版本在bzoj,但是那题是\(m\)特别大然后只有一组数据  这题多组数据== ​    首先根据\(\v ...

  7. 【翻译】InterlockedIncrement内部是如何实现的?

        Interlocked系列函数可以对内存进行原子操作,它是如何实现的?     它的实现依赖于底层的CPU架构.对于某些CPU来说,这很简单,例如x86可以通过LOCK前缀直接支持Interl ...

  8. SpringMVC接收复杂集合对象(参数)代码示例

    原文: https://www.jb51.net/article/128233.htm SpringMVC接收复杂集合对象(参数)代码示例 更新时间:2017年11月15日 09:18:15   作者 ...

  9. move_base的全局路径规划代码研究

    algorithmn parameter code 主要是以下三个函数 计算所有的可行点 怎么计算一个点的可行点 从可行点中计算路径path todo algorithmn 算法的解释 Dijkstr ...

  10. 【题解】【LibreOJ Round #6】花团 LOJ 534 时间线段树分治 背包

    Prelude 题目链接:萌萌哒传送门(/≧▽≦)/ Solution 如果完全离线的话,可以直接用时间线段树分治来做,复杂度\(O(qv \log q)\). 现在在线了怎么办呢? 这其实是个假在线 ...