QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#577604 | #5425. Proposition Composition | gyydp123_LIM | TL | 50ms | 10040kb | C++14 | 3.6kb | 2024-09-20 13:19:28 | 2024-09-20 13:19:29 |
Judging History
answer
#include<bits/stdc++.h>
#define For(i,j,k) for(int i=(j);i<=(k);++i)
#define ForDown(i,j,k) for(int i=(j);i>=(k);--i)
#define Debug(fmt, args...) fprintf(stderr,"In function %s on line #%d: " fmt, __func__, __LINE__, ##args),fflush(stderr)
#define debug(fmt, args...) fprintf(stderr,fmt,##args),fflush(stderr)
#define within :
#define LJY main
using namespace std;
typedef long long ll;
const int N=2.5e5+5;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
inline int read(){
char ch=getchar();int x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')
x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return x*f;
}
int n,m;
int pre[N],suf[N],bel[N],siz[N],cnt;ll ans;
int fa[N],S;set<int>st;
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline ll C(ll x){return x*(x-1)/2;}
vector<int>v1,v2;
int Tl[N<<2],Tr[N<<2];
void build(int l=1,int r=n-1,int rt=1){
if(l==r){Tl[rt]=pre[l];Tr[rt]=suf[l];return;}
int mid=(l+r)>>1;
build(l,mid,rt<<1);build(mid+1,r,rt<<1|1);
Tl[rt]=min(Tl[rt<<1],Tl[rt<<1|1]);
Tr[rt]=max(Tr[rt<<1],Tr[rt<<1|1]);
}
int debug_cnt;
void query(int L,int R,int l=1,int r=n-1,int rt=1){
if((Tl[rt]>=L&&Tr[rt]<=R)||l>R||r<L) return;
assert((++debug_cnt)<=1e8);
if(l==r){
if(Tl[rt]<L) v1.emplace_back(l),Tl[rt]=n;
if(Tr[rt]>R) v2.emplace_back(r),Tr[rt]=0;
return;
}int mid=(l+r)>>1;
query(L,R,l,mid,rt<<1);query(L,R,mid+1,r,rt<<1|1);
Tl[rt]=min(Tl[rt<<1],Tl[rt<<1|1]);
Tr[rt]=max(Tr[rt<<1],Tr[rt<<1|1]);
}
void update(int L,int l=1,int r=n-1,int rt=1){
assert((++debug_cnt)<=1e8);
if(l==r){Tl[rt]=pre[l];Tr[rt]=suf[l];return;}
int mid=(l+r)>>1;
if(L<=mid) update(L,l,mid,rt<<1);
else update(L,mid+1,r,rt<<1|1);
Tl[rt]=min(Tl[rt<<1],Tl[rt<<1|1]);
Tr[rt]=max(Tr[rt<<1],Tr[rt<<1|1]);
}
void split(int x){
int id=bel[x],y=suf[x];
suf[x]=0;pre[y]=n;update(x);update(y);
int p1=x,p2=y;while(p1<n&&p2) p1=pre[p1],p2=suf[p2];siz[++cnt]=0;
if(p1==n) for(int i=x;i<n;i=pre[i]) siz[cnt]++,bel[i]=cnt;
else for(int i=y;i;i=suf[i]) siz[cnt]++,bel[i]=cnt;
ans-=C(siz[id]);siz[id]-=siz[cnt];ans+=C(siz[id])+C(siz[cnt]);
}
void split(int x,int y){
int id=bel[x],a=pre[x],b=suf[y];
suf[a]=b;pre[b]=a;pre[x]=n;suf[y]=0;
update(a);update(b);
int p1=a,p2=x,p3=b;
while(p1<n&&p2) p1=pre[p1],p2=suf[p2];
while(p3&&p2) p3=suf[p3],p2=suf[p2];
siz[++cnt]=0;
if(p2) for(int i=x;i;i=suf[i]) siz[cnt]++,bel[i]=cnt;
else{
for(int i=a;i<n;i=pre[i]) siz[cnt]++,bel[i]=cnt;
for(int i=b;i;i=suf[i]) siz[cnt]++,bel[i]=cnt;
}ans-=C(siz[id]);siz[id]-=siz[cnt];ans+=C(siz[id])+C(siz[cnt]);
}
void ljy(){
n=read();m=read();
if(n==1){For(i,1,m) read(),read(),puts("0");return;}
S=n-1;siz[1]=n-1;ans=C(siz[1]);
For(i,1,n-1) pre[i]=i-1,suf[i]=i+1,fa[i]=i,bel[i]=1;fa[n]=n;
st.clear();cnt=1;pre[1]=n;suf[n-1]=0;build();
For(_,1,m){
int x=read(),y=read();if(x>y) swap(x,y);
for(auto it=st.lower_bound(x);it!=st.end()&&*it<y;) it=st.erase(it);
int now=find(x);while(now<y) st.insert(now),S--,fa[now]=now+1,now=find(now);
v1.clear();v2.clear();query(x,y-1);int s1=v1.size(),s2=v2.size();
sort(v1.begin(),v1.end(),[&](int x,int y){return bel[x]<bel[y];});
sort(v2.begin(),v2.end(),[&](int x,int y){return bel[x]<bel[y];});
for(int i=0,j=0;i<s1||j<s2;){
if(i<s1&&j<s2&&bel[v1[i]]==bel[v2[j]]) split(v1[i++],v2[j++]);
else if(i<s1&&(j==s2||bel[v1[i]]<bel[v2[j]])) split(pre[v1[i++]]);
else split(v2[j++]);
}printf("%lld\n",ans+(ll)S*(n+_-1-S)+st.size());
}
}
signed LJY(){int T=read();while(T--) ljy();}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 9900kb
input:
3 4 3 2 4 4 2 3 3 7 3 3 4 1 2 1 7 6 4 1 3 4 6 2 5 3 4
output:
6 5 6 21 24 10 15 12 3 2
result:
ok 10 numbers
Test #2:
score: 0
Accepted
time: 30ms
memory: 9900kb
input:
45540 10 9 10 1 1 10 10 1 1 10 1 10 10 1 1 10 3 3 10 1 10 4 1 2 1 10 3 4 1 10 7 6 7 1 5 6 1 7 6 6 7 1 6 7 9 7 3 3 7 7 5 4 1 1 9 1 9 1 6 5 8 7 1 8 4 4 5 6 1 1 1 8 6 6 4 5 3 3 3 2 3 1 3 3 3 9 3 1 3 3 2 2 3 3 3 1 2 2 1 1 2 3 3 1 10 1 2 1 7 1 1 7 3 8 1 3 1 3 3 3 1 3 2 2 1 3 1 3 3 3 3 6 3 1 1 3 1 3 1 3 1...
output:
45 36 36 36 36 36 36 36 36 45 36 28 21 21 15 10 10 10 6 36 44 50 57 28 21 15 28 28 21 21 15 15 10 3 1 1 3 3 3 3 1 1 1 0 0 45 21 3 1 1 1 1 1 1 1 3 1 1 1 1 1 45 36 36 36 36 36 36 36 3 3 1 0 0 0 0 0 0 3 1 0 0 15 10 10 0 0 0 0 0 0 0 0 28 34 21 6 6 6 6 1 0 0 21 15 15 0 0 0 0 0 0 0 45 53 0 0 0 0 0 0 0 0 1...
result:
ok 249586 numbers
Test #3:
score: 0
Accepted
time: 50ms
memory: 10040kb
input:
2507 86 4 41 41 36 36 31 30 86 1 110 22 1 110 110 1 11 11 110 1 110 1 110 1 1 110 107 106 72 72 106 106 74 74 1 110 110 1 58 58 110 1 110 1 1 110 101 100 110 1 100 100 110 1 8 7 114 180 114 1 114 1 114 1 1 114 1 114 114 1 37 38 49 48 105 106 1 114 90 90 1 114 9 9 114 1 67 68 20 20 114 1 1 114 54 55 ...
output:
3655 3740 3823 3570 5995 5886 5886 5886 5886 5886 5886 5778 5778 5778 5778 5778 5778 5778 5778 5778 5778 5671 5671 5671 5671 5565 6441 6328 6328 6328 6328 6328 6216 6105 5995 5995 5995 5995 5995 5995 5886 5886 5886 5886 5778 5671 5671 5565 5565 5460 5460 5460 5460 5460 5356 5253 5253 5253 5151 5151 ...
result:
ok 249877 numbers
Test #4:
score: -100
Time Limit Exceeded
input:
3 82425 27858 30801 30802 1 82425 73850 73850 1 82425 65949 65949 82425 1 76026 76025 61936 61936 82425 1 82425 1 82425 1 6504 6504 82425 1 25155 25156 79743 79743 1 82425 69406 69406 29247 29247 18351 18351 23171 23170 29704 29703 82425 1 1 82425 82425 1 74918 74917 22395 22394 893 894 82425 1 391 ...
output:
3396899100 3396816676 3396816676 3396734253 3396734253 3396734253 3396651831 3396651831 3396651831 3396651831 3396651831 3396651831 3396651831 3396569410 3396569410 3396569410 3396569410 3396569410 3396569410 3396486990 3396404571 3396404571 3396404571 3396404571 3396322153 3396239736 3396157320 339...