QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#367416 | #5144. Set of Intervals | Baiyu0123 | WA | 8ms | 9732kb | C++14 | 2.5kb | 2024-03-25 22:30:08 | 2024-03-25 22:30:09 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pii pair<int,int>
#define mkp make_pair
using namespace std;
const int maxn=2e5+100;
int n,sum=0;
pii p[maxn],Q[maxn];
int G[maxn],a[maxn],h[maxn],z[maxn];
set<int> s;
int cnt[maxn],w[maxn];
ll ans=0;
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int T;cin>>T;
while (T--) {
cin>>n;ans=0;sum=0;
for (int i=1;i<=n;i++) {
cin>>p[i].fi>>p[i].se;
G[2*i-1]=p[i].fi-1;
G[2*i]=p[i].se;
}
if (n==1) {
cout<<1<<endl;
continue;
}
sort(G+1,G+2*n+1);
int Gw=unique(G+1,G+2*n+1)-G-1;
for (int i=2;i<=Gw;i++) a[i]=G[i]-G[i-1];
for (int i=1;i<=n;i++) {
p[i].fi=lower_bound(G+1,G+Gw+1,p[i].fi-1)-G+1;
p[i].se=lower_bound(G+1,G+Gw+1,p[i].se)-G;
Q[2*i-1]=mkp(p[i].fi,+i);
Q[2*i]=mkp(p[i].se+1,-i);
}
sort(Q+1,Q+2*n+1);
assert(Q[1].fi==2);
for (int i=2,j=0;i<=Gw;i++) {
z[i]=z[i-1];
while (j+1<=2*n&&Q[j+1].fi==i) {
j++;
if (Q[j].se>0)s.insert(Q[j].se);
else z[i]++,s.erase(-Q[j].se);
}
cnt[i]=s.size();
if (cnt[i]==1) w[i]=*s.begin(),h[w[i]]+=a[i];
if (cnt[i]!=0) sum+=a[i];
}
for (int i=1;i<=n;i++) {
if (h[i]!=0) {
int L=z[p[i].fi-1],R=n-z[p[i].se];
ans+=1ll*h[i]*(sum-1);
if (p[i].fi==p[i].se) {
if (L==0||R==0) ans-=1ll*h[i]*(h[i]-1);
continue;
}
if (cnt[p[i].fi]==1&&L==0) ans-=1ll*a[p[i].fi]*(a[p[i].fi]-1);
if (cnt[p[i].se]==1&&R==0) ans-=1ll*a[p[i].se]*(a[p[i].se]-1);
if (cnt[p[i].fi]==1&&L==0&&cnt[p[i].se]==1&&R==0)
ans-=2ll*a[p[i].fi]*a[p[i].se];
}
}
s.clear();
for (int i=2;i<=Gw;i++) {
// cout<<i-1<<" : "<<ans<<endl;
if (cnt[i]==0) {
if (n==2) continue;
int L=z[i],R=n-z[i];
assert(L!=0&&R!=0);
if (L>=2&&R>=2) {
ans+=1ll*a[i]*sum;
ans+=1ll*a[i]*(G[Gw]-G[1]-1);
} else {
if (L==1&&R==2) {
ans+=2ll*a[i]*(sum-(G[i-1]-G[1]));
}
if (L==1&&R>2) {
ans+=1ll*a[i]*(sum-(G[i-1]-G[1]));
ans+=1ll*a[i]*(G[Gw]-G[i]);
}
if (R==1&&L==2) {
ans+=2ll*a[i]*(sum-(G[Gw]-G[i]));
}
if (R==1&&L>2) {
ans+=1ll*a[i]*(sum-(G[Gw]-G[i]));
ans+=1ll*a[i]*(G[i-1]-G[1]);
}
}
continue;
}
if (cnt[i]==2) {
ans+=1ll*a[i]*(sum-1);
continue;
}
}
for (int i=0;i<=max(n,Gw);i++) {
h[i]=w[i]=G[i]=z[i]=a[i]=0;
// cout<<"["<<G[i-1]+1<<","<<G[i]<<"] "<<cnt[i]<<" "<<w[i]<<endl;
}
cout<<ans/2<<endl;
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 9732kb
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: 8ms
memory: 7724kb
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: 5652kb
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:
87909653638688359 20028046845476373 243648882560823245 174616020493159536 32199128172390957 62496028822159158 334204970438096835 60037786115991450 81626954412190714 48056622637419225 71572487165112226 354010409517046269 271548519954568649 3977894369970801 64457797477130583 58500276931576341 92597961...
result:
wrong answer 1st numbers differ - expected: '87849603321470913', found: '87909653638688359'