Submission #285638

# Submission time Handle Problem Language Result Execution time Memory
285638 2020-08-29T11:08:39 Z evpipis Sails (IOI07_sails) C++11
100 / 100
98 ms 5880 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;

const int len = 1e5+5,  mx = 1e5;
int tree[len];
ii arr[len];
multiset<int> mys;

void add(int i, int v){
    while (i <= mx)
        tree[i] += v, i += i&(-i);
}

int ask(int i){
    int ans = 0;
    while (i > 0)
        ans += tree[i], i -= i&(-i);
    return ans;
}

void print(){
    printf("tree now:\n");
    for (int i = 1; i <= 5; i++)
        printf("%d ", ask(i));
    printf("\n");

    printf("multiset now:\n");
    for (auto cur: mys)
        printf("%d ", cur);
    printf("\n");
}

void upd(int a, int b){
    //printf("upd: a = %d, b = %d\n", a, b);
    if (a > 1)
        mys.erase(mys.find(a));
    mys.insert(b+1);

    add(a, +1);
    add(b+1, -1);
}

int main(){
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d %d", &arr[i].fi, &arr[i].se);
    sort(arr, arr+n);

    mys.insert(1);
    for (int i = 0; i < n; i++){
        int h = arr[i].fi, s = arr[i].se;

        int st = h-s+1, rem = s;
        auto it = mys.lower_bound(st);
        int cur = -1, pre = -1;
        if (it != mys.end()) cur = *it;
        if (it == mys.end() || *it > st) pre = *(--it);

        if (cur != -1)
            rem -= (h-cur+1), upd(cur, h);

        //printf("rem = %d\n", rem);
        if (pre != -1){
            upd(pre, pre+rem-1);
        }

        //printf("i = %d, h = %d, s = %d\n", i, h, s);
        //print();
    }

    ll ans = 0;
    for (int i = 1; i <= mx; i++){
        int temp = ask(i);
        ans += temp*1LL*(temp-1);
    }

    printf("%lld\n", ans/2LL);
    return 0;
}

Compilation message

sails.cpp: In function 'int main()':
sails.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   51 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
sails.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   53 |         scanf("%d %d", &arr[i].fi, &arr[i].se);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 768 KB Output is correct
2 Correct 23 ms 1784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 2424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 2168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 3832 KB Output is correct
2 Correct 55 ms 3032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 5880 KB Output is correct
2 Correct 41 ms 2168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 3832 KB Output is correct
2 Correct 62 ms 2552 KB Output is correct