2018-08-13 树状数组&离散化

  • A  -- Stars

  • Description

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.


For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.

You are to write a program that will count the amounts of the stars of each level on a given map.

  • Input

The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate

  • Output

The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1

  • Sample Input

5

1 1

5 1

7 1

3 3

5 5

  • Sample Output

1

2

1

1

0

  • 题目理解

由于星星位置的给出顺序严格按照y的升序所以后面出现的星星都会在已经出现的星星上面,这时候只需要找出前面所有x坐标小于等于当前x坐标的星星数量就能得到星星的level由于每次插入都会改变后面所有坐标的前序和我们使用树状数组来简化插入代价。其中注意的是由于坐标值会出现0所有要将坐标进行向右一个单位的平移,否则在插入的循环过程,由于转移条件不改变i+=(i&(-i)所以导致死循环

#include<cstdio>
#include<cstring>
const int maxn=32005;
int tree[maxn],ans[maxn];
void insert_num(int i){
    for(;i<maxn;i+=(i&(-i)))
        tree[i]+=1;
}
int cal(int i){
    int sum=0;
    for(;i>0;i-=(i&(-i))){
        sum+=tree[i];
    }
    return sum;
}
int main()
{
    int n,x,y;
    while(scanf("%d",&n)!=EOF){
        memset(tree,0,sizeof(tree));
        memset(ans,0,sizeof(ans));
        for(int i=0;i<n;++i){
            scanf("%d%d",&x,&y);
            x++;//平移一位
            ans[cal(x)]++;
            insert_num(x);
        }
        for(int i=0;i<n;++i)
            printf("%d\n",ans[i]);
    }
    return 0;
}

 

  • B  -- Ultra-QuickSort

  • Description

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence

9 1 0 5 4 ,

Ultra-QuickSort produces the output

0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence

  • Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed

  • Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence

  • Sample Input

5

9 1 0 5 4

3

1 2 3

0

  • Sample Output

6

0

  • 题目理解

通过树状数组做逆序对统计之前元素小于当前元素的个数,然后使用总的数量减去统计得到的正序对,由于输入的数值太大,无法对999999999开数组进行存储所以要通过离散化将其转化为一个相对变化的序列,通过lower_bound二分查找在排序好的序列中的下标位置,就是相对大小,在这里同样要避免0的出现所以不是减去排序数组第一个元素的下标而是没有存储任何值的数组首下标

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=500005;
ll tree[maxn],id[maxn],a[maxn],b[maxn];
void insert_num(int i){
    for(;i<maxn;i+=(i&(-i)))
        tree[i]+=1;
}
ll cal(int i){
    ll sum=0;
    for(;i;i-=(i&(-i))){
        sum+=tree[i];
    }
    return sum;
}
void map_index(int n){
    memset(id,0,sizeof(id));//id等于数字原下标和排序后下标间关系就是离散值
    for(int i=1;i<=n;++i)//防止树状数组出现0
        scanf("%lld",&a[i]);
    memcpy(b+1,a+1,n*sizeof(ll));
   //for(int i=0;i<=n;++i)
       // printf("%lld   %lld\n",a[i],b[i]);
    sort(b+1,b+1+n);
    int cnt=unique(b+1,b+1+n)-(b+1);
    for(int i=1;i<=n;++i)
        id[i]=lower_bound(b+1,b+1+cnt,a[i])-b;//这里不减(b+1)得到相对0的坐标映射
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n){
        memset(tree,0,sizeof(tree));
        map_index(n);
        ll t_sum=0;
        for(int i=1;i<=n;++i){
            t_sum+=cal(id[i]);
            insert_num(id[i]);
        }
        ll ans=(ll)n;
        ans=ans*(ans-1)/2-t_sum;
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • C  -- 按钮控制彩灯试验

  • Description

应教学安排,yy又去开心的做电学实验了。实验的内容分外的简单一串按钮通过编程了的EEPROM可以控制一串彩灯。然而选择了最low的一种一对一的控制模式,并很快按照实验指导书做完实验的yy马上感觉到十分无趣。于是他手指在一排按钮上无聊的滑来滑去,对应的彩灯也不断的变化着开关。已知每一个按钮按下会改变对应一个彩灯的状态,如此每次yy滑动都会改变一串彩灯的状态。现已知彩灯最初的状态,已经yy n次无聊的滑动的起点和终点l,r。现问彩灯最终的状态

  • Input

有多组数据。
每组数据第一行,n(1<=n<=10^5)代表彩灯串长度,t(0<=t<=10^5)代表yy滑动的次数
第二行n个数(0表示灭1表示亮)给出n个彩灯的目前的状态。
之后t行每行两个数li,ri(1<=li<=ri<=n)代表每次滑动的区间。

  • Output

每组用一行输出最终的串的状态,格式见样例。

  • Sample Input

3 2

1 0 1

1 3

2 3

  • Sample Output

0 0 1

  • 题目理解

这道题的思路是给定区间分别记录左端点数和又端点数。对于l(i)为前i个端点作为左端点被点击的前序和,r(i)为前i个端点作为右端点被点击的前序和,所以l(i)-r(i-1)就是灯i被点击的次数,通过结合初始转态直接输出最后状态。这道题可以用树状数组去做但是其实不需要那么麻烦,因为前序和的计算只需要在所有区间都输出以后进行一次计算即可不需要动态变化求值,所以可以直接累计然后求一遍前序和

#include<cstdio>
#include<cstring>
const int maxn=100005;
int a[maxn];
void insert_num(int* tree,int i){
    for(;i<maxn;i+=(i&(-i)))
        tree[i]+=1;
}
int cal(int* tree,int i){
    int sum=0;
    for(;i;i-=(i&(-i))){
        sum+=tree[i];
    }
    return sum;
}
int main()
{
    int n,t,x,y;
    int l[maxn],r[maxn];
    while(scanf("%d%d",&n,&t)!=EOF){
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]);
        while(t--){
            scanf("%d%d",&x,&y);
            insert_num(l,x);
            insert_num(r,y);
        }
        for(int i=1;i<=n;++i){
            //printf("~~~~%d\n",a[i]);
            if((cal(l,i)-cal(r,i-1))&1)
                printf("%d",(a[i]+1)%2);
            else printf("%d",a[i]);
            if(i==n)printf("\n");
            else printf(" ");
        }
    }
    return 0;
}

 

  • E  -- Swaps and Inversions

  • Description

Long long ago, there was an integer sequence a.
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don't want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j)(i,j) which 1≤i<j≤n1≤i<j≤n and ai>aj

  • Input

There are multiple test cases, please read till the end of input file.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤1000001≤n,x,y≤100000, numbers in the sequence are in [−109,109][−109,109]. There're 10 test cases.

  • Output

For every test case, a single integer representing minimum money to pay.

  • Sample Input

3 233 666

1 2 3

3 1 666

3 2 1

  • Sample Output

0

3

  • 题目理解

逆序对的求法和B题一模一样就是对于最后结果来说根据贪心我们选取min(x,y)进行相乘

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=500005;
ll tree[maxn],id[maxn],a[maxn],b[maxn];
void insert_num(int i){
    for(;i<maxn;i+=(i&(-i)))
        tree[i]+=1;
}
ll cal(int i){
    ll sum=0;
    for(;i;i-=(i&(-i))){
        sum+=tree[i];
    }
    return sum;
}
void map_index(int n){
    memset(id,0,sizeof(id));//id等于数字原下标和排序后下标间关系就是离散值
    for(int i=1;i<=n;++i)//防止树状数组出现0
        scanf("%lld",&a[i]);
    memcpy(b+1,a+1,n*sizeof(ll));
   //for(int i=0;i<=n;++i)
       // printf("%lld   %lld\n",a[i],b[i]);
    sort(b+1,b+1+n);
    int cnt=unique(b+1,b+1+n)-(b+1);
    for(int i=1;i<=n;++i)
        id[i]=lower_bound(b+1,b+1+cnt,a[i])-b;//这里不减(b+1)得到相对0的坐标映射
}
int main()
{
    int n,x,y;
    while(scanf("%d%d%d",&n,&x,&y)!=EOF){
        memset(tree,0,sizeof(tree));
        map_index(n);
        ll t_sum=0;
        for(int i=1;i<=n;++i){
            t_sum+=cal(id[i]);
            insert_num(id[i]);
        }
        ll ans=(ll)n;
        ans=ans*(ans-1)/2-t_sum;
        if(x<y)
          printf("%lld\n",ans*x);
        else
          printf("%lld\n",ans*y);
    }
    return 0;
}

 

  • G  -- Matrix Sum

  • Description

You are given an N × N matrix. At the beginning every element is 0. Write a program supporting 2 operations:

  1.  1. Add x y value: Add value to the element Axy. (Subscripts starts from 0
  2.  2. Sum x1 y1 x2 y2: Return the sum of every element Axy for x1 ≤ x ≤ x2, y1 ≤ y ≤ y2.  
  • Input

The first line contains 2 integers N and M, the size of the matrix and the number of operations.

Each of the following M line contains an operation.

1 ≤ N ≤ 1000, 1 ≤ M ≤ 100000

For each Add operation: 0 ≤ x < N, 0 ≤ y < N, -1000000 ≤ value ≤ 1000000

For each Sum operation: 0 ≤ x1 ≤ x2 < N, 0 ≤ y1 ≤ y2 < N

  • Output

For each Sum operation output a non-negative number denoting the sum modulo 109+7

  • Sample Input

5 8

Add 0 0 1

Sum 0 0 1 1

Add 1 1 1

Sum 0 0 1 1

Add 2 2 1

Add 3 3 1

Add 4 4 -1

Sum 0 0 4 4

  • Sample Output

1

2

3

  • 题目理解

对树状数组的二维扩展,这里cal(i,j)得到矩阵(x\leq i ;y\leq j)所有元素的和。所以通过平移以后直接通过计算

\small ans=(cal(x1,y1)+cal(x2+1,y2+1)-cal(x1,y2+1)-cal(x2+1,y1)+mod)%\small mod

但是不知道为什么在计算的过程中我标记会出现负值的地方取模的时候+\small mod再取模会出现错误,只有把其去掉才能AC

//如果不平移出现0的话LOWBIT(0)就会造成死循环
#include<cstdio>
#include<cstring>
typedef long long ll;
const int maxn=1005;
const ll mod=1e9+7;
ll tree[maxn][maxn],val;
int n,m,x1,y1,x2,y2;
char opr[10];
void insert_num(int x,int y,ll val){
    for(int i=x;i<=n;i+=(i&(-i)))
        for(int j=y;j<=n;j+=(j&(-j)))
          tree[i][j]=(tree[i][j]+val)%mod;//可能出现负值
        //tree[i][j]=(tree[i][j]+val+mod)%mod;
}
ll cal(int x,int y){
    ll sum=0;
    for(int i=x;i;i-=(i&(-i)))
        for(int j=y;j;j-=(j&(-j)))
          sum=(sum+tree[i][j])%mod;//可能出现负值
        //sum=(sum+tree[i][j]+mod)%mod;

    return sum;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(tree,0,sizeof(tree));
        while(m--){
            scanf("%s",opr);
            if(opr[0]=='S'){
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                x1,y1,x2++,y2++;
                ll ans=(cal(x1,y1)+cal(x2,y2)-cal(x1,y2)-cal(x2,y1)+mod)%mod;//每个计算的地方都应该取模
                printf("%lld\n",ans);
            }else{
                scanf("%d%d%lld",&x1,&y1,&val);
                x1++,y1++;//一定要记得平移
                insert_num(x1,y1,val);
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值