HDU 1166 敌兵布阵 伸展树splay简单练手题

题目

http://acm.hdu.edu.cn/showproblem.php?pid=1166

题意

C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:”你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:”我知错了。。。”但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.

Input
第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令

Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。

思路

简单线段树题目,用来练splay

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

#define key_val son[son[root][1]][0]
typedef long long ll;
const int N = 100010, INF = 0x3f3f3f3f, MOD = 1000000;
int son[N][2], pre[N], siz[N];
ll key[N], val[N];
int root, num, cas;
int arr[N];
int n;
void new_node(int &x, int fa, int v)
{
    x = ++num;
    pre[x] = fa, key[x] = val[x] = v;
    siz[x] = 1;
    son[x][0] = son[x][1] = 0;
}
void push_up(int x)
{
    siz[x] = siz[son[x][0]] + siz[son[x][1]] + 1;
    val[x] = key[x] + val[son[x][0]] + val[son[x][1]];
}
void _rotate(int x, int dir)
{
    int y = pre[x];
    son[y][!dir] = son[x][dir], pre[son[x][dir]] = y;
    if(pre[y]) son[pre[y]][son[pre[y]][1]==y] = x;
    pre[x] = pre[y];
    son[x][dir] = y, pre[y] = x;
    push_up(y);
}
void splay(int x, int goal)
{
    while(pre[x] != goal)
    {
        int y = pre[x];
        if(pre[y] == goal) _rotate(x, son[y][0] == x);
        else
        {
            int dir = son[pre[y]][0] == y;
            if(son[y][dir] == x) _rotate(x, !dir), _rotate(x, dir);
            else _rotate(y, dir), _rotate(x, dir);
        }
    }
    push_up(x);
    if(goal == 0) root = x;
}
int get_prec(int x)
{
    int t = son[x][0];
    while(son[t][1]) t = son[t][1];
    return t;
}
int get_subs(int x)
{
    int t = son[x][1];
    while(son[t][0]) t = son[t][0];
    return t;
}
int _find(int v)
{
    int x = root;
    while(x && key[x] != v) x = son[x][key[x]<v];
    return x;
}
void _delete(int v)
{
    int x = _find(v);
    if(!x) return;
    splay(x, 0);
    if(!son[root][0] || !son[root][1])
        root = son[root][0] + son[root][1], pre[root] = 0;
    else
    {
        int t = get_subs(root);
        splay(t, root);
        son[son[root][1]][0] = son[root][0], root = son[root][1];
        pre[son[root][0]] = root, pre[root] = 0;
        push_up(root);
    }
}
int get_kth(int k)
{
    int x = root;
    while(siz[son[x][0]] + 1 != k)
    {
        if(siz[son[x][0]] + 1 > k) x = son[x][0];
        else k -= (siz[son[x][0]] + 1), x = son[x][1];
    }
    return x;
}
bool _insert(int v)
{
    if(!root) new_node(root, 0, v);
    else
    {
        int x = root;
        while(son[x][key[x]<v])
        {
            if(key[x] == v)
            {
                splay(x, 0);
                return false;
            }
            x = son[x][key[x]<v];
        }
        new_node(son[x][key[x]<v], x, v);
        splay(son[x][key[x]<v], 0);
    }
    return true;
}
void build(int &x, int l, int r, int fa)
{
    if(l > r) return;
    int mid = (l + r) >> 1;
    new_node(x, fa, arr[mid]);
    build(son[x][0], l, mid-1, x);
    build(son[x][1], mid+1, r, x);
    push_up(x);
}
void init()
{
    root = num = 0;
    new_node(root, 0, 0);
    new_node(son[root][1], root, 0);
    build(key_val, 1, n, son[root][1]);
    push_up(son[root][1]), push_up(root);
}
ll query(int l, int r)
{
    splay(get_kth(l), 0);
    splay(get_kth(r+2), root);
    return val[key_val];
}
void update(int a, int b)
{
    int x = get_kth(a+1);
    splay(x, 0);
    key[x] += b;
    push_up(x);
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) scanf("%d", &arr[i]);
        init();
        char str[10];
        int a, b;
        printf("Case %d:\n", ++cas);
        while(scanf("%s", str), str[0] != 'E')
        {
            scanf("%d%d", &a, &b);
            if(str[0] == 'Q') printf("%lld\n", query(a, b));
            else if(str[0] == 'A') update(a, b);
            else update(a, -b);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值