QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#471285 | #8572. Passing Game | ucup-team2307# | WA | 0ms | 9824kb | C++20 | 3.8kb | 2024-07-10 20:19:49 | 2024-07-10 20:19:49 |
Judging History
answer
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
typedef long long ll;
#define int ll
using namespace std;
struct Op
{
int add;
static Op noop() {return {0};}
void apply(Op op)
{
add+=op.add;
}
};
struct Val
{
int mn,cnt;
static Val empty() {return {int(1e9),0};}
static Val combine(Val x,Val y)
{
if(x.mn<y.mn) return x;
if(x.mn>y.mn) return y;
return {x.mn,x.cnt+y.cnt};
}
void apply(Op op)
{
mn+=op.add;
}
int zeros()
{
return mn==0?cnt:0;
}
};
struct Node {
Node *l = 0, *r = 0;
int lo, hi; Val val; Op op = Op::noop();
Node(int lo, int hi, auto &&init): lo(lo), hi(hi) {//explicit
if (lo + 1 < hi) {
int mid = lo + (hi - lo) / 2;
l = new Node(lo, mid, init); r = new Node(mid, hi, init);
val = Val::combine(l->val, r->val);
} else val = init(lo);
}
Val query(int L, int R) {
if (R <= lo || hi <= L) return Val::empty();
if (L <= lo && hi <= R) return val;
push();
return Val::combine(l->query(L, R), r->query(L, R));
}
void update(int L, int R, Op upd) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) val.apply(upd), op.apply(upd);
else {
push(), l->update(L, R, upd), r->update(L, R, upd);
val = Val::combine(l->val, r->val);
}
}
void push() {
l->update(lo, hi, op); r->update(lo, hi, op);
op = Op::noop();
}
};
int n;
int p[500500];
int q[500500];
int fd[500500];
int bd[500500];
set<pair<int, int> > ed;
Node* tree;
bool ERASE(int x, int y)
{
if (x > y)
swap(x, y);
if (ed.count({x, y}))
{
ed.erase({x, y});
return true;
}
return false;
}
void SWAP(int x, int y)
{
// cout<<"SWAP "<<x<<":"<<p[x]<<" "<<y<<":"<<p[y]<<"\n";
swap(p[x], p[y]);
q[p[x]] = x;
q[p[y]] = y;
tree->update(x, x+1, {fd[y]+bd[x]-bd[y]-fd[x]});
swap(fd[x], fd[y]);
swap(bd[x], bd[y]);
// for (int i=1; i<=n; i++)
// cout<<i<<" "<<bd[i]<<" "<<fd[i]<<"\n";
// for (int i=1; i+1<=n; i++)
// cout<<tree->query(i, i+1).mn<<" ";
if (y+1 <= n && ERASE(p[y], p[y+1]))
{
fd[y]--;
bd[y+1]--;
tree->update(y, y+1, {-1});
SWAP(y, y+1);
}
if (x-1 >=1 && ERASE(p[x-1], p[x]))
{
fd[x-1]--;
bd[x]--;
tree->update(x-1, x, {-1});
SWAP(x-1, x);
}
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(time(0));
int Q;
cin>>n>>Q;
for (int i=1; i<=n; i++)
p[i] = q[i] = i;
tree=new Node(1,n,[](int){return Val{0,1};});
while (Q--)
{
int x, y;
cin>>x>>y;
if (q[x] + 1 == q[y] || q[y] + 1 == q[x])
SWAP(min(q[x], q[y]), max(q[x], q[y]));
else
{
int val = 0;
if (ed.count({x, y}))
ed.erase({x, y}), val = -1;
else
ed.insert({x, y}), val = 1;
x = q[x];
y = q[y];
if (x > y) swap(x, y);
tree->update(x, y, {val});
// cout<<"upd "<<x<<" "<<y<<" "<<val<<"\n";
fd[x]+=val;
bd[y]+=val;
}
// for (int i=1; i<=n; i++)
// cout<<i<<" "<<bd[i]<<" "<<fd[i]<<"\n";
// for (int i=1; i+1<=n; i++)
// cout<<tree->query(i, i+1).mn<<" ";
// cout<<"\n";
// cout<<"ans : ";
cout<<1+tree->query(1,n).zeros()<<"\n";
}
// tree->query(1,2).zeros();
// tree->update(1,2,{3});
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 9824kb
input:
2 4 2 3 2 1 6 3 1 1 3 2 0 1 2 1 2
output:
1 2 2 2
result:
wrong answer 1st numbers differ - expected: '7', found: '1'