【题解】CQOI2015任务查询系统

  主席树,操作上面基本上是一样的。每一个时间节点一棵树,一个树上的每个节点代表一个优先级的节点。把开始和结束时间点离散,在每一棵树上进行修改。注意因为一个时间节点可能会有多个修改,但我们要保证都在同一棵树上,所以我采取了让每个节点额外存储所属于的树的一个信息。当更新到一个节点的时候,如果属于建立好的新树,那么就修改;不属于,则重新建链。

  但这题有一个地方需要注意,就是所求的个数有可能小于一个叶子结点中存储的任务个数,这时我们就要返回k * num。

代码(有的地方感觉自己写的有些啰嗦,大家看看就好):

// luogu-judger-enable-o2
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 1500000
int n, m, timer, tot, cnt, sum, pre = 1, root[maxn], Ti[maxn];
map <int, int> Map;
bool vis[maxn];
struct node
{
    int t, num;
}upd[maxn * 2];

struct tree
{
    int lson, rson, cal, root, size, rank;
}T[maxn * 5];

struct project
{
    int s, e, p;
}P[maxn];

bool cmp(project a, project b)
{
    return a.p < b.p;
}

bool cmp2(node a, node b)
{
    return a.t < b.t;
}

int read()
{
    int x = 0, k = 1;
    char c;
    c = getchar();
    while(c < '0' || c > '9') { if(c == '-') k = -1; c = getchar(); }
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * k;
}

void update(int &now, int bf, int x, int num, int l, int r, int root)
{
    if(root != T[now].root) now = ++ sum, T[now] = T[bf], T[now].root = root;
    if(num > 0) T[now].size += 1;
    else T[now].size -= 1;
    if(l == r)
    {
        T[now].cal += num;
        T[now].rank = num;
        return;
    }
    int mid = (l + r) >> 1;
    if(x <= mid) update(T[now].lson, T[bf].lson, x, num, l, mid, root);
    else update(T[now].rson, T[bf].rson, x, num, mid + 1, r, root);
    T[now].cal = T[T[now].lson].cal + T[T[now].rson].cal;
}

int query(int now, int L, int R, int k)
{
    int mid = (L + R) >> 1;
    int size = T[T[now].lson].size;
    if(L == R) return k * T[now].rank;
    if(k >= T[now].size) return T[now].cal; 
    if(k <= size) return query(T[now].lson, L, mid, k);
    else return query(T[now].lson, L, mid, size) + query(T[now].rson, mid + 1, R, k - size);
}

signed main()
{
    m = read(), n = read();
    for(int i = 1; i <= m; i ++)
    {
        P[i].s = read(), P[i].e = read(), P[i].p = read();
        upd[++ cnt] = (node) {P[i].s, P[i].p};
        upd[++ cnt] = (node) {P[i].e + 1, -P[i].p};
    }
    sort(P + 1, P + 1 + m, cmp);
    for(int i = 1; i <= m; i ++)
        if(P[i].p != P[i - 1].p) Map[P[i].p] = ++ tot;
    sort(upd + 1, upd + 1 + cnt, cmp2);
    for(int i = 1; i <= cnt; i ++) 
    {
        if(upd[i].t != upd[i - 1].t) 
        {
            int now = ++ timer; Ti[upd[i].t] = now;
            root[timer] = ++ sum;
            int t = upd[i].t - 1;
            while(!Ti[t]) Ti[t] = now - 1, t --;
        }
        int t = Ti[upd[i].t];
        update(root[t], root[t - 1], Map[abs(upd[i].num)], upd[i].num, 1, tot, t); 
        vis[t] = true;
    }
    for(int i = 1; i <= n; i ++)
    {
        int x = read(), a = read(), b = read(), c = read();
        int ans, k = 1 + (a * pre + b) % c;
        printf("%lld\n", ans = query(root[Ti[x]], 1, tot, k));
        pre = ans;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/twilight-sx/p/8525725.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值