QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#19045 | #1877. Matryoshka Dolls | tky | WA | 27ms | 16456kb | C++11 | 2.1kb | 2022-01-27 20:18:09 | 2022-05-06 03:41:39 |
Judging History
answer
#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#include<unordered_map>
#include<set>
#define um unordered_map
using namespace std;
const int N=2e5+5;
int val[N],id[N],pos[N],tot;
struct interval
{
int l,r;
bool operator<(interval b)const
{
return r<b.r;
}
bool operator==(interval b)const
{
return l==b.l&&r==b.r;
}
};
struct Hash
{
size_t operator()(interval b)const
{
return b.l*N+b.r;
}
};
vector<interval> a[N];
set<int> s;
um<interval,int,Hash> Map;
interval note[N];
void add(int x)
{
s.insert(val[x]);
auto k=s.find(val[x]);
if(s.size()==1)return;
if(k==s.begin())
{
tot+=abs(id[*k]-id[*(++k)]);
return;
}
auto kkk=k;++kkk;
if(kkk==s.end())
{
tot+=abs(id[*k]-id[*(--k)]);
return;
}
auto kk=k;kk--;
tot-=abs(id[*kkk]-id[*kk]);
tot+=abs(id[*k]-id[*kk]);
tot+=abs(id[*k]-id[*kkk]);
}
void del(int x)
{
if(!x)return;
auto k=s.find(val[x]);
auto kkk=k;kkk++;
auto kk=k;--kk;
if(k==s.begin())
{
tot-=abs(id[*k]-id[*(kkk)]);
s.erase(k);
return;
}
if(kkk==s.end())
{
tot-=abs(id[*k]-id[*kk]);
s.erase(k);
return;
}
tot+=abs(id[*kkk]-id[*kk]);
tot-=abs(id[*k]-id[*kk]);
tot-=abs(id[*k]-id[*kkk]);
s.erase(k);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n,m;cin>>n>>m;
int block=sqrt(n);
for(int i=1;i<=n;++i)cin>>val[i];
for(int i=1;i<=n;++i)id[val[i]]=i;
for(int i=1;i<=n;++i)pos[i]=(i-1)/block+1;
int t=pos[n];
for(int i=1;i<=m;++i)
{
int x,y;cin>>x>>y;
a[pos[x]].push_back({x,y});
note[i]={x,y};
}
for(int i=1;i<=t;++i)sort(a[i].begin(),a[i].end());
int l=0,r=0;
for(int i=1;i<=t;++i)
for(auto j:a[i])
{
while(r<j.r)add(++r);
while(r>j.r)del(r--);
while(l<j.l)del(l++);
while(l>j.l)add(--l);
Map[{j.l,j.r}]=tot;
}
for(int i=1;i<=m;++i)
cout<<Map[note[i]]<<'\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 9712kb
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: 2ms
memory: 9812kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 27ms
memory: 16456kb
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'