답안 #1100895

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100895 2024-10-15T02:05:08 Z vjudge1 운세 보기 2 (JOI14_fortune_telling2) C++17
100 / 100
892 ms 44872 KB
#include <bits/stdc++.h>
using namespace std;

const int N=2e5;

int st[12*N+1],n,q;

void update(int x, int low, int hi ,int i, int k)
{
    if(low==hi) {st[x]=max(st[x],k); return;}
    int mid = low+hi>>1;
    if(i<=mid) update(x*2,low,mid,i,k);
    else update(x*2+1,mid+1,hi,i,k);
    st[x]=max(st[x*2],st[x*2+1]);
}

int getmx(int x, int low ,int hi, int i, int j)
{
    if(low>hi || low >j || hi < i) return 0;
    if(low>=i && hi<=j) return st[x];
    int mid=low+hi>>1;
    return max(getmx(x*2,low,mid,i,j), getmx(x*2+1,mid+1,hi,i,j));
}

int bit[3*N+100];

void add(int x)
{
    for(; x<=2*n+q+1;x+=x&(-x))
    {
        bit[x]++;
    }
}

int get(int x)
{
    int ans=0;
    for(; x>0;x-=x&(-x))
    {
        ans+=bit[x];
    }
    return ans;
}

int a[N+1],b[N+1],t[N+1];
int ag[N+1],bg[N+1];
int last[N+1];
int id[N+1];
map<int,int> mp;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> q;
    for(int i=1;i<=n;++i)
    {
        cin >> a[i] >> b[i];
        ag[i]=a[i];
        bg[i]=b[i];
        mp[a[i]]=0;
        mp[b[i]]=0;
    }
    for(int i=1;i<=q;++i)
    {
        cin >> t[i];
        mp[t[i]]=0;
    }
    int z=0;
    for(auto & k : mp) k.second = ++z;
    for(int i=1;i<=q;++i)
    {
        t[i] = mp[t[i]];
        update(1,1,2*n+q,t[i],i);
       // cout << t[i] << '\n';
    }
    for(int i=1;i<=n;++i)
    {
        a[i]=mp[a[i]];
        b[i]=mp[b[i]];
        last[i]=getmx(1,1,2*n+q,min(a[i],b[i]),max(a[i],b[i])-1);
      //  cout << a[i] << " " << b[i] << '\n';
        //cout << last[i] << " ";
        id[i]=i;
    }
   // cout << '\n';
    sort(id+1,id+n+1,[] (int x, int y)
         {
             return last[x]>last[y];
         });
   // cout << "WTF";
    long long ans=0;
    int now=q;
    for(int i=1;i<=n;++i)
    {
        int j = id[i];
       // cout << j << " " << last[j] << '\n';
        while(now > 0 && now>=last[j])
        {
            add(t[now]);
           // cout << "ADD : " << t[now] << '\n';
            now--;
        }
       // cout << "GET : " << max(a[j],b[j]) << '\n';
        int k = get(2*n+q+1) - get(max(a[j],b[j])-1);
       // cout << k << '\n';
        int cc=0;
        if(a[j]>b[j])
        {
            swap(a[j],b[j]);
            swap(ag[j],bg[j]);
            cc=1;
        }
        if(last[j]>0) cc=1;
        k%=2;
        cc^=k;
        if(cc) ans+=(long long)bg[j];
        else ans+=(long long)ag[j];
    }
    cout << ans;
}

Compilation message

fortune_telling2.cpp: In function 'void update(int, int, int, int, int)':
fortune_telling2.cpp:11:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   11 |     int mid = low+hi>>1;
      |               ~~~^~~
fortune_telling2.cpp: In function 'int getmx(int, int, int, int, int)':
fortune_telling2.cpp:21:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   21 |     int mid=low+hi>>1;
      |             ~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8528 KB Output is correct
3 Correct 4 ms 8784 KB Output is correct
4 Correct 3 ms 8784 KB Output is correct
5 Correct 3 ms 8784 KB Output is correct
6 Correct 3 ms 8664 KB Output is correct
7 Correct 3 ms 8784 KB Output is correct
8 Correct 3 ms 8784 KB Output is correct
9 Correct 3 ms 8784 KB Output is correct
10 Correct 2 ms 8744 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8528 KB Output is correct
3 Correct 4 ms 8784 KB Output is correct
4 Correct 3 ms 8784 KB Output is correct
5 Correct 3 ms 8784 KB Output is correct
6 Correct 3 ms 8664 KB Output is correct
7 Correct 3 ms 8784 KB Output is correct
8 Correct 3 ms 8784 KB Output is correct
9 Correct 3 ms 8784 KB Output is correct
10 Correct 2 ms 8744 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
14 Correct 24 ms 10064 KB Output is correct
15 Correct 40 ms 11664 KB Output is correct
16 Correct 63 ms 13392 KB Output is correct
17 Correct 85 ms 14920 KB Output is correct
18 Correct 87 ms 14920 KB Output is correct
19 Correct 86 ms 14920 KB Output is correct
20 Correct 93 ms 14920 KB Output is correct
21 Correct 83 ms 14912 KB Output is correct
22 Correct 55 ms 12728 KB Output is correct
23 Correct 49 ms 11860 KB Output is correct
24 Correct 48 ms 11080 KB Output is correct
25 Correct 65 ms 13316 KB Output is correct
26 Correct 55 ms 12484 KB Output is correct
27 Correct 73 ms 12872 KB Output is correct
28 Correct 59 ms 12872 KB Output is correct
29 Correct 80 ms 13896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8528 KB Output is correct
2 Correct 2 ms 8528 KB Output is correct
3 Correct 4 ms 8784 KB Output is correct
4 Correct 3 ms 8784 KB Output is correct
5 Correct 3 ms 8784 KB Output is correct
6 Correct 3 ms 8664 KB Output is correct
7 Correct 3 ms 8784 KB Output is correct
8 Correct 3 ms 8784 KB Output is correct
9 Correct 3 ms 8784 KB Output is correct
10 Correct 2 ms 8744 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 2 ms 8528 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
14 Correct 24 ms 10064 KB Output is correct
15 Correct 40 ms 11664 KB Output is correct
16 Correct 63 ms 13392 KB Output is correct
17 Correct 85 ms 14920 KB Output is correct
18 Correct 87 ms 14920 KB Output is correct
19 Correct 86 ms 14920 KB Output is correct
20 Correct 93 ms 14920 KB Output is correct
21 Correct 83 ms 14912 KB Output is correct
22 Correct 55 ms 12728 KB Output is correct
23 Correct 49 ms 11860 KB Output is correct
24 Correct 48 ms 11080 KB Output is correct
25 Correct 65 ms 13316 KB Output is correct
26 Correct 55 ms 12484 KB Output is correct
27 Correct 73 ms 12872 KB Output is correct
28 Correct 59 ms 12872 KB Output is correct
29 Correct 80 ms 13896 KB Output is correct
30 Correct 192 ms 20304 KB Output is correct
31 Correct 322 ms 26440 KB Output is correct
32 Correct 414 ms 30940 KB Output is correct
33 Correct 825 ms 44488 KB Output is correct
34 Correct 161 ms 19528 KB Output is correct
35 Correct 773 ms 44872 KB Output is correct
36 Correct 760 ms 44508 KB Output is correct
37 Correct 798 ms 44524 KB Output is correct
38 Correct 784 ms 44512 KB Output is correct
39 Correct 892 ms 44616 KB Output is correct
40 Correct 708 ms 44592 KB Output is correct
41 Correct 752 ms 44620 KB Output is correct
42 Correct 816 ms 44512 KB Output is correct
43 Correct 418 ms 42404 KB Output is correct
44 Correct 399 ms 42572 KB Output is correct
45 Correct 430 ms 42048 KB Output is correct
46 Correct 339 ms 27392 KB Output is correct
47 Correct 314 ms 22088 KB Output is correct
48 Correct 469 ms 33096 KB Output is correct
49 Correct 475 ms 33096 KB Output is correct