[JLOI2011] inequalities [+ Fenwick tree + discrete boundary conditions to deal with the question]

Topic Link


  First of all, this question let me know to deal with the details of the boundary conditions, so know these functions:

Rounded down floor ()

Improvement ToSei ceil ()

Rounded to the nearest integer round ()

  Then go into detail about the various problems of the title, first, the "single inequality" - defined as the title of "coefficient x may be 0" , this time directly determines "b> c" to the correctness.

  Secondly, it is easy to think of, a 0 and a non-zero points but also positive and negative points, it follows that the classification is discussed.

a > 0

x > \frac{c - b}{a}Then there are x \geq \left \lfloor \frac{c - b}{a} \right \rfloor + 1(note that the rounded down symbol)

a < 0

x < \frac{c - b}{a}So the same, if we use allmeans that all "a <0" number of elements in the collection, then we use the inverse problem of whether to set up sexual turn can know: the answer is equal to the original all - f(x \geq \frac{c - b}{a} ), which f(x)represents the number of x in the establishment of the original collection . It is also equal all - f(x \geq\left \lceil \frac{c - b}{a} \right \rceil )(note that symbol rounded up).

  Well, this time the problem can be solved with a two Fenwick tree.

note

Some people (who are not, that is, I) could write so rounding up and rounding down, is wrong, because the value here is not necessarily a positive number, only when positive numbers only meet such wording:

inline int More_UP(int a, int b, int c) { return (c - b) / a + 1; }
inline int Less_Down(int a, int b, int c) { return (b - c + a - 1) / a; }

This may be only 30 minutes I started because of it ooo, ooo, then made two examples, to understand the problems of the process:

2
A -12 15 3
Q 1
ans:0
2
A -11 -15 -4
Q -1
ans:0

Well, the last time, the code on the matter, see a lot of small partners are looked at with special sentenced solution to a problem other way to solve this problem, but I think I might think of first is discrete it may not have time ...... See sake data range.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define eps 1e-8
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
inline int More_UP(int a, int b, int c) { return floor(1. * (c - b) / a) + 1; }
inline int Less_Down(int a, int b, int c) { return ceil(1. * (c - b) / a); }
int Q, _UP;
int Lsan[maxN];
struct BIT_Tree
{
    int tree[maxN], all;
    inline void update(int x, int v)
    {
        while(x <= _UP)
        {
            tree[x] += v;
            x += lowbit(x);
        }
    }
    inline int query(int x)
    {
        int sum = 0;
        while(x)
        {
            sum += tree[x];
            x -= lowbit(x);
        }
        return sum;
    }
} b[2];
int real[maxN];
struct Question
{
    int op, val;    //"-1":del, "0":query, "1":add, "2":add && a==0
    Question(int a=0, int b=0):op(a), val(b) {}
} ques[maxN];
int Stap[maxN], Stop = 0;
bool have_del[maxN] = {false};
int main()
{
    scanf("%d", &Q);
    char op[10]; _UP = 0;
    int flag = 0;
    for(int i=1, a, b, c; i<=Q; i++)
    {
        scanf("%s", op);
        if(op[0] == 'A')
        {
            scanf("%d%d%d", &a, &b, &c);
            if(!a)
            {
                ques[i] = Question(2, b > c);
            }
            else
            {
                ques[i] = Question(1, (a > 0 ? More_UP(a, b, c) : Less_Down(a, b, c)));
                Lsan[++_UP] = ques[i].val;
            }
            real[i] = a > 0;
        }
        else if(op[0] == 'D')
        {
            scanf("%d", &a);
            ques[i] = Question(-1, a);
        }
        else
        {
            scanf("%d", &a);
            ques[i] = Question(0, a);
            Lsan[++_UP] = a;
        }
    }
    sort(Lsan + 1, Lsan + _UP + 1);
    _UP = (int)(unique(Lsan + 1, Lsan + _UP + 1) - Lsan - 1);
    for(int i=1; i<=Q; i++)
    {
        if(ques[i].op == 0 || ques[i].op == 1) ques[i].val = (int)(lower_bound(Lsan + 1, Lsan + _UP + 1, ques[i].val) - Lsan);
    }
    b[0].all = 0;
    for(int i=1, id; i<=Q; i++)
    {
        if(ques[i].op == 1)
        {
            if(real[i])
            {
                b[1].update(ques[i].val, 1);
            }
            else
            {
                b[0].all++;
                b[0].update(ques[i].val, 1);
            }
            Stap[++Stop] = i;
        }
        else if(ques[i].op == 2)
        {
            flag += ques[i].val;
            Stap[++Stop] = i;
        }
        else if(ques[i].op == -1)
        {
            id = Stap[ques[i].val];
            if(have_del[id]) continue;
            have_del[id] = true;
            if(ques[id].op == 2)
            {
                flag -= ques[id].val;
            }
            else if(real[id])
            {
                b[1].update(ques[id].val, -1);
            }
            else
            {
                b[0].all--;
                b[0].update(ques[id].val, -1);
            }
        }
        else
        {
            printf("%d\n", flag + b[1].query(ques[i].val) + b[0].all - b[0].query(ques[i].val));
        }
    }
    return 0;
}

Finally, finally, I would say look, this road is really very interesting subject.

Published 884 original articles · won praise 1057 · Views 120,000 +

Guess you like

Origin blog.csdn.net/qq_41730082/article/details/105225899