Submission #492660

# Submission time Handle Problem Language Result Execution time Memory
492660 2021-12-08T10:51:39 Z lukameladze Distributing Candies (IOI21_candies) C++17
Compilation error
0 ms 0 KB
# include <bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pb push_back
#define pii pair <int, int> 
using namespace std;
const int N = 3e5 + 5;
int n,q,l[N],r[N],val[N],cc,idx,c[N],le,ri,mid,mxid,mnid,ans;
vector < pii > v[N];
int lazy[4*N];
struct nd {
    int mx;
    int mn;
    int mxidx;
    int mnidx;
    int sum;
};
nd tree[4*N],emp;
nd merge(nd a, nd b) {
    nd ans;
    ans.mx = max(a.mx,b.mx);
    ans.mn = min(a.mn,b.mn);
    if (a.mx >= b.mx) ans.mxidx = a.mxidx;
    else ans.mxidx = b.mxidx;
    if (a.mn <= b.mn) ans.mnidx = a.mnidx;
    else ans.mnidx = b.mnidx;
    ans.sum = a.sum + b.sum;
    return ans;
}
void build(int node, int le, int ri) {
    if (le == ri) {
        tree[node].mx = 0;
        tree[node].mn = 0;
        tree[node].mxidx = le;
        tree[node].mnidx = le;
        tree[node].sum = 0;
        return ;
    }
    int mid = (le + ri) / 2;
    build(2*node, le, mid);
    build(2*node + 1, mid + 1, ri);
    tree[node] = merge(tree[2*node],tree[2*node + 1]);
}
void update(int node, int le, int ri, int start, int end, int val) {
    if (lazy[node]) {
        tree[node].mx += lazy[node];
        tree[node].mn += lazy[node];
        tree[node].sum += (ri - le + 1) * lazy[node];
        if (le != ri) {
            lazy[2*node] += lazy[node];
            lazy[2*node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if (le > end || ri < start) return ;
    if (le >= start && ri <= end) {
        tree[node].mx += val;
        tree[node].mn += val;
        tree[node].sum += (ri - le + 1) * val;
        if (le != ri) {
            lazy[2*node] += val;
            lazy[2*node + 1] += val; 
        }
        return ;
    }
    int mid = (le + ri) / 2;
    update(2*node, le, mid ,start,end,val);
    update(2*node + 1, mid + 1, ri, start,end, val);
    tree[node] = merge(tree[2*node],tree[2*node + 1]);
}
nd getans(int node, int le, int ri, int start, int end) {
    if (lazy[node]) {
        tree[node].mx += lazy[node];
        tree[node].mn += lazy[node];
        tree[node].sum += (ri - le + 1) * lazy[node];
        if (le != ri) {
            lazy[2*node] += lazy[node];
            lazy[2*node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if (le > end || ri < start) return emp;
    if (le >= start && ri <= end) {
        return tree[node];
    }
    int mid = (le + ri) / 2;
    return merge(getans(2*node,le,mid,start,end), getans(2*node + 1, mid + 1, ri, start,end));
}
int check(int mid) {
    if (getans(1,0,q,mid,q).mx - getans(1,0,q,mid,q).mn >= cc) return 1;
    else return 0;
}
main() {
	cin>>n;
	for (int i = 1; i <= n; i++) {
		cin>>c[i];
	}
	emp.mx = -1e9;
	emp.mn = 1e9;
	emp.sum = 0;
	cin>>q;
	for (int i = 1; i <= q; i++) {
		cin>>l[i]>>r[i]>>val[i];
		l[i]++;
		r[i]++;
		v[l[i]].pb({i,val[i]});
		v[r[i] + 1].pb({i,-val[i]});
	}
	build(1,0,q);
	for (int i = 1; i <= n; i++) {
	    //cout<<"s"<<endl;
	    for (int j = 0; j < v[i].size(); j++) {
	        idx = v[i][j].f;
	        //if (abs(v[i][j].s) == 26416) {
	        update(1,0,q,idx,q,v[i][j].s);
	        //cout<<idx<<" "<<q<<" "<<v[i][j].s<<endl;
	        //}
	    }
	   le = 0; ri = q;
	   cc = c[i];
	   ans = -1;
	   while (le <= ri) {
	       mid = (le + ri) / 2;
	       if (check(mid)) {
	           ans = mid;
	           le = mid + 1;
	       } else ri = mid - 1;
	   }
	   if (ans == -1) {
	       if (getans(1,0,q,0,q).mn < 0) {
	           int leeid = getans(1,0,q,0,q).mnidx;
	          cout<<getans(1,0,q,q,q).sum - getans(1,0,q,leeid,leeid).sum<<"\n";
	           continue;
	       }
	       cout<<getans(1,0,q,q,q).sum<<endl;
	       continue;
	   }
	   int mnval = getans(1,0,q,ans,q).mn;
	   mnid = getans(1,0,q,ans,q).mnidx;
	   int mxval = getans(1,0,q,ans,q).mx;
	   mxid = getans(1,0,q,ans,q).mxidx;
	   if (mnid >= mxid) {
	       le = mnid;
	       ri = q;
	        int leeid = getans(1,0,q,mnid,q).mnidx;
	        /*
	        while (le <= ri) {
	            mid = (le + ri) / 2;
	            if (getans(1,0,q,mnid,mid).mx == mnid)
	        }*/
	       cout<<getans(1,0,q,q,q).sum-getans(1,0,q,leeid,leeid).sum<<endl;
	   } else {
	       le = mxid;
	       ri = q;
	       int leeid = getans(1,0,q,mxid,q).mxidx;
	       cout<<c[i] + (getans(1,0,q,q,q).sum - getans(1,0,q,leeid,leeid).sum)<<endl;
	   }
	}
}

Compilation message

candies.cpp:94:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   94 | main() {
      | ^~~~
candies.cpp: In function 'int main()':
candies.cpp:113:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |      for (int j = 0; j < v[i].size(); j++) {
      |                      ~~^~~~~~~~~~~~~
candies.cpp:139:9: warning: unused variable 'mnval' [-Wunused-variable]
  139 |     int mnval = getans(1,0,q,ans,q).mn;
      |         ^~~~~
candies.cpp:141:9: warning: unused variable 'mxval' [-Wunused-variable]
  141 |     int mxval = getans(1,0,q,ans,q).mx;
      |         ^~~~~
/usr/bin/ld: /tmp/ccg2tgNo.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccYpMwbp.o:candies.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccg2tgNo.o: in function `main':
grader.cpp:(.text.startup+0x30e): undefined reference to `distribute_candies(std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status