# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1095053 |
2024-10-01T07:59:57 Z |
duyhoanho |
Joker (BOI20_joker) |
C++17 |
|
76 ms |
23300 KB |
#include<bits/stdc++.h>
#define int long long
#define forinc(i,a,b) for(int i=a;i<=b;i++)
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
using namespace std;
const int N = 2e5+10, S = 448;
int n,m,q,p[N],sz[N],lz[N],x[N],y[N];
int ans[N];
stack<pii> sk,pr,si,la;
struct node
{
int l,r,id;
} qu[N];
bool cmp(node a,node b)
{
if(a.l / S != b.l / S) return a.l < b.l;
return a.r > b.r;
}
int pa(int v,int &c)
{
c ^= lz[v];
if(p[v] == v) return v;
return pa(p[v],c);
}
void join(int a,int b)
{
int ca = 0;
int cb = 0;
a = pa(a,ca);
b = pa(b,cb);
if(a != b)
{
if(sz[a] < sz[b])
swap(a,b);
sk.push({a,b});
si.push({sz[a],sz[b]});
pr.push({p[a],p[b]});
la.push({lz[a],lz[b]});
sz[a] += sz[b];
p[b] = a;
if(ca == cb) lz[b] = 1;
}
}
void rollback(int x,int y)
{
if(sk.empty()) return;
auto[a,b] = sk.top();
if(min(a,b) != min(x,y) || max(a,b) != max(x,y)) return;
sk.pop();
auto[sza,szb] = si.top();
si.pop();
sz[a] = sza;
sz[b] = szb;
auto[pa,pb] = pr.top();
pr.pop();
p[a] = pa ;
p[b] = pb;
auto[lza,lzb] = la.top();
la.pop();
lz[a] = lza ;
lz[b] = lzb;
}
int get(int i)
{
return (i - 1) / S + 1;
}
bool check(int a,int b)
{
int ca = 0;
int cb = 0;
a = pa(a,ca);
b = pa(b,cb);
if(a == b && ca == cb) return true;
return false;
}
int32_t main()
{
if(fopen("joker.inp","r"))
{
freopen("joker.inp","r",stdin);
freopen("joker.out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>q;
for(int i=1; i<=m; i++) cin>>x[i]>>y[i];
forinc(i,1,q)
{
int l,r;
cin>>l>>r;
qu[i] = {l,r,i};
}
sort(qu+1,qu+q+1,cmp);
forinc(i,1,n)
{
sz[i] = 1;
lz[i] = 0;
p[i] = i;
}
for(int i=m; i>qu[1].r; i--)
{
if(check(x[i],y[i])) ans[qu[1].id] = 1;
join(x[i],y[i]);
}
for(int i=1; i<qu[1].l; i++)
{
if(check(x[i], y[i])) ans[qu[1].id] = 1;
join(x[i],y[i]);
}
for(int t=2; t<=q; t++)
{
// auto[l,r,id] = qu[t];
if(get(qu[t].l) == get(qu[t-1].l))
{
for(int i=1; i<qu[t-1].l; i++) rollback(x[i],y[i]);
for(int i=qu[t-1].r ; i > qu[t].r ; i--)
{
if(check(x[i],y[i])) ans[qu[t].id] = 1;
join(x[i],y[i]);
}
for(int i=1;i<qu[t].l;i++)
{
if(check(x[i],y[i])) ans[qu[t].id] = 1;
join(x[i],y[i]);
}
}
else
{
forinc(i,1,n)
{
sz[i] = 1;
lz[i] = 0;
p[i] = i;
}
while(sk.size())
{
sk.pop();
la.pop();
si.pop();
pr.pop();
}
for(int i=m; i>qu[t].r; i--)
{
if(check(x[i],y[i])) ans[qu[t].id] = 1;
join(x[i],y[i]);
}
for(int i=1; i<qu[t].l; i++)
{
if(check(x[i], y[i])) ans[qu[t].id] = 1;
join(x[i],y[i]);
}
}
}
forinc(i,1,q) cout<<(ans[i] == 1 ? "YES\n" : "NO\n");
}
Compilation message
Joker.cpp: In function 'int32_t main()':
Joker.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen("joker.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen("joker.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
76 ms |
23300 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |