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

扫描二维码关注公众号,回复: 6093342 查看本文章

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;
}

猜你喜欢

转载自blog.csdn.net/haohaoxuexilmy/article/details/89412580