ZOJ-3635 Cinema in Akiba (tree array + dichotomy)

Source: Internet
Author: User

Test instructions: Known to have n individuals, starting from the first person each person is arranged on the AI empty seat, there is M group inquiry, ask someone to sit position.

Analysis:

1, the tree-like array to maintain the number of empty seats, method:

Initializing all empty seats to 1,sum (x) indicates the number of seats from seat 1 to seat x empty seat.

2, for each person, according to sum (mid), two points for sum (mid) greater than or equal to A[i] the smallest mid, that is, the position of the AI empty block, and add 1 to that position, then the value of that position becomes 0, thus not participating in the statistics of the number of empty seats.

3, Vis[q] is the position of the person who is labeled Q.

#include <cstdio> #include <cstring> #include <cstdlib> #include <cctype> #include <cmath > #include <iostream> #include <sstream> #include <iterator> #include <algorithm> #include <string> #include <vector> #include <set> #include <map> #include <stack> #include < deque> #include <queue> #include <list> #define LOWBIT (x) (X & (x)) const DOUBLE EPS = 1e-8;inline int    DCMP (double A, double b) {if (Fabs (a) < EPS) return 0; Return a > B? 1:-1;} typedef long Long Ll;typedef unsigned long long ull;const int int_inf = 0x3f3f3f3f;const int int_m_inf = 0x7f7f7f7f;const ll ll_inf = 0x3f3f3f3f3f3f3f3f;const LL ll_m_inf = 0x7f7f7f7f7f7f7f7f;const int dr[] = {0, 0,-1, 1,-1,-1, 1, 1};const I NT Dc[] = {-1, 1, 0, 0,-1, 1,-1, 1};const int MOD = 1e9 + 7;const Double pi = ACOs ( -1.0); const int MAXN = 50000 + 10;con St int MAXT = 10000 + 10;using namespace Std;int vis[maxn];int a[maxn];int z[maxn];int n;int sum (int x) {int ans = 0;    for (int i = x; I >= 1; I-= Lowbit (i)) {ans + = z[i]; } return ans;    void Add (int x, int value) {for (int i = x; i <= n; i + = Lowbit (i)) {Z[i] + = value;    }}int solve (int x) {int L = 1, r = N;        while (L < r) {int mid = L + (r-l)/2;        if (sum (mid) >= x) r = Mid;    else L = mid + 1; } return R;}        int main () {while (scanf ("%d", &n) = = 1) {memset (Vis, 0, sizeof vis);            for (int i = 1; I <= n; ++i) {scanf ("%d", &a[i]);        Add (i, 1);            } for (int i = 1; I <= n; ++i) {vis[i] = solve (A[i]);        Add (Vis[i],-1);        } int m;        scanf ("%d", &m);        BOOL flag = TRUE;            while (m--) {int q;            scanf ("%d", &q);            if (flag) flag = false;            else printf ("");        printf ("%d", vis[q]);    } printf ("\ n"); } return 0;}

  

ZOJ-3635 Cinema in Akiba (tree array + dichotomy)

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: info-contact@alibabacloud.com 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.