답안 #86274

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
86274 2018-11-25T21:27:11 Z nikolapesic2802 Zagrade (COI17_zagrade) C++14
컴파일 오류
0 ms 0 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define all(x) x.begin(), x.end()

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a) {
	os << '{';
	for(int i=0;i<sz(a);i++)
	{
		if(i>0&&i<sz(a)-1)
			os << ", ";
		os << a[i];
	}
	os << '}';
    return os;
}
const int N=3e5+5;
ll cnt=0;
int sta[N];
int sz[N];
vector<vector<int> > graf(N);
void dfs(int tr,int par)
{
    sz[tr]=1;
    for(auto p:graf[tr])
    {
        if(p==par)
            continue;
        dfs(p,tr);
        sz[tr]+=sz[p];
    }
}
struct str{
    set<pair<int,int> > set;
    int delta;
    void insert(int i)
    {
        i-=delta;
        if(set.lower_bound({i,0})==set.end())
        {
            set.insert({i,1});
            return;
        }
        pair<int,int> a=*set.lower_bound({i,0});
        if(a.first==i)
        {
            set.erase(a);
            a.second++;
            set.insert(a);
        }
        else
        {
            set.insert({i,1});
        }
    }
    int count(int i)
    {
        i-=delta;
        if(set.lower_bound({i,0})==set.end())
            return 0;
        pair<int,int> a=*set.lower_bound({i,0});
        if(a.first==i)
            return a.second;
        return 0;
    }
    int size()
    {
        return set.size();
    }
    void erase(int i)
    {
        i-=delta;
        pair<int,int> a=*set.lower_bound({i,0});
        if(a.first==i)
        {
            set.erase(a);
            a.second--;
            if(a.second>0)
                set.insert(a);
        }
        else
        {
            assert(0);
        }
    }
    vector<int> erase()
    {
        vector<int> obr;
        while(set.size()&&(*set.begin()).first+delta<0){
            obr.pb((*set.begin()).first+delta);
            set.erase(set.begin());
        }
        return obr;
    }
    vector<int> all_elements()
    {
        vector<int> al;
        for(auto p:set)
        {
            for(int i=0;i<p.second;i++)
                al.pb(p.first+delta);
        }
        return al;
    }
} e;
vector<str> pocetak,kraj;
str poc,krj;
pair<int,int> solve(int tr,int par)
{
    //printf("%i %i\n",tr,par);

    //printf("%i: %lld\n",tr,cnt);
    if(graf[tr].size()==1&&tr!=0)
    {
        //printf("Usao!\n");
        if(sta[tr]==0)
            cnt+=krj.count(1);
        else
            cnt+=poc.count(1);
        pocetak.pb(e);
        kraj.pb(e);
        if(sta[tr]==0)
            pocetak.back().insert(1);
        else
            kraj.back().insert(1);
        return mp(pocetak.size()-1,kraj.size()-1);
    }
    vector<int> obr;
    if(sta[tr]==0)
    {
        cnt+=krj.count(1);
        poc.delta++;
        krj.delta--;
        obr=krj.erase();
    }
    else
    {
        cnt+=poc.count(1);
        poc.delta--;
        krj.delta++;
        obr=poc.erase();
    }
    //printf("Prosao!\n");
    vector<pair<int,int> > ind;
    for(auto p:graf[tr])
    {
        if(p==par)
            continue;
        ind.pb({sz[p],p});
    }
    sort(ind.begin(),ind.end());
    vector<int> dodao,dodaok;
    for(int i=0;i<(int)ind.size()-1;i++)
    {
        pair<int,int> tren=solve(ind[i].second,tr);
        //printf("%i: %i %i %i\n",tr,tren.first,tren.second,ind.size());
        if(sta[tr]==0)
        {
            cnt+=kraj[tren.second].count(1);
            pocetak[tren.first].delta++;
            kraj[tren.second].delta--;
            kraj[tren.second].erase();
        }
        else
        {
            cnt+=pocetak[tren.first].count(1);
            pocetak[tren.first].delta--;
            kraj[tren.second].delta++;
            pocetak[tren.first].erase();
        }
        vector<int> al=pocetak[tren.first].all_elements();
        for(auto p:al)
        {
            //printf("%i: Dodao: %i\n",tr,p);
            dodao.pb(p);
            poc.insert(p);
        }
        al=kraj[tren.second].all_elements();
        for(auto p:al)
        {
            //printf("%i: Dodaokraj: %i\n",tr,p);
            dodaok.pb(p);
            krj.insert(p);
        }
    }
    pair<int,int> tren=solve(ind[ind.size()-1].second,tr);
    if(sta[tr]==0)
    {
        cnt+=kraj[tren.second].count(1);
        pocetak[tren.first].delta++;
        kraj[tren.second].delta--;
        kraj[tren.second].erase();
        pocetak[tren.first].insert(1);
    }
    else
    {
        cnt+=pocetak[tren.first].count(1);
        pocetak[tren.first].delta--;
        kraj[tren.second].delta++;
        pocetak[tren.first].erase();
        kraj[tren.second].insert(1);
    }
    for(auto p:dodao)
    {
        //printf("%i: brisem %i\n",tr,p);
        poc.erase(p);
        pocetak[tren.first].insert(p);
    }
    for(auto p:dodaok)
    {
        //printf("%i: brisem kraj %i\n",tr,p);
        krj.erase(p);
        kraj[tren.second].insert(p);
    }
    if(sta[tr]==0)
    {
        for(auto p:obr)
        {
            krj.insert(p);
        }
        poc.delta--;
        krj.delta++;
    }
    else
    {
        for(auto p:obr)
        {
            poc.insert(p);
        }
        poc.delta++;
        krj.delta--;
    }
    return tren;
}
int main()
{
    e.set.clear();
    e.delta=0;
    int n;
    scanf("%i",&n);
    string s;
    cin >> s;
    for(int i=0;i<n;i++)
    {
        sta[i]=s[i]==')';
    }
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%i %i",&x,&y);
        x--;
        y--;
        graf[x].pb(y);
        graf[y].pb(x);
    }
    solve(0,-1);
    printf("%lld\n",cnt);
    return 0;
}

Compilation message

zagrade.cpp:46:25: error: declaration of 'std::set<std::pair<int, int> > str::set' [-fpermissive]
     set<pair<int,int> > set;
                         ^~~
In file included from /usr/include/c++/7/set:61:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:87,
                 from zagrade.cpp:1:
/usr/include/c++/7/bits/stl_set.h:93:11: error: changes meaning of 'set' from 'class std::set<std::pair<int, int> >' [-fpermissive]
     class set
           ^~~
zagrade.cpp: In function 'int main()':
zagrade.cpp:252:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
zagrade.cpp:262:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i %i",&x,&y);
         ~~~~~^~~~~~~~~~~~~~~