QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#647399 | #7434. 冷たい部屋、一人 | piggy123 | WA | 3ms | 42596kb | C++20 | 8.5kb | 2024-10-17 13:52:18 | 2024-10-17 13:52:19 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[500005],lg[500005],ans[500005],n,m;
int b[21][500005],c[21][500005];
struct qry {
ll l,r,id;
} p[500005],p2[500005];
vector<int> col[500005],CC[500005];
const ll S=600;
ll S2=0;
struct block {
const ll B=800;
int tag[805],z[500005];
void add(ll pos,ll v) {
z[pos]+=v;
tag[(pos-1)/B+1]+=v;
}
ll query(ll pos) {
ll v=(pos-1)/B+1,r=(n-1)/B+1,ans=0;
for (ll i=v+1; i<=r; i++)ans+=tag[i];
for (ll i=pos; i<=v*B; i++)ans+=z[i];
return ans;
}
} B;
inline ll getmx(ll l,ll r) {
ll k=lg[r-l+1];
return max(c[k][l],c[k][r-(1ll<<k)+1]);
}
inline ll getmi(ll l,ll r) {
ll k=lg[r-l+1];
return min(b[k][l],b[k][r-(1ll<<k)+1]);
}
ll v[1000005],cnt[1000005],d[1000005],nxt[1000005],to[1000005],T;
vector<qry> Q[1000005];
struct CHAIN {
ll vis[1000005],sz[1000005],ano[1000005];
array<ll,5> stk[1000005];
ll top,gx;
inline void init(ll len) {
for (ll i=0; i<=len; i++)vis[i]=0,sz[i]=1,ano[i]=i;
top=0,gx=0;
}
inline void link(ll a) {
ll l=ano[a],r=ano[a+1];
ano[l]=r,ano[r]=l;
stk[++top]= {l,r,a,sz[a],sz[a+1]};
gx+=sz[a]*sz[a+1];
sz[l]=sz[r]=sz[a]+sz[a+1];
// cout<<"?" <<a<<" "<< sz[a]<<" "<<sz[a+1]<<"\n";
}
inline void add(ll pos) {
vis[d[pos]]++;
if (vis[d[pos]]==2) {
link(d[pos]);
}
}
inline void del(ll pos) {
vis[d[pos]]--;
if (vis[d[pos]]==1) {
redo(d[pos]);
}
}
inline void redo(ll d) {
auto tp=stk[top--];
gx-=tp[3]*tp[4];
ano[tp[0]]=tp[2];
ano[tp[1]]=tp[2]+1;
sz[tp[0]]=tp[3];
sz[tp[1]]=tp[4];
}
} C;
namespace ly
{
namespace IO
{
#define SIZE (1<<25)
char in[SIZE],out[SIZE],*P1=in,*P2=in,*P3=out;
#define getchar() (P1==P2&&(P2=(P1=in)+fread(in,1,SIZE,stdin),P1==P2)?EOF:*P1++)
#define flush() (fwrite(P3=out,1,SIZE,stdout))
#define putchar(ch) (P3==out+SIZE&&flush(),*P3++=(ch))
class Flush{public:~Flush(){flush();}}_;
template<typename type>
inline void read(type &x)
{
x=0;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
}
template<typename type>
inline void write(type x)
{
static short Stack[50],top(0);
do Stack[++top]=x%10,x/=10;while(x);
while(top) putchar(Stack[top--]|48);
putchar('\n');
}
}
}using namespace ly::IO;
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
read(n),read(m);
for (ll i=1; i<=n; i++) {
read(a[i]);
col[a[i]].push_back(i);
}
for (ll i=1; i<=n; i++) {
if (i>1)lg[i]=lg[i>>1]+1;
read(b[0][i]);
c[0][i]=b[0][i];
}
for (ll i=1; i<=20; i++) {
for (ll j=1; j+(1ll<<i)-1<=n; j++) {
b[i][j]=min(b[i-1][j],b[i-1][j+(1ll<<(i-1))]);
c[i][j]=max(c[i-1][j],c[i-1][j+(1ll<<(i-1))]);
}
}
for (ll i=1; i<=m; i++) {
read(p[i].l),read(p[i].r);
p[i].id=i;
}
for (ll i=1; i<=n; i++) {
if (col[i].size()<=S) {
for (ll j=0; j<(ll)col[i].size(); j++) {
for (ll k=j+1; k<(ll)col[i].size(); k++) {
ll mi=getmi(col[i][j],col[i][k]),mx=getmx(col[i][j],col[i][k]);
CC[mx].push_back(mi);
}
}
}
}
sort(p+1,p+1+m,[](qry a,qry b) {
return a.r<b.r;
});
ll ps=0;
for (ll i=1; i<=m; i++) {
while (ps+1<=n&&ps+1<=p[i].r) {
for (ll j:CC[ps+1]) {
B.add(j,1);
}
ps++;
}
ans[p[i].id]+=B.query(p[i].l);
}
for (ll i=1; i<=n; i++) {
if (col[i].size()>S) {
T=i;
ll N=col[i].size();
vector<ll> book;
for (ll j=0; j+1<N; j++) {
ll mi=getmi(col[i][j],col[i][j+1]),mx=getmx(col[i][j],col[i][j+1]);
book.push_back(mi);
book.push_back(mx);
}
sort(book.begin(),book.end());
book.erase(unique(book.begin(),book.end()),book.end());
for (ll j=0; j+1<N; j++) {
ll mi=getmi(col[i][j],col[i][j+1]),mx=getmx(col[i][j],col[i][j+1]);
++cnt[lower_bound(book.begin(),book.end(),mi)-book.begin()+1];
++cnt[lower_bound(book.begin(),book.end(),mx)-book.begin()+1];
}
for (ll j=1; j<=(ll)book.size(); j++)cnt[j]+=cnt[j-1];
ll lim=cnt[book.size()];
for (ll j=0; j+1<N; j++) {
ll mi=getmi(col[i][j],col[i][j+1]),mx=getmx(col[i][j],col[i][j+1]);
ll mip=lower_bound(book.begin(),book.end(),mi)-book.begin()+1;
ll mxp=lower_bound(book.begin(),book.end(),mx)-book.begin()+1;
d[cnt[mip]--]=j;
to[cnt[mip]+1]=mi;
d[cnt[mxp]--]=j;
to[cnt[mxp]+1]=mx;
}
ll ps=0;
for (ll j=1; j<=n; j++) {
while (ps+1<=lim&&to[ps+1]<=j)ps++;
nxt[j]=ps;
}
S2=lim/sqrt(m)+1;
for (ll j=1; j<=m; j++) {
p2[j].l=nxt[p[j].l-1]+1,p2[j].r=nxt[p[j].r],p2[j].id=p[j].id;
if (p2[j].l<=p2[j].r) {
if ((p2[j].l-1)/S2==(p2[j].r-1)/S2) {
C.init(lim);
for (ll k=p2[j].l; k<=p2[j].r; k++) {
C.add(k);
}
ans[p2[j].id]+=C.gx;
} else {
Q[(p2[j].l-1)/S2+1].push_back(p2[j]);
}
}
}
for (ll j=1; j<=(lim-1)/S2+1; j++) {
if (!Q[j].size())continue;
ll lb=j*S2;
C.init(lim);
ll L=lb+1,R=lb;
// cout<<(j-1)*S2+1<<"~"<<j*S2<<":\n";
for (qry k:Q[j]) {
// cout<<k.l<<","<<k.r<<"\n";
while (R<k.r)C.add(++R);
while (L>k.l)C.add(--L);
ans[k.id]+=C.gx;
while (L<lb+1)C.del(L++);
}
}
for (ll j=0; j<=lim; j++)cnt[j]=to[j]=nxt[j]=d[j]=0,Q[j].clear();
}
}
for (ll i=1; i<=m; i++) {
write(ans[i]);
}
flush();
return 0;
}
/*
99 14
44 57 23 96 61 57 8 59 91 61 45 98 56 92 86 11 89 33 83 79 86 52 70 9 90 30 8 31 5 96 57 53 83 47 52 49 63 32 53 27 60 11 41 77 4 64 68 46 95 86 86 69 41 5 37 39 41 37 66 72 18 68 49 84 50 88 22 21 65 47 41 33 67 98 65 18 68 15 79 25 37 42 2 92 14 21 69 33 51 15 36 17 4 93 81 4 92 44 18
13 2 10 99 97 28 59 83 87 91 19 63 33 41 72 52 92 42 95 18 9 48 65 67 66 8 7 77 6 5 78 82 55 36 57 40 26 4 88 17 62 69 15 14 25 56 98 20 21 60 76 34 22 29 79 16 51 35 37 45 84 39 47 61 85 96 58 23 38 24 71 90 32 80 74 93 12 3 89 43 31 53 73 54 68 30 86 44 75 70 46 27 94 11 49 81 1 64 50
5 34
17 92
44 91
33 35
3 58
19 36
12 51
23 69
85 98
69 91
58 82
73 82
24 85
17 93
■■■■■ ■■ ■■■ ■■■ ■ ■ ■ ■■■■ ■■■■
■ ■■ ■■ ■ ■■ ■ ■■ ■ ■ ■■ ■ ■■ ■■ ■
■ ■ ■■ ■ ■ ■ ■ ■ ■ ■■■ ■■ ■■ ■ ■■
■ ■ ■■ ■ ■ ■ ■ ■ ■ ■■ ■ ■■ ■■
■ ■ ■■ ■ ■ ■■ ■■ ■■ ■
■ ■■ ■■ ■ ■■■ ■ ■■■ ■■ ■■ ■■ ■■■
■■■■■ ■■ ■ ■ ■ ■ ■■ ■■ ■■ ■■
■ ■■ ■ ■ ■ ■ ■■ ■■ ■■ ■
■ ■■ ■ ■ ■ ■ ■■ ■■ ■ ■ ■■
■ ■■ ■■ ■■ ■■ ■■ ■■ ■■ ■ ■■ ■■
■ ■■ ■■■■ ■■■■ ■■ ■■ ■■■■■■ ■■■■
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 42596kb
input:
100 100 4 1 5 1 2 1 7 5 3 7 2 3 6 6 5 3 2 2 4 1 6 5 6 2 2 2 7 6 1 3 6 3 5 6 7 6 1 2 3 3 4 2 1 1 5 4 4 3 6 7 1 1 6 1 5 6 2 3 7 4 2 4 6 7 7 3 5 3 7 2 3 3 5 1 4 7 1 2 2 5 2 2 4 3 4 7 2 7 7 3 7 3 6 6 5 4 5 4 7 6 93 52 12 70 25 36 18 37 27 99 68 40 84 3 76 57 60 19 33 41 92 87 58 13 15 43 28 63 64 59 31 ...
output:
1 0 13 71 1 1 3 7 0 0 2 1 3 20 12 6 61 24 1 0 0 2 3 19 0 0 6 2 0 0 4 1 135 0 19 1 1 29 14 39 1 0 1 7 7 0 12 3 0 1 0 1 1 5 0 28 14 19 2 1 0 0 6 5 0 0 2 7 5 1 2 2 0 1 11 1 0 1 0 10 0 0 5 1 33 1 17 2 1 22 20 1 2 0 0 16 0 1 1 15
result:
wrong answer Output contains longer sequence [length = 201], but answer contains 100 elements