#include <iostream> #include <cstdio> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <cmath> #include <algorithm> #include <sstream> #include <stack> #include <cstring> #include <iomanip> #include <ctime> using namespace std; #define pb push_back #define INF 1001001001 #define FOR(i,n) for(int (i)=0;(i)<(n);++(i)) #define FORI(i,n) for(int (i)=1;(i)<=(n);++(i)) #define mp make_pair #define pii pair<int,int> #define ll long long #define vi vector<int> #define SZ(x) ((int)((x).size())) #define fi first #define se second #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); #define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k)); inline void pisz(int n) { printf("%d\n",n); } template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; } #define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl; #define ALL(t) t.begin(),t.end() #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define TESTS wez(testow)while(testow--) #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i) #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i) #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define IOS ios_base::sync_with_stdio(0); const ll mod = 1000000007; vector<pii> v; pair<int,ll> dp[1000042]; int M=1; pii w[4000042]; //rozmiar ????? void insert(int pos, pii val) { int v=pos+M; w[v]=val; while(v!=1) { v/=2; w[v]=mp(max(w[2*v].first,w[2*v+1].first),min(w[2*v].second,w[2*v+1].second)); } } pii query(int a,int b) { //if(b < a)return 0; pii res; int va=a+M,vb=b+M; //vector<long long int> duze,male; res=w[va]; if(va!=vb) { res.first=max(res.first,w[vb].first); res.second=min(res.second,w[vb].second); } while(va/2!=vb/2) { if(va%2==0) { res.first=max(w[va+1].first,res.first); res.second=min(res.second,w[va+1].second); } if(vb%2==1) { res.first=max(w[vb-1].first,res.first); res.second=min(res.second,w[vb-1].second); } va/=2; vb/=2; } return res; } int main() { wez(n); M=1; while(M<n)M*=2; for(int i=0;i<n;i++) { wez2(a,b); v.pb(mp(a,b)); insert(i,mp(a,b)); } int heura = 200000000/n; for(int i=0;i<n;i++) { //if(i%100==0)printf("%d ",i); int a=v[i].first,b=v[i].second; int kand=0; int pocz=0, kon=INF; /*for(int j=i;j>=max(0,i-a+1);j--) { pocz = max(pocz,v[j].first); kon = min(kon,v[j].second); }*/ pii temp = query(max(0,i-a+1),i); pocz=temp.first; kon=temp.second; int poczt=pocz,kont=kon; int cnt=0; int straznik=INF; for(int j=max(i-a+1,0);j>=max(0,i-b+1);j--,cnt++) { straznik = j; if(cnt>heura && n>=2000)break; //if(i-j>10000)break; poczt = max(poczt,v[j].first); kont = min(kont,v[j].second); if(i-j+1<poczt || i-j+1>kont)continue; if(j==0) { if(kand < 1)kand=1; //kadnydat na calosc continue; } if(kand < dp[j-1].first + 1)kand = dp[j-1].first + 1; } temp = query(min(straznik-1,max(0,i-b+1 +heura)),i); poczt = temp.first; kont=temp.second; for(int j=min(straznik-1,max(0,i-b+1 + heura));j>=max(i-b+1,0);j--) { poczt = max(poczt,v[j].first); kont = min(kont,v[j].second); if(i-j+1<poczt || i-j+1>kont)continue; if(j==0) { if(kand < 1)kand=1; //kadnydat na calosc continue; } if(kand < dp[j-1].first + 1)kand = dp[j-1].first + 1; } if(kand==0) { dp[i].first=0; dp[i].second=1; continue; } ll res=0; //if(i==3)cout<<"kandydat "<<kand<<endl; cnt=0; straznik=INF; for(int j=max(i-a+1,0);j>=max(0,i-b+1);j--,cnt++) { straznik=j; if(cnt>heura && n>=2000)break; //if(i-j>10000)break; pocz = max(pocz,v[j].first); kon = min(kon,v[j].second); if(i-j+1<pocz || i-j+1>kon)continue; if(j==0) { if(kand == 1)res=1; continue; } if(kand == dp[j-1].first + 1)res = (res + dp[j-1].second)%mod; } temp = query(min(straznik-1,max(0,i-b+1 + heura)),i); pocz = temp.first; kon=temp.second; for(int j=min(straznik-1,max(0,i-b+1 + heura));j>=max(i-b+1,0);j--) { pocz = max(pocz,v[j].first); kon = min(kon,v[j].second); if(i-j+1<pocz || i-j+1>kon)continue; if(j==0) { if(kand == 1)res=1; continue; } if(kand == dp[j-1].first + 1)res = (res + dp[j-1].second)%mod; } dp[i]=mp(kand,res); } /*cout<<"dp"<<endl; for(int i=0;i<n;i++) { cout<<i<<" grupy: "<<dp[i].first<<", liczba: "<<dp[i].second<<endl; }*/ if(dp[n-1].first==0)printf("NIE\n"); else printf("%d %lld\n",dp[n-1].first,dp[n-1].second); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #include <iostream> #include <cstdio> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <cmath> #include <algorithm> #include <sstream> #include <stack> #include <cstring> #include <iomanip> #include <ctime> using namespace std; #define pb push_back #define INF 1001001001 #define FOR(i,n) for(int (i)=0;(i)<(n);++(i)) #define FORI(i,n) for(int (i)=1;(i)<=(n);++(i)) #define mp make_pair #define pii pair<int,int> #define ll long long #define vi vector<int> #define SZ(x) ((int)((x).size())) #define fi first #define se second #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); #define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k)); inline void pisz(int n) { printf("%d\n",n); } template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; } #define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl; #define ALL(t) t.begin(),t.end() #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define TESTS wez(testow)while(testow--) #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i) #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i) #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define IOS ios_base::sync_with_stdio(0); const ll mod = 1000000007; vector<pii> v; pair<int,ll> dp[1000042]; int M=1; pii w[4000042]; //rozmiar ????? void insert(int pos, pii val) { int v=pos+M; w[v]=val; while(v!=1) { v/=2; w[v]=mp(max(w[2*v].first,w[2*v+1].first),min(w[2*v].second,w[2*v+1].second)); } } pii query(int a,int b) { //if(b < a)return 0; pii res; int va=a+M,vb=b+M; //vector<long long int> duze,male; res=w[va]; if(va!=vb) { res.first=max(res.first,w[vb].first); res.second=min(res.second,w[vb].second); } while(va/2!=vb/2) { if(va%2==0) { res.first=max(w[va+1].first,res.first); res.second=min(res.second,w[va+1].second); } if(vb%2==1) { res.first=max(w[vb-1].first,res.first); res.second=min(res.second,w[vb-1].second); } va/=2; vb/=2; } return res; } int main() { wez(n); M=1; while(M<n)M*=2; for(int i=0;i<n;i++) { wez2(a,b); v.pb(mp(a,b)); insert(i,mp(a,b)); } int heura = 200000000/n; for(int i=0;i<n;i++) { //if(i%100==0)printf("%d ",i); int a=v[i].first,b=v[i].second; int kand=0; int pocz=0, kon=INF; /*for(int j=i;j>=max(0,i-a+1);j--) { pocz = max(pocz,v[j].first); kon = min(kon,v[j].second); }*/ pii temp = query(max(0,i-a+1),i); pocz=temp.first; kon=temp.second; int poczt=pocz,kont=kon; int cnt=0; int straznik=INF; for(int j=max(i-a+1,0);j>=max(0,i-b+1);j--,cnt++) { straznik = j; if(cnt>heura && n>=2000)break; //if(i-j>10000)break; poczt = max(poczt,v[j].first); kont = min(kont,v[j].second); if(i-j+1<poczt || i-j+1>kont)continue; if(j==0) { if(kand < 1)kand=1; //kadnydat na calosc continue; } if(kand < dp[j-1].first + 1)kand = dp[j-1].first + 1; } temp = query(min(straznik-1,max(0,i-b+1 +heura)),i); poczt = temp.first; kont=temp.second; for(int j=min(straznik-1,max(0,i-b+1 + heura));j>=max(i-b+1,0);j--) { poczt = max(poczt,v[j].first); kont = min(kont,v[j].second); if(i-j+1<poczt || i-j+1>kont)continue; if(j==0) { if(kand < 1)kand=1; //kadnydat na calosc continue; } if(kand < dp[j-1].first + 1)kand = dp[j-1].first + 1; } if(kand==0) { dp[i].first=0; dp[i].second=1; continue; } ll res=0; //if(i==3)cout<<"kandydat "<<kand<<endl; cnt=0; straznik=INF; for(int j=max(i-a+1,0);j>=max(0,i-b+1);j--,cnt++) { straznik=j; if(cnt>heura && n>=2000)break; //if(i-j>10000)break; pocz = max(pocz,v[j].first); kon = min(kon,v[j].second); if(i-j+1<pocz || i-j+1>kon)continue; if(j==0) { if(kand == 1)res=1; continue; } if(kand == dp[j-1].first + 1)res = (res + dp[j-1].second)%mod; } temp = query(min(straznik-1,max(0,i-b+1 + heura)),i); pocz = temp.first; kon=temp.second; for(int j=min(straznik-1,max(0,i-b+1 + heura));j>=max(i-b+1,0);j--) { pocz = max(pocz,v[j].first); kon = min(kon,v[j].second); if(i-j+1<pocz || i-j+1>kon)continue; if(j==0) { if(kand == 1)res=1; continue; } if(kand == dp[j-1].first + 1)res = (res + dp[j-1].second)%mod; } dp[i]=mp(kand,res); } /*cout<<"dp"<<endl; for(int i=0;i<n;i++) { cout<<i<<" grupy: "<<dp[i].first<<", liczba: "<<dp[i].second<<endl; }*/ if(dp[n-1].first==0)printf("NIE\n"); else printf("%d %lld\n",dp[n-1].first,dp[n-1].second); return 0; } |