Submission #86287

# Submission time Handle Problem Language Result Execution time Memory
86287 2018-11-25T22:34:51 Z nikolapesic2802 Zagrade (COI17_zagrade) C++14
40 / 100
3000 ms 382180 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];
    }
}
int ins=0,ers=0;
struct str{
    set<pair<int,int> > set1;
    int delta;
    void insert(int i,int p=1)
    {
        ins++;
        i-=delta;
        if(set1.lower_bound({i,0})==set1.end())
        {
            set1.insert({i,p});
            return;
        }
        pair<int,int> a=*set1.lower_bound({i,0});
        if(a.first==i)
        {
            set1.erase(a);
            a.second+=p;
            set1.insert(a);
        }
        else
        {
            set1.insert({i,p});
        }
    }
    int count(int i)
    {
        i-=delta;
        if(set1.lower_bound({i,0})==set1.end())
            return 0;
        pair<int,int> a=*set1.lower_bound({i,0});
        if(a.first==i)
            return a.second;
        return 0;
    }
    int size()
    {
        return set1.size();
    }
    void erase(int i,int p=1)
    {
        i-=delta;
        pair<int,int> a=*set1.lower_bound({i,0});
        if(a.first==i)
        {
            set1.erase(a);
            a.second-=p;
            if(a.second>0)
                set1.insert(a);
        }
        else
        {
            assert(0);
        }
        ers++;
    }
    int erase()
    {
        int obr=0;
        int c=0;
        while(set1.size()&&(*set1.begin()).first+delta<0){
            assert((*set1.begin()).first+delta==-1);
            obr=((*set1.begin()).second);
            set1.erase(set1.begin());
            assert(c==0);
            c++;
        }
        return obr;
    }
    vector<pair<int,int> > all_elements()
    {
        vector<pair<int,int> > al;
        for(auto p:set1)
        {
            al.pb(mp(p.first+delta,p.second));
        }
        return al;
    }
} e;
vector<str> pocetak,kraj;
vector<int> poc,krj;
pair<int,int> solve(int tr,int par)
{
    //if(tr%100==0)
    //printf("%i %i %i %i  %i %i\n",tr,par,poc.size(),krj.size(),ins,ers);
    //printf("%i: %lld\n",tr,cnt);
    if(graf[tr].size()==1&&tr!=0)
    {
        //printf("Usao!\n");
        if(sta[tr]==0)
            for(auto p:krj)
                cnt+=kraj[p].count(1);
        else
            for(auto p:poc)
                cnt+=pocetak[p].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)
    {
        for(auto p:krj)
            cnt+=kraj[p].count(1);
        for(auto p:krj)
            kraj[p].delta--;
        for(auto p:poc)
            pocetak[p].delta++;
        for(int i=0;i<krj.size();i++)
            obr.pb(kraj[krj[i]].erase());
    }
    else
    {
        for(auto p:poc)
            cnt+=pocetak[p].count(1);
        for(auto p:krj)
            kraj[p].delta++;
        for(auto p:poc)
            pocetak[p].delta--;
        for(int i=0;i<poc.size();i++)
            obr.pb(pocetak[poc[i]].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();
        }
        poc.pb(tren.first);
        dodao.pb(tren.first);
        krj.pb(tren.second);
        dodaok.pb(tren.second);
    }
    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)
    {
        for(auto d:pocetak[p].all_elements())
            pocetak[tren.first].insert(d.first,d.second);
        poc.pop_back();
    }
    for(auto p:dodaok)
    {
        for(auto d:kraj[p].all_elements())
            kraj[tren.second].insert(d.first,d.second);
        krj.pop_back();
    }
    if(sta[tr]==0)
    {
        for(int i=0;i<krj.size();i++)
            if(obr[i])
                kraj[krj[i]].insert(-1,obr[i]);
        for(auto p:krj)
            kraj[p].delta++;
        for(auto p:poc)
            pocetak[p].delta--;
    }
    else
    {
        for(int i=0;i<poc.size();i++)
            if(obr[i])
                pocetak[poc[i]].insert(-1,obr[i]);
        for(auto p:krj)
            kraj[p].delta--;
        for(auto p:poc)
            pocetak[p].delta++;

    }
    return tren;
}
int main()
{
    FILE *f;
    f=fopen("zagrade.in.3e","r");
    e.set1.clear();
    e.delta=0;
    int n;
    //fscanf(f,"%i",&n);
    //printf("%i\n",n);
    scanf("%i",&n);
    string s;
    char cc[n];
    //fscanf(f,"%s",cc);
    //s=cc;
    cin >> s;
    //printf("Uradio!\n");
    for(int i=0;i<n;i++)
    {
        sta[i]=s[i]==')';
    }
    for(int i=1;i<n;i++)
    {
        int x,y;
        //fscanf(f,"%i %i",&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: In function 'std::pair<int, int> solve(int, int)':
zagrade.cpp:157:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<krj.size();i++)
                     ~^~~~~~~~~~~
zagrade.cpp:168:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<poc.size();i++)
                     ~^~~~~~~~~~~
zagrade.cpp:235:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<krj.size();i++)
                     ~^~~~~~~~~~~
zagrade.cpp:245:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0;i<poc.size();i++)
                     ~^~~~~~~~~~~
zagrade.cpp: In function 'int main()':
zagrade.cpp:258:11: warning: variable 'f' set but not used [-Wunused-but-set-variable]
     FILE *f;
           ^
zagrade.cpp:267:10: warning: unused variable 'cc' [-Wunused-variable]
     char cc[n];
          ^~
zagrade.cpp:265:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
zagrade.cpp:280:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i %i",&x,&y);
         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7544 KB Output is correct
2 Correct 11 ms 7680 KB Output is correct
3 Correct 11 ms 7768 KB Output is correct
4 Correct 9 ms 7768 KB Output is correct
5 Correct 9 ms 7768 KB Output is correct
6 Correct 9 ms 7768 KB Output is correct
7 Correct 9 ms 7824 KB Output is correct
8 Correct 10 ms 7836 KB Output is correct
9 Correct 10 ms 7848 KB Output is correct
10 Correct 10 ms 7988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 348 ms 159652 KB Output is correct
2 Correct 417 ms 159700 KB Output is correct
3 Correct 340 ms 159700 KB Output is correct
4 Correct 422 ms 159700 KB Output is correct
5 Correct 354 ms 159756 KB Output is correct
6 Correct 389 ms 159756 KB Output is correct
7 Correct 354 ms 159756 KB Output is correct
8 Correct 421 ms 159756 KB Output is correct
9 Correct 334 ms 159756 KB Output is correct
10 Correct 409 ms 161888 KB Output is correct
11 Correct 314 ms 161888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7544 KB Output is correct
2 Correct 11 ms 7680 KB Output is correct
3 Correct 11 ms 7768 KB Output is correct
4 Correct 9 ms 7768 KB Output is correct
5 Correct 9 ms 7768 KB Output is correct
6 Correct 9 ms 7768 KB Output is correct
7 Correct 9 ms 7824 KB Output is correct
8 Correct 10 ms 7836 KB Output is correct
9 Correct 10 ms 7848 KB Output is correct
10 Correct 10 ms 7988 KB Output is correct
11 Correct 348 ms 159652 KB Output is correct
12 Correct 417 ms 159700 KB Output is correct
13 Correct 340 ms 159700 KB Output is correct
14 Correct 422 ms 159700 KB Output is correct
15 Correct 354 ms 159756 KB Output is correct
16 Correct 389 ms 159756 KB Output is correct
17 Correct 354 ms 159756 KB Output is correct
18 Correct 421 ms 159756 KB Output is correct
19 Correct 334 ms 159756 KB Output is correct
20 Correct 409 ms 161888 KB Output is correct
21 Correct 314 ms 161888 KB Output is correct
22 Correct 679 ms 161888 KB Output is correct
23 Correct 824 ms 161888 KB Output is correct
24 Correct 777 ms 161888 KB Output is correct
25 Correct 790 ms 161888 KB Output is correct
26 Execution timed out 3068 ms 382180 KB Time limit exceeded
27 Halted 0 ms 0 KB -