QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#514953 | #5425. Proposition Composition | szzjz | RE | 0ms | 5784kb | C++14 | 4.1kb | 2024-08-11 13:46:51 | 2024-08-11 13:46:51 |
Judging History
answer
#include<bits/stdc++.h>
#define fi first
#define se second
#define pub emplace_back
#define mk make_pair
using namespace std;
typedef long long ll;
const int N=2.5e5+10;
const int V=1e9;
mt19937 rd(time(0));
int n;
vector<int> v1,v2;
unordered_set<int> s;
ll res2;
struct CT{
set<int> s;
vector<int> v;
int res;
void clean(){
res=0;
s.clear();
}
void del(int l,int r){
auto it=s.lower_bound(l);
v.clear();
while(it!=s.end()&&*it<=r){
res--;
auto itt=it;it++;
v.pub(*itt);
s.erase(itt);
}
}
void add(const vector<int> &dt){
for(const int &x:dt) s.insert(x),res++;
}
}s0,s1;
struct FHQ{
struct T{
int ls,rs;
int rnd,sz,fa;
}t[N];int ts;//t[0].sz must be 0
inline void psu(int p){
if(!p) return;
t[p].fa=0;
t[p].sz=t[t[p].ls].sz+t[t[p].rs].sz+1;
t[t[p].ls].fa=t[t[p].rs].fa=p;
}
void split(int p,int key,int &x,int &y){
if(!p) return x=y=0,void();
if(p<=key){
x=p;
split(t[p].rs,key,t[x].rs,y);
psu(x);
}else{
y=p;
split(t[p].ls,key,x,t[y].ls);
psu(y);
}
}
int merge(int x,int y){
if(!x||!y) return psu(x+y),x+y;
if(t[x].rnd>t[y].rnd){
t[x].rs=merge(t[x].rs,y);
return psu(x),x;
}else{
t[y].ls=merge(x,t[y].ls);
return psu(y),y;
}
}
int New(){
t[++ts]={0,0,(int)rd(),1,0};
return ts;
}
int getfa(int x){
while(t[x].fa) x=t[x].fa;
return x;
}
void dt(int rt,int op){//rt must be [1,n]
res2+=op*(ll)t[rt].sz*(t[rt].sz-1)/2;
}
void ch(int rt,int l,int r){
dt(rt,-1);
int x,y,z;
split(rt,l-1,x,y);
split(y,r,y,z);
x=merge(x,z);
dt(x,1);dt(y,1);//here it's sure that x>0 and y>0
}
int getsz(int p){
int res=0;
while(t[p].fa){
int to=t[p].fa;
if(t[to].rs==p) res+=t[t[to].ls].sz+1;
p=to;
}
return res+1;
}
int get(int rt,int lim){
if(t[rt].sz<lim) return -1;
if(lim==0) return -1;
int p=rt;
while(t[p].ls||t[p].rs){
int ck=t[t[p].ls].sz+1;
if(lim>ck) lim-=ck,p=t[p].rs;
else if(lim==ck) return p;
else p=t[p].ls;
}
return p;
}
}t2;
struct SGT{
struct T{
int l,r;
int mi,mx;
}t[N*4];
void psu(int p){
t[p].mi=min(t[p*2].mi,t[p*2+1].mi);
t[p].mx=max(t[p*2].mx,t[p*2+1].mx);
}
void build(int p,int l,int r){
t[p].l=l,t[p].r=r;
if(l==r){
t[p].mi=l==1?1:l-1;
t[p].mx=l==n?n:l+1;
return;
}
int mid=l+r>>1;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
psu(p);
}
void solve(int p,int x,int y){
if(x<=t[p].mi&&t[p].mx<=y) return;
if(t[p].l==t[p].r){
int id=t[p].l;
s.insert(t2.getfa(id));
if(t[p].mi<x) v2.pub(t[p].mi),t[p].mi=id;
if(t[p].mx>y) v1.pub(t[p].mx),t[p].mx=id;
return;
}
int mid=t[p].l+t[p].r>>1;
if(x<=mid) solve(p*2,x,y);
if(y>mid) solve(p*2+1,x,y);
psu(p);
}
void ch1(int p,int x,int dt){
if(t[p].l==t[p].r) return t[p].mi=dt,void();
int mid=t[p].l+t[p].r>>1;
if(x<=mid) ch1(p*2,x,dt); else ch1(p*2+1,x,dt);
psu(p);
}
void ch2(int p,int x,int dt){
if(t[p].l==t[p].r) return t[p].mx=dt,void();
int mid=t[p].l+t[p].r>>1;
if(x<=mid) ch2(p*2,x,dt); else ch2(p*2+1,x,dt);
psu(p);
}
}t1;
void work(){
s0.clean(),s1.clean();
t2.ts=0;
int qs;
cin>>n>>qs;
n--;
t1.build(1,1,n);
for(int i=1,lst=0;i<=n;i++) lst=t2.merge(lst,t2.New());
for(int i=1;i<=n;i++) v1.pub(i);
s0.add(v1);
res2=(ll)n*(n-1)/2;
for(int i=1;i<=qs;i++){
int l,r;cin>>l>>r;
if(l>r) swap(l,r);
r--;
v1.clear(),v2.clear(),s.clear();
t1.solve(1,l,r);
for(int rt:s) t2.ch(rt,l,r);
for(int x:v1){
int rk=t2.getsz(x);
int res=t2.get(t2.getfa(x),rk-1);
if(res==-1) res=x;
t1.ch1(1,x,res);
}
for(int x:v2){
int rk=t2.getsz(x);
int res=t2.get(t2.getfa(x),rk+1);
if(res==-1) res=x;
t1.ch2(1,x,res);
}
s0.del(l,r);
s1.del(l,r);
s1.add(s0.v);
//cerr<<s0.res<<' '<<s1.res<<' '<<res2<<endl;
cout<<s1.res+res2+(ll)s0.res*(n+i-s0.res)<<endl;
}
}
int main(){
// auto file1=freopen("dd.in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int tt;cin>>tt;while(tt--) work();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5784kb
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: -100
Runtime Error
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 10 36 44 50 57 28 21 15 35 36 30 31 26 27 23 3 4 5 3 3 3 3 1 1 1 1 1 45 27 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 1 1 1 1 1 1 3 1 0 0 15 10 10