E. Addition on Segments+Avito Code Challenge 2018 树分治

  • E. Addition on Segments
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Grisha come to a contest and faced the following problem.

    You are given an array of size n

    , initially consisting of zeros. The elements of the array are enumerated from 1 to n. You perform q operations on the array. The i-th operation is described with three integers li, ri and xi (1lirin, 1xin) and means that you should add xi to each of the elements with indices li,li+1,,ri

    . After all operations you should find the maximum in the array.

    Grisha is clever, so he solved the problem quickly.

    However something went wrong inside his head and now he thinks of the following question: "consider we applied some subset of the operations to the array. What are the possible values of the maximum in the array?"

    Help Grisha, find all integers y

    between 1 and n such that if you apply some subset (possibly empty) of the operations, then the maximum in the array becomes equal to y

    .

    Input

    The first line contains two integers n

    and q (1n,q104

    ) — the length of the array and the number of queries in the initial problem.

    The following q

    lines contain queries, one per line. The i-th of these lines contains three integers li, ri and xi (1lirin, 1xin), denoting a query of adding xi to the segment from li-th to ri

    -th elements of the array, inclusive.

    Output

    In the first line print the only integer k

    , denoting the number of integers from 1 to n

    , inclusive, that can be equal to the maximum in the array after applying some subset (possibly empty) of the given operations.

    In the next line print these k

    integers from 1 to n

     — the possible values of the maximum. Print these integers in increasing order.

    Examples
    Input
    Copy
    4 3
    1 3 1
    2 4 2
    3 4 4
    
    Output
    Copy
    4
    1 2 3 4 
    
    Input
    Copy
    7 2
    1 5 1
    3 7 2
    
    Output
    Copy
    3
    1 2 3 
    
    Input
    Copy
    10 3
    1 1 2
    1 1 3
    1 1 6
    
    Output
    Copy
    6
    2 3 5 6 8 9 
    
    Note

    Consider the first example. If you consider the subset only of the first query, the maximum is equal to 1

    . If you take only the second query, the maximum equals to 2. If you take the first two queries, the maximum becomes 3. If you take only the fourth query, the maximum becomes 4. If you take the fourth query and something more, the maximum becomes greater that n

    , so you shouldn't print it.

    In the second example you can take the first query to obtain 1

    . You can take only the second query to obtain 2. You can take all queries to obtain 3

    .

    In the third example you can obtain the following maximums:

    • You can achieve the maximim of 2
    by using queries: (1)
  • .
  • You can achieve the maximim of 3 by using queries:
(2). You can achieve the maximim of 5 by using queries: (1,2). You can achieve the maximim of 6 by using queries: (3). You can achieve the maximim of 8 by using queries: (1,3). You can achieve the maximim of 9

by using queries: (2,3).

#define happy

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;


#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)

#define all(a) (a).begin(),(a).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second



const ll INF=1e18;
const int MOD=1e9+7;
const int N=1e4+10;

ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

typedef bitset<10002> state;
state tmp[N*4],cur,ret;
vi nd[N*4];

void modify(int p,int l,int r,int tl,int tr,int v){
    if(tl==l&&tr==r)nd[p].pb(v);
    else{
        int md=(l+r)>>1;
        if(tr<=md)modify(p+p,l,md,tl,tr,v);
        else if(tl>md)modify(p+p+1,md+1,r,tl,tr,v);
        else modify(p+p,l,md,tl,md,v),modify(p+p+1,md+1,r,md+1,tr,v);
    }
}

void solve(int p,int l,int r){
    tmp[p]=cur;
    for(auto l:nd[p])cur|=cur<<l;
    if(l==r){
        ret|=cur;
    }else{
        int md=(l+r)>>1;
        solve(p+p,l,md);
        solve(p+p+1,md+1,r);
    }
    cur=tmp[p];
}


int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    int n=rd(),q=rd();
    rep(i,1,q){
        int l=rd(),r=rd(),c=rd();
        modify(1,1,n,l,r,c);
    }
    cur[0]=1;
    solve(1,1,n);
    int ans=0;
    rep(i,1,n)if(ret[i])ans++;
    printf("%d\n",ans);
    rep(i,1,n)if(ret[i])printf("%d ",i);
}

猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/80487311