QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#863818 | #3209. Differencia | peimuda | 100 ✓ | 15546ms | 52176kb | C++11 | 4.3kb | 2025-01-19 23:02:15 | 2025-01-19 23:02:15 |
Judging History
answer
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#define pr pair
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pll pr<ll,ll>
#define pii pr<int,int>
#define piii pr<int,pii>
using namespace std;
int A,B,C=~(1<<31),M=(1<<16)-1;
int rnd(int x)
{
A = ( 36969 + ( x >> 3 ) ) * ( A & M) + ( A >> 16 ) ;
B = ( 18000 + ( x >> 3 ) ) * ( B & M) + ( B >> 16 ) ;
return (C & ( ( A << 16 ) + B ) ) % 1000000000;
}
struct node
{
int l,r;
int ts;
int ls,rs;
} nd[3000006];
int t;
int bd(int l,int r)
{
int rt=t++;
node&x=nd[rt];
x.l=l;
x.r=r;
x.ts=0;
x.ls=0;
x.rs=0;
if(l==r-1) return rt;
x.ls=bd(l,l+r>>1);
x.rs=bd(l+r>>1,r);
return rt;
}
int add(int i,int l)
{
int rt=t++;
node&x=nd[rt];
x=nd[i];
x.ts++;
if(x.l==x.r-1) return rt;
int m=x.l+x.r>>1;
if(l<m) x.ls=add(x.ls,l);
else x.rs=add(x.rs,l);
return rt;
}
int ask(int i,int l,int r)
{
node&x=nd[i];
if(x.l>=l&&x.r<=r) return x.ts;
int rt=0;
int m=x.l+x.r>>1;
if(l<m) rt+=ask(x.ls,l,r);
if(r>m) rt+=ask(x.rs,l,r);
return rt;
}
int a[100005],b[100005];
int w[100005],z;
int s[100005];
void ad(int x,int v)
{
// cout<<"Adbit "<<x<<' '<<v<<endl;
while(x<=100000)
{
s[x]+=v;
x+=x&-x;
}
}
int ask(int x)
{
// cout<<"Asbit "<<x<<' ';
int r=0;
while(x)
{
r+=s[x];
x&=x-1;
}
// cout<<r<<endl;
return r;
}
int rot[100005];
int ffls(int l,int r,int x)
{
if(x<=0) return 0;
return ask(rot[r],0,x)-ask(rot[l],0,x);
}
int fls(int l,int r,int x)
{
int rt=ffls(l,r,x);
// cout<<"Fl "<<l<<' '<<r<<' '<<x<<" "<<rt<<endl;
return rt;
}
set<piii> sgs;
set<piii>::iterator ls,ds;
void otp()
{
cout<<"------\n";
for(piii i:sgs)
{
cout<<'['<<i.f<<"] val:"<<i.s.f<<" sum:"<<i.s.s<<' ';
}
cout<<"\n------"<<endl;
}
void sl()
{
int n,q;
cin>>n>>q>>A>>B;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++) cin>>b[i],w[i]=b[i];
sort(w,w+n);
z=unique(w,w+n)-w;
for(int i=0;i<n;i++) a[i]=upper_bound(w,w+z,a[i])-w;
for(int i=0;i<n;i++) b[i]=upper_bound(w,w+z,b[i])-w-1;
t=0;
rot[0]=bd(0,z);
for(int i=0;i<n;i++) rot[i+1]=add(rot[i],b[i]);
sgs.clear();
for(int i=0;i<n;i++) sgs.insert(mp(i,mp(a[i],a[i]>b[i])));
sgs.insert(mp(n,mp(-1,-1)));
sgs.insert(mp(-1,mp(-1,-1)));
for(int i=0;i<=n+2;i++) s[i]=0;
for(int i=0;i<n;i++) ad(i+1,a[i]>b[i]);
int l,r,x;
int las=0;
ll op=0;
// otp();
for(int ut=1;ut<=q;ut++)
{
l=rnd(las)%n+1;
r=rnd(las)%n+1;
x=rnd(las)+1;
if(l>r) swap(l,r);
// cout<<"Fd "<<l<<' '<<r<<' '<<x<<' '<<((l+r+x)%2==0?'?':'+')<<endl;
l--;
if((l+r+x)&1)
{
x=upper_bound(w,w+z,x)-w;
int g=ask(r)-ask(l);
ls=sgs.lower_bound(mp(l,mp(0,0)));
int fr=(*ls).f;
ls--;
x=(*ls).s.f;
g+=fls(l,fr,x);
ls=sgs.lower_bound(mp(r,mp(0,0)));
fr=(*ls).f;
ls--;
x=(*ls).s.f;
g-=fls(r,fr,x);
las=g;
op+=1ll*ut*g;
}
else
{
ls=sgs.lower_bound(mp(l,mp(0,0)));
x=upper_bound(w,w+z,x)-w;
if(ls==sgs.lower_bound(mp(r,mp(114514,0))))
{
ls--;
piii fd=*ls;
sgs.erase(ls);
ad(fd.f+1,-fd.s.s);
int g=fls(fd.f,l,fd.s.f);
ad(fd.f+1,g);
sgs.insert(mp(fd.f,mp(fd.s.f,g)));
g=fls(l,r,x);
ad(l+1,g);
sgs.insert(mp(l,mp(x,g)));
g=fls(fd.f,r,fd.s.f);
fd.f=r;
fd.s.s-=g;
ad(r+1,fd.s.s);
sgs.insert(fd);
// otp();
continue;
}
// cout<<"-\n";
int cts=0;
ls=sgs.lower_bound(mp(l,mp(0,0)));
int fr=(*ls).f;
ls--;
int fx=(*ls).s.f;
int g=fls(l,fr,fx);
piii fd=*ls;
sgs.erase(ls);
fd.s.s-=g;
sgs.insert(fd);
cts+=fls(l,fr,x);
if(fd.f>=0) ad(fd.f+1,-g);
ds=sgs.lower_bound(mp(r,mp(114514,0)));
while(1)
{
ls=sgs.lower_bound(mp(l,mp(0,0)));
ls++;
if(ls==ds) break;
fr=(*ls).f;
ls--;
fd=*ls;
sgs.erase(ls);
ad(fd.f+1,-fd.s.s);
cts+=fls(fd.f,fr,x);
}
ds--;
fd=*ds;
sgs.erase(ds);
cts+=fls(fd.f,r,x);
ad(fd.f+1,-fd.s.s);
fd.s.s-=fls(fd.f,r,fd.s.f);
fd.f=r;
sgs.insert(fd);
ad(fd.f+1,fd.s.s);
ad(l+1,cts);
sgs.insert(mp(l,mp(x,cts)));
}
// cout<<"Fd "<<op<<endl;
// otp();
}
cout<<op%1000000007<<'\n';
}
int main()
{
ios_base::sync_with_stdio(0);
int t;
cin>>t;
while(t--) sl();
return 0;
}/*1
5 10 1 2
5 4 3 2 1
1 2 3 4 5
274
5 10 1 2
5 4 3 2 1
1 2 3 4 5
5 10 3 4
5 4 4 2 1
1 2 3 4 5
5 10 5 6
5 4 5 2 1
1 2 2 4 5
1
10 10 60879 21802
6 5 2 1 7 9 7 2 5 5
2 4 7 6 2 2 8 7 7 9
*/
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 100
Accepted
time: 15546ms
memory: 52176kb
input:
274 5 10 1 2 5 4 3 2 1 1 2 3 4 5 5 10 3 4 5 4 4 2 1 1 2 3 4 5 5 10 5 6 5 4 5 2 1 1 2 2 4 5 10 10 60879 21802 6 5 2 1 7 9 7 2 5 5 2 4 7 6 2 2 8 7 7 9 10 10 41083 35398 9 6 10 8 8 6 10 3 3 9 1 10 5 8 1 10 7 8 4 8 10 10 1259 44124 1 10 2 5 1 7 7 4 1 4 5 4 5 10 1 5 1 2 5 1 10 10 55129 39158 6 2 9 9 7 6 ...
output:
81 88 87 78 130 25 104 106 114 131 133 112 155 175 172 106 150 97 76 74 134 78 131 53 38 86 90 6 151 27 59 34 28 99 164 99 129 63 1 51 150 116 74 109 85 142 63 267 59 58 87 88 94 107 94 108 99 36 168 139 35 46 73 118 67 109 111 67 202 55 85 162 105 87 75 32 30 31 104 148 93 93 161 151 124 32 125 64 ...
result:
ok 274 lines