Advertisement
Guest User

Untitled

a guest
Feb 1st, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include <list>
  2. #include <map>
  3. #include <set>
  4. #include <deque>
  5. #include <stack>
  6. #include <queue>
  7. #include <algorithm>
  8. #include <sstream>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <cstdio>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <memory.h>
  15. #include <ctime>
  16. #include <bitset>
  17. #include <unordered_map>
  18.  
  19. using namespace std;
  20.  
  21. #define ABS(a) ((a>0)?a:-(a))
  22. #define MIN(a,b) ((a<b)?(a):(b))
  23. #define MAX(a,b) ((a<b)?(b):(a))
  24. #define FOR(i,a,n) for (int i=(a);i<(n);++i)
  25. #define FI(i,n) for (int i=0; i<(n); ++i)
  26. #define pnt pair <int, int>
  27. #define mp make_pair
  28. #define PI 3.1415926535897
  29. #define MEMS(a,b) memset(a,b,sizeof(a))
  30. #define LL long long
  31. #define U unsigned
  32.  
  33. int used[30];
  34. set<int> s;
  35. int a[30];
  36. int c[30];
  37.  
  38. const int INF = 1000000001;
  39.  
  40. vector<pair<int,pnt > > get1(int n)
  41. {
  42. vector<pair<int,pnt > > res;
  43. int i = 1;
  44. for (i = 1; i*i<=n; ++i)
  45. {
  46. res.push_back(mp(n/i,mp(i,i)));
  47. }
  48. while (i<=n)
  49. {
  50. int k = n/i;
  51. int to = n/k;
  52. res.push_back(mp(k,mp(i,to)));
  53. i = to + 1;
  54. }
  55. res.push_back(mp(0,mp(n+1,INF)));
  56. reverse(res.begin(), res.end());
  57. return res;
  58. }
  59.  
  60. vector<pnt > all;
  61.  
  62. void get2(int a, int b, int v)
  63. {
  64. vector<pair<int,pnt > > res1 = get1(a);
  65. vector<pair<int,pnt > > res2 = get1(b);
  66. int i = 0, j = 0;
  67. while ((i<res1.size()) && (j<res2.size()))
  68. {
  69. if (res1[i].first == res2[j].first)
  70. {
  71. int l = max(res1[i].second.first, res2[j].second.first);
  72. int r = min(res1[i].second.second, res2[j].second.second);
  73. if (l<=r)
  74. {
  75. //cout<<l<<" "<<r<<endl;
  76. all.push_back(mp(l,v));
  77. all.push_back(mp(r+1,-v));
  78. }
  79. i++;
  80. j++;
  81. }
  82. else
  83. if (res1[i].first<res2[j].first)
  84. i++;
  85. else
  86. j++;
  87. }
  88. //cout<<endl;
  89. }
  90.  
  91. vector<int> order;
  92.  
  93. int min1[30];
  94. int max1[30];
  95.  
  96. void brut(int x, int y)
  97. {
  98. FOR(i,1,y+2)
  99. {
  100. if (x/i == y/i)
  101. cout<<i<<endl;
  102. }
  103. }
  104.  
  105. int main() {
  106. #ifdef Fcdkbear
  107. freopen("in.txt", "r", stdin);
  108. double beg = clock();
  109. //freopen("out.txt", "w", stdout);
  110. #endif
  111.  
  112. /*brut(2000,2015);
  113. return 0;*/
  114. /*vector<pair<int, pnt > > t = get1(25);
  115. FOR(i,0,t.size())
  116. {
  117. cout<<t[i].first<<": "<<t[i].second.first<<" "<<t[i].second.second<<endl;
  118. }
  119. return 0;*/
  120. int n;
  121. cin>>n;
  122. FOR(i,0,n)
  123. {
  124. cin>>a[i]>>c[i];
  125. a[i]--;
  126. s.insert(c[i]);
  127. if ((order.size() == 0) || (order.back() != c[i])) {
  128. order.push_back(c[i]);
  129. min1[c[i]] = a[i];
  130. }
  131. max1[c[i]] = a[i];
  132. }
  133. if (s.size() == 1)
  134. {
  135. cout<<-1<<endl;
  136. return 0;
  137. }
  138. used[c[0]]=1;
  139. FOR(i,1,n)
  140. {
  141. if ((c[i]!=c[i-1]) && (used[c[i]]))
  142. {
  143. cout<<0<<endl;
  144. return 0;
  145. }
  146. }
  147. int tot = 0;
  148. FOR(i,0,n)
  149. FOR(j,i,n)
  150. if (c[i] == c[j])
  151. {
  152. tot++;
  153. get2(a[i],a[j],1);
  154. }
  155. else
  156. get2(a[i],a[j],-1);
  157. sort(all.begin(), all.end());
  158. int last = 0;
  159. int res = 0;
  160. int p = 0;
  161. int cnt = 0;
  162. while (p<all.size())
  163. {
  164. if (all[p].first > 1000000000)
  165. break;
  166. int c = p;
  167. while ((c<all.size()) && (all[c].first == all[p].first))
  168. {
  169. cnt+=all[c].second;
  170. c++;
  171. }
  172. if (cnt == tot)
  173. {
  174. //cout<<all[p].first << " " << all[c].first - 1<<endl;
  175. res += all[c].first - all[p].first;
  176. }
  177. p = c;
  178. }
  179. cout<<res<<endl;
  180.  
  181. #ifdef Fcdkbear
  182. double end = clock();
  183. fprintf(stderr, "*** Total time = %.3lf ***\n",
  184. (end - beg) / CLOCKS_PER_SEC);
  185. #endif
  186. return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement