中山大学重现赛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

题意是给出m个时间段,这个时间段之内的人可以一对一对的参加聚会。每对只能参加一次。

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+5;
int rr[maxn];
int main()
{
    int n,m,l,r;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1; i<=n; i++)
            rr[i]=i;
        for(int j=0; j<m; j++)
        {
            scanf("%d%d",&l,&r);
            long long ans=0;
            for(int i=l; i<=r; i++)
            {
                if(rr[i]>=r)
                    break;
                ans+=(r-rr[i]);
                rr[i]=max(rr[i],r);
                //for(int k=1;k<=n;k++){
                  //  cout<<rr[k]<<" ";
                //}
                //cout<<endl;
            }
            printf("%lld\n",ans);
        }
    }
    return 0;
}

 rr[ i ] 的变化 :

以后可以这么想:确定l,r 可以知道有多少对,所以记录这个位置的值大小即可。2 4: (2,3)(2,4)(3,4)4-2+4-3

如果前面出现过 就增大为右端点的值,下次直接相减即可。

5 4
1 2
2 2 3 4 5
1
4 5
2 2 3 5 5
1
1 4
4 2 3 5 5
4 4 3 5 5
4 4 4 5 5
5
2 5
4 5 4 5 5
4 5 5 5 5
2

1004

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<bitset>
#include<stack>
#include<set>
#include<vector>
#include <time.h>
#include<string.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

const int maxn = 1e5+5;
const int Mod=1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e=exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;

int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        vector< vector<int> > v(n+2);
        for(int i=0;i<=n+1;i++) v[i].resize(m+2);
        vector< vector<int> > sum(n+2);
        for(int i=0;i<=n+1;i++) sum[i].resize(m+2);
        int p;
        scanf("%d",&p);
        for(int i=1;i<=p;i++)
        {
            int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            v[a][b]++;
            v[c+1][d+1]++;
            v[c+1][b]--;
            v[a][d+1]--;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                v[i][j]+=v[i-1][j]+v[i][j-1]-v[i-1][j-1];
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(v[i][j]>=1) v[i][j]=1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+v[i][j];
            }
        }

        int q;
        scanf("%d",&q);
        while(q--)
        {
             int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            int cnt=sum[c][d]-sum[a-1][d]-sum[c][b-1]+sum[a-1][b-1];
            if(cnt==(d-b+1)*(c-a+1)) printf("YES\n");
            else printf("NO\n");
        }
    }
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值