Submission #558870

#TimeUsernameProblemLanguageResultExecution timeMemory
558870aryan12Wall (IOI14_wall)C++17
0 / 100
121 ms262144 KiB
#include "wall.h"
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
int seg[N * 4];
deque<pair<int, int> > lazy[N * 4];



void UpdateLazyAdding(int l, int r, int pos, int qoperation, int qvalue)
{
    // if(l == 0 && r == 4)
    // {
    //     cout << "I have come here: " << lazy[pos].size() << "\n";
    //     cout << "query values: (" << qoperation << ", " << qvalue << ")\n";
    // }
    while(lazy[pos].size() != 0)
    {
        int siz = lazy[pos].size() - 1;
        if(lazy[pos][siz].first == qoperation)
        {
            if(lazy[pos][siz].second > qvalue)
            {
                lazy[pos].pop_back();
            }
            else
            {
                return;
            }
        }
        else if(lazy[pos][siz].first == 0)
        {
            break;
        }
        else 
        {  
            if(qoperation == 1) // qoperation removing phase while last one adding phase
            {
                if(lazy[pos][siz].second >= qvalue)
                {
                    lazy[pos].clear();
                    lazy[pos].push_back({0LL, qvalue});
                    return;
                }
                else
                {
                    break;
                }
            }
            else // qoperation adding phase while last one removing phase
            {
                if(lazy[pos][siz].second <= qvalue)
                {
                    lazy[pos].clear();
                    lazy[pos].push_back({0LL, qvalue});
                    return;
                }
                else
                {
                    break;
                }
            }
        }  
    }
    lazy[pos].push_back({qoperation, qvalue});
}



void UpdateLazy(int l, int r, int pos)
{
    // if(l == 0 && r == 0 && lazy[pos].size() != 0)
    // {
    //     cout << "\n\n\n\n\n\n\n\n\n\n\n";
    //     cout << "lazy[pos].size() = " << lazy[pos].size() << "\n";
    //     cout << "\n\n\n\n\n\n\n\n\n\n\n";
    // }
    if(l != r)
    {
        int mid = (l + r) >> 1;
        for(int i = 0; i < lazy[pos].size(); i++)
        {
            UpdateLazyAdding(l, mid, pos * 2, lazy[pos][i].first, lazy[pos][i].second);
            UpdateLazyAdding(mid + 1, r, pos * 2 + 1, lazy[pos][i].first, lazy[pos][i].second);
        }
        lazy[pos].clear();
    }
    else
    {
        for(int i = 0; i < lazy[pos].size(); i++)
        {
            if(lazy[pos][i].first == 1) // adding phase
            {
                seg[pos] = max(seg[pos], lazy[pos][i].second);
            }
            else if(lazy[pos][i].first == 2) // removing phase
            {
                seg[pos] = min(seg[pos], lazy[pos][i].second);
            }
            else // setting phase
            {
                seg[pos] = lazy[pos][i].second;
            }
        }
        lazy[pos].clear();
    }
}


int Query(int l, int r, int pos, int qpos)
{
    // cout << "l = " << l << ", r = " << r << ", pos = " << pos << ", qpos = " << qpos << "\n";
    UpdateLazy(l, r, pos);
    if(l == r)
    {
        return seg[pos];
    }
    int mid = (l + r) >> 1;
    if(qpos <= mid)
    {
        return Query(l, mid, pos * 2, qpos);
    }
    return Query(mid + 1, r, pos * 2 + 1, qpos);
}


void Update(int l, int r, int pos, int qoperation, int qleft, int qright, int qvalue)
{
    UpdateLazy(l, r, pos);
    if(qleft > r || l > qright)
    {
        // cout << "Update: (" << l << ", " << r << "), query range(" << qleft << ", " << qright << ", query operation and value: (" << qoperation << ", " << qvalue << ")\n"; 
        // cout << "no intersection\n";
        return;
    }
    if(l == r)
    {
        // cout << "Update: (" << l << ", " << r << "), query range(" << qleft << ", " << qright << ", query operation and value: (" << qoperation << ", " << qvalue << ")\n"; 
        // cout << "full intersection (point)\n";
        if(qoperation == 1) //adding phase
        {
            seg[pos] = max(seg[pos], qvalue);
        }
        else //removing phase
        {
            seg[pos] = min(seg[pos], qvalue);
        }
        return;
    }
    if(qleft <= l && r <= qright)
    {
        // cout << "Update: (" << l << ", " << r << "), query range(" << qleft << ", " << qright << ", query operation and value: (" << qoperation << ", " << qvalue << ")\n"; 
        // cout << "full intersection (range)\n";
        UpdateLazyAdding(l, r, pos, qoperation, qvalue);
        return;
    }
    int mid = (l + r) >> 1;
    Update(l, mid, pos * 2, qoperation, qleft, qright, qvalue);
    Update(mid + 1, r, pos * 2 + 1, qoperation, qleft, qright, qvalue);
    // cout << "Update: (" << l << ", " << r << "), query range(" << qleft << ", " << qright << ", query operation and value: (" << qoperation << ", " << qvalue << ")\n"; 
    // cout << "partial intersection\n";
}


void buildWall(int n, int k, int op[], int left[], int right[], int height[], int finalHeight[])
{
    for(int i = 0; i < k; i++)
    {
        Update(0, n - 1, 1, op[i], left[i], right[i], height[i]);
        // cout << "QUERYING\n";
        // for(int j = 0; j < n; j++)
        // {
        //     cout << Query(0, n - 1, 1, j) << "\n";
        // }
        // cout << "\n\n\n\n\n";
    }
    for(int i = 0; i < n; i++)
    {
        finalHeight[i] = Query(0, n - 1, 1, i);
    }
}

Compilation message (stderr)

wall.cpp: In function 'void UpdateLazy(int, int, int)':
wall.cpp:82:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         for(int i = 0; i < lazy[pos].size(); i++)
      |                        ~~^~~~~~~~~~~~~~~~~~
wall.cpp:91:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         for(int i = 0; i < lazy[pos].size(); i++)
      |                        ~~^~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...