Submission #724943

# Submission time Handle Problem Language Result Execution time Memory
724943 2023-04-16T11:17:21 Z Rafi22 Alternating Current (BOI18_alternating) C++14
100 / 100
55 ms 10476 KB
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;

void gg()
{
    cout<<"impossible";
    exit(0);
}

const int N=100007,pot=1<<17;

bool ans[N];
int l[N];
int r[N];
int ile[N];
int L[N];

vector<int>V[N];
int o[N];

pair<int,int> seg[2*pot+7];

pair<int,int> que(int v)
{
    v+=pot-1;
    pair<int,int>p={inf,0};
    while(v>0)
    {
        p=min(p,seg[v]);
        v/=2;
    }
    return p;
}
void ins(int v,int a,int b,int l,int r,int x,int i)
{
    if(a<=l&&b>=r)
    {
        seg[v]=min(seg[v], {x,i});
        return ;
    }
    if(r<a||l>b) return ;
    ins(2*v,a,b,l,(l+r)/2,x,i);
    ins(2*v+1,a,b,(l+r)/2+1,r,x,i);
}


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    int mx=0,k;
    for(int i=1;i<=m;i++)
    {
        cin>>l[i]>>r[i];
        l[i]--;
        r[i]--;
        if((r[i]-l[i]+n)%n+1>mx)
        {
            mx=(r[i]-l[i]+n)%n+1;
            k=i;
        }
    }
    ans[k]=1;
    int t=l[k];
    for(int i=1;i<=m;i++)
    {
        l[i]=(l[i]-t+n)%n;
        r[i]=(r[i]-t+n)%n;
     //   cout<<l[i]<<" "<<r[i]<<endl;
        if(l[i]<=r[i])
        {
            ile[l[i]]++;
            ile[r[i]+1]--;
        }
        else
        {
            ile[l[i]]++;
            ile[0]++;
            ile[r[i]+1]--;
        }
    }
    int act=0;
    L[0]=-1;
    for(int i=0;i<n;i++)
    {
        act+=ile[i];
        if(act<2) gg();
        if(i>0) L[i]=L[i-1];
        if(act==2) L[i]=i;
    }
    for(int i=1;i<=m;i++) if(i!=k) V[l[i]].pb(i);
    for(int i=1;i<2*pot;i++) seg[i].st=inf;
    ins(1,L[r[k]]+2,r[k]+2,1,pot,r[k]+1,k);
    int X=-1;
    if(r[k]==n-1) X=k;
    for(int j=0;j<n;j++)
    {
        for(auto i:V[j])
        {
            pair<int,int>p=que(l[i]+1);
            o[i]=p.nd;
            if(o[i]!=0&&r[i]==n-1) X=i;
            if(l[i]<=r[i]) ins(1,max(p.st,L[r[i]]+1)+1,r[i]+2,1,pot,r[i]+1,i);
            else
            {
                if(o[i]!=0&&L[r[i]]==-1) X=i;
            }
        }
    }
    if(X==-1) gg();
    while(X>0)
    {
        ans[X]=1;
        X=o[X];
    }
    for(int i=1;i<=m;i++) cout<<ans[i];

    return 0;
}

Compilation message

alternating.cpp: In function 'int main()':
alternating.cpp:78:11: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
   78 |     ans[k]=1;
      |     ~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4692 KB Output is correct
2 Correct 2 ms 4692 KB Output is correct
3 Correct 2 ms 4692 KB Output is correct
4 Correct 3 ms 4740 KB Output is correct
5 Correct 2 ms 4692 KB Output is correct
6 Correct 3 ms 4692 KB Output is correct
7 Correct 3 ms 4740 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 3 ms 4692 KB Output is correct
10 Correct 2 ms 2692 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 4692 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 4740 KB Output is correct
15 Correct 3 ms 4692 KB Output is correct
16 Correct 3 ms 4732 KB Output is correct
17 Correct 3 ms 4732 KB Output is correct
18 Correct 3 ms 4692 KB Output is correct
19 Correct 2 ms 4736 KB Output is correct
20 Correct 2 ms 4740 KB Output is correct
21 Correct 2 ms 4692 KB Output is correct
22 Correct 3 ms 4692 KB Output is correct
23 Correct 3 ms 4692 KB Output is correct
24 Correct 3 ms 4692 KB Output is correct
25 Correct 2 ms 4732 KB Output is correct
26 Correct 2 ms 4692 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 4692 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 3 ms 4692 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 3 ms 4692 KB Output is correct
34 Correct 1 ms 2688 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 3 ms 4692 KB Output is correct
37 Correct 2 ms 4692 KB Output is correct
38 Correct 2 ms 2684 KB Output is correct
39 Correct 2 ms 4692 KB Output is correct
40 Correct 3 ms 4692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4692 KB Output is correct
2 Correct 2 ms 4692 KB Output is correct
3 Correct 2 ms 4692 KB Output is correct
4 Correct 3 ms 4740 KB Output is correct
5 Correct 2 ms 4692 KB Output is correct
6 Correct 3 ms 4692 KB Output is correct
7 Correct 3 ms 4740 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 3 ms 4692 KB Output is correct
10 Correct 2 ms 2692 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 4692 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 4740 KB Output is correct
15 Correct 3 ms 4692 KB Output is correct
16 Correct 3 ms 4732 KB Output is correct
17 Correct 3 ms 4732 KB Output is correct
18 Correct 3 ms 4692 KB Output is correct
19 Correct 2 ms 4736 KB Output is correct
20 Correct 2 ms 4740 KB Output is correct
21 Correct 2 ms 4692 KB Output is correct
22 Correct 3 ms 4692 KB Output is correct
23 Correct 3 ms 4692 KB Output is correct
24 Correct 3 ms 4692 KB Output is correct
25 Correct 2 ms 4732 KB Output is correct
26 Correct 2 ms 4692 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 4692 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 3 ms 4692 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 3 ms 4692 KB Output is correct
34 Correct 1 ms 2688 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 3 ms 4692 KB Output is correct
37 Correct 2 ms 4692 KB Output is correct
38 Correct 2 ms 2684 KB Output is correct
39 Correct 2 ms 4692 KB Output is correct
40 Correct 3 ms 4692 KB Output is correct
41 Correct 2 ms 4736 KB Output is correct
42 Correct 3 ms 4736 KB Output is correct
43 Correct 3 ms 4692 KB Output is correct
44 Correct 2 ms 4692 KB Output is correct
45 Correct 2 ms 4692 KB Output is correct
46 Correct 2 ms 4692 KB Output is correct
47 Correct 3 ms 4692 KB Output is correct
48 Correct 2 ms 2692 KB Output is correct
49 Correct 3 ms 4692 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2684 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 2 ms 2644 KB Output is correct
54 Correct 3 ms 4692 KB Output is correct
55 Correct 3 ms 4692 KB Output is correct
56 Correct 3 ms 4692 KB Output is correct
57 Correct 3 ms 4692 KB Output is correct
58 Correct 2 ms 4692 KB Output is correct
59 Correct 2 ms 4692 KB Output is correct
60 Correct 2 ms 4692 KB Output is correct
61 Correct 2 ms 4692 KB Output is correct
62 Correct 1 ms 2644 KB Output is correct
63 Correct 1 ms 2644 KB Output is correct
64 Correct 2 ms 4692 KB Output is correct
65 Correct 2 ms 2644 KB Output is correct
66 Correct 2 ms 4692 KB Output is correct
67 Correct 3 ms 4692 KB Output is correct
68 Correct 3 ms 4736 KB Output is correct
69 Correct 3 ms 4728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4692 KB Output is correct
2 Correct 2 ms 4692 KB Output is correct
3 Correct 2 ms 4692 KB Output is correct
4 Correct 3 ms 4740 KB Output is correct
5 Correct 2 ms 4692 KB Output is correct
6 Correct 3 ms 4692 KB Output is correct
7 Correct 3 ms 4740 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 3 ms 4692 KB Output is correct
10 Correct 2 ms 2692 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 4692 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 4740 KB Output is correct
15 Correct 3 ms 4692 KB Output is correct
16 Correct 3 ms 4732 KB Output is correct
17 Correct 3 ms 4732 KB Output is correct
18 Correct 3 ms 4692 KB Output is correct
19 Correct 2 ms 4736 KB Output is correct
20 Correct 2 ms 4740 KB Output is correct
21 Correct 2 ms 4692 KB Output is correct
22 Correct 3 ms 4692 KB Output is correct
23 Correct 3 ms 4692 KB Output is correct
24 Correct 3 ms 4692 KB Output is correct
25 Correct 2 ms 4732 KB Output is correct
26 Correct 2 ms 4692 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 4692 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 3 ms 4692 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 3 ms 4692 KB Output is correct
34 Correct 1 ms 2688 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 3 ms 4692 KB Output is correct
37 Correct 2 ms 4692 KB Output is correct
38 Correct 2 ms 2684 KB Output is correct
39 Correct 2 ms 4692 KB Output is correct
40 Correct 3 ms 4692 KB Output is correct
41 Correct 2 ms 4736 KB Output is correct
42 Correct 3 ms 4736 KB Output is correct
43 Correct 3 ms 4692 KB Output is correct
44 Correct 2 ms 4692 KB Output is correct
45 Correct 2 ms 4692 KB Output is correct
46 Correct 2 ms 4692 KB Output is correct
47 Correct 3 ms 4692 KB Output is correct
48 Correct 2 ms 2692 KB Output is correct
49 Correct 3 ms 4692 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2684 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 2 ms 2644 KB Output is correct
54 Correct 3 ms 4692 KB Output is correct
55 Correct 3 ms 4692 KB Output is correct
56 Correct 3 ms 4692 KB Output is correct
57 Correct 3 ms 4692 KB Output is correct
58 Correct 2 ms 4692 KB Output is correct
59 Correct 2 ms 4692 KB Output is correct
60 Correct 2 ms 4692 KB Output is correct
61 Correct 2 ms 4692 KB Output is correct
62 Correct 1 ms 2644 KB Output is correct
63 Correct 1 ms 2644 KB Output is correct
64 Correct 2 ms 4692 KB Output is correct
65 Correct 2 ms 2644 KB Output is correct
66 Correct 2 ms 4692 KB Output is correct
67 Correct 3 ms 4692 KB Output is correct
68 Correct 3 ms 4736 KB Output is correct
69 Correct 3 ms 4728 KB Output is correct
70 Correct 3 ms 4692 KB Output is correct
71 Correct 3 ms 4692 KB Output is correct
72 Correct 2 ms 4692 KB Output is correct
73 Correct 2 ms 4692 KB Output is correct
74 Correct 2 ms 4692 KB Output is correct
75 Correct 2 ms 4736 KB Output is correct
76 Correct 2 ms 2644 KB Output is correct
77 Correct 3 ms 4740 KB Output is correct
78 Correct 2 ms 2644 KB Output is correct
79 Correct 2 ms 2644 KB Output is correct
80 Correct 2 ms 2712 KB Output is correct
81 Correct 3 ms 4728 KB Output is correct
82 Correct 3 ms 4696 KB Output is correct
83 Correct 3 ms 4692 KB Output is correct
84 Correct 4 ms 4692 KB Output is correct
85 Correct 3 ms 4692 KB Output is correct
86 Correct 3 ms 4692 KB Output is correct
87 Correct 3 ms 4696 KB Output is correct
88 Correct 3 ms 4692 KB Output is correct
89 Correct 2 ms 2644 KB Output is correct
90 Correct 3 ms 4692 KB Output is correct
91 Correct 2 ms 4692 KB Output is correct
92 Correct 3 ms 4692 KB Output is correct
93 Correct 3 ms 4692 KB Output is correct
94 Correct 2 ms 2644 KB Output is correct
95 Correct 2 ms 4736 KB Output is correct
96 Correct 3 ms 4692 KB Output is correct
97 Correct 4 ms 4692 KB Output is correct
98 Correct 3 ms 4744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 6904 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 9 ms 3412 KB Output is correct
4 Correct 18 ms 7680 KB Output is correct
5 Correct 55 ms 9028 KB Output is correct
6 Correct 17 ms 5324 KB Output is correct
7 Correct 49 ms 10244 KB Output is correct
8 Correct 5 ms 5588 KB Output is correct
9 Correct 4 ms 5460 KB Output is correct
10 Correct 50 ms 10364 KB Output is correct
11 Correct 48 ms 9260 KB Output is correct
12 Correct 36 ms 10476 KB Output is correct
13 Correct 2 ms 2904 KB Output is correct
14 Correct 2 ms 2812 KB Output is correct
15 Correct 39 ms 9728 KB Output is correct
16 Correct 10 ms 4436 KB Output is correct
17 Correct 42 ms 9612 KB Output is correct
18 Correct 34 ms 7660 KB Output is correct
19 Correct 3 ms 3464 KB Output is correct
20 Correct 40 ms 9164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4692 KB Output is correct
2 Correct 2 ms 4692 KB Output is correct
3 Correct 2 ms 4692 KB Output is correct
4 Correct 3 ms 4740 KB Output is correct
5 Correct 2 ms 4692 KB Output is correct
6 Correct 3 ms 4692 KB Output is correct
7 Correct 3 ms 4740 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 3 ms 4692 KB Output is correct
10 Correct 2 ms 2692 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 4692 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 4740 KB Output is correct
15 Correct 3 ms 4692 KB Output is correct
16 Correct 3 ms 4732 KB Output is correct
17 Correct 3 ms 4732 KB Output is correct
18 Correct 3 ms 4692 KB Output is correct
19 Correct 2 ms 4736 KB Output is correct
20 Correct 2 ms 4740 KB Output is correct
21 Correct 2 ms 4692 KB Output is correct
22 Correct 3 ms 4692 KB Output is correct
23 Correct 3 ms 4692 KB Output is correct
24 Correct 3 ms 4692 KB Output is correct
25 Correct 2 ms 4732 KB Output is correct
26 Correct 2 ms 4692 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 4692 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 3 ms 4692 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 3 ms 4692 KB Output is correct
34 Correct 1 ms 2688 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 3 ms 4692 KB Output is correct
37 Correct 2 ms 4692 KB Output is correct
38 Correct 2 ms 2684 KB Output is correct
39 Correct 2 ms 4692 KB Output is correct
40 Correct 3 ms 4692 KB Output is correct
41 Correct 2 ms 4736 KB Output is correct
42 Correct 3 ms 4736 KB Output is correct
43 Correct 3 ms 4692 KB Output is correct
44 Correct 2 ms 4692 KB Output is correct
45 Correct 2 ms 4692 KB Output is correct
46 Correct 2 ms 4692 KB Output is correct
47 Correct 3 ms 4692 KB Output is correct
48 Correct 2 ms 2692 KB Output is correct
49 Correct 3 ms 4692 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2684 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 2 ms 2644 KB Output is correct
54 Correct 3 ms 4692 KB Output is correct
55 Correct 3 ms 4692 KB Output is correct
56 Correct 3 ms 4692 KB Output is correct
57 Correct 3 ms 4692 KB Output is correct
58 Correct 2 ms 4692 KB Output is correct
59 Correct 2 ms 4692 KB Output is correct
60 Correct 2 ms 4692 KB Output is correct
61 Correct 2 ms 4692 KB Output is correct
62 Correct 1 ms 2644 KB Output is correct
63 Correct 1 ms 2644 KB Output is correct
64 Correct 2 ms 4692 KB Output is correct
65 Correct 2 ms 2644 KB Output is correct
66 Correct 2 ms 4692 KB Output is correct
67 Correct 3 ms 4692 KB Output is correct
68 Correct 3 ms 4736 KB Output is correct
69 Correct 3 ms 4728 KB Output is correct
70 Correct 3 ms 4692 KB Output is correct
71 Correct 3 ms 4692 KB Output is correct
72 Correct 2 ms 4692 KB Output is correct
73 Correct 2 ms 4692 KB Output is correct
74 Correct 2 ms 4692 KB Output is correct
75 Correct 2 ms 4736 KB Output is correct
76 Correct 2 ms 2644 KB Output is correct
77 Correct 3 ms 4740 KB Output is correct
78 Correct 2 ms 2644 KB Output is correct
79 Correct 2 ms 2644 KB Output is correct
80 Correct 2 ms 2712 KB Output is correct
81 Correct 3 ms 4728 KB Output is correct
82 Correct 3 ms 4696 KB Output is correct
83 Correct 3 ms 4692 KB Output is correct
84 Correct 4 ms 4692 KB Output is correct
85 Correct 3 ms 4692 KB Output is correct
86 Correct 3 ms 4692 KB Output is correct
87 Correct 3 ms 4696 KB Output is correct
88 Correct 3 ms 4692 KB Output is correct
89 Correct 2 ms 2644 KB Output is correct
90 Correct 3 ms 4692 KB Output is correct
91 Correct 2 ms 4692 KB Output is correct
92 Correct 3 ms 4692 KB Output is correct
93 Correct 3 ms 4692 KB Output is correct
94 Correct 2 ms 2644 KB Output is correct
95 Correct 2 ms 4736 KB Output is correct
96 Correct 3 ms 4692 KB Output is correct
97 Correct 4 ms 4692 KB Output is correct
98 Correct 3 ms 4744 KB Output is correct
99 Correct 32 ms 6904 KB Output is correct
100 Correct 3 ms 5076 KB Output is correct
101 Correct 9 ms 3412 KB Output is correct
102 Correct 18 ms 7680 KB Output is correct
103 Correct 55 ms 9028 KB Output is correct
104 Correct 17 ms 5324 KB Output is correct
105 Correct 49 ms 10244 KB Output is correct
106 Correct 5 ms 5588 KB Output is correct
107 Correct 4 ms 5460 KB Output is correct
108 Correct 50 ms 10364 KB Output is correct
109 Correct 48 ms 9260 KB Output is correct
110 Correct 36 ms 10476 KB Output is correct
111 Correct 2 ms 2904 KB Output is correct
112 Correct 2 ms 2812 KB Output is correct
113 Correct 39 ms 9728 KB Output is correct
114 Correct 10 ms 4436 KB Output is correct
115 Correct 42 ms 9612 KB Output is correct
116 Correct 34 ms 7660 KB Output is correct
117 Correct 3 ms 3464 KB Output is correct
118 Correct 40 ms 9164 KB Output is correct
119 Correct 3 ms 5332 KB Output is correct
120 Correct 37 ms 9564 KB Output is correct
121 Correct 24 ms 7952 KB Output is correct
122 Correct 2 ms 4692 KB Output is correct
123 Correct 3 ms 3472 KB Output is correct
124 Correct 7 ms 3796 KB Output is correct
125 Correct 17 ms 6920 KB Output is correct
126 Correct 4 ms 5460 KB Output is correct
127 Correct 8 ms 5972 KB Output is correct
128 Correct 39 ms 9420 KB Output is correct
129 Correct 25 ms 8072 KB Output is correct
130 Correct 33 ms 8420 KB Output is correct
131 Correct 4 ms 5204 KB Output is correct
132 Correct 3 ms 5332 KB Output is correct
133 Correct 38 ms 9860 KB Output is correct
134 Correct 13 ms 6912 KB Output is correct
135 Correct 3 ms 5204 KB Output is correct
136 Correct 11 ms 4212 KB Output is correct
137 Correct 18 ms 4664 KB Output is correct
138 Correct 4 ms 5512 KB Output is correct