QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#784962#5144. Set of IntervalsqlwpcWA 3ms5992kbC++142.8kb2024-11-26 16:26:552024-11-26 16:26:59

Judging History

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

  • [2024-11-26 16:26:59]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:5992kb
  • [2024-11-26 16:26:55]
  • 提交

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
                int L=l[1],R=r[2],SL=l[2],SR=r[1];
                ans = (SR-SL+1ll)*(SR-SL)/2 + 1ll*(SR-SL+1)*(SL-L+R-SR);
                ans += 1ll*(SL-L)*(R-SR);
            }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;
                    if (r[i]>R)
                    {
                        SR=max(R, SR), SL=min(SL, R0);
                        R=r[i]; R0=l[i];
                        Ridx=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);
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 5712kb

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: 0
Accepted
time: 2ms
memory: 5928kb

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:

87849603321470913
18821102290156188
113106465274673025
75195165925255965
5141989504048811
1256173734724260
207604390726385765
56483863859672889
37024984393194673
36132096841419954
22573394495301601
143688903920213372
166852999281224709
3932968549941246
63833225117266922
49459548807361176
70057738855...

result:

ok 10000 numbers

Test #4:

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

input:

10000
3
1 1000000000
521067549 666980062
562319713 714009993
3
42407212 75107815
610219669 973789020
873806856 902158965
3
1 1000000000
673648508 809445815
599994567 757240668
3
92939611 410549965
597447135 678622426
676819188 811525072
3
230740729 455081734
606008912 926990742
615223886 822068791
3...

output:

174329051362239765
85557920642565305
187516336041444375
122087055515083130
162964083984911120
303666532800970347
176358932288144970
330593156050995465
62550768753522599
247444999637419584
223189919337023814
199454344726240709
186982464464105447
183846500125341984
200274422590053105
23873074464915904...

result:

wrong answer 2nd numbers differ - expected: '269965111370246655', found: '85557920642565305'