QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#546847 | #2214. Link Cut Digraph | wrkwrk | AC ✓ | 1379ms | 306572kb | C++20 | 9.6kb | 2024-09-04 14:38:30 | 2024-09-04 14:38:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
bool st;
namespace _wrk{;
template<int mod>
struct modint{
int num;
const static __uint128_t brt=((__uint128_t)(1)<<(64))/mod;
modint(){
num=0;
}
modint(int x){
num=x%mod;
}
modint(long long x){
num=x%mod;
}
modint<mod>operator=(int x){
num=x%mod;
return (*this);
}
modint<mod>operator=(long long x){
num=x%mod;
return (*this);
}
modint<mod>operator=(modint<mod>x){
num=x.num;
return (*this);
}
modint<mod> operator+(modint<mod> c)const{
long long x=num+c.num;
return x>=mod?x-mod:x;
}
modint<mod> operator-(modint<mod> c)const{
long long x=num-c.num;
return x<0?x+mod:x;
}
modint<mod>operator*(modint<mod>c)const{
long long x=(long long)num*c.num;
x=x-mod*(brt*x>>64);
while(x>=mod)x-=mod;
return x;
}
modint<mod>fpof(int x)const{
if(x<0)return inv().fpof(-x);
if(x==0)return 1;
auto c=((*this)*(*this)).fpof(x/2);
if(x&1)return c*(*this);
else return c;
}
struct modint_pow{
int pf;
modint_pow(int x){
pf=x;
}
modint_pow(modint<mod> x){
pf=x.num;
}
modint_pow operator+(modint_pow x){
return pf+x.pf;
}
};
modint_pow operator*(){
return modint_pow(num);
}
modint<mod> operator*(modint_pow x){
return (*this).fpof(x.pf);
}
modint<mod>inv()const{
return fpof(mod-2);
}
modint<mod>operator/(modint<mod>c){
return (*this)*c.inv();
}
operator int(){
return num;
}
modint<mod>operator+=(modint<mod> c){
return (*this)=(*this)+c;
}
modint<mod>operator-=(modint<mod> c){
return (*this)=(*this)-c;
}
modint<mod>operator*=(modint<mod> c){
return (*this)=(*this)*c;
}
modint<mod>operator/=(modint<mod> c){
return (*this)=(*this)/c;
}
modint<mod>operator-(){
return mod-num;
}
friend ostream& operator<<(ostream &w,modint<mod>&&x){
w<<x.num;
return w;
}
friend istream& operator>>(istream &w,modint<mod>&x){
w>>x.num;
x.num%=mod;
return w;
}
bool operator==(modint<mod>x){
return num==x.num;
}
};
template<int mod,class type>
modint<mod>operator+(type a,modint<mod>b){
return modint<mod>(a)+b;
}
template<int mod,class type>
modint<mod>operator-(type a,modint<mod>b){
return modint<mod>(a)-b;
}
template<int mod,class type>
modint<mod>operator*(type a,modint<mod>b){
return modint<mod>(a)*b;
}
template<int mod,class type>
modint<mod>operator/(type a,modint<mod>b){
return modint<mod>(a)/b;
}
#define int long long
template<class type,int N>
struct matrix{
type a[N+2][N+2];
int n;
type* operator[](int pos){
return a[pos];
}
matrix<type,N> operator*(matrix<type,N>b){
matrix<type,N>ans;
ans.n=n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int k=1;k<=n;k++){
ans[i][j]+=a[i][k]*b[k][j];
}
}
}
return ans;
}
};
template<class type>
type fp(type a,int b){
if(b==0)return type();
if(b==1)return a;
type w=fp(a*a,b/2);
if(b%2)return w*a;
return w;
}
template<class type,int N>
struct backup_array{
type name[N+5];
vector<vector<pair<int,int>>>cc;
void new_array(){
cc.push_back(vector<pair<int,int>>());
}
backup_array(){
cc.resize(1);
}
struct x{
int id;
type* name;
vector<vector<pair<int,int>>> &cc;
operator type(){
return name[id];
}
type operator=(type w){
cc.back().push_back({id,name[id]});
name[id]=w;
return w;
}
};
x operator[](int pos){
return {pos,name,cc};
}
void backup(){
reverse(cc.back().begin(),cc.back().end());
for(auto &x:cc.back()){
name[x.first]=x.second;
}
cc.pop_back();
}
};
template<class type,int N>
struct Math{
type jc[N+5],inv[N+5],invjc[N+5];
Math(){
jc[0]=jc[1]=inv[1]=invjc[1]=invjc[0]=1;
for(int i=2;i<=N;i++){
jc[i]=jc[i-1]*type(i);
}
invjc[N]=type(1)/jc[N];
for(int i=N-1;i>=2;i--){
invjc[i]=invjc[i+1]*type(i+1);
}
for(int i=1;i<=N;i++){
inv[i]=invjc[i]*jc[i-1];
}
}
type binom(int a,int b){
return jc[a]*invjc[b]*invjc[a-b];
}
type catalan(int n){
return binom(2*n,n)/type(n+1);
}
};
#ifdef use_seg_tree
template<class type,class laz_type,int len>
struct segment_tree{
type a[len<<2];
laz_type laz[len<<2];
void pushup(int now){
PUSHUP_VALUE
}
void pushdown(int now,int l,int r){
PUSHDOWN_VALUE
}
void build(int now,int l,int r){
if(l+1==r){
FIRST_VALUE
return;
}
LAZ_CLEAR
int mid=(l+r)>>1;
build(now<<1,l,mid);
build(now<<1|1,mid,r);
pushup(now);
}
void do1(int now,int l,int r,int L,int R,...){
if(l+1!=r)pushdown(now,l,r);
if(R<=l||r<=L)return;
if(L<=l&&r<=R){
FULL_BLOCK_VALUE
return;
}
int mid=(l+r)>>1;
do1(now<<1,l,mid,L,R,...);
do1(now<<1|1,mid,r,L,R,...);
pushup(now);
}
void do1_one(int now,int l,int r,int p,...){
if(l+1!=r)pushdown(now,l,r);
if(l+1==r){
DO1_VALUE
return;
}
int mid=(l+r)>>1;
if(p<mid)do1_one(now<<1,l,mid,p,...);
else do1_one(now<<1|1,mid,r,p,...);
pushup(now);
}
type qu1(int now,int l,int r,int L,int R){
if(l+1!=r)pushdown(now,l,r);
if(R<=l||r<=L)return BASE_VALUE
if(L<=l&&r<=R)return a[now];
int mid=(l+r)>>1;
return RETURN_VALVE qu1(now<<1,l,mid,L,R)+qu1(now<<1|1,mid,r,L,R);
}
type qu1_one(int now,int l,int r,int p){
if(l+1!=r)pushdown(now,l,r);
if(l+1==r)return a[now];
int mid=(l+r)>>1;
if(p<mid)return qu1_one(now<<1,l,mid,p);
else return qu1_one(now<<1|1,mid,r,p);
}
};
#endif
//#define mod 998244353
//#define mint modint<mod>
//Math<mint,1000006>math;
set<pair<int,int>>gg[21][100005],*g;
//#define g gg[0]
vector<int>wp[100005];
int cnt=1,beinbl;
int block[100005],f1[100005],f2[100005];
bool insta[100005];
int ans[300005];
int low[100005],dfsid[100005];
int sss[100005];
int isf[300005];
int ww=0;
int fin1(int now){
return f1[now]==now?now:f1[now]=fin1(f1[now]);
}
int fin2(int now){
return f2[now]==now?now:f2[now]=fin2(f2[now]);
}
stack<int>sta;
int st,en;
vector<int>ids;
void dfs(int now){
sta.push(now);
insta[now]=1;
low[now]=dfsid[now]=cnt++;
auto z=g[now].begin();
vector<set<pair<int,int>>::iterator>w;
while(z!=g[now].end()&&(*z).first<en){
auto &edge=*z;
int to=fin2(edge.second);
if(block[fin2(now)]==block[fin2(to)]&&fin2(now)!=fin2(to)){
ww++;
if(!dfsid[to]){
dfs(to);
low[now]=min(low[now],low[to]);
}else if(insta[to]){
low[now]=min(low[now],dfsid[to]);
}
}
if(fin2(now)==fin2(to)){
w.push_back(z);
}
z++;
}
for(auto x:w){
g[now].erase(x);
}
if(low[now]==dfsid[now]){
while(sta.top()!=now){
f1[fin1(sta.top())]=fin1(now);
insta[sta.top()]=0;
sta.pop();
}
sta.pop();
insta[now]=0;
}
}
void solve(int l,int r,vector<int>&nc,int dep=0){
ww=0;
g=gg[dep];
// cerr<<l<<' '<<r<<' '<<dep<<endl;
// for(auto p:nc){
// cout<<fin2(p)<<' '<<sss[fin2(p)]<<endl;;
// for(auto x:g[fin2(p)]){
// cout<<' '<<x.first<<'-'<<fin2(p)<<','<<x.second<<endl;
// }
// }
// cout<<endl;
if(l>=r)return ;
int mid=(l+r)>>1;
en=mid+1;
for(auto &x:nc){
x=fin2(x);
f1[fin2(x)]=fin2(x);
dfsid[fin2(x)]=0;
}
for(auto x:nc){
if(!dfsid[fin2(x)]){
dfs(fin2(x));
// cout<<mid<<' '<<fin2(x)<<' '<<g[fin2(x)].size()<<endl;
}
}
for(auto x:nc){
wp[fin2(x)].clear();
}
for(auto x:nc){
if(g[fin2(x)].size())wp[fin1(fin2(x))].push_back(fin2(x));
}
vector<int>bac,cc;
vector<vector<int>>zz;
vector<int>p;
for(auto x:nc){
if(wp[fin2(x)].size()){
bac.push_back(fin2(x));
zz.push_back(wp[fin2(x)]);
int a=0,b=0;
for(auto w:wp[fin2(x)]){
a+=sss[w];
b+=sss[w]*sss[w];
}
ans[mid]+=(a*a-b)/2;
p.push_back(a);
beinbl++;
for(auto y:wp[x]){
block[y]=beinbl;
}
for(auto y:wp[x]){
for(auto w:g[y]){
ww++;
if(w.first<mid&&block[fin2(y)]==block[fin2(w.second)]){
gg[dep+1][fin2(y)].insert({w.first,fin2(w.second)});
}
}
}
wp[fin2(x)].clear();
}
}
int wwp=ww;
solve(l,mid,nc,dep+1);
ww=wwp;
g=gg[dep];
for(auto w:nc){
gg[dep+1][fin2(w)].clear();
}
for(auto y:zz){
beinbl++;
for(auto z:y){
block[z]=beinbl;
}
}
beinbl++;
// cout<<mid<<endl;
for(int i=0;i<bac.size();i++){
int x=bac[i];
vector<int>y=zz[i];
sss[fin2(x)]=p[i];
// cout<<mid<<' '<<x<<' '<<p[i]<<endl;
for(auto w:y){
for(auto xx:g[w]){
// cout<<fin2(x)<<' '<<fin2(xx.second)<<' '<<block[fin2(x)]<<' '<<block[fin2(xx.second)]<<endl;
// ww++;
if(block[fin2(x)]!=block[fin2(xx.second)]){
gg[dep+1][fin2(x)].insert({xx.first,fin2(xx.second)});
// cout<<mid<<','<<fin2(x)<<' '<<fin2(xx.second)<<endl;
}
}
}
for(auto w:y){
ww++;
f2[fin2(w)]=fin2(x);
}
block[fin2(x)]=beinbl;
}
// cerr<<ww<<endl;
solve(mid+1 ,r,bac,dep+1);
g=gg[dep];
for(auto &x:bac){
gg[dep+1][x].clear();
}
}
void pre(int l,int r){
if(l>=r)return;
int mid=(l+r)>>1;
isf[mid]=l-1;
pre(l,mid);
pre(mid+1,r);
}
signed main(){
// freopen("graph.in","r",stdin);
// freopen("graph.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
gg[0][a].insert({i,b});
}
vector<int>w;
for(int i=1;i<=n;i++){
f1[i]=f2[i]=i;
sss[i]=1;
w.push_back(i);
}
pre(1,m+1);
solve(1,m+1,w);
for(int i=1;i<=m;i++){
ans[i]+=ans[isf[i]];
cout<<ans[i]<<'\n';
}
return 0;
}
}
bool en;
signed main(){
#ifdef LOCAL_WRK
cerr<<"[Static Memory : "<<fixed<<((&st)-(&en))/1048576.0<<"MB]"<<endl;
#endif
return _wrk::main();
}
Details
Test #1:
score: 100
Accepted
time: 1355ms
memory: 285332kb
Test #2:
score: 0
Accepted
time: 1310ms
memory: 286268kb
Test #3:
score: 0
Accepted
time: 1297ms
memory: 285608kb
Test #4:
score: 0
Accepted
time: 1141ms
memory: 298876kb
Test #5:
score: 0
Accepted
time: 1301ms
memory: 283376kb
Test #6:
score: 0
Accepted
time: 1321ms
memory: 286208kb
Test #7:
score: 0
Accepted
time: 1307ms
memory: 284836kb
Test #8:
score: 0
Accepted
time: 1351ms
memory: 290352kb
Test #9:
score: 0
Accepted
time: 988ms
memory: 306572kb
Test #10:
score: 0
Accepted
time: 1312ms
memory: 287944kb
Test #11:
score: 0
Accepted
time: 1372ms
memory: 285140kb
Test #12:
score: 0
Accepted
time: 1379ms
memory: 286728kb
Test #13:
score: 0
Accepted
time: 1041ms
memory: 305840kb
Test #14:
score: 0
Accepted
time: 1027ms
memory: 305804kb
Test #15:
score: 0
Accepted
time: 455ms
memory: 176288kb
Test #16:
score: 0
Accepted
time: 1201ms
memory: 241800kb
Test #17:
score: 0
Accepted
time: 1168ms
memory: 240856kb
Test #18:
score: 0
Accepted
time: 1170ms
memory: 238212kb
Test #19:
score: 0
Accepted
time: 1378ms
memory: 286236kb
Test #20:
score: 0
Accepted
time: 1199ms
memory: 291088kb
Test #21:
score: 0
Accepted
time: 1165ms
memory: 291068kb
Test #22:
score: 0
Accepted
time: 1173ms
memory: 290592kb
Test #23:
score: 0
Accepted
time: 1162ms
memory: 291396kb
Test #24:
score: 0
Accepted
time: 1125ms
memory: 291088kb
Test #25:
score: 0
Accepted
time: 1107ms
memory: 289656kb
Test #26:
score: 0
Accepted
time: 1122ms
memory: 287624kb
Test #27:
score: 0
Accepted
time: 1134ms
memory: 283820kb