QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#784884#5144. Set of IntervalsqlwpcWA 3ms5928kbC++142.6kb2024-11-26 16:14:062024-11-26 16:14:09

Judging History

你现在查看的是最新测评结果

  • [2024-11-26 16:14:09]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:5928kb
  • [2024-11-26 16:14:06]
  • 提交

answer

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
const int N = 600300;
const int mod = 998244353;
using ll = long long;
using namespace std;
using pii = pair<int,int>;
const int INF = 0x3f3f3f3f;
int read(){
    int x=0,flag=1;char c;
    while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
    while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x*flag;
}
int l[N],r[N];
int main()
{
    int T=read();
    while(T--)
    {
        int n=read();
        for (int i=1;i<=n;++i) l[i]=read(),r[i]=read();
        if (n==1) printf("1\n");
        else if (n==2)
        {
            ll ans=0;
            if (l[1]>l[2]) swap(l[1],l[2]), swap(r[1],r[2]);
            if (r[1]<l[2]) ans = 1ll*(r[1]-l[1]+1)*(r[2]-l[2]+1);
            else if (r[1]<=r[2])
            {// l1 l2 r1 r2
                ans = 1ll*(l[2]-l[1])*(r[2]-l[2]+1) + 1ll*(r[1]-l[2]+1)*(r[1]-l[2])/2 + 
                      1ll*(r[2]-r[1])*(r[1]-l[1]+1);
            }else
            {// l1 l2 r2 r1
                int L=l[1],R=r[1],SL=l[2],SR=r[2];
                ans = (SR-SL+1ll)*(SR-SL)/2 + 1ll*(SR-SL+1)*(SL-L+R-SR);
            }
            printf("%lld\n",ans);
        }else
        {
            ll ans=0;
            int L=INF,L1=0,R0=INF,R=0,SL=INF,SR=0;
            int Lidx=0,Ridx=0;
            for (int i=1;i<=n;++i)
            {
                if (l[i]<L) 
                {
                    if (Lidx!=Ridx)
                        SL=min(L,SL), SR=max(SR, L1);
                    L=l[i];L1=r[i];
                    Lidx=i;
                }
                else if (r[i]>R||r[i]==R&&Ridx==Lidx)
                {
                    if (Ridx!=Lidx)
                        SR=max(R, SR), SL=min(SL, R0);
                    R=r[i]; R0=l[i];
                    Ridx=i;
                } 
                else {
                    if (r[i]>SR) SR=r[i];
                    if (l[i]<SL) SL=l[i];
                }
            }
            //printf("L=%d R=%d SL=%d SR=%d\n",L,R,SL,SR);
            if (Ridx!=Lidx)
            {
                // L , SL , SR, R
                ans = (SR-SL+1ll)*(SR-SL)/2 + 1ll*(SR-SL+1)*(SL-L+R-SR);
                // [L, SR] [R0, R] 
                int TL=max(L1,SR), TR=min(SL,R0);
                // [L, TL] [TR, R]
                ans += 1ll*(min(TL+1, SL)-L) * (R-max(SR, TR-1));
            }else
            {
                ans = (SR-SL+1ll)*(SR-SL)/2 + 1ll*(SR-SL+1)*(SL-L+R-SR);
            }
            printf("%lld\n",ans);
        }
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5928kb

input:

4
1
1 1000000000
2
1 1000000000
1 1000000000
4
1 2
3 4
5 6
7 8
4
1 3
2 4
5 8
6 7

output:

1
499999999500000000
26
28

result:

ok 4 number(s): "1 499999999500000000 26 28"

Test #2:

score: 0
Accepted
time: 0ms
memory: 5736kb

input:

10000
1
778216842 910688403
1
513404058 890988011
1
1 1000000000
1
1 1000000000
1
758932694 848837772
1
516433381 715411928
1
1 1000000000
1
1 1000000000
1
1 1000000000
1
1 1000000000
1
1 1000000000
1
652548522 898071173
1
1 1000000000
1
509357508 603420032
1
1 1000000000
1
657294869 887475066
1
1 1...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 10000 numbers

Test #3:

score: -100
Wrong Answer
time: 3ms
memory: 5884kb

input:

10000
2
427286995 863604876
582970459 874563920
2
181948005 565025282
799528580 848659925
2
1 1000000000
716032287 836380611
2
383809946 544540272
520881396 990456979
2
156308569 178412791
731100211 963724967
2
426113388 802990296
556666621 560014605
2
1 1000000000
575838571 811122140
2
255734272 64...

output:

89555745253519329
18821102290156188
113106465274673025
136317615532971115
5141989504048811
1256173734724260
207604390726385765
84029959076101827
37024984393194673
66477540333968810
22573394495301601
143688903920213372
166852999281224709
3932968549941246
69893746982274994
49459548807361176
7005773885...

result:

wrong answer 1st numbers differ - expected: '87849603321470913', found: '89555745253519329'