2019中山大学程序设计竞赛--Party--思维+线段树

Problem Description

n person have just entered a company, and Xiaoxun, as a supervisor, gives each of them a number from 1 to n that is not repeated.

In order to let them to get to know each other better, they would have a party every day, and there was a limit on the number of people at each party. Xiaoxun didn't want to find a way to find people randomly. So every time he had a party, he would only invite people with numbers in a certain interval to play.

The people who come to the party will get to know each other, and Xiaoxun wants to know how effective each party is. The effect is evaluated as the number of pairs of people at this party who did not appear at the same previous party.

 

 

Input

There are mutiple test cases.

Each case starts with a line containing two integers n,m(n≤5×105,m≤5×105) which represent the number of people and the number of parties. And this is followed by m lines each containing two integers l,r(1≤l≤r≤n) which mean the interval, and only the people whose number in the interval can take part in this party.

 

 

Output

For each case you should print m lines.

Each line containing a integer mean the effect of this party.

 

 

Sample Input

 

5 4 1 2 4 5 1 4 2 5

 

 

Sample Output

 

1 1 5 2

Hint

explaination of sample: The first party: 1-2 The second party: 4-5 The third party: 1-3 1-4 2-3 2-4 3-4 The fourth party: 2-5 3-5

这个题当时不会做啊,还是看的题解才想明白的。

就是先看一个区间,然后每个值让他的a[i]=i,每次要查询时,需要先更新一下这个区间内所有的a[i]。

a[i]更改的目标值是这个区间内最左端点的a[i]值。

用原始a[i]减去现在a[i]并累加就是和了。

但是要注意区间最大值的维护。

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <string>
#include <iostream>
#include <queue>
#include <math.h>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define left (now<<1)
#define right ((now<<1)+1)
#define mid ((l + r) >> 1)
#define midmid ((r + mid) >> 1)
#define LONG_LONG_MIN -9223372036854775808ll
#define LONG_LONG_MAX 9223372036854775807ll
using namespace std;
typedef long long int ll;

const int MAXN = 5e5 + 10;

struct segmentTree
{
    ll num,maxn;
};

int n,m;
segmentTree tree[MAXN * 4];

void builtTree(int now,int l,int r)
{
    if(l == r)
    {
        tree[now].num = tree[now].maxn = l;
        return;
    }
    builtTree(left,l,mid);
    builtTree(right,mid + 1,r);
    tree[now].maxn = max(tree[left].maxn,tree[right].maxn);
    //建立树,并初始化最大值;
}

ll getsum(int now,int l,int r,int ql,int qr,ll x)
{
    if(x >= tree[now].maxn)
    {
        return 0;
    }//如果左端点>最大值,就不用再往下走了。就相当于没有可以用的区间了。
    if(l == r)
    {
        ll re = tree[now].num - x;
        tree[now].num = tree[now].maxn = x;
        return re;//到叶子节点了。
    }
    if(ql <= l && qr >= r)
    {
        if(x >= tree[now].maxn)
        {
            return 0;//----此时本要更新最小值为x的,可是最大值已经小x,那么此时已经没有合理区间
        }
        else
        {
            ll re = 0;//那个差值
            re += getsum(left,l,mid,ql,min(qr,mid),x);
            re += getsum(right,mid + 1,r,max(mid + 1,ql),qr,x);
            tree[now].maxn = max(tree[left].maxn,tree[right].maxn);
            return re;
        }
    }
    ll re = 0;
    if(ql <= mid)
    {
        re += getsum(left,l,mid,ql,min(qr,mid),x);//-----
    }
    if(qr > mid)
    {
        re += getsum(right,mid + 1,r,max(ql,mid + 1),qr,x);//
    }
    tree[now].maxn = max(tree[left].maxn,tree[right].maxn);
    return re;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        builtTree(1,1,n);
        for(int i = 1; i <= m; ++i)
        {
            ll l,r;
            scanf("%I64d%I64d",&l,&r);
            ll need = getsum(1,1,n,l,r,l);
            ll ans = need;
            printf("%I64d\n",ans);
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值