hdu1166 hdu1394 hdu2795

注意输入输出 线段树 单点更新

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <string>

using namespace std;

#define MAXN 60000

int n, val[MAXN];

struct Node
{
    int l, r, sum;
}node[MAXN << 2];

void build(int i, int l, int r)
{
    node[i].l = l;
    node[i].r = r;

    if (l == r)
    {
        node[i].sum = val[l];
        return ;
    }
    int mid = (l + r) >> 1;
    build(i * 2, l, mid);
    build(i * 2 + 1, mid + 1, r);
    node[i].sum = node[i * 2].sum + node[i * 2 + 1].sum;
}

void update(int i, int x, int val)
{
    int l = node[i].l;
    int r = node[i].r;
    if (l == x && r == x)
    {
        node[i].sum += val;
        return ;
    }

    int mid = (l + r) >> 1;
    if (x <= mid)
    {
        update(i * 2, x, val);
    }
    else
    {
        update(i * 2 + 1, x, val);
    }

    node[i].sum = node[i * 2].sum + node[i * 2 + 1].sum;
}

int query(int i, int x, int y)
{
    int l = node[i].l;
    int r = node[i].r;
    if (l == x && r == y)
    {
        return node[i].sum;
    }

    int mid = (l + r) >> 1;

    if (y <= mid)
    {
        return query(i * 2, x, y);
    }
    else if (x > mid)
    {
        return query(i * 2 + 1, x, y);
    }
    else
    {
        return query(i * 2, x, mid) + query(i * 2 + 1, mid + 1, y);
    }
}

char ch[20];

void input()
{
    int t, cases = 0, x, y, v;

    cin >> t;

    while (t--)
    {
        cin >> n;

        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &val[i]);
        }

        build(1, 1, n);

        cout << "Case " << ++cases << ":" << endl;

        while (true)
        {
            scanf("%s", ch);
            if (ch[0] == 'E')
            {
                break;
            }
            else if (ch[0] == 'A')
            {
                scanf("%d %d", &x, &v);
                update(1, x, v);
            }
            else if (ch[0] == 'S')
            {
                scanf("%d %d", &x, &v);
                update(1, x, -v);
            }
            else
            {
                scanf("%d %d", &x, &y);
                printf("%d\n", query(1, x, y));
            }
        }
    }
}

int main()
{
    input();
    return 0;
}
hdu1394 求最小逆序
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <string>

using namespace std;

#define MAXN 6000

int val[MAXN], n;
struct Node
{
    int l, r, sum;
}node[MAXN << 2];

void build(int i, int l, int r)
{
    node[i].l = l, node[i].r = r;
    if (l == r)
    {
        node[i].sum = 0;
        return ;
    }
    int mid = (l + r) >> 1;
    build(i * 2, l, mid);
    build(i * 2 + 1, mid + 1, r);
    node[i].sum = node[i * 2].sum + node[i * 2 + 1].sum;
}

void update(int i, int x)
{
    int l = node[i].l, r = node[i].r;
    if (l == x && r == x)
    {
        node[i].sum = 1;
        return ;
    }
    int mid = (l + r) >> 1;
    if (x <= mid)
    {
        update(i * 2, x);
    }
    else
    {
        update(i * 2 + 1, x);
    }
    node[i].sum = node[i * 2].sum + node[i * 2 + 1].sum;
}

int query(int i, int x, int y)
{
    int l = node[i].l, r = node[i].r;
    if (l == x && r == y)
    {
        return node[i].sum;
    }
    int mid = (l + r) >> 1;
    if (y <= mid)
    {
        return query(i * 2, x, y);
    }
    else if (x > mid)
    {
        return query(i * 2 + 1, x, y);
    }
    else
    {
        return query(i * 2, x, mid) + query(i * 2 + 1, mid + 1, y);
    }
}

void input()
{
    while (cin >> n)
    {
        build(1, 0, n - 1);

        int ans = 0;

        for (int i = 0; i < n; i++)
        {
            scanf("%d", &val[i]);
            update(1, val[i]);
            ans += i + 1 - query(1, 0, val[i]);
        }

        int res = ans;

        for (int i = 0; i < n - 1; i++)
        {
            ans = ans - val[i] - 1 + n - val[i];
            res = min(res, ans);
        }
        cout << res << endl;
    }
}

int main()
{
    input();
    return 0;
}



hdu2795

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <string.h>

using namespace std;

const int MAXN = 200000;

int h, w, n;
struct Node
{
    int l, r, sum;
}node[MAXN << 2];

void build(int i, int l, int r)
{
    node[i].l = l, node[i].r = r;
    if (l == r)
    {
        node[i].sum = w;
        return ;
    }
    int mid = (l + r) >>1;
    build(i * 2, l, mid);
    build(i * 2 + 1, mid + 1, r);
    node[i].sum = max(node[i * 2].sum, node[i * 2 + 1].sum);
}

int update(int i, int val)
{
    int l = node[i].l, r = node[i].r;

    if (l == r)
    {
        node[i].sum -= val;
        return l + 1;
    }
    int res = 0;

    if (node[i * 2].sum >= val)
    {
        res = update(i * 2, val);
    }
    else
    {
        res = update(i * 2 + 1, val);
    }

    node[i].sum = max(node[i * 2].sum, node[i * 2 + 1].sum);
    return res;
}

void input()
{
    int val;

    while (cin >> h >> w >> n)
    {
        build(1, 0, min(h, n) - 1);

        for (int i = 0; i < n; i++)
        {
            scanf("%d", &val);

            if (node[1].sum < val)
            {
                printf("-1\n");
            }
            else
            {
                printf("%d\n", update(1, val));
            }
        }
    }
}

int main()
{
    input();
    return 0;
}

hdu1698 区间更新
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <string>

using namespace std;

const int MAXN = 100000 + 10;

int n, m;
struct Node
{
    int l, r, sum, lazy, val;
}node[MAXN << 2];

void build(int i, int l, int r)
{
    node[i].l = l, node[i].r = r;
    node[i].val = 1;
    node[i].lazy = 0;
    if (l == r)
    {
        node[i].sum = 1;
        return ;
    }
    int mid = (l + r) >> 1;
    build(i * 2, l, mid);
    build(i * 2 + 1, mid + 1, r);
    node[i].sum = node[i * 2].sum + node[i * 2 + 1].sum;
}

void update(int i, int x, int y, int val)
{
    int l = node[i].l, r = node[i].r;
    if (l == x && y == r)
    {
        node[i].sum = (r - l + 1) * val;
        node[i].val = val;
        node[i].lazy = 1;
        return ;
    }
    int mid = (l + r) >> 1;

    if (node[i].lazy)
    {
        node[i].lazy = 0;
        node[i * 2].sum = (node[i * 2].r - node[i * 2].l + 1) * node[i].val;
        node[i * 2 + 1].sum = (node[i * 2 + 1].r - node[i * 2 + 1].l + 1) * node[i].val;
        node[i * 2].val = node[i * 2 + 1].val = node[i].val;
        node[i * 2].lazy = node[i * 2 + 1].lazy = 1;
    }

    if (y <= mid)
    {
        update(i * 2, x, y, val);
    }
    else if (x > mid)
    {
        update(i * 2 + 1, x, y, val);
    }
    else
    {
        update(i * 2, x, mid, val);
        update(i * 2 + 1, mid + 1, y, val);
    }

    node[i].sum = node[i * 2].sum + node[i * 2 + 1].sum;
}

void input()
{
    int t, x, y, val, cases = 0;

    scanf("%d", &t);

    while (t--)
    {
        scanf("%d", &n);
        scanf("%d", &m);
        build(1, 0, n - 1);

        for (int i = 0; i < m; i++)
        {
            scanf("%d %d %d", &x, &y, &val);
            update(1, x - 1, y - 1, val);
        }

        cout << "Case " << ++cases << ": The total value of the hook is " << node[1].sum << "." << endl;
    }
}

int main()
{
    input();
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值