Submission #995845

# Submission time Handle Problem Language Result Execution time Memory
995845 2024-06-10T02:05:29 Z cpptowin Experimental Charges (NOI19_charges) C++17
100 / 100
36 ms 11300 KB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 100010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
struct DSU
{
    vi par,sz,t;
    DSU(int n) : par(n),sz(n),t(n) {};
    void make(int u)
    {
        par[u] = u;
        sz[u] = 1;
        t[u] = 1e5 + 5;
    }
    int find(int u,int ti)
    {
        if(t[u] > ti) return u;
        return find(par[u],ti);
    }
    void Union(int u,int v,int ti)
    {
        u = find(u,ti);
        v = find(v,ti);
        if(u == v) return ;
        if(sz[u] < sz[v]) swap(u,v);
        sz[u] += sz[v];
        par[v] = u;
        minimize(t[v],ti);
    }
};
int n,q;
array<int,3> qry[maxn];
vii ke[maxn];
int val[maxn];
void dfs(int u,int par = 0)
{
    for(auto [v,w] : ke[u]) if(v != par and val[v] == -1)
    {
        if(w == 1) val[v] = val[u];
        else val[v] = 1 - val[u];
        dfs(v,u);
    }
}
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> q;
    DSU g(n + 5);
    fo(i,1,n) g.make(i);
    fo(i,1,q)
    {
        char t;
        cin >> t >> qry[i][1] >> qry[i][2];
        if(t != 'Q') qry[i][0] = 1;
        else qry[i][0] = 2;
        if(qry[i][0] != 2)
        {
            int val = t == 'A' ? -1 : 1;
            ke[qry[i][1]].pb(qry[i][2],val);
            ke[qry[i][2]].pb(qry[i][1],val);
            g.Union(qry[i][1],qry[i][2],i);
        }
    }
    memset(val,-1,sizeof val);
    fo(i,1,n) if(val[i] == -1) val[i] = 0,dfs(i);
    fo(i,1,q) if(qry[i][0] == 2)
    {
        auto [_,u,v] = qry[i];
        if(u == v)
        {
            cout << "R";en;
            continue;
        }
        int l = 1,r = q,res = q + 1;
        while(l <= r)
        {
            int mid = (l + r) >> 1;
            if(g.find(u,mid) == g.find(v,mid)) res = mid,r = mid - 1;
            else l = mid + 1;
        }

        if(res > i) cout << '?';
        else
        {
            if(val[u] == val[v]) cout << 'R';
            else cout << 'A';
        }
        en;
    }
}

Compilation message

charges.cpp:79:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   79 | main()
      | ^~~~
charges.cpp: In function 'int main()':
charges.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(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
charges.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 2 ms 3416 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 7380 KB Output is correct
2 Correct 21 ms 8652 KB Output is correct
3 Correct 22 ms 7884 KB Output is correct
4 Correct 25 ms 8404 KB Output is correct
5 Correct 25 ms 8660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 10072 KB Output is correct
2 Correct 24 ms 9052 KB Output is correct
3 Correct 28 ms 7140 KB Output is correct
4 Correct 30 ms 8280 KB Output is correct
5 Correct 29 ms 7772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 9816 KB Output is correct
2 Correct 27 ms 10332 KB Output is correct
3 Correct 34 ms 8904 KB Output is correct
4 Correct 32 ms 9564 KB Output is correct
5 Correct 32 ms 9556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 2 ms 3416 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 1 ms 3164 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 1 ms 3164 KB Output is correct
10 Correct 1 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 2 ms 3416 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 20 ms 7380 KB Output is correct
7 Correct 21 ms 8652 KB Output is correct
8 Correct 22 ms 7884 KB Output is correct
9 Correct 25 ms 8404 KB Output is correct
10 Correct 25 ms 8660 KB Output is correct
11 Correct 21 ms 10072 KB Output is correct
12 Correct 24 ms 9052 KB Output is correct
13 Correct 28 ms 7140 KB Output is correct
14 Correct 30 ms 8280 KB Output is correct
15 Correct 29 ms 7772 KB Output is correct
16 Correct 22 ms 9816 KB Output is correct
17 Correct 27 ms 10332 KB Output is correct
18 Correct 34 ms 8904 KB Output is correct
19 Correct 32 ms 9564 KB Output is correct
20 Correct 32 ms 9556 KB Output is correct
21 Correct 1 ms 3164 KB Output is correct
22 Correct 1 ms 3164 KB Output is correct
23 Correct 1 ms 3164 KB Output is correct
24 Correct 1 ms 3164 KB Output is correct
25 Correct 1 ms 3164 KB Output is correct
26 Correct 23 ms 11300 KB Output is correct
27 Correct 26 ms 9812 KB Output is correct
28 Correct 31 ms 9308 KB Output is correct
29 Correct 30 ms 9360 KB Output is correct
30 Correct 36 ms 9300 KB Output is correct