ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#202135 | #3524. C | Little09 | 100 | 2671ms | 76008kb | C++11 | 5.8kb | 2024-02-13 10:45:22 | 2024-02-13 13:01:56 |
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mem(x) memset(x,0,sizeof(x))
#define endl "\n"
#define printYes cout << "Yes\n"
#define printYES cout << "YES\n"
#define printNo cout << "No\n"
#define printNO cout << "NO\n"
#define lowbit(x) ((x)&(-(x)))
#define pb push_back
#define mkp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i,j,k) for (int i=(j);i<=(k);i++)
#define per(i,j,k) for (int i=(j);i>=(k);i--)
#define pcnt(x) __builtin_popcount(x)
mt19937_64 rnd(time(0));
template<class T>void chkmin(T&x,T y){x=min(x,y);}
template<class T>void chkmax(T&x,T y){x=max(x,y);}
const ll inf=1000000000000000000;
//const ll inf=1000000000;
//const ll mod=998244353;
//const ll mod=1000000007;
const int N=400005;
int n,m;
int a[N],b[N];
int fa[N],c[N],d[N],num[N];
ll ans[N],ansb[N];
int find(int x)
{
if (x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
void assign(int l,int r,int x)
{
l=find(l);
while (l<r)
{
if (c[l]==0) c[l]=x;
else
{
d[l]=x;
fa[l]=l+1;
}
l=find(l+1);
}
}
set<int>s[N],p[N];
int mx[N*4],mn[N*4];
inline void push_up(int u)
{
mx[u]=max(mx[u*2],mx[u*2+1]);
mn[u]=min(mn[u*2],mn[u*2+1]);
}
void update(int u,int l,int r,int x)
{
if (l==r)
{
if (SZ(s[l])==0) mx[u]=0;
else mx[u]=*s[l].rbegin();
if (SZ(p[l])==0) mn[u]=1e9;
else mn[u]=*p[l].begin();
return;
}
int mid=(l+r)>>1;
if (x<=mid) update(u*2,l,mid,x);
else update(u*2+1,mid+1,r,x);
push_up(u);
}
int querymax(int u,int L,int R,int l,int r)
{
if (l>r) return 0;
if (l<=L&&R<=r) return mx[u];
int mid=(L+R)>>1;
int res=0;
if (l<=mid) chkmax(res,querymax(u*2,L,mid,l,r));
if (mid<r) chkmax(res,querymax(u*2+1,mid+1,R,l,r));
return res;
}
int querymin(int u,int L,int R,int l,int r)
{
if (l>r) return 1e9;
if (l<=L&&R<=r) return mn[u];
int mid=(L+R)>>1;
int res=1e9;
if (l<=mid) chkmin(res,querymin(u*2,L,mid,l,r));
if (mid<r) chkmin(res,querymin(u*2+1,mid+1,R,l,r));
return res;
}
void add(int x,int y)
{
//cerr << "add " << x << " " << y << endl;
s[x].insert(y),p[y].insert(x);
update(1,1,n-1,x);
update(1,1,n-1,y);
}
void del(int x,int y)
{
//cerr << "del " << x << " " << y << endl;
s[x].erase(y),p[y].erase(x);
update(1,1,n-1,x);
update(1,1,n-1,y);
}
int rt[N*2],cnt;
ll res;
namespace treap
{
int tot=0;
struct node
{
int val,rnk,l,r,size,mx,mn,fa;
}tree[N];
inline void update(int u)
{
tree[u].fa=0;
if (tree[u].l) tree[tree[u].l].fa=u;
if (tree[u].r) tree[tree[u].r].fa=u;
tree[u].size=tree[tree[u].l].size+tree[tree[u].r].size+1;
tree[u].mx=max({tree[tree[u].l].mx,tree[tree[u].r].mx,tree[u].val});
tree[u].mn=min({tree[tree[u].l].mn,tree[tree[u].r].mn,tree[u].val});
}
int make_node(int val)
{
tree[++tot].size=1;
tree[tot].val=val;
tree[tot].mx=tree[tot].mn=val;
tree[tot].l=tree[tot].r=0;
tree[tot].rnk=rand();
return tot;
}
void split(int u,int val,int &x,int &y)
{
if (u==0)
{
x=y=0;
return;
}
if (tree[u].val<=val)
{
x=u;
split(tree[u].r,val,tree[u].r,y);
}
else
{
y=u;
split(tree[u].l,val,x,tree[u].l);
}
update(u);
}
void merge(int &u,int x,int y)
{
if (x==0||y==0)
{
u=x+y;
return;
}
if (tree[x].rnk<tree[y].rnk)
{
u=x;
merge(tree[u].r,tree[x].r,y);
}
else
{
u=y;
merge(tree[u].l,x,tree[y].l);
}
update(u);
}
void insert(int &u,int val)
{
int x=0,y=0;
split(u,val,x,y);
merge(x,x,make_node(val));
merge(u,x,y);
}
int ask(int u)
{
if (tree[u].fa) return ask(tree[u].fa);
return u;
}
void init()
{
tree[0].mn=1e9;
rep(i,1,n-1) insert(rt[1],i);
rep(i,1,n-2) add(i,i+1);
res=1ll*(n-1)*(n-2)/2;
}
void split(int u,int l,int r)
{
//cerr << u << " " << l << " " << r << endl;
if (tree[u].mn<l&&tree[u].mx>r)
{
res-=1ll*(tree[u].size-1)*tree[u].size/2;
int x,y,z;
split(u,l-1,x,y);
split(y,r,y,z);
del(tree[x].mx,tree[y].mn);
del(tree[y].mx,tree[z].mn);
add(tree[x].mx,tree[z].mn);
merge(x,x,z);
res+=1ll*(tree[x].size-1)*tree[x].size/2;
res+=1ll*(tree[y].size-1)*tree[y].size/2;
}
else if (tree[u].mn<l&&tree[u].mx>=l)
{
res-=1ll*(tree[u].size-1)*tree[u].size/2;
int x,y;
split(u,l-1,x,y);
del(tree[x].mx,tree[y].mn);
res+=1ll*(tree[x].size-1)*tree[x].size/2;
res+=1ll*(tree[y].size-1)*tree[y].size/2;
}
else if (tree[u].mn<=r&&tree[u].mx>r)
{
//cerr << u << " " << l << " " << r << endl;
res-=1ll*(tree[u].size-1)*tree[u].size/2;
int x,y;
split(u,r,x,y);
del(tree[x].mx,tree[y].mn);
res+=1ll*(tree[x].size-1)*tree[x].size/2;
res+=1ll*(tree[y].size-1)*tree[y].size/2;
}
}
}
void update(int l,int r)
{
while (1)
{
int w=querymin(1,1,n-1,l,r);
if (w>=l) break;
int id=treap::ask(w);
treap::split(id,l,r);
}
while (1)
{
int w=querymax(1,1,n-1,l,r);
if (w<=r) break;
int id=treap::ask(w);
treap::split(id,l,r);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m;
rep(i,1,m)
{
cin >> a[i] >> b[i];
if (a[i]>b[i]) swap(a[i],b[i]);
}
rep(i,1,n+1) fa[i]=i;
rep(i,1,m) assign(a[i],b[i],i);
rep(i,1,n-1)
{
int v=m;
if (c[i]) v=c[i]-1;
num[v]++;
}
per(i,m,1) num[i]+=num[i+1];
rep(i,1,m) ans[i]+=1ll*num[i]*(n-1+i-num[i]);
rep(i,1,n-1)
{
if (c[i]==0) continue;
int l=c[i],r=m;
if (d[i]) r=d[i]-1;
ansb[l]++,ansb[r+1]--;
}
treap::init();
rep(i,1,m)
{
int l=a[i],r=b[i]-1;
if (l<=r) update(l,r);
ans[i]+=res;
}
rep(i,1,m) ansb[i]+=ansb[i-1];
rep(i,1,m) cout << ans[i]+ansb[i] << endl;
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 19ms
memory: 38824kb
input:
98 97 79 41 86 87 97 36 66 83 79 79 46 4 67 50 75 8 78 67 70 35 74 21 7 7 8 44 32 63 33 12 75 86 81 ...
output:
4753 4773 3890 3525 3561 1315 1243 1075 1076 1038 856 860 858 790 756 727 727 591 570 528 513 511 50...
result:
ok 97 lines
Test #2:
score: 0
Accepted
time: 8ms
memory: 38824kb
input:
96 92 95 89 13 78 44 41 25 94 21 55 41 11 88 61 29 46 63 35 35 67 78 95 77 57 78 30 90 11 52 39 18 8...
output:
4560 4194 4029 2579 1935 1590 1479 1424 1369 1336 1345 1338 1344 1349 1334 1306 515 504 482 477 466 ...
result:
ok 92 lines
Test #3:
score: 0
Accepted
time: 3ms
memory: 38824kb
input:
97 94 12 50 38 3 22 34 15 17 96 80 76 97 32 55 10 80 42 97 14 90 92 78 75 61 77 27 57 58 35 73 3 67 ...
output:
4656 4025 3906 3931 3164 2841 2435 749 697 627 617 521 497 493 463 422 406 403 402 394 385 387 375 3...
result:
ok 94 lines
Test #4:
score: 0
Accepted
time: 4ms
memory: 38824kb
input:
98 93 86 5 46 60 63 60 35 91 6 32 56 23 53 98 37 61 57 20 17 10 54 50 95 43 13 59 9 44 50 6 14 94 36...
output:
4753 3817 3638 2180 2061 1620 961 945 904 859 849 819 797 796 800 798 798 800 803 725 659 658 647 64...
result:
ok 93 lines
Subtask #2:
score: 20
Accepted
Test #5:
score: 20
Accepted
time: 7ms
memory: 38988kb
input:
915 957 872 180 305 728 738 334 592 686 693 273 386 729 305 685 548 313 154 694 218 139 648 442 522 ...
output:
418155 304167 290363 262385 246036 235173 233860 226786 196219 183394 175567 84373 83548 83162 75163...
result:
ok 957 lines
Test #6:
score: 0
Accepted
time: 8ms
memory: 38996kb
input:
953 979 771 375 720 102 673 742 401 826 601 320 724 933 721 721 359 314 813 837 300 898 442 426 777 ...
output:
453628 327863 313480 269702 243485 165583 165704 163923 162431 157570 154747 154534 124990 123931 11...
result:
ok 979 lines
Test #7:
score: 0
Accepted
time: 8ms
memory: 38996kb
input:
995 968 182 826 584 317 839 935 134 411 482 15 120 209 700 758 699 341 547 764 376 683 944 467 875 1...
output:
494515 393939 332273 247796 146464 142151 131508 121865 119106 116384 95299 92898 89771 87863 85511 ...
result:
ok 968 lines
Test #8:
score: 0
Accepted
time: 7ms
memory: 38984kb
input:
933 957 648 569 18 189 54 202 564 82 265 707 445 682 337 722 804 32 388 54 86 62 264 925 346 408 869...
output:
434778 421951 414362 315730 256566 234375 216392 157910 155149 154723 58409 57316 52999 52083 49808 ...
result:
ok 957 lines
Subtask #3:
score: 30
Accepted
Test #9:
score: 30
Accepted
time: 31ms
memory: 40684kb
input:
9662 9451 8768 7672 6510 8860 3239 6506 7737 7050 2801 4519 187 4 4043 9656 7126 8513 3378 4712 8355...
output:
46672291 45304122 37627449 37170752 32168158 31062784 25685684 25448931 25012877 24912730 24768825 6...
result:
ok 9451 lines
Test #10:
score: 0
Accepted
time: 29ms
memory: 40660kb
input:
9635 9031 1630 313 8737 642 8969 7164 9186 5161 6633 9060 5783 9346 8823 6894 6041 3962 2935 1073 62...
output:
46411795 36727046 26066869 17114016 16321588 14373043 14290618 11341216 9760210 9144728 8747503 8567...
result:
ok 9031 lines
Test #11:
score: 0
Accepted
time: 21ms
memory: 40708kb
input:
9702 9632 371 6694 2944 7186 6777 82 7704 1063 7337 7758 1323 5979 3383 1948 7368 3680 4001 3252 686...
output:
47059551 34299021 32295191 27314301 26848792 24259332 22499213 21807868 21134607 21052016 20801427 7...
result:
ok 9632 lines
Test #12:
score: 0
Accepted
time: 21ms
memory: 40652kb
input:
9590 9125 6576 8178 6726 9530 5852 3923 4933 7672 7734 5967 9029 7563 7180 7594 1434 42 9286 174 539...
output:
45979255 43600734 37905245 32964552 32870362 32355909 32183591 24372885 6703382 4942343 4930581 4832...
result:
ok 9125 lines
Subtask #4:
score: 40
Accepted
Test #13:
score: 40
Accepted
time: 610ms
memory: 75396kb
input:
194696 196016 137552 1712 33642 77572 141678 113389 157609 167759 111356 137123 156703 115322 135428...
output:
18953168860 14915577485 12718157324 11297526703 11153790801 8856121896 8820053827 8576417365 8478090...
result:
ok 196016 lines
Test #14:
score: 0
Accepted
time: 614ms
memory: 74272kb
input:
182501 197823 137988 54299 179150 95016 43119 172611 28549 16806 177963 65347 119364 115581 69254 88...
output:
16653216250 11458733385 9836459357 8239058564 7904952002 7756734741 7559732679 6111134561 5816518550...
result:
ok 197823 lines
Test #15:
score: 0
Accepted
time: 679ms
memory: 76008kb
input:
198186 190154 172127 43896 162240 62021 115802 113402 39452 65737 150178 24390 45053 79 154994 3553 ...
output:
19638746205 16831381312 16596685666 15497976976 12510048402 8898696050 8791416876 8691948483 7088877...
result:
ok 190154 lines
Test #16:
score: 0
Accepted
time: 602ms
memory: 75924kb
input:
196171 186430 4978 72789 18656 48869 158809 88737 44806 34043 67984 170817 73407 131407 59959 179612...
output:
19241432535 18105582307 13353918130 13144636067 9132280140 7769495221 6112134926 5898029822 47718123...
result:
ok 186430 lines
Extra Test:
score: 0
Extra Test Passed