Codeforces 706D Vasiliy ' s Multiset (dictionary tree query + greedy)

Source: Internet
Author: User

Test instructions: At first, there is a collection, there is only one element in the collection 0, now there are Q operations, the operation is divided into 3 kinds:

+ x: Indicates that an element is added to the collection X

-x: Deletes an element in the collection that has a value of X

? X: Indicates the maximum number of values in the query collection that are different from X or

Analysis: This is a dictionary tree application, but did not see it .... The main idea is this, first with 10 binary number, turn into binary number, note the number of 0,1 of each node, so increase and delete, that is, 01 of the deletion,

The rest is the query, so try to make 0 and 1XOR is the largest, so, for a given number, we want to try his XOR number, if found on add, can not find, find the next. This is the biggest.

The code is as follows:

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < Stack>using namespace Std;typedef Long Long ll;typedef pair<int, int> p;const int INF = 0x3f3f3f3f;const double I NF = 0x3f3f3f3f3f3f;const LL LNF = 100000000000000000;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const int MAXN = 4e6 + 5;const int mod = 1e9 + 7;const char *mark = "+-*"; const int dr[] = {-1, 0, 1, 0};const int dc[] = {0, 1, 0, -1};i NT N, m;inline bool is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && c < m; }inline ll Max (ll A, ll b) {return a < b b:a;} inline ll Min (ll A, ll b) {return a > b b:a;} inline int Max (int a, int b) {return a < b b:a;} Inline int Min (int a, int b) {return a > b b:a;}    int ch[maxn][2];int T[maxn];int cnt = 0;void Add (int x) {int k = 1;        for (int i = +; I >= 0; i) {int j = ((1<<i) & X) > 0;        if (!ch[k][j]) ch[k][j] = ++cnt;        k = Ch[k][j];    ++T[K];    }}void del (int x) {int k = 1;        for (int i = +; I >= 0; i) {int j = ((1<<i) & X) > 0;        k = Ch[k][j];    --T[K];    }}int query (int x) {int k = 1;    int ans = 0;        for (int i = +; I >= 0; i) {int j = ((1<<i) & x) = = 0;            if (T[ch[k][j]]) {ans |= (1<<i);        k = Ch[k][j];    } else K = ch[k][1-j]; } return ans;        int main () {while (scanf ("%d", &n) = = 1) {cnt = 1;        memset (CH, 0, sizeof (CH));        memset (t, 0, sizeof (t));        Add (0);            while (n--) {char s[3];            int x;            scanf ("%s%d", S, &x);            if (s[0] = = ' + ') add (x); ElsE if (s[0] = = '-') del (x);        else printf ("%d\n", query (x)); }} return 0;}


Codeforces 706D Vasiliy ' s Multiset (dictionary tree query + greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.