이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define N 100005
#define ll long long int
#define fo(i,x,y) for(int i=x;i<=y;i++)
#define fs(ar,n) fo(i,1,n) cin>>ar[i]
#define sp " "
#define fast cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define ii pair<int,int>
#define lli pair<ll,ll>
#include "shoes.h"
using namespace std;
ll n,m,ar[N],sum,tree[4*N],lazy[4*N],mark[N],tutmac[N][3],tut[N];
stack<int> st[N][3];
void build(int ind,int l,int r)
{
if(l==r)
{
tree[ind]=l;
return;
}
int mid=(l+r)/2;
build(ind*2,l,mid);
build(ind*2+1,mid+1,r);
}
void push(int ind,int l,int r)
{
ll x=lazy[ind];
lazy[ind]=0;
tree[ind]+=x;
if(l==r)
return;
lazy[ind*2]+=x;
lazy[ind*2+1]+=x;
}
ll query(int ind,int l,int r,int a)
{
if(l>a || r<a)
return 0;
push(ind,l,r);
if(l==r)
return tree[ind];
int mid=(l+r)/2;
if(a<=mid)
return query(ind*2,l,mid,a);
else
return query(ind*2+1,mid+1,r,a);
}
void upd(int ind,int l,int r,int a,int b)
{
if(l>b || r<a)
return;
push(ind,l,r);
if(l>=a && r<=b)
{
lazy[ind]++;
push(ind,l,r);
return;
}
int mid=(l+r)/2;
upd(ind*2,l,mid,a,b);
upd(ind*2+1,mid+1,r,a,b);
}
void pre()
{
for(int i=n;i>=1;i--)
{
if(ar[i]<0)
st[-ar[i]][0].push(i);
else
st[ar[i]][1].push(i);
}
}
long long count_swaps(std::vector<int> s) {
n=s.size();
fo(i,1,n)
ar[i]=s[i-1];
pre();
build(1,1,n);
// cout<<query(1,1,n,7)<<endl;
// upd(1,1,n,1,6);
// upd(1,1,n,2,4);
// cout<<query(1,1,n,7)<<endl;
// cout<<endl;
fo(i,1,n)
{
if(mark[i])
continue;
mark[i]=1;
if(ar[i]>0)
{
while(mark[st[ar[i]][0].top()])
st[ar[i]][0].pop();
ll x=st[ar[i]][0].top();
mark[x]=1;
ll a=query(1,1,n,i);
ll b=query(1,1,n,x);
sum+=b-a;
// cerr<<i<<sp<<x<<sp<<a<<sp<<b<<endl;
upd(1,1,n,i,x-1);
}
else
{
while(mark[st[-ar[i]][1].top()])
st[-ar[i]][1].pop();
ll x=st[-ar[i]][1].top();
mark[x]=1;
ll a=query(1,1,n,i);
ll b=query(1,1,n,x);
sum+=b-a-1;
// cerr<<i<<sp<<x<<sp<<a<<sp<<b<<endl;
upd(1,1,n,i,x-1);
}
}
return sum;
}
// int main() {
// int n;
// assert(1 == scanf("%d", &n));
// vector<int> S(2 * n);
// for (int i = 0; i < 2 * n; i++)
// assert(1 == scanf("%d", &S[i]));
// fclose(stdin);
// long long result = count_swaps(S);
// printf("%lld\n", result);
// fclose(stdout);
// return 0;
// }
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |