NOIP 2017 列队 (线段树动态开点)

Description

既然众所周知我就不多说了

Solution

考虑对于每一行的前 (m1) 个点建一棵线段树,最后一列单独建一棵线段树,每次操作在线段树中修改后把接在后面的人的编号丢到一个vector 里就行了。至于空间,动态开点即可。

Code

//Au: Hany01

#include

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define For(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define fir first
#define sec second
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARvctS__)
#else
#define debug(...)
#endif

template <typename T> inline bool chkmax(T &a , T b) { return a < b ? (a = b , 1) : 0; }
template <typename T> inline bool chkmin(T &a , T b) { return b < a ? (a = b , 1) : 0; }

int _ , __;
char c_;
inline int read()
{
    for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifdef hany01
    freopen("phalanx.in" , "r" , stdin);
    freopen("phalanx.out" , "w" , stdout);
#endif
}

const int maxn = 300003, maxm = 10000003;

int n, m, q, lc[maxm], rc[maxm], s[maxm], N, rt[maxn], cnt;
ll tmp;
vector vct[maxn];

ll qry(int l, int r, int t, int rnk)
{
    if (l == r) return l;
    register int mid = (l + r) >> 1;
    int res = mid - l + 1 - s[lc[t]];
    if (res >= rnk) return qry(l, mid, lc[t], rnk);
    return qry(mid + 1, r, rc[t], rnk - res);
}

void upd(int l, int r, int &t, int tmp)
{
    if (!t) t = ++ cnt;
    ++ s[t];
    if (l == r) return ;
    register int mid = (l + r) >> 1;
    if (mid >= tmp) upd(l, mid, lc[t], tmp); else upd(mid + 1, r, rc[t], tmp);
}

inline ll del_line(int x, int y)
{
    register ll tmp = qry(1, N, rt[x], y);
    upd(1, N, rt[x], tmp);
    return tmp < m ? (x - 1) * 1ll * m + tmp : vct[x][tmp - m];
}

inline ll del_row(int x)
{
    register ll tmp = qry(1, N, rt[n + 1], x);
    upd(1, N, rt[n + 1], tmp);
    return tmp <= n ? tmp * 1ll * m : vct[n + 1][tmp - n - 1];
}

int main()
{
    File();
    n = read(); m = read(); q = read(); N = max(n, m) + q;
    while (q --)
    {
        register int x, y;
        x = read(); y = read();
        if (y == m) tmp = del_row(x), vct[n + 1].pb(tmp), printf("%lld\n", tmp);
        else {
            tmp = del_line(x, y), vct[n + 1].pb(tmp);
            printf("%lld\n", tmp);
            tmp = del_row(x), vct[x].pb(tmp);
        }
    }
    return 0;
}
//今朝郡斋冷,忽念山中客。
//涧底束荆薪,归来煮白石。
//欲恃一瓢酒,远慰风雨夕。
//落叶满空山,何处寻行迹。
//--韦应物《寄全椒山中道士》

你可能感兴趣的:(线段树)