QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#19047#1877. Matryoshka DollstkyCompile Error//C++112.1kb2022-01-27 20:22:082022-05-18 04:05:38

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-18 04:05:38]
  • 评测
  • [2022-01-27 20:22:08]
  • 提交

answer

#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
#include<unordered_map>
#include<set>
#pragma GCC optimize("Ofast")
#define int long long
#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;
}
      
      

详细

cc1plus: error: ‘::main’ must return ‘int’