Submission #94274

# Submission time Handle Problem Language Result Execution time Memory
94274 2019-01-17T09:27:01 Z Kastanda Simple game (IZhO17_game) C++11
0 / 100
76 ms 49656 KB
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pb push_back
using namespace __gnu_pbds;
using namespace std;
template < typename T >
using ordered_set = tree < T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update >;
const int N = 100005, MXN = 1000006;
int n, q, A[N], cnt[2][MXN], st[MXN];
int qa[N], qb[N];
vector < int > U;
struct Fen
{
    int ts = 0;
    ordered_set < pair < int , int > > S[N + N];
    inline void Add(int i, int val)
    {
        for (i ++; i < N + N; i += i & -i)
            S[i].insert({val, ts ++});
    }
    inline void Del(int i, int val)
    {
        for (i ++; i < N + N; i += i & -i)
            S[i].erase(S[i].lower_bound({val, -1}));
    }
    inline int Get(int i, int val)
    {
        int ret = 0;
        for (i ++; i; i -= i & -i)
            ret += (int)S[i].size() - S[i].order_of_key({val, ts});
        return (ret);
    }
};
Fen F[2];
inline void Add(int w, int a, int b)
{
    F[w].Add(st[a] + cnt[w][a], b);
    cnt[w][a] ++;
}
inline void Del(int w, int a, int b)
{
    cnt[w][a] --;
    F[w].Del(st[a] + cnt[w][a], b);
}
int main()
{
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; i++)
        scanf("%d", &A[i]), U.pb(A[i]);

    for (int i = 1; i <= q; i++)
    {
        int tp;
        scanf("%d%d", &tp, &qa[i]);
        if (tp == 1)
            scanf("%d", &qb[i]), U.pb(qb[i]);
    }
    int r = 1; U.pb(-1);
    sort(U.begin(), U.end());
    for (int i = 1; i < MXN; i++)
    {
        st[i] = r;
        while (r < (int)U.size() && U[r] == i)
            r ++;
    }
    for (int i = 1; i <= n; i++)
    {
        if (i < n)
            Add(0, A[i], A[i + 1]);
        if (i > 1)
            Add(1, A[i], A[i - 1]);
    }
    for (int i = 1; i <= q; i++)
    {
        if (qb[i] == 0)
        {
            int lb = lower_bound(U.begin(), U.end(), qa[i]) - U.begin() - 1;
            printf("%d\n", F[0].Get(lb, qa[i]) + F[1].Get(lb, qa[i]));
            continue;
        }
        int id = qa[i], val = qb[i];

        if (id < n)
        {
            Del(0, A[id], A[id + 1]);
            Del(1, A[id + 1], A[id]);
        }
        if (id > 1)
        {
            Del(1, A[id], A[id - 1]);
            Del(0, A[id - 1], A[id]);
        }

        A[id] = val;

        if (id < n)
        {
            Add(0, A[id], A[id + 1]);
            Add(1, A[id + 1], A[id]);
        }
        if (id > 1)
        {
            Add(1, A[id], A[id - 1]);
            Add(0, A[id - 1], A[id]);
        }
    }
    return 0;
}

Compilation message

game.cpp: In function 'int main()':
game.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &q);
     ~~~~~^~~~~~~~~~~~~~~~
game.cpp:50:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &A[i]), U.pb(A[i]);
         ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
game.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &tp, &qa[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~
game.cpp:57:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &qb[i]), U.pb(qb[i]);
             ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 50 ms 41848 KB Output is correct
2 Correct 73 ms 49656 KB Output is correct
3 Incorrect 76 ms 49400 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 50 ms 41848 KB Output is correct
2 Correct 73 ms 49656 KB Output is correct
3 Incorrect 76 ms 49400 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 50 ms 41848 KB Output is correct
2 Correct 73 ms 49656 KB Output is correct
3 Incorrect 76 ms 49400 KB Output isn't correct
4 Halted 0 ms 0 KB -