Submission #1106861

# Submission time Handle Problem Language Result Execution time Memory
1106861 2024-10-31T08:09:02 Z hengliao Plus Minus (BOI17_plusminus) C++17
100 / 100
58 ms 8544 KB
#include <bits/stdc++.h>
#include <chrono>
#include <random>
using namespace std;

#define F first
#define S second
#define pb push_back
#define vll vector<ll>
#define pll pair<ll, ll>
typedef long long ll;

const ll mxN=1e5+5;
const ll mod=1e9+7;

vll con;

ll pw(ll a, ll b){
    a%=mod;
    ll re=1;
    for(ll i=0;i<63;i++){
        if(b&(1LL<<i)){
            re*=a;
            re%=mod;
        }
        a*=a;
        a%=mod;
    }
    return re;
}

ll id(ll tar){
    return lower_bound(con.begin(), con.end(), tar)-con.begin();
}

ll x[mxN];
ll y[mxN];
ll flag[mxN];
bool bad=0;
ll state[2*mxN];
ll sz;

void solve(){
	ll n, m, k;
    cin>>n>>m>>k;
    for(ll i=0;i<k;i++){
        char c;
        cin>>c;
        if(c=='+') flag[i]=1;
        else flag[i]=-1;
        cin>>x[i]>>y[i];
        con.pb(x[i]);
        con.pb(y[i]);
    }
    sort(con.begin(), con.end());
    con.erase(unique(con.begin(), con.end()), con.end());
    sz=con.size();
    for(ll i=0;i<sz;i++){
        state[i]=0;
    }
    bad=0;
    auto f=[&](ll idx, ll flg){
        if(idx%2==0){
            return -flg;// test
        }
        else{
            return flg;
        }
    };
    for(ll i=0;i<k;i++){
        ll re=f(x[i], flag[i]);
        if(state[id(y[i])]==0){
            state[id(y[i])]=re;
        }
        else if(state[id(y[i])]!=re){
            bad=1;
        }
    }
    ll ans=0;
    if(!bad){
        ll tep=m;
        for(ll i=0;i<sz;i++){
            if(state[i]!=0) tep--;
        }
        ans+=pw(2, tep);
        ll tep2=0;
        bool bad2=0;
        for(ll i=0;i<sz;i++){
            if(state[i]!=0){
                ll re=f(con[i], state[i]);
                if(tep2==0){
                    tep2=re;
                }
                else if(tep2!=re){
                    bad2=1;
                }
            }
        }
        if(!bad2){
            ans--;
            if(k==0) ans--;
            if(ans<0) ans+=mod;
        }
    }
    for(ll i=0;i<sz;i++){
        state[i]=0;
    }
    bad=0;
    for(ll i=0;i<k;i++){
        ll re=f(y[i], flag[i]);
        if(state[id(x[i])]==0){
            state[id(x[i])]=re;
        }
        else if(state[id(x[i])]!=re){
            bad=1;
        }
    }
    if(!bad){
        ll tep=n;
        for(ll i=0;i<sz;i++){
            if(state[i]!=0) tep--;
        }
        ans+=pw(2, tep);
        ans%=mod;
    }
    
    cout<<ans<<'\n';
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

	solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2520 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2520 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 2 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2384 KB Output is correct
15 Correct 1 ms 2528 KB Output is correct
16 Correct 42 ms 7628 KB Output is correct
17 Correct 34 ms 7628 KB Output is correct
18 Correct 42 ms 7628 KB Output is correct
19 Correct 34 ms 7556 KB Output is correct
20 Correct 34 ms 7648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2520 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2384 KB Output is correct
12 Correct 2 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2384 KB Output is correct
15 Correct 1 ms 2528 KB Output is correct
16 Correct 42 ms 7628 KB Output is correct
17 Correct 34 ms 7628 KB Output is correct
18 Correct 42 ms 7628 KB Output is correct
19 Correct 34 ms 7556 KB Output is correct
20 Correct 34 ms 7648 KB Output is correct
21 Correct 58 ms 7884 KB Output is correct
22 Correct 1 ms 2384 KB Output is correct
23 Correct 50 ms 7892 KB Output is correct
24 Correct 52 ms 7896 KB Output is correct
25 Correct 53 ms 7884 KB Output is correct
26 Correct 50 ms 8396 KB Output is correct
27 Correct 46 ms 8396 KB Output is correct
28 Correct 52 ms 8396 KB Output is correct
29 Correct 47 ms 8396 KB Output is correct
30 Correct 58 ms 8544 KB Output is correct