답안 #377888

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
377888 2021-03-15T12:15:33 Z ACmachine Examination (JOI19_examination) C++17
0 / 100
1193 ms 9680 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;

#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)

const double EPS = 1e-9;
const int MOD = 1e9+7;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};

void DBG(){cout << "]" << endl;}
template<typename T, typename ...U> void DBG(const T& head, const U... args){ cout << head << "; "; DBG(args...); }
#define dbg(...) cout << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__);
#define chk() cout << "Check at line(" << __LINE__ << ") hit." << endl;

template<class T, unsigned int U>
ostream& operator<<(ostream& out, const array<T, U> &v){out << "[";  REP(i, U) out << v[i] << ", ";  out << "]"; return out;}
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "[";  REP(i, v.size()) out << v[i] << ", ";  out << "]"; return out;}

template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
/*
template<typename T>
struct fenwick{
    vector<T> tree;
    int n;
    fenwick(int _n) : n(_n){
        tree.resize(n + 5, 0);
    }
    void upd(int pos, T val){
        pos++;
        for(int i = pos; i <= n; i += (i & -i))
            tree[i] += val;
    }
    T pq(int r){
        T res = 0;
        for(int i = r; i > 0; i -= (i & -i))
            res += tree[i];
        return res;
    }
    T query(int l, int r){
        return pq(r) - pq(l);
    }
};*/


template<typename T>
struct fenwick{
    vector<T> tree;
    int n;
    fenwick(){}
    fenwick(int _n) {
        n = _n;
        tree.resize(n+1, 0);
    }
    void upd(int x, T val){
        ++x;
        while(x <= n){
            tree[x] += val;
            x += (x & -x);
        }
    }
    T pref_query(int x){ // query 0 .. x], ++x because BIT is indexed from 1 internally
        ++x;
        T res = 0;
        while(x > 0){
            res += tree[x];
            x -= (x & -x);
        }
        return res;
    }
    T query(int l, int r){ // query [l, r)
        return pref_query(--r) - pref_query(--l);
    }
};
int main(){
 	ios_base::sync_with_stdio(false);
 	cin.tie(NULL); cout.tie(NULL);
    int n, q;
    cin >> n >> q;
    vector<array<int, 2>> points(n);
    vector<array<int, 2>> points2(n);
    vector<int> sum_comp(n);
    vector<int> sum(n);
    REP(i, n){
        cin >> points[i][0] >> points2[i][0];
        points[i][1] = points2[i][1] = i;
        sum_comp[i] = points[i][0] + points2[i][0];
        sum[i] = sum_comp[i];
    }
    const int block = 600;
    sort(all(points));
    sort(all(points2));
    vector<int> compressed_sum(n);
    vector<int> x_comp(n), y_comp(n);
    REP(i, n){
        x_comp[i] = points[i][0]; y_comp[i] = points2[i][0];
    }
    sort(all(sum_comp));
    x_comp.erase(unique(all(x_comp)), x_comp.end());
    y_comp.erase(unique(all(y_comp)), y_comp.end());
    sum_comp.erase(unique(all(sum_comp)), sum_comp.end());
    REP(i, n){
        compressed_sum[i] = lower_bound(all(sum_comp), sum[i]) - sum_comp.begin();
        points[i][0] = lower_bound(all(x_comp), points[i][0]) - x_comp.begin();
        points2[i][0] = lower_bound(all(y_comp), points2[i][0]) - y_comp.begin();
    }
    struct query{
        int x, y, z, id;
    };
    vector<query> queries(q);
    REP(i, q){
        cin >> queries[i].x >> queries[i].y >> queries[i].z;
        queries[i].id = i;
        queries[i].z = lower_bound(all(sum_comp), queries[i].z) - sum_comp.begin();
        queries[i].x = lower_bound(all(x_comp), queries[i].x) - x_comp.begin();
        queries[i].y = lower_bound(all(y_comp), queries[i].y) - y_comp.begin();
    }
    auto comp = [&](query &lhs, query &rhs){
        if(lhs.x / block == rhs.x / block){
            return lhs.y < rhs.y;
        }
        else{
            return lhs.x < rhs.x;
        }
    };
    sort(all(queries), comp);
    fenwick<int> ft(n + 10);
    vector<int> cnt(n, 0);
    int xpointer = n; int ypointer = n;
    auto move_x_left = [&](){
        xpointer--;
        int id = points[xpointer][1];
        cnt[id]++;
        if(cnt[id] == 2){
            ft.upd(compressed_sum[id], 1);
        }
    };
    auto move_x_right = [&](){
        int id = points[xpointer][1];
        cnt[id]--;
        if(cnt[id] == 1){
            ft.upd(compressed_sum[id], -1);
        }
        xpointer++;
    };
    auto move_y_left = [&](){
        ypointer--;
        int id = points2[ypointer][1];
        cnt[id]++;
        if(cnt[id] == 2){
            ft.upd(compressed_sum[id], 1);
        }
    };
    auto move_y_right = [&](){
        int id = points2[ypointer][1];
        cnt[id]--;
        if(cnt[id] == 1){
            ft.upd(compressed_sum[id], -1);
        }
        ypointer++;
    };
    vector<int> ans(q, 0);
    REP(i, queries.size()){
        auto que = queries[i];
        while(xpointer > que.x)
            move_x_left();
        while(xpointer < que.x)
            move_x_right();
        while(ypointer > que.y)
            move_y_left();
        while(ypointer < que.y)
            move_y_right();
        ans[que.id] = ft.query(que.z, n + 5);
    }
    REP(i, q){
        cout << ans[i] << "\n";
    }
    return 0;
}

Compilation message

examination.cpp: In function 'int main()':
examination.cpp:19:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<main()::query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
      |                                        ^
examination.cpp:21:18: note: in expansion of macro 'FOR'
   21 | #define REP(i,b) FOR(i,0,b,1)
      |                  ^~~
examination.cpp:198:5: note: in expansion of macro 'REP'
  198 |     REP(i, queries.size()){
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1193 ms 9680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1193 ms 9680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -