Submission #1300260

#TimeUsernameProblemLanguageResultExecution timeMemory
1300260danglayloi1Lasers (NOI19_lasers)C++20
100 / 100
350 ms39340 KiB
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using ll = long long;
const ll mod=1e9+7;
const int nx=1e6+5;
int L, n, m, ans=0, bit[nx];
vector<int> a[nx], rar;
vector<ii> seg;
ii cur;
void add(int x, int val)
{
    for(; x <= m; x+=x&-x)
        bit[x]+=val;
}
int get(int x)
{
    int res=0;
    for(; x > 0; x-=x&-x)
        res+=bit[x];
    return res;
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>L>>n;
    ans=L;
    for(int i = 1; i <= n; i++)
    {
        int k, le=1, ri=L;
        cin>>k;
        a[i].resize(k);
        for(int j = 0; j < k; j++)
            cin>>a[i][j], ri-=a[i][j];
        for(int j = 0; j <= k; j++)
        {
            if(le<=ri)
            {
                rar.emplace_back(le);
                rar.emplace_back(ri+1);
            }
            if(j<k) le+=a[i][j], ri+=a[i][j];
        }
    }
    sort(rar.begin(), rar.end());
    rar.erase(unique(rar.begin(), rar.end()), rar.end());
    m=rar.size();
    for(int i = 1; i <= n; i++)
    {
        int k=a[i].size(), le=1, ri=L;
        seg.clear();
        cur={-1, -1};
        for(int j = 0; j < k; j++)
            ri-=a[i][j];
        for(int j = 0; j <= k; j++)
        {
            if(le<=ri) seg.emplace_back(le, ri);
            if(j<k) le+=a[i][j], ri+=a[i][j];
        }
        sort(seg.begin(), seg.end());
        for(auto it:seg)
        {
            if(cur.fi==-1) cur=it;
            else if(it.fi>cur.se+1)
            {
                int l=upper_bound(rar.begin(), rar.end(), cur.fi)-rar.begin();
                int r=upper_bound(rar.begin(), rar.end(), cur.se+1)-rar.begin();
                add(l, 1);
                add(r, -1);
                cur=it;
            }
            else cur.se=max(cur.se, it.se);
        }
        if(cur.fi!=-1)
        {
            int l=upper_bound(rar.begin(), rar.end(), cur.fi)-rar.begin();
            int r=upper_bound(rar.begin(), rar.end(), cur.se+1)-rar.begin();
            add(l, 1);
            add(r, -1);
        }
    }
    for(int i = 1; i <= m; i++)
    {
        int len=(i==m)?1:rar[i]-rar[i-1];
        if(get(i)==n) ans-=len;
    }
    cout<<ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...