답안 #143542

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
143542 2019-08-14T14:24:51 Z icypiggy Rectangles (IOI19_rect) C++14
0 / 100
280 ms 306300 KB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <stack>
#include <assert.h>
using namespace std;
const int n_max = 2550; // make it not lag
vector<pair<int,bool>> h_seg[n_max][n_max];
vector<pair<int,bool>> v_seg[n_max][n_max];
vector<pair<int,int>> v_map2[n_max];

stack<int> s;
void gen_h_seg(vector<int> &x, int r) {
    s.push(0);
    for(int i=1; i<x.size(); i++) {
        while(!s.empty() && x[s.top()]<x[i]) {
            if(s.top()!=i-1) {
                h_seg[r][s.top()+1].push_back(make_pair(i-1, true));
                //cout << r << " " << s.top()+1 << " " << i-1 << "\n";
            }
            int t = x[s.top()];
            while(!s.empty() && x[s.top()]==t) {
                s.pop();
            }
        }
        if(!s.empty() && s.top()!=i-1) {
            h_seg[r][s.top()+1].push_back(make_pair(i-1, true));
            //cout << r << " " << s.top()+1 << " " << i-1 << "\n";
        }
        s.push(i);
    }
    while(!s.empty()) s.pop();
}
int fwbit[n_max][n_max];
int fwa[n_max][n_max];
int fw_pt = 0;
struct fenwick {
    int *BIT;
    int *a;
    int n;
    fenwick(): n(n_max) {}
    void reset() {
        BIT = fwbit[fw_pt];
        a = fwa[fw_pt];
        fw_pt++;
    }
    void update(int x, int delta)
    {
          for(; x <= n; x += x&-x)
            BIT[x] += delta;
    }
    int query(int x) {
         int sum = 0;
         for(; x > 0; x -= x&-x)
            sum += BIT[x];
         return sum;
    }
    int query(int x, int y) {
        return query(y)-query(x-1);
    }
};
void gen_v_seg(vector<int> &x, int r) {
    s.push(0);
    for(int i=1; i<x.size(); i++) {
        while(!s.empty() && x[s.top()]<x[i]) {
            if(s.top()!=i-1) {
                v_seg[r][s.top()+1].push_back(make_pair(i-1, true));
                //cout << r << " " << s.top()+1 << " " << i-1 << "\n";
            }
            int t = x[s.top()];
            while(!s.empty() && x[s.top()]==t) {
                s.pop();
            }
        }
        if(!s.empty() && s.top()!=i-1) {
            v_seg[r][s.top()+1].push_back(make_pair(i-1, true));
            //cout << r << " " << s.top()+1 << " " << i-1 << "\n";
        }
        s.push(i);
    }
    while(!s.empty()) s.pop();
}

long long count_rectangles(vector<vector<int>> a) {
    for(int i=0; i<a.size(); i++) {
        gen_h_seg(a[i],i);
    }
    for(int i=0; i<a[0].size(); i++) {
        vector<int> tmp;
        for(int j=0; j<a.size(); j++) {
            tmp.push_back(a[j][i]);
        }
        gen_v_seg(tmp, i);
    }
    for(int i=0; i<a.size(); i++) {
        for(int j=0; j<a[0].size(); j++) {
            sort(h_seg[i][j].begin(), h_seg[i][j].end());
            sort(v_seg[j][i].begin(), v_seg[j][i].end());
        }
    }

    fenwick f[a.size()];
    for(int i=0; i<a.size(); i++) {
        f[i].reset();
    }

    long long ans = 0;
    for(int j=0; j<a[0].size(); j++) {
        vector<pair<int,int>> updates;
        for(int i=0; i<a.size(); i++) {
            for(auto &p: h_seg[i][j]) {
                if(p.second) {
                    int down = i;
                    while(down+1!=a.size()) {
                        auto it = lower_bound(h_seg[down+1][j].begin(), h_seg[down+1][j].end(), make_pair(p.first,true));
                        if(it==h_seg[down+1][j].end()) break;
                        if(*it==make_pair(p.first, true)) {
                            it->second = false;
                            f[down].update(p.first, 1);
                            updates.push_back(make_pair(down,p.first));
                            down++;
                        } else {
                            break;
                        }
                    }
                }
            }
            swap(i,j);
            for(auto &p: v_seg[i][j]) {
                if(p.second) {
                    int down = i;
                    while(down+1!=a[0].size()) {
                        auto it = lower_bound(v_seg[down+1][j].begin(), v_seg[down+1][j].end(), make_pair(p.first,true));
                        if(it==v_seg[down+1][j].end()) break;
                        if(*it==make_pair(p.first, true)) {
                            it->second = false;
                            down++;
                        } else {
                            break;
                        }
                    }
                    v_map2[j].push_back(make_pair(p.first, down));
                    //cout << "i,j,p.first,down(v): " << i << " " << j << " " << p.first << " " << down << "\n";
                }
            }
            swap(i,j);
            for(int a=0; a<v_map2[i].size(); a++) {
                while(a<v_map2[i].size() && v_map2[i][a].second<j) {
                    swap(v_map2[i][a], v_map2[i][v_map2[i].size()-1]);
                    v_map2[i].pop_back();
                }
            }
            for(auto q: v_map2[i]) {
                int tmp = f[q.first].query(j,q.second);
                ans += tmp;
            }
        }
        for(auto p: updates) {
            f[p.first].update(p.second, -1);
        }
    }

    return ans;
}

Compilation message

rect.cpp: In function 'void gen_h_seg(std::vector<int>&, int)':
rect.cpp:16:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=1; i<x.size(); i++) {
                  ~^~~~~~~~~
rect.cpp: In function 'void gen_v_seg(std::vector<int>&, int)':
rect.cpp:65:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=1; i<x.size(); i++) {
                  ~^~~~~~~~~
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:86:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<a.size(); i++) {
                  ~^~~~~~~~~
rect.cpp:89:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<a[0].size(); i++) {
                  ~^~~~~~~~~~~~
rect.cpp:91:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j=0; j<a.size(); j++) {
                      ~^~~~~~~~~
rect.cpp:96:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<a.size(); i++) {
                  ~^~~~~~~~~
rect.cpp:97:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j=0; j<a[0].size(); j++) {
                      ~^~~~~~~~~~~~
rect.cpp:104:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<a.size(); i++) {
                  ~^~~~~~~~~
rect.cpp:109:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0; j<a[0].size(); j++) {
                  ~^~~~~~~~~~~~
rect.cpp:111:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0; i<a.size(); i++) {
                      ~^~~~~~~~~
rect.cpp:115:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                     while(down+1!=a.size()) {
                           ~~~~~~^~~~~~~~~~
rect.cpp:133:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                     while(down+1!=a[0].size()) {
                           ~~~~~~^~~~~~~~~~~~~
rect.cpp:148:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int a=0; a<v_map2[i].size(); a++) {
                          ~^~~~~~~~~~~~~~~~~
rect.cpp:149:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 while(a<v_map2[i].size() && v_map2[i][a].second<j) {
                       ~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 274 ms 305912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 274 ms 305912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 274 ms 305912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 274 ms 305912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 275 ms 306300 KB Output is correct
2 Correct 274 ms 306168 KB Output is correct
3 Correct 272 ms 305892 KB Output is correct
4 Correct 272 ms 305832 KB Output is correct
5 Incorrect 278 ms 306000 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 280 ms 305756 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 274 ms 305912 KB Output isn't correct
2 Halted 0 ms 0 KB -