QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#18894 | #1877. Matryoshka Dolls | RPChe_ | WA | 10ms | 26312kb | C++14 | 3.9kb | 2022-01-27 14:38:07 | 2022-05-06 03:05:59 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define endl putchar('\n')
const int N=500005;
typedef long long ll;
using namespace std;
int n,m,a[N],rv[N];
ll ans[N];
unordered_map<unsigned,int> berv;
struct tire {
#define int unsigned
int ch[1050][32],s[1050],cnt,c[1050];
int get(int x,int d) {
int msk=(1<<5)-1;
return (x>>(5*d))&msk;
}
int highbit(int x) { return 1<<(31-__builtin_clz(x)); }
int lowbit(int x) { return x&-x; }
void insert(int x) {
int t=get(x,1),p=get(x,0),&a=ch[0][t];
s[0]|=1<<t; if(!a) a=++cnt;
int &b=ch[a][p];
s[a]|=1<<p; if(!b) b=++cnt;
if(!c[b]) c[b]++,c[a]++,c[0]++;
}
void erase(int x) {
int t=get(x,1),p=get(x,0),a=ch[0][t];
if(!a) return;
int b=ch[a][p];
if(!b) return;
if(c[b]) {
c[b]--,c[a]--,c[0]--;
s[a]^=1<<p;
if(!c[a]) s[0]^=1<<t;
}
}
signed prev(int x) {
if(!c[0]) return -1;
int t=get(x,1),p=get(x,0),a=ch[0][t];
if(a&&c[a]) {
if(s[a]&((1<<p)-1)) return (t<<5)|berv[highbit(s[a]&((1<<p)-1))];
}
if(s[0]&((1<<t)-1)) {
int q=berv[highbit(s[0]&((1<<t)-1))];
int p=berv[highbit(s[ch[0][q]])];
return (q<<5)|p;
}
return -1;
}
signed next(int x) {
if(!c[0]) return -1;
int t=get(x,1),p=get(x,0),a=ch[0][t];
if(a&&c[a]&&p!=31) {
int nw=(s[a]>>(p+1))<<(p+1);
if(nw) return (t<<5)|berv[lowbit(nw)];
}
if(t==31) return -1;
int nw=(s[0]>>(t+1))<<(t+1);
if(nw) {
int q=berv[lowbit(nw)];
int p=berv[lowbit(s[ch[0][q]])];
return (q<<5)|p;
}
return -1;
}
#undef int
} t[800];
namespace ran {
int bel[N],lf[N],rt[N],blk,tot;
ll sum[N];
void init() {
rep(i,1,n) bel[i]=(i-1)/blk+1;
tot=bel[n];
rep(i,1,tot) lf[i]=(i-1)*blk+1,rt[i]=i*blk;
}
void insert(int x) {
int b=bel[a[x]],v=a[x]-lf[b]+1;
int pre=t[b].prev(v),nex=t[b].next(v);
if(pre!=-1) pre+=lf[b]-1;
if(nex!=-1) nex+=lf[b]-1;
if(pre!=-1&&nex!=-1) {
sum[b]-=abs(rv[pre]-rv[nex]);
sum[b]+=abs(rv[pre]-x)+abs(rv[nex]-x);
} else if(pre!=-1) sum[b]+=abs(rv[pre]-x);
else if(nex!=-1) sum[b]+=abs(rv[nex]-x);
t[b].insert(v);
}
void erase(int x) {
int b=bel[a[x]],v=a[x]-lf[b]+1;
int pre=t[b].prev(v),nex=t[b].next(v);
if(pre!=-1) pre+=lf[b]-1;
if(nex!=-1) nex+=lf[b]-1;
if(pre!=-1&&nex!=-1) {
sum[b]+=abs(rv[pre]-rv[nex]);
sum[b]-=abs(rv[pre]-x)+abs(rv[nex]-x);
} else if(pre!=-1) sum[b]-=abs(rv[pre]-x);
else if(nex!=-1) sum[b]-=abs(rv[nex]-x);
t[b].erase(v);
}
int query() {
int pre=-1,res=0;
rep(i,1,tot) {
res+=sum[i];
int mn=t[i].next(0);
if(mn!=-1) mn+=lf[i]-1;
if(pre!=-1&&mn!=-1) res+=abs(rv[mn]-rv[pre]);
int nw=t[i].prev(1001);
if(nw!=-1) pre=nw+lf[i]-1;
}
return res;
}
}
namespace seq{
int bel[N],lf[N],rt[N],blk,tot,L,R;
struct ques {
int l,r,id;
bool operator < (const ques &x) const { return bel[l]==bel[x.l]?r<x.r:bel[l]<bel[x.l]; }
} q[N];
void init() {
rep(i,1,n) bel[i]=(i-1)/blk+1;
tot=bel[n];
rep(i,1,tot) lf[i]=(i-1)*blk+1,rt[i]=i*blk;
}
void insert(int x) {
ran::insert(x);
}
void erase(int x) {
ran::erase(x);
}
void solve() {
sort(q+1,q+m+1);
rep(i,1,m) {
while(R<q[i].r) insert(++R);
while(L>q[i].l) insert(--L);
while(R>q[i].r) erase(R--);
while(L<q[i].l) erase(L++);
ans[q[i].id]=ran::query();
}
}
}
void init() {
unsigned x=1;
rep(i,0,31) berv[x]=i,x<<=1;
seq::init(),ran::init();
}
void solve() {
seq::solve();
}
void file() {
freopen("data.txt","r",stdin);
freopen("yours.txt","w",stdout);
}
int main() {
// file();
// printf("%lf\n",1.0*sizeof t/1024/1024);
scanf("%d%d",&n,&m),seq::blk=150,ran::blk=1000;
rep(i,1,n) scanf("%d",&a[i]),rv[a[i]]=i;
rep(i,1,m) {
int l,r; scanf("%d%d",&l,&r);
seq::q[i]={l,r,i};
}
init();
solve();
rep(i,1,m) printf("%lld\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 18100kb
input:
5 5 1 5 2 4 3 1 5 1 4 1 3 1 2 1 1
output:
7 5 3 1 0
result:
ok 5 number(s): "7 5 3 1 0"
Test #2:
score: 0
Accepted
time: 0ms
memory: 18200kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 10ms
memory: 26312kb
input:
100000 1 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 ...
output:
704982704
result:
wrong answer 1st numbers differ - expected: '4999950000', found: '704982704'