HDU 2689 (树状数组模板)

2689

Sort it
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7404 Accepted Submission(s): 4766

Problem Description
You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need.
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.

Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.

Output
For each case, output the minimum times need to sort it in ascending order on a single line.

Sample Input
3
1 2 3
4
4 3 2 1

Sample Output
0
6

大概意思是给你一个1-n的全排列序列,求通过多少次交换,能让该序列成为一个单调递增的序列。
可以转化为求逆序对的个数,求select(i)就是求i前面有多少给小于i的数,则i-sum(i)就是A[i]的贡献,用树状数组可以解决。

```cpp
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1005;
int val[maxn];
int n;
int lowbit(int x)
{ //去掉最低位的1
    return x & (-x);
}
void update(int pos, int value)
{ //修改操作
    while (pos <= n)
    {
        val[pos] += value;
        pos += lowbit(pos);
    }
}
int select(int x)
{ //查询操作
    int res = 0;
    while (x)
    {
        res += val[x];
        x -= lowbit(x);
    }
    return res;
}
int main()
{
    int ans;
    while (~scanf("%d", &n))
    {
        ans = 0;
        memset(val, 0, sizeof(val));
        for (int i = 0; i < n; i++)
        {

            int x;
            scanf("%d", &x);
            update(x + 1, 1); //下标为0的逆序对是0,不用修改
            ans += i - select(x);
        }
        cout << ans << endl;
    }

    return 0;
}

题目描述 如题,已知一个数列,你需要进行下面两种操作:

将某一个数加上 x

求出某区间每一个数的和

输入格式 第一行包含两个正整数 n,m,分别表示该数列数字的个数和操作的总个数。

第二行包含 nn 个用空格分隔的整数,其中第 i个数字表示数列第 i 项的初始值。

接下来 mm 行每行包含 3 个整数,表示一个操作,具体如下:

1 x k 含义:将第 x 个数加上 k

2 x y 含义:输出区间 [x,y][x,y] 内每个数的和

输出格式 输出包含若干行整数,即为所有操作 2的结果。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 500005;
int val[maxn];
int n;
int lowbit(int x)
{ //去掉最低位的1
    return x & (-x);
}
void update(int pos, int value)
{ //修改操作
    while (pos <= n)
    {
        val[pos] += value;
        pos += lowbit(pos);
    }
}
int select(int x)
{ //查询操作
    int res = 0;
    while (x)
    {
        res += val[x];
        x -= lowbit(x);
    }
    return res;
}
int main()
{
    int m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> val[i];
    }
    while (m--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        switch (a)
        {
        case 1:
        {
            update(b, c);
            break;
        }
        case 2:
        {
            cout << select(c) - select(b - 1) << endl;
            break;
        }
        }
    }

    return 0;
}

题目描述 如题,已知一个数列,你需要进行下面两种操作:

将某区间每一个数数加上 x;

求出某一个数的值。

输入格式 第一行包含两个整数 N、M,分别表示该数列数字的个数和操作的总个数。

第二行包含 NN 个用空格分隔的整数,其中第 i 个数字表示数列第 i 项的初始值。

接下来 M 行每行包含 22 或 44个整数,表示一个操作,具体如下:

操作 1: 格式:1 x y k 含义:将区间 [x,y]内每个数加上 k;

操作 2: 格式:2 x 含义:输出第 x 个数的值。

输出格式 输出包含若干行整数,即为所有操作 22 的结果。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 500005;
int val[maxn];
int n;
int lowbit(int x)
{ //去掉最低位的1
    return x & (-x);
}
void update(int pos, int value)
{ //修改操作
    while (pos <= n)
    {
        val[pos] += value;
        pos += lowbit(pos);
    }
}
int select(int x)
{ //查询操作
    int res = 0;
    while (x)
    {
        res += val[x];
        x -= lowbit(x);
    }
    return res;
}
int main()
{
    int m;
    int pre = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        int a;
        scanf("%d", &a);
        update(i, a - pre);//差分
        pre = a;
    }
    while (m--)
    {
        int x;
        scanf("%d", &x);
        if (x == 1)
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            update(a, c);//先全部加上
            update(b + 1, -c);//减掉多加的
        }
        else
        {
            scanf("%d", &x);
            printf("%d\n", select(x));
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值