QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#865835 | #5425. Proposition Composition | 2147483647str | RE | 60ms | 28872kb | C++14 | 4.4kb | 2025-01-22 00:18:32 | 2025-01-22 00:18:32 |
Judging History
answer
#include<bits/stdc++.h>
#define l(p) t[p].ls
#define r(p) t[p].rs
#define v(p) t[p].val
using namespace std;
const int N=2.5e5+5,P=1e9+7,inf=0x3f3f3f3f;
using ull = unsigned long long;
int n,m;
ull ans[N];
//选出两条树边
struct Node{
int ls,rs;
ull val;
}t[N*80*2];
int rt[N],tot;
ull pw[N];
void change(int &p,int q,int l,int r,int k,ull v){
p=++tot;t[p]=t[q];v(p)+=v*pw[k-1];
if(l==r)return;
int mid=l+r>>1;
if(k<=mid)change(l(p),l(q),l,mid,k,v);
else change(r(p),r(q),mid+1,r,k,v);
}
int query(int p,int q,int l,int r){
if(l==r)return l;
int mid=l+r>>1;
if(v(l(p))==v(l(q)))return query(r(p),r(q),mid+1,r);
return query(l(p),l(q),l,mid);
}
int LCP(int x,int y){
int ans=query(rt[x],rt[y],0,m+1);
// printf("%d %d %d\n",x,y,ans);
return ans-1;
}
struct OP{
int l,r;
}q[N];
bool cmp(int x,int y){
int lcp=LCP(x,y);
if(lcp==m+1)return 0;
return q[lcp+1].l<=y&&y<=q[lcp+1].r;
}
//选出一条树边和一条非树边
int minn[N<<2][2],cnt[N<<2][2],tag[N<<2];
void pushup(int p){
int mn=min(minn[p<<1][0],minn[p<<1|1][0]),se=inf;
for(int i=0;i<2;i++)for(int j=0;j<2;j++)
if(minn[p<<1|i][j]!=mn)se=min(se,minn[p<<1|i][j]);
minn[p][0]=mn;minn[p][1]=se;
cnt[p][0]=cnt[p][1]=0;
for(int i=0;i<2;i++)for(int j=0;j<2;j++){
if(minn[p<<1|i][j]==mn)cnt[p][0]+=cnt[p<<1|i][j];
if(minn[p<<1|i][j]==se)cnt[p][1]+=cnt[p<<1|i][j];
}
}
void upd(int p,int v){
tag[p]+=v;
minn[p][0]+=v;
minn[p][1]+=v;
}
void pushdown(int p){
if(tag[p]){
upd(p<<1,tag[p]);
upd(p<<1|1,tag[p]);
tag[p]=0;
}
}
void build(int p,int l,int r){
tag[p]=0;
if(l==r){
minn[p][0]=0;minn[p][1]=inf;
cnt[p][0]=1;cnt[p][1]=0;
return;
}int mid=l+r>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
pushup(p);
}
void change(int p,int l,int r,int l1,int r1){
if(l1>r1)return;
if(l1<=l&&r<=r1){
upd(p,1);
return;
}int mid=l+r>>1;pushdown(p);
if(l1<=mid)change(p<<1,l,mid,l1,r1);
if(r1>mid)change(p<<1|1,mid+1,r,l1,r1);
pushup(p);
}
ull query(int i){//i为非树边的数量
ull cnt0=0,cnt1=0;
if(minn[1][0]==0)cnt0+=cnt[1][0];
if(minn[1][0]==1)cnt1+=cnt[1][0];
if(minn[1][1]==1)cnt1+=cnt[1][1];
// assert(cnt0+cnt1<=n);
return cnt0*(n-cnt0+i)+cnt1;
}
int id[N];
struct DSU{
int fa[N],siz[N];
ull ans;
void init(int n){
for(int i=1;i<=n;i++)fa[i]=i,siz[i]=1;
ans=0;
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x,int y){
x=find(x);y=find(y);
if(x==y)return;
if(siz[x]<siz[y])swap(x,y);
ans-=1ull*siz[x]*(siz[x]-1)/2;ans-=1ull*siz[y]*(siz[y]-1)/2;
fa[y]=x;siz[x]+=siz[y];
ans+=1ull*siz[x]*(siz[x]-1)/2;
}
}dsu;
struct op{
int pos,val;
}p[N];
bool operator<(op x,op y){
return x.val>y.val;
}
typedef pair<int,int> pii;
vector<pii>vec[N];
int solve(){
// memset(t,0,sizeof(t));
scanf("%d%d",&n,&m);tot=0;n--;
for(int i=1;i<=n;i++)id[i]=i,vec[i].clear();
for(int i=1;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
if(q[i].l>q[i].r)swap(q[i].l,q[i].r);
q[i].r--;
vec[q[i].l].push_back({i,1});
vec[q[i].r+1].push_back({i,-1});
}
if(!n){
for(int i=1;i<=m;i++)printf("0\n");
return 0;
}
for(int i=1;i<=n;i++){
rt[i]=rt[i-1];
for(auto&[x,y]:vec[i])change(rt[i],rt[i],0,m+1,x,y);
}
build(1,1,n);dsu.init(n);
sort(id+1,id+n+1,cmp);
// printf("ID:");for(int i=1;i<=n;i++)printf(" %d",id[i]);puts("");
for(int i=1;i<=m;i++){
change(1,1,n,q[i].l,q[i].r);
ans[i]=query(i);
}
for(int i=1;i<n;i++)p[i].pos=i,p[i].val=LCP(id[i],id[i+1]);
sort(p+1,p+n);
for(int i=m,cur=1;i>=1;i--){
while(cur<n&&p[cur].val==i){
dsu.merge(p[cur].pos,p[cur].pos+1);
cur++;
// printf("DSU:%d\n",i);
}
ans[i]+=dsu.ans;
}
for(int i=1;i<=m;i++)printf("%llu\n",ans[i]);
// if(clock()>(double)CLOCKS_PER_SEC*0.8)exit(0);
return 0;
}
int main(){
pw[0]=1;
for(int i=1;i<=N-5;i++)pw[i]=pw[i-1]*P;
int t;scanf("%d",&t);
while(t--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 27268kb
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: 60ms
memory: 28872kb
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: -100
Runtime Error
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 ...