답안 #14442

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
14442 2015-05-16T14:32:31 Z ansol4328 공장 (KOI13_factory) C++
20 / 20
355 ms 16708 KB
#include<stdio.h>
#include<algorithm>

struct A
{
    int x, num;
};

int n, IT[2000002], check[1000002];
long long sum;
A m[500002];

int cmp(const A &a, const A &b)
{
    return a.x<b.x;
}

void input()
{
    int i, a;
    scanf("%d",&n);
    for(i=1 ; i<=n ; i++)
    {
        scanf("%d",&a);
        check[a]=i;
    }
    for(i=1 ; i<=n ; i++)
    {
        scanf("%d",&a);
        m[i].x=check[a];
        m[i].num=i;
    }
    std::sort(m+1,m+1+n,cmp);
}

void insert(int p)
{
    while(p!=0)
    {
        IT[p]++;
        p/=2;
    }
}

int find(int from, int to)
{
    int s=0;
    while(from<to)
    {
        if(from%2==1)
        {
            s+=IT[from];
            from++;
        }
        if(to%2==0)
        {
            s+=IT[to];
            to--;
        }
        from/=2, to/=2;
    }
    if(from==to) s+=IT[from];
    return s;
}

void process()
{
    int i, k=1;
    while(k<n) k*=2;
    k--;
    for(i=1 ; i<=n ; i++)
    {
        insert(k+m[i].num);
        sum+=find(k+m[i].num+1,k+n);
    }
}

void output()
{
    printf("%lld",sum);
}

int main()
{
    input();
    process();
    output();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 16708 KB Output is correct
2 Correct 0 ms 16708 KB Output is correct
3 Correct 0 ms 16708 KB Output is correct
4 Correct 0 ms 16708 KB Output is correct
5 Correct 0 ms 16708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 16708 KB Output is correct
2 Correct 0 ms 16708 KB Output is correct
3 Correct 3 ms 16708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 16708 KB Output is correct
2 Correct 36 ms 16708 KB Output is correct
3 Correct 51 ms 16708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 16708 KB Output is correct
2 Correct 120 ms 16708 KB Output is correct
3 Correct 132 ms 16708 KB Output is correct
4 Correct 186 ms 16708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 261 ms 16708 KB Output is correct
2 Correct 327 ms 16708 KB Output is correct
3 Correct 308 ms 16708 KB Output is correct
4 Correct 331 ms 16708 KB Output is correct
5 Correct 355 ms 16708 KB Output is correct
6 Correct 333 ms 16708 KB Output is correct