답안 #364376

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
364376 2021-02-09T04:37:57 Z daniel920712 운세 보기 2 (JOI14_fortune_telling2) C++14
4 / 100
313 ms 262148 KB
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <string.h>
#include <map>
#include <vector>
#include <set>

using namespace std;
long long x[200005],y[200005];
pair < long long , long long > all[200005];
vector < pair < pair < long long , long long > , pair < long long , long long > > > a,b;
bool F(pair < long long , long long > a,pair < long long , long long > b)
{
    if(a.first>b.first) return 1;
    if(a.first<b.first) return 0;
    return a.second<b.second;
}
set < long long > vis;
struct A
{
    long long l,r;
    int nxl,nxr,nxt;
    long long add;
}Node[50000005];
int now=1;
void New(long long l,long long r,long long a,long long here)
{
    Node[here].l=l;
    Node[here].r=r;
    Node[here].add=0;
    Node[here].nxl=-1;
    Node[here].nxr=-1;
    Node[here].nxt=a;
}
long long Find(long long x,long long y,long long here)
{
    long long t=0;
    if(here==-1) return 0;
    t=Node[here].add;
    if(Node[here].nxt==-2)
    {
        //t+=Find(x,y,Node[here].nxt);
        if(Node[here].l==Node[here].r) return t;
        if(y<=(Node[here].l+Node[here].r)/2) t+=Find(x,y,Node[here].nxl);
        else t+=Find(x,y,Node[here].nxr);
    }
    else
    {
        t+=Find(x,y,Node[here].nxt);
        if(Node[here].l==Node[here].r) return t;
        if(x<=(Node[here].l+Node[here].r)/2) t+=Find(x,y,Node[here].nxl);
        else t+=Find(x,y,Node[here].nxr);
    }
    return t;
}
void add(long long l1,long long r1,long long l2,long long r2,long long con,long long here)
{
    long long t;
    //if(here==-1) return 0;
    if(Node[here].nxt==-2)
    {
        if(l2==Node[here].l&&r2==Node[here].r)
        {
            //if(con==1) printf("%lld %lld %lld %lld %lld\n",l1,r1,l2,r2,con);
            Node[here].add+=con;
            return;
        }

        if(r2<=(Node[here].l+Node[here].r)/2)
        {
            if(Node[here].nxl==-1)
            {
                Node[here].nxl=now++;
                New(Node[here].l,(Node[here].l+Node[here].r)/2,-2,Node[here].nxl);
            }
            add(l1,r1,l2,r2,con,Node[here].nxl);
        }
        else if(l2>(Node[here].l+Node[here].r)/2)
        {
            if(Node[here].nxr==-1)
            {
                Node[here].nxr=now++;
                New((Node[here].l+Node[here].r)/2+1,Node[here].r,-2,Node[here].nxr);
            }
            add(l1,r1,l2,r2,con,Node[here].nxr);
        }
        else
        {
            if(Node[here].nxl==-1)
            {
                Node[here].nxl=now++;
                New(Node[here].l,(Node[here].l+Node[here].r)/2,-2,Node[here].nxl);
            }
            add(l1,r1,l2,(Node[here].l+Node[here].r)/2,con,Node[here].nxl);
            if(Node[here].nxr==-1)
            {
                Node[here].nxr=now++;
                New((Node[here].l+Node[here].r)/2+1,Node[here].r,-2,Node[here].nxr);
            }
            add(l1,r1,(Node[here].l+Node[here].r)/2+1,r2,con,Node[here].nxr);

        }
    }
    else
    {
        if(l1==Node[here].l&&r1==Node[here].r)
        {
            if(Node[here].nxt==-1)
            {
                Node[here].nxt=now++;
                New(0,2e9,-2,Node[here].nxt);
            }
            add(l1,r1,l2,r2,con,Node[here].nxt);
            return;
        }

        if(r1<=(Node[here].l+Node[here].r)/2)
        {
            if(Node[here].nxl==-1)
            {
                Node[here].nxl=now++;
                New(Node[here].l,(Node[here].l+Node[here].r)/2,-1,Node[here].nxl);
            }
            add(l1,r1,l2,r2,con,Node[here].nxl);
        }
        else if(l1>(Node[here].l+Node[here].r)/2)
        {
            if(Node[here].nxr==-1)
            {
                Node[here].nxr=now++;
                New((Node[here].l+Node[here].r)/2+1,Node[here].r,-1,Node[here].nxr);
            }
            add(l1,r1,l2,r2,con,Node[here].nxr);
        }
        else
        {
            if(Node[here].nxl==-1)
            {
                Node[here].nxl=now++;
                New(Node[here].l,(Node[here].l+Node[here].r)/2,-1,Node[here].nxl);
            }
            add(l1,(Node[here].l+Node[here].r)/2,l2,r2,con,Node[here].nxl);
            if(Node[here].nxr==-1)
            {
                Node[here].nxr=now++;
                New((Node[here].l+Node[here].r)/2+1,Node[here].r,-1,Node[here].nxr);
            }
            add((Node[here].l+Node[here].r)/2+1,r1,l2,r2,con,Node[here].nxr);

        }
    }

}
int main()
{
    long long N,K,i,j,con,ok=0,ans=0,con2=0,tt,tt2;
    scanf("%lld %lld",&N,&K);
    for(i=0;i<N;i++) scanf("%lld %lld",&x[i],&y[i]);
    for(i=1;i<=K;i++)
    {
        scanf("%lld",&all[i].first);
        all[i].second=i;
    }
    vis.insert(0);
    vis.insert(2e9);

    for(i=K;i>=1;i--)
    {
        con=0;
        for(j=i+1;j<=K;j++) if(all[j].first>all[i].first) con++;
        if(vis.find(all[i].first)!=vis.end()) continue;
        tt=*vis.upper_bound(all[i].first);
        tt2=*prev(vis.upper_bound(all[i].first));
        if(con%2==0)
        {
            a.push_back(make_pair(make_pair(all[i].first+1,tt),make_pair(tt2,all[i].first)));
            b.push_back(make_pair(make_pair(tt2,all[i].first),make_pair(all[i].first+1,tt)));
        }
        else
        {
            b.push_back(make_pair(make_pair(all[i].first+1,tt),make_pair(tt2,all[i].first)));
            a.push_back(make_pair(make_pair(tt2,all[i].first),make_pair(all[i].first+1,tt)));
        }
        vis.insert(all[i].first);
    }
    New(0,2e9,-1,0);
    all[0].first=2e9;
    all[0].second=0;
    sort(all,all+K+1,F);
    all[K+1].first=0;
    all[K+1].second=K+1;

    for(i=1;i<=K+1;i++)
    {
        con=0;
        con2=0;
        for(j=0;j<i;j++) if(all[j].second>all[i].second&&all[j].first>all[i].first) con++;
        for(j=0;j<i;j++) if(all[j].first>all[i].first) con2++;
        if(all[i].first!=all[i-1].first)
        {
            if(con2%2==1) a.push_back(make_pair(make_pair(all[i].first+1,all[i-1].first),make_pair(all[i].first+1,all[i-1].first)));
            else b.push_back(make_pair(make_pair(all[i].first+1,all[i-1].first),make_pair(all[i].first+1,all[i-1].first)));
        }
    }

    for(auto j:a)
    {
        //printf("%lld %lld %lld %lld\n",j.first.first,j.first.second,j.second.first,j.second.second);
        add(j.first.first,j.first.second,j.second.first,j.second.second,1,0);

    }
    for(auto j:b) add(j.first.first,j.first.second,j.second.first,j.second.second,1e9,0);

    for(i=0;i<N;i++)
    {
        ok=0;


        ok=Find(x[i],y[i],0);
        //printf("%lld\n",ok);
        if(ok>=1000000000)
        {
            ans+=y[i];
            //if(ans%1000000000) while(1);
        }
        else ans+=x[i];
    }
    printf("%lld\n",ans);

    return 0;
}

Compilation message

fortune_telling2.cpp: In function 'void add(long long int, long long int, long long int, long long int, long long int, long long int)':
fortune_telling2.cpp:61:15: warning: unused variable 't' [-Wunused-variable]
   61 |     long long t;
      |               ^
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:160:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  160 |     scanf("%lld %lld",&N,&K);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:161:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  161 |     for(i=0;i<N;i++) scanf("%lld %lld",&x[i],&y[i]);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:164:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  164 |         scanf("%lld",&all[i].first);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 120320 KB Output is correct
2 Correct 123 ms 120044 KB Output is correct
3 Correct 126 ms 121196 KB Output is correct
4 Correct 121 ms 121196 KB Output is correct
5 Correct 124 ms 121068 KB Output is correct
6 Correct 122 ms 120832 KB Output is correct
7 Correct 123 ms 120372 KB Output is correct
8 Correct 122 ms 121068 KB Output is correct
9 Correct 119 ms 117888 KB Output is correct
10 Correct 6 ms 3308 KB Output is correct
11 Correct 120 ms 119788 KB Output is correct
12 Correct 118 ms 117100 KB Output is correct
13 Correct 123 ms 121260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 120320 KB Output is correct
2 Correct 123 ms 120044 KB Output is correct
3 Correct 126 ms 121196 KB Output is correct
4 Correct 121 ms 121196 KB Output is correct
5 Correct 124 ms 121068 KB Output is correct
6 Correct 122 ms 120832 KB Output is correct
7 Correct 123 ms 120372 KB Output is correct
8 Correct 122 ms 121068 KB Output is correct
9 Correct 119 ms 117888 KB Output is correct
10 Correct 6 ms 3308 KB Output is correct
11 Correct 120 ms 119788 KB Output is correct
12 Correct 118 ms 117100 KB Output is correct
13 Correct 123 ms 121260 KB Output is correct
14 Runtime error 313 ms 262148 KB Execution killed with signal 9
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 120320 KB Output is correct
2 Correct 123 ms 120044 KB Output is correct
3 Correct 126 ms 121196 KB Output is correct
4 Correct 121 ms 121196 KB Output is correct
5 Correct 124 ms 121068 KB Output is correct
6 Correct 122 ms 120832 KB Output is correct
7 Correct 123 ms 120372 KB Output is correct
8 Correct 122 ms 121068 KB Output is correct
9 Correct 119 ms 117888 KB Output is correct
10 Correct 6 ms 3308 KB Output is correct
11 Correct 120 ms 119788 KB Output is correct
12 Correct 118 ms 117100 KB Output is correct
13 Correct 123 ms 121260 KB Output is correct
14 Runtime error 313 ms 262148 KB Execution killed with signal 9
15 Halted 0 ms 0 KB -