답안 #877705

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
877705 2023-11-23T13:03:02 Z josanneo22 Sails (IOI07_sails) C++17
100 / 100
41 ms 3160 KB
/*
Problem: IOI 2007 Sails
When:    2023-11-16 14:54:19
Author:  Ning07
*/

#pragma warning(suppress : 4996)
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;

void stupid() {
    int N; cin >> N;
    vector<int> flags(N), height(N);
    multiset<pair<i64, int>> S;
    int mh = 0; vector<int> mx(2e5 + 500);
    for (int i = 0; i < N; i++) {
        cin >> height[i] >> flags[i];
        mh = max(mh, height[i]);
        mx[1]++; mx[height[i] + 1]--;
    }
    for (int i = 1; i <= mh; i++) mx[i] += mx[i - 1];
    for (int i = 1; i <= mh; i++) if (mx[i] != 0) S.insert(make_pair(0, i));
    vector<int> ord(N);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](int x, int y) {
        return height[x] < height[y];
        });
    for (int p = 0; p < N; p++) {
        int i = ord[p];
        multiset<pair<i64, int>> copy;
        int cnt = flags[i];
        for (auto& v : S) {
            if (v.second >= height[i] + 1) copy.insert(make_pair(v.first, v.second));
            else if (mx[v.second] == v.first) copy.insert(make_pair(v.first, v.second));
            else if (cnt) { cnt--; copy.insert(make_pair(v.first + 1, v.second)); }
            else copy.insert(make_pair(v.first, v.second));
        } swap(S, copy);
    }
    i64 ans = 0;
    for (auto& v : S) ans += (v.first) * (v.first - 1) / 2;
    cout << ans << '\n';
}
int N, NN = 1 << 17, t[(1 << 17) + 5];
void modify(int i, int delta) {
    for (; i <= NN; i += i & -i) t[i] += delta;
}
int query(int i) {
    int sum = 0;
    for (; i; i -= i & -i) sum += t[i];
    return sum;
}
int find(int x) {
    int pos = 0;
    for (int i = NN; i; i >>= 1) {
        int j = pos + i;
        if (j <= NN && t[j] >= x) {
            pos = j;
            x -= t[j];
        }
    }
    return pos;
}

namespace fast {
#pragma GCC optimize(Ofast)
#pragma GCC optimization (unroll-loops)
#pragma GCC target(avx,avx2,fma)
#pragma GCC target(sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native)
#pragma GCC optimize(3)
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
}
using namespace fast;

inline string read_string() {
    char ch = getchar(); string st1 = "";
    while (!((ch >= 'A') && (ch <= 'Z'))) ch = getchar();
    while ((ch >= 'A') && (ch <= 'Z'))  st1 += ch, ch = getchar();
    return st1;
}

template<typename T> inline void re(T& x) { x = 0; T f = 1; char ch = getchar();  while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * (1 << 1) + x * (1 << 3) + (ch - 48); ch = getchar(); } x *= f; }
template<typename x, typename... y>void re(x& a, y&... b) { re(a); re(b...); }
template<typename T> inline void ps(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9)  ps(x / 10); putchar(x % 10 + '0'); }
template<typename x, typename... y>void ps(x& a, y&... b) { ps(a); putchar(' '); ps(b...); }
#define sp putchar(' ')
#define nl putchar('\n')
void smart() {

    re(N);

    vector<int> flags(N), height(N);
    for (int i = 0; i < N; i++)
        re(height[i],flags[i]);

    vector<int> ord(N);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](int x, int y) {
        return height[x] < height[y];
        });
    /*  假设现在的序列为6 6 6 5 5 4 2 1
        flag = 4
        我们加上了这个6 6 5 6 5 3 2
        但是同样的我们可以 6 6 6 5 5 3 2
        让第一个k大的是l,最后是r,
        需要加上的是need=height[i]-r
        需要加上的区间[l,l+need], [r+1,height[i]]
    */
    for (int x = 0; x < N; x++) {
        int i = ord[x];
        int pos = height[i] - flags[i] + 1;
        int val = query(pos);
        int l = find(val + 1) + 1, r = (val == 0) ? height[i] : find(val);
        modify(l, +1);   modify(l + r - pos + 1, -1);
        modify(r + 1, +1); modify(height[i] + 1, -1);
    }
    i64 ans = 0;
    for (int i = 1; i <= height[ord[N - 1]]; i++) {
        i64 t = query(i);
        ans += t * (t - 1) / 2;
    }
    ps(ans);
    //cout << get(1) << '\n';
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    //stupid();
    smart();
}

Compilation message

sails.cpp:7: warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
    7 | #pragma warning(suppress : 4996)
      | 
sails.cpp:67: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   67 | #pragma GCC optimization (unroll-loops)
      | 
sails.cpp:66:22: warning: '#pragma GCC optimize' is not a string or number [-Wpragmas]
   66 | #pragma GCC optimize(Ofast)
      |                      ^~~~~
sails.cpp:68:20: warning: '#pragma GCC option' is not a string [-Wpragmas]
   68 | #pragma GCC target(avx,avx2,fma)
      |                    ^~~
sails.cpp:69:20: warning: '#pragma GCC option' is not a string [-Wpragmas]
   69 | #pragma GCC target(sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native)
      |                    ^~~
sails.cpp:89:39: warning: bad option '-fwhole-program' to pragma 'optimize' [-Wpragmas]
   89 | #pragma GCC optimize("-fwhole-program")
      |                                       ^
sails.cpp:96:41: warning: bad option '-fstrict-overflow' to pragma 'optimize' [-Wpragmas]
   96 | #pragma GCC optimize("-fstrict-overflow")
      |                                         ^
sails.cpp:98:41: warning: bad option '-fcse-skip-blocks' to pragma 'optimize' [-Wpragmas]
   98 | #pragma GCC optimize("-fcse-skip-blocks")
      |                                         ^
sails.cpp:112:51: warning: bad option '-funsafe-loop-optimizations' to pragma 'optimize' [-Wpragmas]
  112 | #pragma GCC optimize("-funsafe-loop-optimizations")
      |                                                   ^
sails.cpp:118:27: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
  118 | inline string read_string() {
      |                           ^
sails.cpp:118:27: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
sails.cpp:118:27: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
sails.cpp:118:27: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
sails.cpp:125:41: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
  125 | template<typename T> inline void re(T& x) { x = 0; T f = 1; char ch = getchar();  while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * (1 << 1) + x * (1 << 3) + (ch - 48); ch = getchar(); } x *= f; }
      |                                         ^
sails.cpp:125:41: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
sails.cpp:125:41: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
sails.cpp:125:41: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
sails.cpp:126:57: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
  126 | template<typename x, typename... y>void re(x& a, y&... b) { re(a); re(b...); }
      |                                                         ^
sails.cpp:126:57: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
sails.cpp:126:57: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
sails.cpp:126:57: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
sails.cpp:127:40: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
  127 | template<typename T> inline void ps(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9)  ps(x / 10); putchar(x % 10 + '0'); }
      |                                        ^
sails.cpp:127:40: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
sails.cpp:127:40: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
sails.cpp:127:40: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
sails.cpp:128:57: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
  128 | template<typename x, typename... y>void ps(x& a, y&... b) { ps(a); putchar(' '); ps(b...); }
      |                                                         ^
sails.cpp:128:57: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
sails.cpp:128:57: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
sails.cpp:128:57: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
sails.cpp:131:12: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
  131 | void smart() {
      |            ^
sails.cpp:131:12: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
sails.cpp:131:12: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
sails.cpp:131:12: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
sails.cpp: In function 'void smart()':
sails.cpp:141:50: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
  141 |     sort(ord.begin(), ord.end(), [&](int x, int y) {
      |                                                  ^
sails.cpp:141:50: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
sails.cpp:141:50: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
sails.cpp:141:50: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
sails.cpp: At global scope:
sails.cpp:168:10: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
  168 | int main() {
      |          ^
sails.cpp:168:10: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
sails.cpp:168:10: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
sails.cpp:168:10: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 604 KB Output is correct
2 Correct 11 ms 1064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 1216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 2408 KB Output is correct
2 Correct 26 ms 2660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 2960 KB Output is correct
2 Correct 24 ms 2612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 3160 KB Output is correct
2 Correct 31 ms 2656 KB Output is correct