diff --git "a/data/cpp/test.jsonl" "b/data/cpp/test.jsonl" deleted file mode 100644--- "a/data/cpp/test.jsonl" +++ /dev/null @@ -1,267 +0,0 @@ -{"contest_id":"1324","problem_id":"D","statement":"D. Pair of Topicstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe next lecture in a high school requires two topics to be discussed. The ii-th topic is interesting by aiai units for the teacher and by bibi units for the students.The pair of topics ii and jj (ibi+bjai+aj>bi+bj (i.e. it is more interesting for the teacher).Your task is to find the number of good pairs of topics.InputThe first line of the input contains one integer nn (2\u2264n\u22642\u22c51052\u2264n\u22642\u22c5105) \u2014 the number of topics.The second line of the input contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641091\u2264ai\u2264109), where aiai is the interestingness of the ii-th topic for the teacher.The third line of the input contains nn integers b1,b2,\u2026,bnb1,b2,\u2026,bn (1\u2264bi\u22641091\u2264bi\u2264109), where bibi is the interestingness of the ii-th topic for the students.OutputPrint one integer \u2014 the number of good pairs of topic.ExamplesInputCopy5\n4 8 2 6 2\n4 5 4 1 3\nOutputCopy7\nInputCopy4\n1 3 2 4\n1 3 2 4\nOutputCopy0\n","tags":["binary search","data structures","sortings","two pointers"],"code":"#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\n#define endl \"\\n\"\n#define ll long long\n#define bl bool\n#define ch char\n#define vec vector \n#define vll vector \n#define sll set \n#define pll pair \n#define mkp make_pair\n#define mll map \n#define puf push_front\n#define pub push_back\n#define pof pop_front()\n#define pob pop_back()\n#define em empty()\n#define fi first\n#define se second\n#define fr front()\n#define ba back()\n#define be begin()\n#define rbe rbegin()\n#define en end()\n#define ren rend()\n#define all(x) x.begin(),x.end()\n#define rall(x) x.rbegin(),x.rend()\n#define fo(i,x,y) for(ll i=x;i<=y;++i)\n#define fa(i,v) for(auto &i:v)\n#define re return \n#define rz return 0; \n#define sz size()\n#define len length()\n#define con continue; \n#define br break; \n#define ma(a,x) a=max(a,x)\n#define mi(a,x) a=min(a,x)\n#define so(v) sort(all(v))\n#define rso(v) sort(rall(v))\n#define rev(v) reverse(all(v))\n#define i(x) for(ll i=0;i void ff(T a,A... b){ cout< void cc(T a,A... b){ cout< void cl(T a,A... b){ cout< void cn(T a,A... b){ cout< void ci(A&... a){ (cin>>...>>a); }; \ntemplatevoid ou(T v){fa(i,v)cout<void oun(T v){fa(i,v)cout<void ouu(T v){fa(i,v){fa(j,i)cout< void oul(T v){fa(i,v)cout<void in(T &v){fa(i,v)cin>>i;}\ntemplatevoid inn(T &v){fa(i,v)fa(j,i)cin>>j;}\ntemplatevoid oump(T &v){fa(i,v)ff(i.fi,i.se);}\n\ntemplatevoid pi(pair &p){ci(p.fi,p.se);}\ntemplatevoid po(pair &p){ff(p.fi,p.se);}\n\nvoid init(){\n\t\t ios::sync_with_stdio(false);\n\t\t cin.tie(0);\n}\nvoid solve();\nvoid ori();\nll ck(){\n\t\t std::random_device seed_gen;\n\t\t std::mt19937 engine(seed_gen());\n\t\t \/\/ [-1.0, 1.0)\u306e\u5024\u306e\u7bc4\u56f2\u3067\u3001\u7b49\u78ba\u7387\u306b\u5b9f\u6570\u3092\u751f\u6210\u3059\u308b\n\t\t std::uniform_real_distribution<> dist1(1.0, 100000);\n\t\t i(10000){ \/\/ \u5404\u5206\u5e03\u6cd5\u306b\u57fa\u3044\u3066\u4e71\u6570\u3092\u751f\u6210\n\t\t\t\t\t ll n = dist1(engine);\n\t\t } rz;\n}\nbl isup(ch c){\n\t\t re 'A'<=c&&c<='Z';\n}\nbl islo(ch c){\n\t\t re 'a'<=c&&c<='z';\n}\n\/\/isdigit\nmll pr_fa(ll x){\n\t\t mll mp;\n\t\t for(ll i=2;i*i<=x;++i){\n\t\t\t\t\t while(x%i==0){\n\t\t\t\t\t\t\t\t++mp[i];\n\t\t\t\t\t\t\t\tx\/=i;\n\t\t\t\t\t }\n\t\t }\n\t\t if(x!=1)\n\t\t\t\t\t ++mp[x];\n\t\t re mp;\n}\nch to_up(ch a){\n\t\t re toupper(a);\n}\nch to_lo(ch a){\n\t\t re tolower(a);\n}\n#define str(x) to_string(x)\n#define acc(v) accumulate(v.begin(),v.end(),0LL)\n#define acci(v,i) accumulate(v.begin(),v.begin()+i,0LL)\n#define dq deque\nint main(){\n\t\t init();\n\t\t solve();\n\t\t rz;\n}\ntemplate class pnt{\n\t\t public:\n\t\t\t\t\t T x,y;\n\t\t\t\t\t pnt(T x=0,T y=0):x(x),y(y){}\n\t\t\t\t\t pnt operator + (const pnt r)const {\n\t\t\t\t\t\t\t\treturn pnt(x+r.x,y+r.y);}\n\t\t\t\t\t pnt operator - (const pnt r)const {\n\t\t\t\t\t\t\t\treturn pnt(x-r.x,y-r.y);}\n\t\t\t\t\t pnt operator * (const pnt r)const {\n\t\t\t\t\t\t\t\treturn pnt(x*r.x,y*r.y);}\n\t\t\t\t\t pnt operator \/ (const pnt r)const {\n\t\t\t\t\t\t\t\treturn pnt(x\/r.x,y\/r.y);}\n\t\t\t\t\t pnt &operator += (const pnt r){\n\t\t\t\t\t\t\t\tx+=r.x;y+=r.y;return *this;}\n\t\t\t\t\t pnt &operator -= (const pnt r){\n\t\t\t\t\t\t\t\tx-=r.x;y-=r.y;return *this;}\n\t\t\t\t\t pnt &operator *= (const pnt r){\n\t\t\t\t\t\t\t\tx*=r.x;y*=r.y;return *this;}\n\t\t\t\t\t pnt &operator \/= (const pnt r){\n\t\t\t\t\t\t\t\tx\/=r.x;y\/=r.y;return *this;}\n\t\t\t\t\t ll dist(const pnt r){\n\t\t\t\t\t\t\t\tre (x-r.x)*(x-r.x)+(y-r.y)*(y-r.y);\n\t\t\t\t\t }\n\t\t\t\t\t pnt rot(const dou theta){\n\t\t\t\t\t\t\t\tT xx,yy;\n\t\t\t\t\t\t\t\txx=cos(theta)*x-sin(theta)*y;\n\t\t\t\t\t\t\t\tyy=sin(theta)*x+cos(theta)*y;\n\t\t\t\t\t\t\t\treturn pnt(xx,yy);\n\t\t\t\t\t }\n};\nistream &operator >> (istream &is,pnt &r){is>>r.x>>r.y;return is;}\nostream &operator << (ostream &os,pnt &r){os<>= 1)\n\t\t\t\t\t if (b & 1) res = res * a % mod;\n\t\t return res;\n}\nclass mint { \n\t\t public:\n\t\t\t\t\t ll a;\n\t\t\t\t\t mint(ll x=0):a(x%MOD){} \n\t\t\t\t\t mint operator + (const mint rhs) const { \n\t\t\t\t\t\t\t\treturn mint(*this) += rhs; } \n\t\t\t\t\t mint operator - (const mint rhs) const { \n\t\t\t\t\t\t\t\treturn mint(*this) -= rhs; } \n\t\t\t\t\t mint operator * (const mint rhs) const { \n\t\t\t\t\t\t\t\treturn mint(*this) *= rhs; } \n\t\t\t\t\t mint operator \/ (const mint rhs) const { \n\t\t\t\t\t\t\t\treturn mint(*this) \/= rhs; } \n\t\t\t\t\t mint &operator += (const mint rhs) { \n\t\t\t\t\t\t\t\ta += rhs.a; if (a >= MOD) a -= MOD; return *this; } \n\t\t\t\t\t mint &operator -= (const mint rhs) { \n\t\t\t\t\t\t\t\tif (a < rhs.a) a += MOD; a -= rhs.a; return *this; } \n\t\t\t\t\t mint &operator *= (const mint rhs) { \n\t\t\t\t\t\t\t\ta = a * rhs.a % MOD; return *this; } \n\t\t\t\t\t mint &operator \/= (mint rhs) { \n\t\t\t\t\t\t\t\tll exp = MOD - 2; while (exp) { if (exp % 2) *this *= rhs; rhs *= rhs; exp \/= 2; } return *this; } \n\t\t\t\t\t bool operator > (const mint& rhs)const{ return (this->a>rhs.a); } \n\t\t\t\t\t bool operator < (const mint& rhs)const{ return (this->a= (const mint& rhs)const{ return (this->a>=rhs.a); } \n\t\t\t\t\t bool operator <= (const mint& rhs)const{ return (this->a<=rhs.a);} \n\t\t\t\t\t bool operator == (const mint& rhs)const{ return (this->a==rhs.a);} \n}; \nistream& operator>>(istream& is, mint& r) { is>>r.a;return is;} \nostream& operator<<(ostream& os, const mint& r) { os<\n#define top top()\n\nll sumw(ll v,ll r){ re (v==0?0:sumw(v\/10,r)*r+v%10); }\n#define com complex\nstruct UFS{ \n\t\t map par;maprk,siz;\n\t\t st root(st x){ \n\t\t\t\t\t auto it=par.find(x);\n\t\t\t\t\t if(it==par.en){\n\t\t\t\t\t\t\t\tpar[x]=x;siz[x]=1;re x;\n\t\t\t\t\t }\n\t\t\t\t\t if(par[x]==x)return x;\n\t\t\t\t\t else return par[x]=root(par[x]);\n\t\t }\n\t\t bool same(st x,st y){ return root(x)==root(y); }\n\t\t bool unite(st x,st y){ \n\t\t\t\t\t st rx=root(x),ry=root(y);\n\t\t\t\t\t if(rx==ry) return false;\n\t\t\t\t\t if(rk[rx] fact, fact_inv, inv;\n\/* init_nCk :\u4e8c\u9805\u4fc2\u6570\u306e\u305f\u3081\u306e\u524d\u51e6\u7406\n \u8a08\u7b97\u91cf:O(n)\n *\/\nvll fact,inv,fact_inv;\nvoid init_nCk(int SIZE) {\n\tfact.resize(SIZE + 5);\n\tfact_inv.resize(SIZE + 5);\n\tinv.resize(SIZE + 5);\n\tfact[0] = fact[1] = 1;\n\tfact_inv[0] = fact_inv[1] = 1;\n\tinv[1] = 1;\n\tfor (int i = 2; i < SIZE + 5; i++) {\n\t\tfact[i] = fact[i - 1] * i % MOD;\n\t\tinv[i] = MOD - inv[MOD % i] * (MOD \/ i) % MOD;\n\t\tfact_inv[i] = fact_inv[i - 1] * inv[i] % MOD;\n\t}\n}\nlong long nCk(int n, int k) {\n\treturn fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD;\n}\nstruct UF{ vll par,rk,siz; UF(ll n):par(n+5,-1),rk(n+5,0){ }\n\tll root(ll x){ if(par[x]<0)return x; else return par[x]=root(par[x]); }\n\tbool same(ll x,ll y){ return root(x)==root(y); }\n\tbool unite(ll x,ll y){ ll rx=root(x),ry=root(y); if(rx==ry) return false; if(rk[rx]. *++ ! \/%* +- << < == & && +=?:\n *\/\n\/\/vll dx={-1,-1,-1,0,0,1,1,1},dy={-1,0,1,-1,1,-1,0,1};\nvll dx={-1,0,0,1},dy={0,-1,1,0};\n\/\/#define N 11 \n\n#define N 100000\n\/\/ 12345\nvoid solve(){\n\t\/\/2<=n<=2*10^5;\n\t\/\/1<=a_i,b_i<=10^9;\n\t\/\/O(n) is ok\n\t\/\/O(n*(n-1)\/2) is ng \n\t\/\/#|(i,j) s.t. ai+aj>bi+bj| (i0|(i0|(i-dis_j|(i0);\n\tff(ans\/2);\n}\n","language":"cpp"} -{"contest_id":"1324","problem_id":"D","statement":"D. Pair of Topicstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe next lecture in a high school requires two topics to be discussed. The ii-th topic is interesting by aiai units for the teacher and by bibi units for the students.The pair of topics ii and jj (ibi+bjai+aj>bi+bj (i.e. it is more interesting for the teacher).Your task is to find the number of good pairs of topics.InputThe first line of the input contains one integer nn (2\u2264n\u22642\u22c51052\u2264n\u22642\u22c5105) \u2014 the number of topics.The second line of the input contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641091\u2264ai\u2264109), where aiai is the interestingness of the ii-th topic for the teacher.The third line of the input contains nn integers b1,b2,\u2026,bnb1,b2,\u2026,bn (1\u2264bi\u22641091\u2264bi\u2264109), where bibi is the interestingness of the ii-th topic for the students.OutputPrint one integer \u2014 the number of good pairs of topic.ExamplesInputCopy5\n4 8 2 6 2\n4 5 4 1 3\nOutputCopy7\nInputCopy4\n1 3 2 4\n1 3 2 4\nOutputCopy0\n","tags":["binary search","data structures","sortings","two pointers"],"code":"\/\/ Problem: D. Pair of Topics\n\n\/\/ Contest: Codeforces - Codeforces Round #627 (Div. 3)\n\n\/\/ URL: https:\/\/codeforces.com\/problemset\/problem\/1324\/D\n\n\/\/ Memory Limit: 256 MB\n\n\/\/ Time Limit: 2000 ms\n\n\/\/ \n\n\/\/ Powered by CP Editor (https:\/\/cpeditor.org)\n\n\n\n#include \n\n#define int long long\n\n#pragma GCC optimize(\"Ofast\")\n\n#define mod (int)(1e9+7)\n\nusing namespace std;\n\nconst int N=1e5+5;\n\nvoid solve(){\n\n\tint n;\n\n\tcin >> n;\n\n\tvector v(n), a(n);\n\n\tfor ( int &e:v ) cin >> e;\n\n\tfor ( int i=0; i> x;\n\n\t\ta[i]=v[i]-x;\n\n\t}\n\n\tsort(begin(a),end(a));\n\n\tint ans=0;\n\n\tfor ( int i=0; i> t;\n\n while ( t-- ){\n\n solve(); cout << '\\n';\n\n }\n\n}\n\n\/*\n\n*\/","language":"cpp"} -{"contest_id":"1324","problem_id":"C","statement":"C. Frog Jumpstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a frog staying to the left of the string s=s1s2\u2026sns=s1s2\u2026sn consisting of nn characters (to be more precise, the frog initially stays at the cell 00). Each character of ss is either 'L' or 'R'. It means that if the frog is staying at the ii-th cell and the ii-th character is 'L', the frog can jump only to the left. If the frog is staying at the ii-th cell and the ii-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 00.Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.The frog wants to reach the n+1n+1-th cell. The frog chooses some positive integer value dd before the first jump (and cannot change it later) and jumps by no more than dd cells at once. I.e. if the ii-th character is 'L' then the frog can jump to any cell in a range [max(0,i\u2212d);i\u22121][max(0,i\u2212d);i\u22121], and if the ii-th character is 'R' then the frog can jump to any cell in a range [i+1;min(n+1;i+d)][i+1;min(n+1;i+d)].The frog doesn't want to jump far, so your task is to find the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it can jump by no more than dd cells at once. It is guaranteed that it is always possible to reach n+1n+1 from 00.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1\u2264t\u22641041\u2264t\u2264104) \u2014 the number of test cases.The next tt lines describe test cases. The ii-th test case is described as a string ss consisting of at least 11 and at most 2\u22c51052\u22c5105 characters 'L' and 'R'.It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2\u22c51052\u22c5105 (\u2211|s|\u22642\u22c5105\u2211|s|\u22642\u22c5105).OutputFor each test case, print the answer \u2014 the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it jumps by no more than dd at once.ExampleInputCopy6\nLRLRRLL\nL\nLLR\nRRRR\nLLLLLL\nR\nOutputCopy3\n2\n3\n1\n7\n1\nNoteThe picture describing the first test case of the example and one of the possible answers:In the second test case of the example; the frog can only jump directly from 00 to n+1n+1.In the third test case of the example, the frog can choose d=3d=3, jump to the cell 33 from the cell 00 and then to the cell 44 from the cell 33.In the fourth test case of the example, the frog can choose d=1d=1 and jump 55 times to the right.In the fifth test case of the example, the frog can only jump directly from 00 to n+1n+1.In the sixth test case of the example, the frog can choose d=1d=1 and jump 22 times to the right.","tags":["binary search","data structures","dfs and similar","greedy","implementation"],"code":"\/\/ Vidur Goel\n\n\n\n\/\/Codeforcees Handle: Vidurcodviz\n\n\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n\n\nusing namespace std;\n\nusing namespace chrono;\n\n\n\nvoid solve_array();\n\nvoid solve_single();\n\nvoid solve_mul();\n\n\n\ntypedef long long int ll;\n\ntypedef unsigned long long int ull;\n\ntypedef long double lld;\n\ntypedef pair pll;\n\ntypedef vector > vpll;\n\ntypedef vector > vvl;\n\n\n\n#define make_it_fast() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n\n#define rept(i, a, n) for (ll i = (a); i < (n); i++)\n\n#define all(x) (x).begin(), (x).end()\n\n#define sor(x) sort(all(x))\n\n#define lb lower_bound\n\n#define ub upper_bound\n\n#define pb push_back\n\n#define ppb pop_back\n\n#define mp make_pair\n\n#define MOD 1000000007\n\n#define MOD1 998244353\n\n#define PI 3.141592653589793238462\n\n#define vec vector\n\n#define nn endl\n\n\n\nll seiv[1000001]={0};\n\n\n\nstring yup=\"YES\";\n\nstring nope=\"NO\";\n\n\n\nll minar(ll * arr,ll n){return *min_element(arr,arr+n);}\n\nll maxar(ll * arr,ll n){return *max_element(arr,arr+n);}\n\n\n\nll fibonacci(ll n){ll a=0;ll b=1;ll c;if(n==0 || n==1){return n;}for(ll i=2;i>arr[i];}return;}\n\nvoid print_array(ll * &arr,ll n){for(ll i=0;i=b){return gcd(b,a%b);}else{return gcd(b,a);}}\n\nll expo(ll num,ll coef){ll res=1;while(coef!=0){if(coef%2==0){coef=coef\/2;num=num*num;}else{coef=coef-1;res=res*num;}}return res;}\n\nll expo_mod(ll a,ll n,ll m){ll res=1;while(n!=0){if(n%2==0){a=a*a;a=a%m;n=n\/2;}else{res=res*a;res=res%m;n--;}}return res;}\n\n\n\nll add_mod(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}\n\nll mul_mod(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}\n\nll sub_mod(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}\n\n\n\n\/*\n\n sqrt() in built function to give the square root in float\/double\n\n cbrt() in built function to give the cube root in float\/double\n\n abs() is used for the absolute value of a number\n\n sort() inbuilt function in cpp\n\n swap() function in c++ used to swap value of two elements of the same data type.\n\n toupper() This function is used for converting a lowercase character to uppercase.\n\n tolower() This function is used for converting an uppercase character to lowercase.\n\n ceil() and floor() function\n\n vector vect(arr, arr+n) used to make a vector containg same elements as that of the array arr\n\n sort(vect.begin(),vect.end(), greater());\n\n sort(arr,arr+n, greater()) sort in the decreasing order\n\n reverse(vect.begin(), vect.end());\n\n reverse(arr,arr+n);\n\n accumulate(first_iterator, last_iterator, initial value of sum) \u2013 Does the summation of vector elements eg: accumulate(arr,arr+n,0) will give summation of the array\n\n count(first_iterator, last_iterator,x) \u2013 To count the occurrences of x in vector.\n\n find(first_iterator, last_iterator, x) \u2013 Returns an iterator to the first occurrence of x in vector and points to last address of vector ((name_of_vector).end()) if element is not present in vector\n\n find(vect.begin(), vect.end(),5) != vect.end()?\n\n cout << \"\\nElement found\":\n\n cout << \"\\nElement not found\";\n\n maximium value long long can take 9, 223, 372, 036, 854, 775, 807\n\n 2^63-1\n\n i.e, length of 19 only\n\n maximium value long long can take 18, 446, 744, 073, 709, 551, 615\n\n 2^64-1\n\n i.e, length of 20 only\n\n\n\n reverse(s.begin(), s.end()); to reverse the string.(in built function)\n\n set > s1;\n\n s1.insert(10);\n\n set a;\n\n\n\nby default the sets are sorted in the ascending order\n\n\n\n this is how we are going to use the pair here\n\n vector< pair > v;\n\n ll count=1;\n\n for(ll i=0;i p1 ,pair p2){\n\n if(p1.first>test;\n\n for(ll i=0;i>s;\n\n s=s+'R';\n\n s='R'+s;\n\n vec a;\n\n for(ll i=0;i>n;\n\n ll * arr=new ll[n];\n\n read_array(arr,n);\n\n}\n\n\n\nint main(){\n\n make_it_fast();\n\n \/\/seive();\n\n solve_mul();\n\n \/\/solve_array();\n\n \/\/solve_single();\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1285","problem_id":"C","statement":"C. Fadi and LCMtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; Osama gave Fadi an integer XX, and Fadi was wondering about the minimum possible value of max(a,b)max(a,b) such that LCM(a,b)LCM(a,b) equals XX. Both aa and bb should be positive integers.LCM(a,b)LCM(a,b) is the smallest positive integer that is divisible by both aa and bb. For example, LCM(6,8)=24LCM(6,8)=24, LCM(4,12)=12LCM(4,12)=12, LCM(2,3)=6LCM(2,3)=6.Of course, Fadi immediately knew the answer. Can you be just like Fadi and find any such pair?InputThe first and only line contains an integer XX (1\u2264X\u226410121\u2264X\u22641012).OutputPrint two positive integers, aa and bb, such that the value of max(a,b)max(a,b) is minimum possible and LCM(a,b)LCM(a,b) equals XX. If there are several possible such pairs, you can print any.ExamplesInputCopy2\nOutputCopy1 2\nInputCopy6\nOutputCopy2 3\nInputCopy4\nOutputCopy1 4\nInputCopy1\nOutputCopy1 1\n","tags":["brute force","math","number theory"],"code":"\/*----------------------------------\n\n \/\/\u0939\u0930\u0947 \u0915\u0943\u0937\u094d\u0923 - \u0930\u093e\u0927\u0947 \u0930\u093e\u0927\u0947\n\n Author: Arnab Ghosh\n\n-----------------------------------*\/\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n\n\n\n#include \n\n#include\n\n#include\n\n\n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\n\n\n\n\n#define endl '\\n'\n\n#define sz(x) (int)(x).size()\n\n#define all(x) (x).begin(), (x).end()\n\n#define rall(x) (x).rbegin(), (x).rend()\n\nconst long long mod = 1000000007;\n\nconst long long mod1 = 998244353;\n\n\n\n\n\n\n\nusing ll = long long; \n\nusing ld = long double;\n\ntemplate using minpq = priority_queue, greater>;\n\ntemplate using oset = tree, rb_tree_tag, tree_order_statistics_node_update >; \n\n\/\/ find_by_order(k) returns iterator to kth element starting from 0\n\n\/\/ order_of_key(k) returns count of elements strictly smaller than k \/\/less for ascending \/\/greater for decending \/\/less_equal\/greater_equal stores duplicate elements.\n\n\/\/queue -> push, front, back, pop \n\n\/\/deque -> push_back, push_front, pop_back, pop_front, front, back\n\n\n\n\n\n\n\ntemplate::value, typename T_container::value_type>::type>istream& operator >> (istream &is, T_container &v) {for(T &x : v) is >> x; return is;}\n\n#ifdef __SIZEOF_INT128__\n\nostream& operator << (ostream &os, __int128 const& value){static char buffer[64];int index = 0;__uint128_t T = (value < 0) ? (-(value + 1)) + __uint128_t(1) : value;if (value < 0) os << '-';else if (T == 0) return os << '0';for(; T > 0; ++index){ buffer[index] = static_cast('0' + (T % 10)); T \/= 10;} while(index > 0) os << buffer[--index];return os;}\n\nistream& operator >> (istream& is, __int128& T){static char buffer[64];is >> buffer;size_t len = strlen(buffer), index = 0;T = 0; int mul = 1;if (buffer[index] == '-') ++index, mul *= -1;for(; index < len; ++index) T = T * 10 + static_cast(buffer[index] - '0');T *= mul; return is;}\n\n#endif\n\ntemplate \n\nostream& operator<<(ostream &os, const pair &p){ return os << '(' << p.first << \", \" << p.second << ')';}\n\ntemplate::value, typename T_container::value_type>::type> ostream& operator << (ostream &os, const T_container &v) {for (const T &x : v) os<< x << \" \"; return os;}\n\ntemplate, class R = less

> ostream& operator << (ostream& out, priority_queue const& M){static priority_queue U;U = M;out << \"{ \";while(!U.empty()) out << U.top() << \" \", U.pop();return (out << \"}\");}\n\ntemplate ostream& operator << (ostream& out, queue

const& M){static queue

U;U = M;out << \"{\"; string sep;while(!U.empty()){ out << sep << U.front(); sep = \", \"; U.pop();}return (out << \"}\");}\n\n \n\n#ifndef ONLINE_JUDGE\n\n#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)\n\ntemplate \n\nvoid __f(const char* name, Arg1&& arg1){cerr << name << \" : \" << arg1 << endl;}\n\ntemplate \n\nvoid __f(const char* names, Arg1&& arg1, Args&&... args){int count_open = 0, len = 1;for(int k = 1; ; ++k){ char cur = *(names + k); count_open += (cur == '(' ? 1 : (cur == ')' ? -1: 0)); if (cur == ',' && count_open == 0){ const char* comma = names + k; cerr.write(names, len) << \" : \" << arg1 << \" | \"; __f(comma + 1, args...); return; } len = (cur == ' ' ? len : k + 1);}}\n\n#else\n\n #define debug(...) 1\n\n#endif\n\n\n\n\n\ntemplate\n\nvoid pr(T&&... args) {\n\n ((cout << args << \" \"), ...);\n\n cout << '\\n';\n\n}\n\ntemplate\n\nvoid prln(T&&... args) {\n\n ((cout << args << '\\n'), ...);\n\n}\n\n\n\n\/*---------------------------------------------------------------------------------*\/\n\n\n\n\n\n\n\n\n\nvoid solve() {\n\n ll x;\n\n cin >> x;\n\n\n\n ll ans;\n\n for(ll i = 1; i * i <= x; i++) {\n\n if(x % i == 0 && lcm(i, x\/i) == x) {\n\n ans = i;\n\n }\n\n }\n\n\n\n pr(ans, x \/ ans);\n\n}\n\n\n\n \n\nint main() \n\n{ \n\n#ifndef ONLINE_JUDGE\n\n freopen(\"error.txt\", \"w\", stderr);\n\n#endif\n\n ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);\n\n int tt = 1; \n\n \/\/ cin >> tt;\n\n for(int tc = 1; tc <= tt; tc++) {\n\n solve();\n\n cerr << \"-----------\" << '\\n';\n\n }\n\n}","language":"cpp"} -{"contest_id":"1324","problem_id":"F","statement":"F. Maximum White Subtreetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree consisting of nn vertices. A tree is a connected undirected graph with n\u22121n\u22121 edges. Each vertex vv of this tree has a color assigned to it (av=1av=1 if the vertex vv is white and 00 if the vertex vv is black).You have to solve the following problem for each vertex vv: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex vv? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains cntwcntw white vertices and cntbcntb black vertices, you have to maximize cntw\u2212cntbcntw\u2212cntb.InputThe first line of the input contains one integer nn (2\u2264n\u22642\u22c51052\u2264n\u22642\u22c5105) \u2014 the number of vertices in the tree.The second line of the input contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u226410\u2264ai\u22641), where aiai is the color of the ii-th vertex.Each of the next n\u22121n\u22121 lines describes an edge of the tree. Edge ii is denoted by two integers uiui and vivi, the labels of vertices it connects (1\u2264ui,vi\u2264n,ui\u2260vi(1\u2264ui,vi\u2264n,ui\u2260vi).It is guaranteed that the given edges form a tree.OutputPrint nn integers res1,res2,\u2026,resnres1,res2,\u2026,resn, where resiresi is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex ii.ExamplesInputCopy9\n0 1 1 1 0 0 0 0 1\n1 2\n1 3\n3 4\n3 5\n2 6\n4 7\n6 8\n5 9\nOutputCopy2 2 2 2 2 1 1 0 2 \nInputCopy4\n0 0 1 0\n1 2\n1 3\n1 4\nOutputCopy0 -1 1 -1 \nNoteThe first example is shown below:The black vertices have bold borders.In the second example; the best subtree for vertices 2,32,3 and 44 are vertices 2,32,3 and 44 correspondingly. And the best subtree for the vertex 11 is the subtree consisting of vertices 11 and 33.","tags":["dfs and similar","dp","graphs","trees"],"code":"\/\/ <-- Code By: Ashu Mittal -->\n#include\n#include \n#include \nusing namespace std;\nusing namespace __gnu_pbds;\n\ntemplate\nusing minheap = priority_queue, greater >;\n\ntemplate\nusing ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update> ;\n\ntemplate>\nusing ordered_map = tree;\n\n#define ff first\n#define ss second\n#define ll long long\n#define all(sss) (sss).begin(),(sss).end()\n#define pb push_back\n#define pob pop_back\n#define endl \"\\n\"\n#define input(ass) for(auto &x:(ass)) cin>>x;\n#define iendl \"\\n\", cout<>= 1; aaa = (aaa * aaa);} return ans;}\nll mod_inv(ll a) {return power(a, mod - 2);}\n\n\/*----------------------------------x x x---------------------------------*\/\n\nvoid solve() {\n\tll n;\n\tcin >> n;\n\n\tstd::vector co(n);\n\tfor (auto &x : co) {\n\t\tcin >> x;\n\t\tif (x == 0) x = -1;\n\t}\n\n\tstd::vector> v(n);\n\tfor (int i = 0; i < n - 1; i++) {\n\t\tll a, b; cin >> a >> b;\n\t\tv[a - 1].push_back(b - 1);\n\t\tv[b - 1].push_back(a - 1);\n\t}\n\n\tstd::vector dp(n, 0);\n\tfunction dfs = [&](ll i, ll p) {\n\t\tdp[i] += co[i];\n\t\tfor (auto &x : v[i]) {\n\t\t\tif (x == p) continue;\n\t\t\tdfs(x, i);\n\t\t\tdp[i] += max(0ll, dp[x]);\n\t\t}\n\t};\n\tdfs(0, -1);\n\tdbg(dp)\n\n\tfunction dfs1 = [&](ll i, ll p) {\n\t\tfor (auto &x : v[i]) {\n\t\t\tif (x == p) continue;\n\t\t\tll val = (dp[x] < 0) ? 0 : dp[x];\n\t\t\tdp[x] += max(0ll, dp[i] - val);\n\t\t\tdfs1(x, i);\n\t\t}\n\t};\n\tdfs1(0, -1);\n\n\tfor (auto &x : dp) cout << x << ' ';\n\tcout << endl;\n\n}\n\n\/*\n\n*\/\n\nint32_t main() {\n\n\tios::sync_with_stdio(0); cin.tie(0);\n#ifndef ONLINE_JUDGE\n\tfreopen(\"input.txt\", \"r\", stdin);\n\tfreopen(\"output.txt\", \"w\", stdout);\n\tfreopen(\"Error.txt\", \"w\", stderr);\n#endif\n\n\tll TT = 1;\n\t\/\/ cin >> TT;\n\tfor (ll TEST = 1; TEST <= TT; TEST++) {\n\t\t\/\/ cout<<\"Case #\"<\n\n\n\nusing namespace std;\n\n\n\nint main()\n\n{\n\n int t;\n\n cin>>t;\n\n while(t--){\n\n int n,d;\n\n cin>>n>>d;\n\n int l,r,mid,z;\n\n l=0;r=1e9;\n\n bool p=0;\n\n z=n\/2;\n\n z++;\n\n l=ceil((double)d\/z);\n\n z--;\n\n \n\n if((z+l)<=n){\n\n cout<<\"YES\\n\";\n\n }\n\n else cout<<\"NO\\n\";\n\n }\n\n\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1310","problem_id":"E","statement":"E. Strange Functiontime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputLet's define the function ff of multiset aa as the multiset of number of occurences of every number, that is present in aa.E.g., f({5,5,1,2,5,2,3,3,9,5})={1,1,2,2,4}f({5,5,1,2,5,2,3,3,9,5})={1,1,2,2,4}.Let's define fk(a)fk(a), as applying ff to array aa kk times: fk(a)=f(fk\u22121(a)),f0(a)=afk(a)=f(fk\u22121(a)),f0(a)=a. E.g., f2({5,5,1,2,5,2,3,3,9,5})={1,2,2}f2({5,5,1,2,5,2,3,3,9,5})={1,2,2}.You are given integers n,kn,k and you are asked how many different values the function fk(a)fk(a) can have, where aa is arbitrary non-empty array with numbers of size no more than nn. Print the answer modulo 998244353998244353.InputThe first and only line of input consists of two integers n,kn,k (1\u2264n,k\u226420201\u2264n,k\u22642020).OutputPrint one number\u00a0\u2014 the number of different values of function fk(a)fk(a) on all possible non-empty arrays with no more than nn elements modulo 998244353998244353.ExamplesInputCopy3 1\nOutputCopy6\nInputCopy5 6\nOutputCopy1\nInputCopy10 1\nOutputCopy138\nInputCopy10 2\nOutputCopy33\n","tags":["dp"],"code":"#include \n\n#define ms(x, v) memset(x, v, sizeof(x))\n\n#define il __attribute__((always_inline))\n\n#define U(i,l,r) for(int i(l),END##i(r);i<=END##i;++i)\n\n#define D(i,r,l) for(int i(r),END##i(l);i>=END##i;--i)\n\nusing namespace std;\n\n\n\ntypedef unsigned long long ull;\n\ntypedef long long ll;\n\ntemplate using BS = basic_string;\n\n\n\n\/\/const int SZ(1 << 23);\n\n\/\/unsigned char buf[SZ], *S, *Q;\n\n\/\/#define getchar() ((S==Q)&&(Q=buf+fread(S=buf,1,SZ,stdin)),S==Q?EOF:*S++)\n\ntemplate \n\nvoid rd(T& s) {\n\n\tint c = getchar();\n\n\tT f = 1; s = 0;\n\n\twhile (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); }\n\n\twhile (isdigit(c)) { s = s * 10 + (c ^ 48); c = getchar(); }\n\n\ts *= f;\n\n}\n\ntemplate \n\nvoid rd(T& x, Y&... y) { rd(x), rd(y...); }\n\ntemplate \n\nvoid pr(T s, bool f = 1) {\n\n if (s < 0) { printf(\"-\"); s = -s; }\n\n if (!s) return void(f ? printf(\"0\") : 0);\n\n pr(s \/ 10, 0);\n\n printf(\"%d\", (signed)(s % 10));\n\n}\n\n#define meow(...) fprintf(stderr, __VA_ARGS__)\n\n\n\nconst int N = 2025;\n\nconst ll P = 998244353;\n\n\n\nll sub1(ll n) {\n\n\t\/\/ \u6c42 \\sum_1^n \u5206\u62c6\u6570\n\n\tll f[N] {1}; \/\/ \u5212\u5206\u6570\n\n\tU (i, 1, n)\n\n\t\tU (j, i, n)\n\n\t\t\t(f[j] += f[j - i]) %= P;\n\n\tll ans = 0;\n\n\tU (i, 1, n) (ans += f[i]) %= P;\n\n\treturn ans;\n\n}\n\n\n\nll sub2(ll n) {\n\n\t\/\/ \u6784\u9020\u96c6\u5408\u4f7f\u5f97 i*t_i <= n\uff0c\u8ba1\u6570\n\n \/\/ \u5dee\u5206\u5219\u4e0d\u9700\u8981\u8003\u8651 t_i \u51cf\u7684\u9650\u5236\n\n \/\/ \u8fd9\u6837\u4e00\u4e2a c_i=t_i - t_{i-1}=1 \u5bf9\u548c\u7684\u53f3\u79fb\u4e3a sum 1..i = i(i-1)\/2\n\n ll f[N] {1};\n\n for (int i = 1; i * (i + 1) >> 1 <= n; ++i)\n\n for (int j = i * (i + 1) >> 1; j <= n; ++j)\n\n (f[j] += f[j - i * (i + 1) \/ 2]) %= P;\n\n ll ans = 0;\n\n U (i, 1, n) (ans += f[i]) %= P;\n\n return ans;\n\n}\n\n\n\nint n, k;\n\nbool extend(int k, vector v) {\n\n vector g;\n\n for (int j = 1; j < k; ++j) {\n\n int sum = 0;\n\n sort(v.rbegin(), v.rend());\n\n U (i, 0, v.size() - 1) sum += (i + 1) * v[i];\n\n if (sum > n) return 0;\n\n \/\/ if (j + 3 < k && sum > 23) return 0;\n\n U (i, 0, v.size() - 1)\n\n U (j, 1, v[i])\n\n g.push_back(i + 1);\n\n v.swap(g);\n\n vector().swap(g);\n\n }\n\n return 1;\n\n}\n\nvector v;\n\nll ans = 0;\n\nbool dfs(int lim) { \/\/ \u968f\u4fbf\u55ef\u5206\n\n if (!extend(k, v)) return 0;\n\n \/\/ clog << lim << ' ' << v.size() << endl;\n\n ++ans;\n\n \/\/ meow(\"x\");\n\n \/\/ for (int x : v) meow(\"%d \", x);\n\n \/\/ meow(\"\\n\");\n\n for (int i = lim, flag; ; ++i) {\n\n v.push_back(i);\n\n flag = dfs(i);\n\n v.pop_back();\n\n if (!flag) return 1;\n\n }\n\n return 1;\n\n}\n\nll sub3() {\n\n dfs(1);\n\n return ans - 1;\n\n}\n\n\n\nint main() {\n\n \/\/ Sol::main();\n\n \/\/ return 0;\n\n\trd(n, k);\n\n\tif (k == 1) exit(printf(\"%lld\", sub1(n)) & 0);\n\n if (k == 2) exit(printf(\"%lld\", sub2(n)) & 0);\n\n printf(\"%lld\", sub3());\n\n}","language":"cpp"} -{"contest_id":"1292","problem_id":"A","statement":"A. NEKO's Maze Gametime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output3R2 as DJ Mashiro - Happiness Breeze Ice - DJ Mashiro is dead or aliveNEKO#\u03a6\u03c9\u03a6 has just got a new maze game on her PC!The game's main puzzle is a maze; in the forms of a 2\u00d7n2\u00d7n rectangle grid. NEKO's task is to lead a Nekomimi girl from cell (1,1)(1,1) to the gate at (2,n)(2,n) and escape the maze. The girl can only move between cells sharing a common side.However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.After hours of streaming, NEKO finally figured out there are only qq such moments: the ii-th moment toggles the state of cell (ri,ci)(ri,ci) (either from ground to lava or vice versa).Knowing this, NEKO wonders, after each of the qq moments, whether it is still possible to move from cell (1,1)(1,1) to cell (2,n)(2,n) without going through any lava cells.Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?InputThe first line contains integers nn, qq (2\u2264n\u22641052\u2264n\u2264105, 1\u2264q\u22641051\u2264q\u2264105).The ii-th of qq following lines contains two integers riri, cici (1\u2264ri\u226421\u2264ri\u22642, 1\u2264ci\u2264n1\u2264ci\u2264n), denoting the coordinates of the cell to be flipped at the ii-th moment.It is guaranteed that cells (1,1)(1,1) and (2,n)(2,n) never appear in the query list.OutputFor each moment, if it is possible to travel from cell (1,1)(1,1) to cell (2,n)(2,n), print \"Yes\", otherwise print \"No\". There should be exactly qq answers, one after every update.You can print the words in any case (either lowercase, uppercase or mixed).ExampleInputCopy5 5\n2 3\n1 4\n2 4\n2 3\n1 4\nOutputCopyYes\nNo\nNo\nNo\nYes\nNoteWe'll crack down the example test here: After the first query; the girl still able to reach the goal. One of the shortest path ways should be: (1,1)\u2192(1,2)\u2192(1,3)\u2192(1,4)\u2192(1,5)\u2192(2,5)(1,1)\u2192(1,2)\u2192(1,3)\u2192(1,4)\u2192(1,5)\u2192(2,5). After the second query, it's impossible to move to the goal, since the farthest cell she could reach is (1,3)(1,3). After the fourth query, the (2,3)(2,3) is not blocked, but now all the 44-th column is blocked, so she still can't reach the goal. After the fifth query, the column barrier has been lifted, thus she can go to the final goal again. ","tags":["data structures","dsu","implementation"],"code":"#include \n\n#define ll long long\n\n#define MAXN 100010\n\n\n\nusing namespace std;\n\n\n\nint X, Y, N, Q, da_li, rez = 0;\n\nvector > pos(3, vector (MAXN));\n\n\n\nint main()\n\n{\n\n ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n\n cin >> N >> Q;\n\n while(Q--)\n\n {\n\n da_li = 0;\n\n cin >> X >> Y;\n\n if(pos[X][Y]) da_li++;\n\n else da_li--;\n\n pos[X][Y] ^= 1;\n\n for(int i = -1; i <= 1; i++) {if(i+Y >= 1 && i+Y <= N && pos[3 - X][Y + i]) rez += da_li;}\n\n if(rez == 0) {cout << \"YES\" << endl;}\n\n else {cout << \"NO\" << endl;}\n\n }\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1294","problem_id":"F","statement":"F. Three Paths on a Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an unweighted tree with nn vertices. Recall that a tree is a connected undirected graph without cycles.Your task is to choose three distinct vertices a,b,ca,b,c on this tree such that the number of edges which belong to at least one of the simple paths between aa and bb, bb and cc, or aa and cc is the maximum possible. See the notes section for a better understanding.The simple path is the path that visits each vertex at most once.InputThe first line contains one integer number nn (3\u2264n\u22642\u22c51053\u2264n\u22642\u22c5105) \u2014 the number of vertices in the tree. Next n\u22121n\u22121 lines describe the edges of the tree in form ai,biai,bi (1\u2264ai1\u2264ai, bi\u2264nbi\u2264n, ai\u2260biai\u2260bi). It is guaranteed that given graph is a tree.OutputIn the first line print one integer resres \u2014 the maximum number of edges which belong to at least one of the simple paths between aa and bb, bb and cc, or aa and cc.In the second line print three integers a,b,ca,b,c such that 1\u2264a,b,c\u2264n1\u2264a,b,c\u2264n and a\u2260,b\u2260c,a\u2260ca\u2260,b\u2260c,a\u2260c.If there are several answers, you can print any.ExampleInputCopy8\n1 2\n2 3\n3 4\n4 5\n4 6\n3 7\n3 8\nOutputCopy5\n1 8 6\nNoteThe picture corresponding to the first example (and another one correct answer):If you choose vertices 1,5,61,5,6 then the path between 11 and 55 consists of edges (1,2),(2,3),(3,4),(4,5)(1,2),(2,3),(3,4),(4,5), the path between 11 and 66 consists of edges (1,2),(2,3),(3,4),(4,6)(1,2),(2,3),(3,4),(4,6) and the path between 55 and 66 consists of edges (4,5),(4,6)(4,5),(4,6). The union of these paths is (1,2),(2,3),(3,4),(4,5),(4,6)(1,2),(2,3),(3,4),(4,5),(4,6) so the answer is 55. It can be shown that there is no better answer.","tags":["dfs and similar","dp","greedy","trees"],"code":"#include \n\n \n\n#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)\n\n#define file(s) if (fopen(s\".in\", \"r\")) freopen(s\".in\", \"r\", stdin), freopen(s\".out\", \"w\", stdout)\n\n#define all(a) a.begin() , a.end()\n\n#define F first\n\n#define S second\n\n \n\nusing namespace std;\n\nusing ll = long long;\n\n \n\nconst ll N = 3e5+5 , inf = 2e9 + 7;\n\nconst ll INF = 1e18 , mod = 1e9+7 , P = 6547;\t\n\n\n\nll dp[N] , a[4] , ans = 0;\n\nll n;\n\npair mx[N];\n\nvector g[N];\n\nvoid dfs(ll v , ll p = 0){\n\n\tdp[v] = dp[p]+1;\n\n\tmx[v].F = v;\n\n\tfor(ll to : g[v]) if(to != p) dfs(to,v);\n\n}\n\nvector V;\n\nqueue q;\n\n\n\nvoid go(){\n\n\twhile(!q.empty()){\n\n\t\tll v = q.front();\n\n\t\tq.pop();\n\n\t\tfor(ll to : g[v]){\n\n\t\t\tif(dp[to] > dp[v]+1){\n\n\t\t\t\tdp[to] = dp[v]+1;\n\n\t\t\t\tq.push(to);\n\n\t\t\t}\n\n\t\t}\n\n\t}\n\n\tll mx = 0;\n\n\tfor(ll i = 1; i <= n; i++){\n\n\t\tif(i != a[0] && i != a[1]){\n\n\t\t\tif(dp[i] >= mx) {\n\n\t\t\t\tmx = dp[i];\n\n\t\t\t\ta[2] = i;\n\n\t\t\t}\n\n\t\t}\n\n\t}\n\n}\n\nvoid bfs(ll v , ll p = 0){\n\n\tV.push_back(v);\n\n\tif(v == a[0]){\n\n\t\tfor(ll x : V){\n\n\t\t\tdp[x] = 0;\n\n\t\t\tq.push(x);\n\n\t\t}\n\n\t\tgo();\n\n\t\treturn;\n\n\t}\n\n\tfor(ll to : g[v]) if(to != p) bfs(to,v);\n\n\tV.pop_back();\n\n}\n\nvoid solve(){\n\n\tcin >> n;\n\n\tfor(ll i = 1; i < n; i++) {\n\n\t\tll a , b;\n\n\t\tcin >> a >> b;\n\n\t\tg[a].push_back(b);\n\n\t\tg[b].push_back(a);\n\n\t}\n\n\tdfs(1);\n\n\tll V = 1 , mx = 0;\n\n\tfor(ll i = 1; i <= n; i++){\n\n\t\tif(dp[i] > mx) mx = dp[i] , V = i;\n\n\t\tdp[i] = 0;\n\n\t}\n\n\ta[0] = V;\n\n\tdfs(V);\n\n\tV = 1 , mx = 0;\n\n\tfor(ll i = 1; i <= n; i++){\n\n\t\tif(dp[i] > mx) mx = dp[i] , V = i;\n\n\t\tdp[i] = inf;\n\n\t}\n\n\ta[1] = V;\n\n\tbfs(V);\n\n\tcout << mx-1 + dp[a[2]] <<\"\\n\";\n\n\tcout << a[0] << \" \" << a[1] <<\" \" << a[2] <<\"\\n\";\n\n}\t\n\n\/*\n\n\n\n*\/\n\nsigned main(){\n\n\tios;\n\n\tsolve();\n\n\treturn 0;\n\n} ","language":"cpp"} -{"contest_id":"1316","problem_id":"E","statement":"E. Team Buildingtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice; the president of club FCB, wants to build a team for the new volleyball tournament. The team should consist of pp players playing in pp different positions. She also recognizes the importance of audience support, so she wants to select kk people as part of the audience.There are nn people in Byteland. Alice needs to select exactly pp players, one for each position, and exactly kk members of the audience from this pool of nn people. Her ultimate goal is to maximize the total strength of the club.The ii-th of the nn persons has an integer aiai associated with him\u00a0\u2014 the strength he adds to the club if he is selected as a member of the audience.For each person ii and for each position jj, Alice knows si,jsi,j \u00a0\u2014 the strength added by the ii-th person to the club if he is selected to play in the jj-th position.Each person can be selected at most once as a player or a member of the audience. You have to choose exactly one player for each position.Since Alice is busy, she needs you to help her find the maximum possible strength of the club that can be achieved by an optimal choice of players and the audience.InputThe first line contains 33 integers n,p,kn,p,k (2\u2264n\u2264105,1\u2264p\u22647,1\u2264k,p+k\u2264n2\u2264n\u2264105,1\u2264p\u22647,1\u2264k,p+k\u2264n).The second line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an. (1\u2264ai\u22641091\u2264ai\u2264109).The ii-th of the next nn lines contains pp integers si,1,si,2,\u2026,si,psi,1,si,2,\u2026,si,p. (1\u2264si,j\u22641091\u2264si,j\u2264109)OutputPrint a single integer resres \u00a0\u2014 the maximum possible strength of the club.ExamplesInputCopy4 1 2\n1 16 10 3\n18\n19\n13\n15\nOutputCopy44\nInputCopy6 2 3\n78 93 9 17 13 78\n80 97\n30 52\n26 17\n56 68\n60 36\n84 55\nOutputCopy377\nInputCopy3 2 1\n500 498 564\n100002 3\n422332 2\n232323 1\nOutputCopy422899\nNoteIn the first sample; we can select person 11 to play in the 11-st position and persons 22 and 33 as audience members. Then the total strength of the club will be equal to a2+a3+s1,1a2+a3+s1,1.","tags":["bitmasks","dp","greedy","sortings"],"code":"#include \n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define int long long int\n#define ld long double\n#define pi 3.1415926535897932384626433832795028841971\n#define MOD1 998244353\nusing namespace std;\nrandom_device seed_gen;\nmt19937_64 engine(seed_gen());\nint inf = 1e18;\nint dx[4] = {1, 0, -1, 0};\nint dy[4] = {0, 1, 0, -1};\ntemplate void debug(std::vector> v) {for (int i = 0; i < v.size(); i++) cout << \"[ \" << v[i].first << \" \" << v[i].second << \" ]\\n\"; cout << \"\\n\";}\ntemplate void debug(std::vector v) {cout << \"[ \"; for (int i = 0; i < v.size(); i++) cout << v[i] << \" \"; cout << \"]\\n\";}\ntemplate void debug(std::set v) {cout << \"[ \"; for (auto x : v) cout << x << \" \"; cout << \"]\\n\";}\ntemplate void debug(std::multiset v) {cout << \"[ \"; for (auto x : v) cout << x << \" \"; cout << \"]\\n\";}\ntemplate void debug(vector> v) {int n = v.size(), m = v[0].size(); for (int i = 0; i < n; i++) {cout << \"[ \"; for (int j = 0; j < m; j++) cout << v[i][j] << \" \"; cout << \"]\\n\";}}\ntemplate void debug(T i) {cout << \"[ \" << i << \" ]\\n\";}\ntemplate void debug(T i, U j) {cout << \"[ \" << i << \" \" << j << \" ]\\n\";}\ntemplate void debug(T i, U j, V k) {cout << \"[ \" << i << \" \" << j << \" \" << k << \" ]\\n\";}\ntemplate void debug(T i, U j, V k, X l) {cout << \"[ \" << i << \" \" << j << \" \" << k << \" \" << l << \" ]\\n\";}\ntemplate void debug(pair x) {cout << \"[ \" << x.first << \" \" << x.second << \" ]\\n\";}\n\n\nint expo(int a, int b, int mod) {\n int res = 1;\n while (b > 0)\n { if (b & 1)\n res = (res * a) % mod;\n a = (a * a) % mod;\n b = b >> 1;\n } return res;\n}\nint gcd(int a, int b) {\n if (b == 0) return a;\n return gcd(b, a % b);\n}\nint mminvprime(int a, int b) {\n return expo(a, b, b + 2);\n}\nconst int N = 1e5 + 12;\nint a[N], ind[N];\nbool cmp(int x, int y)\n{\n return a[x] > a[y];\n}\nint32_t main() {\n#ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int n, p, k;\n cin >> n >> p >> k;\n vector> s(n + 1, vector(p, 0));\n for (int i = 1; i <= n; ++i)\n {\n cin >> a[i];\n ind[i] = i;\n }\n sort(ind + 1, ind + n + 1, cmp);\n for (int i = 1; i <= n; ++i)\n {\n for (int j = 0; j < p; j++)\n {\n int x;\n cin >> x;\n s[i][j] = x;\n }\n }\n int all = 1 << p;\n vector> dp(all, vector(n + 1, -1));\n dp[0][0] = 0;\n for (int i = 1; i <= n; ++i)\n {\n int curind = ind[i];\n for (int mask = 0; mask < all; mask++)\n {\n int cur = i - __builtin_popcount(mask);\n if (cur <= k)\n {\n if (dp[mask][i - 1] != -1)\n dp[mask][i] = dp[mask][i - 1] + a[curind];\n }\n else\n {\n if (dp[mask][i - 1] != -1)\n dp[mask][i] = dp[mask][i - 1];\n }\n for (int j = 0; j < p; j++)\n {\n if (!(mask & (1 << j))) continue;\n if (dp[mask ^ (1 << j)][i - 1] != -1)\n dp[mask][i] = max(dp[mask][i], dp[mask ^ (1 << j)][i - 1] + s[curind][j]);\n }\n }\n }\n cout << dp[all - 1][n];\n return 0;\n}","language":"cpp"} -{"contest_id":"1301","problem_id":"F","statement":"F. Super Jabertime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputJaber is a superhero in a large country that can be described as a grid with nn rows and mm columns, where every cell in that grid contains a different city.Jaber gave every city in that country a specific color between 11 and kk. In one second he can go from the current city to any of the cities adjacent by the side or to any city with the same color as the current city color.Jaber has to do qq missions. In every mission he will be in the city at row r1r1 and column c1c1, and he should help someone in the city at row r2r2 and column c2c2.Jaber wants your help to tell him the minimum possible time to go from the starting city to the finishing city for every mission.InputThe first line contains three integers nn, mm and kk (1\u2264n,m\u226410001\u2264n,m\u22641000, 1\u2264k\u2264min(40,n\u22c5m)1\u2264k\u2264min(40,n\u22c5m))\u00a0\u2014 the number of rows, columns and colors.Each of the next nn lines contains mm integers. In the ii-th line, the jj-th integer is aijaij (1\u2264aij\u2264k1\u2264aij\u2264k), which is the color assigned to the city in the ii-th row and jj-th column.The next line contains one integer qq (1\u2264q\u22641051\u2264q\u2264105) \u00a0\u2014 the number of missions.For the next qq lines, every line contains four integers r1r1, c1c1, r2r2, c2c2 (1\u2264r1,r2\u2264n1\u2264r1,r2\u2264n, 1\u2264c1,c2\u2264m1\u2264c1,c2\u2264m) \u00a0\u2014 the coordinates of the starting and the finishing cities of the corresponding mission.It is guaranteed that for every color between 11 and kk there is at least one city of that color.OutputFor every mission print the minimum possible time to reach city at the cell (r2,c2)(r2,c2) starting from city at the cell (r1,c1)(r1,c1).ExamplesInputCopy3 4 5\n1 2 1 3\n4 4 5 5\n1 2 1 3\n2\n1 1 3 4\n2 2 2 2\nOutputCopy2\n0\nInputCopy4 4 8\n1 2 2 8\n1 3 4 7\n5 1 7 6\n2 3 8 8\n4\n1 1 2 2\n1 1 3 4\n1 1 2 4\n1 1 4 4\nOutputCopy2\n3\n3\n4\nNoteIn the first example: mission 11: Jaber should go from the cell (1,1)(1,1) to the cell (3,3)(3,3) because they have the same colors, then from the cell (3,3)(3,3) to the cell (3,4)(3,4) because they are adjacent by side (two moves in total); mission 22: Jaber already starts in the finishing cell. In the second example: mission 11: (1,1)(1,1) \u2192\u2192 (1,2)(1,2) \u2192\u2192 (2,2)(2,2); mission 22: (1,1)(1,1) \u2192\u2192 (3,2)(3,2) \u2192\u2192 (3,3)(3,3) \u2192\u2192 (3,4)(3,4); mission 33: (1,1)(1,1) \u2192\u2192 (3,2)(3,2) \u2192\u2192 (3,3)(3,3) \u2192\u2192 (2,4)(2,4); mission 44: (1,1)(1,1) \u2192\u2192 (1,2)(1,2) \u2192\u2192 (1,3)(1,3) \u2192\u2192 (1,4)(1,4) \u2192\u2192 (4,4)(4,4). ","tags":["dfs and similar","graphs","implementation","shortest paths"],"code":"#include \n\n#pragma GCC optimize(\"Ofast\")\n\n\n\n#define debug(x) cerr << #x << \" \" << x << \"\\n\"\n\n#define debugs(x) cerr << #x << \" \" << x << \" \"\n\n\n\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef pair pii;\n\n\n\nconst ll NMAX = 1002;\n\nconst ll VMAX = 41;\n\nconst ll INF = (1LL << 59);\n\nconst ll MOD = 1000000009;\n\nconst ll BLOCK = 318;\n\nconst ll base = 31;\n\nconst ll nrbits = 21;\n\n\n\nshort dist[NMAX][NMAX][41];\n\nshort mat[NMAX][NMAX];\n\nvector v[41];\n\n\n\nstruct ura {\n\n short pf, ps;\n\n int cost;\n\n};\n\n\n\nvector muchii;\n\n\n\nbool viz[41];\n\nshort dx[] = {0, 1, -1, 0};\n\nshort dy[] = {1, 0, 0, -1};\n\nshort n, m;\n\n\n\n\n\nbool OK(short i, short j) {\n\n if(i > 0 && i <= n && j > 0 && j <= m)\n\n return 1;\n\n return 0;\n\n}\n\n\n\nint main() {\n\n#ifdef HOME\n\n ifstream cin(\".in\");\n\n ofstream cout(\".out\");\n\n#endif \/\/ HOME\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(0);\n\n cout.tie(0);\n\n short k, i, j;\n\n cin >> n >> m >> k;\n\n for(i = 1; i <= n; i++) {\n\n for(j = 1; j <= m; j++) {\n\n cin >> mat[i][j];\n\n v[mat[i][j]].push_back({i, j});\n\n }\n\n }\n\n for(int col = 1; col <= k; col++) {\n\n for(i = 1; i <= n + 1; i++) {\n\n for(j = 1; j <= max(m, k); j++) {\n\n dist[i][j][col] = 32767;\n\n }\n\n }\n\n for(i = 1; i <= k; i++) viz[i] = 0; \/\/\/ BRUH Cum sa uit...\n\n queue q;\n\n for(auto x : v[col]) {\n\n dist[x.first][x.second][col] = 0;\n\n q.push({x.first, x.second});\n\n }\n\n while(q.size()) {\n\n pii x = q.front();\n\n q.pop();\n\n for(int d = 0; d < 4; d++) {\n\n pii care = {x.first + dx[d], x.second + dy[d]};\n\n if(!OK(care.first, care.second)) continue;\n\n if(dist[care.first][care.second][col] == 32767) { \/\/\/ Hmm...\n\n dist[care.first][care.second][col] = dist[x.first][x.second][col] + 1;\n\n q.push({care.first, care.second});\n\n }\n\n }\n\n if(!viz[mat[x.first][x.second]]) {\n\n viz[mat[x.first][x.second]] = 1;\n\n for(auto y : v[mat[x.first][x.second]]) {\n\n pii care = y;\n\n if(dist[care.first][care.second][col] == 32767) { \/\/\/ Hmm...\n\n dist[care.first][care.second][col] = dist[x.first][x.second][col] + 1;\n\n q.push({care.first, care.second});\n\n }\n\n }\n\n }\n\n }\n\n }\n\n int q;\n\n cin >> q;\n\n while(q--) {\n\n int a, b, c, d;\n\n cin >> a >> b >> c >> d;\n\n int minim = abs(c - a) + abs(d - b);\n\n for(int col = 1; col <= k; col++) {\n\n minim = min(minim, (int)dist[a][b][col] + (int)dist[c][d][col] + 1);\n\n }\n\n cout << minim << \"\\n\";\n\n }\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"13","problem_id":"E","statement":"E. Holestime limit per test1 secondmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes to play a lot. Most of all he likes to play a game \u00abHoles\u00bb. This is a game for one person with following rules:There are N holes located in a single row and numbered from left to right with numbers from 1 to N. Each hole has it's own power (hole number i has the power ai). If you throw a ball into hole i it will immediately jump to hole i\u2009+\u2009ai; then it will jump out of it and so on. If there is no hole with such number, the ball will just jump out of the row. On each of the M moves the player can perform one of two actions: Set the power of the hole a to value b. Throw a ball into the hole a and count the number of jumps of a ball before it jump out of the row and also write down the number of the hole from which it jumped out just before leaving the row. Petya is not good at math, so, as you have already guessed, you are to perform all computations.InputThe first line contains two integers N and M (1\u2009\u2264\u2009N\u2009\u2264\u2009105, 1\u2009\u2264\u2009M\u2009\u2264\u2009105) \u2014 the number of holes in a row and the number of moves. The second line contains N positive integers not exceeding N \u2014 initial values of holes power. The following M lines describe moves made by Petya. Each of these line can be one of the two types: 0 a b 1 a Type 0 means that it is required to set the power of hole a to b, and type 1 means that it is required to throw a ball into the a-th hole. Numbers a and b are positive integers do not exceeding N.OutputFor each move of the type 1 output two space-separated numbers on a separate line \u2014 the number of the last hole the ball visited before leaving the row and the number of jumps it made.ExamplesInputCopy8 51 1 1 1 1 2 8 21 10 1 31 10 3 41 2OutputCopy8 78 57 3","tags":["data structures","dsu"],"code":"\/\/#define __GLIBCXX_DEBUG 1\n\n\n\n#pragma GCC optimize(\"O3,unroll-loops\")\n\n#pragma GCC target(\"avx,avx2,fma\")\n\n\n\n#include \n\n#include \n\n\n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\n\n\nusing ll = long long;\n\nusing ld = long double;\n\nusing pii = pair;\n\nusing pll = pair;\n\nconst int INF = 1e9;\n\nconst int MOD = 1e9 + 7;\n\nconst int MAXN = 1e5 + 2;\n\n\n\ntypedef tree, rb_tree_tag, tree_order_statistics_node_update> oredered_set;\n\n\n\n\n\n#define all(a) a.begin(), a.end()\n\n#define rall(a) a.rbegin(), a.rend()\n\n#define fastIo(a) ios_base::sync_with_stdio(a); cin.tie(a); cout.tie(a)\n\n#define F first\n\n#define S second\n\n#define pb(a) push_back(a)\n\n#define pf(a) push_front(a)\n\n#define eb(a) emplace_back(a)\n\n#define dbg(a) cerr << #a << \"=\" << a << \"\\n\";\n\n#define dbg1(a) cerr << #a << \" = [ \"; for (auto& _ : a) cerr << _ << \" \"; cerr << \"]\" << \"\\n\";\n\n#define dbg2(a) cerr << #a << \" = [ \"; for (auto& _ : a) cerr << \"(\" << _.F << \" \" << _.S << \") \"; cerr << \"\\n\";\n\n#define int long long\n\n\n\n\n\nint ad(const int &a, const int &b, const int &mod) {\n\n if (a + b >= mod) return a + b - mod;\n\n return a + b;\n\n}\n\n\n\nint sub(const int &a, const int &b, const int &mod) {\n\n if (a < b) return a - b + mod;\n\n return a - b;\n\n}\n\n\n\nint multi(const int &a, const int &b, const int &mod) {\n\n return 1ll * a * b % mod;\n\n}\n\n\n\n\n\nint ask(int x) {\n\n cout << \"- \" << x << endl;\n\n int ans;\n\n cin >> ans;\n\n return ans;\n\n}\n\n\n\nvoid answer(int x) {\n\n cout << \"! \" << x << endl;\n\n}\n\n\n\nconst int sq = 300;\n\n\n\nint nxt[MAXN], cnt[MAXN], power[MAXN], last[MAXN];\n\n\n\nsigned main() {\n\n fastIo(0);\n\n \/\/cout << fixed << setprecision(6);\n\n\n\n int n, m;\n\n cin >> n >> m;\n\n for (int i = 0 ; i < n ; ++i) cin >> power[i];\n\n for (int i = n - 1 ; i >= 0 ; --i) {\n\n int j = min(n, power[i] + i);\n\n int idxi = i \/ sq, idxj = j \/ sq;\n\n if (j == n) {\n\n nxt[i] = n;\n\n cnt[i] = 1;\n\n last[i] = i;\n\n } else if (idxi == idxj) {\n\n nxt[i] = nxt[j];\n\n last[i] = last[j];\n\n cnt[i] = cnt[j] + 1;\n\n } else {\n\n last[i] = i;\n\n nxt[i] = j;\n\n cnt[i] = 1;\n\n }\n\n }\n\n for (int x = 0 ; x < m ; ++x) {\n\n int c, a, b;\n\n cin >> c >> a;\n\n a--;\n\n if (c) {\n\n int res = 0, ans = a;\n\n while (a != n) {\n\n\/\/ dbg(a);\n\n\/\/ dbg(nxt[a]);\n\n ans = last[a];\n\n res += cnt[a];\n\n a = nxt[a];\n\n }\n\n cout << ans + 1 << \" \" << res << \"\\n\";\n\n } else {\n\n cin >> b;\n\n power[a] = b;\n\n for (int i = a ; i >= a \/ sq * sq ; --i) {\n\n int j = min(n, power[i] + i);\n\n int idxi = i \/ sq, idxj = j \/ sq;\n\n if (j == n) {\n\n nxt[i] = n;\n\n cnt[i] = 1;\n\n last[i] = i;\n\n } else if (idxi == idxj) {\n\n nxt[i] = nxt[j];\n\n cnt[i] = cnt[j] + 1;\n\n last[i] = last[j];\n\n } else {\n\n last[i] = i;\n\n nxt[i] = j;\n\n cnt[i] = 1;\n\n }\n\n }\n\n }\n\n }\n\n}\n\n","language":"cpp"} -{"contest_id":"1286","problem_id":"F","statement":"F. Harry The Pottertime limit per test9 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputTo defeat Lord Voldemort; Harry needs to destroy all horcruxes first. The last horcrux is an array aa of nn integers, which also needs to be destroyed. The array is considered destroyed if all its elements are zeroes. To destroy the array, Harry can perform two types of operations: choose an index ii (1\u2264i\u2264n1\u2264i\u2264n), an integer xx, and subtract xx from aiai. choose two indices ii and jj (1\u2264i,j\u2264n;i\u2260j1\u2264i,j\u2264n;i\u2260j), an integer xx, and subtract xx from aiai and x+1x+1 from ajaj. Note that xx does not have to be positive.Harry is in a hurry, please help him to find the minimum number of operations required to destroy the array and exterminate Lord Voldemort.InputThe first line contains a single integer nn\u00a0\u2014 the size of the array aa (1\u2264n\u2264201\u2264n\u226420). The following line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an\u00a0\u2014 array elements (\u22121015\u2264ai\u22641015\u22121015\u2264ai\u22641015).OutputOutput a single integer\u00a0\u2014 the minimum number of operations required to destroy the array aa.ExamplesInputCopy3\n1 10 100\nOutputCopy3\nInputCopy3\n5 3 -2\nOutputCopy2\nInputCopy1\n0\nOutputCopy0\nNoteIn the first example one can just apply the operation of the first kind three times.In the second example; one can apply the operation of the second kind two times: first; choose i=2,j=1,x=4i=2,j=1,x=4, it transforms the array into (0,\u22121,\u22122)(0,\u22121,\u22122), and then choose i=3,j=2,x=\u22122i=3,j=2,x=\u22122 to destroy the array.In the third example, there is nothing to be done, since the array is already destroyed.","tags":["brute force","constructive algorithms","dp","fft","implementation","math"],"code":"#include \n\nusing namespace std;\n\n#define int long long\n\n#define ll long long\n\n#define mp make_pair\n\n#define inf 1e9\n\n#define pii pair \n\nconst int mod = 1e9 + 7;\n\nint read () {\n\n\tint x = 0, f = 1;\n\n\tchar ch = getchar ();\n\n\twhile (ch < '0' || ch > '9') {\n\n\t\tif (ch == '-') f = -1;\n\n\t\tch = getchar ();\n\n\t}\n\n\twhile (ch >= '0' && ch <= '9') {\n\n\t\tx = x * 10 + ch - '0';\n\n\t\tch = getchar ();\n\n\t}\n\n\treturn x * f;\n\n}\n\nvoid write (int x) {\n\n\tif (x < 0) x = -x, putchar ('-');\n\n\tif (x >= 10) write (x \/ 10);\n\n\tputchar (x % 10 + '0');\n\n}\n\nint quickmod (int x, int y) {\n\n\tint Ans = 1;\n\n\twhile (y) {\n\n\t\tif (y & 1) Ans = (Ans * x) % mod;\n\n\t\tx = (x * x) % mod;\n\n\t\ty >>= 1;\n\n\t}\n\n\treturn Ans;\n\n}\n\nint n;\n\nint a[25];\n\nint cnt[(1<<20)+5], sum[(1<<20)+5];\n\nint Abs(int x) {\n\n if(x > 0) return x;\n\n return -x;\n\n}\n\nint f[(1<<20)+5], g[(1<<20)+5];\n\nsigned main () {\n\n\/\/\tfreopen (\".in\", \"r\", stdin);\n\n\/\/\tfreopen (\".out\", \"w\", stdout);\n\n n = read();\n\n for(int i = 1; i <= n; i++) {\n\n a[i] = read();\n\n if(!a[i]) i--, n--;\n\n }\n\n for(int S = 0; S < (1 << n); S++) for(int i = 1; i <= n; i++) cnt[S] += (S >> (i - 1) & 1), sum[S] += (S >> (i - 1) & 1) * a[i];\n\n for(int S = 1; S < (1 << n); S++) {\n\n f[S] = 0;\n\n if((sum[S] & 1) == (cnt[S] & 1)) continue;\n\n for(int S2 = (S & (S - 1)); S2 && !f[S]; S2 = (S & (S2 - 1))) {\n\n if(Abs(sum[S2] - sum[S2^S]) < cnt[S]) f[S] = 1;\n\n }\n\n }\n\n for(int S = 1; S < (1 << n); S++) {\n\n if(!g[S] && f[S]) {\n\n g[S] = 1;\n\n int t = (((1 << n) - 1) ^ S);\n\n for(int S2 = t; S2; S2 = t & (S2 - 1)) {\n\n if(g[S2|S] < f[S] + g[S2]) {\n\n g[S2|S] = f[S] + g[S2];\n\n }\n\n }\n\n }\n\n }\n\n write(n - g[(1<\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\nusing namespace std;\ninline void read(int &x){\n\tx=0;\n\tbool sgn=0;\n\tchar ch;\n\twhile(ch=getchar(),ch<'!');\n\tif(ch=='-'){\n\t\tsgn=1;\n\t}else{\n\t\tx=ch-48;\n\t}\n\twhile(ch=getchar(),ch>'!'){\n\t\tx=(x<<3)+(x<<1)+ch-48;\n\t}\n\tif(sgn==1){\n\t\tx=-x;\n\t}\n\treturn;\n}\ninline void write(int x){\n\tif(x<0){\n\t\tputchar('-');\n\t\tx=-x;\n\t}\n\tif(x>9){\n\t\twrite(x\/10);\n\t}\n\tputchar(x%10+48);\n\treturn;\n}\ninline void read(long long &x){\n\tx=0;\n\tbool sgn=0;\n\tchar ch;\n\twhile(ch=getchar(),ch<'!');\n\tif(ch=='-'){\n\t\tsgn=1;\n\t}else{\n\t\tx=ch-48;\n\t}\n\twhile(ch=getchar(),ch>'!'){\n\t\tx=(x<<3)+(x<<1)+ch-48;\n\t}\n\tif(sgn==1){\n\t\tx=-x;\n\t}\n\treturn;\n}\ninline void write(long long x){\n\tif(x<0){\n\t\tputchar('-');\n\t\tx=-x;\n\t}\n\tif(x>9){\n\t\twrite(x\/10);\n\t}\n\tputchar(x%10+48);\n\treturn;\n}\nlong long n,m,k,cnt,lcp[1050][1050],dp[1050][1050],redp[1050][1050]; \nchar s[1050];\nstruct st{\n\tlong long l,r;\n}a[1000050];\ninline bool operator<(st const &lhs,st const &rhs) {\n\tlong long length=lcp[lhs.l][rhs.l];\n\tif(length>=lhs.r-lhs.l+1||length>=rhs.r-rhs.l+1){\n\t\treturn lhs.r-lhs.l+1=1;i--){\n\t\tlong long t=min(length,lcp[a[x].l][i]);\n\t\tif(t==length||s[i+t]>s[a[x].l+t]){\n\t\t\tfor(int j=1;j<=m;j++){\n\t\t\t\tdp[i][j]=redp[i+t+1][j-1];\n\t\t\t}\n\t\t}\n\t\tfor(int j=0;j<=m;j++){\n\t\t\tredp[i][j]=min(redp[i+1][j]+dp[i][j],k);\n\t\t}\n\t}\n\treturn dp[1][m]>=k;\n}\nint main(){\n\tread(n);\n\tread(m);\n\tread(k);\n\tscanf(\"%s\",s+1);\n\tfor(int i=n;i>=1;i--){\n\t\tfor(int j=n;j>=1;j--){\n\t\t\tlcp[i][j]=0;\n\t\t\tif(s[i]==s[j]){\n\t\t\t\tlcp[i][j]=lcp[i+1][j+1]+1;\n\t\t\t}\n\t\t}\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tfor(int j=i;j<=n;j++){\n\t\t\ta[++cnt]=st{i,j};\n\t\t}\n\t}\n\tsort(a+1,a+1+cnt);\n\tlong long l=1,r=cnt;\n\twhile(l<=r){\n\t\tlong long mid=(l+r)>>1;\n\t\tif(check(mid)){\n\t\t\tl=mid+1;\n\t\t}else{\n\t\t\tr=mid-1;\n\t\t}\n\t}\n\tfor(int i=a[l].l;i<=a[l].r;i++){\n\t\tputchar(s[i]);\n\t}\n\treturn 0;\n}\n\n\t \t\t\t \t\t \t\t\t \t\t\t\t\t\t\t \t \t\t\t","language":"cpp"} -{"contest_id":"1325","problem_id":"D","statement":"D. Ehab the Xorcisttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven 2 integers uu and vv, find the shortest array such that bitwise-xor of its elements is uu, and the sum of its elements is vv.InputThe only line contains 2 integers uu and vv (0\u2264u,v\u22641018)(0\u2264u,v\u22641018).OutputIf there's no array that satisfies the condition, print \"-1\". Otherwise:The first line should contain one integer, nn, representing the length of the desired array. The next line should contain nn positive integers, the array itself. If there are multiple possible answers, print any.ExamplesInputCopy2 4\nOutputCopy2\n3 1InputCopy1 3\nOutputCopy3\n1 1 1InputCopy8 5\nOutputCopy-1InputCopy0 0\nOutputCopy0NoteIn the first sample; 3\u22951=23\u22951=2 and 3+1=43+1=4. There is no valid array of smaller length.Notice that in the fourth sample the array is empty.","tags":["bitmasks","constructive algorithms","greedy","number theory"],"code":"\/\/ LUOGU_RID: 102435206\n#include\nusing namespace std;\nlong long u,v,c;\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n cin>>u>>v;\n c=v-u;\n if(c<0||(c&1)){\n cout<<-1;\n return 0;\n }\n if(!c){\n if(!u)\n cout<<0;\n else\n cout<<1<>=1;\n if(!(c&u))\n cout<<2<\n\n#define int long long \n\n#define pb push_back\n\nusing namespace std;\n\nconst int INF=7e5+5;\n\nint t,n,l[INF],r[INF],f[INF];\n\nvector v;\n\nint Get(int x) {return lower_bound(v.begin(),v.end(),x)-v.begin()+1;}\n\nstruct node_{\n\n\tbool l,r;\n\n\tint xx;\n\n};\n\nnode_ mer(node_ x,node_ y) {return {x.l,y.r,x.xx+y.xx-(x.r&y.l)};}\n\nstruct Segment{\n\n\t#define ll tl[id]\n\n\t#define rr tr[id]\n\n\t#define ls(x) x<<1\n\n\t#define rs(x) x<<1|1\n\n\tnode_ sum[INF<<2];\n\n\tint tl[INF<<2],tr[INF<<2];\n\n\tvoid push_up(int id) {\n\n\t\tsum[id]=mer(sum[ls(id)],sum[rs(id)]);\n\n\t}\n\n\tvoid build(int l,int r,int id) {\n\n\t\tll=l;rr=r;\n\n\t\tif (l==r) {sum[id]={f[l]>0,f[l]>0,f[l]>0};return ;}\n\n\t\tint Mid=(l+r)>>1;\n\n\t\tbuild(l,Mid,ls(id));\n\n\t\tbuild(Mid+1,r,rs(id));\n\n\t\tpush_up(id);\n\n\t}\n\n\tnode_ query(int l,int r,int id) {\n\n\t\tif (l<=ll && rr<=r) return sum[id];\n\n\t\tint Mid=(ll+rr)>>1;\n\n\t\tif (l>Mid) return query(l,r,rs(id));\n\n\t\telse if (r<=Mid) return query(l,r,ls(id));\n\n\t\telse return mer(query(l,r,ls(id)),query(l,r,rs(id)));\n\n\t}\n\n} T1;\n\n\n\n\n\nstruct Segment2{\n\n\t#define ll tl[id]\n\n\t#define rr tr[id]\n\n\t#define ls(x) x<<1\n\n\t#define rs(x) x<<1|1\n\n\tnode_ sum[INF<<2];\n\n\tint tl[INF<<2],tr[INF<<2];\n\n\tvoid push_up(int id) {\n\n\t\tsum[id]=mer(sum[ls(id)],sum[rs(id)]);\n\n\t}\n\n\tvoid build(int l,int r,int id) {\n\n\t\tll=l;rr=r;\n\n\t\tif (l==r) {sum[id]={f[l]>1,f[l]>1,f[l]>1};return ;}\n\n\t\tint Mid=(l+r)>>1;\n\n\t\tbuild(l,Mid,ls(id));\n\n\t\tbuild(Mid+1,r,rs(id));\n\n\t\tpush_up(id);\n\n\t}\n\n\tnode_ query(int l,int r,int id) {\n\n\t\tif (l<=ll && rr<=r) return sum[id];\n\n\t\tint Mid=(ll+rr)>>1;\n\n\t\tif (l>Mid) return query(l,r,rs(id));\n\n\t\telse if (r<=Mid) return query(l,r,ls(id));\n\n\t\telse return mer(query(l,r,ls(id)),query(l,r,rs(id)));\n\n\t}\n\n} T2;\n\nvoid solve() {\n\n\tcin>>n;\n\n\tfor (int i=1;i<=n;i++) {\n\n\t\tcin>>l[i]>>r[i];\n\n\t\tl[i]*=2;r[i]*=2;\n\n\t\tv.pb(l[i]);v.pb(r[i]);\n\n\t\tv.pb(r[i]+1);\n\n\t}\n\n\tsort(v.begin(),v.end());\n\n\tv.erase(unique(v.begin(),v.end()),v.end());\n\n\tint len=v.size();\n\n\tfor (int i=1;i<=len+5;i++) f[i]=0;\n\n\tfor (int i=1;i<=n;i++) {\n\n\t\tint it=Get(l[i]),it1=Get(r[i]+1);\n\n\t\tf[it]++;f[it1]--;\n\n\t}\n\n\tfor (int i=1;i<=len;i++) f[i]+=f[i-1];\n\n\tT1.build(1,len,1);T2.build(1,len,1);\n\n\tint res=0;\n\n\tfor (int i=1;i<=n;i++) {\n\n\t\tint it=Get(l[i]),it1=Get(r[i]);\n\n\t\tnode_ xx={0,0,0};if (it-1>=1) xx=T1.query(1,it-1,1);\n\n\t\tnode_ yy={0,0,0};yy=T2.query(it,it1,1);\n\n\t\tnode_ zz={0,0,0};if (it1+1<=len) zz=T1.query(it1+1,len,1);\n\n\t\tres=max(res,mer(xx,mer(yy,zz)).xx); \n\n\t}\n\n\tcout<>t;\n\n\twhile (t--) solve();\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1292","problem_id":"F","statement":"F. Nora's Toy Boxestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSIHanatsuka - EMber SIHanatsuka - ATONEMENTBack in time; the seven-year-old Nora used to play lots of games with her creation ROBO_Head-02, both to have fun and enhance his abilities.One day, Nora's adoptive father, Phoenix Wyle, brought Nora nn boxes of toys. Before unpacking, Nora decided to make a fun game for ROBO.She labelled all nn boxes with nn distinct integers a1,a2,\u2026,ana1,a2,\u2026,an and asked ROBO to do the following action several (possibly zero) times: Pick three distinct indices ii, jj and kk, such that ai\u2223ajai\u2223aj and ai\u2223akai\u2223ak. In other words, aiai divides both ajaj and akak, that is ajmodai=0ajmodai=0, akmodai=0akmodai=0. After choosing, Nora will give the kk-th box to ROBO, and he will place it on top of the box pile at his side. Initially, the pile is empty. After doing so, the box kk becomes unavailable for any further actions. Being amused after nine different tries of the game, Nora asked ROBO to calculate the number of possible different piles having the largest amount of boxes in them. Two piles are considered different if there exists a position where those two piles have different boxes.Since ROBO was still in his infant stages, and Nora was still too young to concentrate for a long time, both fell asleep before finding the final answer. Can you help them?As the number of such piles can be very large, you should print the answer modulo 109+7109+7.InputThe first line contains an integer nn (3\u2264n\u2264603\u2264n\u226460), denoting the number of boxes.The second line contains nn distinct integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u2264601\u2264ai\u226460), where aiai is the label of the ii-th box.OutputPrint the number of distinct piles having the maximum number of boxes that ROBO_Head can have, modulo 109+7109+7.ExamplesInputCopy3\n2 6 8\nOutputCopy2\nInputCopy5\n2 3 4 9 12\nOutputCopy4\nInputCopy4\n5 7 2 9\nOutputCopy1\nNoteLet's illustrate the box pile as a sequence bb; with the pile's bottommost box being at the leftmost position.In the first example, there are 22 distinct piles possible: b=[6]b=[6] ([2,6,8]\u2212\u2192\u2212\u2212(1,3,2)[2,8][2,6,8]\u2192(1,3,2)[2,8]) b=[8]b=[8] ([2,6,8]\u2212\u2192\u2212\u2212(1,2,3)[2,6][2,6,8]\u2192(1,2,3)[2,6]) In the second example, there are 44 distinct piles possible: b=[9,12]b=[9,12] ([2,3,4,9,12]\u2212\u2192\u2212\u2212(2,5,4)[2,3,4,12]\u2212\u2192\u2212\u2212(1,3,4)[2,3,4][2,3,4,9,12]\u2192(2,5,4)[2,3,4,12]\u2192(1,3,4)[2,3,4]) b=[4,12]b=[4,12] ([2,3,4,9,12]\u2212\u2192\u2212\u2212(1,5,3)[2,3,9,12]\u2212\u2192\u2212\u2212(2,3,4)[2,3,9][2,3,4,9,12]\u2192(1,5,3)[2,3,9,12]\u2192(2,3,4)[2,3,9]) b=[4,9]b=[4,9] ([2,3,4,9,12]\u2212\u2192\u2212\u2212(1,5,3)[2,3,9,12]\u2212\u2192\u2212\u2212(2,4,3)[2,3,12][2,3,4,9,12]\u2192(1,5,3)[2,3,9,12]\u2192(2,4,3)[2,3,12]) b=[9,4]b=[9,4] ([2,3,4,9,12]\u2212\u2192\u2212\u2212(2,5,4)[2,3,4,12]\u2212\u2192\u2212\u2212(1,4,3)[2,3,12][2,3,4,9,12]\u2192(2,5,4)[2,3,4,12]\u2192(1,4,3)[2,3,12]) In the third sequence, ROBO can do nothing at all. Therefore, there is only 11 valid pile, and that pile is empty.","tags":["bitmasks","combinatorics","dp"],"code":"#include \nusing namespace std;\ntypedef long long ll;\ntypedef uint64_t ull;\ninline int read(){\n\tint f=1,r=0;char c=getchar();\n\twhile(!isdigit(c))f^=c=='-',c=getchar();\n\twhile(isdigit(c))r=(r<<1)+(r<<3)+(c&15),c=getchar();\n\treturn f?r:-r;\n}\nconst int N=66,M=1<<15|7,mod=1e9+7;\ninline void inc(int &x,int y){x+=y-mod,x+=x>>31&mod;}\nint pa[N];\nint find(int x){return pa[x]^x?pa[x]=find(pa[x]):x;}\nint n,sum,ans=1,a[N],id[N],C[N][N],f[N][M],num[M];\nbool flg[N];\null S[N];\ninline void solve(int pos){\n\tint tot=0;vectorvec,d;\n\tfor(int i=1;i<=n;i++)\n\t\tif(flg[i] && pa[i]==pos)id[i]=tot++,vec.push_back(i);\n\tint U=(1<>j&1)s|=1<>i&1)num[s]+=num[s^(1<num[s])continue;\n\t\t\tinc(f[i+1][s],(ll)v*(num[s]-i-1)%mod);\n\t\t\tfor(int j:d){\n\t\t\t\tint t=S[j]&s;\n\t\t\t\tif(!t || t==S[j])continue;\n\t\t\t\tinc(f[i+1][s|S[j]],v);\n\t\t\t}\n\t\t}\n\tans=(ll)ans*f[cnt][U]%mod;\n}\nint main(){\n#ifndef ONLINE_JUDGE\n\tfreopen(\"1.in\",\"r\",stdin);\n\tfreopen(\"1.out\",\"w\",stdout);\n#endif\n\tn=read();\n\tfor(int i=1;i<=n;i++)a[i]=read();\n\tsort(a+1,a+n+1);\n\tC[0][0]=1;\n\tfor(int i=1;i<=n;i++) {\n\t\tC[i][0]=1;\n\t\tfor (int j=1; j<=n; j++) C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tflg[i]=true,pa[i]=i;\n\t\tfor(int j=1;j<=n;j++)\n\t\t\tflg[i]&=i==j || a[i]%a[j]!=0;\n\t}\n\tfor(int i=1;i<=n;i++){\n\t\tif(flg[i])continue;\n\t\tint lst=0;\n\t\tfor(int j=1;j<=n;j++){\n\t\t\tif(i==j || !flg[j])continue;\n\t\t\tif(a[i]%a[j]==0){\n\t\t\t\tif(!lst)lst=find(j);\n\t\t\t\tS[i]|=1ull<\n\n\n\n#define ll long long\n\n#define pii pair\n\n#define fi first\n\n#define se second\n\n#define endl '\\n'\n\nusing namespace std;\n\n\n\nvoid DuckMyKofta() {\n\n ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);\n\n#ifndef ONLINE_JUDGE\n\n freopen(\"input.txt\", \"r\", stdin);\n\n freopen(\"output.txt\", \"w\", stdout);\n\n#else\n\n \/\/ freopen(\"input.txt\", \"r\", stdin);\n\n \/\/ freopen(\"output.txt\", \"w\", stdout);\n\n#endif\n\n}\n\n\n\nconst int N = 1e5 + 5;\n\nconst long long BASE = 1e9 + 7;\n\nconst int INV_2 = 5e8 + 4;\n\nconst int LOG = 20;\n\nconst long double PI = 3.14159265358979323846;\n\nint dx[] = {+0, +0, -1, +1, +1, -1, +1, -1};\n\nint dy[] = {-1, +1, +0, +0, +1, -1, -1, +1};\n\n\n\n\n\nvoid solve(int testcase) {\n\n int n, k;\n\n cin >> n >> k;\n\n ll a[n];\n\n for (int i = 0; i < n; ++i) {\n\n cin >> a[i];\n\n }\n\n int m = (int) (log(LONG_LONG_MAX) \/ log(k));\n\n vector> bits(n, vector(m+1));\n\n\n\n for (int i = 0; i < n; ++i) {\n\n ll tmp = a[i];\n\n int cnt = 0;\n\n while (tmp) {\n\n bits[i][cnt] = tmp % k;\n\n tmp \/= k;\n\n cnt++;\n\n }\n\n }\n\n set st;\n\n for (int i = 0; i < n; ++i) {\n\n for (int j = 0; j < m; ++j) {\n\n if (bits[i][j] == 1) {\n\n if (st.contains(j)) {\n\n cout << \"NO\" << endl;\n\n return;\n\n }\n\n st.insert(j);\n\n } else if (bits[i][j] != 0) {\n\n cout << \"NO\" << endl;\n\n return;\n\n }\n\n\n\n }\n\n }\n\n cout << \"YES\" << endl;\n\n}\n\n\n\n\n\nsigned main() {\n\n DuckMyKofta();\n\n int t = 1, cnt = 1;\n\n cin >> t;\n\n while (t--) {\n\n solve(cnt++);\n\n }\n\n}\n\n","language":"cpp"} -{"contest_id":"1284","problem_id":"G","statement":"G. Seollaltime limit per test3 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputIt is only a few days until Seollal (Korean Lunar New Year); and Jaehyun has invited his family to his garden. There are kids among the guests. To make the gathering more fun for the kids, Jaehyun is going to run a game of hide-and-seek.The garden can be represented by a n\u00d7mn\u00d7m grid of unit cells. Some (possibly zero) cells are blocked by rocks, and the remaining cells are free. Two cells are neighbors if they share an edge. Each cell has up to 4 neighbors: two in the horizontal direction and two in the vertical direction. Since the garden is represented as a grid, we can classify the cells in the garden as either \"black\" or \"white\". The top-left cell is black, and two cells which are neighbors must be different colors. Cell indices are 1-based, so the top-left corner of the garden is cell (1,1)(1,1).Jaehyun wants to turn his garden into a maze by placing some walls between two cells. Walls can only be placed between neighboring cells. If the wall is placed between two neighboring cells aa and bb, then the two cells aa and bb are not neighboring from that point. One can walk directly between two neighboring cells if and only if there is no wall directly between them. A maze must have the following property. For each pair of free cells in the maze, there must be exactly one simple path between them. A simple path between cells aa and bb is a sequence of free cells in which the first cell is aa, the last cell is bb, all cells are distinct, and any two consecutive cells are neighbors which are not directly blocked by a wall.At first, kids will gather in cell (1,1)(1,1), and start the hide-and-seek game. A kid can hide in a cell if and only if that cell is free, it is not (1,1)(1,1), and has exactly one free neighbor. Jaehyun planted roses in the black cells, so it's dangerous if the kids hide there. So Jaehyun wants to create a maze where the kids can only hide in white cells.You are given the map of the garden as input. Your task is to help Jaehyun create a maze.InputYour program will be judged in multiple test cases.The first line contains the number of test cases tt. (1\u2264t\u22641001\u2264t\u2264100). Afterward, tt test cases with the described format will be given.The first line of a test contains two integers n,mn,m (2\u2264n,m\u2264202\u2264n,m\u226420), the size of the grid.In the next nn line of a test contains a string of length mm, consisting of the following characters (without any whitespace): O: A free cell. X: A rock. It is guaranteed that the first cell (cell (1,1)(1,1)) is free, and every free cell is reachable from (1,1)(1,1). If t\u22652t\u22652 is satisfied, then the size of the grid will satisfy n\u226410,m\u226410n\u226410,m\u226410. In other words, if any grid with size n>10n>10 or m>10m>10 is given as an input, then it will be the only input on the test case (t=1t=1).OutputFor each test case, print the following:If there are no possible mazes, print a single line NO.Otherwise, print a single line YES, followed by a grid of size (2n\u22121)\u00d7(2m\u22121)(2n\u22121)\u00d7(2m\u22121) denoting the found maze. The rules for displaying the maze follows. All cells are indexed in 1-base. For all 1\u2264i\u2264n,1\u2264j\u2264m1\u2264i\u2264n,1\u2264j\u2264m, if the cell (i,j)(i,j) is free cell, print 'O' in the cell (2i\u22121,2j\u22121)(2i\u22121,2j\u22121). Otherwise, print 'X' in the cell (2i\u22121,2j\u22121)(2i\u22121,2j\u22121). For all 1\u2264i\u2264n,1\u2264j\u2264m\u221211\u2264i\u2264n,1\u2264j\u2264m\u22121, if the neighboring cell (i,j),(i,j+1)(i,j),(i,j+1) have wall blocking it, print ' ' in the cell (2i\u22121,2j)(2i\u22121,2j). Otherwise, print any printable character except spaces in the cell (2i\u22121,2j)(2i\u22121,2j). A printable character has an ASCII code in range [32,126][32,126]: This includes spaces and alphanumeric characters. For all 1\u2264i\u2264n\u22121,1\u2264j\u2264m1\u2264i\u2264n\u22121,1\u2264j\u2264m, if the neighboring cell (i,j),(i+1,j)(i,j),(i+1,j) have wall blocking it, print '\u00a0' in the cell (2i,2j\u22121)(2i,2j\u22121). Otherwise, print any printable character except spaces in the cell (2i,2j\u22121)(2i,2j\u22121) For all 1\u2264i\u2264n\u22121,1\u2264j\u2264m\u221211\u2264i\u2264n\u22121,1\u2264j\u2264m\u22121, print any printable character in the cell (2i,2j)(2i,2j). Please, be careful about trailing newline characters or spaces. Each row of the grid should contain exactly 2m\u221212m\u22121 characters, and rows should be separated by a newline character. Trailing spaces must not be omitted in a row.ExampleInputCopy4\n2 2\nOO\nOO\n3 3\nOOO\nXOO\nOOO\n4 4\nOOOX\nXOOX\nOOXO\nOOOO\n5 6\nOOOOOO\nOOOOOO\nOOOOOO\nOOOOOO\nOOOOOO\nOutputCopyYES\nOOO\n O\nOOO\nNO\nYES\nOOOOO X\n O O \nX O O X\n O \nOOO X O\nO O O\nO OOOOO\nYES\nOOOOOOOOOOO\n O O O\nOOO OOO OOO\nO O O \nOOO OOO OOO\n O O O\nOOO OOO OOO\nO O O \nOOO OOO OOO\n","tags":["graphs"],"code":"#include \n\nusing namespace std;\nconst int N=10005;\nconst int M=105;\nint head[N],nxt[N],edgenum=1,vet[N],num[N],val[N],dir[N],fa[N],flag[N],vis[N],n,m;\nchar ans[M][M],c[M][M];\nconst int dx[4]={1,-1,0,0};\nconst int dy[4]={0,0,1,-1};\nvoid Add(int u,int v,int w) { vet[++edgenum]=v; nxt[edgenum]=head[u]; head[u]=edgenum; val[edgenum]=w; }\nint find(int x) { if (x!=fa[x]) fa[x]=find(fa[x]); return fa[x]; }\nbool dfs(int x) {\nif (vis[x]) return 0;\nvis[x]=1;\nfor (int e=head[x]; e; e=nxt[e]) {\nint v=vet[e];\nif (!vis[v] && (!num[v] || dfs(num[v]))) {\nnum[v]=x;\ndir[v]=val[e]^1;\nreturn 1;\n}\n}\nreturn 0;\n}\nvoid dfs2(int x,int f) {\nfor (int e=head[x]; e; e=nxt[e]) {\nint v=vet[e]; if (v==f) continue;\nif (find(x)!=find(v)) {\nfa[find(x)]=find(v);\nans[(x-1)\/m*2+1+dx[val[e]]][(x-1)%m*2+1+dy[val[e]]]='O';\nif (v!=1) dfs2(num[v],v);\n}\n}\n}\nint main() {\n\/\/\tfreopen(\"sample1.in\",\"r\",stdin);\n\/\/\tfreopen(\"1.out\",\"w\",stdout);\nint deb=0;\nint T; scanf(\"%d\",&T);\nif (deb) cout<n || ny>m || (nx==1 && ny==1) || c[nx][ny]!='O') continue;\nAdd((x-1)*m+y,(nx-1)*m+ny,k);\n}\n}\nfor (int i=1; i<=n; i++)\nfor (int j=(i&1)+1; j<=m; j+=2)\nif (c[i][j]=='O') {\nfor (int k=1; k<=n*m; k++) vis[k]=0;\nflag[(i-1)*m+j]=dfs((i-1)*m+j);\n}\n\nfor (int i=1; i<=n; i++)\nfor (int j=-(i&1)+2; j<=m; j+=2)\nif (i+j>2 && c[i][j]=='O') {\nif (!num[(i-1)*m+j]) { puts(\"NO\"); goto out; }\nfa[find((i-1)*m+j)]=find(num[(i-1)*m+j]);\nans[(i<<1)-1+dx[dir[(i-1)*m+j]]][(j<<1)-1+dy[dir[(i-1)*m+j]]]='O';\n}\n\nif (c[2][1]=='O') Add(m+1,1,1);\nif (c[1][2]=='O') Add(2,1,3);\nfor (int i=1; i<=n; i++)\nfor (int j=(i&1)+1; j<=m; j+=2)\nif (c[i][j]=='O' && !flag[(i-1)*m+j]) dfs2((i-1)*m+j,-1);\n\nfor (int i=1,rt=find(1); i<=n; i++)\nfor (int j=1; j<=m; j++) {\nif (c[i][j]=='O' && find((i-1)*m+j)!=rt) { puts(\"NO\"); goto out; }\nans[(i<<1)-1][(j<<1)-1]=c[i][j];\n}\nif (deb) { for (int i=1; i<(n<<1); i++) for (int j=1; j<(m<<1); j++) if (ans[i][j]==' ') ans[i][j]='K'; }\nputs(\"YES\");\nfor (int i=1; i<(n<<1); i++,puts(\"\"))\nfor (int j=1; j<(m<<1); j++) printf(\"%c\",ans[i][j]);\nout:;\n}\nreturn 0;\n}\n","language":"cpp"} -{"contest_id":"1322","problem_id":"E","statement":"E. Median Mountain Rangetime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputBerland\u00a0\u2014 is a huge country with diverse geography. One of the most famous natural attractions of Berland is the \"Median mountain range\". This mountain range is nn mountain peaks, located on one straight line and numbered in order of 11 to nn. The height of the ii-th mountain top is aiai. \"Median mountain range\" is famous for the so called alignment of mountain peaks happening to it every day. At the moment of alignment simultaneously for each mountain from 22 to n\u22121n\u22121 its height becomes equal to the median height among it and two neighboring mountains. Formally, if before the alignment the heights were equal bibi, then after the alignment new heights aiai are as follows: a1=b1a1=b1, an=bnan=bn and for all ii from 22 to n\u22121n\u22121 ai=median(bi\u22121,bi,bi+1)ai=median(bi\u22121,bi,bi+1). The median of three integers is the second largest number among them. For example, median(5,1,2)=2median(5,1,2)=2, and median(4,2,4)=4median(4,2,4)=4.Recently, Berland scientists have proved that whatever are the current heights of the mountains, the alignment process will stabilize sooner or later, i.e. at some point the altitude of the mountains won't changing after the alignment any more. The government of Berland wants to understand how soon it will happen, i.e. to find the value of cc\u00a0\u2014 how many alignments will occur, which will change the height of at least one mountain. Also, the government of Berland needs to determine the heights of the mountains after cc alignments, that is, find out what heights of the mountains stay forever. Help scientists solve this important problem!InputThe first line contains integers nn (1\u2264n\u22645000001\u2264n\u2264500000)\u00a0\u2014 the number of mountains.The second line contains integers a1,a2,a3,\u2026,ana1,a2,a3,\u2026,an (1\u2264ai\u22641091\u2264ai\u2264109)\u00a0\u2014 current heights of the mountains.OutputIn the first line print cc\u00a0\u2014 the number of alignments, which change the height of at least one mountain.In the second line print nn integers\u00a0\u2014 the final heights of the mountains after cc alignments.ExamplesInputCopy5\n1 2 1 2 1\nOutputCopy2\n1 1 1 1 1 \nInputCopy6\n1 3 2 5 4 6\nOutputCopy1\n1 2 3 4 5 6 \nInputCopy6\n1 1 2 2 1 1\nOutputCopy0\n1 1 2 2 1 1 \nNoteIn the first example; the heights of the mountains at index 11 and 55 never change. Since the median of 11, 22, 11 is 11, the second and the fourth mountains will have height 1 after the first alignment, and since the median of 22, 11, 22 is 22, the third mountain will have height 2 after the first alignment. This way, after one alignment the heights are 11, 11, 22, 11, 11. After the second alignment the heights change into 11, 11, 11, 11, 11 and never change from now on, so there are only 22 alignments changing the mountain heights.In the third examples the alignment doesn't change any mountain height, so the number of alignments changing any height is 00.","tags":["data structures"],"code":"#include\n\ntypedef long long ll;\n\ntypedef unsigned long long ull;\n\n#define rep(i, a, b) for(int i = (a); i <= (b); i ++)\n\n#define per(i, a, b) for(int i = (a); i >= (b); i --)\n\n#define Ede(i, u) for(int i = head[u]; i; i = e[i].nxt)\n\nusing namespace std;\n\n\n\n#define eb emplace_back\n\n\n\ninline int read() {\n\n\tint x = 0, f = 1; char c = getchar();\n\n\twhile(c < '0' || c > '9') f = (c == '-') ? - 1 : 1, c = getchar();\n\n\twhile(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();\n\n\treturn x * f;\n\n}\n\n\n\nconst int N = 5e5 + 10;\n\nint n, a[N], m, b[N], tim, ans[N];\n\nbool vis[N];\n\nvector g[N];\n\nset pos, valid;\n\n\n\nvoid calc(int l, int r, int v) {\n\n\tauto it = valid.lower_bound(l);\n\n\twhile(it != valid.end() && *it <= r) ans[*it] = v, it = valid.erase(it);\n\n}\n\n\n\nvoid update(int p, int v) {\n\n\tif(p < 1 || p > n) return;\n\n\tauto it = pos.lower_bound(p); int r = *it, l = *(-- it) + 1;\n\n\ttim = max(tim, (r - l) >> 1);\n\n\tint mid = (l + r) >> 1;\n\n\tif(vis[l]) calc(l, mid, v);\n\n\tif(vis[r]) calc(mid + 1, r, v);\n\n}\n\n\n\nvoid insert(int p) {\n\n\tvis[p] = true;\n\n\tif(p > 1 && !vis[p - 1]) pos.erase(p - 1); else pos.insert(p - 1);\n\n\tif(p < n && !vis[p + 1]) pos.erase(p); else pos.insert(p);\n\n}\n\n\n\nint main() {\n\n\tn = m = read();\n\n\trep(i, 1, n) a[i] = b[i] = read();\n\n\tsort(b + 1, b + m + 1);\n\n\tm = unique(b + 1, b + m + 1) - (b + 1);\n\n\tpos.insert(0);\n\n\trep(i, 1, n)\n\n\t\tpos.insert(i), valid.insert(i), \n\n\t\ta[i] = lower_bound(b + 1, b + m + 1, a[i]) - b, g[a[i]].eb(i);\n\n\trep(i, 1, m) {\n\n\t\tfor(int o : g[i]) insert(o);\n\n\t\tfor(int o : g[i]) rep(d, -1, 1) update(o + d, i);\n\n\t}\n\n\tprintf(\"%d\\n\", tim);\n\n\trep(i, 1, n) printf(\"%d%c\", b[ans[i]], i == n ? '\\n' : ' ');\n\n\treturn 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1303","problem_id":"D","statement":"D. Fill The Bagtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a bag of size nn. Also you have mm boxes. The size of ii-th box is aiai, where each aiai is an integer non-negative power of two.You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.For example, if n=10n=10 and a=[1,1,32]a=[1,1,32] then you have to divide the box of size 3232 into two parts of size 1616, and then divide the box of size 1616. So you can fill the bag with boxes of size 11, 11 and 88.Calculate the minimum number of divisions required to fill the bag of size nn.InputThe first line contains one integer tt (1\u2264t\u226410001\u2264t\u22641000) \u2014 the number of test cases.The first line of each test case contains two integers nn and mm (1\u2264n\u22641018,1\u2264m\u22641051\u2264n\u22641018,1\u2264m\u2264105) \u2014 the size of bag and the number of boxes, respectively.The second line of each test case contains mm integers a1,a2,\u2026,ama1,a2,\u2026,am (1\u2264ai\u22641091\u2264ai\u2264109) \u2014 the sizes of boxes. It is guaranteed that each aiai is a power of two.It is also guaranteed that sum of all mm over all test cases does not exceed 105105.OutputFor each test case print one integer \u2014 the minimum number of divisions required to fill the bag of size nn (or \u22121\u22121, if it is impossible).ExampleInputCopy3\n10 3\n1 32 1\n23 4\n16 1 4 1\n20 5\n2 1 16 1 8\nOutputCopy2\n-1\n0\n","tags":["bitmasks","greedy"],"code":"#include \n\nusing namespace std;\n\n \n\nint Solve() {\n\n long long n; int m; \n\n scanf(\"%lld%d\", &n, &m);\n\n vector cnt(31), nd(31);\n\n long long sum = 0;\n\n for (int i = 0; i < m; ++i) {\n\n int x; scanf(\"%d\", &x);\n\n sum += x;\n\n cnt[__builtin_ctz(x)]++;\n\n }\n\n if (sum < n) return -1;\n\n for (int i = 0; i < 60; ++i) {\n\n if (n >> i & 1) {\n\n if (i <= 30) nd[i]++;\n\n else nd[30] += (1 << (i - 30));\n\n }\n\n }\n\n int res = 0;\n\n for (int i = 0; i < 30; ++i) {\n\n if (nd[i] == 1 && cnt[i] == 0) {\n\n for (int j = i + 1; j <= 30; ++j) {\n\n if (cnt[j] > 0) {\n\n res += j - i;\n\n cnt[j]--;\n\n for (int k = j - 1; k > i; --k) cnt[k]++;\n\n cnt[i] += 2;\n\n break;\n\n }\n\n }\n\n }\n\n if (nd[i] == 1 && cnt[i] == 0) return -1;\n\n if (nd[i] == 1) cnt[i]--; \n\n cnt[i + 1] += cnt[i] \/ 2;\n\n }\n\n if (cnt[30] < nd[30]) return -1;\n\n return res;\n\n}\n\n \n\nint main() {\n\n int t; scanf(\"%d\", &t);\n\n while (t--) printf(\"%d\\n\", Solve());\n\n}\n\n","language":"cpp"} -{"contest_id":"1290","problem_id":"B","statement":"B. Irreducible Anagramstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's call two strings ss and tt anagrams of each other if it is possible to rearrange symbols in the string ss to get a string, equal to tt.Let's consider two strings ss and tt which are anagrams of each other. We say that tt is a reducible anagram of ss if there exists an integer k\u22652k\u22652 and 2k2k non-empty strings s1,t1,s2,t2,\u2026,sk,tks1,t1,s2,t2,\u2026,sk,tk that satisfy the following conditions: If we write the strings s1,s2,\u2026,sks1,s2,\u2026,sk in order, the resulting string will be equal to ss; If we write the strings t1,t2,\u2026,tkt1,t2,\u2026,tk in order, the resulting string will be equal to tt; For all integers ii between 11 and kk inclusive, sisi and titi are anagrams of each other. If such strings don't exist, then tt is said to be an irreducible anagram of ss. Note that these notions are only defined when ss and tt are anagrams of each other.For example, consider the string s=s= \"gamegame\". Then the string t=t= \"megamage\" is a reducible anagram of ss, we may choose for example s1=s1= \"game\", s2=s2= \"gam\", s3=s3= \"e\" and t1=t1= \"mega\", t2=t2= \"mag\", t3=t3= \"e\": On the other hand, we can prove that t=t= \"memegaga\" is an irreducible anagram of ss.You will be given a string ss and qq queries, represented by two integers 1\u2264l\u2264r\u2264|s|1\u2264l\u2264r\u2264|s| (where |s||s| is equal to the length of the string ss). For each query, you should find if the substring of ss formed by characters from the ll-th to the rr-th has at least one irreducible anagram.InputThe first line contains a string ss, consisting of lowercase English characters (1\u2264|s|\u22642\u22c51051\u2264|s|\u22642\u22c5105).The second line contains a single integer qq (1\u2264q\u22641051\u2264q\u2264105) \u00a0\u2014 the number of queries.Each of the following qq lines contain two integers ll and rr (1\u2264l\u2264r\u2264|s|1\u2264l\u2264r\u2264|s|), representing a query for the substring of ss formed by characters from the ll-th to the rr-th.OutputFor each query, print a single line containing \"Yes\" (without quotes) if the corresponding substring has at least one irreducible anagram, and a single line containing \"No\" (without quotes) otherwise.ExamplesInputCopyaaaaa\n3\n1 1\n2 4\n5 5\nOutputCopyYes\nNo\nYes\nInputCopyaabbbbbbc\n6\n1 2\n2 4\n2 2\n1 9\n5 7\n3 5\nOutputCopyNo\nYes\nYes\nYes\nNo\nNo\nNoteIn the first sample; in the first and third queries; the substring is \"a\"; which has itself as an irreducible anagram since two or more non-empty strings cannot be put together to obtain \"a\". On the other hand; in the second query, the substring is \"aaa\", which has no irreducible anagrams: its only anagram is itself, and we may choose s1=s1= \"a\", s2=s2= \"aa\", t1=t1= \"a\", t2=t2= \"aa\" to show that it is a reducible anagram.In the second query of the second sample, the substring is \"abb\", which has, for example, \"bba\" as an irreducible anagram.","tags":["binary search","constructive algorithms","data structures","strings","two pointers"],"code":"#include \n\n\n\nusing namespace std;\n\n\n\nusing LL = long long;\n\n#define endl '\\n'\n\nusing db = double;\n\ntemplate \n\nusing max_heap = priority_queue;\n\ntemplate \n\nusing min_heap = priority_queue, greater<>>;\n\n\n\nint main()\n\n{\n\n ios::sync_with_stdio(false);\n\n cin.tie(nullptr);\n\n string s;\n\n cin >> s;\n\n int n = s.size();\n\n s = ' ' + s;\n\n vector> pre(n + 1, vector(26));\n\n for (int i = 1; i <= n; ++i)\n\n {\n\n pre[i] = pre[i - 1];\n\n pre[i][s[i] - 'a']++;\n\n }\n\n int Q;\n\n cin >> Q;\n\n while (Q--)\n\n {\n\n int l, r;\n\n cin >> l >> r;\n\n vector cnt(26);\n\n for (int i = 0; i < 26; ++i)\n\n cnt[i] = pre[r][i] - pre[l - 1][i];\n\n bool ok = 0;\n\n sort(cnt.begin(), cnt.end(), greater());\n\n if (cnt[2] > 0 || (cnt[1] > 0 && s[l] != s[r]))\n\n ok = 1;\n\n if (ok || r - l + 1 == 1)\n\n cout << \"Yes\" << endl;\n\n else\n\n cout << \"No\" << endl;\n\n }\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1292","problem_id":"C","statement":"C. Xenon's Attack on the Gangstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputINSPION FullBand Master - INSPION INSPION - IOLITE-SUNSTONEOn another floor of the A.R.C. Markland-N; the young man Simon \"Xenon\" Jackson, takes a break after finishing his project early (as always). Having a lot of free time, he decides to put on his legendary hacker \"X\" instinct and fight against the gangs of the cyber world.His target is a network of nn small gangs. This network contains exactly n\u22121n\u22121 direct links, each of them connecting two gangs together. The links are placed in such a way that every pair of gangs is connected through a sequence of direct links.By mining data, Xenon figured out that the gangs used a form of cross-encryption to avoid being busted: every link was assigned an integer from 00 to n\u22122n\u22122 such that all assigned integers are distinct and every integer was assigned to some link. If an intruder tries to access the encrypted data, they will have to surpass SS password layers, with SS being defined by the following formula:S=\u22111\u2264u\n\n#pragma GCC optimize(\"Ofast\")\n\n#pragma GCC optimize(\"unroll-loops\")\n\n\n\nusing namespace std;\n\n\n\n#define F \t\t\tfirst\n\n#define S \t\t\tsecond\n\n#define pb \t\t\tpush_back\n\n#define sze\t\t\tsize()\n\n#define\tall(x)\t\tx.begin() , x.end()\n\n#define wall__\t\tcout << \"--------------------------------------\\n\";\n\n#define node\t\tint mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1\n\n#define file_io\t\tfreopen(\"input.cpp\", \"r\", stdin) ; freopen(\"output.cpp\", \"w\", stdout);\n\n\n\ntypedef long long ll;\n\ntypedef long double dl;\n\ntypedef pair < int , int > pii;\n\ntypedef pair < int , ll > pil;\n\ntypedef pair < ll , int > pli;\n\ntypedef pair < ll , ll > pll;\n\ntypedef pair < int , pii > piii;\n\ntypedef pair < ll, pll > plll;\n\n\n\n\n\nconst ll N = 3e3 + 10;\n\nconst ll mod = 1e9 + 7;\n\nconst ll inf = 2e16;\n\nconst ll rinf = -2e16;\n\nconst ll INF = 1e9 + 10;\n\nconst ll rINF = -1e9 - 10;\n\nconst ll lg = 12;\n\n\n\nll n, subtree[N], val[N][N], dp[N][N], st[N], en[N], timer, parent[N], par[lg][N], lca[N][N], dis[N][N], h[N], ppp[N][N];\n\nvector < int > g[N];\n\nvector < pii > len[N];\n\n\n\nvoid dfs (int v, int p) {\n\n\n\n\tst[v] = ++timer;\n\n\tparent[v] = p;\n\n\tpar[0][v] = p;\n\n\th[v] = h[p] + 1;\n\n\tfor (int i = 1; i < lg; i++) par[i][v] = par[i - 1][par[i - 1][v]];\n\n\tsubtree[v] = 1;\n\n\tfor (auto u : g[v]) {\n\n\n\n\t\tif (p == u) continue;\n\n\t\tdfs (u, v);\n\n\t\tsubtree[v] += subtree[u];\n\n\n\n\t}\n\n\ten[v] = ++timer;\n\n\n\n}\n\n\n\nbool be_anc (int v, int u) {\n\n\n\n\tif (st[v] <= st[u] && en[u] <= en[v]) return 1;\n\n\treturn 0;\n\n\n\n}\n\n\n\nint get_par (int v, int u) {\n\n\n\n\tif (be_anc(u, v)) swap(v, u);\n\n\tfor (int i = lg - 1; i >= 0; --i) {\n\n\n\n\t\tif (!be_anc(par[i][u], v)) u = par[i][u];\n\n\n\n\t}\n\n\treturn u;\n\n\n\n}\n\n\n\nvoid solve() {\n\n\n\n\tcin >> n;\n\n\tfor (int i = 1; i < n; i++) {\n\n\n\n\t\tint v, u; cin >> v >> u;\n\n\t\tg[v].pb(u);\n\n\t\tg[u].pb(v);\n\n\n\n\t}\n\n\tst[0] = -1;\n\n\ten[0] = INF;\n\n\tdfs (1, 0);\n\n\tfor (int i = 1; i <= n; i++) {\n\n\n\n\t\tval[i][i] = subtree[i] * (n - subtree[i] + 1);\n\n\t\tlca[i][i] = i;\n\n\t\tfor (int j = i + 1; j <= n; j++) {\n\n\n\n\t\t\tint pp = get_par(i, j);\n\n\t\t\tppp[i][j] = ppp[j][i] = pp;\n\n\t\t\tint lc = par[0][pp];\n\n\t\t\tif (i == lc) val[i][j] = subtree[j] * (n - subtree[pp]);\n\n\t\t\telse if (j == lc) val[i][j] = subtree[i] * (n - subtree[pp]);\n\n\t\t\telse {\n\n\n\n\t\t\t\tval[i][j] = subtree[i] * subtree[j];\n\n\n\n\t\t\t}\n\n\t\t\tlca[i][j] = lca[j][i] = lc;\n\n\t\t\tdis[i][j] = h[i] + h[j] - (2 * h[lc]);\n\n\t\t\tlen[dis[i][j]].pb({i, j});\n\n\t\t\tval[j][i] = val[i][j];\n\n\n\n\t\t}\n\n\t\tdp[i][i] = 0;\n\n\n\n\t}\n\n\t\/\/ cout << \"lca = \\n\";\n\n\t\/\/ for (int i = 1; i <= n; i++) {\n\n\n\n\t\/\/ \tcout << i << \" : \";\n\n\t\/\/ \tfor (int j = 1; j <= n; j++) cout << j << \" = \" << lca[i][j] << \" , \" << val[i][j] << \" | \";\n\n\t\/\/ \tcout << '\\n';\n\n\n\n\t\/\/ }\n\n\t\/\/ wall__\n\n\t\/\/ cout << \"subtree = \";for (int i = 1; i <= n; i++) cout << i << \" , \" << subtree[i] << \" | \"; cout << '\\n';\n\n\t\/\/ wall__\n\n\n\n\tll ans = 0;\n\n\tfor (int i = 1; i <= n; i++) {\n\n\n\n\t\tif (len[i].sze == 0) continue;\n\n\t\tfor (auto e : len[i]) {\n\n\n\n\t\t\tint v = e.F;\n\n\t\t\tint u = e.S;\n\n\t\t\tif (lca[v][u] == v) \n\n\t\t\t\tdp[v][u] = max(dp[v][parent[u]] + val[v][u], dp[ppp[v][u]][u] + val[v][u]);\n\n\t\t\telse if (lca[v][u] == u)\n\n\t\t\t\tdp[v][u] = max(dp[parent[v]][u] + val[v][u], dp[v][ppp[u][v]] + val[v][u]);\n\n\t\t\telse \n\n\t\t\t\tdp[v][u] = max(dp[parent[v]][u] + val[v][u], dp[v][parent[u]] + val[v][u]);\n\n\t\t\tdp[u][v] = dp[v][u];\n\n\t\t\tans = max(ans, dp[v][u]);\n\n\n\n\t\t}\n\n\n\n\t}\n\n\tcout << ans;\n\n\n\n}\n\n\n\n\n\nint main() {\n\n\tios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n\n\t\n\n\tint t = 1; \n\n\t\/\/ cin >> t;\n\n\twhile (t--) {solve();}\n\n\n\n return 0;\n\n}\n\n\/*\n\n*\/","language":"cpp"} -{"contest_id":"1305","problem_id":"E","statement":"E. Kuroni and the Score Distributiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni is the coordinator of the next Mathforces round written by the \"Proof by AC\" team. All the preparation has been done; and he is discussing with the team about the score distribution for the round.The round consists of nn problems, numbered from 11 to nn. The problems are ordered in increasing order of difficulty, no two problems have the same difficulty. A score distribution for the round can be denoted by an array a1,a2,\u2026,ana1,a2,\u2026,an, where aiai is the score of ii-th problem. Kuroni thinks that the score distribution should satisfy the following requirements: The score of each problem should be a positive integer not exceeding 109109. A harder problem should grant a strictly higher score than an easier problem. In other words, 1\u2264a1\n\n\n\nusing namespace std;\n\n\n\ntypedef long long int ll;\n\ntypedef vector vll;\n\ntypedef pair pll;\n\ntypedef vector vpll;\n\ntypedef map mll;\n\n\n\n#define N 200'006\n\n#define MOD 1000000007\n\n#define FOR(i, n) for (i = 0; i < n; i++)\n\n#define FORR(i, a, b) for (i = a; i <= b; i++)\n\n#define FASTIO \\\n\n ios_base::sync_with_stdio(false); \\\n\n cin.tie(NULL);\n\n#define FREOPEN \\\n\n freopen(\"i.in\", \"r\", stdin); \\\n\n freopen(\"o.out\", \"w\", stdout);\n\n#define gohome \\\n\n cout << \"NO\\n\"; \\\n\n return;\n\n#define arprt(x) \\\n\n for (auto it : x) \\\n\n cout << it << \" \"; \\\n\n cout << \"\\n\";\n\n\n\nint main()\n\n{\n\n FASTIO\n\n ll i, j, n, m, p = -1;\n\n cin >> n >> m;\n\n ll a[n];\n\n ll trips = 0;\n\n for (i = 0; i < n; i++)\n\n {\n\n trips += i \/ 2;\n\n if (trips > m)\n\n {\n\n trips -= i \/ 2;\n\n p = i - 1;\n\n break;\n\n }\n\n }\n\n if (p == -1)\n\n {\n\n if (trips == m)\n\n {\n\n for (i = 1; i <= n; i++)\n\n cout << i << \" \";\n\n }\n\n else\n\n cout << -1;\n\n return 0;\n\n }\n\n for (i = 0; i <= p; i++)\n\n a[i] = i + 1;\n\n\n\n ll t = m - trips;\n\n a[p + 1] = 2 * (p + 1) - 2 * t + 1;\n\n\n\n for (i = p + 2; i < n; i++)\n\n {\n\n a[i] = N * i + 1;\n\n }\n\n arprt(a);\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1286","problem_id":"B","statement":"B. Numbers on Treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEvlampiy was gifted a rooted tree. The vertices of the tree are numbered from 11 to nn. Each of its vertices also has an integer aiai written on it. For each vertex ii, Evlampiy calculated cici\u00a0\u2014 the number of vertices jj in the subtree of vertex ii, such that aj\n\nusing namespace std;\n\ntypedef long long ll;\n\n#define endl '\\n'\n\nconst int maxn=2*1e5+11;\n\nconst int Maxx=1e5+11;\n\nconst int mod=1e9+7;\n\nint t;\n\nint n;\n\nint p[2005],c[2005];\n\nvectorson[2005];\n\nint ans[2005];\n\nint f;\n\nvector dfs(int x)\n\n{\n\n vectortmp;\n\n for(auto P:son[x])\n\n {\n\n vectorres=dfs(P);\n\n tmp.insert(tmp.end(),res.begin(),res.end());\n\n }\n\n if(c[x]>int(tmp.size())) {f=1;return tmp;}\n\n tmp.insert(tmp.begin()+c[x],x);\n\n return tmp;\n\n}\n\nvoid solve()\n\n{\n\n cin>>n;\n\n int root=0;\n\n for(int i=1;i<=n;++i)\n\n {\n\n cin>>p[i]>>c[i];\n\n if(!p[i]) root=i;\n\n else son[p[i]].push_back(i);\n\n }\n\n f=0;\n\n vectorres=dfs(root);\n\n if(f) {cout<<\"NO\"<>t;while(t--)\n\n solve();\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1310","problem_id":"A","statement":"A. Recommendationstime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputVK news recommendation system daily selects interesting publications of one of nn disjoint categories for each user. Each publication belongs to exactly one category. For each category ii batch algorithm selects aiai publications.The latest A\/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of ii-th category within titi seconds. What is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can't remove publications recommended by the batch algorithm.InputThe first line of input consists of single integer nn\u00a0\u2014 the number of news categories (1\u2264n\u22642000001\u2264n\u2264200000).The second line of input consists of nn integers aiai\u00a0\u2014 the number of publications of ii-th category selected by the batch algorithm (1\u2264ai\u22641091\u2264ai\u2264109).The third line of input consists of nn integers titi\u00a0\u2014 time it takes for targeted algorithm to find one new publication of category ii (1\u2264ti\u2264105)1\u2264ti\u2264105).OutputPrint one integer\u00a0\u2014 the minimal required time for the targeted algorithm to get rid of categories with the same size.ExamplesInputCopy5\n3 7 9 7 8\n5 2 5 7 5\nOutputCopy6\nInputCopy5\n1 2 3 4 5\n1 1 1 1 1\nOutputCopy0\nNoteIn the first example; it is possible to find three publications of the second type; which will take 6 seconds.In the second example; all news categories contain a different number of publications.","tags":["data structures","greedy","sortings"],"code":"#include \nusing namespace std;\n\nusing ll = long long;\n\nint main()\n{\n int n; cin >> n;\n vector> A(n);\n for (auto& [x, y] : A)\n cin >> x;\n for (auto& [x, y] : A)\n cin >> y;\n sort(A.begin(), A.end());\n\n multiset, std::greater<>> Q;\n int s = 0;\n ll res{};\n int i = 0;\n while (i < (int)A.size() || !Q.empty())\n {\n while (i < (int)A.size() && A[i][0] == s)\n {\n Q.insert({A[i][1], A[i][0]});\n i++;\n }\n \n if (Q.empty())\n {\n s = A[i][0];\n continue;\n }\n auto [cost, val] = *Q.begin();\n res += (ll)(s - val) * cost;\n Q.erase(Q.begin());\n s++;\n }\n std::cout << res << '\\n';\n}\n","language":"cpp"} -{"contest_id":"1301","problem_id":"A","statement":"A. Three Stringstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three strings aa, bb and cc of the same length nn. The strings consist of lowercase English letters only. The ii-th letter of aa is aiai, the ii-th letter of bb is bibi, the ii-th letter of cc is cici.For every ii (1\u2264i\u2264n1\u2264i\u2264n) you must swap (i.e. exchange) cici with either aiai or bibi. So in total you'll perform exactly nn swap operations, each of them either ci\u2194aici\u2194ai or ci\u2194bici\u2194bi (ii iterates over all integers between 11 and nn, inclusive).For example, if aa is \"code\", bb is \"true\", and cc is \"help\", you can make cc equal to \"crue\" taking the 11-st and the 44-th letters from aa and the others from bb. In this way aa becomes \"hodp\" and bb becomes \"tele\".Is it possible that after these swaps the string aa becomes exactly the same as the string bb?InputThe input consists of multiple test cases. The first line contains a single integer tt (1\u2264t\u22641001\u2264t\u2264100) \u00a0\u2014 the number of test cases. The description of the test cases follows.The first line of each test case contains a string of lowercase English letters aa.The second line of each test case contains a string of lowercase English letters bb.The third line of each test case contains a string of lowercase English letters cc.It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100100.OutputPrint tt lines with answers for all test cases. For each test case:If it is possible to make string aa equal to string bb print \"YES\" (without quotes), otherwise print \"NO\" (without quotes).You can print either lowercase or uppercase letters in the answers.ExampleInputCopy4\naaa\nbbb\nccc\nabc\nbca\nbca\naabb\nbbaa\nbaba\nimi\nmii\niim\nOutputCopyNO\nYES\nYES\nNO\nNoteIn the first test case; it is impossible to do the swaps so that string aa becomes exactly the same as string bb.In the second test case, you should swap cici with aiai for all possible ii. After the swaps aa becomes \"bca\", bb becomes \"bca\" and cc becomes \"abc\". Here the strings aa and bb are equal.In the third test case, you should swap c1c1 with a1a1, c2c2 with b2b2, c3c3 with b3b3 and c4c4 with a4a4. Then string aa becomes \"baba\", string bb becomes \"baba\" and string cc becomes \"abab\". Here the strings aa and bb are equal.In the fourth test case, it is impossible to do the swaps so that string aa becomes exactly the same as string bb.","tags":["implementation","strings"],"code":"#include \n\nusing namespace std;\n\n\n\nint main() {\n\n int t;\n\n\n\n cin >> t; \/\/ Taking the number of test case\n\n\n\n while (t--) {\n\n \/\/ Declaring and taking input in three string variable\n\n string str1, str2, str3;\n\n\n\n cin >> str1 >> str2 >> str3;\n\n\n\n \/\/ looping through all strings and compareing their words in same position\n\n for (int i = 0; i < str1.size(); i++) {\n\n if (str1[i] == str3[i]) {\n\n str2[i] = str3[i]; \/\/ if string1[i] == string3[i] then string2[i] = string3[i]\n\n } else if (str2[i] == str3[i]) {\n\n str1[i] = str3[i]; \/\/ if string2[i] == string3[i] then string1[i] = string3[i]\n\n } else {\n\n str1[i] = str3[i]; \/\/ If none of them are true then just do string1[i] = string3[i]\n\n }\n\n }\n\n\n\n if (str1 == str2) {\n\n cout << \"YES\" << endl;\n\n } else {\n\n cout << \"NO\" << endl;\n\n }\n\n }\n\n\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"13","problem_id":"D","statement":"D. Trianglestime limit per test2 secondsmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes to draw. He drew N red and M blue points on the plane in such a way that no three points lie on the same line. Now he wonders what is the number of distinct triangles with vertices in red points which do not contain any blue point inside.InputThe first line contains two non-negative integer numbers N and M (0\u2009\u2264\u2009N\u2009\u2264\u2009500; 0\u2009\u2264\u2009M\u2009\u2264\u2009500) \u2014 the number of red and blue points respectively. The following N lines contain two integer numbers each \u2014 coordinates of red points. The following M lines contain two integer numbers each \u2014 coordinates of blue points. All coordinates do not exceed 109 by absolute value.OutputOutput one integer \u2014 the number of distinct triangles with vertices in red points which do not contain any blue point inside.ExamplesInputCopy4 10 010 010 105 42 1OutputCopy2InputCopy5 55 106 18 6-6 -77 -15 -110 -4-10 -8-10 5-2 -8OutputCopy7","tags":["dp","geometry"],"code":"#include \"bits\/stdc++.h\"\nusing namespace std;\n\ntypedef double ld;\ntypedef long long ll;\n#define f first\n#define s second\n\nconst int N=500;\nint n,m;\n\nstruct pt{\n int x,y;\n};\n\npt R[N], B[N];\nint cnt[N][N];\n\nint main(){\n cin.tie(0)->sync_with_stdio(0);\n\n cin >> n >> m;\n for(int i=0; i> R[i].x >> R[i].y;\n }\n for(int i=0; i> B[i].x >> B[i].y;\n }\n sort(R, R+n, [&](pt a, pt b){ return a.y u={R[j].x-R[i].x, R[j].y-R[i].y};\n pair v={B[k].x-R[j].x, B[k].y-R[j].y};\n cnt[i][j]+=ll(u.f)*v.s-ll(u.s)*v.f>0;\n }\n }\n }\n\n int ans=0;\n for(int i=0; i\n#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)\n#define ll long long int\n#define rep(i,n) for(ll i=0;i\n#define scanit(vvv,nnn) for(ll i=0; i>vvv[i];\n#define ff first\n#define ss second\n#include \n#include \nusing namespace std;\nusing namespace __gnu_pbds;\ntemplate > using indexed_map = tree;\ntemplate > using indexed_set = indexed_map;\n \n \n void solve(){\n int n;cin>>n;\n vector v(n);scanit(v,n);\n vector> adj(n+1);\n for(int i=0;i>a>>b;\n a--;b--;\n adj[a].pb(b);\n adj[b].pb(a);\n }\n vector dis(n,0);\n rep(i,n){\n if(v[i]==1) v[i] = 1;\n else v[i] = -1;\n }\n function dfs1 = [&](int x,int y){\n dis[x] += v[x];\n for(auto i : adj[x]){\n if(i==y) continue;\n dfs1(i,x);\n dis[x] += max(0,dis[i]);\n }\n };\n dfs1(0,-1);\n \/\/ for(auto i : dis){\n \/\/ cout< dfs = [&](int x,int y){\n \n for(auto i : adj[x]){\n if(i==y) continue;\n dis[i] += max(dis[x] - max(0,dis[i]) ,0);\n dfs(i,x);\n }\n\n };\n dfs(0,-1);\n for(auto i : dis){\n cout<> testcase;\n while (testcase--){\n solve();\n }\n return 0;\n}","language":"cpp"} -{"contest_id":"1296","problem_id":"E1","statement":"E1. String Coloring (easy version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an easy version of the problem. The actual problems are different; but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.You are given a string ss consisting of nn lowercase Latin letters.You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in ss).After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.The goal is to make the string sorted, i.e. all characters should be in alphabetical order.Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.InputThe first line of the input contains one integer nn (1\u2264n\u22642001\u2264n\u2264200) \u2014 the length of ss.The second line of the input contains the string ss consisting of exactly nn lowercase Latin letters.OutputIf it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print \"NO\" (without quotes) in the first line.Otherwise, print \"YES\" in the first line and any correct coloring in the second line (the coloring is the string consisting of nn characters, the ii-th character should be '0' if the ii-th character is colored the first color and '1' otherwise).ExamplesInputCopy9\nabacbecfd\nOutputCopyYES\n001010101\nInputCopy8\naaabbcbb\nOutputCopyYES\n01011011\nInputCopy7\nabcdedc\nOutputCopyNO\nInputCopy5\nabcde\nOutputCopyYES\n00000\n","tags":["constructive algorithms","dp","graphs","greedy","sortings"],"code":"#include\n\n#include\n\n#include\n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\n#define ordered_set tree, rb_tree_tag, tree_order_statistics_node_update>\n\n#define endl '\\n'\n\n#define int long long\n\n#define all(a) a.begin(),a.end()\n\n#define pb push_back\n\n#define mod 1000000007\n\n#define inf 1e18\n\n#define ppb pop_back\n\n#define ff first\n\n#define ss second\n\n \n\n\/\/\/ order_of_key return number of elements less than x -> os.order_of_key(x)\n\n\/\/\/ cout << \"oth element : \" << *os.find_by_order(0) << endl; so it returns value of index\n\n \n\nint lcm(int x,int y)\n\n{\n\n return (x * 1LL * y) \/ __gcd(x,y);\n\n}\n\n \n\n\/\/ Graph on 2D Grid\n\n\/*----------------------Graph Moves----------------*\/\n\n\/\/const int dx[]={+1,-1,+0,+0};\n\n\/\/const int dy[]={+0,+0,+1,-1};\n\n\/\/const int dx[]={+0,+0,+1,-1,-1,+1,-1,+1}; \/\/ Kings Move\n\n\/\/const int dy[]={-1,+1,+0,+0,+1,+1,-1,-1}; \/\/ Kings Move\n\n\/\/const int dx[]={-2, -2, -1, -1, 1, 1, 2, 2}; \/\/ Knights Move\n\n\/\/const int dy[]={-1, 1, -2, 2, -2, 2, -1, 1}; \/\/ Knights Move\n\n\/*------------------------------------------------*\/\n\n \n\n#define debug(x); cerr << #x <<\" \"; _print(x); cerr << endl;\n\n \n\nvoid _print(int t) {cerr << t;}\n\nvoid _print(string t) {cerr << t;}\n\nvoid _print(char t) {cerr << t;}\n\nvoid _print(double t) {cerr << t;}\n\n \n\ntemplate void _print(pair p);\n\ntemplate void _print(vector v);\n\ntemplate void _print(set v);\n\ntemplate void _print(map v);\n\ntemplate void _print(multiset v);\n\ntemplate void _print(pair p) {cerr << \"{\"; _print(p.ff); cerr << \",\"; _print(p.ss); cerr << \"}\";}\n\ntemplate void _print(vector v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\ntemplate void _print(set v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\ntemplate void _print(multiset v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\ntemplate void _print(map v) {cerr << \"[ \"; for (auto i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\n \n\nvoid solve()\n\n{\n\n string s,ans;\n\n int n,i;\n\n cin >> n >> s;\n\n char mx = 'a',one = 'a';\n\n for(auto it : s)\n\n {\n\n if(it >= mx)\n\n {\n\n mx = it;\n\n ans += '0';\n\n }\n\n else if(it >= one)\n\n {\n\n one = it;\n\n ans += '1';\n\n }\n\n else \n\n {\n\n cout << \"NO\" << endl;\n\n return ;\n\n }\n\n }\n\n cout << \"YES\" << endl;\n\n cout << ans << endl;\n\n}\n\n \n\nint32_t main()\n\n{\n\n ios_base::sync_with_stdio(0);\n\n cin.tie(0);\n\n cout.tie(0);\n\n \n\n int t= 1;\n\n while(t--)\n\n {\n\n solve();\n\n }\n\n}\n\n\/**\n\nTest Case :\n\n \n\n**\/","language":"cpp"} -{"contest_id":"1303","problem_id":"B","statement":"B. National Projecttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYour company was appointed to lay new asphalt on the highway of length nn. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are gg days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next bb days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again gg good days, bb bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days 1,2,\u2026,g1,2,\u2026,g are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the n=5n=5 then at least 33 units of the highway should have high quality; if n=4n=4 then at least 22 units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?InputThe first line contains a single integer TT (1\u2264T\u22641041\u2264T\u2264104) \u2014 the number of test cases.Next TT lines contain test cases \u2014 one per line. Each line contains three integers nn, gg and bb (1\u2264n,g,b\u22641091\u2264n,g,b\u2264109) \u2014 the length of the highway and the number of good and bad days respectively.OutputPrint TT integers \u2014 one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.ExampleInputCopy3\n5 1 1\n8 10 10\n1000000 1 1000000\nOutputCopy5\n8\n499999500000\nNoteIn the first test case; you can just lay new asphalt each day; since days 1,3,51,3,5 are good.In the second test case, you can also lay new asphalt each day, since days 11-88 are good.","tags":["math"],"code":"#ifdef LOCAL\n #define _GLIBCXX_DEBUG\n#endif\n#include \"bits\/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\n#define sz(s) ((int)s.size())\n#define all(v) begin(v), end(v)\n\ntypedef long double ld;\nconst int MOD = 1000000007;\n#define ff first\n#define ss second\n\nmt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());\n\n\/\/ *-> KISS*\nint solve() {\n ll n, g, b; cin >> n >> g >> b;\n ll num = (n + 1) \/ 2, leftOut = n - num;\n ll how1 = num \/ g, how2 = num % g;\n ll ans = 0;\n if(how2 == 0) {\n ll ok = (how1 - 1) * b;\n leftOut = max(0LL, leftOut - ok);\n ans += ok + how1 * g;\n ans += leftOut;\n }\n else {\n if(how1 == 0) {\n ans = n;\n }\n else {\n ll ok = (how1) * b;\n leftOut = max(0LL, leftOut - ok);\n ans += ok + how1 * g;\n ans += how2;\n ans += leftOut;\n }\n }\n cout << ans;\n return 0;\n}\nint32_t main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n int TET = 1;\n cin >> TET;\n cout << fixed << setprecision(6);\n for (int i = 1; i <= TET; i++) {\n #ifdef LOCAL\n cout << \"##################\" << '\\n';\n #endif\n if (solve()) {\n break;\n }\n cout << '\\n';\n }\n #ifdef LOCAL\n cout << endl << \"finished in \" << clock() * 1.0 \/ CLOCKS_PER_SEC << \" sec\" << endl;\n #endif\n return 0;\n}\n\/\/ -> Keep It Simple Stupid!","language":"cpp"} -{"contest_id":"1315","problem_id":"C","statement":"C. Restoring Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a sequence b1,b2,\u2026,bnb1,b2,\u2026,bn. Find the lexicographically minimal permutation a1,a2,\u2026,a2na1,a2,\u2026,a2n such that bi=min(a2i\u22121,a2i)bi=min(a2i\u22121,a2i), or determine that it is impossible.InputEach test contains one or more test cases. The first line contains the number of test cases tt (1\u2264t\u22641001\u2264t\u2264100).The first line of each test case consists of one integer nn\u00a0\u2014 the number of elements in the sequence bb (1\u2264n\u22641001\u2264n\u2264100).The second line of each test case consists of nn different integers b1,\u2026,bnb1,\u2026,bn\u00a0\u2014 elements of the sequence bb (1\u2264bi\u22642n1\u2264bi\u22642n).It is guaranteed that the sum of nn by all test cases doesn't exceed 100100.OutputFor each test case, if there is no appropriate permutation, print one number \u22121\u22121.Otherwise, print 2n2n integers a1,\u2026,a2na1,\u2026,a2n\u00a0\u2014 required lexicographically minimal permutation of numbers from 11 to 2n2n.ExampleInputCopy5\n1\n1\n2\n4 1\n3\n4 1 3\n4\n2 3 4 5\n5\n1 5 7 2 8\nOutputCopy1 2 \n-1\n4 5 1 2 3 6 \n-1\n1 3 5 6 7 9 2 4 8 10 \n","tags":["greedy"],"code":"#import\n\nusing namespace std;\nint i,j,n,t,a[240],b[105],p[240];\nmain()\n{\nfor(cin>>t;t--;)\n{\ncin>>n;\nfor(i=0;i++>b[i];\np[b[i]]=1;\n}\nfor(i=0;i++2*n)\n{\ncout<<-1<\n\n \n\n#define file(s) if (fopen(s\".in\", \"r\")) freopen(s\".in\", \"r\", stdin), freopen(s\".out\", \"w\", stdout)\n\n#define optimus_prime ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)\n\n#define fxd(x) fixed << setprecision(x)\n\n#define all(a) (a.begin() , a.end())\n\n#define lwb lower_bound\n\n#define upb upper_bound\n\n#define dl double long\n\n#define ll long long\n\n#define pb push_back\n\n#define sz() size()\n\n#define F first\n\n#define S second\n\n \n\nusing namespace std;\n\n \n\nconst ll N = 1e2+9;\n\n\n\nvoid solve() {\n\n\tvector v;\n\n\tll n;\n\n\tcin >> n;\n\n\tpair p[n+9];\n\n\tfor (int i = 1 ; i <= n ; i++)cin >> p[i].F >> p[i].S;\n\n\tsort (p+1 , p+1+n);\n\n\tll x1=p[1].F , x2=p[1].S;\n\n\twhile (x1--)v.pb('R');\n\n\twhile (x2--)v.pb('U');\n\n\tfor (int i = 2 ; i <= n ; i++){\n\n\t\tif (p[i].F>p[i-1].F&&p[i].Sp[i-1].F&&p[i].S==p[i-1].S){\n\n\t\t\tll x=p[i].F-p[i-1].F;\n\n\t\t\twhile (x--)v.pb('R');\n\n\t\t}\n\n\t\tif (p[i].F!=p[i-1].F&&p[i].S!=p[i-1].S){\n\n\t\t\tll x=p[i].F-p[i-1].F;\n\n\t\t\twhile (x--)v.pb('R');\n\n\t\t\tx=p[i].S-p[i-1].S;\n\n\t\t\twhile (x--)v.pb('U');\n\n\t\t}\n\n\t}\n\n\tcout << \"YES\\n\";\n\n\tfor (int i = 0 ; i < v.sz() ; i++)cout << v[i];\n\n\tcout << \"\\n\";\n\n}\n\n\n\nsigned main() {\n\n optimus_prime;\n\n ll t;\n\n cin >> t;\n\n while (t--)solve();\n\nreturn 0;\n\n}","language":"cpp"} -{"contest_id":"1320","problem_id":"A","statement":"A. Journey Planningtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTanya wants to go on a journey across the cities of Berland. There are nn cities situated along the main railroad line of Berland, and these cities are numbered from 11 to nn. Tanya plans her journey as follows. First of all, she will choose some city c1c1 to start her journey. She will visit it, and after that go to some other city c2>c1c2>c1, then to some other city c3>c2c3>c2, and so on, until she chooses to end her journey in some city ck>ck\u22121ck>ck\u22121. So, the sequence of visited cities [c1,c2,\u2026,ck][c1,c2,\u2026,ck] should be strictly increasing.There are some additional constraints on the sequence of cities Tanya visits. Each city ii has a beauty value bibi associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities cici and ci+1ci+1, the condition ci+1\u2212ci=bci+1\u2212bcici+1\u2212ci=bci+1\u2212bci must hold.For example, if n=8n=8 and b=[3,4,4,6,6,7,8,9]b=[3,4,4,6,6,7,8,9], there are several three possible ways to plan a journey: c=[1,2,4]c=[1,2,4]; c=[3,5,6,8]c=[3,5,6,8]; c=[7]c=[7] (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above.Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?InputThe first line contains one integer nn (1\u2264n\u22642\u22c51051\u2264n\u22642\u22c5105) \u2014 the number of cities in Berland.The second line contains nn integers b1b1, b2b2, ..., bnbn (1\u2264bi\u22644\u22c51051\u2264bi\u22644\u22c5105), where bibi is the beauty value of the ii-th city.OutputPrint one integer \u2014 the maximum beauty of a journey Tanya can choose.ExamplesInputCopy6\n10 7 1 9 10 15\nOutputCopy26\nInputCopy1\n400000\nOutputCopy400000\nInputCopy7\n8 9 26 11 12 29 14\nOutputCopy55\nNoteThe optimal journey plan in the first example is c=[2,4,5]c=[2,4,5].The optimal journey plan in the second example is c=[1]c=[1].The optimal journey plan in the third example is c=[3,6]c=[3,6].","tags":["data structures","dp","greedy","math","sortings"],"code":"#include \n\n\n\nusing namespace std;\n\ntypedef long long ll;\n\n\n\nconst int N = 1e6 + 3;\n\n\n\nint main()\n\n{\n\n cin.tie(0);cin.sync_with_stdio(0);\n\n cout.tie(0);cout.sync_with_stdio(0);\n\n\n\n int n;\n\n cin >> n;\n\n\n\n map fr;\n\n\n\n ll ans =0 ;\n\n for (int i = 0; i < n; ++i) {\n\n int x;\n\n cin >> x;\n\n fr[x - i] += x;\n\n ans = max(ans , fr[x-i] ) ;\n\n }\n\n\n\n cout << ans ;\n\n\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1324","problem_id":"F","statement":"F. Maximum White Subtreetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree consisting of nn vertices. A tree is a connected undirected graph with n\u22121n\u22121 edges. Each vertex vv of this tree has a color assigned to it (av=1av=1 if the vertex vv is white and 00 if the vertex vv is black).You have to solve the following problem for each vertex vv: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex vv? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains cntwcntw white vertices and cntbcntb black vertices, you have to maximize cntw\u2212cntbcntw\u2212cntb.InputThe first line of the input contains one integer nn (2\u2264n\u22642\u22c51052\u2264n\u22642\u22c5105) \u2014 the number of vertices in the tree.The second line of the input contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u226410\u2264ai\u22641), where aiai is the color of the ii-th vertex.Each of the next n\u22121n\u22121 lines describes an edge of the tree. Edge ii is denoted by two integers uiui and vivi, the labels of vertices it connects (1\u2264ui,vi\u2264n,ui\u2260vi(1\u2264ui,vi\u2264n,ui\u2260vi).It is guaranteed that the given edges form a tree.OutputPrint nn integers res1,res2,\u2026,resnres1,res2,\u2026,resn, where resiresi is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex ii.ExamplesInputCopy9\n0 1 1 1 0 0 0 0 1\n1 2\n1 3\n3 4\n3 5\n2 6\n4 7\n6 8\n5 9\nOutputCopy2 2 2 2 2 1 1 0 2 \nInputCopy4\n0 0 1 0\n1 2\n1 3\n1 4\nOutputCopy0 -1 1 -1 \nNoteThe first example is shown below:The black vertices have bold borders.In the second example; the best subtree for vertices 2,32,3 and 44 are vertices 2,32,3 and 44 correspondingly. And the best subtree for the vertex 11 is the subtree consisting of vertices 11 and 33.","tags":["dfs and similar","dp","graphs","trees"],"code":"#include \n\nusing namespace std;\n\n\n\n\/\/hello\n\n\n\n#define int long long\n\n#define inf (1ll<<60)\n\n#define pii pair\n\nint32_t N = 2e6 + 5;\n\nint32_t mod = 1e9 + 7;\n\n\n\nvoid dfs1(int v, int p, vector &a, vector> &adj, vector &cnt)\n\n{\n\n cnt[v] = a[v];\n\n \n\n for(auto u : adj[v])\n\n {\n\n if(u != p)\n\n {\n\n dfs1(u,v,a,adj,cnt);\n\n cnt[v] += max(0ll,cnt[u]);\n\n }\n\n }\n\n}\n\n\n\nvoid dfs2(int v, int p, vector &a, vector> &adj, vector &cnt, vector &ans)\n\n{\n\n ans[v] = cnt[v];\n\n \n\n for(auto u : adj[v])\n\n {\n\n if(u != p)\n\n {\n\n cnt[v] -= max(0ll,cnt[u]);\n\n cnt[u] += max(0ll,cnt[v]);\n\n dfs2(u,v,a,adj,cnt,ans);\n\n cnt[u] -= max(0ll,cnt[v]);\n\n cnt[v] += max(0ll,cnt[u]);\n\n }\n\n }\n\n}\n\n\n\nvoid solveCase()\n\n{\n\n int n=0;\n\n cin >> n;\n\n vector a(n+1);\n\n for(int i=1; i<=n; i++)\n\n {\n\n cin >> a[i];\n\n a[i] = a[i]==0 ? -1 : a[i];\n\n }\n\n \n\n vector> adj(n+1,vector());\n\n vector cnt(n+1,0), ans(n+1,0);\n\n \n\n for(int i=0; i> u >> v;\n\n adj[u].push_back(v);\n\n adj[v].push_back(u);\n\n }\n\n \n\n dfs1(1,-1,a,adj,cnt);\n\n \n\n dfs2(1,-1,a,adj,cnt,ans);\n\n \n\n for(int i=1; i<=n; i++)\n\n cout << ans[i] << \" \";\n\n cout << \"\\n\";\n\n}\n\n\n\nint32_t main()\n\n{\n\n ios::sync_with_stdio(false), cin.tie(NULL);\n\n int t = 1;\n\n \/\/ cin >> t;\n\n while (t--)\n\n solveCase();\n\n}","language":"cpp"} -{"contest_id":"1320","problem_id":"D","statement":"D. Reachable Stringstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn this problem; we will deal with binary strings. Each character of a binary string is either a 0 or a 1. We will also deal with substrings; recall that a substring is a contiguous subsequence of a string. We denote the substring of string ss starting from the ll-th character and ending with the rr-th character as s[l\u2026r]s[l\u2026r]. The characters of each string are numbered from 11.We can perform several operations on the strings we consider. Each operation is to choose a substring of our string and replace it with another string. There are two possible types of operations: replace 011 with 110, or replace 110 with 011. For example, if we apply exactly one operation to the string 110011110, it can be transformed into 011011110, 110110110, or 110011011.Binary string aa is considered reachable from binary string bb if there exists a sequence s1s1, s2s2, ..., sksk such that s1=as1=a, sk=bsk=b, and for every i\u2208[1,k\u22121]i\u2208[1,k\u22121], sisi can be transformed into si+1si+1 using exactly one operation. Note that kk can be equal to 11, i.\u2009e., every string is reachable from itself.You are given a string tt and qq queries to it. Each query consists of three integers l1l1, l2l2 and lenlen. To answer each query, you have to determine whether t[l1\u2026l1+len\u22121]t[l1\u2026l1+len\u22121] is reachable from t[l2\u2026l2+len\u22121]t[l2\u2026l2+len\u22121].InputThe first line contains one integer nn (1\u2264n\u22642\u22c51051\u2264n\u22642\u22c5105) \u2014 the length of string tt.The second line contains one string tt (|t|=n|t|=n). Each character of tt is either 0 or 1.The third line contains one integer qq (1\u2264q\u22642\u22c51051\u2264q\u22642\u22c5105) \u2014 the number of queries.Then qq lines follow, each line represents a query. The ii-th line contains three integers l1l1, l2l2 and lenlen (1\u2264l1,l2\u2264|t|1\u2264l1,l2\u2264|t|, 1\u2264len\u2264|t|\u2212max(l1,l2)+11\u2264len\u2264|t|\u2212max(l1,l2)+1) for the ii-th query.OutputFor each query, print either YES if t[l1\u2026l1+len\u22121]t[l1\u2026l1+len\u22121] is reachable from t[l2\u2026l2+len\u22121]t[l2\u2026l2+len\u22121], or NO otherwise. You may print each letter in any register.ExampleInputCopy5\n11011\n3\n1 3 3\n1 4 2\n1 2 3\nOutputCopyYes\nYes\nNo\n","tags":["data structures","hashing","strings"],"code":"#include\n\nusing namespace std;\n\nint const M=200200,mod1=1e9+7,mod2=998244353;\n\nint i,n,x,y,k,T,cnt[M];char s[M];\n\nlong long Pow1[M],Pow2[M],Hash1[2][M],Hash2[2][M];\n\nint read(){\n\n\tint x=0;char ch=getchar();\n\n\twhile (ch<'0'||ch>'9') ch=getchar();\n\n\twhile (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();\n\n\treturn x;\n\n}\n\nint HASH1(int x,int k){return ((Hash1[x&1][x+k-1]-Hash1[x&1][x-1]*Pow1[cnt[x+k-1]-cnt[x-1]])%mod1+mod1)%mod1;}\n\nint HASH2(int x,int k){return ((Hash2[x&1][x+k-1]-Hash2[x&1][x-1]*Pow2[cnt[x+k-1]-cnt[x-1]])%mod2+mod2)%mod2;}\n\nint main(){\n\n\tscanf(\"%d%s\",&n,s+1);\n\n\tfor (i=Pow1[0]=Pow2[0]=1;i<=n;i++){\n\n\t\tPow1[i]=Pow1[i-1]*233%mod1;Pow2[i]=Pow2[i-1]*2333%mod2;\n\n\t\tHash1[0][i]=Hash1[0][i-1];Hash1[1][i]=Hash1[1][i-1];\n\n\t\tHash2[0][i]=Hash2[0][i-1];Hash2[1][i]=Hash2[1][i-1];\n\n\t\tcnt[i]=cnt[i-1];if (s[i]^48) continue;cnt[i]++;\n\n\t\tHash1[0][i]=(Hash1[0][i]*233+!(i&1)+1)%mod1;Hash1[1][i]=(Hash1[1][i]*233+(i&1)+1)%mod1;\n\n\t\tHash2[0][i]=(Hash2[0][i]*2333+!(i&1)+1)%mod2;Hash2[1][i]=(Hash2[1][i]*2333+(i&1)+1)%mod2;\n\n\t}\t\n\n\tT=read();while (T--){\n\n\t\tx=read();y=read();k=read();\n\n\t\tputs(HASH1(x,k)==HASH1(y,k)&&HASH2(x,k)==HASH2(y,k)?\"Yes\":\"No\");\n\n\t}\n\n\treturn 0;\n\n} ","language":"cpp"} -{"contest_id":"1284","problem_id":"E","statement":"E. New Year and Castle Constructiontime limit per test3 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputKiwon's favorite video game is now holding a new year event to motivate the users! The game is about building and defending a castle; which led Kiwon to think about the following puzzle.In a 2-dimension plane, you have a set s={(x1,y1),(x2,y2),\u2026,(xn,yn)}s={(x1,y1),(x2,y2),\u2026,(xn,yn)} consisting of nn distinct points. In the set ss, no three distinct points lie on a single line. For a point p\u2208sp\u2208s, we can protect this point by building a castle. A castle is a simple quadrilateral (polygon with 44 vertices) that strictly encloses the point pp (i.e. the point pp is strictly inside a quadrilateral). Kiwon is interested in the number of 44-point subsets of ss that can be used to build a castle protecting pp. Note that, if a single subset can be connected in more than one way to enclose a point, it is counted only once. Let f(p)f(p) be the number of 44-point subsets that can enclose the point pp. Please compute the sum of f(p)f(p) for all points p\u2208sp\u2208s.InputThe first line contains a single integer nn (5\u2264n\u226425005\u2264n\u22642500).In the next nn lines, two integers xixi and yiyi (\u2212109\u2264xi,yi\u2264109\u2212109\u2264xi,yi\u2264109) denoting the position of points are given.It is guaranteed that all points are distinct, and there are no three collinear points.OutputPrint the sum of f(p)f(p) for all points p\u2208sp\u2208s.ExamplesInputCopy5\n-1 0\n1 0\n-10 -1\n10 -1\n0 3\nOutputCopy2InputCopy8\n0 1\n1 2\n2 2\n1 3\n0 -1\n-1 -2\n-2 -2\n-1 -3\nOutputCopy40InputCopy10\n588634631 265299215\n-257682751 342279997\n527377039 82412729\n145077145 702473706\n276067232 912883502\n822614418 -514698233\n280281434 -41461635\n65985059 -827653144\n188538640 592896147\n-857422304 -529223472\nOutputCopy213","tags":["combinatorics","geometry","math","sortings"],"code":"#include\n\n#include\n\n#include\n\n#include\n\nusing namespace std;\n\nusing LL = long long;\n\nusing point_t = long long; \/\/\u5168\u5c40\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4fee\u6539\u4e3a long long \u7b49\n\n\n\nconst point_t eps = 1e-8;\n\nconst long double PI = acosl(-1);\n\n\n\n\/\/ \u70b9\u4e0e\u5411\u91cf\n\ntemplate \n\nstruct point{\n\n T x, y;\n\n\n\n bool operator==(const point &a) const { return (abs(x - a.x) <= eps && abs(y - a.y) <= eps); }\n\n bool operator<(const point &a) const{\n\n if (abs(x - a.x) <= eps)\n\n return y < a.y - eps;\n\n return x < a.x - eps;\n\n }\n\n bool operator>(const point &a) const { return !(*this < a || *this == a); }\n\n point operator+(const point &a) const { return {x + a.x, y + a.y}; }\n\n point operator-(const point &a) const { return {x - a.x, y - a.y}; }\n\n point operator-() const { return {-x, -y}; }\n\n point operator*(const T k) const { return {k * x, k * y}; }\n\n point operator\/(const T k) const { return {x \/ k, y \/ k}; }\n\n T operator*(const point &a) const { return x * a.x + y * a.y; } \/\/ \u70b9\u79ef\n\n T operator^(const point &a) const { return x * a.y - y * a.x; } \/\/ \u53c9\u79ef\uff0c\u6ce8\u610f\u4f18\u5148\u7ea7\n\n int toleft(const point &a) const\n\n {\n\n const auto t = (*this) ^ a;\n\n return (t > eps) - (t < -eps);\n\n } \/\/ to-left \u6d4b\u8bd5\n\n T len2() const { return (*this) * (*this); } \/\/ \u5411\u91cf\u957f\u5ea6\u7684\u5e73\u65b9\n\n T dis2(const point &a) const { return (a - (*this)).len2(); } \/\/ \u4e24\u70b9\u8ddd\u79bb\u7684\u5e73\u65b9\n\n\n\n \/\/ \u6d89\u53ca\u6d6e\u70b9\u6570\n\n long double len() const { return sqrtl(len2()); } \/\/ \u5411\u91cf\u957f\u5ea6\n\n long double dis(const point &a) const { return sqrtl(dis2(a)); } \/\/ \u4e24\u70b9\u8ddd\u79bb\n\n long double ang(const point &a) const { return acosl(max(-1.0l, min(1.0l, ((*this) * a) \/ (len() * a.len())))); } \/\/ \u5411\u91cf\u5939\u89d2\n\n point rot(const long double rad) const { return {x * cos(rad) - y * sin(rad), x * sin(rad) + y * cos(rad)}; } \/\/ \u9006\u65f6\u9488\u65cb\u8f6c\uff08\u7ed9\u5b9a\u89d2\u5ea6\uff09\n\n point rot(const long double cosr, const long double sinr) const { return {x * cosr - y * sinr, x * sinr + y * cosr}; } \/\/ \u9006\u65f6\u9488\u65cb\u8f6c\uff08\u7ed9\u5b9a\u89d2\u5ea6\u7684\u6b63\u5f26\u4e0e\u4f59\u5f26\uff09\n\n};\n\n\n\nusing Point = point;\n\n\n\n\/\/ \u6781\u89d2\u6392\u5e8f\n\nstruct argcmp\n\n{\n\n bool operator()(const Point &a, const Point &b) const\n\n {\n\n const auto quad = [](const Point &a)\n\n {\n\n if (a.y < -eps)\n\n return 1;\n\n if (a.y > eps)\n\n return 4;\n\n if (a.x < -eps)\n\n return 5;\n\n if (a.x > eps)\n\n return 3;\n\n return 2;\n\n };\n\n const int qa = quad(a), qb = quad(b);\n\n if (qa != qb)\n\n return qa < qb;\n\n const auto t = a ^ b;\n\n \/\/ if (abs(t)<=eps) return a*a eps;\n\n }\n\n};\n\n\n\nconst int maxn = 5005;\n\nPoint p[maxn], v[maxn];\n\n\n\nint main(){\n\n\n\n#ifdef LOCAL\n\n freopen(\"data.in\", \"r\", stdin);\n\n freopen(\"data.out\", \"w\", stdout);\n\n#endif\n\n\n\n cin.tie(0);\n\n cout.tie(0);\n\n ios::sync_with_stdio(0);\n\n\n\n int n;\n\n cin >> n;\n\n LL C5 = 1;\n\n for(int i = 1; i <= 5; i++)\n\n C5 = C5 * (n - i + 1) \/ i;\n\n\n\n auto C3 = [](int x){\n\n return 1LL * x * (x - 1) * (x - 2) \/ 6;\n\n };\n\n\n\n for(int i = 1; i <= n; i++)\n\n cin >> p[i].x >> p[i].y;\n\n LL cnt = 0;\n\n for(int i = 1; i <= n; i++){\n\n int tot = 0;\n\n for(int j = 1; j <= n; j++){\n\n if (i == j) continue;\n\n v[++tot] = p[j] - p[i];\n\n }\n\n sort(v + 1, v + tot + 1, argcmp());\n\n for(int j = 1; j <= tot; j++) v[j + tot] = v[j];\n\n for(int j = 1, k = 2; j <= tot; j++){\n\n while(k < j + tot && (v[j] ^ v[k]) >= 0) k++;\n\n int c1 = k - j - 1;\n\n int c2 = tot - c1 - 1;\n\n cnt += C3(c1) + C3(c2);\n\n }\n\n }\n\n cout << C5 * 5 - cnt \/ 2 << '\\n';\n\n\n\n}","language":"cpp"} -{"contest_id":"1284","problem_id":"G","statement":"G. Seollaltime limit per test3 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputIt is only a few days until Seollal (Korean Lunar New Year); and Jaehyun has invited his family to his garden. There are kids among the guests. To make the gathering more fun for the kids, Jaehyun is going to run a game of hide-and-seek.The garden can be represented by a n\u00d7mn\u00d7m grid of unit cells. Some (possibly zero) cells are blocked by rocks, and the remaining cells are free. Two cells are neighbors if they share an edge. Each cell has up to 4 neighbors: two in the horizontal direction and two in the vertical direction. Since the garden is represented as a grid, we can classify the cells in the garden as either \"black\" or \"white\". The top-left cell is black, and two cells which are neighbors must be different colors. Cell indices are 1-based, so the top-left corner of the garden is cell (1,1)(1,1).Jaehyun wants to turn his garden into a maze by placing some walls between two cells. Walls can only be placed between neighboring cells. If the wall is placed between two neighboring cells aa and bb, then the two cells aa and bb are not neighboring from that point. One can walk directly between two neighboring cells if and only if there is no wall directly between them. A maze must have the following property. For each pair of free cells in the maze, there must be exactly one simple path between them. A simple path between cells aa and bb is a sequence of free cells in which the first cell is aa, the last cell is bb, all cells are distinct, and any two consecutive cells are neighbors which are not directly blocked by a wall.At first, kids will gather in cell (1,1)(1,1), and start the hide-and-seek game. A kid can hide in a cell if and only if that cell is free, it is not (1,1)(1,1), and has exactly one free neighbor. Jaehyun planted roses in the black cells, so it's dangerous if the kids hide there. So Jaehyun wants to create a maze where the kids can only hide in white cells.You are given the map of the garden as input. Your task is to help Jaehyun create a maze.InputYour program will be judged in multiple test cases.The first line contains the number of test cases tt. (1\u2264t\u22641001\u2264t\u2264100). Afterward, tt test cases with the described format will be given.The first line of a test contains two integers n,mn,m (2\u2264n,m\u2264202\u2264n,m\u226420), the size of the grid.In the next nn line of a test contains a string of length mm, consisting of the following characters (without any whitespace): O: A free cell. X: A rock. It is guaranteed that the first cell (cell (1,1)(1,1)) is free, and every free cell is reachable from (1,1)(1,1). If t\u22652t\u22652 is satisfied, then the size of the grid will satisfy n\u226410,m\u226410n\u226410,m\u226410. In other words, if any grid with size n>10n>10 or m>10m>10 is given as an input, then it will be the only input on the test case (t=1t=1).OutputFor each test case, print the following:If there are no possible mazes, print a single line NO.Otherwise, print a single line YES, followed by a grid of size (2n\u22121)\u00d7(2m\u22121)(2n\u22121)\u00d7(2m\u22121) denoting the found maze. The rules for displaying the maze follows. All cells are indexed in 1-base. For all 1\u2264i\u2264n,1\u2264j\u2264m1\u2264i\u2264n,1\u2264j\u2264m, if the cell (i,j)(i,j) is free cell, print 'O' in the cell (2i\u22121,2j\u22121)(2i\u22121,2j\u22121). Otherwise, print 'X' in the cell (2i\u22121,2j\u22121)(2i\u22121,2j\u22121). For all 1\u2264i\u2264n,1\u2264j\u2264m\u221211\u2264i\u2264n,1\u2264j\u2264m\u22121, if the neighboring cell (i,j),(i,j+1)(i,j),(i,j+1) have wall blocking it, print ' ' in the cell (2i\u22121,2j)(2i\u22121,2j). Otherwise, print any printable character except spaces in the cell (2i\u22121,2j)(2i\u22121,2j). A printable character has an ASCII code in range [32,126][32,126]: This includes spaces and alphanumeric characters. For all 1\u2264i\u2264n\u22121,1\u2264j\u2264m1\u2264i\u2264n\u22121,1\u2264j\u2264m, if the neighboring cell (i,j),(i+1,j)(i,j),(i+1,j) have wall blocking it, print '\u00a0' in the cell (2i,2j\u22121)(2i,2j\u22121). Otherwise, print any printable character except spaces in the cell (2i,2j\u22121)(2i,2j\u22121) For all 1\u2264i\u2264n\u22121,1\u2264j\u2264m\u221211\u2264i\u2264n\u22121,1\u2264j\u2264m\u22121, print any printable character in the cell (2i,2j)(2i,2j). Please, be careful about trailing newline characters or spaces. Each row of the grid should contain exactly 2m\u221212m\u22121 characters, and rows should be separated by a newline character. Trailing spaces must not be omitted in a row.ExampleInputCopy4\n2 2\nOO\nOO\n3 3\nOOO\nXOO\nOOO\n4 4\nOOOX\nXOOX\nOOXO\nOOOO\n5 6\nOOOOOO\nOOOOOO\nOOOOOO\nOOOOOO\nOOOOOO\nOutputCopyYES\nOOO\n O\nOOO\nNO\nYES\nOOOOO X\n O O \nX O O X\n O \nOOO X O\nO O O\nO OOOOO\nYES\nOOOOOOOOOOO\n O O O\nOOO OOO OOO\nO O O \nOOO OOO OOO\n O O O\nOOO OOO OOO\nO O O \nOOO OOO OOO\n","tags":["graphs"],"code":"\/\/ LUOGU_RID: 98792725\n#include\n\n#define gc()(xS==xTT&&(xTT=(xS=xB)+fread(xB,1,1<<20,stdin),xS==xTT)?0:*xS++)\n\n#define pc(x)(p3-obuf<1000000)?(*p3++=x):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x)\n\nusing namespace std;typedef long long ll;typedef double db;typedef long double ld;typedef unsigned long long ull;typedef unsigned int ui;char xch,xB[1<<20],*xS=xB,*xTT=xB,obuf[1000000],*p3=obuf;inline ll read(){char ch=gc();ll x=0,f=1;while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}while('0'<=ch&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=gc();}return x*f;}static char cc[20];templateinline void pt( item x){ int len=0;if(!x)pc('0');if(x<0)x=-x,pc('-');while(x)cc[len++]=x%10+'0',x\/=10;while(len--)pc(cc[len]);}inline void pS(string s){for(int i=0;i'1'){if(ch=='-')f=-1;ch=gc();}while('0'<=ch&&ch<='9'){x=(x<<1)+(ch^48);ch=gc();}return x*f;}\n\nconst int maxn=410,maxh=25;\n\nint h,w,n,a[maxh][maxh],T,tx[4]={0,0,1,-1},ty[4]={1,-1,0,0},hd,u[maxn<<2],v[maxn<<2],cnt,du[maxn],pre[maxn<<2],rt[maxn];bool isx[maxn<<2],isy[maxn<<2],xz[maxn<<2],vis[maxn<<2];char ans[maxh<<1][maxh<<1];\n\nint go(){char c=gc();while(c!='O'&&c!='X')c=gc();return c=='X';}\n\nbool in(int x,int y){return x>=1&&x<=h&&y>=1&&y<=w;}\n\nint get(int x,int y){return (x-1)*w+y;}\n\nint fr(int p){return rt[p]==p?p:rt[p]=fr(rt[p]);}\n\nbool mg(int x,int y)\n\n{\n\n\tx=fr(x),y=fr(y);\n\n\tif(x==y)return 0;\n\n\trt[y]=x;return 1;\n\n}\n\nbool zg()\n\n{\n\n\tmemset(isx,0,sizeof isx),memset(isy,0,sizeof isy),memset(pre,0,sizeof pre),memset(du,0,sizeof du),memset(vis,0,sizeof vis);\n\n\tqueueQ;\n\n\tfor(int i=1;i<=n;i++)rt[i]=i;\n\n\tfor(int i=1;i<=cnt;i++)if(xz[i])du[u[i]]++,du[v[i]]++,mg(u[i],v[i]);\n\n\tfor(int i=1;i<=cnt;i++)\n\n\t{\n\n\t\tif(xz[i])continue;\n\n\t\tif(fr(u[i])!=fr(v[i]))isx[i]=1,Q.push(i),vis[i]=1;\n\n\t\tif(du[u[i]]<2)\n\n\t\t{\n\n\t\t\tisy[i]=1;\n\n\t\t\tif(isx[i]){xz[i]=1;return 1;}\n\n\t\t}\n\n\t}\n\n\twhile(!Q.empty())\n\n\t{\n\n\t\tint p=Q.front();Q.pop();\n\n\t\tif(isy[p])\n\n\t\t{\n\n\t\t\twhile(p)xz[p]^=1,p=pre[p];\n\n\t\t\treturn 1;\n\n\t\t}\n\n\t\tif(xz[p])\n\n\t\t{\n\n\t\t\tfor(int i=1;i<=n;i++)rt[i]=i;\n\n\t\t\tfor(int i=1;i<=cnt;i++)if(xz[i]&&i!=p)mg(u[i],v[i]);\n\n\t\t\tfor(int i=1;i<=cnt;i++)\n\n\t\t\t{\n\n\t\t\t\tif(xz[i]||vis[i]||fr(u[i])==fr(v[i]))continue;\n\n\t\t\t\tpre[i]=p,vis[i]=1,Q.push(i);\n\n\t\t\t}\n\n\t\t}\n\n\t\telse\n\n\t\t{\n\n\t\t\tfor(int i=1;i<=cnt;i++)\n\n\t\t\t{\n\n\t\t\t\tif(!xz[i]||vis[i]||du[u[p]]-(u[i]==u[p])>=2)continue;\n\n\t\t\t\tpre[i]=p,vis[i]=1,Q.push(i);\n\n\t\t\t}\n\n\t\t}\n\n\t}\n\n\treturn 0;\n\n}\n\nsigned main()\n\n{\n\n\tT=read();\n\n\twhile(T--)\n\n\t{\n\n\t\tcnt=0;memset(xz,0,sizeof xz),hd=0;\n\n\t\th=read(),w=read();n=get(h,w);\n\n\t\tfor(int i=1;i<=h;i++)for(int j=1;j<=w;j++)a[i][j]=go();\n\n\t\tfor(int i=1;i<=h;i++)\n\n\t\t{\n\n\t\t\tfor(int j=(i==1?2:1);j<=w;j++)\n\n\t\t\t{\n\n\t\t\t\tif(!(i+j&1)&&!a[i][j])\n\n\t\t\t\t{\n\n\t\t\t\t\thd+=2;\n\n\t\t\t\t\tfor(int t=0;t<4;t++)\n\n\t\t\t\t\t{\n\n\t\t\t\t\t\tint i2=i+tx[t],j2=j+ty[t];\n\n\t\t\t\t\t\tif(in(i2,j2)&&!a[i2][j2])u[++cnt]=get(i,j),v[cnt]=get(i2,j2);\n\n\t\t\t\t\t}\n\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t}\n\n\t\twhile(zg())hd--;\n\n\t\tif(hd)pS(\"NO\\n\");\n\n\t\telse\n\n\t\t{\n\n\t\t\tpS(\"YES\\n\");\n\n\t\t\tif(!a[1][2])u[++cnt]=get(1,1),v[cnt]=get(1,2);\n\n\t\t\tif(!a[2][1])u[++cnt]=get(1,1),v[cnt]=get(2,1);\n\n\t\t\tfor(int i=1;i\n\nusing namespace std;\n\n#define all(v)\t\t\t\t((v).begin()), ((v).end())\n\n#define rall(v)\t\t\t\t((v).rbegin()), ((v).rend())\n\n#define F first\n\n#define S second\n\n#define oo 1e18+5\n\n#define MOD ll(1e9+7)\n\n\/\/#define endl '\\n'\n\n#define fvec(i,vec) for(auto i:vec)\n\n#define pb push_back\n\n#define mpr make_pair\n\n#define min3(a,b,c) min(a,min(b,c))\n\n#define max3(a,b,c) max(a,max(b,c))\n\n# define M_PI 3.14159265358979323846\n\n#define foor(i,a,b) for (ll i = a; i < b; i++)\n\n#define num_ocur_insort(vec,x) equal_range(all(vec), x) \/* return pair upper lower auto r=num_ocur_insort(a,6);*\/\n\ntypedef long long ll;\n\ntypedef unsigned long long ull;\n\ntypedef vector vi;\n\ntypedef vector vii;\n\ntypedef pair pi;\n\ntypedef vector vip;\n\ntypedef vector vss;\n\ntypedef map mapi;\n\ntypedef unordered_map umapi;\n\nstruct bebo {ll fir;ll sec;ll thi;ll fot;};\n\ntypedef vector viip;\n\nll gcd(ll a, ll b) { return ((b == 0) ? a : gcd(b, a % b)); }\n\nconst int N=8*1e5;\n\n\n\nll add(ll a, ll b) {\n\n return ((a % MOD + b % MOD) + MOD) % MOD;\n\n}\n\n\n\nll multi(ll a, ll b) {\n\n return ((a % MOD * b % MOD) + MOD) % MOD;\n\n}\n\n\n\nll fastpow(ll a, ll b) {\n\n if (b == 0)return ll(1);\n\n ll temp = fastpow(a, b \/ 2);\n\n ll total = multi(temp, temp);\n\n if (b % 2 == 1) {\n\n total = multi(total, a);\n\n }\n\n return total;\n\n}\n\nint fact[N], inv[N];\n\nint nCr( ll n , ll r)\n\n{\n\n return multi(fact[n],multi(inv[n-r],inv[r]));\n\n}\n\n\n\nvoid solve() {\n\n ll n, m;\n\n cin >> n >> m;\n\n cout << nCr(n + 2 * m - 1, 2 * m) << endl;\n\n}\n\nint main() {\n\n ios_base::sync_with_stdio(0);\n\n cin.tie(0);\n\n cout.tie(0);\n\n ll t = 1;\n\n \/\/ cin >> t;\n\n fact[0] = inv[0] = 1;\n\n for (int i = 1; i <= N; i++) {\n\n fact[i] = multi(i, fact[i - 1]);\n\n inv[i] = fastpow(fact[i], MOD - 2);\n\n }\n\n while (t--) {\n\n solve();\n\n }\n\n}\n\n","language":"cpp"} -{"contest_id":"1323","problem_id":"A","statement":"A. Even Subset Sum Problemtime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa consisting of nn positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by 22) or determine that there is no such subset.Both the given array and required subset may contain equal values.InputThe first line contains a single integer tt (1\u2264t\u22641001\u2264t\u2264100), number of test cases to solve. Descriptions of tt test cases follow.A description of each test case consists of two lines. The first line contains a single integer nn (1\u2264n\u22641001\u2264n\u2264100), length of array aa.The second line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641001\u2264ai\u2264100), elements of aa. The given array aa can contain equal values (duplicates).OutputFor each test case output \u22121\u22121 if there is no such subset of elements. Otherwise output positive integer kk, number of elements in the required subset. Then output kk distinct integers (1\u2264pi\u2264n1\u2264pi\u2264n), indexes of the chosen elements. If there are multiple solutions output any of them.ExampleInputCopy3\n3\n1 4 3\n1\n15\n2\n3 5\nOutputCopy1\n2\n-1\n2\n1 2\nNoteThere are three test cases in the example.In the first test case; you can choose the subset consisting of only the second element. Its sum is 44 and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum.","tags":["brute force","dp","greedy","implementation"],"code":"#include\n\n\/\/#include \n\n\/\/#include \n\n#define ll long long\n\n#define ull unsigned long long\n\n#define fixed(name) fixed << setprecision(name)\n\n#define all(s) s.begin(), s.end()\n\n#define rall(s) s.rbegin(), s.rend()\n\n#define pi 3.14159265359\n\n#define getline(s) getline(cin >> ws, s)\n\n#define dl \"\\n\"\n\n#define cin(v) for(auto &i: v) cin>>i\n\n#define cout(v) for(auto &i: v) cout<>\n\n\/\/ using ordered_map = tree;\n\n\/\/ template >\n\n\/\/ using ordered_set = ordered_map;\n\n\n\n\/\/ template >\n\n\/\/ using ordered_multimap = tree;\n\n\/\/ template >\n\n\/\/ using ordered_multiset = ordered_multimap;\n\n\n\ntemplate < typename T = int > istream& operator >> (istream &in, vector < T > &v) {\n\n for (auto &x : v) in >> x;\n\n return in;\n\n}\n\ntemplate < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {\n\n for (const T &x : v) out << x << ' ';\n\n return out;\n\n}\n\n\/\/ summation n*(n+1)\/2 -> (-1+ sqrt(1+8*x))\/2\n\n\/\/ 2D vector vector>v(n,vector(m, -1)) ;\n\nbool is_vowel (char c){\n\n if(c == 'a' || c=='u' || c=='i' || c=='e' || c=='o' ) return true;\n\n else return false;\n\n}\n\nvoid solve(){\n\n int n, sum=0, c=0, i=0; cin>>n;\n\n vectorv(n); cin>>v;\n\n for(i=0; i 0 && sum%2 == 0) break;\n\n else {sum+=v[i]; c++;}\n\n }\n\n if(sum > 0 && sum%2 == 0){\n\n cout<>t;\n\n while(t--){\n\n solve();\n\n if(t) cout << dl ; \n\n }\n\n}","language":"cpp"} -{"contest_id":"1324","problem_id":"D","statement":"D. Pair of Topicstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe next lecture in a high school requires two topics to be discussed. The ii-th topic is interesting by aiai units for the teacher and by bibi units for the students.The pair of topics ii and jj (ibi+bjai+aj>bi+bj (i.e. it is more interesting for the teacher).Your task is to find the number of good pairs of topics.InputThe first line of the input contains one integer nn (2\u2264n\u22642\u22c51052\u2264n\u22642\u22c5105) \u2014 the number of topics.The second line of the input contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641091\u2264ai\u2264109), where aiai is the interestingness of the ii-th topic for the teacher.The third line of the input contains nn integers b1,b2,\u2026,bnb1,b2,\u2026,bn (1\u2264bi\u22641091\u2264bi\u2264109), where bibi is the interestingness of the ii-th topic for the students.OutputPrint one integer \u2014 the number of good pairs of topic.ExamplesInputCopy5\n4 8 2 6 2\n4 5 4 1 3\nOutputCopy7\nInputCopy4\n1 3 2 4\n1 3 2 4\nOutputCopy0\n","tags":["binary search","data structures","sortings","two pointers"],"code":"#include \n\nusing namespace std;\n\nint main()\n\n{\n\n ios::sync_with_stdio(0);\n\n cin.tie(0);\n\n long long t,i,j,p=0,q=0;\n\n\n\n\n\n int n;\n\n cin >> n;\n\n long long a[n], b[n] ,d[n],c[200005]={0};\n\n\n\n for (i=0;i> a[i];\n\n }\n\n for (i=0;i> b[i];\n\n c[i] = a[i] - b[i];\n\n d[i] = c[i];\n\n }\n\n sort (c,c+n);\n\n q = 0;\n\n for (i=0;i\n\n\n\nusing namespace std;\n\nusing ll = long long;\n\nusing iii = tuple;\n\nusing viii = vector;\n\n\n\nint main() {\n\n cin.tie(0), ios::sync_with_stdio(0);\n\n ll q, n, m, t, l, h;\n\n cin >> q;\n\n while (q--) {\n\n cin >> n >> m;\n\n viii a(n);\n\n for (int i = 0; i < n; i++) {\n\n cin >> t >> l >> h;\n\n a[i] = {t, l, h};\n\n }\n\n ll t0 = 0, mn = m, mx = m;\n\n bool ok = 1;\n\n for (int i = 0; i < n; i++) {\n\n tie(t, l, h) = a[i];\n\n mn -= (t-t0);\n\n mx += (t-t0);\n\n if (l > mx || h < mn) { ok = 0; break; }\n\n mn = max(mn, l);\n\n mx = min(mx, h);\n\n t0 = t;\n\n }\n\n cout << (ok?\"YES\\n\":\"NO\\n\");\n\n }\n\n}","language":"cpp"} -{"contest_id":"1323","problem_id":"B","statement":"B. Count Subrectanglestime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn and array bb of length mm both consisting of only integers 00 and 11. Consider a matrix cc of size n\u00d7mn\u00d7m formed by following rule: ci,j=ai\u22c5bjci,j=ai\u22c5bj (i.e. aiai multiplied by bjbj). It's easy to see that cc consists of only zeroes and ones too.How many subrectangles of size (area) kk consisting only of ones are there in cc?A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x1,x2,y1,y2x1,x2,y1,y2 (1\u2264x1\u2264x2\u2264n1\u2264x1\u2264x2\u2264n, 1\u2264y1\u2264y2\u2264m1\u2264y1\u2264y2\u2264m) a subrectangle c[x1\u2026x2][y1\u2026y2]c[x1\u2026x2][y1\u2026y2] is an intersection of the rows x1,x1+1,x1+2,\u2026,x2x1,x1+1,x1+2,\u2026,x2 and the columns y1,y1+1,y1+2,\u2026,y2y1,y1+1,y1+2,\u2026,y2.The size (area) of a subrectangle is the total number of cells in it.InputThe first line contains three integers nn, mm and kk (1\u2264n,m\u226440000,1\u2264k\u2264n\u22c5m1\u2264n,m\u226440000,1\u2264k\u2264n\u22c5m), length of array aa, length of array bb and required size of subrectangles.The second line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u226410\u2264ai\u22641), elements of aa.The third line contains mm integers b1,b2,\u2026,bmb1,b2,\u2026,bm (0\u2264bi\u226410\u2264bi\u22641), elements of bb.OutputOutput single integer\u00a0\u2014 the number of subrectangles of cc with size (area) kk consisting only of ones.ExamplesInputCopy3 3 2\n1 0 1\n1 1 1\nOutputCopy4\nInputCopy3 5 4\n1 1 1\n1 1 1 1 1\nOutputCopy14\nNoteIn first example matrix cc is: There are 44 subrectangles of size 22 consisting of only ones in it: In second example matrix cc is: ","tags":["binary search","greedy","implementation"],"code":"\/\/Made By Phuong Nam PROPTIT <3\/\/\n\n#pragma GCC Optimize(\"O3\")\n\n#include\n\n#include \n\n#include \n\n#define f(i,a,b) for(int i=a;i<=b;i++)\n\n#define f1(i,n) for(int i=1;i<=n;i++)\n\n#define f0(i,n) for(int i=0;i=a;i--)\n\n#define el cout<<'\\n'\n\n#define fi first\n\n#define se second\n\n#define pb push_back\n\n#define pk pop_back\n\n#define vi vector\n\n#define vl vector\n\n#define pii pair\n\n#define pll pair\n\n#define all(s) s.begin(),s.end()\n\n#define oset tree,rb_tree_tag,tree_order_statistics_node_update>\n\nusing namespace __gnu_pbds;\n\nusing namespace __gnu_cxx;\n\nusing namespace std;\n\ntypedef long long ll;\n\nconst int N=1e6+3;\n\nconst int MOD=1e9+7;\n\nint n,m,k,a[N],b[N];\n\nll dem=0;\n\nsets;\n\nll tinh(int x,int y)\n\n{\n\n ll s1=0,s2=0;\n\n f1(i,n)\n\n {\n\n if(a[i])\n\n {\n\n int j=i;\n\n while(j<=n&&a[j]==1) j++;\n\n j--;\n\n s1+=max(0,((j-i+1)-x+1));\n\n i=j;\n\n }\n\n }\n\n f1(i,m)\n\n {\n\n if(b[i])\n\n {\n\n int j=i;\n\n while(j<=m&&b[j]==1) j++;\n\n j--;\n\n s2+=max(0,((j-i+1)-y+1));\n\n i=j;\n\n }\n\n }\n\n return s1*s2;\n\n}\n\nvoid xuly()\n\n{\n\n cin>>n>>m>>k;\n\n f1(i,n) cin>>a[i];\n\n f1(i,m) cin>>b[i];\n\n s.insert({1,k});\n\n f(i,2,sqrt(k))\n\n {\n\n if(k%i==0) s.insert({min(i,k\/i),max(i,k\/i)});\n\n }\n\n for(auto i:s)\n\n {\n\n int x=i.fi,y=i.se;\n\n dem+=tinh(x,y);\n\n if(x!=y) dem+=tinh(y,x);\n\n }\n\n cout<>t;\n\n while(t--) xuly();\n\n}\n\n\/\/-----YEU CODE HON CRUSH-----\/\/\n\n","language":"cpp"} -{"contest_id":"1301","problem_id":"F","statement":"F. Super Jabertime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputJaber is a superhero in a large country that can be described as a grid with nn rows and mm columns, where every cell in that grid contains a different city.Jaber gave every city in that country a specific color between 11 and kk. In one second he can go from the current city to any of the cities adjacent by the side or to any city with the same color as the current city color.Jaber has to do qq missions. In every mission he will be in the city at row r1r1 and column c1c1, and he should help someone in the city at row r2r2 and column c2c2.Jaber wants your help to tell him the minimum possible time to go from the starting city to the finishing city for every mission.InputThe first line contains three integers nn, mm and kk (1\u2264n,m\u226410001\u2264n,m\u22641000, 1\u2264k\u2264min(40,n\u22c5m)1\u2264k\u2264min(40,n\u22c5m))\u00a0\u2014 the number of rows, columns and colors.Each of the next nn lines contains mm integers. In the ii-th line, the jj-th integer is aijaij (1\u2264aij\u2264k1\u2264aij\u2264k), which is the color assigned to the city in the ii-th row and jj-th column.The next line contains one integer qq (1\u2264q\u22641051\u2264q\u2264105) \u00a0\u2014 the number of missions.For the next qq lines, every line contains four integers r1r1, c1c1, r2r2, c2c2 (1\u2264r1,r2\u2264n1\u2264r1,r2\u2264n, 1\u2264c1,c2\u2264m1\u2264c1,c2\u2264m) \u00a0\u2014 the coordinates of the starting and the finishing cities of the corresponding mission.It is guaranteed that for every color between 11 and kk there is at least one city of that color.OutputFor every mission print the minimum possible time to reach city at the cell (r2,c2)(r2,c2) starting from city at the cell (r1,c1)(r1,c1).ExamplesInputCopy3 4 5\n1 2 1 3\n4 4 5 5\n1 2 1 3\n2\n1 1 3 4\n2 2 2 2\nOutputCopy2\n0\nInputCopy4 4 8\n1 2 2 8\n1 3 4 7\n5 1 7 6\n2 3 8 8\n4\n1 1 2 2\n1 1 3 4\n1 1 2 4\n1 1 4 4\nOutputCopy2\n3\n3\n4\nNoteIn the first example: mission 11: Jaber should go from the cell (1,1)(1,1) to the cell (3,3)(3,3) because they have the same colors, then from the cell (3,3)(3,3) to the cell (3,4)(3,4) because they are adjacent by side (two moves in total); mission 22: Jaber already starts in the finishing cell. In the second example: mission 11: (1,1)(1,1) \u2192\u2192 (1,2)(1,2) \u2192\u2192 (2,2)(2,2); mission 22: (1,1)(1,1) \u2192\u2192 (3,2)(3,2) \u2192\u2192 (3,3)(3,3) \u2192\u2192 (3,4)(3,4); mission 33: (1,1)(1,1) \u2192\u2192 (3,2)(3,2) \u2192\u2192 (3,3)(3,3) \u2192\u2192 (2,4)(2,4); mission 44: (1,1)(1,1) \u2192\u2192 (1,2)(1,2) \u2192\u2192 (1,3)(1,3) \u2192\u2192 (1,4)(1,4) \u2192\u2192 (4,4)(4,4). ","tags":["dfs and similar","graphs","implementation","shortest paths"],"code":"#include\n\n#include \n\n#include \n\nusing namespace std;\n\nusing L = __int128;\n\n#include\n\n#include \n\nusing namespace __gnu_pbds;\n\nusing ll = long long;\n\nusing ull = unsigned long long;\n\nusing ld = long double;\n\n#define nd \"\\n\"\n\n#define all(x) (x).begin(), (x).end()\n\n#define lol cout <<\"i am here\"<\n\nvoid print(container v) { for (auto& it : v) cout << it << ' ' ;cout <\n\nll fp(ll a , ll p){ if(!p) return 1; ll v = fp(a , p\/2); v*=v;return p & 1 ? v*a : v; }\n\nll inf = 1e9+5 , mod= 1e9+7;\/\/998244353;\/\/\n\ntemplate using ordered_set = tree, rb_tree_tag,tree_order_statistics_node_update>;\n\nconst int N = 1e6,LOG = 12;\n\nll mul (ll a, ll b){\n\n return ( ( a % mod ) * ( b % mod ) ) % mod;\n\n}\n\nll add (ll a , ll b){\n\n return (a + b + mod) % mod;\n\n}\n\n\n\ntemplate< typename T > using min_heap = priority_queue , greater < T > > ;\n\n\/\/ m n\n\n\n\nll f (ll a ,ll b){\n\n return (a + b - 1)\/ b;\n\n}\n\nll Fp (ll x , ll p){\n\n if (!p) return 1;\n\n ll v = Fp(x , p\/2);\n\n v = mul(v , v);\n\n if (p & 1)\n\n v = mul(x , v);\n\n return v;\n\n}\n\nint dx [] {0 , 0 , 1 ,-1};\n\nint dy [] {1 ,-1 , 0 , 0};\n\n\n\nvoid main_(int tc){\n\n ll n , m , k;scanf(\"%lli%lli%lli\" , &n , &m , & k);\n\n vector < vector > grid(n , vector (m));\n\n vector < vector > > nodes (k+5);\n\n for (int i = 0; i < n; ++i){\n\n for (int j = 0; j < m; ++j){\n\n scanf(\"%d\" , &grid[i][j]);\n\n nodes[grid[i][j]].emplace_back(i , j);\n\n }\n\n }\n\n\n\n vector < vector < vector > > dist(40+5 , vector < vector > (n+5 , vector (m+5 , inf)));\n\n\n\n auto v =[&] (pair p)-> bool{\n\n return p.first >=0 && p.second >=0 && p.first > q;\n\n vector < bool > mark(k+5);\n\n for (auto &i : nodes[c])\n\n q.emplace_back(i.first , i.second) ,dist[c][i.first][i.second] = 0;\n\n mark[c] = 1;\n\n while (!q.empty()){\n\n auto top = q.front(); q.pop_front();\n\n if (!mark[grid[top.first][top.second]]){\n\n mark[grid[top.first][top.second]] =1;\n\n for (auto &i : nodes[grid[top.first][top.second]]){\n\n if (dist[c][i.first][i.second] > dist[c][top.first][top.second]+ 1) {\n\n dist[c][i.first][i.second] = 1 + dist[c][top.first][top.second];\n\n q.emplace_back(i.first, i.second);\n\n }\n\n }\n\n }\n\n\n\n for (int i = 0; i < 4; ++i){\n\n int nr = top.first + dx[i];\n\n int nc = top.second + dy[i];\n\n if (!v({nr , nc})) continue;\n\n if (dist[c][nr][nc] > dist[c][top.first][ top.second]+1){\n\n q.emplace_back( nr , nc);\n\n dist[c][nr][nc] =dist[c][top.first][top.second]+1;\n\n }\n\n }\n\n }\n\n };\n\n\n\n for (int i = 1; i <= k; ++i) bfs(i);\n\n\n\n int q ,r1 , c1 , r2 , c2;\n\n scanf(\"%d\" , &q);\n\n while (q--){\n\n scanf(\"%d%d%d%d\" , &r1 , &c1 , &r2 , &c2);--r1 , --r2 , --c1 , --c2;\n\n int ans = abs(r1-r2) + abs(c1-c2);\n\n for (int i = 1; i <= k; ++i) ans = min(ans , 1 + dist[i][r1][c1] + dist[i][r2][c2]);\n\n printf(\"%d\\n\" , ans);\n\n \/\/\/cout << ans << nd;\n\n }\n\n\n\n\n\n\n\n \n\n}\n\nint main(){\n\n \/\/ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);\n\n \/\/the_best_is_still_yet_to_come();\n\n \/\/freopen(\"red2.in \",\"r\",stdin);\/\/ freopen(\"output.txt\",\"w\",stdout);\n\n int tt = 1 , tc = 0;\n\n \/\/ cin>>tt;\n\n while(tt--) main_(++tc);\n\n#ifndef ONLINE_JUDGE\n\n cout << \"Running Time: \" << 1.0 * clock() \/ CLOCKS_PER_SEC << \" s .\\n\";\n\n#endif\n\n return 0;\n\n}\n\n\/*\n\n * if we dont use cells from the same color the ans = manhattan distance\n\n * else we will see for from each color what is the min distance to reach this cell by bfs\n\n * and add edge between the two cells from the same color with cost one\n\n *\n\n *\/\n\n\n\n","language":"cpp"} -{"contest_id":"1301","problem_id":"F","statement":"F. Super Jabertime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputJaber is a superhero in a large country that can be described as a grid with nn rows and mm columns, where every cell in that grid contains a different city.Jaber gave every city in that country a specific color between 11 and kk. In one second he can go from the current city to any of the cities adjacent by the side or to any city with the same color as the current city color.Jaber has to do qq missions. In every mission he will be in the city at row r1r1 and column c1c1, and he should help someone in the city at row r2r2 and column c2c2.Jaber wants your help to tell him the minimum possible time to go from the starting city to the finishing city for every mission.InputThe first line contains three integers nn, mm and kk (1\u2264n,m\u226410001\u2264n,m\u22641000, 1\u2264k\u2264min(40,n\u22c5m)1\u2264k\u2264min(40,n\u22c5m))\u00a0\u2014 the number of rows, columns and colors.Each of the next nn lines contains mm integers. In the ii-th line, the jj-th integer is aijaij (1\u2264aij\u2264k1\u2264aij\u2264k), which is the color assigned to the city in the ii-th row and jj-th column.The next line contains one integer qq (1\u2264q\u22641051\u2264q\u2264105) \u00a0\u2014 the number of missions.For the next qq lines, every line contains four integers r1r1, c1c1, r2r2, c2c2 (1\u2264r1,r2\u2264n1\u2264r1,r2\u2264n, 1\u2264c1,c2\u2264m1\u2264c1,c2\u2264m) \u00a0\u2014 the coordinates of the starting and the finishing cities of the corresponding mission.It is guaranteed that for every color between 11 and kk there is at least one city of that color.OutputFor every mission print the minimum possible time to reach city at the cell (r2,c2)(r2,c2) starting from city at the cell (r1,c1)(r1,c1).ExamplesInputCopy3 4 5\n1 2 1 3\n4 4 5 5\n1 2 1 3\n2\n1 1 3 4\n2 2 2 2\nOutputCopy2\n0\nInputCopy4 4 8\n1 2 2 8\n1 3 4 7\n5 1 7 6\n2 3 8 8\n4\n1 1 2 2\n1 1 3 4\n1 1 2 4\n1 1 4 4\nOutputCopy2\n3\n3\n4\nNoteIn the first example: mission 11: Jaber should go from the cell (1,1)(1,1) to the cell (3,3)(3,3) because they have the same colors, then from the cell (3,3)(3,3) to the cell (3,4)(3,4) because they are adjacent by side (two moves in total); mission 22: Jaber already starts in the finishing cell. In the second example: mission 11: (1,1)(1,1) \u2192\u2192 (1,2)(1,2) \u2192\u2192 (2,2)(2,2); mission 22: (1,1)(1,1) \u2192\u2192 (3,2)(3,2) \u2192\u2192 (3,3)(3,3) \u2192\u2192 (3,4)(3,4); mission 33: (1,1)(1,1) \u2192\u2192 (3,2)(3,2) \u2192\u2192 (3,3)(3,3) \u2192\u2192 (2,4)(2,4); mission 44: (1,1)(1,1) \u2192\u2192 (1,2)(1,2) \u2192\u2192 (1,3)(1,3) \u2192\u2192 (1,4)(1,4) \u2192\u2192 (4,4)(4,4). ","tags":["dfs and similar","graphs","implementation","shortest paths"],"code":"#include\n\n#define pb push_back\n\n#define mk make_pair\n\nusing namespace std;\n\nint const M=1010,inf=1e9,dx[]={-1,1,0,0},dy[]={0,0,-1,1};\n\nvector>g[42];queue>q;bool vis[42];\n\nint i,j,n,m,k,c,r1,c1,r2,c2,x,y,T,X,Y,Min,col[M][M],dis[42][M][M];\n\nint read(){\n\n\tint x=0;char ch=getchar();\n\n\twhile (ch<'0'||ch>'9') ch=getchar();\n\n\twhile (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();\n\n\treturn x;\n\n}\n\nint main(){\n\n\tn=read();m=read();k=read();\n\n\tmemset(dis,63,sizeof(dis));\n\n\tfor (i=1;i<=n;i++) for (j=1;j<=m;j++)\n\n\t\tg[col[i][j]=read()].pb(mk(i,j));\n\n\tfor (T=1;T<=k;T++){ memset(vis,0,sizeof(vis));\n\n\t\tfor (i=0;iinf)\n\n\t\t\t\t\tdis[T][X][Y]=dis[T][x][y]+1,q.push(mk(X,Y));\n\n\t\t\t} c=col[x][y];\n\n\t\t\tif (vis[c]) continue;vis[c]=1;\n\n\t\t\tfor (i=0;iinf)\n\n\t\t\t\t\tdis[T][g[c][i].first][g[c][i].second]=dis[T][x][y]+1,q.push(g[c][i]); \n\n\t\t}\n\n\t}\n\n\tT=read();while (T--){\n\n\t\tr1=read();c1=read();r2=read();c2=read();Min=abs(r1-r2)+abs(c1-c2);\n\n\t\tfor (i=1;i<=k;i++) Min=min(Min,dis[i][r1][c1]+dis[i][r2][c2]+1);\n\n\t\tprintf(\"%d\\n\",Min);\n\n\t}\n\n\treturn 0;\n\n} ","language":"cpp"} -{"contest_id":"1304","problem_id":"C","statement":"C. Air Conditionertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGildong owns a bulgogi restaurant. The restaurant has a lot of customers; so many of them like to make a reservation before visiting it.Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant.The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially.Each customer is characterized by three values: titi \u2014 the time (in minutes) when the ii-th customer visits the restaurant, lili \u2014 the lower bound of their preferred temperature range, and hihi \u2014 the upper bound of their preferred temperature range.A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the ii-th customer is satisfied if and only if the temperature is between lili and hihi (inclusive) in the titi-th minute.Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers.InputEach test contains one or more test cases. The first line contains the number of test cases qq (1\u2264q\u22645001\u2264q\u2264500). Description of the test cases follows.The first line of each test case contains two integers nn and mm (1\u2264n\u22641001\u2264n\u2264100, \u2212109\u2264m\u2264109\u2212109\u2264m\u2264109), where nn is the number of reserved customers and mm is the initial temperature of the restaurant.Next, nn lines follow. The ii-th line of them contains three integers titi, lili, and hihi (1\u2264ti\u22641091\u2264ti\u2264109, \u2212109\u2264li\u2264hi\u2264109\u2212109\u2264li\u2264hi\u2264109), where titi is the time when the ii-th customer visits, lili is the lower bound of their preferred temperature range, and hihi is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive.The customers are given in non-decreasing order of their visit time, and the current time is 00.OutputFor each test case, print \"YES\" if it is possible to satisfy all customers. Otherwise, print \"NO\".You can print each letter in any case (upper or lower).ExampleInputCopy4\n3 0\n5 1 2\n7 3 5\n10 -1 0\n2 12\n5 7 10\n10 16 20\n3 -100\n100 0 0\n100 -50 50\n200 100 100\n1 100\n99 -100 0\nOutputCopyYES\nNO\nYES\nNO\nNoteIn the first case; Gildong can control the air conditioner to satisfy all customers in the following way: At 00-th minute, change the state to heating (the temperature is 0). At 22-nd minute, change the state to off (the temperature is 2). At 55-th minute, change the state to heating (the temperature is 2, the 11-st customer is satisfied). At 66-th minute, change the state to off (the temperature is 3). At 77-th minute, change the state to cooling (the temperature is 3, the 22-nd customer is satisfied). At 1010-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at 00-th minute and leave it be. Then all customers will be satisfied. Note that the 11-st customer's visit time equals the 22-nd customer's visit time.In the second and the fourth case, Gildong has to make at least one customer unsatisfied.","tags":["dp","greedy","implementation","sortings","two pointers"],"code":"#include \n\n#include \n\n#include \n\n\n\nusing namespace __gnu_pbds;\n\nusing namespace std;\n\n\n\ntypedef long long ll;\n\ntypedef tree,rb_tree_tag,\n\n tree_order_statistics_node_update> indexed_set;\n\ntypedef priority_queue,greater> min_priority_queue;\n\n\n\nconst int MOD = 1e9 + 7;\n\nconst ll INF = 1e18;\n\nconst string YES = \"YES\";\n\nconst string NO = \"NO\";\n\n\n\n\n\nvoid solve() {\n\n ll n, m;\n\n cin >> n >> m;\n\n ll low = m, high = m;\n\n vector> A(n);\n\n for (int i = 0; i < n; ++i) {\n\n cin >> A[i][0] >> A[i][1] >> A[i][2];\n\n }\n\n sort(A.begin(), A.end());\n\n ll prev = 0;\n\n for (int i = 0; i < n; ++i) {\n\n ll t =A[i][0], l = A[i][1], r = A[i][2];\n\n ll d = t - prev;\n\n if (low - d > r || high + d < l) {\n\n cout << NO << endl;\n\n return;\n\n }\n\n prev = t;\n\n low = max(l, low - d);\n\n high = min(r, high + d);\n\n }\n\n\n\n cout << YES << endl;\n\n}\n\n\n\nint main() {\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(0);\n\n int T = 1;\n\n cin >> T;\n\n while (T--) {\n\n solve();\n\n }\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1293","problem_id":"A","statement":"A. ConneR and the A.R.C. Markland-Ntime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSakuzyo - ImprintingA.R.C. Markland-N is a tall building with nn floors numbered from 11 to nn. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin \"ConneR\" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor ss of the building. On each floor (including floor ss, of course), there is a restaurant offering meals. However, due to renovations being in progress, kk of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!InputThe first line contains one integer tt (1\u2264t\u226410001\u2264t\u22641000)\u00a0\u2014 the number of test cases in the test. Then the descriptions of tt test cases follow.The first line of a test case contains three integers nn, ss and kk (2\u2264n\u22641092\u2264n\u2264109, 1\u2264s\u2264n1\u2264s\u2264n, 1\u2264k\u2264min(n\u22121,1000)1\u2264k\u2264min(n\u22121,1000))\u00a0\u2014 respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants.The second line of a test case contains kk distinct integers a1,a2,\u2026,aka1,a2,\u2026,ak (1\u2264ai\u2264n1\u2264ai\u2264n)\u00a0\u2014 the floor numbers of the currently closed restaurants.It is guaranteed that the sum of kk over all test cases does not exceed 10001000.OutputFor each test case print a single integer\u00a0\u2014 the minimum number of staircases required for ConneR to walk from the floor ss to a floor with an open restaurant.ExampleInputCopy5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77\nOutputCopy2\n0\n4\n0\n2\nNoteIn the first example test case; the nearest floor with an open restaurant would be the floor 44.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the 66-th floor.","tags":["binary search","brute force","implementation"],"code":"#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\nusing namespace std;\n\n\n\nint main()\n\n{\n\n cout.tie(nullptr);\n\n std::ios::sync_with_stdio(false);\n\n \n\nint t; cin >> t;\n\nwhile(t--){\n\nint n,s,k;\n\ncin >> n >> s >> k;\n\n\n\nvector v;\n\nfor(int i=0;i> a;\n\n v.push_back(a);\n\n}\n\nint ans=0;\n\nfor (int i=0; i<=k; i++) {\n\n\t\tif (s+i<=n) {\n\n if(find(v.begin(),v.end(),s+i)==v.end()){\n\n ans=i; break;\n\n }\n\n }\n\n\n\n if (s-i>=1){\n\n if(find(v.begin(),v.end(),s-i)==v.end()){\n\n ans=i; break;\n\n }\n\n\t\t}\n\n\t}\n\n\n\ncout << ans << '\\n';\n\n\n\n\n\n\n\n\n\n}\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1294","problem_id":"B","statement":"B. Collecting Packagestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a robot in a warehouse and nn packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0)(0,0). The ii-th package is at the point (xi,yi)(xi,yi). It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point (0,0)(0,0) doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point (x,y)(x,y) to the point (x+1,yx+1,y) or to the point (x,y+1)(x,y+1).As we say above, the robot wants to collect all nn packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string ss of length nn is lexicographically less than the string tt of length nn if there is some index 1\u2264j\u2264n1\u2264j\u2264n that for all ii from 11 to j\u22121j\u22121 si=tisi=ti and sj \n\n#include \n\n#include \n\n#include \/\/ \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2557 \u2588\u2588\u2557 \n\n#define endl \"\\n\" \/\/ \u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255d\u255a\u2550\u2550\u2588\u2588\u2554\u2550\u2550\u255d\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255d\u2588\u2588\u2551 \u2588\u2588\u2551 \n\n#define F first \/\/ \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2551 \u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2551 \u2588\u2588\u2551 \n\n#define S second \/\/ \u255a\u2550\u2550\u2550\u2550\u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2554\u2550\u2550\u255d \u255a\u2588\u2588\u2557 \u2588\u2588\u2554\u255d \n\n#define For(i, x , n) for(ll i=x ;x==0?i=0:i>0;i--) \/\/\u255a\u2550\u2550\u2550\u2550\u2550\u2550\u255d \u255a\u2550\u255d \u255a\u2550\u2550\u2550\u2550\u2550\u2550\u255d \u255a\u2550\u2550\u2550\u255d \n\n#define cin(v,x,n) For(i,x,n) cin>>v[i] \n\n#define Insert(v,x,n) For(i,x,n){int in;cin>>in;v.insert(in);} \n\n#define Cin long long n ; cin>>n ; long long arr[n] ; cin(arr,0,n) ; \n\n#define Cin2 LL arr2[n] ; cin(arr2,n) ; \n\n#define r0 return 0 \n\n#define B begin()\n\n#define E end()\n\n#define RB rbegin()\n\n#define RE rend()\n\n#define pow 1LL*pow\n\n#define Back(s) *s.rbegin()\n\n#define Front(s) *s.begin()\n\n#define PF(x) push_front(x)\n\n#define PB(x) push_back(x)\n\n#define POF pop_front()\n\n#define POB pop_back()\n\n#define Digits(n) (n==0?1:(int)log10(n)+1)\n\n#define Bits(n) (ll)log2(n)+1\n\n#define pof(n) ((n&(n-1))==0)\n\n#define smaller(n) order_of_key(n) \n\n#define bigger(cont , n , x) ( (n) - cont.smaller( x + 1) )\n\n\/\/ #define bigger(cont , x) distance(cont.upper_bound(x) , cont.E)\n\n#define At(n) find_by_order(n) \n\n#define Idx(cont , x) (int)( L(ms)-(distance(cont.lower_bound(x), cont.E))) \/\/ linear time for set... \n\n#define Sigma(n) (1LL)*( n%2==0?((ll)n\/2)*((ll)n+1) :(((ll)n+1)\/2)*(ll)n ) \/\/ use bracket Sigma(n -+\\*)\n\n#define Modx(a,b,c) ((a%c)*(b%c))%c \n\n#define Modp(a,b,c) ((a%c)+(b%c))%c \n\n#define Edis(X1 , X2 , Y1 , Y2) (double) sqrt( pow( abs( X1 - X2 ) , 2) + pow( abs ( Y1 - Y2 ) , 2 )) \n\n#define intersect(l , r , l2 , r2) (l<=r2 and r >=l2)\n\n#define lcm(a , b) a*b \/ __gcd(a , b)\n\n#define uniq(x) sort(All(x)); x.erase(unique(All(x)),x.E)\n\n#define L(s) (int) s.size() \/\/ \u0644\u0627 \u062a\u062d\u0627\u0648\u0644 \u0627\u0646 \u062a\u062a\u062e\u0637\u064a \u0642\u062f\u0631\u0627\u062a\u0643 \/\/\n\n#define IDX(fnd,s) fnd-s.B \/\/ \u0628\u0644 \u0627\u0635\u0646\u0639 \u0627\u0644\u064a\u0648\u0645 \u0628\u0645\u0627 \u062a\u0633\u062a\u0637\u0639 \/\/\n\n#define MAX INT_MAX \/\/ \u0633\u062a\u062c\u062f \u0641\u064a \u0627\u0644\u063a\u062f \/\/\n\n#define MIN INT_MIN \/\/ ++\u0642\u062f\u0631\u0627\u062a\u0643 \/\/\n\n#define uMAX 0xffffffff\n\n#define LA(a) (int)(sizeof(a)\/sizeof(a[0])) \n\n#define full(x) !(x.empty()) \n\n#define All(v) v.B,v.E \n\n#define Allr(v) v.rbegin(),v.rend() \n\n#define getName(x) #x\n\n#define debug(x) cout<, rb_tree_tag,tree_order_statistics_node_update>\n\n#define oMap tree, rb_tree_tag,tree_order_statistics_node_update>\n\n#define Acc accumulate\n\n\/\/ lower_bound and upper_bound work oppositely\n\n#define oMultiset tree, rb_tree_tag, tree_order_statistics_node_update> \n\n#define tests int tt ; cin >> tt ; while(tt--)\n\n void judge (){\n\n #ifndef ONLINE_JUDGE \n\n freopen(\"input.txt\", \"r\", stdin);\n\n freopen(\"output.txt\", \"w\", stdout); \n\n #endif\n\n }\n\nint rnd(int a, int b){ return a + rand() % (b - a + 1);}\n\nint generator(){int w = rnd(0, 10);return w; }\n\nusing namespace std ;\n\nusing namespace __gnu_pbds;\n\nusing namespace __gnu_cxx;\n\ntypedef long long ll ;\n\ntypedef unsigned long long ull ;\n\ntypedef pair Pair ;\n\nconst int N = 200005 ;\n\nconst ll MOD = 1e9+7 ;\n\nconst double PI =3.141592653589793238462643383279502884;\n\nconst int INF = MAX ; \n\nconst ll INFl = LLONG_MAX ; \n\nint di[] = { 1, -1, 0, 0, 1, -1, 1, -1 };\n\nint dj[] = { 0, 0, 1, -1, 1, -1,-1, 1 };\n\nchar dc [] = {'D','U','R','L'} ; \n\nint knightX[] = { 2, 1, -1, -2, -2, -1, 1, 2 };\n\nint knightY[] = { 1, 2, 2, 1, -1, -2, -2, -1 };\n\nll n , m , components , k ; \n\n\/\/ bool f ; \n\nbool Salka(int x, int y) { return x > 0 && y > 0 && x <= n && y <= m; } \/\/ 1 based\n\n\/*equal_less \n\n auto it = ms.upper_bound(k-i+1) ; \n\nif((it!=ms.E and it!=ms.B)or (it==ms.E and full(ms)))\n\nit-- ;\n\n*\/\n\ntemplate void dabt_el_masna3(T1 &v , T2 &v2){ v =T1(n+1) ; v2 =T2(n+1) ; }\n\nstruct point{public : double x , y ;} ; \n\nstruct triple{public : ll a , b , c ;} ; \n\ntemplate void debugcont(T&v){for(auto i : v)debug(i);}\n\ntemplate void debugcont2(T&v){for(auto [l ,r] : v)debug(l) ,debug(r) ; }\n\ntemplate auto equal_less(T&v , int value){ \/\/return iterator needed or end when fail\n\nauto it = upper_bound(All(v) , value) ; \n\nif((it!=v.E and it!=v.B) or (it==v.E and full(v)))\n\nit-- ;\n\nelse it=v.E;\n\nreturn it; }\n\n \/* \u201cYou just keep pushing. You just keep pushing. \n\n I made every mistake that could be made. \n\n But I just kept pushing.\u201d \n\n \u2014 Ren\u00e9 Descartes *\/\n\n\n\n \/* Steven's Golden Rules \u2764\n\n \u274f if you feel so dumb you didnt work enough\n\n \u274f give it more time more effort you will be better \n\n \u274f if it gives WA that means you will learn something \n\n \u274f every problem either teach you something or make you better in something other than that you didnt realy solve it . *\/\n\n \/\/*\n\n \n\n main(){\n\n judge() ; \n\n Mo_Salah ;\n\n tests{\n\n int n ; cin>> n ;\n\n vector> v(1001);\n\n string s =\"\" ; \n\n For(i,0,n){\n\n int x , y ; cin >> x >> y ; \n\n v[x].push_back(y);\n\n }\n\n int x = 0 , y = 0 , ans = 0 ;\n\n while(x<=1000){\n\n if( full(v[x]) ){\n\n sort(v[x].B , v[x].E) ; \n\n for(auto i : v[x]) {\n\n if(i>=y) s.append(i-y,'U') , y= i , ans++;\n\n else break ; \n\n }\n\n }\n\n if( ans == n )break;\n\n x++ ;\n\n s.push_back('R') ; \n\n }\n\n if( ans == n ) cout<<\"YES\"<\n#define P 998244353ll\n#define ll long long\nusing namespace std;\nnamespace QYB {\n void exgcd(ll a, ll b, ll &x, ll &y) {\n if (!b) return x = 1, y = 0, void();\n exgcd(b, a % b, y, x); y -= a \/ b * x;\n } ll inv(ll a) {\n ll x, y; exgcd(a, P, x, y); return (x % P + P) % P;\n } struct matrix {\n ll a[2][2];\n ll *operator[](int x) { return a[x]; }\n matrix(ll _0 = 0, ll _1 = 0, ll _2 = 0, ll _3 = 0) { a[0][0] = _0; a[0][1] = _1; a[1][0] = _2; a[1][1] = _3; }\n matrix operator*(matrix x) {\n return matrix((a[0][0] * x[0][0] + a[0][1] * x[1][0]) % P, (a[0][0] * x[0][1] + a[0][1] * x[1][1]) % P, (a[1][0] * x[0][0] + a[1][1] * x[1][0]) % P, (a[1][0] * x[0][1] + a[1][1] * x[1][1]) % P);\n }\n } s[500005];\n int n; ll ans, d[100005], v[100005], g[2][100005];\n void modify(int p, int l, int r, int x, int a, int b) {\n if (x < l || x > r) return;\n if (l == r) return s[p][a][b] = g[a][x], void();\n int mid = l + r >> 1;\n modify(p << 1, l, mid, x, a, b);\n modify(p << 1 | 1, mid + 1, r, x, a, b);\n s[p] = s[p << 1 | 1] * s[p << 1];\n } matrix query(int p, int l, int r, int L, int R) {\n if (r < L || R < l) return matrix(1, 0, 0, 1);\n if (L <= l && r <= R) return s[p];\n int mid = l + r >> 1;\n return query(p << 1 | 1, mid + 1, r, L, R) * query(p << 1, l, mid, L, R);\n } int main() {\n scanf(\"%d\", &n);\n vector > f;\n for (int i = 1; i <= n; i++) {\n scanf(\"%lld%lld%lld\", d + i, v + i, &g[1][i]);\n g[0][i] = (P + 1 - ((g[1][i] *= 828542813ll) %= P)) % P;\n i == 1 || v[i - 1] - v[i] >= 0? modify(1, 1, n, i, 0, 0): f.push_back({i, 0});\n i == 1 || v[i - 1] + v[i] <= 0? modify(1, 1, n, i, 0, 1): f.push_back({i, 1});\n i == 1 || v[i - 1] + v[i] >= 0? modify(1, 1, n, i, 1, 0): f.push_back({i, 2});\n i == 1 || v[i - 1] - v[i] <= 0? modify(1, 1, n, i, 1, 1): f.push_back({i, 3});\n } sort(f.begin(), f.end(), [](pair x, pair y) {\n auto [ix, sx] = x; auto [iy, sy] = y;\n ll vx = (sx & 1? 1: -1) * v[ix - 1] + (sx & 2? -1: 1) * v[ix];\n ll vy = (sy & 1? 1: -1) * v[iy - 1] + (sy & 2? -1: 1) * v[iy];\n return (d[ix] - d[ix - 1]) * vy > (d[iy] - d[iy - 1]) * vx;\n });\n for (auto [i, st]: f) {\n modify(1, 1, n, i, (st >> 1) & 1, (st >> 0) & 1);\n matrix res = query(1, 1, n, i + 1, n) * (st == 0? matrix(g[0][i], 0, 0, 0): (st == 1? matrix(0, g[0][i], 0, 0): (st == 2? matrix(0, 0, g[1][i], 0): matrix(0, 0, 0, g[1][i])))) * query(1, 1, n, 1, i - 1);\n (ans += (d[i] - d[i - 1]) * inv(((st & 1? 1: P - 1) * v[i - 1] % P + (st & 2? P - 1: 1) * v[i] % P) % P) % P * (res[0][0] + res[1][0]) % P) %= P;\n } return !printf(\"%lld\\n\", ans);\n }\n} int main() {\n return QYB::main();\n}","language":"cpp"} -{"contest_id":"1290","problem_id":"D","statement":"D. Coffee Varieties (hard version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. You can find the easy version in the Div. 2 contest. Both versions only differ in the number of times you can ask your friend to taste coffee.This is an interactive problem.You're considering moving to another city; where one of your friends already lives. There are nn caf\u00e9s in this city, where nn is a power of two. The ii-th caf\u00e9 produces a single variety of coffee aiai. As you're a coffee-lover, before deciding to move or not, you want to know the number dd of distinct varieties of coffees produced in this city.You don't know the values a1,\u2026,ana1,\u2026,an. Fortunately, your friend has a memory of size kk, where kk is a power of two.Once per day, you can ask him to taste a cup of coffee produced by the caf\u00e9 cc, and he will tell you if he tasted a similar coffee during the last kk days.You can also ask him to take a medication that will reset his memory. He will forget all previous cups of coffee tasted. You can reset his memory at most 30\u00a000030\u00a0000 times.More formally, the memory of your friend is a queue SS. Doing a query on caf\u00e9 cc will: Tell you if acac is in SS; Add acac at the back of SS; If |S|>k|S|>k, pop the front element of SS. Doing a reset request will pop all elements out of SS.Your friend can taste at most 3n22k3n22k cups of coffee in total. Find the diversity dd (number of distinct values in the array aa).Note that asking your friend to reset his memory does not count towards the number of times you ask your friend to taste a cup of coffee.In some test cases the behavior of the interactor is adaptive. It means that the array aa may be not fixed before the start of the interaction and may depend on your queries. It is guaranteed that at any moment of the interaction, there is at least one array aa consistent with all the answers given so far.InputThe first line contains two integers nn and kk (1\u2264k\u2264n\u226410241\u2264k\u2264n\u22641024, kk and nn are powers of two).It is guaranteed that 3n22k\u226415\u00a00003n22k\u226415\u00a0000.InteractionYou begin the interaction by reading nn and kk. To ask your friend to taste a cup of coffee produced by the caf\u00e9 cc, in a separate line output? ccWhere cc must satisfy 1\u2264c\u2264n1\u2264c\u2264n. Don't forget to flush, to get the answer.In response, you will receive a single letter Y (yes) or N (no), telling you if variety acac is one of the last kk varieties of coffee in his memory. To reset the memory of your friend, in a separate line output the single letter R in upper case. You can do this operation at most 30\u00a000030\u00a0000 times. When you determine the number dd of different coffee varieties, output! ddIn case your query is invalid, you asked more than 3n22k3n22k queries of type ? or you asked more than 30\u00a000030\u00a0000 queries of type R, the program will print the letter E and will finish interaction. You will receive a Wrong Answer verdict. Make sure to exit immediately to avoid getting other verdicts.After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hack formatThe first line should contain the word fixedThe second line should contain two integers nn and kk, separated by space (1\u2264k\u2264n\u226410241\u2264k\u2264n\u22641024, kk and nn are powers of two).It must hold that 3n22k\u226415\u00a00003n22k\u226415\u00a0000.The third line should contain nn integers a1,a2,\u2026,ana1,a2,\u2026,an, separated by spaces (1\u2264ai\u2264n1\u2264ai\u2264n).ExamplesInputCopy4 2\nN\nN\nY\nN\nN\nN\nN\nOutputCopy? 1\n? 2\n? 3\n? 4\nR\n? 4\n? 1\n? 2\n! 3\nInputCopy8 8\nN\nN\nN\nN\nY\nY\nOutputCopy? 2\n? 6\n? 4\n? 5\n? 2\n? 5\n! 6\nNoteIn the first example; the array is a=[1,4,1,3]a=[1,4,1,3]. The city produces 33 different varieties of coffee (11, 33 and 44).The successive varieties of coffee tasted by your friend are 1,4,1,3,3,1,41,4,1,3,3,1,4 (bold answers correspond to Y answers). Note that between the two ? 4 asks, there is a reset memory request R, so the answer to the second ? 4 ask is N. Had there been no reset memory request, the answer to the second ? 4 ask is Y.In the second example, the array is a=[1,2,3,4,5,6,6,6]a=[1,2,3,4,5,6,6,6]. The city produces 66 different varieties of coffee.The successive varieties of coffee tasted by your friend are 2,6,4,5,2,52,6,4,5,2,5.","tags":["constructive algorithms","graphs","interactive"],"code":"#include\n\nusing namespace std;\n\n#define ll long long\n\n#define N 100001\n\nll n,k;\n\ninline bool gt(ll x){\n\n\tcout<<\"? \"<>tem;\n\n\tif(tem=='N')return 0;\n\n\treturn 1;\n\n}\n\ninline void clear(){\n\n\tcout<<'R'<<'\\n';\n\n\treturn ;\n\n}\n\nll an[N];\n\nint main()\n\n{\n\n\/\/\tfreopen(\"test1.in\",\"r\",stdin);\n\n\t\/\/freopen(\".in\",\"r\",stdin);\n\n\t\/\/freopen(\"test1.out\",\"w\",stdout);\n\n\tios::sync_with_stdio(false);cin.tie(0);cout.tie(0);\n\n\tcin>>n>>k;\n\n\tif(k==1){\n\n\t\tll ans=n;\n\n\t\tfor(int i=2;i<=n;i++){\n\n\t\t\tfor(int j=1;jg-i)continue;\n\n\t\t\tfor(int k=j;k<=g;k+=i){\n\n\t\t\t\tfor(int p=(k-1)*ji+1;p<=k*ji;p++)an[p]|=gt(p);\n\n\t\t\t}clear();\n\n\t\t}\n\n\t} \n\n\tfor(int i=1;i<=n;i++)if(an[i])ans--;\n\n\tcout<<\"! \"<bi+bjai+aj>bi+bj (i.e. it is more interesting for the teacher).Your task is to find the number of good pairs of topics.InputThe first line of the input contains one integer nn (2\u2264n\u22642\u22c51052\u2264n\u22642\u22c5105) \u2014 the number of topics.The second line of the input contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641091\u2264ai\u2264109), where aiai is the interestingness of the ii-th topic for the teacher.The third line of the input contains nn integers b1,b2,\u2026,bnb1,b2,\u2026,bn (1\u2264bi\u22641091\u2264bi\u2264109), where bibi is the interestingness of the ii-th topic for the students.OutputPrint one integer \u2014 the number of good pairs of topic.ExamplesInputCopy5\n4 8 2 6 2\n4 5 4 1 3\nOutputCopy7\nInputCopy4\n1 3 2 4\n1 3 2 4\nOutputCopy0\n","tags":["binary search","data structures","sortings","two pointers"],"code":"#include \n\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing uint = unsigned int;\nusing vi = vector;\nusing pi = pair;\n\n#define FAST_IO \\\n ios_base::sync_with_stdio(false); \\\n cin.tie(NULL)\n\nbool is_prime(ll x) {\n if (x <= 1) return 0;\n for (ll y = 2; y * y <= x; ++y)\n if (x % y == 0) return 0;\n return 1;\n}\n\nusing std::gcd; \/\/ Computes the greatest common divisor of the integers m and\n \/\/ n.\nusing std::lcm; \/\/ Computes the least common multiple of the integers m and n.\n\nint popcnt(uint x) { return __builtin_popcount(x); }\nint popcnt(int x) { return __builtin_popcount(x); }\nint popcnt(ull x) { return __builtin_popcountll(x); }\nint popcnt(ll x) { return __builtin_popcountll(x); }\nint bsr(uint x) { return 31 - __builtin_clz(x); }\nint bsr(ull x) { return 63 - __builtin_clzll(x); }\nint ctz(int x) { return __builtin_ctz(x); }\nint ctz(ll x) { return __builtin_ctzll(x); }\nint ctz(ull x) { return __builtin_ctzll(x); }\n\ntemplate \nusing vc = vector;\ntemplate \nusing vvc = vector>;\ntemplate \nusing vvvc = vector>;\ntemplate \nusing vvvvc = vector>;\ntemplate \nusing vvvvvc = vector>;\ntemplate \nusing pq = priority_queue;\ntemplate \nusing pqg = priority_queue, greater>;\n\n#define all(x) x.begin(), x.end()\n#define len(x) ll(x.size())\n\n#define SUM(v) accumulate(all(v), 0LL)\n#define MIN(v) *min_element(all(v))\n#define MAX(v) *max_element(all(v))\n\n#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())\n\nll ceil(ll x, ll y) {\n assert(y >= 1);\n return (x > 0 ? (x + y - 1) \/ y : x \/ y);\n}\n\nll floor(ll x, ll y) {\n assert(y >= 1);\n return (x > 0 ? x \/ y : (x - y + 1) \/ y);\n}\n\nll mod(ll x, ll y) { return x - y * floor(x, y); }\n\n#line 74 \"library\/ds\/unionfind.hpp\"\nstruct UnionFind {\n int n;\n int n_comp;\n std::vector size, par;\n UnionFind(int n) : n(n), n_comp(n), size(n, 1), par(n) {\n std::iota(par.begin(), par.end(), 0);\n }\n int find(int x) {\n assert(0 <= x && x < n);\n while (par[x] != x) {\n x = par[x] = par[par[x]];\n }\n return x;\n }\n\n int operator[](int x) { return find(x); }\n\n bool merge(int x, int y) {\n x = find(x);\n y = find(y);\n if (x == y) {\n return false;\n }\n n_comp--;\n if (size[x] < size[y]) std::swap(x, y);\n size[x] += size[y];\n size[y] = 0;\n par[y] = x;\n return true;\n }\n\n std::vector find_all() {\n std::vector A(n);\n for (int i = 0; i < n; ++i) A[i] = find(i);\n return A;\n }\n\n void reset() {\n n_comp = n;\n size.assign(n, 1);\n std::iota(par.begin(), par.end(), 0);\n }\n};\n\n\/**************************** debug utils begin *******************************\/\n\n#line 121 \"debug_utils.cpp\"\n\n#ifdef OJ_DEBUG\n#define LOG std::cerr << \"line: \" << __LINE__ << \", \"\n#else\nstruct NonFunctioningLogger {\n public:\n template \n NonFunctioningLogger& operator<<(const T& obj) {\n return *this;\n }\n};\nNonFunctioningLogger LOG;\n#endif \/\/ OJ_DEBUG\n\n#define DEFINE_CONTAINER_PRINTER(CONTAINER_TYPE) \\\n template \\\n ostream& operator<<(ostream& ostr, const CONTAINER_TYPE& a) { \\\n ostr << \"{\"; \\\n for (auto it = a.begin(); it != a.end(); it++) { \\\n if (it != a.begin()) ostr << \", \"; \\\n ostr << *it; \\\n } \\\n ostr << \"}\"; \\\n return ostr; \\\n }\n\nDEFINE_CONTAINER_PRINTER(std::vector)\nDEFINE_CONTAINER_PRINTER(std::deque)\nDEFINE_CONTAINER_PRINTER(std::list)\nDEFINE_CONTAINER_PRINTER(std::set)\nDEFINE_CONTAINER_PRINTER(std::unordered_set)\n\ntemplate \nostream& operator<<(ostream& os, const pair& A) {\n os << \"(\" << A.first << \", \" << A.second << \")\";\n return os;\n}\n\ntemplate \nauto operator<<(std::ostream& os, const std::vector>& a)\n -> std::ostream& {\n os << \"{\";\n int i;\n for (i = 0; i < a.size() - 1; i++) {\n os << \"(\" << a[i].first << \", \" << a[i].second << \"), \";\n }\n if (i < a.size()) {\n os << a[i];\n }\n os << \"}\";\n return os;\n}\n\ntemplate \ninline bool chmin(T& a, const S& b) {\n return (a > b ? a = b, 1 : 0);\n}\n\n\/**************************** debug utils end *******************************\/\n\n#line 182 \"main.cpp\"\n\nint main() {\n FAST_IO;\n int n;\n cin >> n;\n vc a(n);\n vc b(n);\n for (int i = 0; i < n; i++) cin >> a[i];\n for (int i = 0; i < n; i++) cin >> b[i];\n for (int i = 0; i < n; i++) {\n a[i] -= b[i];\n }\n sort(all(a));\n ll cnt{0};\n for (int i = 0; i < n - 1; i++) {\n if (a[i] <= 0) {\n cnt += distance(lower_bound(a.begin() + i + 1, a.end(), 1LL - a[i]),\n a.end());\n } else {\n cnt += (n - 1 - i);\n }\n }\n cout << cnt << \"\\n\";\n return 0;\n}\n","language":"cpp"} -{"contest_id":"1294","problem_id":"C","statement":"C. Product of Three Numberstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given one integer number nn. Find three distinct integers a,b,ca,b,c such that 2\u2264a,b,c2\u2264a,b,c and a\u22c5b\u22c5c=na\u22c5b\u22c5c=n or say that it is impossible to do it.If there are several answers, you can print any.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1\u2264t\u22641001\u2264t\u2264100) \u2014 the number of test cases.The next nn lines describe test cases. The ii-th test case is given on a new line as one integer nn (2\u2264n\u22641092\u2264n\u2264109).OutputFor each test case, print the answer on it. Print \"NO\" if it is impossible to represent nn as a\u22c5b\u22c5ca\u22c5b\u22c5c for some distinct integers a,b,ca,b,c such that 2\u2264a,b,c2\u2264a,b,c.Otherwise, print \"YES\" and any possible such representation.ExampleInputCopy5\n64\n32\n97\n2\n12345\nOutputCopyYES\n2 4 8 \nNO\nNO\nNO\nYES\n3 5 823 \n","tags":["greedy","math","number theory"],"code":"#include \n\n#include \n\nusing namespace std;\n\n \n\nint main(){\n\n int t,n,n1,m,a,b,x,y,c;\n\n cin >> t;\n\n while(t--){\n\n cin>> n;\n\n n1 = n;\n\n b=0,a=0,x=0,y=0,m=3;\n\n if(n%2 == 0){\n\n a = 2;\n\n while(n%2 == 0){\n\n n \/= 2;\n\n x++;\n\n }\n\n }\n\n else{\n\n while(!a){\n\n while(n%m == 0){\n\n n \/= m;\n\n x++;\n\n }\n\n if(x != 0){a = m;}\n\n else if(m*m < n1){m += 2;}\n\n else{break;}\n\n }\n\n }\n\n if((n == 1 && x < 6) || m*m > n1){cout<<\"NO\"<<'\\n';}\n\n else if(n == 1){\n\n cout<<\"YES\"<<'\\n';\n\n b = a*a;\n\n c = pow(a,x-3);\n\n cout<< a <<' '<< b <<' '<< c <<'\\n';}\n\n else if(x > 2){\n\n cout<<\"YES\"<<'\\n';\n\n b = pow(a,x-1);\n\n c = n1\/pow(a,x);\n\n cout<< a <<' '<< b <<' '<< c <<'\\n';}\n\n else{\n\n while(!b){\n\n while(n%m == 0){\n\n n \/= m;\n\n y++;\n\n }\n\n if(y != 0){b = m;}\n\n else if(m*m < n1){m += 2;}\n\n else{break;}\n\n }\n\n if((n == 1 && x+y < 4) || m*m > n1){cout<<\"NO\"<<'\\n';}\n\n else if(n == 1 && x==1){\n\n cout<<\"YES\"<<'\\n';\n\n c = pow(b,y-1);\n\n cout<< a <<' '<< b <<' '<< c <<'\\n';}\n\n else if(n == 1 && y==1){\n\n cout<<\"YES\"<<'\\n';\n\n c = pow(a,x-1);\n\n cout<< a <<' '<< b <<' '<< c <<'\\n';}\n\n else if(n == 1){\n\n cout<<\"YES\"<<'\\n';\n\n c = a*b;\n\n a = pow(a,x-1);\n\n b = pow(b,y-1);\n\n cout<< a <<' '<< b <<' '<< c <<'\\n';}\n\n else{\n\n cout<<\"YES\"<<'\\n';\n\n a = pow(a,x);\n\n b = pow(b,y);\n\n cout<< a <<' '<< b <<' '<< n <<'\\n';\n\n }\n\n } \n\n }\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1305","problem_id":"E","statement":"E. Kuroni and the Score Distributiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni is the coordinator of the next Mathforces round written by the \"Proof by AC\" team. All the preparation has been done; and he is discussing with the team about the score distribution for the round.The round consists of nn problems, numbered from 11 to nn. The problems are ordered in increasing order of difficulty, no two problems have the same difficulty. A score distribution for the round can be denoted by an array a1,a2,\u2026,ana1,a2,\u2026,an, where aiai is the score of ii-th problem. Kuroni thinks that the score distribution should satisfy the following requirements: The score of each problem should be a positive integer not exceeding 109109. A harder problem should grant a strictly higher score than an easier problem. In other words, 1\u2264a1\n\nusing namespace std;\n\nusing ll = long long;\n\nusing ld = long double;\n\n \n\nconstexpr int N = int(4e2) + 5;\n\nconstexpr int inf = 0x7f7f7f7f;\n\nconstexpr int MOD = int(1e9) + 7;\n\n\n\nvoid solve(){\n\n int n, m, k = 0, l;\n\n cin >> n >> m;\n\n\n\n vector v;\n\n for(int i = 1; i <= n; i++){\n\n if(k + (i - 1 >> 1) <= m){\n\n k += i - 1 >> 1;\n\n v.push_back(i);\n\n l = v.back() + 1;\n\n }\n\n else if(k < m){\n\n v.push_back(2 * (i - m + k) - 1);\n\n k = m;\n\n l = v.back() + 1;\n\n }\n\n else v.push_back(int(1e9) - l * (n - i));\n\n }\n\n if(k != m) cout << -1;\n\n else for(auto& i : v) cout << i << ' ';\n\n}\n\nint main() {\n\n ios::sync_with_stdio(0); \n\n cin.tie(0);\n\n\n\n int t = 1;\n\n \/\/cin >> t;\n\n while(t--){\n\n solve();\n\n }\n\n}","language":"cpp"} -{"contest_id":"1295","problem_id":"A","statement":"A. Display The Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of 77 segments which can be turned on and off to compose different digits. The following picture describes how you can display all 1010 decimal digits:As you can see, different digits may require different number of segments to be turned on. For example, if you want to display 11, you have to turn on 22 segments of the screen, and if you want to display 88, all 77 segments of some place to display a digit should be turned on.You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than nn segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than nn segments.Your program should be able to process tt different test cases.InputThe first line contains one integer tt (1\u2264t\u22641001\u2264t\u2264100) \u2014 the number of test cases in the input.Then the test cases follow, each of them is represented by a separate line containing one integer nn (2\u2264n\u22641052\u2264n\u2264105) \u2014 the maximum number of segments that can be turned on in the corresponding testcase.It is guaranteed that the sum of nn over all test cases in the input does not exceed 105105.OutputFor each test case, print the greatest integer that can be displayed by turning on no more than nn segments of the screen. Note that the answer may not fit in the standard 3232-bit or 6464-bit integral data type.ExampleInputCopy2\n3\n4\nOutputCopy7\n11\n","tags":["greedy"],"code":"#include \n\nusing namespace std;\n\n\n\ntypedef long long ll;\n\nll lcm(ll a,ll b) { return a\/gcd(a,b)*b; }\n\n#define endl '\\n';\n\n\n\nvoid solve()\n\n{\n\n int n; cin >> n;\n\n if (n % 2 != 0)\n\n {\n\n cout << \"7\";\n\n n -= 3;\n\n }\n\n \n\n while (n)\n\n {\n\n cout << 1;\n\n n -= 2;\n\n }\n\n cout << \"\\n\";\n\n}\n\n\t\t\n\nint main()\n\n{\n\n\tios_base::sync_with_stdio(0);\n\n cin.tie(0); cout.tie(0);\n\n\n\n\tint t;\n\n\tcin >> t;\n\n\twhile(t--)\n\n\t{\n\n\t\tsolve();\n\n\t}\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1304","problem_id":"B","statement":"B. Longest Palindrometime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputReturning back to problem solving; Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings \"pop\", \"noon\", \"x\", and \"kkkkkk\" are palindromes, while strings \"moon\", \"tv\", and \"abab\" are not. An empty string is also a palindrome.Gildong loves this concept so much, so he wants to play with it. He has nn distinct strings of equal length mm. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.InputThe first line contains two integers nn and mm (1\u2264n\u22641001\u2264n\u2264100, 1\u2264m\u2264501\u2264m\u226450) \u2014 the number of strings and the length of each string.Next nn lines contain a string of length mm each, consisting of lowercase Latin letters only. All strings are distinct.OutputIn the first line, print the length of the longest palindrome string you made.In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all.ExamplesInputCopy3 3\ntab\none\nbat\nOutputCopy6\ntabbat\nInputCopy4 2\noo\nox\nxo\nxx\nOutputCopy6\noxxxxo\nInputCopy3 5\nhello\ncodef\norces\nOutputCopy0\n\nInputCopy9 4\nabab\nbaba\nabcd\nbcde\ncdef\ndefg\nwxyz\nzyxw\nijji\nOutputCopy20\nababwxyzijjizyxwbaba\nNoteIn the first example; \"battab\" is also a valid answer.In the second example; there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.In the third example; the empty string is the only valid palindrome string.","tags":["brute force","constructive algorithms","greedy","implementation","strings"],"code":"\/* ______ Defines _____ *\/\n\n#pragma GCC optimize(\"O3\")\n\n#pragma GCC optimize(\"unroll-loops\")\n\n#include \n\n#include \n\nusing namespace __gnu_pbds;\n\n#define ordered_set tree, null_type,less>, rb_tree_tag,tree_order_statistics_node_update>\n\n#define Time cerr << \"Time Taken: \" << (float)clock() \/ CLOCKS_PER_SEC << \" Secs\" << \"\\n\";\n\n#include \n\n#define Num_of_Digits(n) ((int)log10(n)+1)\n\nusing namespace std;\n\n#define Lion_Heart ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\ndouble pi=3.141592653590;\n\ntypedef long long ll;\n\ntypedef long double ld;\n\ntypedef unsigned long long ull;\n\ntypedef long double ld;\n\n#define yes cout<<\"YES\\n\";return;\n\n#define no cout<<\"NO\\n\";return;\n\n#define F first\n\n#define S second\n\n#define el '\\n'\n\n#define INF 1e18\n\n#define PI 3.1415926535\n\n#define L00 0x3f3f3f3f3f3f3f3f\n\n#define I00 0x3f3f3f3f\n\n#define NL00 -0x3f3f3f3f3f3f3f3f\n\n#define popcount __builtin_popcount\n\nll MOD = 1e18+5;\n\nint TEST_CASE = 1;\n\nvoid file(){\n\n#ifndef ONLINE_JUDGE\n\n freopen(\"input.txt\", \"r\", stdin);\n\n freopen(\"output.txt\", \"w\", stdout);\n\n#endif\n\n}\n\nint\n\ndi[] = {1, 0, -1, 0, 1, -1, -1, 1},\n\ndj[] = {0, 1, 0, -1, 1, 1, -1, -1},\n\ndx[] = {0, 1, 0 , -1},\n\ndy[] = {1, 0, -1, 0};\n\n\/\/right, down, left, up\n\nint const N = 1e5 + 5, M = 2*N + 9 ,SQRT=1e6+10 ,mod = 1e9 + 7, oo = 1e9 + 10;\n\n\/\/------------start solve-------------------\n\nvoid solve() {\n\n int n, m; cin>>n>>m;\n\n map mp;\n\n string s, res = \"\";\n\n for (int i = 0; i < n; ++i) {\n\n cin>>s;\n\n \/\/check if there plaindrome\n\n string rev = s;\n\n reverse(rev.begin(), rev.end());\n\n if(mp.find(rev) != mp.end()) {\n\n res += s;\n\n mp.erase(rev);\n\n }else mp[s]++;\n\n }\n\n string mid = \"\";\n\n for(auto mm:mp) {\n\n string tmp = mm.F;\n\n string rev = tmp;\n\n std::reverse(rev.begin(), rev.end());\n\n if(tmp == rev) mid = tmp;\n\n }\n\n cout<>tt;\n\n for (int i = 0; i < tt; ++i) {\n\n solve();\n\n\/\/ TEST_CASE++;\n\n }\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1292","problem_id":"A","statement":"A. NEKO's Maze Gametime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output3R2 as DJ Mashiro - Happiness Breeze Ice - DJ Mashiro is dead or aliveNEKO#\u03a6\u03c9\u03a6 has just got a new maze game on her PC!The game's main puzzle is a maze; in the forms of a 2\u00d7n2\u00d7n rectangle grid. NEKO's task is to lead a Nekomimi girl from cell (1,1)(1,1) to the gate at (2,n)(2,n) and escape the maze. The girl can only move between cells sharing a common side.However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.After hours of streaming, NEKO finally figured out there are only qq such moments: the ii-th moment toggles the state of cell (ri,ci)(ri,ci) (either from ground to lava or vice versa).Knowing this, NEKO wonders, after each of the qq moments, whether it is still possible to move from cell (1,1)(1,1) to cell (2,n)(2,n) without going through any lava cells.Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?InputThe first line contains integers nn, qq (2\u2264n\u22641052\u2264n\u2264105, 1\u2264q\u22641051\u2264q\u2264105).The ii-th of qq following lines contains two integers riri, cici (1\u2264ri\u226421\u2264ri\u22642, 1\u2264ci\u2264n1\u2264ci\u2264n), denoting the coordinates of the cell to be flipped at the ii-th moment.It is guaranteed that cells (1,1)(1,1) and (2,n)(2,n) never appear in the query list.OutputFor each moment, if it is possible to travel from cell (1,1)(1,1) to cell (2,n)(2,n), print \"Yes\", otherwise print \"No\". There should be exactly qq answers, one after every update.You can print the words in any case (either lowercase, uppercase or mixed).ExampleInputCopy5 5\n2 3\n1 4\n2 4\n2 3\n1 4\nOutputCopyYes\nNo\nNo\nNo\nYes\nNoteWe'll crack down the example test here: After the first query; the girl still able to reach the goal. One of the shortest path ways should be: (1,1)\u2192(1,2)\u2192(1,3)\u2192(1,4)\u2192(1,5)\u2192(2,5)(1,1)\u2192(1,2)\u2192(1,3)\u2192(1,4)\u2192(1,5)\u2192(2,5). After the second query, it's impossible to move to the goal, since the farthest cell she could reach is (1,3)(1,3). After the fourth query, the (2,3)(2,3) is not blocked, but now all the 44-th column is blocked, so she still can't reach the goal. After the fifth query, the column barrier has been lifted, thus she can go to the final goal again. ","tags":["data structures","dsu","implementation"],"code":"#include \n\ntypedef long long ll;\n\n#define vi vector \n\n#define vll vector \n\n#define vs vector \n\n#define vc vector \n\n#define PII pair\n\n#define MII map\n\n#define UMII unordered_map\n\n#define sz(v) (int)v.size()\n\n#define all(v) v.begin(),v.end()\n\n#define ff first \n\n#define ss second \n\n#define int long long\n\n#define endl \"\\n\"\n\n#define TxtIO freopen(\"input.txt\",\"r\",stdin); freopen(\"output.txt\",\"w\",stdout);\n\nusing namespace std;\n\nconst int M = 1e9+7;\n\nconst int N = 1e5+7;\n\nsigned main()\n\n{\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(0);\n\n cout.tie(0);\n\n int n,q;\n\n cin >> n >> q;\n\n vector> v(2,vector(n+1));\n\n int locks{};\n\n while(q--){\n\n int a,b; cin >> a >> b;\n\n a--; b--;\n\n if(v[a][b] == 0){\n\n v[a][b] = 1;\n\n locks += v[a^1][b];\n\n if(b-1 >= 0) locks += v[a^1][b-1];\n\n if(b+1 < n) locks += v[a^1][b+1];\n\n }else{\n\n v[a][b] = 0;\n\n locks -= v[a^1][b];\n\n if(b-1 >= 0) locks -= v[a^1][b-1];\n\n if(b+1 < n) locks -= v[a^1][b+1];\n\n }\n\n if(!locks){\n\n cout << \"YES\" << \"\\n\";\n\n }else{\n\n cout << \"NO\" << \"\\n\";\n\n }\n\n }\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1285","problem_id":"A","statement":"A. Mezo Playing Zomatime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; Mezo is playing a game. Zoma, a character in that game, is initially at position x=0x=0. Mezo starts sending nn commands to Zoma. There are two possible commands: 'L' (Left) sets the position x:=x\u22121x:=x\u22121; 'R' (Right) sets the position x:=x+1x:=x+1. Unfortunately, Mezo's controller malfunctions sometimes. Some commands are sent successfully and some are ignored. If the command is ignored then the position xx doesn't change and Mezo simply proceeds to the next command.For example, if Mezo sends commands \"LRLR\", then here are some possible outcomes (underlined commands are sent successfully): \"LRLR\" \u2014 Zoma moves to the left, to the right, to the left again and to the right for the final time, ending up at position 00; \"LRLR\" \u2014 Zoma recieves no commands, doesn't move at all and ends up at position 00 as well; \"LRLR\" \u2014 Zoma moves to the left, then to the left again and ends up in position \u22122\u22122. Mezo doesn't know which commands will be sent successfully beforehand. Thus, he wants to know how many different positions may Zoma end up at.InputThe first line contains nn (1\u2264n\u2264105)(1\u2264n\u2264105) \u2014 the number of commands Mezo sends.The second line contains a string ss of nn commands, each either 'L' (Left) or 'R' (Right).OutputPrint one integer \u2014 the number of different positions Zoma may end up at.ExampleInputCopy4\nLRLR\nOutputCopy5\nNoteIn the example; Zoma may end up anywhere between \u22122\u22122 and 22.","tags":["math"],"code":"#include\n\nusing namespace std;\n\nint main(){\n\nint n;\n\ncin>>n;\n\nstring s;\n\ncin>>s;\n\ncout<\n\n\nint main() {\nint n, m; scanf(\"%d%d\", &n, &m);\nint a{n * m}, C[a]{};\nfor (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {\nint x; scanf(\"%d\", &x);\nif (--x % m == j and x < a)\nC[j * n + (i - x \/ m + n) % n]--;\nC[j * n + i] += i;\n}\nfor (int k = 0; k < n * m; k += n)\na += *std::min_element(C + k, C + k + n);\nprintf(\"%d\", a);\n}","language":"cpp"} -{"contest_id":"1315","problem_id":"B","statement":"B. Homecomingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter a long party Petya decided to return home; but he turned out to be at the opposite end of the town from his home. There are nn crossroads in the line in the town, and there is either the bus or the tram station at each crossroad.The crossroads are represented as a string ss of length nn, where si=Asi=A, if there is a bus station at ii-th crossroad, and si=Bsi=B, if there is a tram station at ii-th crossroad. Currently Petya is at the first crossroad (which corresponds to s1s1) and his goal is to get to the last crossroad (which corresponds to snsn).If for two crossroads ii and jj for all crossroads i,i+1,\u2026,j\u22121i,i+1,\u2026,j\u22121 there is a bus station, one can pay aa roubles for the bus ticket, and go from ii-th crossroad to the jj-th crossroad by the bus (it is not necessary to have a bus station at the jj-th crossroad). Formally, paying aa roubles Petya can go from ii to jj if st=Ast=A for all i\u2264t\n\nusing namespace std;\n\n#define fast \\\n\n ios_base::sync_with_stdio(false); \\\n\n cin.tie(0); \\\n\n cout.tie(0)\n\n#define ll long long\n\n#define all(v) v.begin(), v.end()\n\n#define pb push_back\n\n#define vl vector\n\n#define vp vector>\n\n#define in(a) \\\n\n for (auto &x : a) \\\n\n cin >> x;\n\n#define out(a) \\\n\n for (auto x : a) \\\n\n cout << x << ' '; \\\n\n cout << endl;\n\n#define pr(a) cout << a << endl\n\nconst ll mod = 1e9 + 7;\n\nconst double pi = acos(-1);\n\nvoid solve()\n\n{\n\n ll i, a, b, p, ans;\n\n string s;\n\n cin >> a >> b >> p >> s;\n\n char c = 'X';\n\n for (i = s.size() - 2; i >= 0; i--)\n\n {\n\n if (c != s[i])\n\n {\n\n c = s[i];\n\n p -= (c == 'A' ? a : b);\n\n if (p < 0)\n\n {\n\n break;\n\n }\n\n }\n\n }\n\n pr(i + 2);\n\n}\n\nint main()\n\n{\n\n fast;\n\n int t;\n\n cin >> t;\n\n while (t--)\n\n solve();\n\n}","language":"cpp"} -{"contest_id":"1141","problem_id":"G","statement":"G. Privatization of Roads in Treelandtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTreeland consists of nn cities and n\u22121n\u22121 roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right \u2014 the country's topology is an undirected tree.There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads.The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed kk and the number of companies taking part in the privatization is minimal.Choose the number of companies rr such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most kk. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal rr that there is such assignment to companies from 11 to rr that the number of cities which are not good doesn't exceed kk. The picture illustrates the first example (n=6,k=2n=6,k=2). The answer contains r=2r=2 companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number 33) is not good. The number of such vertices (just one) doesn't exceed k=2k=2. It is impossible to have at most k=2k=2 not good cities in case of one company. InputThe first line contains two integers nn and kk (2\u2264n\u2264200000,0\u2264k\u2264n\u221212\u2264n\u2264200000,0\u2264k\u2264n\u22121) \u2014 the number of cities and the maximal number of cities which can have two or more roads belonging to one company.The following n\u22121n\u22121 lines contain roads, one road per line. Each line contains a pair of integers xixi, yiyi (1\u2264xi,yi\u2264n1\u2264xi,yi\u2264n), where xixi, yiyi are cities connected with the ii-th road.OutputIn the first line print the required rr (1\u2264r\u2264n\u221211\u2264r\u2264n\u22121). In the second line print n\u22121n\u22121 numbers c1,c2,\u2026,cn\u22121c1,c2,\u2026,cn\u22121 (1\u2264ci\u2264r1\u2264ci\u2264r), where cici is the company to own the ii-th road. If there are multiple answers, print any of them.ExamplesInputCopy6 2\n1 4\n4 3\n3 5\n3 6\n5 2\nOutputCopy2\n1 2 1 1 2 InputCopy4 2\n3 1\n1 4\n1 2\nOutputCopy1\n1 1 1 InputCopy10 2\n10 3\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9\nOutputCopy3\n1 1 2 3 2 3 1 3 1 ","tags":["binary search","constructive algorithms","dfs and similar","graphs","greedy","trees"],"code":"#include\n\n#include \n\n#include \n\nusing namespace std;\n\n\/\/using L = __int128;\n\n#include\n\n#include \n\nusing namespace __gnu_pbds;\n\nusing ll = long long;\n\nusing ull = unsigned long long;\n\nusing ld = long double;\n\n#define nd \"\\n\"\n\n#define all(x) (x).begin(), (x).end()\n\n#define lol cout <<\"i am here\"< void print(container v) { for (auto& it : v) cout << it << ' ' ;cout <\n\nll fp(ll a , ll p){ if(!p) return 1; ll v = fp(a , p\/2); v*=v;return p & 1 ? v*a : v; }\n\n\n\ntemplate using ordered_set = tree, rb_tree_tag,tree_order_statistics_node_update>;\n\n\n\nll mul (ll a, ll b){\n\n return ( ( a % mod ) * ( b % mod ) ) % mod;\n\n}\n\nll add (ll a , ll b) {\n\n return (a + b + mod) % mod;\n\n}\n\n\n\ntemplate< typename T > using min_heap = priority_queue , greater < T > > ;\n\nvector < vector > > g;\n\nvector dep(N);\n\nvoid dfs(int node , int par){\n\n for (auto &ch : g[node]){\n\n if (par == ch.first) continue;\n\n dep[ch.first] = 1 + dep[node];\n\n dfs(ch.first , node);\n\n }\n\n}\n\nvoid hi(int tc) {\n\n ll n , k; cin >>n >> k;\n\n g = vector < vector > > (n+5);\n\n for (int u , v ,i = 1; i < n; ++i){\n\n cin >> u >> v;\n\n g[u].emplace_back(v , i);\n\n g[v].emplace_back(u , i);\n\n }\n\n vector > bad;\n\n for (int i = 1; i <= n; ++i) bad.emplace_back((int)g[i].size() , i);\n\n\n\n sort(all(bad) , [&](pair & a , pair & b) {\n\n return a.first > b.first;\n\n });\n\n dfs(1 , 1);\n\n cout << bad[k].first < > order;\n\n vector col(n+5 , -1);\n\n \/\/ for (int i = 0; i < k; ++i)\n\n \/\/ for (auto &ch : g[bad[i].second])\n\n \/\/col[ch.second] = 1;\n\n for (int i = k; i < n; ++i)order.emplace_back(dep[bad[i].second] , bad[i].second);\n\n sort(all(order));\n\n\n\n for (auto &p : order){\n\n int node = p.second;\n\n set st;\n\n \/\/ cout << node <<\" \"<> tt;\n\n while(tt--) hi(++tc);\n\n return 0;\n\n}\n\n\/*\n\n * greedy\n\n * if k == 0 thw answer is equal to the maximum degree of a vertex\n\n * cuz if it was less\/ then there at least two edges out of me with the same color\n\n *\/\n\n","language":"cpp"} -{"contest_id":"1325","problem_id":"B","statement":"B. CopyCopyCopyCopyCopytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEhab has an array aa of length nn. He has just enough free time to make a new array consisting of nn copies of the old array, written back-to-back. What will be the length of the new array's longest increasing subsequence?A sequence aa is a subsequence of an array bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements. The longest increasing subsequence of an array is the longest subsequence such that its elements are ordered in strictly increasing order.InputThe first line contains an integer tt\u00a0\u2014 the number of test cases you need to solve. The description of the test cases follows.The first line of each test case contains an integer nn (1\u2264n\u22641051\u2264n\u2264105)\u00a0\u2014 the number of elements in the array aa.The second line contains nn space-separated integers a1a1, a2a2, \u2026\u2026, anan (1\u2264ai\u22641091\u2264ai\u2264109)\u00a0\u2014 the elements of the array aa.The sum of nn across the test cases doesn't exceed 105105.OutputFor each testcase, output the length of the longest increasing subsequence of aa if you concatenate it to itself nn times.ExampleInputCopy2\n3\n3 2 1\n6\n3 1 4 1 5 9\nOutputCopy3\n5\nNoteIn the first sample; the new array is [3,2,1,3,2,1,3,2,1][3,2,1,3,2,1,3,2,1]. The longest increasing subsequence is marked in bold.In the second sample, the longest increasing subsequence will be [1,3,4,5,9][1,3,4,5,9].","tags":["greedy","implementation"],"code":"#include \n\n\n\nusing namespace std;\n\n\n\nint main()\n\n{\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(0); cout.tie(0);\n\n int32_t test; cin >> test;\n\n int32_t n, tmp;\n\n while (test--)\n\n {\n\n cin >> n;\n\n set st;\n\n for (int i = 0; i < n; i++)\n\n {\n\n cin >> tmp; st.insert(tmp);\n\n }\n\n cout << st.size() << '\\n';\n\n }\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1299","problem_id":"B","statement":"B. Aerodynamictime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGuy-Manuel and Thomas are going to build a polygon spaceship. You're given a strictly convex (i. e. no three points are collinear) polygon PP which is defined by coordinates of its vertices. Define P(x,y)P(x,y) as a polygon obtained by translating PP by vector (x,y)\u2212\u2192\u2212\u2212(x,y)\u2192. The picture below depicts an example of the translation:Define TT as a set of points which is the union of all P(x,y)P(x,y) such that the origin (0,0)(0,0) lies in P(x,y)P(x,y) (both strictly inside and on the boundary). There is also an equivalent definition: a point (x,y)(x,y) lies in TT only if there are two points A,BA,B in PP such that AB\u2212\u2192\u2212=(x,y)\u2212\u2192\u2212\u2212AB\u2192=(x,y)\u2192. One can prove TT is a polygon too. For example, if PP is a regular triangle then TT is a regular hexagon. At the picture below PP is drawn in black and some P(x,y)P(x,y) which contain the origin are drawn in colored: The spaceship has the best aerodynamic performance if PP and TT are similar. Your task is to check whether the polygons PP and TT are similar.InputThe first line of input will contain a single integer nn (3\u2264n\u22641053\u2264n\u2264105)\u00a0\u2014 the number of points.The ii-th of the next nn lines contains two integers xi,yixi,yi (|xi|,|yi|\u2264109|xi|,|yi|\u2264109), denoting the coordinates of the ii-th vertex.It is guaranteed that these points are listed in counterclockwise order and these points form a strictly convex polygon.OutputOutput \"YES\" in a separate line, if PP and TT are similar. Otherwise, output \"NO\" in a separate line. You can print each letter in any case (upper or lower).ExamplesInputCopy4\n1 0\n4 1\n3 4\n0 3\nOutputCopyYESInputCopy3\n100 86\n50 0\n150 0\nOutputCopynOInputCopy8\n0 0\n1 0\n2 1\n3 3\n4 6\n3 6\n2 5\n1 3\nOutputCopyYESNoteThe following image shows the first sample: both PP and TT are squares. The second sample was shown in the statements.","tags":["geometry"],"code":"\/**\n\n * Author: Richw818\n\n * Created: 02.10.2023 15:06:35\n\n**\/\n\n\n\n#include \n\nusing namespace std;\n\ntemplate\n\nstruct Point2D{\n\n using P2 = Point2D;\n\n T x, y;\n\n Point2D(T _x = 0, T _y = 0){ x = _x, y = _y; }\n\n bool operator<(P2 p) const { return tie(x, y) < tie(p.x, p.y); }\n\n bool operator==(P2 p) const { return tie(x, y) == tie(p.x, p.y); }\n\n bool operator!=(P2 p) const { return !(tie(x, y) == tie(p.x, p.y)); }\n\n P2 operator+(P2 p) const { return P2(x + p.x, y + p.y); }\n\n P2 operator-(P2 p) const { return P2(x - p.x, y - p.y); }\n\n P2 operator*(T d) const { return P2(x * d, y * d); }\n\n P2 operator\/(T d) const { return P2(x \/ d, y \/ d); }\n\n T cross(P2 p) const { return x * p.y - y * p.x; }\n\n T cross(P2 a, P2 b) const { return (a - *this).cross(b - *this); }\n\n T dot(P2 p) const { return x * p.x + y * p.y; }\n\n T dist2() const { return x * x + y * y; }\n\n double dist() const { return sqrt(dist2()); }\n\n P2 unit() const { return *this \/ dist(); }\n\n P2 perp() const { return P2(-y, x); }\n\n P2 normal() const { return perp().unit(); }\n\n P2 rotate(double a) const {\n\n return P2(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a));\n\n }\n\n friend ostream& operator<<(ostream& os, P2 p){\n\n return os << \"(\" << p.x << \", \" << p.y << \")\\n\";\n\n }\n\n friend istream& operator>>(istream& is, P2& p){\n\n return is >> p.x >> p.y;\n\n }\n\n};\n\nint main(){\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(nullptr);\n\n using P2 = Point2D;\n\n int n; cin >> n;\n\n if(n & 1){\n\n cout << \"NO\\n\";\n\n return 0;\n\n }\n\n vector points(n); for(auto& P : points) cin >> P;\n\n P2 check = points[0] + points[n \/ 2];\n\n for(int i = 1; i < n \/ 2; i++){\n\n if(points[i] + points[i + n \/ 2] != check){\n\n cout << \"NO\\n\";\n\n return 0;\n\n }\n\n }\n\n cout << \"YES\\n\";\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1305","problem_id":"H","statement":"H. Kuroni the Private Tutortime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAs a professional private tutor; Kuroni has to gather statistics of an exam. Kuroni has appointed you to complete this important task. You must not disappoint him.The exam consists of nn questions, and mm students have taken the exam. Each question was worth 11 point. Question ii was solved by at least lili and at most riri students. Additionally, you know that the total score of all students is tt.Furthermore, you took a glance at the final ranklist of the quiz. The students were ranked from 11 to mm, where rank 11 has the highest score and rank mm has the lowest score. Ties were broken arbitrarily.You know that the student at rank pipi had a score of sisi for 1\u2264i\u2264q1\u2264i\u2264q.You wonder if there could have been a huge tie for first place. Help Kuroni determine the maximum number of students who could have gotten as many points as the student with rank 11, and the maximum possible score for rank 11 achieving this maximum number of students.InputThe first line of input contains two integers (1\u2264n,m\u22641051\u2264n,m\u2264105), denoting the number of questions of the exam and the number of students respectively.The next nn lines contain two integers each, with the ii-th line containing lili and riri (0\u2264li\u2264ri\u2264m0\u2264li\u2264ri\u2264m).The next line contains a single integer qq (0\u2264q\u2264m0\u2264q\u2264m). The next qq lines contain two integers each, denoting pipi and sisi (1\u2264pi\u2264m1\u2264pi\u2264m, 0\u2264si\u2264n0\u2264si\u2264n). It is guaranteed that all pipi are distinct and if pi\u2264pjpi\u2264pj, then si\u2265sjsi\u2265sj.The last line contains a single integer tt (0\u2264t\u2264nm0\u2264t\u2264nm), denoting the total score of all students.OutputOutput two integers: the maximum number of students who could have gotten as many points as the student with rank 11, and the maximum possible score for rank 11 achieving this maximum number of students. If there is no valid arrangement that fits the given data, output \u22121\u22121 \u22121\u22121.ExamplesInputCopy5 4\n2 4\n2 3\n1 1\n0 1\n0 0\n1\n4 1\n7\nOutputCopy3 2\nInputCopy5 6\n0 6\n0 6\n2 5\n6 6\n4 6\n1\n3 3\n30\nOutputCopy-1 -1\nNoteFor the first sample; here is one possible arrangement that fits the data:Students 11 and 22 both solved problems 11 and 22.Student 33 solved problems 22 and 33.Student 44 solved problem 44.The total score of all students is T=7T=7. Note that the scores of the students are 22, 22, 22 and 11 respectively, which satisfies the condition that the student at rank 44 gets exactly 11 point. Finally, 33 students tied for first with a maximum score of 22, and it can be proven that we cannot do better with any other arrangement.","tags":["binary search","greedy"],"code":"#include\n\ntemplate void read(_Tp &x){\n\n\tchar ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();\n\n\tx=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();\n\n\tif(f)x=-x;\n\n}\n\ntemplate void read(_Tp &t,Args &...args){read(t);read(args...);}\n\nconst int N=100005;typedef long long ll;\n\nint L[N],R[N],p[N],s[N],a[N],n,m,val[N],VAL[N];ll t,lim[N],b[N],suf[N];\n\nll calc1(int mid){ll tot=0;for(int i=1;i<=n;++i)tot+=std::min(R[i],std::max(L[i],mid));return tot;}\n\nbool check(int mid){\n\n\tint x=-1,y=-1;for(int i=1;i<=mid;++i)if(val[i]!=-1)(x==-1?x:y)=val[i];\n\n\tif(x!=-1&&y!=-1&&x!=y)return 0;\n\n\tif(x==-1&&y==-1){\n\n\t\tll tmp=1e18,tot=0;\n\n\t\tfor(int i=1;i<=m;++i)tot+=i>mid?VAL[i]:0,tmp=std::min(tmp,(lim[i]-tot)\/std::min(i,mid));\n\n\t\tfor(int i=1;i<=m;++i)b[i]=i<=mid?tmp:VAL[i];\n\n\t}\n\n\telse for(int i=1;i<=m;++i)b[i]=i<=mid?x:VAL[i];\n\n\tll tot=0;for(int i=1;i<=m;++i){tot+=b[i],suf[i]=lim[i]-tot;if(tot>lim[i])return 0;}\n\n\tsuf[m+1]=1e18;for(int i=m;i>=1;--i)suf[i]=std::min(suf[i],suf[i+1]);\n\n\tll sum=0;for(int i=mid+1;i<=m;++i)if(val[i]==-1)b[i]=std::min(VAL[i]+suf[i]-sum,b[i-1]),sum+=b[i]-VAL[i];\n\n\tfor(int i=1;i=1;--i)if(!VAL[i])VAL[i]=VAL[i+1];\n\n\tread(t);int l=0,r=m,mid;\n\n\twhile(l>1,calc1(mid)<=t?l=mid:r=mid-1;\n\n\tll tot=0;for(int i=1;i<=n;++i)a[i]=std::min(R[i],std::max(L[i],l)),tot+=a[i];\n\n\tfor(int i=1;i<=n;++i)if(tot>1,check(mid)?l=mid:r=mid-1;\n\n\tif(!check(l))return puts(\"-1 -1\"),0;\n\n\tprintf(\"%d %lld\\n\",l,b[1]);\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1307","problem_id":"A","statement":"A. Cow and Haybalestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of nn haybale piles on the farm. The ii-th pile contains aiai haybales. However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices ii and jj (1\u2264i,j\u2264n1\u2264i,j\u2264n) such that |i\u2212j|=1|i\u2212j|=1 and ai>0ai>0 and apply ai=ai\u22121ai=ai\u22121, aj=aj+1aj=aj+1. She may also decide to not do anything on some days because she is lazy.Bessie wants to maximize the number of haybales in pile 11 (i.e. to maximize a1a1), and she only has dd days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile 11 if she acts optimally!InputThe input consists of multiple test cases. The first line contains an integer tt (1\u2264t\u22641001\u2264t\u2264100) \u00a0\u2014 the number of test cases. Next 2t2t lines contain a description of test cases \u00a0\u2014 two lines per test case.The first line of each test case contains integers nn and dd (1\u2264n,d\u22641001\u2264n,d\u2264100) \u2014 the number of haybale piles and the number of days, respectively. The second line of each test case contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u22641000\u2264ai\u2264100) \u00a0\u2014 the number of haybales in each pile.OutputFor each test case, output one integer: the maximum number of haybales that may be in pile 11 after dd days if Bessie acts optimally.ExampleInputCopy3\n4 5\n1 0 3 2\n2 2\n100 1\n1 8\n0\nOutputCopy3\n101\n0\nNoteIn the first test case of the sample; this is one possible way Bessie can end up with 33 haybales in pile 11: On day one, move a haybale from pile 33 to pile 22 On day two, move a haybale from pile 33 to pile 22 On day three, move a haybale from pile 22 to pile 11 On day four, move a haybale from pile 22 to pile 11 On day five, do nothing In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile 22 to pile 11 on the second day.","tags":["greedy","implementation"],"code":"#include\nusing namespace std;\nint a[1001];\nint main()\n{\n\tint n,t;\n\tcin>>t;\n\twhile(t--)\n\t{\n\t\tint k=0;\n\t\tcin>>n>>k;\n\t\tint s=0;\n\t\tfor(int i=1;i<=n;i++)\n\t\t{\n\t\t\tcin>>a[i];\n\t\t}\n\t\tfor(int i=1;i<=k;i++)\n\t\t{\n\t\t\tfor(int j=2;j<=n;j++)\n\t\t\t{\n\t\t\t\tif(a[j]>0)\n\t\t\t\t{\n\t\t\t\t\ta[j]--,a[j-1]++;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t}\n\t\tcout<\n\nusing namespace std;\nstruct custom_hash\n{\n static uint64_t splitmix64(uint64_t x)\n {\n x += 0x9e3779b97f4a7c15;\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n return x ^ (x >> 31);\n }\n size_t operator()(uint64_t x) const\n {\n static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\n return splitmix64(x + FIXED_RANDOM);\n }\n};\n#define endl '\\n'\n#define fio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n#define incr_loop(a, n) for (int i = a; i < n; i++)\n#define decr_loop(a, b) for (int i = a; i > b; i--)\n#define nested_incr_loop(a, b) for (int j = a; j < b; j++)\n#define nested_decr_loop(a, b) for (int j = b; j > a; --j)\ntypedef long long ll;\ntypedef double long dl;\n#define pb push_back\n#define ppb pop_back\n#define MP make_pair\n#define sort_all(v) sort(all(v));\n#define PI 3.141592653589793238462\n#define space cout << ' ';\ntypedef unordered_map mp;\nll MOD = 998244353;\n\nvector primes;\nll ans = 0;\n\nvoid solve()\n{\n ll n, m, k;\n cin >> n >> m >> k;\n vector arr(n), brr(m);\n for (int i = 0; i < n; i++)\n {\n cin >> arr[i];\n }\n for (int i = 0; i < m; i++)\n {\n cin >> brr[i];\n }\n vector mmp1(n + 1), mmp2(m + 1);\n for (int i = 0; i < n;)\n {\n if (arr[i] == 0)\n {\n i++;\n continue;\n }\n int cnt = 0;\n while (i < n && arr[i] == 1)\n {\n i++;\n cnt++;\n }\n for (int j = 1; j <= cnt; j++)\n {\n mmp1[j] += cnt + 1 - j;\n }\n }\n for (int i = 0; i < m;)\n {\n if (brr[i] == 0)\n {\n i++;\n continue;\n }\n int cnt = 0;\n while (i < m && brr[i] == 1)\n {\n i++;\n cnt++;\n }\n for (int j = 1; j <= cnt; j++)\n {\n mmp2[j] += cnt + 1 - j;\n }\n }\n\n ll ans = 0;\n for (int i = 1; i <= k; i++)\n {\n if (i > n)\n {\n break;\n }\n if (k % i)\n {\n continue;\n }\n if ((k \/ i) > m)\n {\n continue;\n }\n ans += mmp1[i] * mmp2[k \/ i];\n }\n cout << ans << endl;\n}\n\nint main()\n{\n fio int tc = 1;\n \/\/ cin >> tc;\n \/\/ int p = 10000000;\n \/\/ vector is_prime(p, true);\n \/\/ for (int i = 2; i < p; ++i)\n \/\/ {\n \/\/ if (!is_prime[i])\n \/\/ continue;\n \/\/ primes.push_back(i);\n \/\/ for (int j = i + i; j < p; j += i)\n \/\/ {\n \/\/ is_prime[j] = false;\n \/\/ }\n \/\/ }\n\n for (int t = 1; t <= tc; t++)\n {\n \/\/ cout << \"Case #\" << t << \": \";\n solve();\n }\n}","language":"cpp"} -{"contest_id":"1323","problem_id":"B","statement":"B. Count Subrectanglestime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn and array bb of length mm both consisting of only integers 00 and 11. Consider a matrix cc of size n\u00d7mn\u00d7m formed by following rule: ci,j=ai\u22c5bjci,j=ai\u22c5bj (i.e. aiai multiplied by bjbj). It's easy to see that cc consists of only zeroes and ones too.How many subrectangles of size (area) kk consisting only of ones are there in cc?A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x1,x2,y1,y2x1,x2,y1,y2 (1\u2264x1\u2264x2\u2264n1\u2264x1\u2264x2\u2264n, 1\u2264y1\u2264y2\u2264m1\u2264y1\u2264y2\u2264m) a subrectangle c[x1\u2026x2][y1\u2026y2]c[x1\u2026x2][y1\u2026y2] is an intersection of the rows x1,x1+1,x1+2,\u2026,x2x1,x1+1,x1+2,\u2026,x2 and the columns y1,y1+1,y1+2,\u2026,y2y1,y1+1,y1+2,\u2026,y2.The size (area) of a subrectangle is the total number of cells in it.InputThe first line contains three integers nn, mm and kk (1\u2264n,m\u226440000,1\u2264k\u2264n\u22c5m1\u2264n,m\u226440000,1\u2264k\u2264n\u22c5m), length of array aa, length of array bb and required size of subrectangles.The second line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u226410\u2264ai\u22641), elements of aa.The third line contains mm integers b1,b2,\u2026,bmb1,b2,\u2026,bm (0\u2264bi\u226410\u2264bi\u22641), elements of bb.OutputOutput single integer\u00a0\u2014 the number of subrectangles of cc with size (area) kk consisting only of ones.ExamplesInputCopy3 3 2\n1 0 1\n1 1 1\nOutputCopy4\nInputCopy3 5 4\n1 1 1\n1 1 1 1 1\nOutputCopy14\nNoteIn first example matrix cc is: There are 44 subrectangles of size 22 consisting of only ones in it: In second example matrix cc is: ","tags":["binary search","greedy","implementation"],"code":"#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#define forin(i,a,n) for(int i=a;i<=n;i++)\n\n#define forni(i,n,a) for(int i=n;i>=a;i--)\n\n#define fi first\n\n#define se second\n\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef double db;\n\ntypedef pair PII;\n\nconst double eps=1e-7;\n\nconst int N=6e5+7 ,M=2*N , INF=0x3f3f3f3f,mod=1e9+7;\n\ninline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}\n\nwhile(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}\n\nvoid stin() {freopen(\"in_put.txt\",\"r\",stdin);freopen(\"my_out_put.txt\",\"w\",stdout);}\n\n\n\ntemplate T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}\n\ntemplate T lcm(T a,T b) {return a*b\/gcd(a,b);}\n\n\n\nint T;\n\nint n,m,k;\n\nint a[N],b[N];\n\nint la[N],lb[N];\n\n\n\nvoid solve() { \n\n n=read(),m=read(),k=read();\n\n \n\n for(int i=1;i<=n;i++) a[i]=read();\n\n for(int i=1;i<=m;i++) b[i]=read();\n\n \n\n vector vec;\n\n for(int i=1;i<=k\/i;i++) {\n\n if(k%i==0) {\n\n vec.push_back(i);\n\n if(i!=k\/i) vec.push_back(k\/i);\n\n }\n\n }\n\n \n\n sort(vec.begin(),vec.end());\n\n \n\n int last=0;\n\n for(int i=1;i<=n;i++) {\n\n if(a[i]==1) last++;\n\n else {\n\n int temp=last;\n\n for(int j=1;j<=temp;j++) la[j]+=last--;\n\n last=0;\n\n }\n\n }\n\n \n\n if(last!=0) {\n\n int temp=last;\n\n for(int j=1;j<=temp;j++) la[j]+=last--;\n\n }\n\n \n\n last=0;\n\n for(int i=1;i<=m;i++) {\n\n if(b[i]==1) last++;\n\n else {\n\n int temp=last;\n\n for(int j=1;j<=temp;j++) lb[j]+=last--;\n\n last=0;\n\n }\n\n }\n\n \n\n if(last!=0) {\n\n int temp=last;\n\n for(int j=1;j<=temp;j++) lb[j]+=last--;\n\n }\n\n \n\n ll ans=0;\n\n for(int i=0;i=1&&l<=n&&r>=1&&r<=m) {\n\n ans+=(ll)la[l]*lb[r];\n\n }\n\n }\n\n \n\n printf(\"%lld\\n\",ans);\n\n}\n\n\n\nint main() {\n\n \/\/ init();\n\n \/\/ stin();\n\n \n\n \/\/ scanf(\"%d\",&T);\n\n T=1; \n\n while(T--) solve();\n\n \n\n return 0; \n\n} ","language":"cpp"} -{"contest_id":"1301","problem_id":"D","statement":"D. Time to Runtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBashar was practicing for the national programming contest. Because of sitting too much in front of the computer without doing physical movements and eating a lot Bashar became much fatter. Bashar is going to quit programming after the national contest and he is going to become an actor (just like his father); so he should lose weight.In order to lose weight, Bashar is going to run for kk kilometers. Bashar is going to run in a place that looks like a grid of nn rows and mm columns. In this grid there are two one-way roads of one-kilometer length between each pair of adjacent by side cells, one road is going from the first cell to the second one, and the other road is going from the second cell to the first one. So, there are exactly (4nm\u22122n\u22122m)(4nm\u22122n\u22122m) roads.Let's take, for example, n=3n=3 and m=4m=4. In this case, there are 3434 roads. It is the picture of this case (arrows describe roads):Bashar wants to run by these rules: He starts at the top-left cell in the grid; In one move Bashar may go up (the symbol 'U'), down (the symbol 'D'), left (the symbol 'L') or right (the symbol 'R'). More formally, if he stands in the cell in the row ii and in the column jj, i.e. in the cell (i,j)(i,j) he will move to: in the case 'U' to the cell (i\u22121,j)(i\u22121,j); in the case 'D' to the cell (i+1,j)(i+1,j); in the case 'L' to the cell (i,j\u22121)(i,j\u22121); in the case 'R' to the cell (i,j+1)(i,j+1); He wants to run exactly kk kilometers, so he wants to make exactly kk moves; Bashar can finish in any cell of the grid; He can't go out of the grid so at any moment of the time he should be on some cell; Bashar doesn't want to get bored while running so he must not visit the same road twice. But he can visit the same cell any number of times. Bashar asks you if it is possible to run by such rules. If it is possible, you should tell him how should he run.You should give him aa steps to do and since Bashar can't remember too many steps, aa should not exceed 30003000. In every step, you should give him an integer ff and a string of moves ss of length at most 44 which means that he should repeat the moves in the string ss for ff times. He will perform the steps in the order you print them.For example, if the steps are 22 RUD, 33 UUL then the moves he is going to move are RUD ++ RUD ++ UUL ++ UUL ++ UUL == RUDRUDUULUULUUL.Can you help him and give him a correct sequence of moves such that the total distance he will run is equal to kk kilometers or say, that it is impossible?InputThe only line contains three integers nn, mm and kk (1\u2264n,m\u22645001\u2264n,m\u2264500, 1\u2264k\u22641091\u2264k\u2264109), which are the number of rows and the number of columns in the grid and the total distance Bashar wants to run.OutputIf there is no possible way to run kk kilometers, print \"NO\" (without quotes), otherwise print \"YES\" (without quotes) in the first line.If the answer is \"YES\", on the second line print an integer aa (1\u2264a\u226430001\u2264a\u22643000)\u00a0\u2014 the number of steps, then print aa lines describing the steps.To describe a step, print an integer ff (1\u2264f\u22641091\u2264f\u2264109) and a string of moves ss of length at most 44. Every character in ss should be 'U', 'D', 'L' or 'R'.Bashar will start from the top-left cell. Make sure to move exactly kk moves without visiting the same road twice and without going outside the grid. He can finish at any cell.We can show that if it is possible to run exactly kk kilometers, then it is possible to describe the path under such output constraints.ExamplesInputCopy3 3 4\nOutputCopyYES\n2\n2 R\n2 L\nInputCopy3 3 1000000000\nOutputCopyNO\nInputCopy3 3 8\nOutputCopyYES\n3\n2 R\n2 D\n1 LLRR\nInputCopy4 4 9\nOutputCopyYES\n1\n3 RLD\nInputCopy3 4 16\nOutputCopyYES\n8\n3 R\n3 L\n1 D\n3 R\n1 D\n1 U\n3 L\n1 D\nNoteThe moves Bashar is going to move in the first example are: \"RRLL\".It is not possible to run 10000000001000000000 kilometers in the second example because the total length of the roads is smaller and Bashar can't run the same road twice.The moves Bashar is going to move in the third example are: \"RRDDLLRR\".The moves Bashar is going to move in the fifth example are: \"RRRLLLDRRRDULLLD\". It is the picture of his run (the roads on this way are marked with red and numbered in the order of his running):","tags":["constructive algorithms","graphs","implementation"],"code":"#include\n\n#include \n\n\/\/#include\n\n\n\n\/\/pragmas\n\n#pragma GCC optimize(\"O3\")\n\n\n\n\/\/types\n\n#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\n\n#define ll long long int\n\n#define ull unsigned long long int\n\n#define vec vector\n\n#define pall pair\n\n#define vecpair vector>\n\n#define vecvec(a, i, j) vector> a (i, vec (j, 0))\n\n#define vecvecvec(a, i, j, k) vector>> dp (i + 1, vector>(j + 1, vector(k + 1, 0)))\n\n\n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\n\n\n\/\/random stuff\n\n#define all(a) a.begin(),a.end()\n\n#define read(a) for (auto &x : a) cin >> x\n\n#define endl \"\\n\"\n\n#define pb push_back\n\n#define size(x) (long long int)x.size()\n\n#define print(a) for(auto x : a) cout << x << \" \"; cout << endl\n\n#define sp \" \" \n\nll INF = 9223372036854775807;\n\ntypedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set;\n\nstruct custom_hash {\n\n static uint64_t splitmix64(uint64_t x) {\n\n x += 0x9e3779b97f4a7c15;\n\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n\n return x ^ (x >> 31);\n\n }\n\n\n\n size_t operator()(uint64_t x) const {\n\n static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\n\n return splitmix64(x + FIXED_RANDOM);\n\n }\n\n};\n\n#define safe_map unordered_map\n\n\n\n\n\n\/\/debug\n\nvoid __print(int x) {cerr << x;}\n\nvoid __print(long x) {cerr << x;}\n\nvoid __print(long long x) {cerr << x;}\n\nvoid __print(unsigned x) {cerr << x;}\n\nvoid __print(unsigned long x) {cerr << x;}\n\nvoid __print(unsigned long long x) {cerr << x;}\n\nvoid __print(float x) {cerr << x;}\n\nvoid __print(double x) {cerr << x;}\n\nvoid __print(long double x) {cerr << x;}\n\nvoid __print(char x) {cerr << '\\'' << x << '\\'';}\n\nvoid __print(const char *x) {cerr << '\\\"' << x << '\\\"';}\n\nvoid __print(const string &x) {cerr << '\\\"' << x << '\\\"';}\n\nvoid __print(bool x) {cerr << (x ? \"true\" : \"false\");}\n\n\n\ntemplate\n\nvoid __print(const pair &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}\n\ntemplate\n\nvoid __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? \",\" : \"\"), __print(i); cerr << \"}\";}\n\nvoid _print() {cerr << \"]\\n\";}\n\ntemplate \n\nvoid _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << \", \"; _print(v...);}\n\n#ifndef ONLINE_JUDGE\n\n#define debug(x...) cerr << \"[\" << #x << \"] = [\"; _print(x)\n\n#define reach cerr<<\"reached\"< a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}\n\nll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}\n\nvoid extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a \/ b); v[0] = x; return;} \/\/pass an arry of size1 3\n\nll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} \/\/for non prime b\n\nll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}\n\nbool revsort(ll a, ll b) {return a > b;}\n\nvoid swap(int &x, int &y) {int temp = x; x = y; y = temp;}\n\nll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}\n\nvoid google(int t) {cout << \"Case #\" << t << \": \";}\n\nvector sieve(int n) {int*arr = new int[n + 1](); vector vect; for (ll i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (ll j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}\n\nll mod_add(ll a, ll b, ll m = 1000000007) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}\n\nll mod_mul(ll a, ll b, ll m = 1000000007) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}\n\nll mod_sub(ll a, ll b, ll m = 1000000007) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}\n\nll mod_div(ll a, ll b, ll m = 1000000007) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} \/\/only for prime m\n\nll phin(ll n) {ll number = n; if (n % 2 == 0) {number \/= 2; while (n % 2 == 0) n \/= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n \/= i; number = (number \/ i * (i - 1));}} if (n > 1)number = (number \/ n * (n - 1)) ; return number;} \/\/O(sqrt(N))\n\nvoid precision(int a) {cout << setprecision(a) << fixed;}\n\nll ceil_div(ll x, ll y){return (x + y - 1) \/ y;}\n\nunsigned long long power(unsigned long long x,ll y, ll p){unsigned long long res = 1;x = x % p; while (y > 0){if (y & 1)res = (res * x) % p;y = y >> 1;x = (x * x) % p;}return res;}\n\nunsigned long long modInverse(unsigned long long n,int p){return power(n, p - 2, p);}\n\nll nCr(ll n,ll r, ll p){if (n < r)return 0;if (r == 0)return 1;unsigned long long fac[n + 1];fac[0] = 1;for (int i = 1; i <= n; i++)fac[i] = (fac[i - 1] * i) % p;return (fac[n] * modInverse(fac[r], p) % p* modInverse(fac[n - r], p) % p)% p;}\n\nll accumulate(const vec &nums){ll sum = 0; for(auto x : nums) sum += x; return sum;}\n\nll tmax(ll a, ll b, ll c = 0, ll d = -INF, ll e = -INF, ll f = -INF){return max(a, max(b, max(c, max(d, max(e, f)))));}\n\nint log2_floor(unsigned long long i) {return i ? __builtin_clzll(1) - __builtin_clzll(i) : -1;}\n\nstring bin(ll n){return bitset<32>(n).to_string();}\n\n\/*--------------------------------------------------------------------------------------------------------------------------*\/\n\n\n\nll mod = 1000000007;\n\nvector> mv;\n\n\n\n\/\/code starts\n\nint main()\n\n{\n\n fastio();\n\n\n\n ll n, m, k;\n\n cin >> m >> n >> k;\n\n if(k <= 4 * n * m - 2 * (n + m))\n\n {\n\n cout << \"YES\" << endl;\n\n ll run = 0;\n\n if(n - 1)\n\n {\n\n mv.pb({min(k, n - 1), \"R\"});\n\n run += min(k, n - 1);\n\n }\n\n for(ll i = n; i > 1 and run < k; i --)\n\n {\n\n if((k - run)\/3 == 0)\n\n { \n\n if(m - 1)\n\n {\n\n if(k - run == 1) mv.pb({1, \"D\"});\n\n if(k - run == 2) mv.pb({1, \"DL\"});\n\n run += k - run;\n\n }\n\n }\n\n else if((k - run)\/3 < m - 1)\n\n {\n\n ll down = (k - run)\/3; \/\/go down by this much\n\n mv.pb({(k - run)\/3, \"DLR\"});\n\n run += ((k - run)\/3) * 3;\n\n if(down and run < k)\n\n {\n\n mv.pb({1, \"D\"});\n\n run ++;\n\n if(k - run)\n\n {\n\n mv.pb({1, \"U\"});\n\n run ++;\n\n }\n\n }\n\n } \n\n else\n\n {\n\n if(m - 1)\n\n {\n\n mv.pb({m - 1, \"DLR\"});\n\n run += (m - 1) * 3; \n\n if(run < k) \/\/go back up\n\n {\n\n mv.pb({min(k - run, m - 1), \"U\"});\n\n run += min(k - run, m - 1);\n\n } \n\n }\n\n }\n\n if(run < k) \/\/go left\n\n {\n\n mv.pb({1, \"L\"});\n\n run ++;\n\n }\n\n }\n\n if(run < k)\n\n {\n\n if(m - 1)\n\n {\n\n mv.pb({min(k - run, m - 1), \"D\"});\n\n run += min(k - run, m - 1);\n\n if(run < k)\n\n {\n\n mv.pb({min(k - run, m - 1), \"U\"});\n\n run += min(k - run, m - 1);\n\n }\n\n }\n\n }\n\n cout << size(mv) << endl;\n\n for(auto &x : mv) cout << x.first << sp << x.second << endl;\n\n }\n\n else\n\n cout << \"NO\" << endl;\n\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\/\/ There is an idea of a Patrick Bateman. Some kind of abstraction.\n\n\/\/ But there is no real me. Only an entity. Something illusory. \n\n\/\/ And though I can hide my cold gaze, and you can shake my hand and\n\n\/\/ feel flesh gripping yours, and maybe you can even sense our lifestyles\n\n\/\/ are probably comparable, I simply am not there.","language":"cpp"} -{"contest_id":"1284","problem_id":"G","statement":"G. Seollaltime limit per test3 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputIt is only a few days until Seollal (Korean Lunar New Year); and Jaehyun has invited his family to his garden. There are kids among the guests. To make the gathering more fun for the kids, Jaehyun is going to run a game of hide-and-seek.The garden can be represented by a n\u00d7mn\u00d7m grid of unit cells. Some (possibly zero) cells are blocked by rocks, and the remaining cells are free. Two cells are neighbors if they share an edge. Each cell has up to 4 neighbors: two in the horizontal direction and two in the vertical direction. Since the garden is represented as a grid, we can classify the cells in the garden as either \"black\" or \"white\". The top-left cell is black, and two cells which are neighbors must be different colors. Cell indices are 1-based, so the top-left corner of the garden is cell (1,1)(1,1).Jaehyun wants to turn his garden into a maze by placing some walls between two cells. Walls can only be placed between neighboring cells. If the wall is placed between two neighboring cells aa and bb, then the two cells aa and bb are not neighboring from that point. One can walk directly between two neighboring cells if and only if there is no wall directly between them. A maze must have the following property. For each pair of free cells in the maze, there must be exactly one simple path between them. A simple path between cells aa and bb is a sequence of free cells in which the first cell is aa, the last cell is bb, all cells are distinct, and any two consecutive cells are neighbors which are not directly blocked by a wall.At first, kids will gather in cell (1,1)(1,1), and start the hide-and-seek game. A kid can hide in a cell if and only if that cell is free, it is not (1,1)(1,1), and has exactly one free neighbor. Jaehyun planted roses in the black cells, so it's dangerous if the kids hide there. So Jaehyun wants to create a maze where the kids can only hide in white cells.You are given the map of the garden as input. Your task is to help Jaehyun create a maze.InputYour program will be judged in multiple test cases.The first line contains the number of test cases tt. (1\u2264t\u22641001\u2264t\u2264100). Afterward, tt test cases with the described format will be given.The first line of a test contains two integers n,mn,m (2\u2264n,m\u2264202\u2264n,m\u226420), the size of the grid.In the next nn line of a test contains a string of length mm, consisting of the following characters (without any whitespace): O: A free cell. X: A rock. It is guaranteed that the first cell (cell (1,1)(1,1)) is free, and every free cell is reachable from (1,1)(1,1). If t\u22652t\u22652 is satisfied, then the size of the grid will satisfy n\u226410,m\u226410n\u226410,m\u226410. In other words, if any grid with size n>10n>10 or m>10m>10 is given as an input, then it will be the only input on the test case (t=1t=1).OutputFor each test case, print the following:If there are no possible mazes, print a single line NO.Otherwise, print a single line YES, followed by a grid of size (2n\u22121)\u00d7(2m\u22121)(2n\u22121)\u00d7(2m\u22121) denoting the found maze. The rules for displaying the maze follows. All cells are indexed in 1-base. For all 1\u2264i\u2264n,1\u2264j\u2264m1\u2264i\u2264n,1\u2264j\u2264m, if the cell (i,j)(i,j) is free cell, print 'O' in the cell (2i\u22121,2j\u22121)(2i\u22121,2j\u22121). Otherwise, print 'X' in the cell (2i\u22121,2j\u22121)(2i\u22121,2j\u22121). For all 1\u2264i\u2264n,1\u2264j\u2264m\u221211\u2264i\u2264n,1\u2264j\u2264m\u22121, if the neighboring cell (i,j),(i,j+1)(i,j),(i,j+1) have wall blocking it, print ' ' in the cell (2i\u22121,2j)(2i\u22121,2j). Otherwise, print any printable character except spaces in the cell (2i\u22121,2j)(2i\u22121,2j). A printable character has an ASCII code in range [32,126][32,126]: This includes spaces and alphanumeric characters. For all 1\u2264i\u2264n\u22121,1\u2264j\u2264m1\u2264i\u2264n\u22121,1\u2264j\u2264m, if the neighboring cell (i,j),(i+1,j)(i,j),(i+1,j) have wall blocking it, print '\u00a0' in the cell (2i,2j\u22121)(2i,2j\u22121). Otherwise, print any printable character except spaces in the cell (2i,2j\u22121)(2i,2j\u22121) For all 1\u2264i\u2264n\u22121,1\u2264j\u2264m\u221211\u2264i\u2264n\u22121,1\u2264j\u2264m\u22121, print any printable character in the cell (2i,2j)(2i,2j). Please, be careful about trailing newline characters or spaces. Each row of the grid should contain exactly 2m\u221212m\u22121 characters, and rows should be separated by a newline character. Trailing spaces must not be omitted in a row.ExampleInputCopy4\n2 2\nOO\nOO\n3 3\nOOO\nXOO\nOOO\n4 4\nOOOX\nXOOX\nOOXO\nOOOO\n5 6\nOOOOOO\nOOOOOO\nOOOOOO\nOOOOOO\nOOOOOO\nOutputCopyYES\nOOO\n O\nOOO\nNO\nYES\nOOOOO X\n O O \nX O O X\n O \nOOO X O\nO O O\nO OOOOO\nYES\nOOOOOOOOOOO\n O O O\nOOO OOO OOO\nO O O \nOOO OOO OOO\n O O O\nOOO OOO OOO\nO O O \nOOO OOO OOO\n","tags":["graphs"],"code":"\/*+Rainybunny+*\/\n\n#include \n\n#define rep(i, l, r) for (int i = l, rep##i = r; i <= rep##i; ++i)\n#define per(i, r, l) for (int i = r, per##i = l; i >= per##i; --i)\n\ntypedef std::pair PII;\n#define fi first\n#define se second\n\nconst int MAXN = 20, IINF = 0x3f3f3f3f;\nconst int MOVE[4][2] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };\nint n, m, deg[MAXN * MAXN + 5];\nchar maz[MAXN + 5][MAXN + 5], str[MAXN * 2 + 5][MAXN * 2 + 5];\n\ninline bool inside(const int i, const int j) {\n return 1 <= i && i <= n && 1 <= j && j <= m;\n}\n\ninline int id(const int i, const int j) {\n return (i - 1) * m + j;\n}\n\nstruct DSU {\n int fa[MAXN * MAXN + 5], siz[MAXN * MAXN + 5];\n inline void init() { rep (i, 1, n * m) siz[fa[i] = i] = 1; }\n inline int find(const int x) {\n return x == fa[x] ? x : fa[x] = find(fa[x]);\n }\n inline bool unite(int x, int y) {\n if ((x = find(x)) == (y = find(y))) return false;\n if (siz[x] < siz[y]) x ^= y ^= x ^= y;\n return siz[fa[y] = x] += siz[y], true;\n }\n};\n\nnamespace MI { \/\/ Matroid Intersection.\n\n\/\/ M1: deg>=2; M2: no circle.\n\nstd::vector U;\nstd::vector ans, dis, L, R, T;\nstd::queue que;\nDSU dsu;\n\ninline void initInd() {\n dsu.init();\n rep (i, 1, n) rep (j, 1, m) deg[id(i, j)] = (i + j) & 1 ? -IINF : 0;\n deg[1] = -IINF;\n rep (i, 0, int(U.size()) - 1) if (ans[i]) {\n dsu.unite(U[i].fi, U[i].se), ++deg[U[i].fi], ++deg[U[i].se];\n }\n}\n\ninline void build() {\n initInd();\n L.clear(), R.clear();\n T.clear(), T.resize(U.size());\n dis.clear(), dis.resize(U.size(), IINF);\n rep (i, 0, int(U.size()) - 1) {\n if (ans[i]) L.push_back(i);\n else {\n R.push_back(i);\n if (deg[U[i].fi] < 2 && deg[U[i].se] < 2) T[i] = true;\n if (dsu.find(U[i].fi) != dsu.find(U[i].se)) {\n que.push(i), dis[i] = 0;\n }\n }\n }\n}\n\ninline std::vector augment() {\n int fin = -1;\n std::vector pre(U.size(), -1);\n while (!que.empty()) {\n int u = que.front(); que.pop();\n if (T[u]) { fin = u; break; }\n if (ans[u]) {\n dsu.init();\n for (int x: L) if (x != u) dsu.unite(U[x].fi, U[x].se);\n for (int v: R) {\n if (dis[v] == IINF && dsu.find(U[v].fi) != dsu.find(U[v].se)) {\n dis[v] = dis[u] + 1, pre[v] = u, que.push(v);\n }\n }\n } else {\n for (int v: L) if (dis[v] == IINF) {\n ++deg[U[u].fi], ++deg[U[u].se];\n --deg[U[v].fi], --deg[U[v].se];\n if (deg[U[u].fi] <= 2 && deg[U[u].se] <= 2\n && deg[U[v].fi] <= 2 && deg[U[v].se] <= 2) {\n dis[v] = dis[u] + 1, pre[v] = u, que.push(v);\n }\n --deg[U[u].fi], --deg[U[u].se];\n ++deg[U[v].fi], ++deg[U[v].se];\n }\n }\n }\n if (!~fin) return {};\n while (!que.empty()) que.pop();\n std::vector ret;\n ret.push_back(fin);\n while (~pre[fin]) ret.push_back(fin = pre[fin]);\n return ret;\n}\n\ninline void solve() {\n ans.clear(), ans.resize(U.size());\n while (true) {\n build();\n auto&& res(augment());\n if (res.empty()) break;\n for (int id: res) ans[id] ^= 1;\n }\n}\n\n} \/\/ namespace MI.\n\nint main() {\n int T; scanf(\"%d\", &T);\n while (T--) {\n scanf(\"%d %d\", &n, &m);\n rep (i, 1, n) scanf(\"%s\", maz[i] + 1);\n\n MI::U.clear();\n rep (i, 1, n) rep (j, 1, m) {\n if (~(i + j) & 1 && id(i, j) != 1 && maz[i][j] == 'O') {\n rep (k, 0, 3) {\n int x = i + MOVE[k][0], y = j + MOVE[k][1];\n if (inside(x, y) && maz[x][y] == 'O') {\n MI::U.emplace_back(id(i, j), id(x, y));\n }\n }\n }\n }\n\n MI::solve();\n MI::initInd();\n rep (i, 1, n) rep (j, 1, m) {\n if (~(i + j) & 1 && id(i, j) != 1\n && maz[i][j] == 'O' && deg[id(i, j)] != 2) {\n puts(\"NO\"); goto FIN;\n }\n }\n rep (i, 1, 2 * n - 1) {\n rep (j, 1, 2 * m - 1) {\n str[i][j] = i & 1 && j & 1 ? maz[i + 1 >> 1][j + 1 >> 1] : ' ';\n }\n str[i][2 * m] = '\\0';\n }\n\n \n if (maz[1][2] == 'O') {\n MI::U.emplace_back(id(1, 1), id(1, 2)), MI::ans.push_back(0);\n }\n if (maz[2][1] == 'O') {\n MI::U.emplace_back(id(1, 1), id(2, 1)), MI::ans.push_back(0);\n }\n rep (i, 0, int(MI::U.size()) - 1) {\n if (MI::dsu.find(MI::U[i].fi) != MI::dsu.find(MI::U[i].se)) {\n MI::dsu.unite(MI::U[i].fi, MI::U[i].se);\n MI::ans[i] = true;\n }\n }\n rep (i, 1, n) rep (j, 1, m) {\n if (maz[i][j] == 'O' && MI::dsu.find(id(i, j))\n != MI::dsu.find(id(1, 1))) {\n puts(\"NO\"); goto FIN;\n }\n }\n\n rep (i, 0, int(MI::U.size()) - 1) if (MI::ans[i]) {\n int b = (MI::U[i].fi - 1) % m + 1, a = (MI::U[i].fi - b) \/ m + 1;\n int d = (MI::U[i].se - 1) % m + 1, c = (MI::U[i].se - d) \/ m + 1;\n str[a + c - 1][b + d - 1] = 'O';\n }\n puts(\"YES\");\n rep (i, 1, 2 * n - 1) puts(str[i] + 1);\n FIN: ;\n }\n return 0;\n}\n","language":"cpp"} -{"contest_id":"1299","problem_id":"C","statement":"C. Water Balancetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn water tanks in a row, ii-th of them contains aiai liters of water. The tanks are numbered from 11 to nn from left to right.You can perform the following operation: choose some subsegment [l,r][l,r] (1\u2264l\u2264r\u2264n1\u2264l\u2264r\u2264n), and redistribute water in tanks l,l+1,\u2026,rl,l+1,\u2026,r evenly. In other words, replace each of al,al+1,\u2026,aral,al+1,\u2026,ar by al+al+1+\u22ef+arr\u2212l+1al+al+1+\u22ef+arr\u2212l+1. For example, if for volumes [1,3,6,7][1,3,6,7] you choose l=2,r=3l=2,r=3, new volumes of water will be [1,4.5,4.5,7][1,4.5,4.5,7]. You can perform this operation any number of times.What is the lexicographically smallest sequence of volumes of water that you can achieve?As a reminder:A sequence aa is lexicographically smaller than a sequence bb of the same length if and only if the following holds: in the first (leftmost) position where aa and bb differ, the sequence aa has a smaller element than the corresponding element in bb.InputThe first line contains an integer nn (1\u2264n\u22641061\u2264n\u2264106)\u00a0\u2014 the number of water tanks.The second line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641061\u2264ai\u2264106)\u00a0\u2014 initial volumes of water in the water tanks, in liters.Because of large input, reading input as doubles is not recommended.OutputPrint the lexicographically smallest sequence you can get. In the ii-th line print the final volume of water in the ii-th tank.Your answer is considered correct if the absolute or relative error of each aiai does not exceed 10\u2212910\u22129.Formally, let your answer be a1,a2,\u2026,ana1,a2,\u2026,an, and the jury's answer be b1,b2,\u2026,bnb1,b2,\u2026,bn. Your answer is accepted if and only if |ai\u2212bi|max(1,|bi|)\u226410\u22129|ai\u2212bi|max(1,|bi|)\u226410\u22129 for each ii.ExamplesInputCopy4\n7 5 5 7\nOutputCopy5.666666667\n5.666666667\n5.666666667\n7.000000000\nInputCopy5\n7 8 8 10 12\nOutputCopy7.000000000\n8.000000000\n8.000000000\n10.000000000\n12.000000000\nInputCopy10\n3 9 5 5 1 7 5 3 8 7\nOutputCopy3.000000000\n5.000000000\n5.000000000\n5.000000000\n5.000000000\n5.000000000\n5.000000000\n5.000000000\n7.500000000\n7.500000000\nNoteIn the first sample; you can get the sequence by applying the operation for subsegment [1,3][1,3].In the second sample, you can't get any lexicographically smaller sequence.","tags":["data structures","geometry","greedy"],"code":"#include \nusing namespace std;\n\nvoid solve() {\n\n\tint n;\n\tcin >> n;\n\tvectorf(n);\n\tfor(auto &i : f) cin >> i;\n\tvector>a;\n\n\tfor(int i = 0 ; i < n ; i++) {\n\t\tpair kur = make_pair(f[i] , 1);\n\n\t\twhile(!a.empty()) {\n\t\t\tif((a.back().first + kur.first) \/ (a.back().second + kur.second) < a.back().first \/ a.back().second) {\n\t\t\t\tkur.first += a.back().first , kur.second += a.back().second;\n\t\t\t\ta.pop_back();\n\t\t\t} else {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\ta.push_back(kur);\n\t}\n\n\tfor(auto [sum , knt] : a) {\n\t\tfor(int i = 0 ; i < knt ; i++) \n\t\t\tcout << fixed << setprecision(10) << sum \/ knt << '\\n';\n\t}\n\n\treturn;\n}\n\nint main() {\n\n\tios_base::sync_with_stdio(0);\n\tcin.tie(0);\n\tcout.tie(0);\n\n\tint tt = 1;\n\t\/\/ cin >> tt;\n\twhile(tt--) {\n\t\tsolve();\n\t}\n\n return 0;\n} \n","language":"cpp"} -{"contest_id":"1307","problem_id":"B","statement":"B. Cow and Friendtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play! More specifically; he wants to get from (0,0)(0,0) to (x,0)(x,0) by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance between the endpoints of a hop is one of its nn favorite numbers: a1,a2,\u2026,ana1,a2,\u2026,an. What is the minimum number of hops Rabbit needs to get from (0,0)(0,0) to (x,0)(x,0)? Rabbit may land on points with non-integer coordinates. It can be proved that Rabbit can always reach his destination.Recall that the Euclidean distance between points (xi,yi)(xi,yi) and (xj,yj)(xj,yj) is (xi\u2212xj)2+(yi\u2212yj)2\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u2212\u221a(xi\u2212xj)2+(yi\u2212yj)2.For example, if Rabbit has favorite numbers 11 and 33 he could hop from (0,0)(0,0) to (4,0)(4,0) in two hops as shown below. Note that there also exists other valid ways to hop to (4,0)(4,0) in 22 hops (e.g. (0,0)(0,0) \u2192\u2192 (2,\u22125\u2013\u221a)(2,\u22125) \u2192\u2192 (4,0)(4,0)). Here is a graphic for the first example. Both hops have distance 33, one of Rabbit's favorite numbers. In other words, each time Rabbit chooses some number aiai and hops with distance equal to aiai in any direction he wants. The same number can be used multiple times.InputThe input consists of multiple test cases. The first line contains an integer tt (1\u2264t\u226410001\u2264t\u22641000) \u00a0\u2014 the number of test cases. Next 2t2t lines contain test cases \u2014 two lines per test case.The first line of each test case contains two integers nn and xx (1\u2264n\u22641051\u2264n\u2264105, 1\u2264x\u22641091\u2264x\u2264109) \u00a0\u2014 the number of favorite numbers and the distance Rabbit wants to travel, respectively.The second line of each test case contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641091\u2264ai\u2264109) \u00a0\u2014 Rabbit's favorite numbers. It is guaranteed that the favorite numbers are distinct.It is guaranteed that the sum of nn over all the test cases will not exceed 105105.OutputFor each test case, print a single integer\u00a0\u2014 the minimum number of hops needed.ExampleInputCopy42 41 33 123 4 51 552 1015 4OutputCopy2\n3\n1\n2\nNoteThe first test case of the sample is shown in the picture above. Rabbit can hop to (2,5\u2013\u221a)(2,5), then to (4,0)(4,0) for a total of two hops. Each hop has a distance of 33, which is one of his favorite numbers.In the second test case of the sample, one way for Rabbit to hop 33 times is: (0,0)(0,0) \u2192\u2192 (4,0)(4,0) \u2192\u2192 (8,0)(8,0) \u2192\u2192 (12,0)(12,0).In the third test case of the sample, Rabbit can hop from (0,0)(0,0) to (5,0)(5,0).In the fourth test case of the sample, Rabbit can hop: (0,0)(0,0) \u2192\u2192 (5,102\u2013\u221a)(5,102) \u2192\u2192 (10,0)(10,0).","tags":["geometry","greedy","math"],"code":"#include\nusing namespace std;\nint main(){\n int t;\n cin>>t;\n while(t--){\n int n,m,x,mx=0;\n bool flag=0;\n cin>>n>>x;\n for(int i=1;i<=n;i++){\n cin>>m;\n if(m==x){\n flag=1;\n }\n mx=max(mx,m);\n }\n if(flag){\n cout<<1< ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << \", \" << p.second << ')'; }\n\ntemplate::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = \" \"; return os; }\n\n#define debug(a) cerr << \"(\" << #a << \": \" << a << \")\\n\";\n\ntypedef long long ll;\n\ntypedef vector vi;\n\ntypedef vector vvi;\n\ntypedef pair pi;\n\nconst int mxN = 1e5+1, oo = 1e9;\n\nstruct LCA {\n\n vi par,d,jmp,in,out;\n\n vector> adj;\n\n LCA(int n) : par(n),d(n),jmp(n),in(n),out(n),adj(n) {}\n\n void add(int i) {\n\n int p = par[i];\n\n d[i]=1+d[p];\n\n if(d[p] - d[jmp[p]] == d[jmp[p]] - d[jmp[jmp[p]]]) jmp[i] = jmp[jmp[p]];\n\n else jmp[i] = p;\n\n }\n\n int jump(int a, int k) {\n\n int D = max(0,d[a]-k);\n\n while(d[a]>D) {\n\n if(d[jmp[a]]>=D) a = jmp[a];\n\n else a = par[a];\n\n }\n\n return a;\n\n }\n\n int lca(int a, int b) {\n\n if(d[a]> adj;\n\n vi v;\n\n int n;\n\n int size() {return n;}\n\n virtualtree (const vi& vv, LCA& lca) : v(vv) {\n\n sort(all(v),[&](int i, int j) {return lca.in[i]> n;\n\n LCA lca(n);\n\n for(int i=0;i> x >> y;\n\n --x,--y;\n\n lca.addE(x,y);\n\n }\n\n lca.init();\n\n int q; cin >> q;\n\n vi who(n),ans(n);\n\n while(q--) {\n\n int k,m; cin >> k >> m;\n\n vi s(k);\n\n vi v(k+m);\n\n for(int i=0;i> v[i];\n\n v[i]--;\n\n cin >> s[i];\n\n }\n\n for(int i=k;i> v[i];\n\n --v[i];\n\n }\n\n virtualtree vt(v,lca);\n\n for(int i=0;imake_pair(o.time,o.virusid);\n\n }\n\n };\n\n priority_queue pq;\n\n for(int i=0;i vis(vt.n);\n\n while(!pq.empty()) {\n\n auto e = pq.top(); pq.pop();\n\n if(vis[e.at]) continue;\n\n vis[e.at]=1;\n\n ans[vt.v[e.at]]=e.virusid+1;\n\n for(auto [to,w] : vt.adj[e.at]) {\n\n int need = max(0, (w-e.extra + s[e.virusid]-1)\/s[e.virusid]); \/\/ ceil division\n\n pq.push({e.time+need,to,e.virusid,e.extra+need*s[e.virusid]-w});\n\n }\n\n }\n\n for(int i=k;i\nusing namespace std;\n#define I inline int\n#define V inline void\n#define FOR(i,a,b) for(int i=a;i<=b;i++)\n#define ROF(i,a,b) for(int i=a;i>=b;i--)\n#define REP(u) for(int i=h[u],v;v=e[i].t,i;i=e[i].n)\nconst int N=1e5+1,M=400,mod=1e9+7,bit[]={1,3,7,15,31};\nV check(int&x){x-=mod,x+=x>>31&mod;}\nint n,m,tot,qwq,txt[N],dfn[N];\nint h[N],fa[N],dep[N],dis[N],tag[N];\nint to[M][M],dp[N][M],pre[N],id[1<<15];\nstruct edge{int t,n,w;}e[N<<1];\nstruct bas{\n\tint a[5],flag;\n\tI ins(int x){\n\t\tROF(i,4,0)if(x>>i&1){\n\t\t\tif(a[i])x^=a[i];\n\t\t\telse return a[i]=x,1;\n\t\t}\n\t\treturn flag=1,0;\n\t}\n\tI comp(){return a[0]|a[1]<<1|a[2]<<3|a[3]<<6|a[4]<<10;}\n\tV assign(int x){a[0]=x&1,a[1]=x>>1&3,a[2]=x>>3&7,a[3]=x>>6&15,a[4]=x>>10;}\n\tI hash(){\n\t\tFOR(i,0,4)if(a[i]&&!(a[i]>>i&1))return -1;\n\t\tFOR(i,0,4)FOR(j,i+1,4)if(a[j]>>i&1)a[j]^=a[i];\n\t\treturn comp();\n\t}\n}t[N][2],tmp;\nI merge(int x,int y){\n\tstatic bas tx,ty;tx.assign(x),ty.assign(y);\n\tFOR(i,0,4)if(ty.a[i]&&!tx.ins(ty.a[i]))return 2;\n\treturn tx.hash();\n}\nV add_edge(int x,int y,int w){\n\tif(y-1)e[++tot]=(edge){y,h[x],w},h[x]=tot;\n\tif(x-1)e[++tot]=(edge){x,h[y],w},h[y]=tot;\n}\nI find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}\nV con(int x,int y){if(x-1&&y-1)fa[find(x)]=find(y);}\nV input(){\n\tscanf(\"%d%d\",&n,&m);\n\tFOR(i,1,n)fa[i]=i;int x,y,w;\n\twhile(m--)scanf(\"%d%d%d\",&x,&y,&w),con(x,y),add_edge(x,y,w);\n}\nqueueq;\nV bfs(){\n\tfor(q.push(1);!q.empty();q.pop()){\n\t\tint u=q.front(),p=dfn[find(u)];\n\t\tREP(u)\n\t\t\tif(!dep[v])\n\t\t\t\tdep[v]=dep[u]+1,pre[v]=u,dis[v]=dis[u]^e[i].w,q.push(v);\n\t\t\telse if(u>v&&pre[u]!=v){\n\t\t\t\tif(dep[u]==dep[v]&&dep[u]==1)tag[p]=dis[u]^dis[v]^e[i].w;\n\t\t\t\telse t[p][0].ins(dis[u]^dis[v]^e[i].w);\n\t\t\t}\n\t}\n}\nV init(){\n\tFOR(i,2,n)if(!dfn[find(i)])dfn[find(i)]=++qwq;\n\ttot=0,memset(tag,-1,sizeof(tag)),bfs(),dp[0][1]=1;\n\tint p,x=0;\n\tFOR(i,0,(1<<15)-1){\n\t\ttmp.assign(i),p=tmp.hash();\n\t\tif(~p&&!id[p])txt[id[p]=++tot]=p;\n\t}\n\tFOR(i,1,qwq)if(~tag[i])t[i][1]=t[i][0],t[i][1].ins(tag[i]);\n\tFOR(i,1,tot)FOR(j,1,tot)to[i][j]=id[merge(txt[i],txt[j])];\n}\nV work(){\n\tint p,ans=0;\n\tFOR(i,1,qwq){\n\t\tif(!t[i][0].flag){\n\t\t\tp=id[t[i][0].hash()];\n\t\t\tFOR(j,1,tot)check(dp[i][to[j][p]]+=(dp[i-1][j]<\n\n#pragma GCC optimize(\"Ofast\")\n\n\n\n#define debug(x) cerr << #x << \" \" << x << \"\\n\"\n\n#define debugs(x) cerr << #x << \" \" << x << \" \"\n\n\n\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef pair pii;\n\n\n\nconst ll NMAX = 1002;\n\nconst ll VMAX = 41;\n\nconst ll INF = (1LL << 59);\n\nconst ll MOD = 1000000009;\n\nconst ll BLOCK = 318;\n\nconst ll base = 31;\n\nconst ll nrbits = 21;\n\n\n\nint dist[NMAX][NMAX][41];\n\nint mat[NMAX][NMAX];\n\nvector v[41];\n\n\n\nbool viz[41];\n\nint dx[] = {0, 1, -1, 0};\n\nint dy[] = {1, 0, 0, -1};\n\nint n, m;\n\n\n\n\n\nbool OK(int i, int j) {\n\n if(i > 0 && i <= n && j > 0 && j <= m)\n\n return 1;\n\n return 0;\n\n}\n\n\n\nint main() {\n\n#ifdef HOME\n\n ifstream cin(\".in\");\n\n ofstream cout(\".out\");\n\n#endif \/\/ HOME\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(0);\n\n cout.tie(0);\n\n int k, i, j;\n\n cin >> n >> m >> k;\n\n for(i = 1; i <= n; i++) {\n\n for(j = 1; j <= m; j++) {\n\n cin >> mat[i][j];\n\n v[mat[i][j]].push_back({i, j});\n\n }\n\n }\n\n for(int col = 1; col <= k; col++) {\n\n for(i = 1; i <= n + 1; i++) {\n\n for(j = 1; j <= max(m, k); j++) {\n\n dist[i][j][col] = 32767;\n\n }\n\n }\n\n for(i = 1; i <= k; i++) viz[i] = 0;\n\n queue q;\n\n for(auto x : v[col]) {\n\n dist[x.first][x.second][col] = 0;\n\n q.push({x.first, x.second});\n\n }\n\n while(q.size()) {\n\n pii x = q.front();\n\n q.pop();\n\n for(int d = 0; d < 4; d++) {\n\n pii care = {x.first + dx[d], x.second + dy[d]};\n\n if(!OK(care.first, care.second)) continue;\n\n if(dist[care.first][care.second][col] == 32767) { \/\/\/ Hmm...\n\n dist[care.first][care.second][col] = dist[x.first][x.second][col] + 1;\n\n q.push({care.first, care.second});\n\n }\n\n }\n\n if(!viz[mat[x.first][x.second]]) {\n\n viz[mat[x.first][x.second]] = 1;\n\n for(auto y : v[mat[x.first][x.second]]) {\n\n pii care = y;\n\n if(dist[care.first][care.second][col] == 32767) { \/\/\/ Hmm...\n\n dist[care.first][care.second][col] = dist[x.first][x.second][col] + 1;\n\n q.push({care.first, care.second});\n\n }\n\n }\n\n }\n\n }\n\n }\n\n int q;\n\n cin >> q;\n\n while(q--) {\n\n int a, b, c, d;\n\n cin >> a >> b >> c >> d;\n\n int minim = abs(c - a) + abs(d - b);\n\n for(int col = 1; col <= k; col++) {\n\n minim = min(minim, (int)dist[a][b][col] + (int)dist[c][d][col] + 1);\n\n }\n\n cout << minim << \"\\n\";\n\n }\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1285","problem_id":"D","statement":"D. Dr. Evil Underscorestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; as a friendship gift, Bakry gave Badawy nn integers a1,a2,\u2026,ana1,a2,\u2026,an and challenged him to choose an integer XX such that the value max1\u2264i\u2264n(ai\u2295X)max1\u2264i\u2264n(ai\u2295X) is minimum possible, where \u2295\u2295 denotes the bitwise XOR operation.As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of max1\u2264i\u2264n(ai\u2295X)max1\u2264i\u2264n(ai\u2295X).InputThe first line contains integer nn (1\u2264n\u22641051\u2264n\u2264105).The second line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u2264230\u221210\u2264ai\u2264230\u22121).OutputPrint one integer \u2014 the minimum possible value of max1\u2264i\u2264n(ai\u2295X)max1\u2264i\u2264n(ai\u2295X).ExamplesInputCopy3\n1 2 3\nOutputCopy2\nInputCopy2\n1 5\nOutputCopy4\nNoteIn the first sample; we can choose X=3X=3.In the second sample, we can choose X=5X=5.","tags":["bitmasks","brute force","dfs and similar","divide and conquer","dp","greedy","strings","trees"],"code":"#include\n\ntypedef long long ll;\n\ntypedef unsigned long long ull;\n\nusing namespace std;\n\nconst ll mod = 998244353;\n\nconst int mm = 1e5 + 10;\n\nvectorb;\n\nll dfs(int x,vectorv){\n\n\tif(v.size()==0||x<0)return 0;\n\n\tvectorl,r;\n\n\tfor(auto i:v){\n\n\t\tif((1<>n;\n\n\tfor(int i=1;i<=n;i++){\n\n\t\tll x;\n\n\t\tcin>>x;\n\n\t\tb.push_back(x);\n\n\t}\n\n\tcout<\nint main()\n{\nlong long int n,m,ans=0;scanf(\"%lld%lld\",&n,&m);\nlong long int store[n+2]; store[1] = 1;\nfor(int i=2;i<=n+1;i++)store[i]=(store[i-1]*i)%m;\nfor(int i=1;i<=n;i++)ans+=(n-i+1)*(store[i]*store[n-i+1]%m),ans%=m;\nprintf(\"%lld\\n\",ans);\n}","language":"cpp"} -{"contest_id":"1141","problem_id":"D","statement":"D. Colored Bootstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn left boots and nn right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings ll and rr, both of length nn. The character lili stands for the color of the ii-th left boot and the character riri stands for the color of the ii-th right boot.A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair.InputThe first line contains nn (1\u2264n\u22641500001\u2264n\u2264150000), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots).The second line contains the string ll of length nn. It contains only lowercase Latin letters or question marks. The ii-th character stands for the color of the ii-th left boot.The third line contains the string rr of length nn. It contains only lowercase Latin letters or question marks. The ii-th character stands for the color of the ii-th right boot.OutputPrint kk \u2014 the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors.The following kk lines should contain pairs aj,bjaj,bj (1\u2264aj,bj\u2264n1\u2264aj,bj\u2264n). The jj-th of these lines should contain the index ajaj of the left boot in the jj-th pair and index bjbj of the right boot in the jj-th pair. All the numbers ajaj should be distinct (unique), all the numbers bjbj should be distinct (unique).If there are many optimal answers, print any of them.ExamplesInputCopy10\ncodeforces\ndodivthree\nOutputCopy5\n7 8\n4 9\n2 2\n9 10\n3 1\nInputCopy7\nabaca?b\nzabbbcc\nOutputCopy5\n6 5\n2 3\n4 6\n7 4\n1 2\nInputCopy9\nbambarbia\nhellocode\nOutputCopy0\nInputCopy10\ncode??????\n??????test\nOutputCopy10\n6 2\n1 6\n7 3\n3 5\n4 8\n9 7\n5 1\n2 4\n10 9\n8 10\n","tags":["greedy","implementation"],"code":"#include \n\n#define int long long\n\n\n\nusing namespace std;\n\n\n\nint32_t main()\n\n{\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(NULL);\n\n vector> a(26);\n\n vector> b(26);\n\n vector qa;\n\n vectorqb;\n\n int n;\n\n string x, y;\n\n cin >> n >> x >> y;\n\n for (int i = 0; i < n; i++) {\n\n if (x[i] == '?') qa.push_back(i + 1);\n\n else a[x[i] - 'a'].push_back(i + 1);\n\n if (y[i] == '?') qb.push_back(i + 1);\n\n else b[y[i] - 'a'].push_back(i + 1);\n\n }\n\n vector> ans;\n\n vector sa;\n\n vector sb;\n\n int z;\n\n for (int i = 0; i < 26; i++) {\n\n z = min((int)a[i].size(), (int)b[i].size());\n\n for (int j = 0; j < z; j++) {\n\n ans.push_back({a[i].back(), b[i].back()});\n\n a[i].pop_back();\n\n b[i].pop_back();\n\n }\n\n for (auto z : a[i]) sa.push_back(z);\n\n for (auto z : b[i]) sb.push_back(z);\n\n }\n\n z = min((int)sa.size(), (int)qb.size());\n\n for (int i = 0; i < z; i++) {\n\n ans.push_back({sa.back(), qb.back()});\n\n sa.pop_back();\n\n qb.pop_back();\n\n }\n\n z = min((int)qa.size(), (int)sb.size());\n\n for (int i = 0; i < z; i++) {\n\n ans.push_back({qa.back(), sb.back()});\n\n qa.pop_back();\n\n sb.pop_back();\n\n }\n\n z = min((int)qa.size(), (int)qb.size());\n\n for (int i = 0; i < z; i++) {\n\n ans.push_back({qa.back(), qb.back()});\n\n qa.pop_back();\n\n qb.pop_back();\n\n }\n\n cout << ans.size() << '\\n';\n\n for (auto [l, r] : ans) {\n\n cout << l << \" \" << r << '\\n';\n\n }\n\n}","language":"cpp"} -{"contest_id":"1316","problem_id":"F","statement":"F. Battalion Strengthtime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn officers in the Army of Byteland. Each officer has some power associated with him. The power of the ii-th officer is denoted by pipi. As the war is fast approaching, the General would like to know the strength of the army.The strength of an army is calculated in a strange way in Byteland. The General selects a random subset of officers from these nn officers and calls this subset a battalion.(All 2n2n subsets of the nn officers can be chosen equally likely, including empty subset and the subset of all officers).The strength of a battalion is calculated in the following way:Let the powers of the chosen officers be a1,a2,\u2026,aka1,a2,\u2026,ak, where a1\u2264a2\u2264\u22ef\u2264aka1\u2264a2\u2264\u22ef\u2264ak. The strength of this battalion is equal to a1a2+a2a3+\u22ef+ak\u22121aka1a2+a2a3+\u22ef+ak\u22121ak. (If the size of Battalion is \u22641\u22641, then the strength of this battalion is 00).The strength of the army is equal to the expected value of the strength of the battalion.As the war is really long, the powers of officers may change. Precisely, there will be qq changes. Each one of the form ii xx indicating that pipi is changed to xx.You need to find the strength of the army initially and after each of these qq updates.Note that the changes are permanent.The strength should be found by modulo 109+7109+7. Formally, let M=109+7M=109+7. It can be shown that the answer can be expressed as an irreducible fraction p\/qp\/q, where pp and qq are integers and q\u2261\u03380modMq\u22620modM). Output the integer equal to p\u22c5q\u22121modMp\u22c5q\u22121modM. In other words, output such an integer xx that 0\u2264x\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#define ll long long\n\nusing namespace std;\n\n#define I inline ll\n\n#define her1 20090115\n\n#define IV inline void\n\n#define cht 1000000007\n\n#define ld long double\n\n#define Aestas16 392699\n\n#define ull unsigned long long\n\n#define mem(x,val)memset(x,val,sizeof x)\n\n#define D(i,j,n)for(register int i=j;i>=n;i--)\n\n#define E(i,o)for(register int i=first[o];i;i=e[i].nxt)\n\n#define F(i,j,n)for(register int i=j;i<=n;i++)\n\n#define DL(i,j,n)for(register ll i=j;i>=n;i--)\n\n#define EL(i,o)for(register ll i=first[o];i;i=e[i].nxt)\n\n#define FL(i,j,n)for(register ll i=j;i<=n;i++)\n\n\/\/#define D(i,j,n)for(int i=j;i>=n;i--)\n\n\/\/#define E(i,o)for(int i=first[o];i;i=e[i].nxt)\n\n\/\/#define F(i,j,n)for(int i=j;i<=n;i++)\n\n\/\/#define DL(i,j,n)for(ll i=j;i>=n;i--)\n\n\/\/#define EL(i,o)for(ll i=first[o];i;i=e[i].nxt)\n\n\/\/#define FL(i,j,n)for(ll i=j;i<=n;i++)\n\nll read(){\n\n\tll ans=0,f=1;\n\n\tchar c=getchar();\n\n\twhile(c<'0'||c>'9'){\n\n\t\tif(c=='-')f=-1;\n\n\t\tc=getchar();\n\n\t}\n\n\twhile(c>='0'&&c<='9')ans=ans*10+c-'0',c=getchar();\n\n\treturn ans*f;\n\n}\n\n#define ls (o<<1)\n\n#define rs (o<<1|1)\n\nmt19937 rnd(her1);\n\nconst int N = 6e5+5;\n\nconst int maxn = 1e6+5;\n\nstruct tree{ll l,r,tot,val,val1,val2;}t[N*4];\n\nstruct item{ll val,*s;}b[maxn];ll cnt,tot,pw[N],ip[N],n,q,a[N],p[N],x[N];\n\nIV build(ll o,ll l,ll r){\n\n\tt[o].l=l;t[o].r=r;t[o].tot=t[o].val=t[o].val1=t[o].val2=0;\n\n\tif(l==r)return;ll mid=l+r>>1;build(ls,l,mid);build(rs,mid+1,r);\n\n}\n\nIV pushup(ll o){\n\n\tt[o].tot=t[ls].tot+t[rs].tot;\n\n\tt[o].val1=(t[ls].val1+t[rs].val1*pw[t[ls].tot])%cht;\n\n\tt[o].val2=(t[ls].val2+t[rs].val2*ip[t[ls].tot])%cht;\n\n\tt[o].val=(t[ls].val+t[rs].val+t[ls].val1*t[rs].val2%cht*ip[t[ls].tot])%cht;\n\n}\n\nIV upd(ll o,ll p,ll ty){\n\n\tif(t[o].l==t[o].r){\n\n\t\tif(~ty){\n\n\t\t\tt[o].tot=1;t[o].val1=b[p].val;\n\n\t\t\tt[o].val2=b[p].val*ip[1]%cht;t[o].val=0;\n\n\t\t}\n\n\t\telse t[o].tot=t[o].val1=t[o].val2=t[o].val=0;return;\n\n\t}\n\n\tll mid=t[o].l+t[o].r>>1;\n\n\tp<=mid?upd(ls,p,ty):upd(rs,p,ty);pushup(o);\n\n}\n\nint main(){\n\n\tn=read();F(i,1,n)b[++cnt].val=a[i]=read(),b[cnt].s=a+i;\n\n\tq=read();F(i,1,q)p[i]=read(),b[++cnt].val=x[i]=read(),b[cnt].s=x+i;\n\n\tsort(b+1,b+1+cnt,[](item u,item v){return u.val>1;F(i,2,n)ip[i]=ip[i-1]*ip[1]%cht;\n\n\tbuild(1,1,tot);F(i,1,n)upd(1,a[i],1);printf(\"%lld\\n\",t[1].val);\n\n\tF(i,1,q)upd(1,a[p[i]],-1),a[p[i]]=x[i],upd(1,a[p[i]],1),printf(\"%lld\\n\",t[1].val);return 0;\n\n}","language":"cpp"} -{"contest_id":"1285","problem_id":"F","statement":"F. Classical?time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array aa, consisting of nn integers, find:max1\u2264i\n\nusing namespace std;\n\n\n\n#define ll long long\n\n#define fi first\n\n#define se second\n\n#define Pii pair \n\n\n\nint n, a[100005], cnt[100005];\n\nvector fac[100005];\n\nbool vis[100005];\n\nint prime[100005], len = 0, mu[100005];\n\nint suc[100005];\n\nll ans = 0;\n\n\n\nvoid MySolve() {\n\n memset(vis, true, sizeof(vis));\n\n vis[1] = false, mu[1] = 1;\n\n for (int i = 2; i <= 1e5; ++i) {\n\n if (vis[i]) prime[ ++len ] = i, mu[i] = -1;\n\n for (int j = 1; j <= len && i * prime[j] <= 1e5; ++j) {\n\n vis[ i * prime[j] ] = false;\n\n if (i % prime[j] == 0) {\n\n mu[ i * prime[j] ] = 0;\n\n break;\n\n }\n\n mu[ i * prime[j] ] = -mu[i];\n\n }\n\n }\n\n cin >> n;\n\n for (int i = 1; i <= n; ++i) {\n\n cin >> a[i];\n\n ++cnt[ a[i] ];\n\n }\n\n for (int i = 1; i <= 1e5; ++i) {\n\n if (cnt[i] >= 2) {\n\n ans = max(ans, 1ll * i);\n\n }\n\n }\n\n for (int i = 1; i <= 1e5; ++i) {\n\n int V = 1e5 \/ i;\n\n for (int j = 1; j <= V; ++j) {\n\n fac[j].clear();\n\n suc[j] = 0;\n\n }\n\n for (int j = 1; j <= V; ++j) {\n\n for (int k = j; k <= V; k += j) {\n\n fac[k].push_back(j);\n\n }\n\n }\n\n int p = 1;\n\n for (int j = 1; j <= V; ++j) {\n\n for (auto d : fac[j]) {\n\n suc[d] += mu[d] * cnt[ i * j ];\n\n }\n\n }\n\n for (int j = V; j; --j) if (cnt[ i * j ]) {\n\n auto calc = [&] () {\n\n int sum = 0;\n\n for (auto d : fac[j]) {\n\n sum += suc[d];\n\n }\n\n return sum;\n\n };\n\n while (p <= j && calc()) {\n\n for (auto d : fac[p]) {\n\n suc[d] -= mu[d] * cnt[ i * p ];\n\n }\n\n ++p;\n\n }\n\n if (p > j) break;\n\n ans = max(ans, 1ll * i * (p - 1) * j);\n\n }\n\n }\n\n cout << ans << '\\n';\n\n}\n\n\n\nint main() {\n\n ios :: sync_with_stdio(0), cin.tie(0);\n\n int t = 1;\n\n while (t--) {\n\n MySolve();\n\n }\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1290","problem_id":"F","statement":"F. Making Shapestime limit per test5 secondsmemory limit per test768 megabytesinputstandard inputoutputstandard outputYou are given nn pairwise non-collinear two-dimensional vectors. You can make shapes in the two-dimensional plane with these vectors in the following fashion: Start at the origin (0,0)(0,0). Choose a vector and add the segment of the vector to the current point. For example, if your current point is at (x,y)(x,y) and you choose the vector (u,v)(u,v), draw a segment from your current point to the point at (x+u,y+v)(x+u,y+v) and set your current point to (x+u,y+v)(x+u,y+v). Repeat step 2 until you reach the origin again.You can reuse a vector as many times as you want.Count the number of different, non-degenerate (with an area greater than 00) and convex shapes made from applying the steps, such that the shape can be contained within a m\u00d7mm\u00d7m square, and the vectors building the shape are in counter-clockwise fashion. Since this number can be too large, you should calculate it by modulo 998244353998244353.Two shapes are considered the same if there exists some parallel translation of the first shape to another.A shape can be contained within a m\u00d7mm\u00d7m square if there exists some parallel translation of this shape so that every point (u,v)(u,v) inside or on the border of the shape satisfies 0\u2264u,v\u2264m0\u2264u,v\u2264m.InputThe first line contains two integers nn and mm \u00a0\u2014 the number of vectors and the size of the square (1\u2264n\u226451\u2264n\u22645, 1\u2264m\u22641091\u2264m\u2264109).Each of the next nn lines contains two integers xixi and yiyi \u00a0\u2014 the xx-coordinate and yy-coordinate of the ii-th vector (|xi|,|yi|\u22644|xi|,|yi|\u22644, (xi,yi)\u2260(0,0)(xi,yi)\u2260(0,0)).It is guaranteed, that no two vectors are parallel, so for any two indices ii and jj such that 1\u2264i\n\n#define ll long long\n\n#define fir first\n\n#define sec second\n\n#define pii pair\n\nusing namespace std;\n\n\n\nconst int maxn=6;\n\nconst int inf=0x3f3f3f3f;\n\nconst int mod=998244353;\n\n\n\nstruct mint {\n\n\tint val;\n\n\tmint():val(0) {}\n\n\tmint(ll tval) {\n\n\t\tif(-mod<=tval&&tval<2*mod) {\n\n\t\t\tif(tval>=mod) {\n\n\t\t\t\ttval-=mod;\n\n\t\t\t} \n\n\t\t\tif(tval<0) {\n\n\t\t\t\ttval+=mod;\n\n\t\t\t}\n\n\t\t} else {\n\n\t\t\ttval%=mod;\n\n\t\t\tif(tval<0) {\n\n\t\t\t\ttval+=mod;\n\n\t\t\t}\t\t\t\n\n\t\t}\n\n\t\tval=tval;\n\n\t}\n\n\tmint& operator += (const mint &b) {\n\n\t\tval=val+b.val>=mod?val+b.val-mod:val+b.val;\n\n\t\treturn *this;\n\n\t}\n\n\tmint& operator -= (const mint &b) {\n\n\t\tval=val-b.val<0?val-b.val+mod:val-b.val;\n\n\t\treturn *this;\n\n\t}\n\n\tmint& operator *= (const mint &b) {\n\n\t\tval=1ll*val*b.val>=mod?1ll*val*b.val%mod:val*b.val;\n\n\t\treturn *this;\n\n\t}\n\n\tmint& operator \/= (const mint &b) {\n\n\t\t*this*=b.inv();\n\n\t\treturn *this;\n\n\t}\n\n\tfriend mint operator + (const mint &a,const mint &b) {\n\n\t\tmint ans=a;\n\n\t\tans+=b;\n\n\t\treturn ans;\n\n\t}\n\n\tfriend mint operator - (const mint &a,const mint &b) {\n\n\t\tmint ans=a;\n\n\t\tans-=b;\n\n\t\treturn ans;\n\n\t}\n\n\tfriend mint operator * (const mint &a,const mint &b) {\n\n\t\tmint ans=a;\n\n\t\tans*=b;\n\n\t\treturn ans;\n\n\t}\n\n\tfriend mint operator \/ (const mint &a,const mint &b) {\n\n\t\tmint ans=a;\n\n\t\tans\/=b;\n\n\t\treturn ans;\n\n\t}\n\n\tfriend bool operator < (const mint &a,const mint &b) {\n\n\t\treturn a.val> (istream &is,mint &x) {\n\n\t\tll val;\n\n\t\tcin>>val;\n\n\t\tx.val=val%mod; \n\n\t\treturn is;\n\n\t}\n\n\tfriend ostream& operator << (ostream &os,const mint &x) {\n\n\t\tos<>=1;\n\n\t\t}\n\n\t\treturn ans;\n\n\t}\n\n\tmint inv() const {\n\n\t\t\/\/ mod must be a prime\n\n\t\treturn qpow(mod-2);\n\n\t}\n\n\tmint sqrt() {\n\n\t\tmint a=*this; \n\n\t\tauto check=[&](mint x) {\n\n\t\t\treturn x.qpow((mod-1)\/2)==1;\n\n\t\t};\n\n\t\tstatic mt19937 rnd(73385);\n\n\t\tmint b=rnd()%mod;\n\n\t\twhile(check(b*b-a)) {\n\n\t\t\tb=rnd()%mod;\n\n\t\t}\n\n\t\tstatic mint val=b*b-a;\n\n\t\tstruct Complex {\n\n\t\t\tmint real,imag;\n\n\t\t\tComplex(mint treal=0,mint timag=0):real(treal),imag(timag) {}\n\n\t\t\tComplex operator * (const Complex &rhs) {\n\n\t\t\t\treturn {real*rhs.real+imag*rhs.imag*val,real*rhs.imag+imag*rhs.real};\n\n\t\t\t}\n\n\t\t\tComplex& operator *= (const Complex &rhs) {\n\n\t\t\t\treturn *this=*this*rhs;\n\n\t\t\t}\n\n\t\t};\n\n\t\tauto qpow=[&](Complex x,int y) {\n\n\t\t\tComplex ans={1};\n\n\t\t\twhile(y) {\n\n\t\t\t\tif(y&1) {\n\n\t\t\t\t\tans*=x;\n\n\t\t\t\t}\n\n\t\t\t\tx*=x;\n\n\t\t\t\ty>>=1;\n\n\t\t\t}\n\n\t\t\treturn ans;\n\n\t\t};\n\n\t\tmint ans=qpow({b,1},(mod+1)\/2).real;\n\n\t\treturn min(ans,mod-ans);\n\n\t}\n\n};\n\n\n\nint n,m;\n\nint dx[maxn];\n\nint dy[maxn];\n\nbool vis[35][25][25][25][25][2][2];\n\nmint f[35][25][25][25][25][2][2];\n\n\n\n#define state p][x][nx][y][ny][fx][fy\n\nmint dfs(int p,int x,int nx,int y,int ny,bool fx,bool fy) {\n\n\tif(vis[state]) {\n\n\t\treturn f[state];\n\n\t}\n\n\tif(p==30) {\n\n\t\treturn !x&&!nx&&!y&&!ny&&fx&&fy;\n\n\t}\n\n\tauto ck=[&](bool f,int x,int m) {\n\n\t\treturn x!=m?x>(i-1))&1) {\n\n\t\t\t\t\tif(dx[i]>=0) {\n\n\t\t\t\t\t\tx+=dx[i]*v;\n\n\t\t\t\t\t} else {\n\n\t\t\t\t\t\tnx-=dx[i]*v;\n\n\t\t\t\t\t}\n\n\t\t\t\t\tif(dy[i]>=0) {\n\n\t\t\t\t\t\ty+=dy[i]*v;\n\n\t\t\t\t\t} else {\n\n\t\t\t\t\t\tny-=dy[i]*v;\n\n\t\t\t\t\t}\n\n\t\t\t\t}\n\n\t\t\t}\/\/\n\n\t\t};\n\n\t\ttrans(1);\n\n\t\tif((x&1)==(nx&1)&&(y&1)==(ny&1)) {\n\n\t\t\tans+=dfs(p+1,x>>1,nx>>1,y>>1,ny>>1,ck(fx,(x&1),(m>>p)&1),ck(fy,(y&1),(m>>p)&1));\n\n\t\t}\n\n\t\ttrans(-1);\n\n\t}\n\n\/\/\tcerr<>n>>m;\n\n\tm++;\n\n\tfor(int i=1;i<=n;i++) {\n\n\t\tcin>>dx[i]>>dy[i];\n\n\t}\n\n\tcout<\n\nusing namespace std;\n\n\n\n#define int int64_t\n\n#define double long double\n\nusing pii = pair;\n\ntemplate using Prior = std::priority_queue;\n\ntemplate using prior = std::priority_queue, greater>;\n\n\n\n\/\/ #define X first\n\n\/\/ #define Y second\n\n#define eb emplace_back\n\n#define ef emplace_front\n\n#define ee emplace\n\n#define pb pop_back\n\n#define pf pop_front\n\n#define ALL(x) begin(x), end(x)\n\n#define RALL(x) rbegin(x), rend(x)\n\n#define SZ(x) ((int)(x).size())\n\n\n\ntemplate struct Vec : vector> {\n\n static_assert(D >= 1, \"Vector dimension must be greater than zero!\");\n\n template Vec(int n = 0, Args... args) : vector>(n, Vec(args...)) {}\n\n};\n\n\n\ntemplate struct Vec<1, T> : vector {\n\n Vec(int n = 0, const T& val = T()) : vector(n, val) {}\n\n};\n\n\n\n#ifdef local\n\n#define fastIO() void()\n\n#define debug(...) \\\n\n _color.eb(\"\\u001b[31m\"), \\\n\n fprintf(stderr, \"%sAt [%s], line %d: (%s) = \", _color.back().c_str(), __FUNCTION__, __LINE__, #__VA_ARGS__), \\\n\n _do(__VA_ARGS__), _color.pop_back(), \\\n\n fprintf(stderr, \"%s\", _color.back().c_str())\n\ndeque _color{\"\\u001b[0m\"};\n\n\n\ntemplate concept is_string = is_same_v or is_same_v;\n\ntemplate concept is_iterable = requires (T _t) {begin(_t);};\n\n\n\ntemplate inline void _print_err(T &&_t);\n\ntemplate inline void _print_err(T &&_t) requires is_iterable and (not is_string);\n\ntemplate inline typename enable_if::type _print_err(const tuple &);\n\ntemplate inline typename enable_if::type _print_err(const tuple &_t);\n\ntemplate inline typename enable_if::type _print_err(tuple &);\n\ntemplate inline typename enable_if::type _print_err(tuple &_t);\n\ntemplate ostream& operator << (ostream &os, const pair &_tu);\n\n\n\ninline void _do() {cerr << \"\\n\";};\n\ntemplate inline void _do(T &&_t) {_print_err(_t), cerr << \"\\n\";}\n\ntemplate inline void _do(T &&_t, U &&..._u) {_print_err(_t), cerr << \", \", _do(_u...);}\n\n#else\n\n#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)\n\n#define debug(...) void()\n\n#endif\n\n\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n\n\ninline int getRand(int L, int R) {\n\n if (L > R) swap(L, R);\n\n return (int)(rng() % ((uint64_t)R - L + 1) + L);\n\n}\n\n\n\ntemplate bool chmin(T &lhs, U rhs) {return lhs > rhs ? lhs = rhs, 1 : 0;}\n\ntemplate bool chmax(T &lhs, U rhs) {return lhs < rhs ? lhs = rhs, 1 : 0;}\n\n\n\nvoid solve() {\n\n int N, X, Y; cin >> N >> X >> Y;\n\n \n\n int min_place = min(max(X+Y-N, (int)0) + 1, N);\n\n int max_place = min({X-1, Y-1, N-X, N-Y}) + abs(X-Y) + min(X, Y);\n\n \n\n cout << min_place << \" \" << max_place << \"\\n\";\n\n}\n\n\n\nint32_t main() {\n\n fastIO();\n\n \n\n int t = 1; cin >> t;\n\n for (int _ = 1; _ <= t; ++_) {\n\n \/\/ cout << \"Case #\" << _ << \": \";\n\n solve();\n\n }\n\n \n\n return 0;\n\n}\n\n\n\n\/\/\/ below are Fast I\/O and _print_err templates \/\/\/\n\n\n\n\/*\n\n\/\/\/ Fast I\/O by FHVirus \/\/\/\n\n\/\/\/ https:\/\/fhvirus.github.io\/blog\/2020\/fhvirus-io\/ \/\/\/\n\n\n\n#include \n\n\n\nconst int S = 65536;\n\n\n\nint OP = 0;\n\nchar OB[S];\n\n\n\ninline char RC() {\n\n static char buf[S], *p = buf, *q = buf;\n\n return p == q and (q = (p = buf) + read(0, buf, S)) == buf ? -1 : *p++;\n\n}\n\n\n\ninline int RI() {\n\n static char c;\n\n int a;\n\n while (((c = RC()) < '0' or c > '9') and c != '-' and c != -1);\n\n if (c == '-') {\n\n a = 0;\n\n while ((c = RC()) >= '0' and c <= '9') a *= 10, a -= c ^ '0';\n\n }\n\n else {\n\n a = c ^ '0';\n\n while ((c = RC()) >= '0' and c <= '9') a *= 10, a += c ^ '0';\n\n }\n\n return a;\n\n}\n\n\n\ninline void WI(int n, char c = '\\n') {\n\n static char buf[20], p;\n\n if (n == 0) OB[OP++] = '0';\n\n p = 0;\n\n if (n < 0) {\n\n OB[OP++] = '-';\n\n while (n) buf[p++] = '0' - (n % 10), n \/= 10;\n\n }\n\n else {\n\n while (n) buf[p++] = '0' + (n % 10), n \/= 10;\n\n }\n\n for (--p; p >= 0; --p) OB[OP++] = buf[p];\n\n OB[OP++] = c;\n\n if (OP > S-20) write(1, OB, OP), OP = 0;\n\n}\n\n\n\n\/\/\/ Fast I\/O by FHVirus \/\/\/\n\n\/\/\/ https:\/\/fhvirus.github.io\/blog\/2020\/fhvirus-io\/ \/\/\/\n\n*\/\n\n\n\n#ifdef local\n\n\n\ntemplate inline void _print_err(T &&_t) {cerr << _t;}\n\n\n\ntemplate inline void _print_err(T &&_t) requires is_iterable and (not is_string) {\n\n string _tmp_color = _color.back();\n\n ++_tmp_color[3], _color.eb(_tmp_color);\n\n cerr << _color.back() << \"[\";\n\n for (bool _first = true; auto &_x : _t) {\n\n if (!_first) cerr << \", \";\n\n _print_err(_x), _first = false;\n\n }\n\n cerr << \"]\" << (_color.pb(), _color.back());\n\n}\n\n\n\ntemplate ostream& operator << (ostream &os, const pair &_tu) {\n\n string _tmp_color = _color.back();\n\n ++_tmp_color[3], _color.eb(_tmp_color);\n\n cerr << _color.back() << \"(\";\n\n _print_err(_tu.first), cerr << \", \", _print_err(_tu.second);\n\n cerr << \")\" << (_color.pb(), _color.back());\n\n return os;\n\n}\n\n\n\ntemplate inline typename enable_if::type _print_err(const tuple &) {\n\n cerr << \")\" << (_color.pb(), _color.back());\n\n}\n\n\n\ntemplate inline typename enable_if::type _print_err(const tuple &_t) {\n\n if (!I) {\n\n string _tmp_color = _color.back();\n\n ++_tmp_color[3], _color.eb(_tmp_color);\n\n cerr << _color.back();\n\n }\n\n cerr << (I ? \", \" : \"(\"), _print_err(get(_t)), _print_err(_t);\n\n}\n\n\n\ntemplate inline typename enable_if::type _print_err(tuple &) {\n\n cerr << \")\" << (_color.pb(), _color.back());\n\n}\n\n\n\ntemplate inline typename enable_if::type _print_err(tuple &_t) {\n\n if (!I) {\n\n string _tmp_color = _color.back();\n\n ++_tmp_color[3], _color.eb(_tmp_color);\n\n cerr << _color.back();\n\n }\n\n cerr << (I ? \", \" : \"(\"), _print_err(get(_t)), _print_err(_t);\n\n}\n\n\n\n#endif\n\n","language":"cpp"} -{"contest_id":"1304","problem_id":"F1","statement":"F1. Animal Observation (easy version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe only difference between easy and hard versions is the constraint on kk.Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The color of one camera is red, and the other one's color is blue.Gildong is going to take videos for nn days, starting from day 11 to day nn. The forest can be divided into mm areas, numbered from 11 to mm. He'll use the cameras in the following way: On every odd day (11-st, 33-rd, 55-th, ...), bring the red camera to the forest and record a video for 22 days. On every even day (22-nd, 44-th, 66-th, ...), bring the blue camera to the forest and record a video for 22 days. If he starts recording on the nn-th day with one of the cameras, the camera records for only one day. Each camera can observe kk consecutive areas of the forest. For example, if m=5m=5 and k=3k=3, he can put a camera to observe one of these three ranges of areas for two days: [1,3][1,3], [2,4][2,4], and [3,5][3,5].Gildong got information about how many animals will be seen in each area each day. Since he would like to observe as many animals as possible, he wants you to find the best way to place the two cameras for nn days. Note that if the two cameras are observing the same area on the same day, the animals observed in that area are counted only once.InputThe first line contains three integers nn, mm, and kk (1\u2264n\u2264501\u2264n\u226450, 1\u2264m\u22642\u22c51041\u2264m\u22642\u22c5104, 1\u2264k\u2264min(m,20)1\u2264k\u2264min(m,20)) \u2013 the number of days Gildong is going to record, the number of areas of the forest, and the range of the cameras, respectively.Next nn lines contain mm integers each. The jj-th integer in the i+1i+1-st line is the number of animals that can be seen on the ii-th day in the jj-th area. Each number of animals is between 00 and 10001000, inclusive.OutputPrint one integer \u2013 the maximum number of animals that can be observed.ExamplesInputCopy4 5 2\n0 2 1 1 0\n0 0 3 1 2\n1 0 4 3 1\n3 3 0 0 4\nOutputCopy25\nInputCopy3 3 1\n1 2 3\n4 5 6\n7 8 9\nOutputCopy31\nInputCopy3 3 2\n1 2 3\n4 5 6\n7 8 9\nOutputCopy44\nInputCopy3 3 3\n1 2 3\n4 5 6\n7 8 9\nOutputCopy45\nNoteThe optimal way to observe animals in the four examples are as follows:Example 1: Example 2: Example 3: Example 4: ","tags":["data structures","dp"],"code":"\/\/ Judges with GCC >= 12 only needs Ofast\n\n\/\/ #pragma GCC optimize(\"O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize\")\n\n\/\/ MLE optimization\n\n\/\/ #pragma GCC optimize(\"conserve-stack\")\n\n\/\/ Old judges\n\n\/\/ #pragma GCC target(\"sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2\")\n\n\/\/ New judges. Test with assert(__builtin_cpu_supports(\"avx2\"));\n\n\/\/ #pragma GCC target(\"avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native\")\n\n\/\/ Atcoder\n\n\/\/ #pragma GCC target(\"avx2,popcnt,lzcnt,abm,bmi,bmi2,fma\")\n\n#include\n\nusing namespace std;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nuniform_real_distribution<> pp(0.0,1.0);\n\n#define ld long double\n\n#define pii pair\n\n#define piii pair\n\n#define fi first\n\n#define se second\n\nconst int inf=1e9;\n\nconst int mod=998244353;\n\nconst int mod2=1e9+7;\n\nconst int maxn=55;\n\nconst int maxm=20005;\n\nconst int maxq=500005;\n\nconst int maxl=20;\n\nconst int maxa=1000005;\n\nint power(int a,int n){\n\n int res=1;\n\n while(n){\n\n if(n&1) res=res*a%mod;\n\n a=a*a%mod;n>>=1;\n\n }\n\n return res;\n\n}\n\nstruct node{\n\n int Max[3];\n\n node(){Max[0]=Max[1]=Max[2]=inf;}\n\n friend node operator+(node a,node b){\n\n node res;\n\n for(int i=0;i<3;i++) res.Max[i]=max(a.Max[i],b.Max[i]);\n\n return res;\n\n }\n\n};\n\nint n,m,k,a[maxn][maxm],pre[maxm],suf[maxm],dp[maxm],cur[maxm];\n\nnamespace Segtree{\n\n node tree[4*maxm];\n\n void build(int l,int r,int id){\n\n if(l==r){\n\n tree[id].Max[0]=dp[l];\n\n tree[id].Max[1]=dp[l]+suf[l+k];\n\n tree[id].Max[2]=dp[l]+pre[l-1];\n\n return;\n\n }\n\n int mid=(l+r)>>1;\n\n build(l,mid,id<<1);build(mid+1,r,id<<1|1);\n\n tree[id]=tree[id<<1]+tree[id<<1|1];\n\n }\n\n int query(int l,int r,int id,int tl,int tr,int t){\n\n if(r>1;\n\n return max(query(l,mid,id<<1,tl,tr,t),query(mid+1,r,id<<1|1,tl,tr,t));\n\n }\n\n}\n\nvoid solve(){\n\n cin >> n >> m >> k;\n\n for(int i=1;i<=n;i++){\n\n for(int j=1;j<=m;j++) cin >> a[i][j];\n\n }\n\n for(int i=1;i<=k;i++) dp[1]+=a[1][i]+a[2][i];\n\n for(int i=2;i<=m-k+1;i++) dp[i]=dp[i-1]-a[1][i-1]-a[2][i-1]+a[1][i+k-1]+a[2][i+k-1];\n\n for(int i=1;i<=m;i++) cur[i]=cur[i-1]+a[2][i];\n\n for(int i=3;i<=n+1;i++){\n\n for(int j=1;j<=m;j++){pre[j]=cur[j];cur[j]=cur[j-1]+a[i][j];}\n\n for(int j=m;j>=1;j--) suf[j]=suf[j+1]+a[i-1][j];\n\n Segtree::build(1,m-k+1,1);\n\n for(int j=1;j<=m-k+1;j++){\n\n dp[j]=Segtree::query(1,m-k+1,1,max(1,j-k+1),j,1)-suf[j+k]+cur[j+k-1]-cur[j-1];\n\n \/\/cout << dp[j] << ' ';\n\n dp[j]=max(dp[j],Segtree::query(1,m-k+1,1,j,min(j+k-1,m-k+1),2)-pre[j-1]+cur[j+k-1]-cur[j-1]);\n\n \/\/cout << dp[j] << ' ';\n\n if(j>k) dp[j]=max(dp[j],Segtree::query(1,m-k+1,1,1,j-k,0)+pre[j+k-1]-pre[j-1]+cur[j+k-1]-cur[j-1]);\n\n \/\/cout << dp[j] << ' ';\n\n if(j+k<=m-k+1) dp[j]=max(dp[j],Segtree::query(1,m-k+1,1,j+k,m-k+1,0)+pre[j+k-1]-pre[j-1]+cur[j+k-1]-cur[j-1]);\n\n \/\/cout << dp[j] << ' ' << pre[j+k-1]-pre[j-1]+cur[j+k-1]-cur[j-1] << '\\n';\n\n }\n\n \/\/cout << '\\n';\n\n }\n\n int ans=0;\n\n for(int i=1;i<=m-k+1;i++) ans=max(ans,dp[i]);\n\n cout << ans << '\\n';\n\n}\n\nsigned main(){\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(NULL);cout.tie(NULL);\n\n int test=1;\/\/cin >> test;\n\n while(test--) solve();\n\n}\n\n","language":"cpp"} -{"contest_id":"1325","problem_id":"A","statement":"A. EhAb AnD gCdtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a positive integer xx. Find any such 22 positive integers aa and bb such that GCD(a,b)+LCM(a,b)=xGCD(a,b)+LCM(a,b)=x.As a reminder, GCD(a,b)GCD(a,b) is the greatest integer that divides both aa and bb. Similarly, LCM(a,b)LCM(a,b) is the smallest integer such that both aa and bb divide it.It's guaranteed that the solution always exists. If there are several such pairs (a,b)(a,b), you can output any of them.InputThe first line contains a single integer tt (1\u2264t\u2264100)(1\u2264t\u2264100) \u00a0\u2014 the number of testcases.Each testcase consists of one line containing a single integer, xx (2\u2264x\u2264109)(2\u2264x\u2264109).OutputFor each testcase, output a pair of positive integers aa and bb (1\u2264a,b\u2264109)1\u2264a,b\u2264109) such that GCD(a,b)+LCM(a,b)=xGCD(a,b)+LCM(a,b)=x. It's guaranteed that the solution always exists. If there are several such pairs (a,b)(a,b), you can output any of them.ExampleInputCopy2\n2\n14\nOutputCopy1 1\n6 4\nNoteIn the first testcase of the sample; GCD(1,1)+LCM(1,1)=1+1=2GCD(1,1)+LCM(1,1)=1+1=2.In the second testcase of the sample, GCD(6,4)+LCM(6,4)=2+12=14GCD(6,4)+LCM(6,4)=2+12=14.","tags":["constructive algorithms","greedy","number theory"],"code":"#include \n\nusing namespace std;\n\n\n\nint main()\n\n{\n\n int t;\n\n cin>>t;\n\n while(t--){\n\n int n;\n\n cin>>n;\n\n cout<<1<<\" \"<\n\n\n\nusing namespace std;\n\n\n\ntypedef long long ll;\n\ntypedef pair pii;\n\ntypedef pair pll;\n\ntypedef vector vi;\n\n\n\n#define fi first\n\n#define se second\n\n#define pp push_back\n\n#define all(x) (x).begin(), (x).end()\n\n#define Ones(n) __builtin_popcount(n)\n\n#define endl '\\n'\n\n#define fill(arrr,xx) memset(arrr,xx,sizeof arrr)\n\n#define rep(aa, bb, cc) for(int aa = bb; aa < cc;aa++)\n\n#define PI acos(-1)\n\n\/\/#define int long long\n\n\n\nvoid Gamal() {\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(nullptr);\n\n cout.tie(nullptr);\n\n#ifdef Clion\n\n freopen(\"input.txt\", \"r\", stdin), freopen(\"output.txt\", \"w\", stdout);\n\n#endif\n\n}\n\n\n\nint dx[] = {+0, +0, -1, +1, +1, +1, -1, -1};\n\nint dy[] = {-1, +1, +0, +0, +1, -1, +1, -1};\n\n\n\nconst double EPS = 1e-9;\n\nconst ll N = 2e5 + 5, INF = INT_MAX, MOD = 1e9 + 7, OO = 0X3F3F3F3F3F3F3F3F, LOG = 25;\n\n\n\nvectoradj[N];\n\nint deg[N],ans,color[N];\n\n\n\nvoid dfs(int u,int par,int prv){\n\n int cur = 0;\n\n for(auto v:adj[u]){\n\n if(v.fi == par)continue;\n\n if(cur == prv){\n\n cur = (cur + 1)%ans;\n\n }\n\n color[v.se] = cur;\n\n dfs(v.fi,u,cur);\n\n cur = (cur + 1)%ans;\n\n }\n\n}\n\n\n\nvoid solve() {\n\n int n,k;cin >> n >> k;\n\n for (int i = 0; i < n-1; ++i) {\n\n int u,v;cin >> u >> v;\n\n u--,v--;\n\n adj[u].emplace_back(v,i);\n\n adj[v].emplace_back(u,i);\n\n deg[u]++,deg[v]++;\n\n }\n\n sort(deg,deg + n);\n\n ans = deg[n - k - 1];\n\n dfs(0,0,-1);\n\n cout << ans << endl;\n\n for (int i = 0; i < n - 1; ++i) {\n\n cout << color[i] + 1<< ' ';\n\n }\n\n\n\n}\n\n\n\n\n\nsigned main() {\n\n Gamal();\n\n int t = 1;\n\n\/\/ cin >> t;\n\n while (t--) {\n\n solve();\n\n }\n\n}","language":"cpp"} -{"contest_id":"1322","problem_id":"B","statement":"B. Presenttime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputCatherine received an array of integers as a gift for March 8. Eventually she grew bored with it; and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one\u00a0\u2014 xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute(a1+a2)\u2295(a1+a3)\u2295\u2026\u2295(a1+an)\u2295(a2+a3)\u2295\u2026\u2295(a2+an)\u2026\u2295(an\u22121+an)(a1+a2)\u2295(a1+a3)\u2295\u2026\u2295(a1+an)\u2295(a2+a3)\u2295\u2026\u2295(a2+an)\u2026\u2295(an\u22121+an)Here x\u2295yx\u2295y is a bitwise XOR operation (i.e. xx ^ yy in many modern programming languages). You can read about it in Wikipedia: https:\/\/en.wikipedia.org\/wiki\/Exclusive_or#Bitwise_operation.InputThe first line contains a single integer nn (2\u2264n\u22644000002\u2264n\u2264400000)\u00a0\u2014 the number of integers in the array.The second line contains integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641071\u2264ai\u2264107).OutputPrint a single integer\u00a0\u2014 xor of all pairwise sums of integers in the given array.ExamplesInputCopy2\n1 2\nOutputCopy3InputCopy3\n1 2 3\nOutputCopy2NoteIn the first sample case there is only one sum 1+2=31+2=3.In the second sample case there are three sums: 1+2=31+2=3, 1+3=41+3=4, 2+3=52+3=5. In binary they are represented as 0112\u22951002\u22951012=01020112\u22951002\u22951012=0102, thus the answer is 2.\u2295\u2295 is the bitwise xor operation. To define x\u2295yx\u2295y, consider binary representations of integers xx and yy. We put the ii-th bit of the result to be 1 when exactly one of the ii-th bits of xx and yy is 1. Otherwise, the ii-th bit of the result is put to be 0. For example, 01012\u229500112=0110201012\u229500112=01102.","tags":["binary search","bitmasks","constructive algorithms","data structures","math","sortings"],"code":"\/\/ prod by mdolchik\n\n\n\n#pragma GCC optimize(\"Ofast\")\n\n\n\n#include \n\n#include \n\n#include \n\n#include \n\n\n\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef long double ld;\n\ntypedef unsigned long long ull;\n\ntypedef pair pli;\n\ntypedef pair pii;\n\ntypedef pair pll;\n\n#define int ll\n\n#define E '\\n'\n\n#define fi first\n\n#define se second\n\n#define ins insert\n\n#define pb push_back\n\n#define pob pop_back\n\n#define pof pop_front\n\n#define umap unordered_map\n\n#define gp gp_hash_table\n\n#define uset unordered_set\n\n#define all(a) a.begin(),a.end()\n\n#define fbo find_by_order\n\n#define ork order_of_key\n\nmt19937 gen (time (0));\n\n\n\nconst int MAXI = INT_MAX \/ 2;\n\nconst ll LMAXI = LLONG_MAX \/ 2;\n\nint dx[] = {0, 0, -1, 1};\n\nint dy[] = {1, -1, 0, 0};\n\n\n\nconst ll MD = 1e9 + 7;\n\nll bp(ll a, int b) {\n\n ll st = 1;\n\n while(b > 0) {\n\n if (b & 1) {\n\n st *= a;\n\n st %= MD;\n\n --b;\n\n } else {\n\n a *= a;\n\n a %= MD;\n\n b >>= 1;\n\n }\n\n }\n\n return st;\n\n}\n\n\n\nusing namespace __gnu_pbds;\n\ntemplate \n\nusing ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n\n\nconst int N = 4e5 + 12;\n\nint a[N], b[N];\n\nint n;\n\nint get(int L, int R) {\n\n if(L > R)\n\n return 0;\n\n int l = 1, r = 0;\n\n int kol = 0;\n\n for(int i = n; i >= 1; --i) {\n\n while(l <= n && b[l] + b[i] < L)\n\n l++;\n\n while(r + 1 <= n && b[r+1] + b[i] <= R)\n\n r++;\n\n kol += r - l + 1 - (l <= i && i < r);\n\n\/\/ cout << i << ' ' << r << E;\n\n }\n\n return kol \/ 2;\n\n}\n\n\n\nint32_t main() {\n\n ios_base::sync_with_stdio(0);\n\n cin.tie(0);\n\n cout.tie(0);\n\n#ifdef LOCAL\n\n freopen (\"input.txt\", \"r\", stdin);\n\n freopen (\"output.txt\", \"w\", stdout);\n\n#else\n\n\/\/ freopen (\"bridges.in\", \"r\", stdin);\n\n\/\/ freopen (\"bridges.out\", \"w\", stdout);\n\n#endif \/\/ LOCAL\n\n cin >> n;\n\n for(int i = 1; i <= n; ++i) {\n\n cin >> a[i];\n\n }\n\n int ans = 0;\n\n for(int st = 0; st <= 25; st++) {\n\n for(int i = 1; i <= n; ++i) {\n\n b[i] = (a[i] % ((1 << (st + 1))));\n\n\/\/ cout << b[i] << ' ';\n\n }\n\n sort (b + 1, b + n + 1);\n\n\/\/ cout << E;\n\n int cnt = get((1 << st), (1 << (st + 1)) - 1);\n\n cnt += get((1 << (st + 1)) + (1 << st), (1 << (st + 2)) - 1);\n\n if(cnt % 2 == 1) {\n\n\/\/ cout << st << E;\n\n ans += (1 << st);\n\n }\n\n }\n\n cout << ans;\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1307","problem_id":"G","statement":"G. Cow and Exercisetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputFarmer John is obsessed with making Bessie exercise more!Bessie is out grazing on the farm; which consists of nn fields connected by mm directed roads. Each road takes some time wiwi to cross. She is currently at field 11 and will return to her home at field nn at the end of the day.Farmer John has plans to increase the time it takes to cross certain roads. He can increase the time it takes to cross each road by a nonnegative amount, but the total increase cannot exceed xixi for the ii-th plan. Determine the maximum he can make the shortest path from 11 to nn for each of the qq independent plans.InputThe first line contains integers nn and mm (2\u2264n\u2264502\u2264n\u226450, 1\u2264m\u2264n\u22c5(n\u22121)1\u2264m\u2264n\u22c5(n\u22121)) \u2014 the number of fields and number of roads, respectively.Each of the following mm lines contains 33 integers, uiui, vivi, and wiwi (1\u2264ui,vi\u2264n1\u2264ui,vi\u2264n, 1\u2264wi\u22641061\u2264wi\u2264106), meaning there is an road from field uiui to field vivi that takes wiwi time to cross.It is guaranteed that there exists a way to get to field nn from field 11. It is guaranteed that the graph does not contain self-loops or parallel edges. It is possible to have a road from uu to vv and a road from vv to uu.The next line contains a single integer qq (1\u2264q\u22641051\u2264q\u2264105), the number of plans.Each of the following qq lines contains a single integer xixi, the query (0\u2264xi\u22641050\u2264xi\u2264105).OutputFor each query, output the maximum Farmer John can make the shortest path if the total increase does not exceed xixi.Your answer is considered correct if its absolute or relative error does not exceed 10\u2212610\u22126.Formally, let your answer be aa, and the jury's answer be bb. Your answer is accepted if and only if |a\u2212b|max(1,|b|)\u226410\u22126|a\u2212b|max(1,|b|)\u226410\u22126.ExampleInputCopy3 3\n1 2 2\n2 3 2\n1 3 3\n5\n0\n1\n2\n3\n4\nOutputCopy3.0000000000\n4.0000000000\n4.5000000000\n5.0000000000\n5.5000000000\n","tags":["flows","graphs","shortest paths"],"code":"#include\n#define fi first\n#define se second\nusing namespace std;\nconst int _=100,inf=1e9;\nint S,T,n,m,Q,head[_],ne[_*_],to[_*_],w[_*_],c[_*_],tot=1,x;\nint dis[_],vis[_],p[_];\nvector>ans;\nvoid add(int x,int y,int z,int a){\n\tne[++tot]=head[x],head[x]=tot,to[tot]=y,w[tot]=z,c[tot]=a;\n}\nvoid ad(int x,int y,int z){\n\tadd(x,y,1,z),add(y,x,0,-z);\n}\nbool bfs(){\n\tfor(int i=1;i<=n;i++)dis[i]=inf,vis[i]=0;\n\tqueueq;q.push(S),vis[S]=1,dis[S]=0;\n\twhile(q.size()){\n\t\tint u=q.front();q.pop(),vis[u]=0;\n\t\tfor(int i=head[u];i;i=ne[i])\n\t\t\tif(w[i]&&dis[to[i]]>dis[u]+c[i]){\n\t\t\t\tdis[to[i]]=dis[u]+c[i],p[to[i]]=i;\n\t\t\t\tif(!vis[to[i]])vis[to[i]]=1,q.push(to[i]);\n\t\t\t}\n\t}\n\treturn dis[T]!=inf;\n}\nvoid EK(){\n\tint flow=0,cost=0;\n\twhile(bfs()){\n\t\tint f=inf;\n\t\tfor(int i=T;i!=S;i=to[p[i]^1])f=min(f,w[p[i]]);\n\t\tflow+=f,cost+=dis[T]*f;\n\t\tfor(int i=T;i!=S;i=to[p[i]^1])w[p[i]]-=f,w[p[i]^1]+=f;\n\t\tans.push_back({flow,cost});\n\t}\n}\nint main(){\n\tscanf(\"%d%d\",&n,&m);\n\tfor(int i=1,y,z;i<=m;i++)\n\t\tscanf(\"%d%d%d\",&x,&y,&z),ad(x,y,z);\n\tS=1,T=n;EK();\n\tscanf(\"%d\",&Q);\n\twhile(Q--){\n\t\tscanf(\"%d\",&x);\n\t\tlong double Min=inf;\n\t\tfor(auto v:ans)Min=min(Min,(long double)(v.se+x)\/v.fi);\n\t\tprintf(\"%.10Lf\\n\",Min);\n\t}\n\treturn 0;\n}\n\n\t \t\t\t\t \t \t \t \t \t \t","language":"cpp"} -{"contest_id":"1325","problem_id":"C","statement":"C. Ehab and Path-etic MEXstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree consisting of nn nodes. You want to write some labels on the tree's edges such that the following conditions hold: Every label is an integer between 00 and n\u22122n\u22122 inclusive. All the written labels are distinct. The largest value among MEX(u,v)MEX(u,v) over all pairs of nodes (u,v)(u,v) is as small as possible. Here, MEX(u,v)MEX(u,v) denotes the smallest non-negative integer that isn't written on any edge on the unique simple path from node uu to node vv.InputThe first line contains the integer nn (2\u2264n\u22641052\u2264n\u2264105)\u00a0\u2014 the number of nodes in the tree.Each of the next n\u22121n\u22121 lines contains two space-separated integers uu and vv (1\u2264u,v\u2264n1\u2264u,v\u2264n) that mean there's an edge between nodes uu and vv. It's guaranteed that the given graph is a tree.OutputOutput n\u22121n\u22121 integers. The ithith of them will be the number written on the ithith edge (in the input order).ExamplesInputCopy3\n1 2\n1 3\nOutputCopy0\n1\nInputCopy6\n1 2\n1 3\n2 4\n2 5\n5 6\nOutputCopy0\n3\n2\n4\n1NoteThe tree from the second sample:","tags":["constructive algorithms","dfs and similar","greedy","trees"],"code":"#include \n\nusing namespace std;\n\nconst int N=1e5+10;\n\nint a[N],b[N],d[N];\n\n\n\nint main(){\n\n int n;\n\n cin>>n;\n\n for (int i=0;i>a[i]>>b[i];\n\n d[a[i]]++;\n\n d[b[i]]++;\n\n }\n\n int x=0,y=3,m=0;\n\n for(int i=1;i<=n;i++){\n\n if(d[i]>=3){\n\n m=i,y=d[i];\n\n break;\n\n }\n\n }\n\n if(!m) y=0;\n\n for(int i=0;i\n\n#define pb emplace_back\n\ntypedef long long LL;\n\ntypedef unsigned long long ULL;\n\ntypedef std::pair PII;\n\nusing std::vector;\n\nconst int N=2030,mod=998244353;\n\ninline int read()\n\n{\n\n\tint x=0,y=1;char ch=getchar();\n\n\twhile(ch<'0'||ch>'9') {if(ch=='-') y=-1;ch=getchar();}\n\n\twhile(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();\n\n\treturn x*y;\n\n}\n\n\n\nint n,K,ans;\n\ninline int M(int A)\n\n{\n\n\treturn (A>=mod)?A-mod:A;\n\n}\n\n\n\nnamespace sub1{\n\n\t\/\/ \u4e0d\u8d85\u8fc7 n \u7684\u6240\u6709\u5212\u5206\u6570\u65b9\u6848\u3002 \n\n\t\/\/ dp[i]\uff1ai \u7684\u5212\u5206\u65b9\u6848\u6570\uff0c\u9700\u8981\u94a6\u5b9a\u4ece\u5c0f\u5230\u5927\uff0c\u653e\u5165\u4e00\u4e2a\u6570\u3002 \n\n\t\/\/ dp[i]+=dp[i-k]; \n\n\t\/\/ dp[i]+=dp[i-k];\n\n\tint dp[N];\n\n\tsigned main()\n\n\t{\n\n\t\tdp[0]=1;\n\n\t\tfor(int i=1;i<=n;i++) for(int j=i;j<=n;j++) dp[j]=M(dp[j]+dp[j-i]);\n\n\t\tfor(int i=1;i<=n;i++) ans=M(ans+dp[i]);\n\n\t\tprintf(\"%d\\n\",ans);\n\n\t\treturn 0;\n\n\t}\n\n}\n\nnamespace sub2{\n\n\t\/\/ f(f(S))\uff1ac[i] \u4e2a i, \u548c\u5c0f\u7b49\u4e8e n\uff0cc\u5355\u8c03\u4e0d\u589e\uff0c\u65b9\u6848\u6570\u3002\n\n\t\/\/ \u5355\u8c03\u4e0d\u589e -> c[i-1]-c[i]>=0 -> \u5dee\u5206 \n\n\t\/\/ dp[i]=\n\n\tint dp[N];\n\n\tinline int S(int l,int r)\n\n\t{\n\n\t\treturn (1ll*(l+r)*(r-l+1)\/2)%mod;\n\n\t}\n\n\tsigned main()\n\n\t{\n\n\t\tdp[0]=1;\n\n\t\tfor(int i=1,ns;(ns=S(1,i))<=n;i++)\n\n\t\t{\n\n\t\t\tfor(int j=ns;j<=n;j++)\n\n\t\t\t{\n\n\t\t\t\tdp[j]=M(dp[j]+dp[j-ns]);\n\n\t\t\t}\n\n\t\t}\n\n\t\tfor(int i=1;i<=n;i++) ans=M(ans+dp[i]);\n\n\t\tprintf(\"%d\\n\",ans);\n\n\t\treturn 0;\n\n\t}\n\n}\n\nnamespace sub3{\n\n\tint cnt[N];\n\n\tinline bool check(int nn)\n\n\t{\n\n\t\tint nw,sum=0;\n\n\t\tvector a,b;a.clear(),b.clear();\n\n\t\tfor(int i=1;i<=nn;i++) a.pb(cnt[i]);\n\n\t\tfor(int i=1,nw;in) return 0;\n\n\t\t}\n\n\t\treturn 1;\n\n\t}\n\n\tint dfs(int step,int pre,int ns)\n\n\t{\n\n\t\tif(step&&!check(step)) return 0;\n\n\t\tans++;\n\n\t\tfor(int i=pre;i<=n-ns;i++)\n\n\t\t{\n\n\t\t\tcnt[step+1]=i;\n\n\t\t\tif(!dfs(step+1,i,ns+i)) return 1;\n\n\t\t}\n\n\t\treturn 1;\n\n\t}\n\n\tsigned main()\n\n\t{\n\n\t\tdfs(0,1,0),printf(\"%d\\n\",ans-1); \n\n\t\treturn 0;\n\n\t}\n\n}\n\n\n\nsigned main()\n\n{\n\n\/\/ \tfreopen(\"data.in\",\"r\",stdin);\n\n\/\/ \tfreopen(\"zj.out\",\"w\",stdout);\n\n\tn=read(),K=read();\n\n\tif(K==1) sub1::main();\n\n\telse if(K==2) sub2::main();\n\n\telse sub3::main();\n\n\treturn 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1322","problem_id":"B","statement":"B. Presenttime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputCatherine received an array of integers as a gift for March 8. Eventually she grew bored with it; and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one\u00a0\u2014 xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute(a1+a2)\u2295(a1+a3)\u2295\u2026\u2295(a1+an)\u2295(a2+a3)\u2295\u2026\u2295(a2+an)\u2026\u2295(an\u22121+an)(a1+a2)\u2295(a1+a3)\u2295\u2026\u2295(a1+an)\u2295(a2+a3)\u2295\u2026\u2295(a2+an)\u2026\u2295(an\u22121+an)Here x\u2295yx\u2295y is a bitwise XOR operation (i.e. xx ^ yy in many modern programming languages). You can read about it in Wikipedia: https:\/\/en.wikipedia.org\/wiki\/Exclusive_or#Bitwise_operation.InputThe first line contains a single integer nn (2\u2264n\u22644000002\u2264n\u2264400000)\u00a0\u2014 the number of integers in the array.The second line contains integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641071\u2264ai\u2264107).OutputPrint a single integer\u00a0\u2014 xor of all pairwise sums of integers in the given array.ExamplesInputCopy2\n1 2\nOutputCopy3InputCopy3\n1 2 3\nOutputCopy2NoteIn the first sample case there is only one sum 1+2=31+2=3.In the second sample case there are three sums: 1+2=31+2=3, 1+3=41+3=4, 2+3=52+3=5. In binary they are represented as 0112\u22951002\u22951012=01020112\u22951002\u22951012=0102, thus the answer is 2.\u2295\u2295 is the bitwise xor operation. To define x\u2295yx\u2295y, consider binary representations of integers xx and yy. We put the ii-th bit of the result to be 1 when exactly one of the ii-th bits of xx and yy is 1. Otherwise, the ii-th bit of the result is put to be 0. For example, 01012\u229500112=0110201012\u229500112=01102.","tags":["binary search","bitmasks","constructive algorithms","data structures","math","sortings"],"code":"\/\/ prod by mdolchik\n\n\n\n#pragma GCC optimize(\"Ofast\")\n\n\n\n#include \n\n#include \n\n#include \n\n#include \n\n\n\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef long double ld;\n\ntypedef unsigned long long ull;\n\ntypedef pair pli;\n\ntypedef pair pii;\n\ntypedef pair pll;\n\n#define int ll\n\n#define E '\\n'\n\n#define fi first\n\n#define se second\n\n#define ins insert\n\n#define pb push_back\n\n#define pob pop_back\n\n#define pof pop_front\n\n#define umap unordered_map\n\n#define gp gp_hash_table\n\n#define uset unordered_set\n\n#define all(a) a.begin(),a.end()\n\n#define fbo find_by_order\n\n#define ork order_of_key\n\nmt19937 gen (time (0));\n\n\n\nconst int MAXI = INT_MAX \/ 2;\n\nconst ll LMAXI = LLONG_MAX \/ 2;\n\nint dx[] = {0, 0, -1, 1};\n\nint dy[] = {1, -1, 0, 0};\n\n\n\nconst ll MD = 1e9 + 7;\n\nll bp(ll a, int b) {\n\n ll st = 1;\n\n while(b > 0) {\n\n if (b & 1) {\n\n st *= a;\n\n st %= MD;\n\n --b;\n\n } else {\n\n a *= a;\n\n a %= MD;\n\n b >>= 1;\n\n }\n\n }\n\n return st;\n\n}\n\n\n\nusing namespace __gnu_pbds;\n\ntemplate \n\nusing ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n\n\nconst int N = 4e5 + 12;\n\nint a[N], b[N];\n\nint n;\n\nint get(int L, int R) {\n\n if(L > R)\n\n return 0;\n\n int l = 1, r = 1;\n\n int kol = 0;\n\n for(int i = n; i >= 1; --i) {\n\n while(l <= n && b[l] + b[i] < L)\n\n l++;\n\n while(r <= n && b[r] + b[i] <= R)\n\n r++;\n\n kol += r - l - (l <= i && i < r);\n\n\/\/ cout << i << ' ' << r << E;\n\n }\n\n return kol \/ 2;\n\n}\n\n\n\nint32_t main() {\n\n ios_base::sync_with_stdio(0);\n\n cin.tie(0);\n\n cout.tie(0);\n\n#ifdef LOCAL\n\n freopen (\"input.txt\", \"r\", stdin);\n\n freopen (\"output.txt\", \"w\", stdout);\n\n#else\n\n\/\/ freopen (\"bridges.in\", \"r\", stdin);\n\n\/\/ freopen (\"bridges.out\", \"w\", stdout);\n\n#endif \/\/ LOCAL\n\n cin >> n;\n\n for(int i = 1; i <= n; ++i) {\n\n cin >> a[i];\n\n }\n\n int ans = 0;\n\n for(int st = 0; st <= 25; st++) {\n\n for(int i = 1; i <= n; ++i) {\n\n b[i] = (a[i] % ((1 << (st + 1))));\n\n\/\/ cout << b[i] << ' ';\n\n }\n\n sort (b + 1, b + n + 1);\n\n\/\/ cout << E;\n\n int cnt = get((1 << st), (1 << (st + 1)) - 1);\n\n cnt += get((1 << (st + 1)) + (1 << st), (1 << (st + 2)) - 1);\n\n if(cnt % 2 == 1) {\n\n\/\/ cout << st << E;\n\n ans += (1 << st);\n\n }\n\n }\n\n cout << ans;\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1320","problem_id":"E","statement":"E. Treeland and Virusestime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere are nn cities in Treeland connected with n\u22121n\u22121 bidirectional roads in such that a way that any city is reachable from any other; in other words, the graph of cities and roads is a tree. Treeland is preparing for a seasonal virus epidemic, and currently, they are trying to evaluate different infection scenarios.In each scenario, several cities are initially infected with different virus species. Suppose that there are kiki virus species in the ii-th scenario. Let us denote vjvj the initial city for the virus jj, and sjsj the propagation speed of the virus jj. The spread of the viruses happens in turns: first virus 11 spreads, followed by virus 22, and so on. After virus kiki spreads, the process starts again from virus 11.A spread turn of virus jj proceeds as follows. For each city xx not infected with any virus at the start of the turn, at the end of the turn it becomes infected with virus jj if and only if there is such a city yy that: city yy was infected with virus jj at the start of the turn; the path between cities xx and yy contains at most sjsj edges; all cities on the path between cities xx and yy (excluding yy) were uninfected with any virus at the start of the turn.Once a city is infected with a virus, it stays infected indefinitely and can not be infected with any other virus. The spread stops once all cities are infected.You need to process qq independent scenarios. Each scenario is described by kiki virus species and mimi important cities. For each important city determine which the virus it will be infected by in the end.InputThe first line contains a single integer nn (1\u2264n\u22642\u22c51051\u2264n\u22642\u22c5105)\u00a0\u2014 the number of cities in Treeland.The following n\u22121n\u22121 lines describe the roads. The ii-th of these lines contains two integers xixi and yiyi (1\u2264xi,yi\u2264n1\u2264xi,yi\u2264n)\u00a0\u2014 indices of cities connecting by the ii-th road. It is guaranteed that the given graph of cities and roads is a tree.The next line contains a single integer qq (1\u2264q\u22642\u22c51051\u2264q\u22642\u22c5105)\u00a0\u2014 the number of infection scenarios. qq scenario descriptions follow.The description of the ii-th scenario starts with a line containing two integers kiki and mimi (1\u2264ki,mi\u2264n1\u2264ki,mi\u2264n)\u00a0\u2014 the number of virus species and the number of important cities in this scenario respectively. It is guaranteed that \u2211qi=1ki\u2211i=1qki and \u2211qi=1mi\u2211i=1qmi do not exceed 2\u22c51052\u22c5105.The following kiki lines describe the virus species. The jj-th of these lines contains two integers vjvj and sjsj (1\u2264vj\u2264n1\u2264vj\u2264n, 1\u2264sj\u22641061\u2264sj\u2264106)\u00a0\u2013 the initial city and the propagation speed of the virus species jj. It is guaranteed that the initial cities of all virus species within a scenario are distinct.The following line contains mimi distinct integers u1,\u2026,umiu1,\u2026,umi (1\u2264uj\u2264n1\u2264uj\u2264n)\u00a0\u2014 indices of important cities.OutputPrint qq lines. The ii-th line should contain mimi integers\u00a0\u2014 indices of virus species that cities u1,\u2026,umiu1,\u2026,umi are infected with at the end of the ii-th scenario.ExampleInputCopy7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n3\n2 2\n4 1\n7 1\n1 3\n2 2\n4 3\n7 1\n1 3\n3 3\n1 1\n4 100\n7 100\n1 2 3\nOutputCopy1 2\n1 1\n1 1 1\n","tags":["data structures","dfs and similar","dp","shortest paths","trees"],"code":"#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\nstruct edge{int x,y,d;edge(int x=0,int y=0,int d=0){this->x=x,this->y=y,this->d=d;}};\n\nstruct tree\n\n{\n\n\tstd::vector a[200005];\n\n\tint n,rt,dep[200005],fa[200005],sz[200005],son[200005],top[200005],g[200005],cnt;\n\n\tstatic int id[200005];\n\n\tvoid add(int x,int y)\n\n\t{\n\n\/\/\t\tprintf(\"Edge %d %d\\n\",x,y);\n\n\t\ta[x].push_back(y);\n\n\t\ta[y].push_back(x);\n\n\t}\n\n\tvoid dfs1(int v,int d)\n\n\t{\n\n\t\tsz[v]=1;dep[v]=d;son[v]=0;\n\n\t\tfor (int i=0;i<(int)a[v].size();i++)\n\n\t\t{\n\n\t\t\tint u=a[v][i];\n\n\t\t\tif (u==fa[v]) continue;\n\n\t\t\tfa[u]=v;\n\n\t\t\tdfs1(u,d+1);\n\n\t\t\tsz[v]=sz[v]+sz[u];\n\n\t\t\tif (sz[son[v]] solve(std::vector s)\n\n\t{\n\n#define rt 1\n\n\t\tstd::sort(s.begin(),s.end(),cmp);\n\n\t\tstd::vector p;\n\n\t\tstd::stack st;\n\n\t\tstd::vector e;\n\n\t\tst.push(rt);p.push_back(rt);\n\n\t\tfor (int i=0;i<(int)s.size();i++)\n\n\t\t{\n\n\t\t\tint x=s[i],d=lca(st.top(),x);\n\n\t\t\tif (x==rt) continue;\n\n\t\t\tif (st.top()==d){st.push(x),p.push_back(x),g[x]=d;continue;}\n\n\t\t\twhile (!st.empty())\n\n\t\t\t{\n\n\t\t\t\tint k=st.top();st.pop();\n\n\t\t\t\tif (st.empty()){st.push(k);st.push(x);p.push_back(x);break;}\n\n\t\t\t\tif (dep[st.top()]<=dep[d])\n\n\t\t\t\t{\n\n\t\t\t\t\tif (st.top()==d){st.push(x),p.push_back(x),g[x]=d;break;}\n\n\t\t\t\t\tg[k]=d;\n\n\t\t\t\t\tg[d]=st.top(),st.push(d);p.push_back(d);\n\n\t\t\t\t\tg[x]=d,st.push(x),p.push_back(x);\n\n\t\t\t\t\tbreak;\n\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t}\n\n\t\tfor (int i=0;i<(int)p.size();i++) if (p[i]!=rt) e.push_back(edge(p[i],g[p[i]],dep[p[i]]-dep[g[p[i]]]));\n\n\t\tfor (int i=0;i<(int)p.size();i++) g[p[i]]=0;\n\n\/\/\t\tputs(\"qwq\");\n\n\/\/\t\tfor (int i=1;i<=n;i++) printf(\"%d \",g[i]);puts(\"\");\n\n\t\treturn e;\n\n#undef rt\n\n\t}\n\n};\n\nint tree::id[200005];\n\nstruct graph\n\n{\n\n\tstruct edge\n\n\t{\n\n\t\tint to,w;\n\n\t\tedge(int to=0,int w=0){this->to=to,this->w=w;}\n\n\t};\n\n\tstruct node\n\n\t{\n\n\t\tint t,c,d,s;\n\n\t\tnode(int t=0,int c=0,int d=0,int s=0){this->t=t,this->c=c,this->d=d,this->s=s;}\n\n\t\tint operator >(node x)\n\n\t\t{\n\n\t\t\tif (t!=x.t) return t>x.t;\n\n\t\t\tif (c!=x.c) return c>x.c;\n\n\t\t\treturn s>x.s;\n\n\t\t}\n\n\t\tint operator <(node x)\n\n\t\t{\n\n\t\t\tif (t!=x.t) return t a[200005];\n\n\tstruct cmp\n\n\t{\n\n\t\tint operator()(std::pair x,std::pair y){return x.first>y.first;}\n\n\t};\n\n\tstd::priority_queue< std::pair,std::vector< std::pair >,cmp > pq;\n\n\tvoid add(int x,int y,int s){a[x].push_back(edge(y,s));a[y].push_back(edge(x,s));}\n\n\tvoid make(int x,int c,int s){\/*printf(\"qwq %d %d %d\\n\",x,c,s);*\/dis[x]=node(0,c,0,s);pq.push(std::make_pair(dis[x],x));}\n\n\tvoid dijkstra()\n\n\t{\n\n\/\/\t\tputs(\"Start Point Distance\");\n\n\/\/\t\tfor (int i=1;i<=n;i++) printf(\"%d(%d)[%d] \",dis[i].t,dis[i].c,dis[i].d);puts(\"\");\n\n\t\twhile (!pq.empty())\n\n\t\t{\n\n\t\t\tint v=pq.top().second;pq.pop();\n\n\t\t\tif (vis[v]) continue;vis[v]=1;\n\n\/\/\t\t\tprintf(\"v = %d\\n\",v);\n\n\t\t\tfor (int i=0;i<(int)a[v].size();i++)\n\n\t\t\t{\n\n\t\t\t\tint u=a[v][i].to,w=a[v][i].w;\n\n\/\/\t\t\t\tprintf(\"%d -> %d(w %d)[%d]\\n\",v,u,w,dis[v].t);\n\n\t\t\t\tif (dis[v]+wn=n;\n\n\t\tfor (int i=1;i<=n;i++) dis[i]=node(1000000000,-1,-1,-1);\n\n\t}\n\n};\n\n\/\/graph::node;\n\ngraph g;\n\ntree t;\n\nint n,q,pt[200005],p[200005],d[200005];\n\nstd::map mp;\n\nint main()\n\n{\n\n\tscanf(\"%d\",&n);\n\n\tfor (int i=1;i vec,used;\n\n\t\tint m,s;\n\n\t\tscanf(\"%d%d\",&m,&s);\n\n\t\tfor (int i=1;i<=m;i++)\n\n\t\t{\n\n\t\t\tscanf(\"%d%d\",&p[i],&d[i]);\n\n\t\t\tif (!mp[p[i]]) mp[p[i]]=1,vec.push_back(p[i]);\n\n\t\t}\n\n\t\tfor (int i=1;i<=s;i++)\n\n\t\t{\n\n\t\t\tscanf(\"%d\",&pt[i]);\n\n\t\t\tif (!mp[pt[i]]) mp[pt[i]]=1,vec.push_back(pt[i]);\n\n\t\t}\n\n\t\tstd::vector e=t.solve(vec);\n\n\t\tused.push_back(1);\n\n\t\tfor (int i=0;i<(int)e.size();i++)\n\n\t\t{\n\n\t\t\tint x=e[i].x,y=e[i].y,d=e[i].d;\n\n\/\/\t\t\tprintf(\"Edge %d %d %d\\n\",x,y,d);\n\n\t\t\tused.push_back(x);\n\n\t\t\tused.push_back(y);\n\n\t\t\tg.add(x,y,d);\n\n\t\t}\n\n\/\/\t\tprintf(\"pt %d\\n\",used.size());\n\n\/\/\t\tfor (int i=0;i<(int)used.size();i++) printf(\"%d \",used[i]);puts(\"\");\n\n\t\tfor (int i=1;i<=m;i++) g.make(p[i],i,d[i]);\n\n\t\tg.dijkstra();\n\n\/\/\t\tputs(\"Success!\");\n\n\/\/\t\tprintf(\"output: \");\n\n\t\tfor (int i=1;i<=s;i++) printf(\"%d \",g.dis[pt[i]].c);puts(\"\");\n\n\t\tfor (int i=0;i<(int)used.size();i++) g.clear(used[i]);\n\n\t\tfor (int i=1;i<=m;i++) p[i]=d[i]=0;\n\n\t\tfor (int i=1;i<=s;i++) pt[i]=0;\n\n\t\tmp.clear();\n\n\t}\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1307","problem_id":"E","statement":"E. Cow and Treatstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter a successful year of milk production; Farmer John is rewarding his cows with their favorite treat: tasty grass!On the field, there is a row of nn units of grass, each with a sweetness sisi. Farmer John has mm cows, each with a favorite sweetness fifi and a hunger value hihi. He would like to pick two disjoint subsets of cows to line up on the left and right side of the grass row. There is no restriction on how many cows must be on either side. The cows will be treated in the following manner: The cows from the left and right side will take turns feeding in an order decided by Farmer John. When a cow feeds, it walks towards the other end without changing direction and eats grass of its favorite sweetness until it eats hihi units. The moment a cow eats hihi units, it will fall asleep there, preventing further cows from passing it from both directions. If it encounters another sleeping cow or reaches the end of the grass row, it will get upset. Farmer John absolutely does not want any cows to get upset. Note that grass does not grow back. Also, to prevent cows from getting upset, not every cow has to feed since FJ can choose a subset of them. Surprisingly, FJ has determined that sleeping cows are the most satisfied. If FJ orders optimally, what is the maximum number of sleeping cows that can result, and how many ways can FJ choose the subset of cows on the left and right side to achieve that maximum number of sleeping cows (modulo 109+7109+7)? The order in which FJ sends the cows does not matter as long as no cows get upset. InputThe first line contains two integers nn and mm (1\u2264n\u226450001\u2264n\u22645000, 1\u2264m\u226450001\u2264m\u22645000) \u00a0\u2014 the number of units of grass and the number of cows. The second line contains nn integers s1,s2,\u2026,sns1,s2,\u2026,sn (1\u2264si\u2264n1\u2264si\u2264n) \u00a0\u2014 the sweetness values of the grass.The ii-th of the following mm lines contains two integers fifi and hihi (1\u2264fi,hi\u2264n1\u2264fi,hi\u2264n) \u00a0\u2014 the favorite sweetness and hunger value of the ii-th cow. No two cows have the same hunger and favorite sweetness simultaneously.OutputOutput two integers \u00a0\u2014 the maximum number of sleeping cows that can result and the number of ways modulo 109+7109+7. ExamplesInputCopy5 2\n1 1 1 1 1\n1 2\n1 3\nOutputCopy2 2\nInputCopy5 2\n1 1 1 1 1\n1 2\n1 4\nOutputCopy1 4\nInputCopy3 2\n2 3 2\n3 1\n2 1\nOutputCopy2 4\nInputCopy5 1\n1 1 1 1 1\n2 5\nOutputCopy0 1\nNoteIn the first example; FJ can line up the cows as follows to achieve 22 sleeping cows: Cow 11 is lined up on the left side and cow 22 is lined up on the right side. Cow 22 is lined up on the left side and cow 11 is lined up on the right side. In the second example, FJ can line up the cows as follows to achieve 11 sleeping cow: Cow 11 is lined up on the left side. Cow 22 is lined up on the left side. Cow 11 is lined up on the right side. Cow 22 is lined up on the right side. In the third example, FJ can line up the cows as follows to achieve 22 sleeping cows: Cow 11 and 22 are lined up on the left side. Cow 11 and 22 are lined up on the right side. Cow 11 is lined up on the left side and cow 22 is lined up on the right side. Cow 11 is lined up on the right side and cow 22 is lined up on the left side. In the fourth example, FJ cannot end up with any sleeping cows, so there will be no cows lined up on either side.","tags":["binary search","combinatorics","dp","greedy","implementation","math"],"code":"#pragma GCC optimaze(3)\n\n#pragma GCC optimaze(2)\n\n#pragma GCC optimaze(\"Ofast\")\n\n#pragma GCC optimaze(\"inline\")\n\n#pragma GCC optimaze(\"unroll-loops\")\n\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native\")\n\n#include\n\n#define int long long\n\n#define ll long long\n\n#define N 5010\n\nusing namespace std;\n\nconst ll md=1e9+7;\n\n\n\nint ss[N][N],s1[N],s2[N],a[N];\n\nint n,m;\n\nll r1=1,r2=0;\n\n\n\ninline void add(ll &x,ll y)\n\n{\n\n\tx+=y;\n\n\tif (x>=md) x-=md;\n\n}\n\n\n\nsigned main()\n\n{\n\n\tscanf(\"%lld%lld\",&n,&m);\n\n\tfor (int i=1;i<=n;i++)\n\n\t{\n\n\t\tscanf(\"%lld\",&a[i]);\n\n\t\ts2[a[i]]++;\n\n\t}\n\n\tfor (int i=1,x,y;i<=m;i++)\n\n\t{\n\n\t\tscanf(\"%lld%lld\",&x,&y);\n\n\t\tss[x][y]++;\n\n\t}\n\n\tfor (int i=1;i<=n;i++)\n\n\t{\n\n\t\tfor (int j=1;j<=n;j++)\n\n\t\t{\n\n\t\t\tss[i][j]+=ss[i][j-1];\n\n\t\t}\n\n\t}\n\n\tfor (int i=0;i<=n;i++)\n\n\t{\n\n\t\tif (i)\n\n\t\t{\n\n\t\t\ts1[a[i]]++;\n\n\t\t\ts2[a[i]]--;\n\n\t\t}\n\n\t\tll sum=1,num=0;\n\n\t\tint t1,t2;\n\n\t\tif (i)\n\n\t\t{\n\n\t\t\tt1=s1[a[i]],t2=s2[a[i]];\n\n\t\t\tt2=ss[a[i]][t2]-(t2>=t1);\n\n\t\t\tt1=ss[a[i]][t1]-ss[a[i]][t1-1];\n\n\t\t\tif (!t1) continue;\n\n\t\t\tif (t2)\n\n\t\t\t{\n\n\t\t\t\tsum=sum*t1*t2%md;\n\n\t\t\t\tnum+=2;\n\n\t\t\t}\n\n\t\t\telse\n\n\t\t\t{\n\n\t\t\t\tsum=sum*t1%md;\n\n\t\t\t\tnum++;\n\n\t\t\t}\n\n\t\t}\n\n\/\/\t\tcout<r2)\n\n\t\t\tr2=num,r1=sum;\n\n\t\telse if (num==r2)\n\n\t\t\tadd(r1,sum);\n\n\/\/\t\tcout<\n\nusing namespace std;\n\n\n\nusing point_t=long long; \/\/\u5168\u5c40\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4fee\u6539\u4e3a long long \u7b49\n\n\n\nconstexpr point_t eps=0;\n\nconstexpr long double PI=3.1415926535897932384l;\n\n\n\n\/\/ \u70b9\u4e0e\u5411\u91cf\n\ntemplate struct point\n\n{\n\n T x,y;\n\n \/\/ int id=-1;\n\n bool operator==(const point &a) const {return (abs(x-a.x)<=eps && abs(y-a.y)<=eps);}\n\n bool operator<(const point &a) const {if (abs(x-a.x)<=eps) return y(const point &a) const {return !(*thiseps)-(t<-eps);} \/\/ to-left \u6d4b\u8bd5\n\n T len2() const {return (*this)*(*this);} \/\/ \u5411\u91cf\u957f\u5ea6\u7684\u5e73\u65b9\n\n T dis2(const point &a) const {return (a-(*this)).len2();} \/\/ \u4e24\u70b9\u8ddd\u79bb\u7684\u5e73\u65b9\n\n\n\n \/\/ \u6d89\u53ca\u6d6e\u70b9\u6570\n\n long double len() const {return sqrtl(len2());} \/\/ \u5411\u91cf\u957f\u5ea6\n\n long double dis(const point &a) const {return sqrtl(dis2(a));} \/\/ \u4e24\u70b9\u8ddd\u79bb\n\n long double ang(const point &a) const {return acosl(max(-1.0l,min(1.0l,((*this)*a)\/(len()*a.len()))));} \/\/ \u5411\u91cf\u5939\u89d2\n\n point rot(const long double rad) const {return {x*cos(rad)-y*sin(rad),x*sin(rad)+y*cos(rad)};} \/\/ \u9006\u65f6\u9488\u65cb\u8f6c\uff08\u7ed9\u5b9a\u89d2\u5ea6\uff09\n\n point rot(const long double cosr,const long double sinr) const {return {x*cosr-y*sinr,x*sinr+y*cosr};} \/\/ \u9006\u65f6\u9488\u65cb\u8f6c\uff08\u7ed9\u5b9a\u89d2\u5ea6\u7684\u6b63\u5f26\u4e0e\u4f59\u5f26\uff09\n\n};\n\n\n\nusing Point=point;\n\n\n\n\/\/ \u6781\u89d2\u6392\u5e8f\n\nstruct argcmp\n\n{\n\n bool operator()(const Point &a,const Point &b) const\n\n {\n\n const auto quad=[](const Point &a)\n\n {\n\n if (a.y<-eps) return 1;\n\n if (a.y>eps) return 4;\n\n if (a.x<-eps) return 5;\n\n if (a.x>eps) return 3;\n\n return 2;\n\n };\n\n const int qa=quad(a),qb=quad(b);\n\n if (qa!=qb) return qaeps;\n\n }\n\n};\n\n\n\n\/\/ \u76f4\u7ebf\n\ntemplate struct line\n\n{\n\n point p,v; \/\/ p \u4e3a\u76f4\u7ebf\u4e0a\u4e00\u70b9\uff0cv \u4e3a\u65b9\u5411\u5411\u91cf\n\n\n\n bool operator==(const line &a) const {return v.toleft(a.v)==0 && v.toleft(p-a.p)==0;}\n\n int toleft(const point &a) const {return v.toleft(a-p);} \/\/ to-left \u6d4b\u8bd5\n\n bool operator<(const line &a) const \/\/ \u534a\u5e73\u9762\u4ea4\u7b97\u6cd5\u5b9a\u4e49\u7684\u6392\u5e8f\n\n {\n\n if (abs(v^a.v)<=eps && v*a.v>=-eps) return toleft(a.p)==-1;\n\n return argcmp()(v,a.v);\n\n }\n\n\n\n \/\/ \u6d89\u53ca\u6d6e\u70b9\u6570\n\n point inter(const line &a) const {return p+v*((a.v^(p-a.p))\/(v^a.v));} \/\/ \u76f4\u7ebf\u4ea4\u70b9\n\n long double dis(const point &a) const {return abs(v^(a-p))\/v.len();} \/\/ \u70b9\u5230\u76f4\u7ebf\u8ddd\u79bb\n\n point proj(const point &a) const {return p+v*((v*(a-p))\/(v*v));} \/\/ \u70b9\u5728\u76f4\u7ebf\u4e0a\u7684\u6295\u5f71\n\n};\n\n\n\nusing Line=line;\n\n\n\n\/\/\u7ebf\u6bb5\n\ntemplate struct segment\n\n{\n\n point a,b;\n\n\n\n bool operator<(const segment &s) const {return make_pair(a,b) &p) const \n\n {\n\n if (p==a || p==b) return -1;\n\n return (p-a).toleft(p-b)==0 && (p-a)*(p-b)<-eps;\n\n }\n\n\n\n \/\/ \u5224\u65ad\u7ebf\u6bb5\u76f4\u7ebf\u662f\u5426\u76f8\u4ea4\n\n \/\/ -1 \u76f4\u7ebf\u7ecf\u8fc7\u7ebf\u6bb5\u7aef\u70b9 | 0 \u7ebf\u6bb5\u548c\u76f4\u7ebf\u4e0d\u76f8\u4ea4 | 1 \u7ebf\u6bb5\u548c\u76f4\u7ebf\u4e25\u683c\u76f8\u4ea4\n\n int is_inter(const line &l) const\n\n {\n\n if (l.toleft(a)==0 || l.toleft(b)==0) return -1;\n\n return l.toleft(a)!=l.toleft(b);\n\n }\n\n \n\n \/\/ \u5224\u65ad\u4e24\u7ebf\u6bb5\u662f\u5426\u76f8\u4ea4\n\n \/\/ -1 \u5728\u67d0\u4e00\u7ebf\u6bb5\u7aef\u70b9\u5904\u76f8\u4ea4 | 0 \u4e24\u7ebf\u6bb5\u4e0d\u76f8\u4ea4 | 1 \u4e24\u7ebf\u6bb5\u4e25\u683c\u76f8\u4ea4\n\n int is_inter(const segment &s) const\n\n {\n\n if (is_on(s.a) || is_on(s.b) || s.is_on(a) || s.is_on(b)) return -1;\n\n const line l{a,b-a},ls{s.a,s.b-s.a};\n\n return l.toleft(s.a)*l.toleft(s.b)==-1 && ls.toleft(a)*ls.toleft(b)==-1;\n\n }\n\n\n\n \/\/ \u70b9\u5230\u7ebf\u6bb5\u8ddd\u79bb\n\n long double dis(const point &p) const\n\n {\n\n if ((p-a)*(b-a)<-eps || (p-b)*(a-b)<-eps) return min(p.dis(a),p.dis(b));\n\n const line l{a,b-a};\n\n return l.dis(p);\n\n }\n\n\n\n \/\/ \u4e24\u7ebf\u6bb5\u95f4\u8ddd\u79bb\n\n long double dis(const segment &s) const\n\n {\n\n if (is_inter(s)) return 0;\n\n return min({dis(s.a),dis(s.b),s.dis(a),s.dis(b)});\n\n }\n\n};\n\n\n\nusing Segment=segment;\n\nbool check(vector &vec)\n\n{\n\n for(int i=0;i<3;++i)\n\n for(int j=i+1;j<3;++j)\n\n if(vec[i].is_inter(vec[j])==0)return 1;\n\n \n\n for(int i=0;i<3;++i)\n\n for(int j=0;j<3;++j)\n\n if(i!=j)\n\n if((vec[i].a.y-vec[i].b.y)*(vec[j].a.x-vec[j].b.x)\n\n ==(vec[j].a.y-vec[j].b.y)*(vec[i].a.x-vec[i].b.x))\n\n return 1;\n\n \n\n if((vec[1].is_on(vec[0].a))\n\n &&vec[2].is_on(vec[0].b))\n\n {\n\n swap(vec[0],vec[2]);\n\n return 0;\n\n }\n\n \n\n if((vec[1].is_on(vec[0].b))\n\n &&vec[2].is_on(vec[0].a))\n\n {\n\n swap(vec[0],vec[2]);\n\n \n\n return 0;\n\n }\n\n \n\n if((vec[0].is_on(vec[1].a))\n\n &&vec[2].is_on(vec[1].b))\n\n {\n\n swap(vec[1],vec[2]);\n\n return 0;\n\n }\n\n \n\n if((vec[0].is_on(vec[1].b))\n\n &&vec[2].is_on(vec[1].a))\n\n {\n\n swap(vec[1],vec[2]);\n\n return 0;\n\n }\n\n \n\n if((vec[1].is_on(vec[2].a))\n\n &&vec[0].is_on(vec[2].b))\n\n return 0;\n\n \n\n if((vec[1].is_on(vec[2].b))\n\n &&vec[0].is_on(vec[2].a))return 0;\n\n \n\n return 1;\n\n}\n\nvector dot;\n\nvoid print(Segment a)\n\n{\n\n cout<<\"Seg-->\"< vec)\n\n{\n\n if(vec[0].v.toleft(vec[1].v)==0)return true;\n\n if(vec[0].v.toleft(vec[2].v)==0)return true;\n\n if(vec[1].v.toleft(vec[2].v)==0)return true;\n\n return false;\n\n}\n\nbool check2(vector vec)\n\n{\n\n if(vec[0].a==vec[1].a)\n\n {\n\n dot.push_back(vec[0].a);dot.push_back(vec[0].b);\n\n dot.push_back(vec[1].a);dot.push_back(vec[1].b);\n\n return 0;\n\n }\n\n if(vec[0].b==vec[1].b)\n\n {\n\n dot.push_back(vec[0].b);dot.push_back(vec[0].a);\n\n dot.push_back(vec[1].b);dot.push_back(vec[1].a);\n\n return 0;\n\n }\n\n \n\n if(vec[1].b==vec[0].a)\n\n {dot.push_back(vec[1].b);dot.push_back(vec[1].a);\n\n dot.push_back(vec[0].a);dot.push_back(vec[0].b);\n\n return 0;}\n\n \n\n if(vec[0].b==vec[1].a)\n\n {dot.push_back(vec[1].a);dot.push_back(vec[1].b);\n\n dot.push_back(vec[0].b);dot.push_back(vec[0].a);\n\n return 0;}\n\n \n\n return 1;\n\n}\n\nbool ccheck(vector vec)\n\n{\n\n Segment s1={vec[0],vec[1]};\n\n if(s1.is_on(vec[4])==0)swap(vec[4],vec[5]);\n\n point_t l1=vec[0].dis2(vec[4]),l2=vec[2].dis2(vec[5]),\n\n \n\n l3=vec[1].dis2(vec[4]),l4=vec[0].dis2(vec[4]),\n\n l6=vec[2].dis2(vec[5]),l7=vec[3].dis2(vec[5]),\n\n l5=vec[4].dis2(vec[5]);\n\n if(l1+l2-l5<0)return false;\n\n if(max(l4,l3)>(__int128)min(l4,l3)*16)return false;\n\n if(max(l6,l7)>(__int128)min(l6,l7)*16)return false;\n\n return true;\n\n}\n\nbool is_right_triangle(vector &vec)\n\n{\n\n point_t a=vec[0].dis2(vec[1]),b=vec[2].dis2(vec[3]),\n\n c=vec[4].dis2(vec[5]);\n\n if(a+b==c||a+c==b||b+c==a)return true;\n\n return false;\n\n}\n\nvoid solve()\n\n{\n\n dot.clear();\n\n vector vec(6);\n\n for(point_t i=0,x,y;i<6;++i)\n\n {\n\n scanf(\"%lld%lld\",&x,&y);\n\n vec[i]={x,y};\n\n }\n\n vectorvecl(3);\n\n vectorvecs(3);\n\n for(int i=0;i<3;++i)\n\n vecs[i]={vec[i*2],vec[(i*2)^1]};\n\n \/\/ for(int i=0;i<3;++i)\n\n if(check(vecs)){printf(\"NO\\n\");return ;}\n\n if(check2(vecs)){printf(\"NO\\n\");return ;}\n\n if(is_right_triangle(vec)) {printf(\"NO\\n\");return ;}\n\n dot.push_back(vecs[2].a);\n\n dot.push_back(vecs[2].b);\n\n\n\n for(int i=0;i<3;++i)\n\n vec[i*2]=vecs[i].a,vec[(i*2)^1]=vecs[i].b;\n\n \n\n for(int i=0;i<3;++i)\n\n {\n\n vecl[i]={vec[i*2],vec[(i*2)^1]-vec[i*2]};\n\n }\n\n \/\/ if(check_vec(vecl)){printf(\"NO\\n\");return ;}\n\n\n\n\n\n if(ccheck(dot)) {printf(\"YES\\n\");return ;}\n\n\n\n printf(\"NO\\n\");\n\n}\n\nint main()\n\n{\n\n int T;scanf(\"%d\",&T);\n\n while(T--)solve();\n\n}","language":"cpp"} -{"contest_id":"1305","problem_id":"E","statement":"E. Kuroni and the Score Distributiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni is the coordinator of the next Mathforces round written by the \"Proof by AC\" team. All the preparation has been done; and he is discussing with the team about the score distribution for the round.The round consists of nn problems, numbered from 11 to nn. The problems are ordered in increasing order of difficulty, no two problems have the same difficulty. A score distribution for the round can be denoted by an array a1,a2,\u2026,ana1,a2,\u2026,an, where aiai is the score of ii-th problem. Kuroni thinks that the score distribution should satisfy the following requirements: The score of each problem should be a positive integer not exceeding 109109. A harder problem should grant a strictly higher score than an easier problem. In other words, 1\u2264a1\n\nusing namespace std;\n\n#ifdef LOCAL\n#include \"debug.h\"\n#else\n#define dout(...) 0\n#endif\n\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int n, m;\n cin >> n >> m;\n vector a(n);\n int i = 0;\n while (m > 0) {\n if (i == n) {\n cout << -1 << '\\n';\n return 0;\n }\n if (m > i \/ 2) {\n a[i] = i + 1;\n m -= i \/ 2;\n } else {\n a[i] = i + 1;\n int extra = i \/ 2 - m;\n a[i] += extra * 2;\n m = 0;\n }\n i += 1;\n }\n while (i < n) {\n a[i] = 1e9 - 5005 * (n - 1 - i);\n i += 1;\n }\n for (int i = 0; i < n; i++) {\n cout << a[i] << \" \\n\"[i + 1 == n];\n }\n return 0;\n}\n","language":"cpp"} -{"contest_id":"1294","problem_id":"B","statement":"B. Collecting Packagestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a robot in a warehouse and nn packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0)(0,0). The ii-th package is at the point (xi,yi)(xi,yi). It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point (0,0)(0,0) doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point (x,y)(x,y) to the point (x+1,yx+1,y) or to the point (x,y+1)(x,y+1).As we say above, the robot wants to collect all nn packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string ss of length nn is lexicographically less than the string tt of length nn if there is some index 1\u2264j\u2264n1\u2264j\u2264n that for all ii from 11 to j\u22121j\u22121 si=tisi=ti and sj\n\n using namespace std;\n\n int main()\n\n {\n\n \t long long int t;\n\n \t cin>>t;\n\n \t while(t--){\n\n \tlong long int n,m,a,b,i,mx=0,xd=0,yd=0,dl=0;\n\n \tcin>>n;\n\n \tlong long int arrx[2001][2],arra[2001],arr[n],k;\n\n \tstring str;\n\n \tmemset(arra,0,sizeof arra);\n\n \tfor(i=0;i>a>>b;\n\n \t\tm=a+b;\n\n \t\tarr[i]=m;\n\n \t\tarrx[m][0]=a;\n\n \t\tarrx[m][1]=b;\n\n \t\tarra[m]++;\n\n \t\tif(arra[m]==2){\n\n \t\t\tdl=1;\n\n \t\t}\n\n \t}\n\n \tif(dl==1){\n\n \t\tcout<<\"NO\"<\nusing namespace std;\n#define Ma3rof ios_base::sync_with_stdio(false);cin.tie(NULL);\nvoid solve() {\n string s;\n cin >> s;\n vector v;\n v.push_back(0);\n for (int i = 0; i < s.size(); ++i) {\n if (s[i] == 'R') {\n v.push_back(i + 1);\n }\n }\n v.push_back(s.size() + 1);\n int ans = 0;\n for (int i = 0; i < v.size() - 1; ++i) {\n ans = max(ans, v[i + 1] - v[i]);\n }\n cout << ans << '\\n';\n}\nsigned main() {\n Ma3rof\n int t = 1;\n cin >> t;\n while (t--) {\n \/* \u0648\u0645\u0627 \u062a\u062f\u0631\u064a \u0646\u0641\u0633 \u0645\u0627\u0630\u0627 \u062a\u06a9\u0633\u0628 \u063a\u062f\u0627 \u0648\u0645\u0627 \u062a\u062f\u0631\u064a \u0646\u0641\u0633 \u0628\u0627\u064a \u0627\u0631\u0636 \u062a\u0645\u0648\u062a *\/\n solve();\n }\n return 0;\n}\n","language":"cpp"} -{"contest_id":"1286","problem_id":"C1","statement":"C1. Madhouse (Easy version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is different with hard version only by constraints on total answers lengthIt is an interactive problemVenya joined a tour to the madhouse; in which orderlies play with patients the following game. Orderlies pick a string ss of length nn, consisting only of lowercase English letters. The player can ask two types of queries: ? l r \u2013 ask to list all substrings of s[l..r]s[l..r]. Substrings will be returned in random order, and in every substring, all characters will be randomly shuffled. ! s \u2013 guess the string picked by the orderlies. This query can be asked exactly once, after that the game will finish. If the string is guessed correctly, the player wins, otherwise he loses. The player can ask no more than 33 queries of the first type.To make it easier for the orderlies, there is an additional limitation: the total number of returned substrings in all queries of the first type must not exceed (n+1)2(n+1)2.Venya asked you to write a program, which will guess the string by interacting with the orderlies' program and acting by the game's rules.Your program should immediately terminate after guessing the string using a query of the second type. In case your program guessed the string incorrectly, or it violated the game rules, it will receive verdict Wrong answer.Note that in every test case the string is fixed beforehand and will not change during the game, which means that the interactor is not adaptive.InputFirst line contains number nn (1\u2264n\u22641001\u2264n\u2264100)\u00a0\u2014 the length of the picked string.InteractionYou start the interaction by reading the number nn.To ask a query about a substring from ll to rr inclusively (1\u2264l\u2264r\u2264n1\u2264l\u2264r\u2264n), you should output? l ron a separate line. After this, all substrings of s[l..r]s[l..r] will be returned in random order, each substring exactly once. In every returned substring all characters will be randomly shuffled.In the case, if you ask an incorrect query, ask more than 33 queries of the first type or there will be more than (n+1)2(n+1)2 substrings returned in total, you will receive verdict Wrong answer.To guess the string ss, you should output! son a separate line.After printing each query, do not forget to flush the output. Otherwise, you will get Idleness limit exceeded. To flush the output, you can use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. If you received - (dash) as an answer to any query, you need to terminate your program with exit code 0 (for example, by calling exit(0)). This means that there was an error in the interaction protocol. If you don't terminate with exit code 0, you can receive any unsuccessful verdict.Hack formatTo hack a solution, use the following format:The first line should contain one integer nn (1\u2264n\u22641001\u2264n\u2264100)\u00a0\u2014 the length of the string, and the following line should contain the string ss.ExampleInputCopy4\n\na\naa\na\n\ncb\nb\nc\n\ncOutputCopy? 1 2\n\n? 3 4\n\n? 4 4\n\n! aabc","tags":["brute force","constructive algorithms","interactive","math"],"code":"#include\n\nusing namespace std;\n\n#define ll long long\n\n#define N 101\n\n#define bas (133)\n\n#define yu (998244353)\n\nll n;\n\ninline void ask(ll x,ll y){\n\n\tcout<<\"? \"<>n;if(n==1){\n\n\t\task(1,1);\n\n\t\tcin>>s1[0];\n\n\t\tcout<<\"! \"<>s1[i];memset(cnt,0,sizeof(cnt));\n\n\t\tfor(int j=0;j>s2[i];memset(cnt,0,sizeof(cnt));\n\n\t\tfor(int j=0;j1)for(int g=0;g\n\n#include\n\n#include\n\n\n\n\n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\n\n\n\n\ntypedef long long ll;\n\ntypedef double ld;\n\ntypedef tree< ll , null_type , less < ll > , rb_tree_tag , tree_order_statistics_node_update > ordered_set;\n\n\n\n\n\n#define pii pair < int , int >\n\n#define pll pair< ll , ll >\n\n#define pb push_back\n\n#define S second\n\n#define F first\n\n#define sz(x) int32_t(x.size())\n\n#define int ll\n\n#define itn ll\n\n\n\nconst ll N = 5e5 + 123;\n\nconst ll mod = 998244353;\n\nconst ll INF = 1e18;\n\nconst ld eps = 5e-11;\n\n\n\nvector< int > g[N];\n\nint tin[N] , tout[N];\n\nint up[N][30];\n\nint t , h[N] , H;\n\n\n\nvoid dfs( int v , int pr = 1 )\n\n{\n\n cerr << v << '\\n';\n\n H ++;\n\n h[v] = H;\n\n\ttin[v] = ++ t;\n\n\tup[v][0] = pr;\n\n\tfor( int i = 1;i <= 22;++ i )\n\n {\n\n\/\/ cerr << i << '\\n';\n\n up[v][i] = up[up[v][i - 1]][i - 1];\n\n }\n\n\/\/ cerr << v << '\\n';\n\n\tfor( auto to : g[v] )\n\n {\n\n\t\tif( to != pr )\n\n {\n\n dfs( to , v );\n\n }\n\n\t}\n\n\ttout[v] = ++ t;\n\n\t-- H;\n\n}\n\n\n\nbool upper( int a , int b )\n\n{\n\n\treturn tin[a] <= tin[b] && tout[a] >= tout[b];\n\n}\n\n\n\nint lca( int a , int b )\n\n{\n\n\tif( upper( a , b ) )\n\n {\n\n return a;\n\n }\n\n\tif( upper( b , a ) )\n\n {\n\n return b;\n\n }\n\n\tfor( int i = 22;i >= 0;-- i )\n\n\t{\n\n\t if ( !upper( up[a][i] , b ) )\n\n {\n\n a = up[a][i];\n\n }\n\n\t}\n\n\treturn up[a][0];\n\n}\n\n\n\nint len( int v , int u )\n\n{\n\n int lc = lca( v , u );\n\n\n\n\/\/ cout << v << ' ' << u << ' ' << lc << '\\n';\n\n\/\/\n\n\/\/ cout << h[v] << ' ' << h[u] << ' ' << h[lc] << \"\\n\\n\";\n\n\n\n return h[v] + h[u] - 2 * h[lc];\n\n}\n\n\n\nint32_t main()\n\n{\n\n ios_base::sync_with_stdio(0);\n\n cin.tie(0);\n\n cout.tie(0);\n\n\n\n #ifdef LOCAL\n\n freopen(\"input.txt\", \"r\", stdin);\n\n freopen(\"output.txt\", \"w\", stdout);\n\n #else\n\n\/\/ freopen(\"lca.in\", \"r\", stdin);\n\n\/\/ freopen(\"lca.out\", \"w\", stdout);\n\n #endif\n\n\n\n int n;\n\n cin >> n;\n\n\n\n for( int i = 0;i < n - 1;++ i )\n\n {\n\n int v , u;\n\n cin >> v >> u;\n\n\n\n g[v].pb( u );\n\n g[u].pb( v );\n\n }\n\n\n\n dfs( 1 );\n\n\n\n int q;\n\n cin >> q;\n\n\n\n while( q -- )\n\n {\n\n int x , y , a , b , k;\n\n cin >> x >> y >> a >> b >> k;\n\n\n\n bool f = 0;\n\n\n\n int l1 = len( a , b );\n\n int l2 = len( a , x ) + 1 + len( b , y );\n\n int l3 = len( b , x ) + 1 + len( a , y );\n\n\n\n\/\/ cout << l1 << ' ' << l2 << ' ' << l3 << '\\n';\n\n if( ( l1 <= k && l1 % 2 == k % 2 ) || ( l2 <= k && l2 % 2 == k % 2 ) || ( l3 <= k && l3 % 2 == k % 2 ) )\n\n {\n\n cout << \"YES\\n\";\n\n }\n\n else\n\n {\n\n cout << \"NO\\n\";\n\n }\n\n }\n\n\n\n\n\n\n\n}\n\n","language":"cpp"} -{"contest_id":"1296","problem_id":"C","statement":"C. Yet Another Walking Robottime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a robot on a coordinate plane. Initially; the robot is located at the point (0,0)(0,0). Its path is described as a string ss of length nn consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point (x,y)(x,y) to the point (x\u22121,y)(x\u22121,y); 'R' (right): means that the robot moves from the point (x,y)(x,y) to the point (x+1,y)(x+1,y); 'U' (up): means that the robot moves from the point (x,y)(x,y) to the point (x,y+1)(x,y+1); 'D' (down): means that the robot moves from the point (x,y)(x,y) to the point (x,y\u22121)(x,y\u22121). The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point (xe,ye)(xe,ye), then after optimization (i.e. removing some single substring from ss) the robot also ends its path at the point (xe,ye)(xe,ye).This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string ss).Recall that the substring of ss is such string that can be obtained from ss by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of \"LURLLR\" are \"LU\", \"LR\", \"LURLLR\", \"URL\", but not \"RR\" and \"UL\".You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1\u2264t\u226410001\u2264t\u22641000) \u2014 the number of test cases.The next 2t2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer nn (1\u2264n\u22642\u22c51051\u2264n\u22642\u22c5105) \u2014 the length of the robot's path. The second line of the test case contains one string ss consisting of nn characters 'L', 'R', 'U', 'D' \u2014 the robot's path.It is guaranteed that the sum of nn over all test cases does not exceed 2\u22c51052\u22c5105 (\u2211n\u22642\u22c5105\u2211n\u22642\u22c5105).OutputFor each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers ll and rr such that 1\u2264l\u2264r\u2264n1\u2264l\u2264r\u2264n \u2014 endpoints of the substring you remove. The value r\u2212l+1r\u2212l+1 should be minimum possible. If there are several answers, print any of them.ExampleInputCopy4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR\nOutputCopy1 2\n1 4\n3 4\n-1\n","tags":["data structures","implementation"],"code":"#include\n\nusing namespace std;\n\ntypedef long long ll;\n\nll base = 1000000;\n\nll num[200001];\n\nint main()\n\n{\n\n\tios::sync_with_stdio(0);\n\n\tcin.tie(0);\n\n\tcout.tie(0);\n\n\tll T, n, len, ans, l, r, tmp;\n\n\tstring s;\n\n\tcin>>T;\n\n\twhile(T--)\n\n\t{\/\/\n\n\t\tmapmp;\n\n\t\tcin>>n;\n\n\t\tcin>>s;\n\n\t\tlen = s.length();\n\n\t\tans = 1e9;\n\n\t\t\/\/\u4e24\u4e2a\u6807\u51c6, x, y, \u627e\u4e24\u4e2a\u6807\u51c6\u90fd\u76f8\u540c\u9ad8\u5ea6\u4f4d\u7f6e\u8bb0\u5f55\n\n\t\tmp[0] = -1;\n\n\t\tfor(ll i=0;i<=len-1;++i)\n\n\t\t{\n\n\t\t\tif(s[i]=='L')\n\n\t\t\t{\n\n\t\t\t\tnum[i+1] = num[i] + base;\t\t\t\t\t\n\n\t\t\t}\n\n\t\t\telse if(s[i]=='R')\n\n\t\t\t{\n\n\t\t\t\tnum[i+1] = num[i] - base;\n\n\t\t\t}\n\n\t\t\telse if(s[i]=='U')\n\n\t\t\t{\n\n\t\t\t\tnum[i+1] = num[i] + 1;\n\n\t\t\t}\n\n\t\t\telse if(s[i]=='D')\n\n\t\t\t{\n\n\t\t\t\tnum[i+1] = num[i] - 1;\n\n\t\t\t}\n\n\t\t\ttmp = mp[num[i+1]];\n\n\t\t\tif(tmp==0)\n\n\t\t\t{\n\n\t\t\t\tmp[num[i+1]] = i+1;\n\n\t\t\t}\n\n\t\t\telse\n\n\t\t\t{\n\n\t\t\t\tif(tmp==-1)\n\n\t\t\t\t{\n\n\t\t\t\t\ttmp = 0;\n\n\t\t\t\t}\n\n\t\t\t\tif(ans>i+1-tmp)\n\n\t\t\t\t{\n\n\t\t\t\t\tans = (i+1)-tmp;\n\n\t\t\t\t\tl = tmp+1;\n\n\t\t\t\t\tr = i+1;\n\n\t\t\t\t}\n\n\t\t\t\tmp[num[i+1]] = i+1;\n\n\t\t\t}\n\n\t\t}\n\n\t\tif(ans==1e9)\n\n\t\t{\n\n\t\t\tcout<<-1<<\"\\n\";\n\n\t\t}\n\n\t\telse\n\n\t\t{\n\n\t\t\tcout<\n\n\n\nusing namespace std;\n\n\n\nconst int N = 1e6 + 5;\n\n\n\ntypedef long long ll;\n\n\n\ninline int read() {\n\n\tregister int s = 0, f = 1; register char ch = getchar();\n\n\twhile (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = getchar();\n\n\twhile (isdigit(ch)) s = (s * 10) + (ch & 15), ch = getchar();\n\n\treturn s * f;\n\n}\n\n\n\ninline int min_(int a, int b) {\n\n\treturn a < b ? a : b;\n\n}\n\n\n\ninline int max_(int a, int b) {\n\n\treturn a > b ? a : b;\n\n}\n\n\n\nstruct node {\n\n\tint l, r;\n\n\tlong long sum;\n\n\tinline node(int L, int R, long long S) : l(L), r(R), sum(S) { }\n\n\tinline node() { }\n\n\tinline void merge(const node& c) {\n\n\t\tl = min_(l, c.l);\n\n\t\tr = max_(r, c.r);\n\n\t\tsum += c.sum;\n\n\t}\n\n\tinline bool operator > (const node &c) {\n\n\t\treturn 1.0 * sum \/ (r - l + 1) > 1.0 * c.sum \/ (c.r - c.l + 1);\n\n\t}\n\n\tinline bool operator < (const node &c) {\n\n\t\treturn 1.0 * sum \/ (r - l + 1) < 1.0 * c.sum \/ (c.r - c.l + 1);\n\n\t}\n\n} stk[N];\n\n\n\nint n, a[N], top;\n\ndouble b[N];\n\nlong long sum[N];\n\n\n\nint main() {\n\n\tn = read();\n\n\tfor (int i = 1; i <= n; ++i) a[i] = read(), sum[i] = sum[i - 1] + a[i];\n\n\tfor (int i = 1; i <= n; ++i) {\n\n\t\tnode tmp = node(i, i, a[i]);\n\n\t\twhile (top && stk[top] > tmp) tmp.merge(stk[top--]);\n\n\t\tstk[++top] = tmp;\n\n\t}\n\n\tfor (int i = 1; i <= top; ++i)\n\n\t\tfor (int j = stk[i].l; j <= stk[i].r; ++j)\n\n\t\t\tprintf(\"%.9lf\\n\", 1.0 * (sum[stk[i].r] - sum[stk[i].l - 1]) \/ (stk[i].r - stk[i].l + 1));\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1295","problem_id":"C","statement":"C. Obtain The Stringtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two strings ss and tt consisting of lowercase Latin letters. Also you have a string zz which is initially empty. You want string zz to be equal to string tt. You can perform the following operation to achieve this: append any subsequence of ss at the end of string zz. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z=acz=ac, s=abcdes=abcde, you may turn zz into following strings in one operation: z=acacez=acace (if we choose subsequence aceace); z=acbcdz=acbcd (if we choose subsequence bcdbcd); z=acbcez=acbce (if we choose subsequence bcebce). Note that after this operation string ss doesn't change.Calculate the minimum number of such operations to turn string zz into string tt. InputThe first line contains the integer TT (1\u2264T\u22641001\u2264T\u2264100) \u2014 the number of test cases.The first line of each testcase contains one string ss (1\u2264|s|\u22641051\u2264|s|\u2264105) consisting of lowercase Latin letters.The second line of each testcase contains one string tt (1\u2264|t|\u22641051\u2264|t|\u2264105) consisting of lowercase Latin letters.It is guaranteed that the total length of all strings ss and tt in the input does not exceed 2\u22c51052\u22c5105.OutputFor each testcase, print one integer \u2014 the minimum number of operations to turn string zz into string tt. If it's impossible print \u22121\u22121.ExampleInputCopy3\naabce\nace\nabacaba\naax\nty\nyyt\nOutputCopy1\n-1\n3\n","tags":["dp","greedy","strings"],"code":"#include \n\nusing namespace std;\n\n#define FAST ios_base::sync_with_stdio(0); cin.tie(0);\n\ntypedef long long int ll;\n\nconst int N = 1e5+9;\n\nvoid solve()\n\n{\n\n string s1, s2;\n\n cin >> s1 >> s2;\n\n map> mp;\n\n for(int i=0;i> t;\n\n while(t--)\n\n {\n\n \/\/cout << \"Case \" << cs++ << \":\\n\";\n\n solve();\n\n }\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1305","problem_id":"B","statement":"B. Kuroni and Simple Stringstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNow that Kuroni has reached 10 years old; he is a big boy and doesn't like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence!We say that a string formed by nn characters '(' or ')' is simple if its length nn is even and positive, its first n2n2 characters are '(', and its last n2n2 characters are ')'. For example, the strings () and (()) are simple, while the strings )( and ()() are not simple.Kuroni will be given a string formed by characters '(' and ')' (the given string is not necessarily simple). An operation consists of choosing a subsequence of the characters of the string that forms a simple string and removing all the characters of this subsequence from the string. Note that this subsequence doesn't have to be continuous. For example, he can apply the operation to the string ')()(()))', to choose a subsequence of bold characters, as it forms a simple string '(())', delete these bold characters from the string and to get '))()'. Kuroni has to perform the minimum possible number of operations on the string, in such a way that no more operations can be performed on the remaining string. The resulting string does not have to be empty.Since the given string is too large, Kuroni is unable to figure out how to minimize the number of operations. Can you help him do it instead?A sequence of characters aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters.InputThe only line of input contains a string ss (1\u2264|s|\u226410001\u2264|s|\u22641000) formed by characters '(' and ')', where |s||s| is the length of ss.OutputIn the first line, print an integer kk \u00a0\u2014 the minimum number of operations you have to apply. Then, print 2k2k lines describing the operations in the following format:For each operation, print a line containing an integer mm \u00a0\u2014 the number of characters in the subsequence you will remove.Then, print a line containing mm integers 1\u2264a1\n\n\n\n#define turbo() \\\n\n ios_base::sync_with_stdio(0); \\\n\n cin.tie(0); \\\n\n cout.tie(0);\n\n\n\n#define ll long long \n\nconst ll mod=1000000007;\n\nconst ll INF= 1e9+7;\n\n#define pb push_back\n\n#define pp pop_back\n\n#define mp make_pair\n\n#define pf push_front\n\n#define vll vector\n\n#define get(a,n) vll a(n); for(int i=0;i>a[i];\n\n#define print(a,n) for(int i=0;i());\n\n\n\n#define tc() long long int t;cin>>t;while(t--)\n\n#define rep(var,start,to) for(ll var=start;var= c; a--)\n\n#define yes cout<<\"YES\"<>s;\n\n vector x,x1;\n\n vector ans;\n\n for(int i=0;ix1[i]){\n\n break;\n\n }\n\n else{\n\n ans.pb(x[i]);\n\n ans.pb(x1[i]);\n\n }\n\n }\n\n if(ans.size()==0){\n\n cout<<0<\n\n#define ll long long\n\n#define lc(x) ((x) << 1)\n\n#define rc(x) ((x) << 1 | 1)\n\n#define ru(i, l, r) for (int i = (l); i <= (r); i++)\n\n#define rd(i, r, l) for (int i = (r); i >= (l); i--)\n\n#define mid ((l + r) >> 1)\n\n#define pii pair\n\n#define mp make_pair\n\n#define fi first\n\n#define se second\n\n#define sz(s) (int)s.size()\n\n#define maxn 1000005\n\nusing namespace std;\n\ninline int read() {\n\n\tint x = 0, w = 0; char ch = getchar();\n\n\twhile(!isdigit(ch)) {w |= ch == '-'; ch = getchar();}\n\n\twhile(isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}\n\n\treturn w ? -x : x;\n\n}\n\nint n; ll a[maxn]; int sta[maxn], top;\n\nint chk(int x, int y, int z) {\n\n\treturn (ll)(a[z] - a[y]) * (z - x) <= (ll)(a[z] - a[x]) * (z - y);\n\n}\n\nint main() {\n\n\tn = read();\n\n\tru(i, 1, n) a[i] = read() + a[i - 1];\n\n\tru(i, 1, n) {\n\n\t\twhile(top > 0 && chk(sta[top - 1], sta[top], i)) top--;\n\n\t\tsta[++top] = i;\n\n\t} \n\n\/\/\tru(i, 1, top) printf(\"%d \", sta[i]); printf(\"\\n\");\n\n\tru(i, 1, top) {\n\n\t\tru(j, sta[i - 1] + 1, sta[i]) {\n\n\t\t\tprintf(\"%.10f\\n\", (double)(a[sta[i]] - a[sta[i - 1]]) \/ (sta[i] - sta[i - 1]));\n\n\t\t}\n\n\t}\n\n\treturn 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1307","problem_id":"A","statement":"A. Cow and Haybalestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of nn haybale piles on the farm. The ii-th pile contains aiai haybales. However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices ii and jj (1\u2264i,j\u2264n1\u2264i,j\u2264n) such that |i\u2212j|=1|i\u2212j|=1 and ai>0ai>0 and apply ai=ai\u22121ai=ai\u22121, aj=aj+1aj=aj+1. She may also decide to not do anything on some days because she is lazy.Bessie wants to maximize the number of haybales in pile 11 (i.e. to maximize a1a1), and she only has dd days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile 11 if she acts optimally!InputThe input consists of multiple test cases. The first line contains an integer tt (1\u2264t\u22641001\u2264t\u2264100) \u00a0\u2014 the number of test cases. Next 2t2t lines contain a description of test cases \u00a0\u2014 two lines per test case.The first line of each test case contains integers nn and dd (1\u2264n,d\u22641001\u2264n,d\u2264100) \u2014 the number of haybale piles and the number of days, respectively. The second line of each test case contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u22641000\u2264ai\u2264100) \u00a0\u2014 the number of haybales in each pile.OutputFor each test case, output one integer: the maximum number of haybales that may be in pile 11 after dd days if Bessie acts optimally.ExampleInputCopy3\n4 5\n1 0 3 2\n2 2\n100 1\n1 8\n0\nOutputCopy3\n101\n0\nNoteIn the first test case of the sample; this is one possible way Bessie can end up with 33 haybales in pile 11: On day one, move a haybale from pile 33 to pile 22 On day two, move a haybale from pile 33 to pile 22 On day three, move a haybale from pile 22 to pile 11 On day four, move a haybale from pile 22 to pile 11 On day five, do nothing In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile 22 to pile 11 on the second day.","tags":["greedy","implementation"],"code":"\/\/ LUOGU_RID: 99550923\n#include\n\nusing namespace std;\n\nint a[105];\n\nint main()\n\n{\n\n ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);\n\n int t,n,k;\n\n cin>>t;\n\n while(t--)\n\n {\n\n cin>>n>>k;\n\n int sum=0,ans=0;\n\n for(int i=1;i<=n;i++)\n\n {\n\n cin>>a[i];\n\n if(i>1&&k>0)\n\n {\n\n sum=min(k\/(i-1),a[i]);\n\n ans+=sum;\n\n k-=(i-1)*a[i];\n\n }\n\n if(i==1) ans+=a[i];\n\n }\n\n cout<\n\nusing namespace std;\n\n#define int long long\n\n#define mod 1000000007\n\n#define mod2 998244353\n\n#define endl '\\n' \n\n#define sz(x) (int)(x.size())\n\n#define all(x) x.begin(),x.end()\n\n#define print(x) {for(auto v: x) {cout << v<< ' ';} cout << endl;}\n\n#define printp(x) {for(auto v: x) {cout << v.first << ':' << v.second << ' ';} cout << endl;}\n\n\n\nsigned main()\n\n{\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(NULL);cout.tie(NULL);\n\n int n; cin >> n;\n\n string s; cin >> s;\n\n int t = 2;\n\n int org = n;\n\n while (1){\n\n n = sz(s);\n\n int f = 1;\n\n for (int i=25;i>=0;i--){\n\n for (int j=0;j=0 and (s[j-1]-'a')+1==i) or (j+1\n\ntypedef long long ll;\n\ntypedef unsigned long long ull;\n\nusing namespace std;\n\nconst ll mod = 1e11;\n\nconst int mm = 2e5 + 10;\n\nstruct node{\n\n\tll p,id,k;\n\n\tnode(){}\n\n\tnode(ll p,ll id,ll k):p(p),id(id),k(k){}\n\n\tfriend bool operator < (node a,node b){\n\n\t\treturn a.p==b.p?a.k>n;\n\n\tfor(int i=1;i<=n;i++){\n\n\t\tval[i]=(ull)(rand()<<31|rand());\n\n\t\tll x,y,z,w;\n\n\t\tcin>>x>>y>>z>>w;\n\n\t\ta1[i*2-1]=node(x,i,0);\n\n\t\ta1[i*2]=node(y,i,1);\n\n\t\ta2[i*2-1]=node(z,i,0);\n\n\t\ta2[i*2]=node(w,i,1);\n\n\t}int N=n*2;\n\n\tsort(a1+1,a1+1+N);\n\n\tsort(a2+1,a2+1+N);\n\n\tll Xor=0;\n\n\tfor(int i=1;i<=N;i++){\n\n\t\tif(a1[i].k)Xor^=val[a1[i].id];\n\n\t\telse s[a1[i].id]^=Xor;\n\n\t}Xor=0;\n\n\tfor(int i=N;i>=1;i--){\n\n\t\tif(!a1[i].k)Xor^=val[a1[i].id];\n\n\t\telse s[a1[i].id]^=Xor;\n\n\t}Xor=0;\n\n\tfor(int i=1;i<=N;i++){\n\n\t\tif(a2[i].k)Xor^=val[a2[i].id];\n\n\t\telse s[a2[i].id]^=Xor;\n\n\t}Xor=0;\n\n\tfor(int i=N;i>=1;i--){\n\n\t\tif(!a2[i].k)Xor^=val[a2[i].id];\n\n\t\telse s[a2[i].id]^=Xor;\n\n\t}\n\n\tfor(int i=1;i<=n;i++){\n\n\t\tif(s[i]>0){\n\n\t\t\tcout<<\"NO\";\n\n\t\t\treturn 0;\n\n\t\t}\n\n\t}cout<<\"YES\";\n\n}\n\n","language":"cpp"} -{"contest_id":"1320","problem_id":"E","statement":"E. Treeland and Virusestime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere are nn cities in Treeland connected with n\u22121n\u22121 bidirectional roads in such that a way that any city is reachable from any other; in other words, the graph of cities and roads is a tree. Treeland is preparing for a seasonal virus epidemic, and currently, they are trying to evaluate different infection scenarios.In each scenario, several cities are initially infected with different virus species. Suppose that there are kiki virus species in the ii-th scenario. Let us denote vjvj the initial city for the virus jj, and sjsj the propagation speed of the virus jj. The spread of the viruses happens in turns: first virus 11 spreads, followed by virus 22, and so on. After virus kiki spreads, the process starts again from virus 11.A spread turn of virus jj proceeds as follows. For each city xx not infected with any virus at the start of the turn, at the end of the turn it becomes infected with virus jj if and only if there is such a city yy that: city yy was infected with virus jj at the start of the turn; the path between cities xx and yy contains at most sjsj edges; all cities on the path between cities xx and yy (excluding yy) were uninfected with any virus at the start of the turn.Once a city is infected with a virus, it stays infected indefinitely and can not be infected with any other virus. The spread stops once all cities are infected.You need to process qq independent scenarios. Each scenario is described by kiki virus species and mimi important cities. For each important city determine which the virus it will be infected by in the end.InputThe first line contains a single integer nn (1\u2264n\u22642\u22c51051\u2264n\u22642\u22c5105)\u00a0\u2014 the number of cities in Treeland.The following n\u22121n\u22121 lines describe the roads. The ii-th of these lines contains two integers xixi and yiyi (1\u2264xi,yi\u2264n1\u2264xi,yi\u2264n)\u00a0\u2014 indices of cities connecting by the ii-th road. It is guaranteed that the given graph of cities and roads is a tree.The next line contains a single integer qq (1\u2264q\u22642\u22c51051\u2264q\u22642\u22c5105)\u00a0\u2014 the number of infection scenarios. qq scenario descriptions follow.The description of the ii-th scenario starts with a line containing two integers kiki and mimi (1\u2264ki,mi\u2264n1\u2264ki,mi\u2264n)\u00a0\u2014 the number of virus species and the number of important cities in this scenario respectively. It is guaranteed that \u2211qi=1ki\u2211i=1qki and \u2211qi=1mi\u2211i=1qmi do not exceed 2\u22c51052\u22c5105.The following kiki lines describe the virus species. The jj-th of these lines contains two integers vjvj and sjsj (1\u2264vj\u2264n1\u2264vj\u2264n, 1\u2264sj\u22641061\u2264sj\u2264106)\u00a0\u2013 the initial city and the propagation speed of the virus species jj. It is guaranteed that the initial cities of all virus species within a scenario are distinct.The following line contains mimi distinct integers u1,\u2026,umiu1,\u2026,umi (1\u2264uj\u2264n1\u2264uj\u2264n)\u00a0\u2014 indices of important cities.OutputPrint qq lines. The ii-th line should contain mimi integers\u00a0\u2014 indices of virus species that cities u1,\u2026,umiu1,\u2026,umi are infected with at the end of the ii-th scenario.ExampleInputCopy7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n3\n2 2\n4 1\n7 1\n1 3\n2 2\n4 3\n7 1\n1 3\n3 3\n1 1\n4 100\n7 100\n1 2 3\nOutputCopy1 2\n1 1\n1 1 1\n","tags":["data structures","dfs and similar","dp","shortest paths","trees"],"code":"\/**\n\n * Author: LZVSDY\n\n**\/\n\n#include \n\nusing namespace std;\n\n#define FI first\n\n#define SE second\n\n#define MP make_pair\n\n#define PB push_back\n\n\/*\n\nfreopen(\"orxor.in\", \"r\", stdin);\n\n*\/\n\ntypedef long long LL;\n\ntypedef unsigned long long int ULL;\n\ntypedef pair PII;\n\ntypedef vector VI;\n\nconst int INF = 0x3f3f3f3f;\n\nconst LL LNF = 0x3f3f3f3f3f3f3f3f;\n\nconst int mod = 998244353;\n\nconst int N = 2500010;\n\nconst int M = 5e5 + 10;\n\nint h[N], e[M], ne[M], idx;\n\nint dep[N], dfn[N], f[N][19], sz[N], son[N], ti;\n\nint st[N], s[N], tp;\n\nbool vis[N], use[N];\n\nPII dist[N];\n\nint n, k, m;\n\nstruct V{\n\n int v, s, col;\n\n}v[N];\n\nvoid add(int a, int b) {\n\n e[idx] = b, ne[idx] = h[a], h[a] = idx ++;\n\n}\n\nvoid dfs1(int u, int fa) {\n\n sz[u] = 1;\n\n for (int i = h[u]; ~i; i = ne[i]) {\n\n int j = e[i];\n\n if (j == fa) continue;\n\n f[j][0] = u;\n\n dep[j] = dep[u] + 1;\n\n dfs1(j, u);\n\n sz[u] += sz[j];\n\n if (sz[u] > sz[son[u]]) son[u] = j;\n\n }\n\n}\n\nvoid dfs2(int u, int fa) {\n\n dfn[u] = ++ ti;\n\n if (son[u]) {\n\n dfs2(son[u], u);\n\n for (int i = h[u]; ~i; i = ne[i]) {\n\n int j = e[i];\n\n if (j == fa || j == son[u]) continue;\n\n dfs2(j, u);\n\n }\n\n }\n\n}\n\nvoid init() {\n\n for (int j = 1; j < 19; j ++ )\n\n for (int i = 1; i <= n; i ++ )\n\n f[i][j] = f[f[i][j - 1]][j - 1];\n\n}\n\nint lca(int a, int b) {\n\n if (dep[a] < dep[b]) swap(a, b);\n\n for (int k = 18; k >= 0; k -- )\n\n if (dep[f[a][k]] >= dep[b])\n\n a = f[a][k];\n\n if (a == b) return a;\n\n for (int k = 18; k >= 0; k -- ) \n\n if (f[a][k] != f[b][k])\n\n a = f[a][k], b = f[b][k];\n\n return f[a][0];\n\n}\n\nbool cmp1(V &a, V &b) {\n\n return dfn[a.v] < dfn[b.v];\n\n}\n\nbool cmp2(V &a, V &b) {\n\n return a.col < b.col;\n\n}\n\nvoid ins(int x) {\n\n if (!tp) {\n\n st[++ tp] = x;\n\n return;\n\n }\n\n int anc = lca(st[tp], x);\n\n while (tp > 1 && dep[st[tp - 1]] > dep[anc]) {\n\n \/\/ cout << st[tp - 1] << ' ' << st[tp] << endl;\n\n\t\tadd(st[tp - 1], st[tp]), add(st[tp], st[tp - 1]);\n\n tp --;\n\n }\n\n if (dep[st[tp]] > dep[anc]) {\n\n \/\/ cout << anc << ' ' << st[tp] << endl;\n\n add(anc, st[tp]), add(st[tp], anc);\n\n tp --;\n\n }\n\n if (!tp || st[tp] != anc) st[++ tp] = anc;\n\n st[++ tp] = x;\n\n}\n\nint query(int v, int u, int op) {\n\n int anc = lca(u, v);\n\n int len = dep[u] + dep[v] - 2 * dep[anc];\n\n return (len + s[op] - 1) \/ s[op];\n\n}\n\nstruct Node{\n\n int rounds, col, fa, now;\n\n bool operator> (const Node& w) const {\n\n if (rounds != w.rounds) return rounds > w.rounds;\n\n else if (col != w.col) return col > w.col;\n\n else if (fa != w.fa) return fa > w.fa;\n\n else return now > w.now;\n\n }\n\n};\n\nvoid dij() {\n\n priority_queue, greater> q;\n\n sort(v + 1, v + k + m + 1, cmp2);\n\n for (int i = 1; i <= k; i ++ ) {\n\n dist[v[i].v] = {0, v[i].col};\n\n q.push({0, v[i].col, v[i].v, v[i].v});\n\n }\n\n while (!q.empty()) {\n\n auto t = q.top(); q.pop();\n\n if (vis[t.now]) continue;\n\n vis[t.now] = 1;\n\n for (int i = h[t.now]; ~i; i = ne[i]) {\n\n int j = e[i];\n\n int len = query(j, t.fa, t.col);\n\n if (dist[j].FI > len) {\n\n dist[j] = {len, dist[t.now].SE};\n\n \/\/ cout << \"di\" << t.now << ' ' << j << ' ' << dist[j].FI << ' ' << dist[j].SE << endl;\n\n q.push({len, dist[j].SE, t.fa, j});\n\n } else if (dist[j].FI == len && dist[j].SE > dist[t.now].SE) {\n\n dist[j].SE = dist[t.now].SE;\n\n \/\/ cout << \"di\" << t.now << ' ' << j << ' ' << dist[j].FI << ' ' << dist[j].SE << endl;\n\n q.push({len, dist[j].SE, t.fa, j});\n\n }\n\n }\n\n }\n\n}\n\nvoid build(int k, int m) {\n\n for (int i = 1; i <= k; i ++ ) {\n\n cin >> v[i].v >> v[i].s;\n\n s[i] = v[i].s;\n\n v[i].col = i;\n\n }\n\n for (int i = 1; i <= m; i ++ ) {\n\n cin >> v[i + k].v;\n\n v[i + k].col = i + k;\n\n }\n\n sort(v + 1, v + m + k + 1, cmp1);\n\n \/\/ for (int i = 1; i <= k + m; i ++ )\n\n \/\/ cout << v[i].v << endl;\n\n tp = 0;\n\n if (v[1].v != 1) st[++ tp] = 1;\n\n for (int i = 1; i <= k + m; i ++ ) {\n\n if (use[v[i].v]) continue;\n\n ins(v[i].v);\n\n \/\/ cout << v[i].v << endl;\n\n use[v[i].v] = 1;\n\n }\n\n for (int i = 1; i <= k + m; i ++ )\n\n use[v[i].v] = 0;\n\n while (-- tp) {\n\n \/\/ cout << st[tp] << ' ' << st[tp + 1] << endl;\n\n add(st[tp], st[tp + 1]), add(st[tp + 1], st[tp]);\n\n }\n\n dij();\n\n}\n\nvoid recover(int u, int fa) {\n\n \/\/ cout << u << ' ' << dist[u].FI << ' ' << dist[u].SE << endl;\n\n dist[u].FI = dist[u].SE = INF;\n\n vis[u] = 0;\n\n for (int i = h[u]; ~i; i = ne[i]) {\n\n int j = e[i];\n\n if (j == fa) continue;\n\n recover(j, u);\n\n }\n\n h[u] = -1;\n\n}\n\nint main() {\n\n\/\/\t#ifdef lz\n\n\/\/\t\tfreopen(\"lz.in\", \"r\", stdin);\n\n\/\/ #endif\n\n\tios::sync_with_stdio(false);\n\n cin.tie(nullptr);\n\n cin >> n;\n\n for (int i = 1; i <= n; i ++ ) dist[i].FI = dist[i].SE = INF;\n\n for (int i = 1; i <= n; i ++ ) h[i] = -1;\n\n for (int i = 1; i < n; i ++ ) {\n\n int a, b;\n\n cin >> a >> b;\n\n add(a, b), add(b, a);\n\n }\n\n dep[1] = 1;\n\n dfs1(1, -1);\n\n dfs2(1, -1);\n\n init();\n\n for (int i = 1; i <= n; i ++ ) h[i] = -1;\n\n int q;\n\n cin >> q;\n\n while (q -- ) {\n\n cin >> k >> m;\n\n idx = 0;\n\n build(k, m);\n\n for (int i = k + 1; i <= k + m; i ++ )\n\n cout << dist[v[i].v].SE << ' ';\n\n cout << endl;\n\n recover(1, -1);\n\n }\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1305","problem_id":"B","statement":"B. Kuroni and Simple Stringstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNow that Kuroni has reached 10 years old; he is a big boy and doesn't like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence!We say that a string formed by nn characters '(' or ')' is simple if its length nn is even and positive, its first n2n2 characters are '(', and its last n2n2 characters are ')'. For example, the strings () and (()) are simple, while the strings )( and ()() are not simple.Kuroni will be given a string formed by characters '(' and ')' (the given string is not necessarily simple). An operation consists of choosing a subsequence of the characters of the string that forms a simple string and removing all the characters of this subsequence from the string. Note that this subsequence doesn't have to be continuous. For example, he can apply the operation to the string ')()(()))', to choose a subsequence of bold characters, as it forms a simple string '(())', delete these bold characters from the string and to get '))()'. Kuroni has to perform the minimum possible number of operations on the string, in such a way that no more operations can be performed on the remaining string. The resulting string does not have to be empty.Since the given string is too large, Kuroni is unable to figure out how to minimize the number of operations. Can you help him do it instead?A sequence of characters aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters.InputThe only line of input contains a string ss (1\u2264|s|\u226410001\u2264|s|\u22641000) formed by characters '(' and ')', where |s||s| is the length of ss.OutputIn the first line, print an integer kk \u00a0\u2014 the minimum number of operations you have to apply. Then, print 2k2k lines describing the operations in the following format:For each operation, print a line containing an integer mm \u00a0\u2014 the number of characters in the subsequence you will remove.Then, print a line containing mm integers 1\u2264a1\n\nusing namespace std;\n\n\n\n#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\n\n#define FOR(i,n) for(ll i = 0 ; i>s;\n\n\n\n vector v;\n\n \n\n int l = 0;\n\n int r = s.length()-1;\n\n\n\n while(l < r){\n\n \tif(s[l]=='(' and s[r]==')'){\n\n \t v.pb(l+1); v.pb(r+1);\n\n \t l++;r--;\n\n \t \n\n \t}\n\n \tif(s[l] == ')') l++;\n\n \tif(s[r] == '(') r--;\n\n\n\n\n\n\n\n }\n\n sort(v.begin(), v.end());\n\n if(v.size()) cout<<1<\n\n#define eps 1e-9\n\n#define emailam ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\n#define endl '\\n'\n\n#define ll long long\n\n#define ull unsigned long long\n\n#define MAX 200010\n\n#define pb push_back\n\n#define all(a) a.begin(),a.end()\n\n#define pf push_front\n\n#define fi first\n\n#define se second\n\n#define pii pair\n\nconst long long INF = LLONG_MAX;\n\nusing namespace std;\n\nconst int N=1e7+3,M=10;\n\nconst int mod=998244353;\n\n\/*----------------------------------------------------------------*\/\n\nint dx[] = {+0, +0, -1, +1, +1, +1, -1, -1};\n\nint dy[] = {-1, +1, +0, +0, +1, -1, +1, -1};\n\n\/*----------------------------------------------------------------*\/\n\nvoid READ(){\n\n#ifndef ONLINE_JUDGE\n\n freopen(\"input.txt\", \"r\", stdin), freopen(\"output.txt\", \"w\", stdout);\n\n#endif\n\n}\n\n\n\n\n\nvoid solve(){\n\n int n,a,b;\n\n cin>>n>>a>>b;\n\n int x=a+b;\n\n if(x>n){\n\n int y=n-x%(n+1);\n\n cout<>t;\n\n while(t--){\n\n solve();\n\n }\n\n}","language":"cpp"} -{"contest_id":"1286","problem_id":"A","statement":"A. Garlandtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVadim loves decorating the Christmas tree; so he got a beautiful garland as a present. It consists of nn light bulbs in a single row. Each bulb has a number from 11 to nn (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 22). For example, the complexity of 1 4 2 3 5 is 22 and the complexity of 1 3 5 7 6 4 2 is 11.No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.InputThe first line contains a single integer nn (1\u2264n\u22641001\u2264n\u2264100)\u00a0\u2014 the number of light bulbs on the garland.The second line contains nn integers p1,\u00a0p2,\u00a0\u2026,\u00a0pnp1,\u00a0p2,\u00a0\u2026,\u00a0pn (0\u2264pi\u2264n0\u2264pi\u2264n)\u00a0\u2014 the number on the ii-th bulb, or 00 if it was removed.OutputOutput a single number\u00a0\u2014 the minimum complexity of the garland.ExamplesInputCopy5\n0 5 0 2 3\nOutputCopy2\nInputCopy7\n1 0 0 5 0 0 2\nOutputCopy1\nNoteIn the first example; one should place light bulbs as 1 5 4 2 3. In that case; the complexity would be equal to 2; because only (5,4)(5,4) and (2,3)(2,3) are the pairs of adjacent bulbs that have different parity.In the second case, one of the correct answers is 1 7 3 5 6 4 2. ","tags":["dp","greedy","sortings"],"code":"#include \n\nusing namespace std;\n\n\n\n#define int long long\n\nint tt;\n\nvoid solve();\n\n\n\nvoid IO()\n\n{\n\n\tios_base::sync_with_stdio(false);\n\n\tcin.tie(NULL);\n\n\tcout.tie(NULL);\n\n}\n\n\n\nvoid testcase(int t)\n\n{\n\n\tcin >> t;\n\n\twhile (t--) solve();\n\n}\n\n\n\ntypedef long long ll;\n\ntypedef long double lld;\n\n\n\n#define ff first\n\n#define ss second\n\n#define pi pair\n\n#define vi vector\n\n#define vb vector\n\n#define vs vector\n\n#define vc vector\n\n#define pv pair\n\n#define vpi vector\n\n#define vvi vector\n\n#define vvb vector\n\n#define vvc vector\n\n#define vvvi vector\n\n#define pb push_back\n\n#define mp make_pair\n\n#define arr(k) array\n\n#define var(k) vector\n\n#define all(v) v.begin(), v.end()\n\n#define len(v) (int)v.size()\n\n#define rep(i, a, b) for(int i=a; i> a;}\n\nvoid read(string &s) {cin >> s;}\n\nvoid read(double &a) {cin >> a;}\n\nvoid read(lld &a) {cin >> a;}\n\ntemplate void read(pair &a) {read(a.first), read(a.second);}\n\ntemplatevoid read(x &a) {for (auto &i : a) read(i);}\n\ntemplate void read(x& a, y&... b) {read(a); read(b...);}\n\ntemplate void cmin(T&a, T b) {a = min(a, b);}\n\ntemplate void cmax(T&a, T b) {a = max(a, b);}\n\n\n\n\/* ------------------------------------------------------------------ *\/\n\n\n\n\n\n\/* ------------------------------------- DEBUG STARTS ------------------------------- *\/\n\n\n\n#ifndef ONLINE_JUDGE\n\n#define dbg(x) cerr << #x << \" = \"; _print(x); cerr << endl;\n\n#else\n\n#define dbg(x)\n\n#endif\n\n\n\nvoid print(int t) {cout << t;}\n\nvoid print(string t) {cout << t;}\n\nvoid print(char t) {cout << t;}\n\nvoid print(lld t) {cout << t;}\n\nvoid print(double t) {cout << t;}\n\nvoid print(bool t) {cout << t;}\n\nvoid print(deque t) {while (not t.empty()) {cout << t.front() << ' '; t.pop_front();}}\n\nvoid print(queue t) {while (not t.empty()) {cout << t.front() << ' '; t.pop();}}\n\n\n\nvoid _print(int t) {cerr << t;}\n\nvoid _print(string t) {cerr << t;}\n\nvoid _print(char t) {cerr << t;}\n\nvoid _print(lld t) {cerr << t;}\n\nvoid _print(double t) {cerr << t;}\n\nvoid _print(bool t) {cerr << t;}\n\nvoid _print(deque t) {while (not t.empty()) {cerr << t.front() << ' '; t.pop_front();}}\n\nvoid _print(queue t) {while (not t.empty()) {cerr << t.front() << ' '; t.pop();}}\n\n\n\ntemplate void print(pair p);\n\ntemplate void print(vector v);\n\ntemplate void print(pair p) {print(p.ff); cout << \" \"; print(p.ss);}\n\ntemplate void print(vector v) {for (T i : v) {print(i); cout << \" \";}}\n\n\n\ntemplate void _print(pair p);\n\ntemplate void _print(vector v);\n\ntemplate void _print(set v);\n\ntemplate void _print(map v);\n\ntemplate void _print(multiset v);\n\ntemplate void _print(pair p) {cerr << \"{\"; _print(p.ff); cerr << \",\"; _print(p.ss); cerr << \"}\";}\n\ntemplate void _print(vector v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\ntemplate void _print(set v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\ntemplate void _print(multiset v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\ntemplate void _print(map v) {cerr << \"[ \"; for (auto i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\n\n\n\/* ------------------------------------- DEBUG ENDS --------------------------------- *\/\n\n\n\nint n, m, l, r;\n\nint dp[101][101][101][3];\n\nvi a;\n\n\n\n\/\/ we were getting WA due to -1 in parity\n\n\n\n\/\/ rec(i) -> tot complexity in [i..n]\n\nint rec(int i, int odd_left, int even_left, int prev_parity)\n\n{\n\n if (i==n) return 0;\n\n\n\n if (dp[i][odd_left][even_left][prev_parity+1] != -1)\n\n return dp[i][odd_left][even_left][prev_parity+1];\n\n\n\n if (a[i] != 0)\n\n {\n\n return dp[i][odd_left][even_left][prev_parity+1] = (a[i]%2==prev_parity or i==0) ? \n\n rec(i+1, odd_left, even_left, a[i]%2) :1 + rec(i+1, odd_left, even_left, a[i]%2);\n\n }\n\n\n\n int op1 = 1e9, op2 = 1e9;\n\n \/\/ assign odd parity\n\n if (odd_left > 0) \n\n {\n\n op1 = (prev_parity==1 or i==0) ? rec(i+1, odd_left-1, even_left, 1) : \n\n 1+rec(i+1, odd_left-1, even_left,1);\n\n }\n\n\n\n \/\/ assign even parity\n\n if (even_left > 0) \n\n {\n\n op2 = (prev_parity==0 or i==0) ? rec(i+1, odd_left, even_left-1, 0) : \n\n 1+rec(i+1, odd_left, even_left-1,0);\n\n }\n\n return dp[i][odd_left][even_left][prev_parity+1] = min(op1, op2);\n\n}\n\n\n\nvoid solve()\n\n{\n\n\tcin >> n; a.resize(n);\n\n vi vis(n+1, 0);\n\n int odd=0, even=0;\n\n rep(i,0,n)\n\n {\n\n cin >> a[i];\n\n vis[a[i]] = 1;\n\n }\n\n\n\n rep(i,1,n+1)\n\n {\n\n if (vis[i] == 0)\n\n {\n\n if (i&1) odd++;\n\n else even++;\n\n }\n\n }\n\n memset(dp, -1, sizeof dp);\n\n cout << rec(0,odd, even, -1);\n\n}\n\n\n\nsigned main()\n\n{\n\n\tIO();\n\n\n\n\t\/\/ testcase(tt);\n\n\tsolve();\n\n}","language":"cpp"} -{"contest_id":"1312","problem_id":"B","statement":"B. Bogosorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a1,a2,\u2026,ana1,a2,\u2026,an. Array is good if for each pair of indexes i\n\nusing namespace std;\n\nint main()\n\n{\n\n int t;\n\n cin >> t;\n\n while (t--)\n\n {\n\n int n;\n\n cin>>n;\n\n int a[n];\n\n for (int i = 0; i < n; i++)\n\n {\n\n \/* code *\/\n\n cin>>a[i];\n\n }\n\n sort(a,a+n,greater());\n\n for (int i = 0; i < n; i++)\n\n {\n\n \/* code *\/\n\n cout<kk\u2032>k. The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks.InputThe first line contains integer nn (1\u2264n\u226415001\u2264n\u22641500) \u2014 the length of the given array. The second line contains the sequence of elements a[1],a[2],\u2026,a[n]a[1],a[2],\u2026,a[n] (\u2212105\u2264ai\u2264105\u2212105\u2264ai\u2264105).OutputIn the first line print the integer kk (1\u2264k\u2264n1\u2264k\u2264n). The following kk lines should contain blocks, one per line. In each line print a pair of indices li,rili,ri (1\u2264li\u2264ri\u2264n1\u2264li\u2264ri\u2264n) \u2014 the bounds of the ii-th block. You can print blocks in any order. If there are multiple answers, print any of them.ExamplesInputCopy7\n4 1 2 2 1 5 3\nOutputCopy3\n7 7\n2 3\n4 5\nInputCopy11\n-5 -4 -3 -2 -1 0 1 2 3 4 5\nOutputCopy2\n3 4\n1 1\nInputCopy4\n1 1 1 1\nOutputCopy4\n4 4\n1 1\n2 2\n3 3\n","tags":["data structures","greedy"],"code":"#include \n\nusing namespace std;\n\ntypedef long long ll;\n\ntemplate using vc=vector;\n\ntemplate using vvc=vc>;\n\nusing vi = vc;\n\nusing vl = vc;\n\nusing vvi = vc;\n\nusing vvl = vc;\n\nusing pii = pair;\n\nusing pll = pair;\n\n#define inf 0x3f3f3f3f\n\n#define llinf 0x3f3f3f3f3f3f3f3f\n\n#define ALL(x) (x).begin(),(x).end()\n\n#define rALL(x) (x).rbegin(),(x).rend()\n\n#define rev(x) reverse(ALL(x))\n\n#define srt(x) sort(ALL(x))\n\n#define rsrt(x) sort(rALL(x))\n\n#define rb(_, l, r) for(int _= l; _ <= r; ++_)\n\n#define rep(_, l, r) for(int _ = l; _ < r; ++_)\n\n#define br(_, r, l) for(int _ = r; _ >= l; _--)\n\n#define sz(x) (int)(x.size())\n\ntemplate bool chmax(t&a,u b){if(a bool chmin(t&a,u b){if(b void in(vector&a) {for(auto& e: a) cin>> e;}\n\nmt19937_64 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());\n\n\n\nvoid p(int x) { if (x) cout << \"YES\" << '\\n'; else cout << \"NO\" << '\\n'; }\n\n\n\ntemplate void printv(t x, char delimiter = ',', bool needBrace = true) {\n\n needBrace && cout << \"{\"; \n\n for(auto it = x.begin(); it != x.end(); ) {\n\n cout << *it;\n\n it = next(it);\n\n if (it != x.end()) {\n\n cout << delimiter;\n\n } else {\n\n needBrace && cout << \"}\";\n\n }\n\n }\n\n cout << \"\\n\";\n\n}\n\n\n\n#define TEST\n\n#ifdef TEST\n\n #define pr(...) printf(__VA_ARGS__);\n\n #define db(x, ...) debug##x(__VA_ARGS__)\n\n #define ldb(parm...) do {cout << \"Line:\" << __LINE__ << \" \"; db(parm);} while(0)\n\n #define debugv(v...) do {printv(v);} while(0)\n\n #define debug1(a) cout << #a << \" = \" << a << ' ';\n\n #define debug2(a,b) do {debug1(a); debug1(b); cout << '\\n';} while(0)\n\n #define debug3(a,b,c) do {debug1(a); debug2(b, c);} while(0)\n\n #define debug4(a,b,c,d) do {debug1(a); debug3(b, c, d);} while(0)\n\n#else\n\n #define db(...) \"Your attitude towards suffering is the melody of life.\"; \n\n#endif\n\n\/*\n\nstr -> str.c_str()\n\ndb(v, a) -> printv()\n\ndb(1, x) -> print x\n\ndb(2, x, y) -> print x, y\n\n*\/\n\n\n\n\n\nconst char nl = '\\n';\n\n#define SINGLE\n\nvoid solve() {\n\n int n;\n\n cin >> n;\n\n vi a(n);\n\n map> mp;\n\n rep (i, 0, n) {\n\n cin >> a[i];\n\n int cur = 0;\n\n br (j, i, 0) {\n\n cur += a[j];\n\n mp[cur].push_back({j + 1, i + 1});\n\n }\n\n }\n\n vc ans;\n\n for (auto&[k, v]: mp) {\n\n vc t;\n\n int pre = -1;\n\n for (auto&[l, r] : v) {\n\n if (l > pre) {\n\n t.emplace_back(l, r); pre = r;\n\n }\n\n }\n\n if (sz(t) > sz(ans)) ans = t;\n\n }\n\n cout << sz(ans) << nl;\n\n for (auto&[l, r]: ans) {\n\n cout << l << ' ' << r << nl;\n\n }\n\n}\n\n\n\n\n\nint main() {\n\n ios::sync_with_stdio(0);cin.tie(0);\n\n#ifdef SINGLE\n\n solve();\n\n#else\n\n int T; cin >> T; while (T--) solve();\n\n#endif\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1284","problem_id":"D","statement":"D. New Year and Conferencetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputFilled with optimism; Hyunuk will host a conference about how great this new year will be!The conference will have nn lectures. Hyunuk has two candidate venues aa and bb. For each of the nn lectures, the speaker specified two time intervals [sai,eai][sai,eai] (sai\u2264eaisai\u2264eai) and [sbi,ebi][sbi,ebi] (sbi\u2264ebisbi\u2264ebi). If the conference is situated in venue aa, the lecture will be held from saisai to eaieai, and if the conference is situated in venue bb, the lecture will be held from sbisbi to ebiebi. Hyunuk will choose one of these venues and all lectures will be held at that venue.Two lectures are said to overlap if they share any point in time in common. Formally, a lecture held in interval [x,y][x,y] overlaps with a lecture held in interval [u,v][u,v] if and only if max(x,u)\u2264min(y,v)max(x,u)\u2264min(y,v).We say that a participant can attend a subset ss of the lectures if the lectures in ss do not pairwise overlap (i.e. no two lectures overlap). Note that the possibility of attending may depend on whether Hyunuk selected venue aa or venue bb to hold the conference.A subset of lectures ss is said to be venue-sensitive if, for one of the venues, the participant can attend ss, but for the other venue, the participant cannot attend ss.A venue-sensitive set is problematic for a participant who is interested in attending the lectures in ss because the participant cannot be sure whether the lecture times will overlap. Hyunuk will be happy if and only if there are no venue-sensitive sets. Determine whether Hyunuk will be happy.InputThe first line contains an integer nn (1\u2264n\u22641000001\u2264n\u2264100000), the number of lectures held in the conference.Each of the next nn lines contains four integers saisai, eaieai, sbisbi, ebiebi (1\u2264sai,eai,sbi,ebi\u22641091\u2264sai,eai,sbi,ebi\u2264109, sai\u2264eai,sbi\u2264ebisai\u2264eai,sbi\u2264ebi).OutputPrint \"YES\" if Hyunuk will be happy. Print \"NO\" otherwise.You can print each letter in any case (upper or lower).ExamplesInputCopy2\n1 2 3 6\n3 4 7 8\nOutputCopyYES\nInputCopy3\n1 3 2 4\n4 5 6 7\n3 4 5 5\nOutputCopyNO\nInputCopy6\n1 5 2 9\n2 4 5 8\n3 6 7 11\n7 10 12 16\n8 11 13 17\n9 12 14 18\nOutputCopyYES\nNoteIn second example; lecture set {1,3}{1,3} is venue-sensitive. Because participant can't attend this lectures in venue aa, but can attend in venue bb.In first and third example, venue-sensitive set does not exist.","tags":["binary search","data structures","hashing","sortings"],"code":"#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define ld long double\n#define ll long long\nusing namespace std;\n\nstruct event{\n int t, type, id;\n event(int t, int type, int id) : t(t), type(type), id(id) {}\n};\n\nbool operator<(event a, event b){\n if(a.t==b.t){\n if(a.type==b.type){\n return a.id < b.id;\n }\n return a.type>b.type;\n }\n return a.t>n;\n mt19937_64 gen(time(NULL));\n uniform_int_distribution distribution(1,1e9);\n vector hash(n);\n for(int i=0;i a,b;\n for(int i=0;i>l1>>r1>>l2>>r2;\n a.emplace_back(l1,1,i);\n a.emplace_back(r1+1,2,i);\n b.emplace_back(l2,1,i);\n b.emplace_back(r2+1,2,i);\n }\n set founda,foundb;\n sort(a.begin(),a.end());\n sort(b.begin(),b.end());\n ll curr = 0;\n for(int i=0;i in;\n for(int i=0;i> t;\n for (int c = 0; c < t; c++)\n { \n \/\/ cout<<\"Case #\"<kk\u2032>k. The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks.InputThe first line contains integer nn (1\u2264n\u226415001\u2264n\u22641500) \u2014 the length of the given array. The second line contains the sequence of elements a[1],a[2],\u2026,a[n]a[1],a[2],\u2026,a[n] (\u2212105\u2264ai\u2264105\u2212105\u2264ai\u2264105).OutputIn the first line print the integer kk (1\u2264k\u2264n1\u2264k\u2264n). The following kk lines should contain blocks, one per line. In each line print a pair of indices li,rili,ri (1\u2264li\u2264ri\u2264n1\u2264li\u2264ri\u2264n) \u2014 the bounds of the ii-th block. You can print blocks in any order. If there are multiple answers, print any of them.ExamplesInputCopy7\n4 1 2 2 1 5 3\nOutputCopy3\n7 7\n2 3\n4 5\nInputCopy11\n-5 -4 -3 -2 -1 0 1 2 3 4 5\nOutputCopy2\n3 4\n1 1\nInputCopy4\n1 1 1 1\nOutputCopy4\n4 4\n1 1\n2 2\n3 3\n","tags":["data structures","greedy"],"code":"#include \n\n#include \n\n#include \n\n#include \n\n\n\n#define x first\n\n#define y second\n\n\n\nusing namespace std;\n\ntypedef pair pii;\n\nconst int N = 1510;\n\nunordered_map> mp;\n\nint n, s[N];\n\n\n\nint main()\n\n{\n\n cin >> n;\n\n for (int i = 1; i <= n; i ++ )\n\n {\n\n int a;\n\n cin >> a;\n\n s[i] += s[i - 1] + a;\n\n }\n\n for (int i = 1; i <= n; i ++ )\n\n for (int j = i; j <= n; j ++ )\n\n mp[s[j] - s[i - 1]].push_back({i, j});\n\n \n\n int k = 0;\n\n vector res;\n\n for (auto& t : mp)\n\n {\n\n vector path, tmp = t.y;\n\n sort(tmp.begin(), tmp.end(), [&](pii& a, pii& b){\n\n return a.y < b.y;\n\n });\n\n int st = -2e9, ed = -2e9, cnt = 0;\n\n for (auto& range : tmp)\n\n {\n\n int l = range.x, r = range.y;\n\n if (ed < l) {\n\n if (ed != -2e9) path.push_back({st, ed});\n\n st = l;\n\n ed = r;\n\n cnt ++;\n\n }\n\n }\n\n path.push_back({st, ed});\n\n if (k < cnt) k = cnt, res = path;\n\n }\n\n \n\n cout << k << endl;\n\n for (auto& t : res)\n\n cout << t.x << ' ' << t.y << endl;\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1285","problem_id":"C","statement":"C. Fadi and LCMtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; Osama gave Fadi an integer XX, and Fadi was wondering about the minimum possible value of max(a,b)max(a,b) such that LCM(a,b)LCM(a,b) equals XX. Both aa and bb should be positive integers.LCM(a,b)LCM(a,b) is the smallest positive integer that is divisible by both aa and bb. For example, LCM(6,8)=24LCM(6,8)=24, LCM(4,12)=12LCM(4,12)=12, LCM(2,3)=6LCM(2,3)=6.Of course, Fadi immediately knew the answer. Can you be just like Fadi and find any such pair?InputThe first and only line contains an integer XX (1\u2264X\u226410121\u2264X\u22641012).OutputPrint two positive integers, aa and bb, such that the value of max(a,b)max(a,b) is minimum possible and LCM(a,b)LCM(a,b) equals XX. If there are several possible such pairs, you can print any.ExamplesInputCopy2\nOutputCopy1 2\nInputCopy6\nOutputCopy2 3\nInputCopy4\nOutputCopy1 4\nInputCopy1\nOutputCopy1 1\n","tags":["brute force","math","number theory"],"code":"\/\/ Problem: C. Fadi and LCM\n\/\/ Contest: Codeforces - Codeforces Round #613 (Div. 2)\n\/\/ URL: https:\/\/codeforces.com\/problemset\/problem\/1285\/C\n\/\/ Memory Limit: 256 MB\n\/\/ Time Limit: 1000 ms\n\/\/ \n\/\/ Powered by CP Editor (https:\/\/cpeditor.org)\n\n#include\nusing namespace std;\ntypedef long long int ll;\nint lcm(int x, int y){return x\/__gcd(x,y)*y;}\n\n\nint main()\n{\n\t\/\/ios_base::sync_with_stdio(0);\n\t\/\/cin.tie(0);\n \tll n;\n\tcin >> n;\n\tll a=0,b=0;\n\tfor(ll i = 1; i*i <= n; i++){\n\t\tif(n%i==0){\n\t\t\tif(lcm(i , n\/i) == n){\n\t\t\t\ta=i;\n\t\t\t\tb=n\/i;\t\t\t\t\n\t\t\t}\n\t\t}\n\t}\n cout<< a << \" \" << b << '\\n';\n \n\treturn 0;\n}\n\n\t\t \t \t \t\t \t \t\t\t\t \t\t","language":"cpp"} -{"contest_id":"1322","problem_id":"B","statement":"B. Presenttime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputCatherine received an array of integers as a gift for March 8. Eventually she grew bored with it; and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one\u00a0\u2014 xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute(a1+a2)\u2295(a1+a3)\u2295\u2026\u2295(a1+an)\u2295(a2+a3)\u2295\u2026\u2295(a2+an)\u2026\u2295(an\u22121+an)(a1+a2)\u2295(a1+a3)\u2295\u2026\u2295(a1+an)\u2295(a2+a3)\u2295\u2026\u2295(a2+an)\u2026\u2295(an\u22121+an)Here x\u2295yx\u2295y is a bitwise XOR operation (i.e. xx ^ yy in many modern programming languages). You can read about it in Wikipedia: https:\/\/en.wikipedia.org\/wiki\/Exclusive_or#Bitwise_operation.InputThe first line contains a single integer nn (2\u2264n\u22644000002\u2264n\u2264400000)\u00a0\u2014 the number of integers in the array.The second line contains integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641071\u2264ai\u2264107).OutputPrint a single integer\u00a0\u2014 xor of all pairwise sums of integers in the given array.ExamplesInputCopy2\n1 2\nOutputCopy3InputCopy3\n1 2 3\nOutputCopy2NoteIn the first sample case there is only one sum 1+2=31+2=3.In the second sample case there are three sums: 1+2=31+2=3, 1+3=41+3=4, 2+3=52+3=5. In binary they are represented as 0112\u22951002\u22951012=01020112\u22951002\u22951012=0102, thus the answer is 2.\u2295\u2295 is the bitwise xor operation. To define x\u2295yx\u2295y, consider binary representations of integers xx and yy. We put the ii-th bit of the result to be 1 when exactly one of the ii-th bits of xx and yy is 1. Otherwise, the ii-th bit of the result is put to be 0. For example, 01012\u229500112=0110201012\u229500112=01102.","tags":["binary search","bitmasks","constructive algorithms","data structures","math","sortings"],"code":"#include \n\n\n\nusing namespace std;\n\nusing ll = long long;\n\n\n\nconstexpr int N = 2e5 + 10;\n\n\n\nvoid solve() {\n\n\tint n;\n\n\tcin >> n;\n\n\tvector a(n + 1);\n\n\tfor(int i = 1; i <= n; i++)\n\n\t\tcin >> a[i];\n\n\tint ans = 0;\n\n\tauto cal = [&](vector &v, int b) {\n\n\t\tint cnt = 0;\n\n\t\tfor(int i = 1; i <= n; i++) {\n\n\t\t\tint l = lower_bound(v.begin() + i + 1, v.end(), (1 << b) - v[i]) - v.begin(), r = upper_bound(v.begin() + i + 1, v.end(), (1 << (b + 1)) - 1 - v[i]) - v.begin() - 1;\n\n\t\t\tcnt ^= (r - l + 1) % 2;\n\n\t\t\t\/\/cout << i << ' ' << l << ' ' << r << '\\n';\n\n\t\t}\n\n\t\tfor(int i = 1; i <= n; i++) {\n\n\t\t\tint l = lower_bound(v.begin() + i + 1, v.end(), (1 << b) + (1 << (b + 1)) - v[i]) - v.begin(), r = upper_bound(v.begin() + i + 1, v.end(), (1 << (b + 2)) - 1 - v[i]) - v.begin() - 1;\n\n\t\t\tcnt ^= (r - l + 1) % 2;\n\n\t\t\t\/\/cout << i << ' ' << l << ' ' << r << '\\n';\n\n\t\t}\n\n\t\tif(cnt & 1)\n\n\t\t\treturn 1 << b;\n\n\t\telse \n\n\t\t\treturn 0;\n\n\t};\n\n\tfor(int i = 0; i < 28; i++) {\n\n\t\tvector b(n + 1);\n\n\t\tfor(int j = 1; j <= n; j++)\n\n\t\t\tb[j] = a[j] % (1 << (i + 1));\n\n\t\tsort(b.begin() + 1, b.end());\n\n\t\tans += cal(b, i);\n\n\t}\n\n\tcout << ans;\n\n}\n\n\n\nint main() {\n\n\tios::sync_with_stdio(false);\n\n\tcin.tie(nullptr);\n\n\t\n\n\tint t = 1;\n\n\t\n\n\twhile(t--) {\n\n\t\tsolve();\n\n\t}\n\n}","language":"cpp"} -{"contest_id":"1315","problem_id":"B","statement":"B. Homecomingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter a long party Petya decided to return home; but he turned out to be at the opposite end of the town from his home. There are nn crossroads in the line in the town, and there is either the bus or the tram station at each crossroad.The crossroads are represented as a string ss of length nn, where si=Asi=A, if there is a bus station at ii-th crossroad, and si=Bsi=B, if there is a tram station at ii-th crossroad. Currently Petya is at the first crossroad (which corresponds to s1s1) and his goal is to get to the last crossroad (which corresponds to snsn).If for two crossroads ii and jj for all crossroads i,i+1,\u2026,j\u22121i,i+1,\u2026,j\u22121 there is a bus station, one can pay aa roubles for the bus ticket, and go from ii-th crossroad to the jj-th crossroad by the bus (it is not necessary to have a bus station at the jj-th crossroad). Formally, paying aa roubles Petya can go from ii to jj if st=Ast=A for all i\u2264t\n\n \n\nusing namespace std;\n\n \n\ntypedef long long ll;\n\n \n\nint binarySearch(vector main, int money) {\n\n\tint L = 0;\n\n\tint R = main.size() - 1;\n\n\n\n\twhile (R >= L) {\n\n\t\tint mid = L + (R-L)\/2;\n\n\t\tif (main[mid] <= money && main[mid-1] > money) return mid+1;\n\n\n\n\t\tif (main[mid] > money) L = mid + 1;\n\n\t\telse if (main[mid] <= money && main[mid-1] <= money) R = mid - 1; \n\n\t}\n\n\treturn 1;\n\n}\n\n\n\nint main() {\n\n\tint t;\n\n\tcin >> t;\n\n\n\n\twhile (t--) {\n\n\t\tint a, b, p; string s; \n\n\t\tcin >> a >> b >> p >> s;\n\n\n\n\t\tvector needed(s.length());\n\n\t\tneeded[s.length() - 1] = 0;\n\n\t\tneeded[s.length() - 2] = (s[s.length()-2] == 'A' ? a : b);\n\n\t\tfor (int i = s.length()-3; i >= 0; i--) {\n\n\t\t\tif (s[i] == s[i+1]) needed[i] = needed[i+1];\n\n\t\t\telse if (s[i] == 'A') needed[i] = needed[i+1] + a;\n\n\t\t\telse if (s[i] == 'B') needed[i] = needed[i+1] + b;\n\n\t\t}\n\n\n\n\t\tcout << binarySearch(needed, p) << endl;\n\n\t}\n\n}","language":"cpp"} -{"contest_id":"1303","problem_id":"C","statement":"C. Perfect Keyboardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him \u2014 his keyboard will consist of only one row; where all 2626 lowercase Latin letters will be arranged in some order.Polycarp uses the same password ss on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in ss, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in ss, so, for example, the password cannot be password (two characters s are adjacent).Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?InputThe first line contains one integer TT (1\u2264T\u226410001\u2264T\u22641000) \u2014 the number of test cases.Then TT lines follow, each containing one string ss (1\u2264|s|\u22642001\u2264|s|\u2264200) representing the test case. ss consists of lowercase Latin letters only. There are no two adjacent equal characters in ss.OutputFor each test case, do the following: if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem); otherwise, print YES (in upper case), and then a string consisting of 2626 lowercase Latin letters \u2014 the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them. ExampleInputCopy5\nababa\ncodedoca\nabcda\nzxzytyz\nabcdefghijklmnopqrstuvwxyza\nOutputCopyYES\nbacdefghijklmnopqrstuvwxyz\nYES\nedocabfghijklmnpqrstuvwxyz\nNO\nYES\nxzytabcdefghijklmnopqrsuvw\nNO\n","tags":["dfs and similar","greedy","implementation"],"code":"#include \n\n#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);\n\n#define xs(a) cout<= (b); (i)--)\n\n#define mem(a,b) memset(a,b,sizeof(a));\n\nusing namespace std;\n\n#define ull unsigned long long\n\n#define ll long long\n\n#define endl '\\n'\n\ntypedef pair pll;\n\nconst int N=1e6+5;\n\nconst int mod=1e9+7;\n\n\/*-----------------------------------------------------------------------------------------------*\/\n\n\n\n\n\nvoid solve(){\n\n\tmapmp;\n\n\tstring s;cin>>s;\n\n\tstring t;t+=s[0];mp[s[0]]=1;\n\n\tll pos=0;\n\n\tfor(int i=1;i0&&t[pos-1]==s[i])pos--;\n\n\t\t\telse if(pos>T;\n\n\twhile(T--){\n\n\t\tsolve();\n\n\t}\n\n\n\n\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1304","problem_id":"F2","statement":"F2. Animal Observation (hard version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe only difference between easy and hard versions is the constraint on kk.Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The color of one camera is red, and the other one's color is blue.Gildong is going to take videos for nn days, starting from day 11 to day nn. The forest can be divided into mm areas, numbered from 11 to mm. He'll use the cameras in the following way: On every odd day (11-st, 33-rd, 55-th, ...), bring the red camera to the forest and record a video for 22 days. On every even day (22-nd, 44-th, 66-th, ...), bring the blue camera to the forest and record a video for 22 days. If he starts recording on the nn-th day with one of the cameras, the camera records for only one day. Each camera can observe kk consecutive areas of the forest. For example, if m=5m=5 and k=3k=3, he can put a camera to observe one of these three ranges of areas for two days: [1,3][1,3], [2,4][2,4], and [3,5][3,5].Gildong got information about how many animals will be seen in each area on each day. Since he would like to observe as many animals as possible, he wants you to find the best way to place the two cameras for nn days. Note that if the two cameras are observing the same area on the same day, the animals observed in that area are counted only once.InputThe first line contains three integers nn, mm, and kk (1\u2264n\u2264501\u2264n\u226450, 1\u2264m\u22642\u22c51041\u2264m\u22642\u22c5104, 1\u2264k\u2264m1\u2264k\u2264m) \u2013 the number of days Gildong is going to record, the number of areas of the forest, and the range of the cameras, respectively.Next nn lines contain mm integers each. The jj-th integer in the i+1i+1-st line is the number of animals that can be seen on the ii-th day in the jj-th area. Each number of animals is between 00 and 10001000, inclusive.OutputPrint one integer \u2013 the maximum number of animals that can be observed.ExamplesInputCopy4 5 2\n0 2 1 1 0\n0 0 3 1 2\n1 0 4 3 1\n3 3 0 0 4\nOutputCopy25\nInputCopy3 3 1\n1 2 3\n4 5 6\n7 8 9\nOutputCopy31\nInputCopy3 3 2\n1 2 3\n4 5 6\n7 8 9\nOutputCopy44\nInputCopy3 3 3\n1 2 3\n4 5 6\n7 8 9\nOutputCopy45\nNoteThe optimal way to observe animals in the four examples are as follows:Example 1: Example 2: Example 3: Example 4: ","tags":["data structures","dp","greedy"],"code":"\/\/ LUOGU_RID: 101284853\n#include \n\n#define int long long\n\n#define mem(a,b) memset(a,b,sizeof(a))\n\n#define fre(z) freopen(z\".in\",\"r\",stdin),freopen(z\".out\",\"w\",stdout)\n\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef unsigned long long ull;\n\ntypedef pair Pair;\n\nconst double eps=1e-8;\n\nconst int inf=2139062143;\n\n#ifdef ONLINE_JUDGE\n\nstatic char buf[1000000],*p1=buf,*p2=buf,obuf[1000000],*p3=obuf;\n\n#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++\n\n#endif\n\ninline void qread(){}template\n\ninline void qread(T1 &a,T2&...b){\n\n register T1 x=0;register bool f=false;char ch=getchar();\n\n while(ch<'0') f|=(ch=='-'),ch=getchar();\n\n while(ch>='0') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();\n\n x=(f?-x:x);a=x;qread(b...);\n\n}\n\ninline void dread(){}template\n\ninline void dread(T1 &a,T2&...b){\n\n register double w=0;register ll x=0,base=1;\n\n register bool f=false;char ch=getchar();\n\n while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();\n\n while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();\n\n w=(f?-x:x);if(ch!='.') return a=w,dread(b...);x=0,ch=getchar();\n\n while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),base*=10,ch=getchar();\n\n register double tmp=(double)(x\/(double)base);w=w+(double)(f?-tmp:tmp);a=w;dread(b...);\n\n}\n\ntemplate T qmax(T x,T y){return x>y?x:y;}\n\ntemplate T qmax(T x,T y,Arg ...arg){return qmax(x>y?x:y,arg...);}\n\ntemplate T qmin(T x,T y){return x T qmin(T x,T y,Arg ...arg){return qmin(x T randint(T l,T r){static mt19937 eng(time(0));uniform_int_distributiondis(l,r);return dis(eng);}\n\nconst int MAXN=57;\n\nconst int MAXM=2e4+7;\n\nint n,m,k,b[MAXM],a[MAXN][MAXM],f[MAXN][MAXM];\n\nstruct Seg_tree{\n\n #define ls p<<1\n\n #define rs p<<1|1\n\n #define mid ((l+r)>>1)\n\n int tr[MAXM<<2],tag[MAXM<<2];\n\n inline void pup(int p){tr[p]=qmax(tr[ls],tr[rs]);}\n\n inline void f(int p,int l,int r,int k){tr[p]+=k;tag[p]+=k;}\n\n void pwn(int p,int l,int r){\n\n f(ls,l,mid,tag[p]);f(rs,mid+1,r,tag[p]);tag[p]=0;\n\n }void build(int a[],int p,int l,int r){\n\n tag[p]=0;if(l==r) tr[p]=a[l];\n\n else build(a,ls,l,mid),build(a,rs,mid+1,r),pup(p);\n\n }void upd(int p,int l,int r,int L,int R,int k){\n\n if(L<=l&&r<=R) f(p,l,r,k);\n\n else{\n\n pwn(p,l,r);if(L<=mid) upd(ls,l,mid,L,R,k);\n\n if(R>mid) upd(rs,mid+1,r,L,R,k);pup(p);\n\n }\n\n }int que(int p,int l,int r,int L,int R){\n\n if(L<=l&&r<=R) return tr[p];\n\n else{\n\n int res=0;pwn(p,l,r);\n\n if(L<=mid) res=que(ls,l,mid,L,R);\n\n if(R>mid) res=qmax(res,que(rs,mid+1,r,L,R));return res;\n\n }\n\n }\n\n #undef mid\n\n}st[MAXN];int s[MAXN][MAXM],ans;\n\nsigned main(){\n\n qread(n,m,k);int i,j;for(i=1;i<=n;i++) for(j=1;j<=m;j++) qread(a[i][j]),s[i][j]=s[i][j-1]+a[i][j];\n\n for(j=1;j<=m-k+1;j++) f[1][j]=s[1][j+k-1]-s[1][j-1];\n\n for(i=2;i<=n;i++){\n\n for(j=1;j<=k;j++) b[j]=f[i-1][j]+s[i][j+k-1]-s[i][k];\n\n for(j=k+1;j<=m-k+1;j++) b[j]=f[i-1][j]+s[i][j+k-1]-s[i][j-1];\n\n st[i-1].build(b,1,1,m);f[i][1]=st[i-1].que(1,1,m,1,m-k+1)+s[i][k];\n\n \/\/ if(i==2){\n\n \/\/ for(j=1;j<=m;j++) cout< null\");\n\n #endif\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1288","problem_id":"F","statement":"F. Red-Blue Graphtime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a bipartite graph: the first part of this graph contains n1n1 vertices, the second part contains n2n2 vertices, and there are mm edges. The graph can contain multiple edges.Initially, each edge is colorless. For each edge, you may either leave it uncolored (it is free), paint it red (it costs rr coins) or paint it blue (it costs bb coins). No edge can be painted red and blue simultaneously.There are three types of vertices in this graph \u2014 colorless, red and blue. Colored vertices impose additional constraints on edges' colours: for each red vertex, the number of red edges indicent to it should be strictly greater than the number of blue edges incident to it; for each blue vertex, the number of blue edges indicent to it should be strictly greater than the number of red edges incident to it. Colorless vertices impose no additional constraints.Your goal is to paint some (possibly none) edges so that all constraints are met, and among all ways to do so, you should choose the one with minimum total cost. InputThe first line contains five integers n1n1, n2n2, mm, rr and bb (1\u2264n1,n2,m,r,b\u22642001\u2264n1,n2,m,r,b\u2264200) \u2014 the number of vertices in the first part, the number of vertices in the second part, the number of edges, the amount of coins you have to pay to paint an edge red, and the amount of coins you have to pay to paint an edge blue, respectively.The second line contains one string consisting of n1n1 characters. Each character is either U, R or B. If the ii-th character is U, then the ii-th vertex of the first part is uncolored; R corresponds to a red vertex, and B corresponds to a blue vertex.The third line contains one string consisting of n2n2 characters. Each character is either U, R or B. This string represents the colors of vertices of the second part in the same way.Then mm lines follow, the ii-th line contains two integers uiui and vivi (1\u2264ui\u2264n11\u2264ui\u2264n1, 1\u2264vi\u2264n21\u2264vi\u2264n2) denoting an edge connecting the vertex uiui from the first part and the vertex vivi from the second part.The graph may contain multiple edges.OutputIf there is no coloring that meets all the constraints, print one integer \u22121\u22121.Otherwise, print an integer cc denoting the total cost of coloring, and a string consisting of mm characters. The ii-th character should be U if the ii-th edge should be left uncolored, R if the ii-th edge should be painted red, or B if the ii-th edge should be painted blue. If there are multiple colorings with minimum possible cost, print any of them.ExamplesInputCopy3 2 6 10 15\nRRB\nUB\n3 2\n2 2\n1 2\n1 1\n2 1\n1 1\nOutputCopy35\nBUURRU\nInputCopy3 1 3 4 5\nRRR\nB\n2 1\n1 1\n3 1\nOutputCopy-1\nInputCopy3 1 3 4 5\nURU\nB\n2 1\n1 1\n3 1\nOutputCopy14\nRBB\n","tags":["constructive algorithms","flows"],"code":"\/\/ LUOGU_RID: 93522213\n\/*\n* @Author: ftt2333\n* @Email: ftt2333@126.com\n* @Last Modified time: 2022-11-10 21:50:51\n*\/\n\n#include \nusing namespace std;\n#define int ll\n#define off ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)\n#define fin(s) freopen(s, \"r\", stdin)\n#define fout(s) freopen(s, \"w\", stdout)\n#define fio(s) fin(s\".in\"), fout(s\".out\")\nusing ll = long long; using ull = uint64_t;\nusing lll = __int128; using ulll = __uint128_t;\nusing db = double; using ldb = long double;\nusing pii = pair; using pll = pair;\nusing vi = vector; using vl = vector;\nusing uchar = unsigned char; using uint = unsigned int;\ntemplate using uid = uniform_int_distribution;\ntemplate using urd = uniform_real_distribution;\n#define rep(i, a, b) for(auto i = (a); i <= (b); i++)\n#define per(i, a, b) for(auto i = (a); i >= (b); i--)\n#define go(i, h, e, x) for(int i = h[x]; i; i = e[i].nxt)\n#define pb push_back\n#define fi first\n#define se second\n#define all(a) (a).begin(), (a).end()\n#define szof(a) ((int)(a).size())\n#define mem(a, b) memset(a, b, sizeof(a))\n#define mcpy(a, b) memcpy(a, b, sizeof(a))\n\nmt19937 rnd(random_device{}());\nconst int mod = 998244353;\nconst int inf = 1e18;\nconst int N = 2e6 + 10;\nconst int M = 2e6 + 10;\n\nstruct mcmf {\n struct edge { int y, c, w, nxt; }ed[M];\n int head[N], cur[N], dis[N], tot = 1, s, t, mc, mf;\n int deg[N];\n bool vis[N];\n void init(){ tot = 1; mem(head, 0); }\n void add(int x, int y, int c, int w) { ed[++tot] = edge{ y, c, w, head[x] }; head[x] = tot; }\n void link(int x, int y, int l, int r, int w) {\n \/\/ cout << \"LINK \" << x << ' ' << y << ' ' << l << ' ' << r << ' ' << w << '\\n';\n deg[x] -= l, deg[y] += l;\n add(x, y, r - l, w), add(y, x, 0, -w);\n }\n bool lable(){\n fill(dis, dis + N, inf); mem(vis, 0); mcpy(cur, head);\n deque q; dis[s] = 0, vis[s] = 1; q.push_front(s);\n for(; !q.empty(); ) {\n int x = q.front(); vis[x] = 0; q.pop_front();\n go(i, head, ed, x) {\n int y = ed[i].y, c = ed[i].c, w = ed[i].w;\n if(!c || dis[y] <= dis[x] + w) continue; dis[y] = dis[x] + w;\n if(vis[y]) continue; vis[y] = 1;\n if(!q.empty() && dis[y] <= dis[q.front()]) q.push_front(y);\n else q.push_back(y);\n }\n }\n return dis[t] != inf;\n }\n int dfs(int x, int f){\n if(x == t)return f;\n vis[x] = 1; int r = f;\n go(i, head, ed, x) {\n cur[x] = i; if(!r) break;\n int y = ed[i].y, c = ed[i].c, w = ed[i].w;\n if(!c || dis[y] != dis[x] + w || vis[y]) continue;\n int k = dfs(y, min(r, c));\n if(k) r -= k, ed[i].c -= k, ed[i ^ 1].c += k, mc += w * k;\n else dis[y] = inf;\n } vis[x] = 0;\n return f - r;\n }\n void calc(int S, int T){ s=S, t=T; mc = mf = 0; for(; lable(); ) mf += dfs(s, inf); }\n} g;\nint n1, n2, m, r, b;\nchar s1[N], s2[N];\n\nsigned main() {\n cin >> n1 >> n2 >> m >> r >> b;\n int s = n1 + n2 + 1, t = s + 1, S = t + 1, T = S + 1;\n scanf(\"%s%s\", s1 + 1, s2 + 1);\n rep(i, 1, m) {\n int x, y; cin >> x >> y;\n g.link(x, y + n1, 0, 1, r);\n g.link(y + n1, x, 0, 1, b);\n }\n \/\/ cout << \"------------------\\n\";\n rep(i, 1, n1) {\n if(s1[i] == 'R') g.link(s, i, 1, inf, 0);\n if(s1[i] == 'B') g.link(i, t, 1, inf, 0);\n if(s1[i] == 'U') g.link(s, i, 0, inf, 0), g.link(i, t, 0, inf, 0);\n }\n \/\/ cout << \"------------------\\n\";\n rep(i, 1, n2) {\n if(s2[i] == 'B') g.link(s, n1 + i, 1, inf, 0);\n if(s2[i] == 'R') g.link(n1 + i, t, 1, inf, 0);\n if(s2[i] == 'U') g.link(s, n1 + i, 0, inf, 0), g.link(n1 + i, t, 0, inf, 0);\n }\n \/\/ cout << \"------------------\\n\";\n g.link(t, s, 0, inf, 0);\n int sum = 0;\n rep(i, 1, t) {\n if(g.deg[i] > 0) sum += g.deg[i], g.link(S, i, 0, g.deg[i], 0);\n if(g.deg[i] < 0) g.link(i, T, 0, -g.deg[i], 0);\n }\n g.calc(S, T);\n if(g.mf != sum) {\n cout << -1 << '\\n';\n return 0;\n }\n cout << g.mc << '\\n';\n rep(i, 1, m) {\n if(!g.ed[i * 4 - 2].c) cout << 'R';\n else if(!g.ed[i * 4].c) cout << 'B';\n else cout << 'U';\n } \n}","language":"cpp"} -{"contest_id":"1286","problem_id":"A","statement":"A. Garlandtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVadim loves decorating the Christmas tree; so he got a beautiful garland as a present. It consists of nn light bulbs in a single row. Each bulb has a number from 11 to nn (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 22). For example, the complexity of 1 4 2 3 5 is 22 and the complexity of 1 3 5 7 6 4 2 is 11.No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.InputThe first line contains a single integer nn (1\u2264n\u22641001\u2264n\u2264100)\u00a0\u2014 the number of light bulbs on the garland.The second line contains nn integers p1,\u00a0p2,\u00a0\u2026,\u00a0pnp1,\u00a0p2,\u00a0\u2026,\u00a0pn (0\u2264pi\u2264n0\u2264pi\u2264n)\u00a0\u2014 the number on the ii-th bulb, or 00 if it was removed.OutputOutput a single number\u00a0\u2014 the minimum complexity of the garland.ExamplesInputCopy5\n0 5 0 2 3\nOutputCopy2\nInputCopy7\n1 0 0 5 0 0 2\nOutputCopy1\nNoteIn the first example; one should place light bulbs as 1 5 4 2 3. In that case; the complexity would be equal to 2; because only (5,4)(5,4) and (2,3)(2,3) are the pairs of adjacent bulbs that have different parity.In the second case, one of the correct answers is 1 7 3 5 6 4 2. ","tags":["dp","greedy","sortings"],"code":"#include\n\nusing namespace std;\n\n\n\nconst int MAXN = 100;\n\n\n\nint dp[MAXN][MAXN][MAXN][2], a[MAXN], n, oddCount = 0, evenCount = 0;\n\n\n\nint f(int index, int odd, int even, int previousParity)\n\n{\n\n if(index == n) return 0;\n\n\n\n if(dp[index][odd][even][previousParity] != -1) return dp[index][odd][even][previousParity];\n\n\n\n if(a[index]) return (index && previousParity != a[index] % 2) + f(index + 1, odd, even, a[index] % 2);\n\n\n\n int takeOdd = MAXN, takeEven = MAXN;\n\n\n\n if(odd) takeOdd = (index && !previousParity) + f(index + 1, odd - 1, even, 1);\n\n if(even) takeEven = (index && previousParity) + f(index + 1, odd, even - 1, 0);\n\n\n\n return dp[index][odd][even][previousParity] = min(takeOdd, takeEven);\n\n}\n\n\n\nint main()\n\n{\n\n memset(dp, -1, sizeof(dp));\n\n\n\n cin >> n;\n\n\n\n for(int i = 0; i < n; i++) \n\n {\n\n cin >> a[i];\n\n if(a[i] && a[i] % 2) oddCount++;\n\n if(a[i] && a[i] % 2 == 0) evenCount++;\n\n }\n\n\n\n cout << f(0, (n + 1) \/ 2 - oddCount, n \/ 2 - evenCount, 0) << \"\\n\";\n\n}","language":"cpp"} -{"contest_id":"1300","problem_id":"B","statement":"B. Assigning to Classestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputReminder: the median of the array [a1,a2,\u2026,a2k+1][a1,a2,\u2026,a2k+1] of odd number of elements is defined as follows: let [b1,b2,\u2026,b2k+1][b1,b2,\u2026,b2k+1] be the elements of the array in the sorted order. Then median of this array is equal to bk+1bk+1.There are 2n2n students, the ii-th student has skill level aiai. It's not guaranteed that all skill levels are distinct.Let's define skill level of a class as the median of skill levels of students of the class.As a principal of the school, you would like to assign each student to one of the 22 classes such that each class has odd number of students (not divisible by 22). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.What is the minimum possible absolute difference you can achieve?InputEach test contains multiple test cases. The first line contains the number of test cases tt (1\u2264t\u22641041\u2264t\u2264104). The description of the test cases follows.The first line of each test case contains a single integer nn (1\u2264n\u22641051\u2264n\u2264105)\u00a0\u2014 the number of students halved.The second line of each test case contains 2n2n integers a1,a2,\u2026,a2na1,a2,\u2026,a2n (1\u2264ai\u22641091\u2264ai\u2264109)\u00a0\u2014 skill levels of students.It is guaranteed that the sum of nn over all test cases does not exceed 105105.OutputFor each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.ExampleInputCopy3\n1\n1 1\n3\n6 5 4 1 2 3\n5\n13 4 20 13 2 5 8 3 17 16\nOutputCopy0\n1\n5\nNoteIn the first test; there is only one way to partition students\u00a0\u2014 one in each class. The absolute difference of the skill levels will be |1\u22121|=0|1\u22121|=0.In the second test, one of the possible partitions is to make the first class of students with skill levels [6,4,2][6,4,2], so that the skill level of the first class will be 44, and second with [5,1,3][5,1,3], so that the skill level of the second class will be 33. Absolute difference will be |4\u22123|=1|4\u22123|=1.Note that you can't assign like [2,3][2,3], [6,5,4,1][6,5,4,1] or [][], [6,5,4,1,2,3][6,5,4,1,2,3] because classes have even number of students.[2][2], [1,3,4][1,3,4] is also not possible because students with skills 55 and 66 aren't assigned to a class.In the third test you can assign the students in the following way: [3,4,13,13,20],[2,5,8,16,17][3,4,13,13,20],[2,5,8,16,17] or [3,8,17],[2,4,5,13,13,16,20][3,8,17],[2,4,5,13,13,16,20]. Both divisions give minimal possible absolute difference.","tags":["greedy","implementation","sortings"],"code":"#include\n\n#include\n\nusing namespace std;\n\nint tab[200006];\n\nint main(){\n\nint t;\n\ncin>>t;\n\nfor(int k=0;k>n;\n\n for(int i=0;i<2*n;i++){\n\n cin>>tab[i];\n\n }\n\n sort(tab,tab+2*n);\n\n cout<\n\n \n\n#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)\n\n#define file(s) if (fopen(s\".in\", \"r\")) freopen(s\".in\", \"r\", stdin), freopen(s\".out\", \"w\", stdout)\n\n#define all(a) a.begin() , a.end()\n\n#define F first\n\n#define S second\n\n \n\nusing namespace std;\n\nusing ll = long long;\n\n \n\nconst ll N = 6e5+5 , inf = 2e9 + 7;\n\nconst ll INF = 1e18 , mod = 1e9+7 , P = 6547;\t\n\n\n\nll a[N] , t[N*4] , mn[N] , mx[N] , p[N];\n\nll n , m;\n\nvoid build(ll v , ll tl , ll tr){\n\n\tif(tl > n) return;\n\n\tif(tl == tr){\n\n\t\tt[v] = 1;\n\n\t\treturn;\n\n\t}\n\n\tll tm = (tl+tr) >> 1;\n\n\tbuild(v*2,tl,tm);\n\n\tbuild(v*2+1,tm+1,tr);\n\n\tt[v] = t[v*2] + t[v*2+1];\n\n}\n\nll get(ll v, ll tl , ll tr , ll l){\n\n\tif(tl >= l) return t[v];\n\n\tif(tr < l) return 0;\n\n\tll tm = (tl+tr) >> 1;\n\n\treturn get(v*2,tl,tm,l) + get(v*2+1,tm+1,tr,l);\n\n}\n\nvoid upd(ll v, ll tl, ll tr , ll pos, ll val){\n\n\tif(tl == tr){\n\n\t\tt[v] = val;\n\n\t\treturn;\n\n\t}\n\n\tll tm = (tl+tr) >> 1;\n\n\tif(tm >= pos) upd(v*2,tl,tm,pos,val);\n\n\telse upd(v*2+1,tm+1,tr,pos,val);\n\n\tt[v] = t[v*2] + t[v*2+1];\n\n}\n\nvoid solve(){\n\n\tcin >> n >> m;\n\n\tfor(ll i = 1; i <= n; i++){\n\n\t\tmn[i] = i;\n\n\t\tmx[i] = i; \n\n\t\tp[i] = n-i+1;\n\n\t}\t\n\n\tbuild(1,1,n+m);\n\n\tll nw = n+1;\n\n\tfor(ll i = 1; i <= m; i++){\n\n\t\tcin >> a[i];\n\n\t\tmn[a[i]] = 1;\n\n\t\tll res = get(1,1,n+m,p[a[i]]);\n\n\t\tmx[a[i]] = max(mx[a[i]] , res);\n\n\t\tupd(1,1,n+m,p[a[i]],0);\n\n\t\tp[a[i]] = nw++;\n\n\t\tupd(1,1,n+m,p[a[i]],1);\n\n\t}\n\n\tfor(ll i = 1; i <= n; i++){\n\n\t\tll res = get(1,1,n+m,p[i]);\n\n\t\tmx[i] = max(mx[i] , res);\n\n\t}\n\n\tfor(ll i = 1; i <= n; i++){\n\n\t\tcout << mn[i] <<\" \" << mx[i] <<\"\\n\";\n\n\t}\n\n}\n\n\/*\n\n\n\n*\/\n\nsigned main(){\n\n\tios;\n\n\tsolve();\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1290","problem_id":"E","statement":"E. Cartesian Tree time limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIldar is the algorithm teacher of William and Harris. Today; Ildar is teaching Cartesian Tree. However, Harris is sick, so Ildar is only teaching William.A cartesian tree is a rooted tree, that can be constructed from a sequence of distinct integers. We build the cartesian tree as follows: If the sequence is empty, return an empty tree; Let the position of the maximum element be xx; Remove element on the position xx from the sequence and break it into the left part and the right part (which might be empty) (not actually removing it, just taking it away temporarily); Build cartesian tree for each part; Create a new vertex for the element, that was on the position xx which will serve as the root of the new tree. Then, for the root of the left part and right part, if exists, will become the children for this vertex; Return the tree we have gotten.For example, this is the cartesian tree for the sequence 4,2,7,3,5,6,14,2,7,3,5,6,1: After teaching what the cartesian tree is, Ildar has assigned homework. He starts with an empty sequence aa.In the ii-th round, he inserts an element with value ii somewhere in aa. Then, he asks a question: what is the sum of the sizes of the subtrees for every node in the cartesian tree for the current sequence aa?Node vv is in the node uu subtree if and only if v=uv=u or vv is in the subtree of one of the vertex uu children. The size of the subtree of node uu is the number of nodes vv such that vv is in the subtree of uu.Ildar will do nn rounds in total. The homework is the sequence of answers to the nn questions.The next day, Ildar told Harris that he has to complete the homework as well. Harris obtained the final state of the sequence aa from William. However, he has no idea how to find the answers to the nn questions. Help Harris!InputThe first line contains a single integer nn (1\u2264n\u22641500001\u2264n\u2264150000).The second line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u2264n1\u2264ai\u2264n). It is guarenteed that each integer from 11 to nn appears in the sequence exactly once.OutputPrint nn lines, ii-th line should contain a single integer \u00a0\u2014 the answer to the ii-th question.ExamplesInputCopy5\n2 4 1 5 3\nOutputCopy1\n3\n6\n8\n11\nInputCopy6\n1 2 4 5 6 3\nOutputCopy1\n3\n6\n8\n12\n17\nNoteAfter the first round; the sequence is 11. The tree is The answer is 11.After the second round, the sequence is 2,12,1. The tree is The answer is 2+1=32+1=3.After the third round, the sequence is 2,1,32,1,3. The tree is The answer is 2+1+3=62+1+3=6.After the fourth round, the sequence is 2,4,1,32,4,1,3. The tree is The answer is 1+4+1+2=81+4+1+2=8.After the fifth round, the sequence is 2,4,1,5,32,4,1,5,3. The tree is The answer is 1+3+1+5+1=111+3+1+5+1=11.","tags":["data structures"],"code":"\/\/ LUOGU_RID: 100833057\n#include\n\n#define ls 2*p\n\n#define rs 2*p+1\n\n#define INF 2e9\n\nusing namespace std;\n\nvoid read(int &x) {\n\n x = 0;\n\n int f = 1;\n\n char s = getchar();\n\n while (s > '9' || s < '0') {\n\n if (s == '-')\n\n f = -1;\n\n s = getchar();\n\n }\n\n while (s >= '0' && s <= '9') {\n\n x = (x << 3) + (x << 1) + (s - '0');\n\n s = getchar();\n\n }\n\n x *= f;\n\n}\n\nvoid write(long long x) {\n\n if (x < 0) {\n\n putchar('-');\n\n x = (~x) + 1;\n\n }\n\n if (x > 9) {\n\n write(x \/ 10);\n\n } \n\n putchar(x % 10 + '0');\n\n}\n\nconst int MAXN=5e5+5;\n\nstruct Seg_node{\n\n\tint Max_first;\n\n\tint Max_second;\n\n\tint Add1,Add3;\n\n\tlong long sumdate;\n\n\tint Num_mx;\n\n\tint l,r;\n\n\tint Num;\n\n}Tree[MAXN*4];\n\nvoid push_up(int p)\n\n{\n\n\tTree[p].sumdate=Tree[ls].sumdate+Tree[rs].sumdate;\n\n\tTree[p].Num=Tree[ls].Num+Tree[rs].Num;\n\n\tif(Tree[ls].Max_first==Tree[rs].Max_first)\n\n\t{\n\n\t\tTree[p].Max_first=Tree[ls].Max_first;\n\n\t\tTree[p].Num_mx=Tree[ls].Num_mx+Tree[rs].Num_mx;\n\n\t\tTree[p].Max_second=max(Tree[ls].Max_second,Tree[rs].Max_second);\n\n\t}\n\n\telse if(Tree[ls].Max_first>Tree[rs].Max_first)\n\n\t{\n\n\t\tTree[p].Max_first=Tree[ls].Max_first;\n\n\t\tTree[p].Num_mx=Tree[ls].Num_mx;\n\n\t\tTree[p].Max_second=max(Tree[ls].Max_second,Tree[rs].Max_first);\n\n\t}\n\n\telse\n\n\t{\n\n\t\tTree[p].Max_first=Tree[rs].Max_first;\n\n\t\tTree[p].Num_mx=Tree[rs].Num_mx;\n\n\t\tTree[p].Max_second=max(Tree[rs].Max_second,Tree[ls].Max_first);\n\n\t}\n\n\treturn; \n\n}\n\nvoid push_down(int p)\n\n{\n\n\tint Mxc=max(Tree[ls].Max_first,Tree[rs].Max_first);\n\n\tif(Mxc==Tree[ls].Max_first)\n\n\t{\n\n\t\tif(Tree[ls].Num)\n\n\t\t{\n\n\t\t\tTree[ls].Max_first+=Tree[p].Add1;\n\n\t\t\tTree[ls].Max_second+=Tree[p].Add3;\n\n\t\t}\n\n\t\t\n\n\t\tTree[ls].sumdate+=((long long)Tree[p].Add1*Tree[ls].Num_mx);\n\n\t\tTree[ls].sumdate+=((long long)Tree[p].Add3*(Tree[ls].Num-Tree[ls].Num_mx));\n\n\t\tTree[ls].Add1+=Tree[p].Add1;\n\n\t\tTree[ls].Add3+=Tree[p].Add3;\n\n\t}\n\n\telse\n\n\t{\n\n\t\tif(Tree[ls].Num)\n\n\t\t{\n\n\t\t\tTree[ls].Max_first+=Tree[p].Add3;\n\n\t\t\tTree[ls].Max_second+=Tree[p].Add3;\n\n\t\t}\n\n\t\t\n\n\t\tTree[ls].sumdate+=((long long)Tree[p].Add3*(Tree[ls].Num));\n\n\t\tTree[ls].Add1+=Tree[p].Add3;\n\n\t\tTree[ls].Add3+=Tree[p].Add3;\n\n\t} \n\n\t\n\n\tif(Mxc==Tree[rs].Max_first)\n\n\t{\n\n\t\tif(Tree[rs].Num)\n\n\t\t{\n\n\t\t\tTree[rs].Max_first+=Tree[p].Add1;\n\n\t\t\tTree[rs].Max_second+=Tree[p].Add3;\n\n\t\t}\n\n\t\n\n\t\tTree[rs].sumdate+=((long long)Tree[p].Add1*Tree[rs].Num_mx);\n\n\t\tTree[rs].sumdate+=((long long)Tree[p].Add3*(Tree[rs].Num-Tree[rs].Num_mx));\n\n\t\tTree[rs].Add1+=Tree[p].Add1;\n\n\t\tTree[rs].Add3+=Tree[p].Add3;\n\n\t}\n\n\telse\n\n\t{\n\n\t\tif(Tree[rs].Num)\n\n\t\t{\n\n\t\t\tTree[rs].Max_first+=Tree[p].Add3;\n\n\t\t\tTree[rs].Max_second+=Tree[p].Add3;\n\n\t\t}\n\n\t\tTree[rs].sumdate+=((long long)Tree[p].Add3*(Tree[rs].Num));\n\n\t\tTree[rs].Add1+=Tree[p].Add3;\n\n\t\tTree[rs].Add3+=Tree[p].Add3;\n\n\t} \n\n\tTree[p].Add1=0;\n\n\tTree[p].Add3=0;\n\n}\n\nvoid Update_sum(int p,int l,int r,int v)\n\n{\n\n\tif(l>r)\n\n\t{\n\n\t\treturn;\n\n\t}\n\n\tif(Tree[p].l>=l&&Tree[p].r<=r)\n\n\t{\n\n\t\tif(Tree[p].Num)\n\n\t\t{\n\n\t\t\tTree[p].Max_first+=v;\n\n\t\t\tTree[p].Max_second+=v;\n\n\t\t}\n\n\t\tTree[p].sumdate+=((long long)(Tree[p].Num)*v);\n\n\t\tTree[p].Add1+=v;\n\n\t\tTree[p].Add3+=v;\n\n\t\treturn;\n\n\t}\n\n\tint mid=(Tree[p].l+Tree[p].r)>>1;\n\n\tpush_down(p);\n\n\tif(l<=mid)\n\n\t{\n\n\t\tUpdate_sum(ls,l,r,v);\n\n\t}\n\n\tif(r>mid)\n\n\t{\n\n\t\tUpdate_sum(rs,l,r,v);\n\n\t}\n\n\tpush_up(p);\n\n}\n\nvoid update_min(int p,int v)\n\n{\n\n\tif(Tree[p].l==Tree[p].r)\n\n\t{\n\n\t\tTree[p].Max_first=min(Tree[p].Max_first,v);\n\n\t\tTree[p].sumdate=Tree[p].Max_first;\n\n\t\treturn;\n\n\t}\n\n\t\n\n\tif(Tree[p].Max_first=v&&Tree[p].Max_secondr)\n\n\t{\n\n\t\treturn;\n\n\t}\n\n\tif(Tree[p].l>=l&&Tree[p].r<=r)\n\n\t{\n\n\t\tupdate_min(p,v);\n\n\t\treturn;\n\n\t}\n\n\tpush_down(p);\n\n\tint mid=(Tree[p].l+Tree[p].r)>>1;\n\n\tif(l<=mid)\n\n\t{\n\n\t\tUpdate_min(ls,l,r,v);\n\n\t}\n\n\tif(r>mid)\n\n\t{\n\n\t\tUpdate_min(rs,l,r,v);\n\n\t}\n\n\t\n\n\tpush_up(p); \n\n}\n\nvoid Update(int p,int k)\n\n{\n\n\tif(Tree[p].l==Tree[p].r)\n\n\t{\n\n\t\tTree[p].Num++;\n\n\t\treturn;\n\n\t}\n\n\tpush_down(p);\n\n\tint mid=(Tree[p].l+Tree[p].r)>>1;\n\n\tif(k<=mid)\n\n\t{\n\n\t\tUpdate(ls,k);\n\n\t}\n\n\telse\n\n\t{\n\n\t\tUpdate(rs,k);\n\n\t}\n\n\tpush_up(p);\n\n}\n\nint Query(int p,int l,int r)\n\n{\n\n\tif(l>r)\n\n\t{\n\n\t\treturn 0;\n\n\t}\n\n\tif(Tree[p].l>=l&&Tree[p].r<=r)\n\n\t{\n\n\t\treturn Tree[p].Num;\n\n\t}\n\n\tpush_down(p);\n\n\tint mid=(Tree[p].l+Tree[p].r)>>1;\n\n\tint drs=0;\n\n\tif(l<=mid)\n\n\t{\n\n\t\tdrs+=Query(ls,l,r);\n\n\t}\n\n\tif(r>mid)\n\n\t{\n\n\t\tdrs+=Query(rs,l,r);\n\n\t}\n\n\treturn drs;\n\n}\n\nlong long Query_sum(int p,int l,int r)\n\n{\n\n\tif(l>r)\n\n\t{\n\n\t\treturn 0;\n\n\t}\n\n\tif(Tree[p].l>=l&&Tree[p].r<=r)\n\n\t{\n\n\t\treturn Tree[p].sumdate;\n\n\t}\n\n\tpush_down(p);\n\n\tint mid=(Tree[p].l+Tree[p].r)>>1;\n\n\tlong long Res=0;\n\n\tif(l<=mid)\n\n\t{\n\n\t\tRes+=Query_sum(ls,l,r);\n\n\t}\n\n\tif(r>mid)\n\n\t{\n\n\t\tRes+=Query_sum(rs,l,r);\n\n\t}\n\n\treturn Res;\n\n}\n\nvoid print(int p)\n\n{\n\n\tif(Tree[p].l==Tree[p].r)\n\n\t{\n\n\t\tprintf(\"%d \",Tree[p].Max_first);\n\n\t\treturn;\n\n\t}\n\n\tpush_down(p);\n\n\tprint(ls);\n\n\tprint(rs);\n\n}\n\nvoid Build(int p,int l,int r)\n\n{\n\n\tTree[p].l=l;\n\n\tTree[p].r=r;\n\n\tTree[p].Add1=Tree[p].Add3=0;\n\n\tif(l==r)\n\n\t{\n\n\t\tTree[p].Max_first=0;\n\n\t\tTree[p].Max_second=-INF;\n\n\t\tTree[p].Num_mx=1;\n\n\t\tTree[p].sumdate=0;\n\n\t\tTree[p].Num=0;\n\n\t\treturn;\n\n\t}\n\n\tint mid=(l+r)>>1;\n\n\tBuild(ls,l,mid);\n\n\tBuild(rs,mid+1,r);\n\n\tpush_up(p);\n\n}\n\nint n;\n\nint a[MAXN];\n\nint rev[MAXN];\n\nlong long Sum[MAXN];\n\nsigned main()\n\n{\n\n\tscanf(\"%d\",&n);\n\n\tfor(int i=1;i<=n;i++)\n\n\t{\n\n\t\tscanf(\"%d\",&a[i]);\n\n\t\trev[a[i]]=i;\n\n\t}\n\n\tBuild(1,1,n);\n\n\tfor(int i=1;i<=n;i++)\n\n\t{\n\n\t\tUpdate(1,rev[i]);\n\n\t\t\n\n\t\tUpdate_sum(1,rev[i]+1,n,1);\n\n\t\tUpdate_sum(1,rev[i],rev[i],i);\n\n\t\t\n\n\t\tint Rx=Query(1,1,rev[i]-1);\n\n\t\tUpdate_min(1,1,rev[i]-1,Rx);\n\n\t\tSum[i]+=Query_sum(1,1,n); \n\n\t}\n\n\treverse(a+1,a+1+n);\n\n\tfor(int i=1;i<=n;i++)\n\n\t{\n\n\t\trev[a[i]]=i;\n\n\t}\n\n\tBuild(1,1,n);\n\n\tfor(int i=1;i<=n;i++)\n\n\t{\n\n\t\tUpdate(1,rev[i]);\n\n\t\tUpdate_sum(1,rev[i]+1,n,1);\n\n\t\tint Rg=Query(1,1,n);\n\n\t\tUpdate_sum(1,rev[i],rev[i],Rg);\n\n\t\tint Rx=Query(1,1,rev[i]-1);\n\n\t\tUpdate_min(1,1,rev[i]-1,Rx);\n\n\t\tSum[i]+=Query_sum(1,1,n);\n\n\t\tSum[i]-=(long long)i*i;\n\n\t\tprintf(\"%lld\\n\",Sum[i]); \n\n\t}\n\n}\n\n\/\/5\n\n\/\/3 5 1 4 2","language":"cpp"} -{"contest_id":"1312","problem_id":"G","statement":"G. Autocompletiontime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a set of strings SS. Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions: if the current string is tt, choose some lowercase Latin letter cc and append it to the back of tt, so the current string becomes t+ct+c. This action takes 11 second; use autocompletion. When you try to autocomplete the current string tt, a list of all strings s\u2208Ss\u2208S such that tt is a prefix of ss is shown to you. This list includes tt itself, if tt is a string from SS, and the strings are ordered lexicographically. You can transform tt into the ii-th string from this list in ii seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type. What is the minimum number of seconds that you have to spend to type each string from SS?Note that the strings from SS are given in an unusual way.InputThe first line contains one integer nn (1\u2264n\u22641061\u2264n\u2264106).Then nn lines follow, the ii-th line contains one integer pipi (0\u2264pi\n\nusing namespace std;\n\nint const M=1000100;char ch;\n\nint i,n,k,x,a[M],f[M],g[M],s[M];\n\nvector >G[M];\n\nvoid dfs(int x,int p){\n\n\tf[x]=f[p]+1;g[x]=min(f[x],g[p]+s[p]);\n\n\tif (s[x]) f[x]=min(f[x],g[x]+1);\n\n\tsort(G[x].begin(),G[x].end());\n\n\tfor (auto v:G[x]) dfs(v.second,x);\n\n\ts[p]+=s[x];\n\n}\n\nint main(){\n\n\tscanf(\"%d\",&n);\n\n\tfor (i=1;i<=n;i++){\n\n\t scanf(\"%d %c\",&x,&ch);\n\n\t G[x].push_back(make_pair(ch,i));\n\n\t }\n\n\tscanf(\"%d\",&k);\n\n\tfor (i=1;i<=k;i++) scanf(\"%d\",&a[i]),s[a[i]]=1;\n\n\tf[0]=-1;dfs(0,0);\n\n\tfor (i=1;i<=k;i++) printf(\"%d \",f[a[i]]);\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1307","problem_id":"F","statement":"F. Cow and Vacationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie is planning a vacation! In Cow-lifornia; there are nn cities, with n\u22121n\u22121 bidirectional roads connecting them. It is guaranteed that one can reach any city from any other city. Bessie is considering vv possible vacation plans, with the ii-th one consisting of a start city aiai and destination city bibi.It is known that only rr of the cities have rest stops. Bessie gets tired easily, and cannot travel across more than kk consecutive roads without resting. In fact, she is so desperate to rest that she may travel through the same city multiple times in order to do so.For each of the vacation plans, does there exist a way for Bessie to travel from the starting city to the destination city?InputThe first line contains three integers nn, kk, and rr (2\u2264n\u22642\u22c51052\u2264n\u22642\u22c5105, 1\u2264k,r\u2264n1\u2264k,r\u2264n) \u00a0\u2014 the number of cities, the maximum number of roads Bessie is willing to travel through in a row without resting, and the number of rest stops.Each of the following n\u22121n\u22121 lines contain two integers xixi and yiyi (1\u2264xi,yi\u2264n1\u2264xi,yi\u2264n, xi\u2260yixi\u2260yi), meaning city xixi and city yiyi are connected by a road. The next line contains rr integers separated by spaces \u00a0\u2014 the cities with rest stops. Each city will appear at most once.The next line contains vv (1\u2264v\u22642\u22c51051\u2264v\u22642\u22c5105) \u00a0\u2014 the number of vacation plans.Each of the following vv lines contain two integers aiai and bibi (1\u2264ai,bi\u2264n1\u2264ai,bi\u2264n, ai\u2260biai\u2260bi) \u00a0\u2014 the start and end city of the vacation plan. OutputIf Bessie can reach her destination without traveling across more than kk roads without resting for the ii-th vacation plan, print YES. Otherwise, print NO.ExamplesInputCopy6 2 1\n1 2\n2 3\n2 4\n4 5\n5 6\n2\n3\n1 3\n3 5\n3 6\nOutputCopyYES\nYES\nNO\nInputCopy8 3 3\n1 2\n2 3\n3 4\n4 5\n4 6\n6 7\n7 8\n2 5 8\n2\n7 1\n8 1\nOutputCopyYES\nNO\nNoteThe graph for the first example is shown below. The rest stop is denoted by red.For the first query; Bessie can visit these cities in order: 1,2,31,2,3.For the second query, Bessie can visit these cities in order: 3,2,4,53,2,4,5. For the third query, Bessie cannot travel to her destination. For example, if she attempts to travel this way: 3,2,4,5,63,2,4,5,6, she travels on more than 22 roads without resting. The graph for the second example is shown below. ","tags":["dfs and similar","dsu","trees"],"code":"#include \n\nusing namespace std;\n\n\n\n#define Int register int\n\n#define MAXN 400005\n\n\n\ntemplate inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}\n\ntemplate inline void read (T &t,Args&... args){read (t);read (args...);}\n\ntemplate inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x \/ 10);putchar (x % 10 + '0');}\n\ntemplate inline void chkmax (T &a,T b){a = max (a,b);}\n\ntemplate inline void chkmin (T &a,T b){a = min (a,b);}\n\n\n\nint n,K,R;\n\nvector g[MAXN];\n\n\n\nint fa[MAXN],dep[MAXN],dis[MAXN],par[MAXN][21];\n\nvoid dfs1 (int u,int pat){\n\n par[u][0] = pat,dep[u] = dep[pat] + 1;\n\n for (Int i = 1;i <= 20;++ i) par[u][i] = par[par[u][i - 1]][i - 1];\n\n for (Int v : g[u]) if (v != pat) dfs1 (v,u);\n\n}\n\n\n\nint getlca (int u,int v){\n\n if (dep[u] < dep[v]) swap (u,v);\n\n for (Int i = 20,dis = dep[u] - dep[v];~i;-- i) if (dis >> i & 1) u = par[u][i];\n\n if (u == v) return u;\n\n else{\n\n for (Int i = 20;~i;-- i) if (par[u][i] ^ par[v][i]) u = par[u][i],v = par[v][i];\n\n return par[u][0];\n\n }\n\n}\n\n\n\nint findSet (int x){return fa[x] == x ? x : fa[x] = findSet (fa[x]);}\n\nint getdist (int u,int v){\n\n return dep[u] + dep[v] - dep[getlca (u,v)] * 2;\n\n}\n\nint getclimb (int u,int t){\n\n for (Int i = 20;~i;-- i) if (t >> i & 1) u = par[u][i];\n\n return u;\n\n}\n\n\n\nsigned main(){\n\n read (n,K,R);int now = n;\n\n for (Int x = 1;x <= 2 * n;++ x) fa[x] = x;\n\n for (Int i = 2,u,v;i <= n;++ i)\n\n read (u,v),++ now,g[u].push_back (now),g[v].push_back (now),g[now].push_back (u),g[now].push_back (v);\n\n queue q;memset (dis,-1,sizeof (dis));\n\n \tfor (Int i = 1,x;i <= R;++ i) read (x),q.push (x),dis[x] = 0;\n\n while (!q.empty()){\n\n int u = q.front();q.pop ();\n\n if (dis[u] == K) break;\n\n for (Int v : g[u]){\n\n fa[findSet (v)] = findSet (u);\n\n if (dis[v] == -1) dis[v] = dis[u] + 1,q.push (v);\n\n }\n\n }\n\n dfs1 (1,0);\n\n int Q;read (Q);\n\n while (Q --> 0){\n\n int u,v;read (u,v);int len = getdist (u,v);\n\n if (len <= 2 * K){puts (\"YES\");continue;}\n\n int lca = getlca (u,v);\n\n int tu = (dep[u] - dep[lca] >= K) ? getclimb (u,K) : getclimb (v,len - K),\n\n tv = (dep[v] - dep[lca] >= K) ? getclimb (v,K) : getclimb (u,len - K);\n\n if (findSet (tu) == findSet (tv)) puts (\"YES\");\n\n else puts (\"NO\");\n\n }\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1296","problem_id":"E2","statement":"E2. String Coloring (hard version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is a hard version of the problem. The actual problems are different; but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.You are given a string ss consisting of nn lowercase Latin letters.You have to color all its characters the minimum number of colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in ss).After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.The goal is to make the string sorted, i.e. all characters should be in alphabetical order.Your task is to find the minimum number of colors which you have to color the given string in so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.InputThe first line of the input contains one integer nn (1\u2264n\u22642\u22c51051\u2264n\u22642\u22c5105) \u2014 the length of ss.The second line of the input contains the string ss consisting of exactly nn lowercase Latin letters.OutputIn the first line print one integer resres (1\u2264res\u2264n1\u2264res\u2264n) \u2014 the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps.In the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array cc of length nn, where 1\u2264ci\u2264res1\u2264ci\u2264res and cici means the color of the ii-th character.ExamplesInputCopy9\nabacbecfd\nOutputCopy2\n1 1 2 1 2 1 2 1 2 \nInputCopy8\naaabbcbb\nOutputCopy2\n1 2 1 2 1 2 1 1\nInputCopy7\nabcdedc\nOutputCopy3\n1 1 1 1 1 2 3 \nInputCopy5\nabcde\nOutputCopy1\n1 1 1 1 1 \n","tags":["data structures","dp"],"code":"#include \n\n#include \n\n#include \n\n#define mod 1000000007\n\n#define ll long long int\n\n#define endl '\\n'\n\n#define mem(a,x) memset(a,x,sizeof(a))\n\n#define EPS 1e-12\n\n#define double long double\n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\ntypedef tree, rb_tree_tag, tree_order_statistics_node_update> pbds;\n\n\/\/cout << \"0th element: \" << *A.find_by_order(0) << endl;\n\n\/\/cout << \"No. of elems smaller than 6: \" << A.order_of_key(6) << endl;\n\n\/\/priority_queue , greater>\n\nll pow1(ll a,ll b){ll ans=1; while(b>0){if(b%2==0){b=b\/2;a=(a*a)%mod;}else{b--;ans=(ans*a)%mod;}}return ans;}\n\nll mod1=1e9+9;\n\nll pow2(ll a,ll b){ll ans=1; while(b>0){if(b%2==0){b=b\/2;a=(a*a)%mod1;}else{b--;ans=(ans*a)%mod1;}}return ans;}\n\nll lcm(ll a,ll b)\n\n{\n\n return (a*b)\/__gcd(a,b);\n\n}\n\nconst int N=3e5+2;\n\nvoid solve()\n\n{\n\n ll n,i,j,a;\n\n cin>>n;\n\n string s;\n\n cin>>s;\n\n ll ar[n+2],dp[n+2];\n\n ll mx[27];\n\n mem(mx,0);\n\n for(i=0;iar[i];j--)\n\n {\n\n dp[i]=max(dp[i],mx[j]+1);\n\n }\n\n mx[ar[i]]=max(mx[ar[i]],dp[i]);\n\n }\n\n ll ans=0;\n\n for(i=0;i>t;\n\n while (t--)\n\n {\n\n \/\/cout<<\"Case \"<2a+b>2 (e.g. a=b=1a=b=1 is impossible).OutputPrint tt integers\u00a0\u2014 the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn't contain the dead pixel.ExampleInputCopy6\n8 8 0 0\n1 10 0 3\n17 31 10 4\n2 1 0 0\n5 10 3 9\n10 10 4 8\nOutputCopy56\n6\n442\n1\n45\n80\nNoteIn the first test case; the screen resolution is 8\u00d788\u00d78, and the upper left pixel is a dead pixel. Here you can see one of two possible layouts of the maximal window. ","tags":["implementation"],"code":"#include \n\n\n\n#pragma GCC optimize(\"Ofast\")\n\n#pragma GCC optimize(\"no-stack-protector\")\n\n#pragma GCC optimize(\"unroll-loops\")\n\n#pragma GCC target(\"sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native\")\n\n#pragma GCC optimize(\"fast-math\")\n\n#pragma GCC optimize (\"unroll-loops,Ofast,O3\")\n\n#pragma GCC target(\"avx,avx2,fma\")\n\n\n\n#define file(s) freopen(s\".in\", \"r\", stdin), freopen(s\".out\", \"w\", stdout)\n\n#define all(x) (x).begin(),(x).end()\n\n#define int long long\n\n#define rs(p) (p<<1|1)\n\n#define ls(p) (p<<1)\n\n#define pb push_back\n\n#define endl '\\n'\n\n#define sz size()\n\n#define F first\n\n#define S second\n\n#define SlimShady ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);\n\nusing namespace std;\n\nconst long double eps = 1e-10;\n\nconst int MIN = LLONG_MIN;\n\nconst int M = 2e9 + 1000;\n\nconst int N = 1e5 + 100;\n\nvoid solve() {\n\n\tint a, b, i, j;\n\n\tcin >> a >> b >> i >> j;\n\n\tint ans = max(b * (a - i - 1), a * (b - j - 1));\n\n\tans = max(a * j, ans);\n\n\tans = max(b * i, ans);\n\n\tcout << ans << endl;\n\n}\n\nsigned main () {\n\n\tSlimShady\n\n int test = 1;\n\n cin >> test;\n\n while(test--) {\n\n\t solve();\n\n\t}\n\n}","language":"cpp"} -{"contest_id":"1304","problem_id":"F2","statement":"F2. Animal Observation (hard version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe only difference between easy and hard versions is the constraint on kk.Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The color of one camera is red, and the other one's color is blue.Gildong is going to take videos for nn days, starting from day 11 to day nn. The forest can be divided into mm areas, numbered from 11 to mm. He'll use the cameras in the following way: On every odd day (11-st, 33-rd, 55-th, ...), bring the red camera to the forest and record a video for 22 days. On every even day (22-nd, 44-th, 66-th, ...), bring the blue camera to the forest and record a video for 22 days. If he starts recording on the nn-th day with one of the cameras, the camera records for only one day. Each camera can observe kk consecutive areas of the forest. For example, if m=5m=5 and k=3k=3, he can put a camera to observe one of these three ranges of areas for two days: [1,3][1,3], [2,4][2,4], and [3,5][3,5].Gildong got information about how many animals will be seen in each area on each day. Since he would like to observe as many animals as possible, he wants you to find the best way to place the two cameras for nn days. Note that if the two cameras are observing the same area on the same day, the animals observed in that area are counted only once.InputThe first line contains three integers nn, mm, and kk (1\u2264n\u2264501\u2264n\u226450, 1\u2264m\u22642\u22c51041\u2264m\u22642\u22c5104, 1\u2264k\u2264m1\u2264k\u2264m) \u2013 the number of days Gildong is going to record, the number of areas of the forest, and the range of the cameras, respectively.Next nn lines contain mm integers each. The jj-th integer in the i+1i+1-st line is the number of animals that can be seen on the ii-th day in the jj-th area. Each number of animals is between 00 and 10001000, inclusive.OutputPrint one integer \u2013 the maximum number of animals that can be observed.ExamplesInputCopy4 5 2\n0 2 1 1 0\n0 0 3 1 2\n1 0 4 3 1\n3 3 0 0 4\nOutputCopy25\nInputCopy3 3 1\n1 2 3\n4 5 6\n7 8 9\nOutputCopy31\nInputCopy3 3 2\n1 2 3\n4 5 6\n7 8 9\nOutputCopy44\nInputCopy3 3 3\n1 2 3\n4 5 6\n7 8 9\nOutputCopy45\nNoteThe optimal way to observe animals in the four examples are as follows:Example 1: Example 2: Example 3: Example 4: ","tags":["data structures","dp","greedy"],"code":"#include\n\nusing namespace std;\n\nint dp[51][20009];\n\nint a[51][20009];\n\nint pf[51][20009];\n\nint st[80009];\n\nint lazy[80009];\n\nvoid push(int id){\n\nif (lazy[id]==0)return;\n\nst[id*2]+=lazy[id];\n\nst[id*2+1]+=lazy[id];\n\nlazy[id*2]+=lazy[id];\n\nlazy[id*2+1]+=lazy[id];\n\nlazy[id]=0;\n\n}\n\nvoid update(int id,int l,int r,int u,int v,int val){\n\nif (l>v||rv)return;\n\nif (u<=l&&r<=v){\n\n st[id]+=val;\n\n lazy[id]+=val;\n\n return;\n\n}\n\nint mid=(l+r)\/2;\n\npush(id);\n\nupdate(id*2,l,mid,u,v,val);\n\nupdate(id*2+1,mid+1,r,u,v,val);\n\nst[id]=max(st[id*2],st[id*2+1]);\n\n}\n\nint get(int id,int l,int r,int u,int v){\n\nif (l>v||rv)return 0;\n\nif (u<=l&&r<=v){\n\n return st[id];\n\n}\n\npush(id);\n\nint mid=(l+r)\/2;\n\nreturn max(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v));\n\n}\n\nsigned main(){\n\n\tios_base::sync_with_stdio(NULL);\n\n\tcin.tie(nullptr);\n\n\t\/\/freopen(\"usaco.INP\",\"r\",stdin);\n\n\t\/\/freopen(\"usaco.ANS\",\"w\",stdout);\n\n\tint n,m,k;\n\n\tcin>>n>>m>>k;\n\n\tfor (int i=1;i<=n;i++){\n\n for (int j=1;j<=m;j++){\n\n cin>>a[i][j];\n\n pf[i][j]=pf[i][j-1]+a[i][j];\n\n }\n\n\t}\n\n\tfor (int i=k;i<=m;i++){\n\n dp[1][i]=pf[1][i]-pf[1][i-k];\n\n\t}\n\n\tfor (int i=2;i<=n;i++){\n\n dequeid;\n\n memset(st,0,sizeof(st));\n\n memset(lazy,0,sizeof(lazy));\n\n for (int j=k;j<=m;j++){\n\n update(1,1,m,j,j,dp[i-1][j]);\n\n }\n\n for (int j=k;j<=m;j++){\n\n while (!id.empty()&&j-1-k>=id.front())id.pop_front();\n\n if (!id.empty()){\n\n int fi=id.front(),se=id.back();\n\n update(1,1,m,fi,se,a[i][j-k]);\n\n \/\/cerr<=k;j--){\n\n while (!id.empty()&&id.front()-k>=j+1)id.pop_front();\n\n if (!id.empty()){\n\n int fi=id.back(),se=id.front();\n\n update(1,1,m,fi,se,a[i][j+1]);\n\n \/\/cerr<\n\nusing namespace std;\n\n\n\nint main()\n\n{\n\n int n;\n\n cin>>n;\n\n int a[n],b[n];\n\n int count1=0,count2=0;\n\n for(int i=0;i>a[i];\n\n }\n\n for(int i=0;i>b[i];\n\n }\n\n for(int i=0;i\nusing namespace std;\n\n#define Int register int\n#define MAXN 400005\n\ntemplate inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}\ntemplate inline void read (T &t,Args&... args){read (t);read (args...);}\ntemplate inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x \/ 10);putchar (x % 10 + '0');}\ntemplate inline void chkmax (T &a,T b){a = max (a,b);}\ntemplate inline void chkmin (T &a,T b){a = min (a,b);}\n\nint n,K,R;\nvector g[MAXN];\n\nint fa[MAXN],dep[MAXN],dis[MAXN],par[MAXN][21];\nvoid dfs1 (int u,int pat){\n par[u][0] = pat,dep[u] = dep[pat] + 1;\n for (Int i = 1;i <= 20;++ i) par[u][i] = par[par[u][i - 1]][i - 1];\n for (Int v : g[u]) if (v != pat) dfs1 (v,u);\n}\n\nint getlca (int u,int v){\n if (dep[u] < dep[v]) swap (u,v);\n for (Int i = 20,dis = dep[u] - dep[v];~i;-- i) if (dis >> i & 1) u = par[u][i];\n if (u == v) return u;\n else{\n for (Int i = 20;~i;-- i) if (par[u][i] ^ par[v][i]) u = par[u][i],v = par[v][i];\n return par[u][0];\n }\n}\n\nint findSet (int x){return fa[x] == x ? x : fa[x] = findSet (fa[x]);}\nint getdist (int u,int v){\n return dep[u] + dep[v] - dep[getlca (u,v)] * 2;\n}\nint getclimb (int u,int t){\n for (Int i = 20;~i;-- i) if (t >> i & 1) u = par[u][i];\n return u;\n}\n\nsigned main(){\n read (n,K,R);int now = n;\n for (Int x = 1;x <= 2 * n;++ x) fa[x] = x;\n for (Int i = 2,u,v;i <= n;++ i)\n read (u,v),++ now,g[u].push_back (now),g[v].push_back (now),g[now].push_back (u),g[now].push_back (v);\n queue q;memset (dis,-1,sizeof (dis));\n \tfor (Int i = 1,x;i <= R;++ i) read (x),q.push (x),dis[x] = 0;\n while (!q.empty()){\n int u = q.front();q.pop ();\n if (dis[u] == K) break;\n for (Int v : g[u]){\n fa[findSet (v)] = findSet (u);\n if (dis[v] == -1) dis[v] = dis[u] + 1,q.push (v);\n }\n }\n dfs1 (1,0);\n int Q;read (Q);\n while (Q --> 0){\n int u,v;read (u,v);int len = getdist (u,v);\n if (len <= 2 * K){puts (\"YES\");continue;}\n int lca = getlca (u,v);\n int tu = (dep[u] - dep[lca] >= K) ? getclimb (u,K) : getclimb (v,len - K),\n tv = (dep[v] - dep[lca] >= K) ? getclimb (v,K) : getclimb (u,len - K);\n if (findSet (tu) == findSet (tv)) puts (\"YES\");\n else puts (\"NO\");\n }\n return 0;\n}","language":"cpp"} -{"contest_id":"1323","problem_id":"B","statement":"B. Count Subrectanglestime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn and array bb of length mm both consisting of only integers 00 and 11. Consider a matrix cc of size n\u00d7mn\u00d7m formed by following rule: ci,j=ai\u22c5bjci,j=ai\u22c5bj (i.e. aiai multiplied by bjbj). It's easy to see that cc consists of only zeroes and ones too.How many subrectangles of size (area) kk consisting only of ones are there in cc?A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x1,x2,y1,y2x1,x2,y1,y2 (1\u2264x1\u2264x2\u2264n1\u2264x1\u2264x2\u2264n, 1\u2264y1\u2264y2\u2264m1\u2264y1\u2264y2\u2264m) a subrectangle c[x1\u2026x2][y1\u2026y2]c[x1\u2026x2][y1\u2026y2] is an intersection of the rows x1,x1+1,x1+2,\u2026,x2x1,x1+1,x1+2,\u2026,x2 and the columns y1,y1+1,y1+2,\u2026,y2y1,y1+1,y1+2,\u2026,y2.The size (area) of a subrectangle is the total number of cells in it.InputThe first line contains three integers nn, mm and kk (1\u2264n,m\u226440000,1\u2264k\u2264n\u22c5m1\u2264n,m\u226440000,1\u2264k\u2264n\u22c5m), length of array aa, length of array bb and required size of subrectangles.The second line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u226410\u2264ai\u22641), elements of aa.The third line contains mm integers b1,b2,\u2026,bmb1,b2,\u2026,bm (0\u2264bi\u226410\u2264bi\u22641), elements of bb.OutputOutput single integer\u00a0\u2014 the number of subrectangles of cc with size (area) kk consisting only of ones.ExamplesInputCopy3 3 2\n1 0 1\n1 1 1\nOutputCopy4\nInputCopy3 5 4\n1 1 1\n1 1 1 1 1\nOutputCopy14\nNoteIn first example matrix cc is: There are 44 subrectangles of size 22 consisting of only ones in it: In second example matrix cc is: ","tags":["binary search","greedy","implementation"],"code":"#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#include\n#define forin(i,a,n) for(int i=a;i<=n;i++)\n#define forni(i,n,a) for(int i=n;i>=a;i--)\n#define fi first\n#define se second\nusing namespace std;\ntypedef long long ll;\ntypedef double db;\ntypedef pair PII;\nconst double eps=1e-7;\nconst int N=6e5+7 ,M=2*N , INF=0x3f3f3f3f,mod=1e9+7;\ninline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}\nwhile(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}\nvoid stin() {freopen(\"in_put.txt\",\"r\",stdin);freopen(\"my_out_put.txt\",\"w\",stdout);}\n\ntemplate T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}\ntemplate T lcm(T a,T b) {return a*b\/gcd(a,b);}\n\nint T;\nint n,m,k;\nint a[N],b[N];\nint la[N],lb[N];\n\nvoid solve() { \n n=read(),m=read(),k=read();\n \n for(int i=1;i<=n;i++) a[i]=read();\n for(int i=1;i<=m;i++) b[i]=read();\n \n vector vec;\n for(int i=1;i<=k\/i;i++) {\n if(k%i==0) {\n vec.push_back(i);\n if(i!=k\/i) vec.push_back(k\/i);\n }\n }\n \n sort(vec.begin(),vec.end());\n \n int last=0;\n for(int i=1;i<=n;i++) {\n if(a[i]==1) last++;\n else {\n int temp=last;\n for(int j=1;j<=temp;j++) la[j]+=last--;\n last=0;\n }\n }\n \n if(last!=0) {\n int temp=last;\n for(int j=1;j<=temp;j++) la[j]+=last--;\n }\n \n last=0;\n for(int i=1;i<=m;i++) {\n if(b[i]==1) last++;\n else {\n int temp=last;\n for(int j=1;j<=temp;j++) lb[j]+=last--;\n last=0;\n }\n }\n \n if(last!=0) {\n int temp=last;\n for(int j=1;j<=temp;j++) lb[j]+=last--;\n }\n \n ll ans=0;\n for(int i=0;i=1&&l<=n&&r>=1&&r<=m) {\n ans+=(ll)la[l]*lb[r];\n }\n }\n \n printf(\"%lld\\n\",ans);\n}\n\nint main() {\n \/\/ init();\n \/\/ stin();\n \n \/\/ scanf(\"%d\",&T);\n T=1; \n while(T--) solve();\n \n return 0; \n} \n \t\t\t \t\t \t\t \t \t\t \t \t\t","language":"cpp"} -{"contest_id":"1315","problem_id":"C","statement":"C. Restoring Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a sequence b1,b2,\u2026,bnb1,b2,\u2026,bn. Find the lexicographically minimal permutation a1,a2,\u2026,a2na1,a2,\u2026,a2n such that bi=min(a2i\u22121,a2i)bi=min(a2i\u22121,a2i), or determine that it is impossible.InputEach test contains one or more test cases. The first line contains the number of test cases tt (1\u2264t\u22641001\u2264t\u2264100).The first line of each test case consists of one integer nn\u00a0\u2014 the number of elements in the sequence bb (1\u2264n\u22641001\u2264n\u2264100).The second line of each test case consists of nn different integers b1,\u2026,bnb1,\u2026,bn\u00a0\u2014 elements of the sequence bb (1\u2264bi\u22642n1\u2264bi\u22642n).It is guaranteed that the sum of nn by all test cases doesn't exceed 100100.OutputFor each test case, if there is no appropriate permutation, print one number \u22121\u22121.Otherwise, print 2n2n integers a1,\u2026,a2na1,\u2026,a2n\u00a0\u2014 required lexicographically minimal permutation of numbers from 11 to 2n2n.ExampleInputCopy5\n1\n1\n2\n4 1\n3\n4 1 3\n4\n2 3 4 5\n5\n1 5 7 2 8\nOutputCopy1 2 \n-1\n4 5 1 2 3 6 \n-1\n1 3 5 6 7 9 2 4 8 10 \n","tags":["greedy"],"code":"#include \n\n\n\nusing namespace std;\n\nusing ll = long long;\n\nusing vi = vector;\n\nusing si = set;\n\n\n\nint main() {\n\n cin.tie(0), ios::sync_with_stdio(0);\n\n ll t, n;\n\n cin >> t;\n\n while (t--) {\n\n cin >> n;\n\n si s;\n\n for (int i = 1; i <= 2*n; i++) s.insert(i);\n\n vi a(2*n), b(n), c(n), d(n+1);\n\n for (int i = 0; i < n; i++) {\n\n cin >> b[i];\n\n c[i] = b[i];\n\n s.erase(b[i]);\n\n }\n\n sort(c.begin(), c.end());\n\n bool ok=1;\n\n for (int i = 0, j = 1; i < n; i++, j+=2) {\n\n if (c[i] > j) {\n\n ok=0;\n\n break;\n\n }\n\n }\n\n if (!ok) cout << \"-1\\n\";\n\n else {\n\n for (int i = 0; i < n; i++) {\n\n a[2*i] = b[i];\n\n int next = *lower_bound(s.begin(), s.end(), b[i]);\n\n s.erase(next);\n\n a[2*i+1] = next;\n\n }\n\n for (int i = 0; i < 2*n; i++)\n\n cout << a[i] << \" \\n\"[i==2*n-1];\n\n }\n\n }\n\n}","language":"cpp"} -{"contest_id":"1320","problem_id":"F","statement":"F. Blocks and Sensorstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputPolycarp plays a well-known computer game (we won't mention its name). Every object in this game consists of three-dimensional blocks \u2014 axis-aligned cubes of size 1\u00d71\u00d711\u00d71\u00d71. These blocks are unaffected by gravity, so they can float in the air without support. The blocks are placed in cells of size 1\u00d71\u00d711\u00d71\u00d71; each cell either contains exactly one block or is empty. Each cell is represented by its coordinates (x,y,z)(x,y,z) (the cell with these coordinates is a cube with opposite corners in (x,y,z)(x,y,z) and (x+1,y+1,z+1)(x+1,y+1,z+1)) and its contents ax,y,zax,y,z; if the cell is empty, then ax,y,z=0ax,y,z=0, otherwise ax,y,zax,y,z is equal to the type of the block placed in it (the types are integers from 11 to 2\u22c51052\u22c5105).Polycarp has built a large structure consisting of blocks. This structure can be enclosed in an axis-aligned rectangular parallelepiped of size n\u00d7m\u00d7kn\u00d7m\u00d7k, containing all cells (x,y,z)(x,y,z) such that x\u2208[1,n]x\u2208[1,n], y\u2208[1,m]y\u2208[1,m], and z\u2208[1,k]z\u2208[1,k]. After that, Polycarp has installed 2nm+2nk+2mk2nm+2nk+2mk sensors around this parallelepiped. A sensor is a special block that sends a ray in some direction and shows the type of the first block that was hit by this ray (except for other sensors). The sensors installed by Polycarp are adjacent to the borders of the parallelepiped, and the rays sent by them are parallel to one of the coordinate axes and directed inside the parallelepiped. More formally, the sensors can be divided into 66 types: there are mkmk sensors of the first type; each such sensor is installed in (0,y,z)(0,y,z), where y\u2208[1,m]y\u2208[1,m] and z\u2208[1,k]z\u2208[1,k], and it sends a ray that is parallel to the OxOx axis and has the same direction; there are mkmk sensors of the second type; each such sensor is installed in (n+1,y,z)(n+1,y,z), where y\u2208[1,m]y\u2208[1,m] and z\u2208[1,k]z\u2208[1,k], and it sends a ray that is parallel to the OxOx axis and has the opposite direction; there are nknk sensors of the third type; each such sensor is installed in (x,0,z)(x,0,z), where x\u2208[1,n]x\u2208[1,n] and z\u2208[1,k]z\u2208[1,k], and it sends a ray that is parallel to the OyOy axis and has the same direction; there are nknk sensors of the fourth type; each such sensor is installed in (x,m+1,z)(x,m+1,z), where x\u2208[1,n]x\u2208[1,n] and z\u2208[1,k]z\u2208[1,k], and it sends a ray that is parallel to the OyOy axis and has the opposite direction; there are nmnm sensors of the fifth type; each such sensor is installed in (x,y,0)(x,y,0), where x\u2208[1,n]x\u2208[1,n] and y\u2208[1,m]y\u2208[1,m], and it sends a ray that is parallel to the OzOz axis and has the same direction; finally, there are nmnm sensors of the sixth type; each such sensor is installed in (x,y,k+1)(x,y,k+1), where x\u2208[1,n]x\u2208[1,n] and y\u2208[1,m]y\u2208[1,m], and it sends a ray that is parallel to the OzOz axis and has the opposite direction. Polycarp has invited his friend Monocarp to play with him. Of course, as soon as Monocarp saw a large parallelepiped bounded by sensor blocks, he began to wonder what was inside of it. Polycarp didn't want to tell Monocarp the exact shape of the figure, so he provided Monocarp with the data from all sensors and told him to try guessing the contents of the parallelepiped by himself.After some hours of thinking, Monocarp has no clue about what's inside the sensor-bounded space. But he does not want to give up, so he decided to ask for help. Can you write a program that will analyze the sensor data and construct any figure that is consistent with it?InputThe first line contains three integers nn, mm and kk (1\u2264n,m,k\u22642\u22c51051\u2264n,m,k\u22642\u22c5105, nmk\u22642\u22c5105nmk\u22642\u22c5105) \u2014 the dimensions of the parallelepiped.Then the sensor data follows. For each sensor, its data is either 00, if the ray emitted from it reaches the opposite sensor (there are no blocks in between), or an integer from 11 to 2\u22c51052\u22c5105 denoting the type of the first block hit by the ray. The data is divided into 66 sections (one for each type of sensors), each consecutive pair of sections is separated by a blank line, and the first section is separated by a blank line from the first line of the input.The first section consists of mm lines containing kk integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (0,i,j)(0,i,j).The second section consists of mm lines containing kk integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (n+1,i,j)(n+1,i,j).The third section consists of nn lines containing kk integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (i,0,j)(i,0,j).The fourth section consists of nn lines containing kk integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (i,m+1,j)(i,m+1,j).The fifth section consists of nn lines containing mm integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (i,j,0)(i,j,0).Finally, the sixth section consists of nn lines containing mm integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (i,j,k+1)(i,j,k+1).OutputIf the information from the input is inconsistent, print one integer \u22121\u22121.Otherwise, print the figure inside the parallelepiped as follows. The output should consist of nmknmk integers: a1,1,1a1,1,1, a1,1,2a1,1,2, ..., a1,1,ka1,1,k, a1,2,1a1,2,1, ..., a1,2,ka1,2,k, ..., a1,m,ka1,m,k, a2,1,1a2,1,1, ..., an,m,kan,m,k, where ai,j,kai,j,k is the type of the block in (i,j,k)(i,j,k), or 00 if there is no block there. If there are multiple figures consistent with sensor data, describe any of them.For your convenience, the sample output is formatted as follows: there are nn separate sections for blocks having x=1x=1, x=2x=2, ..., x=nx=n; each section consists of mm lines containing kk integers each. Note that this type of output is acceptable, but you may print the integers with any other formatting instead (even all integers on the same line), only their order matters.ExamplesInputCopy4 3 2\n\n1 4\n3 2\n6 5\n\n1 4\n3 2\n6 7\n\n1 4\n1 4\n0 0\n0 7\n\n6 5\n6 5\n0 0\n0 7\n\n1 3 6\n1 3 6\n0 0 0\n0 0 7\n\n4 3 5\n4 2 5\n0 0 0\n0 0 7\nOutputCopy1 4\n3 0\n6 5\n\n1 4\n3 2\n6 5\n\n0 0\n0 0\n0 0\n\n0 0\n0 0\n0 7\nInputCopy1 1 1\n\n0\n\n0\n\n0\n\n0\n\n0\n\n0\nOutputCopy0\nInputCopy1 1 1\n\n0\n\n0\n\n1337\n\n0\n\n0\n\n0\nOutputCopy-1\nInputCopy1 1 1\n\n1337\n\n1337\n\n1337\n\n1337\n\n1337\n\n1337\nOutputCopy1337\n","tags":["brute force"],"code":"\/\/ LUOGU_RID: 100810478\n#include \n#pragma GCC optimize(\"Ofast\")\nusing namespace std;\nnamespace IO {\nconst int SIZ = 1 << 14;\n\ninline char getc() {\n static char bf[SIZ], *begin = bf, *end = bf;\n if (begin == end) begin = bf, end = bf + fread(bf, 1, SIZ, stdin);\n if (begin == end) return EOF;\n return *begin++;\n}\nchar wbf[SIZ], *wend = wbf, *weoo = wbf + SIZ;\ninline void putc(char c) {\n *wend = c, ++wend;\n if (wend == weoo) fwrite(wbf, 1, SIZ, stdout), wend = wbf;\n}\ninline void do_flush() { fwrite(wbf, 1, wend - wbf, stdout); }\ntemplate \ninline void uread(T &ans) {\n static char tmp;\n tmp = getc(), ans = 0;\n while (!isdigit(tmp)) tmp = getc();\n while (isdigit(tmp)) ans = (ans << 1) + (ans << 3) + (tmp ^ 48), tmp = getc();\n}\ntemplate \ninline void read(T &ans) {\n static char tmp2;\n static bool flag;\n tmp2 = getc(), ans = 0, flag = 0;\n while (!isdigit(tmp2)) {\n if (tmp2 == '-') flag = 1;\n tmp2 = getc();\n }\n while (isdigit(tmp2)) ans = (ans << 1) + (ans << 3) + (tmp2 ^ 48), tmp2 = getc();\n if (flag) ans = -ans;\n}\ntemplate \ninline void uwrite(T x) {\n if (x > 9) uwrite(x \/ 10);\n putc(x % 10 + '0');\n}\ntemplate \ninline void write(T x) {\n if (x < 0)\n putc('-'), uwrite(-x);\n else\n uwrite(x);\n}\ninline void putstr(const char str[]) {\n for (int i = 0; str[i]; i++) putc(str[i]);\n}\ninline void readalpha(char &x) {\n for (x = getc(); !isalpha(x); x = getc())\n ;\n}\n}; \/\/ namespace IO\nusing IO::read, IO::write, IO::do_flush, IO::putc, IO::uread, IO::uwrite;\ntemplate \nstruct Table2D {\n vector> vec;\n int __n, __m;\n inline void init(int n, int m) {\n vec.resize(n), __n = n, __m = m;\n for (int i = 0; i < n; i++) vec[i].resize(m);\n }\n\n inline void read(int n, int m) {\n for (int i = 0; i < n; i++)\n for (int j = 0; j < m; j++) ::read(vec[i][j]);\n }\n inline bool inrange(int x, int y) { return x >= 1 && x <= __n && y >= 1 && y <= __m; }\n inline T &at(int x, int y) { return vec[x - 1][y - 1]; }\n};\ntemplate \ninline void fill(Table2D &x, int k) {\n for (int i = 0; i < x.__n; i++)\n for (int j = 0; j < x.__m; j++)\n for (int l = 1; l <= k; l++) x.vec[i][j].insert(l);\n}\nint n, m, k;\n\/\/ x:UD y:FB z:LR\nTable2D> vecD, vecB, vecL;\nTable2D>> vecU, vecF, vecR;\nTable2D colU, colD, colF, colB, colL, colR;\nTable2D> used;\ninline void make(int &a, int b) {\n if (a == -1)\n a = b;\n else if (a != b)\n a = 0;\n}\ninline int getcol(int x, int y, int z) {\n int col = -1;\n if (vecD.at(y, z).size() && *vecD.at(y, z).begin() == x) make(col, colD.at(y, z)); \/\/ cout << \"readcolD\" << colD.at(y, z) << endl ;\n if (vecU.at(y, z).size() && *vecU.at(y, z).begin() == x) make(col, colU.at(y, z)); \/\/ cout << \"readcolU\" << colU.at(y, z) << endl ;\n if (vecB.at(x, z).size() && *vecB.at(x, z).begin() == y) make(col, colB.at(x, z)); \/\/ cout << \"readcolB\" << colB.at(x, z) << endl ;\n if (vecF.at(x, z).size() && *vecF.at(x, z).begin() == y) make(col, colF.at(x, z)); \/\/ cout << \"readcolF\" << colF.at(x, z) << endl ;\n if (vecL.at(x, y).size() && *vecL.at(x, y).begin() == z) make(col, colL.at(x, y)); \/\/ cout << \"readcolL\" << colL.at(x, y) << endl ;\n if (vecR.at(x, y).size() && *vecR.at(x, y).begin() == z) make(col, colR.at(x, y)); \/\/ cout << \"readcolR\" << colR.at(x, y) << endl ;\n return col;\n}\ninline void check(int x, int y, int z) {\n if (x < 1 || x > n || y < 1 || y > m || z < 1 || z > k || !used.at(x, y)[z]) return;\n int p = getcol(x, y, z);\n if (!p) {\n \/\/ cout << \"del:\" << x << ' ' << y << ' ' << z << endl;\n used.at(x, y)[z] = 0;\n vecD.at(y, z).erase(x);\n vecU.at(y, z).erase(x);\n vecB.at(x, z).erase(y);\n vecF.at(x, z).erase(y);\n vecL.at(x, y).erase(z);\n vecR.at(x, y).erase(z);\n if (vecD.at(y, z).size() && *vecD.at(y, z).rbegin() > x) check(*vecD.at(y, z).upper_bound(x), y, z);\n if (vecU.at(y, z).size() && *vecU.at(y, z).rbegin() < x) check(*vecU.at(y, z).upper_bound(x), y, z);\n if (vecB.at(x, z).size() && *vecB.at(x, z).rbegin() > y) check(x, *vecB.at(x, z).upper_bound(y), z);\n if (vecF.at(x, z).size() && *vecF.at(x, z).rbegin() < y) check(x, *vecF.at(x, z).upper_bound(y), z);\n if (vecL.at(x, y).size() && *vecL.at(x, y).rbegin() > z) check(x, y, *vecL.at(x, y).upper_bound(z));\n if (vecR.at(x, y).size() && *vecR.at(x, y).rbegin() < z) check(x, y, *vecR.at(x, y).upper_bound(z));\n }\n}\ntemplate \ninline bool cc(Table2D &a, Table2D &b) {\n for (int i = 1; i <= a.__n; i++)\n for (int j = 1; j <= a.__m; j++)\n if (b.at(i, j) && !a.at(i, j).size()) return 0;\n return 1;\n}\nint main() {\n cin >> n >> m >> k;\n vecU.init(m, k), vecD.init(m, k), vecF.init(n, k), vecB.init(n, k), vecL.init(n, m), vecR.init(n, m);\n fill(vecU, n), fill(vecD, n), fill(vecF, m), fill(vecB, m), fill(vecL, k), fill(vecR, k);\n colU.init(m, k), colD.init(m, k), colF.init(n, k), colB.init(n, k), colL.init(n, m), colR.init(n, m);\n colD.read(m, k), colU.read(m, k), colB.read(n, k), colF.read(n, k), colL.read(n, m), colR.read(n, m);\n used.init(n, m);\n for (int i = 1; i <= n; i++)\n for (int j = 1; j <= m; j++) {\n used.at(i, j).resize(k + 1);\n for (int l = 1; l <= k; l++) used.at(i, j)[l] = 1;\n }\n \/\/ cout << *vecR.at(1, 1).begin() << ' '<< *vecL<< endl;\n \/\/ cout << getcol(1, 1, 2) << endl;\n for (int i = 1; i <= n; i++)\n for (int j = 1; j <= m; j++)\n for (int l = 1; l <= k; l++) check(i, j, l);\n if (!cc(vecU, colU)) return puts(\"-1\"), 0;\n if (!cc(vecD, colD)) return puts(\"-1\"), 0;\n if (!cc(vecL, colL)) return puts(\"-1\"), 0;\n if (!cc(vecR, colR)) return puts(\"-1\"), 0;\n if (!cc(vecB, colB)) return puts(\"-1\"), 0;\n if (!cc(vecF, colF)) return puts(\"-1\"), 0;\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n for (int l = 1; l <= k; l++) {\n int tmp;\n if (!used.at(i, j)[l])\n printf(\"%d \", 0);\n else if ((tmp = getcol(i, j, l)) == -1)\n printf(\"%d \", 0);\n else\n printf(\"%d \", tmp);\n }\n puts(\"\");\n }\n puts(\"\");\n }\n return 0;\n}","language":"cpp"} -{"contest_id":"1296","problem_id":"C","statement":"C. Yet Another Walking Robottime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a robot on a coordinate plane. Initially; the robot is located at the point (0,0)(0,0). Its path is described as a string ss of length nn consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point (x,y)(x,y) to the point (x\u22121,y)(x\u22121,y); 'R' (right): means that the robot moves from the point (x,y)(x,y) to the point (x+1,y)(x+1,y); 'U' (up): means that the robot moves from the point (x,y)(x,y) to the point (x,y+1)(x,y+1); 'D' (down): means that the robot moves from the point (x,y)(x,y) to the point (x,y\u22121)(x,y\u22121). The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point (xe,ye)(xe,ye), then after optimization (i.e. removing some single substring from ss) the robot also ends its path at the point (xe,ye)(xe,ye).This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string ss).Recall that the substring of ss is such string that can be obtained from ss by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of \"LURLLR\" are \"LU\", \"LR\", \"LURLLR\", \"URL\", but not \"RR\" and \"UL\".You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1\u2264t\u226410001\u2264t\u22641000) \u2014 the number of test cases.The next 2t2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer nn (1\u2264n\u22642\u22c51051\u2264n\u22642\u22c5105) \u2014 the length of the robot's path. The second line of the test case contains one string ss consisting of nn characters 'L', 'R', 'U', 'D' \u2014 the robot's path.It is guaranteed that the sum of nn over all test cases does not exceed 2\u22c51052\u22c5105 (\u2211n\u22642\u22c5105\u2211n\u22642\u22c5105).OutputFor each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers ll and rr such that 1\u2264l\u2264r\u2264n1\u2264l\u2264r\u2264n \u2014 endpoints of the substring you remove. The value r\u2212l+1r\u2212l+1 should be minimum possible. If there are several answers, print any of them.ExampleInputCopy4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR\nOutputCopy1 2\n1 4\n3 4\n-1\n","tags":["data structures","implementation"],"code":"#include \n\n\n\nusing namespace std;\n\n\n\nint main() {\n\n#ifdef _DEBUG\n\n\tfreopen(\"input.txt\", \"r\", stdin);\n\n\/\/\tfreopen(\"output.txt\", \"w\", stdout);\n\n#endif\n\n\t\n\n\tint t;\n\n\tcin >> t;\n\n\twhile (t--) {\n\n\t\tint n;\n\n\t\tstring s;\n\n\t\tcin >> n >> s;\n\n\t\tint l = -1, r = n;\n\n\t\tmap, int> vis;\n\n\t\tpair cur = {0, 0};\n\n\t\tvis[cur] = 0;\n\n\t\tfor (int i = 0; i < n; ++i) {\n\n\t\t\tif (s[i] == 'L') --cur.first;\n\n\t\t\tif (s[i] == 'R') ++cur.first;\n\n\t\t\tif (s[i] == 'U') ++cur.second;\n\n\t\t\tif (s[i] == 'D') --cur.second;\n\n\t\t\tif (vis.count(cur)) {\n\n\t\t\t\tif (i - vis[cur] + 1 < r - l + 1) {\n\n\t\t\t\t\tl = vis[cur];\n\n\t\t\t\t\tr = i;\n\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t\tvis[cur] = i + 1;\n\n\t\t}\n\n\t\tif (l == -1) {\n\n\t\t\tcout << -1 << endl;\n\n\t\t} else {\n\n\t\t\tcout << l + 1 << \" \" << r + 1 << endl;\n\n\t\t}\n\n\t}\n\n\t\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1305","problem_id":"C","statement":"C. Kuroni and Impossible Calculationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTo become the king of Codeforces; Kuroni has to solve the following problem.He is given nn numbers a1,a2,\u2026,ana1,a2,\u2026,an. Help Kuroni to calculate \u220f1\u2264i\nusing namespace std;\n#include \n#include \n#include \n#define ll long long\n#define fi first\n#define se second\n#define sst string\n#define pb push_back\n#define maxco 100000+5\n#define lld long double\n#define cha ios_base::sync_with_stdio(false);\n#define ffl cout.flush();\n#define phi acos(-1)\n#define mod 1000000007\n\nll a[200069];\n\nint main(){\n ll n;\n cin>>n;\n ll m;\n cin>>m;\n for(ll i=1;i<=n;i++){\n cin>>a[i];\n }\n if(n>m){\n cout<<0<\n\nusing namespace std;\n\n#define sz(a) ((int) (a).size())\n\n#define L(i, j, k) for(int i = (j); i <= (k); ++i)\n\n#define R(i, j, k) for(int i = (j); i >= (k); --i)\n\n#define endl '\\n'\n\n#define pb push_back\n\n#define all(x) (x).begin(),(x).end()\n\ntypedef long long ll;\n\nconst int N=2e5;\n\nll gcd (ll a, ll b) {\n\n if (b == 0)\n\n return a;\n\n else\n\n return gcd (b, a % b);\n\n}\n\n\n\nbool f(string s){\n\n\tfor(int i=0;i>n>>m;\n\n bool t[n];\n\n string s[n];\n\n string x;\n\n bool tx=0;\n\n vector ans;\n\n for(int i=0;i>s[i];\n\n \tt[i]=0;\n\n \tif(tx==false&&f(s[i])){\n\n \t\tx=s[i];\n\n \t\ttx=true;\n\n\t\t}\n\n\t}\n\n\tfor(int i=0;i=0;i--){\n\n\t\tfor(int j=ans[i].size()-1;j>=0;j--){\n\n\t\t\tcout<\n\n#include \n\n#include \n\n\/\/---------------------------------------------------------------------------------\n\n#define ll long long\n\n#define fixed(n) cout << fixed << setprecision(n)\n\n#define sz(x) int(x.size())\n\n#define TC int t; cin >> t; while(t--)\n\n#define all(s) s.begin(), s.end()\n\n#define rall(s) s.rbegin(), s.rend()\n\n#define dl \"\\n\"\n\n#define Ceil(a, b) ((a \/ b) + (a % b ? 1 : 0))\n\n#define pi 3.141592\n\n#define OO 2'000'000'000\n\n#define MOD 1'000'000'007\n\n#define EPS 1e-10 \n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\n\/\/---------------------------------------------------------------------------------\n\ntemplate >\n\nusing ordered_map = tree;\n\ntemplate >\n\nusing ordered_set = ordered_map;\n\n\n\ntemplate >\n\nusing ordered_multimap = tree;\n\ntemplate >\n\nusing ordered_multiset = ordered_multimap;\n\n\/\/ order_of_key(val) count elements smaller than val\n\n\/\/ *s.find_by_order(idx) element with index idx\n\n\n\ntemplate istream& operator >> (istream &in , vector < T > &v){\n\n for(auto &i : v) in >> i ; \n\n return in ; \n\n}\n\n\n\ntemplate ostream& operator << (ostream &out ,vector < T > &v){\n\n for(auto &i : v) out << i << ' ' ; \n\n return out ; \n\n}\n\n\n\n\/\/-----------------------------------------------------------------------------------------\n\nvoid ZEDAN() {\n\n ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n\n #ifndef ONLINE_JUDGE\n\n freopen(\"input.txt\", \"r\", stdin) ;\n\n freopen(\"output.txt\", \"w\", stdout) ;\n\n #endif\n\n}\n\n\/\/-----------------------------------------(notes)-----------------------------------------\n\n\/*\n\n*\/\n\n\/\/-----------------------------------------(function)--------------------------------------\n\nll n , h , l , r ; \n\nvectorv ; \n\nvector>dp ; \n\nbool valid(ll t){\n\n return t>=l &&t<=r ; \n\n}\n\nll rec(ll i=0 , ll t=0){\n\n if(i>=n) return 0 ; \n\n ll &ret = dp[i][t] ; \n\n if(~ret) return ret ; \n\n return ret = max(valid((t+v[i])%h)+rec(i+1,(t+v[i])%h),valid((t+v[i]-1)%h)+rec(i+1,(t+v[i]-1)%h)) ; \n\n}\n\n\/\/-----------------------------------------(code here)-------------------------------------\n\nvoid solve(){\n\n ll n , h , l , r ; \n\n cin >> n >> h >> l >> r; \n\n vectorv(n) ; \n\n vector>dp(n+5,vector(h+5)) ; \n\n cin >> v ; \n\n auto valid=[&](ll t){\n\n return t>=l && t<=r ; \n\n };\n\n for(ll i=n-1 ; i>=0 ; i--){\n\n for(ll t=0 ; t> t ;\n\n while(t--){\n\n solve() ; \n\n if(t) cout << dl ; \n\n }\n\n return 0; \n\n} ","language":"cpp"} -{"contest_id":"1292","problem_id":"E","statement":"E. Rin and The Unknown Flowertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMisoilePunch\u266a - \u5f69This is an interactive problem!On a normal day at the hidden office in A.R.C. Markland-N; Rin received an artifact, given to her by the exploration captain Sagar.After much analysis, she now realizes that this artifact contains data about a strange flower, which has existed way before the New Age. However, the information about its chemical structure has been encrypted heavily.The chemical structure of this flower can be represented as a string pp. From the unencrypted papers included, Rin already knows the length nn of that string, and she can also conclude that the string contains at most three distinct letters: \"C\" (as in Carbon), \"H\" (as in Hydrogen), and \"O\" (as in Oxygen).At each moment, Rin can input a string ss of an arbitrary length into the artifact's terminal, and it will return every starting position of ss as a substring of pp.However, the artifact has limited energy and cannot be recharged in any way, since the technology is way too ancient and is incompatible with any current A.R.C.'s devices. To be specific: The artifact only contains 7575 units of energy. For each time Rin inputs a string ss of length tt, the artifact consumes 1t21t2 units of energy. If the amount of energy reaches below zero, the task will be considered failed immediately, as the artifact will go black forever. Since the artifact is so precious yet fragile, Rin is very nervous to attempt to crack the final data. Can you give her a helping hand?InteractionThe interaction starts with a single integer tt (1\u2264t\u22645001\u2264t\u2264500), the number of test cases. The interaction for each testcase is described below:First, read an integer nn (4\u2264n\u2264504\u2264n\u226450), the length of the string pp.Then you can make queries of type \"? s\" (1\u2264|s|\u2264n1\u2264|s|\u2264n) to find the occurrences of ss as a substring of pp.After the query, you need to read its result as a series of integers in a line: The first integer kk denotes the number of occurrences of ss as a substring of pp (\u22121\u2264k\u2264n\u22121\u2264k\u2264n). If k=\u22121k=\u22121, it means you have exceeded the energy limit or printed an invalid query, and you need to terminate immediately, to guarantee a \"Wrong answer\" verdict, otherwise you might get an arbitrary verdict because your solution will continue to read from a closed stream. The following kk integers a1,a2,\u2026,aka1,a2,\u2026,ak (1\u2264a1\n\n\/\/ #pragma GCC optimize(\"Ofast\")\n\n\/\/ #pragma GCC optimize(\"unroll-loops\")\n\n\/\/ #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native\")\n\nusing namespace std;\n\ninline int read(){\n\n int s=0,w=1;\n\n char ch=getchar();\n\n while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}\n\n while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();\n\n return s*w;\n\n}\n\n#ifdef local\n\nstring rin;\n\ndouble power_used;\n\nvector query(string s)\n\n{\n\n\tint N=rin.size(),M=s.size();\n\n\tpower_used+=1.0\/(M*M);\n\n\tvector res;\n\n\tfor(int i=0; i+M<=N; ++i)\n\n\t\tif(rin.substr(i,M)==s) res.push_back(i);\n\n\treturn res;\n\n}\n\nbool check(string s)\n\n{\n\n\tcerr<1.4)\n\n\t{\n\n\t\tcout<<\"WA\"< query(string s)\n\n{\n\n\tprintf(\"? \");\n\n\tfor(char i:s) putchar(i);\n\n\tputs(\"\");\n\n\tvector res;\n\n\tfflush(stdout);\n\n\tfor(int T=read(); T--;) res.push_back(read()-1);\n\n\treturn res;\n\n}\n\nbool check(string s)\n\n{\n\n\tprintf(\"! \");\n\n\tfor(char i:s) putchar(i);\n\n\tputs(\"\");\n\n\tfflush(stdout);\n\n\treturn read();\n\n}\n\n#endif\n\nbool query(string s,int pos)\n\n{\n\n\tvector v=query(s);\n\n\tfor(auto i:v) if(i==pos) return 1;\n\n\treturn 0;\n\n}\n\nsigned main()\n\n{\n\n\tfor(int T=read();T--;)\n\n\t{\n\n\t\tint n=read();\n\n#ifdef local\n\n\t\tpower_used=0,cin>>rin;\n\n#endif\n\n\t\tstring ans(n,'A');\n\n\t\tif(n>4)\n\n\t\t{\n\n\t\t\tvector \n\n\t\t\t\tvcc=query(\"CC\"),\n\n\t\t\t\tvch=query(\"CH\"),\n\n\t\t\t\tvco=query(\"CO\"),\n\n\t\t\t\tvoh=query(\"OH\"),\n\n\t\t\t\tvho=query(\"HO\");\n\n\t\t\tint z=233333;\n\n\t\t\tfor(int i:vcc) ans[i]='C',ans[i+1]='C',z=min(z,i);\n\n\t\t\tfor(int i:vch) ans[i]='C',ans[i+1]='H',z=min(z,i);\n\n\t\t\tfor(int i:vco) ans[i]='C',ans[i+1]='O',z=min(z,i);\n\n\t\t\tfor(int i:voh) ans[i]='O',ans[i+1]='H',z=min(z,i);\n\n\t\t\tfor(int i:vho) ans[i]='H',ans[i+1]='O',z=min(z,i);\n\n\t\t\tif(z==233333)\n\n\t\t\t{\n\n\t\t\t\tstring sc(n-1,'C'),sh(n-1,'H'),so(n-1,'O');\n\n\t\t\t\tvector vh=query(sh);\n\n\t\t\t\tif(vh.empty()||vh[0]!=0) ans=so+\"A\";\n\n\t\t\t\telse ans=sh+\"A\";\n\n\t\t\t\tans[n-1]='C';\n\n\t\t\t\tif(ans.substr(n-2,2)==\"OH\"||\n\n\t\t\t\tans.substr(n-2,2)==\"HO\"||\n\n\t\t\t\tquery(ans).empty())\n\n\t\t\t\t{\n\n\t\t\t\t\tans[n-1]='H';\n\n\t\t\t\t\tif(ans.substr(n-2,2)==\"OH\"||\n\n\t\t\t\tans.substr(n-2,2)==\"HO\"||query(ans).empty()) \n\n\t\t\t\t\tans[n-1]='O';\n\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t\telse\n\n\t\t\t{\n\n\t\t\t\tfor(int i=z+1; i+1 \n\n\t\t\t\tvcc=query(\"CC\"),\n\n\t\t\t\tvch=query(\"CH\"),\n\n\t\t\t\tvco=query(\"CO\");\n\n\t\t\tfor(int i:vcc) ans[i]='C',ans[i+1]='C';\n\n\t\t\tfor(int i:vch) ans[i]='C',ans[i+1]='H';\n\n\t\t\tfor(int i:vco) ans[i]='C',ans[i+1]='O';\n\n\t\t\tconst int pos1[]={2,0,1};\n\n\t\t\tconst int pos2[]={0,0,1};\n\n\t\t\tconst int pos3[]={3,3,0};\n\n\t\t\tbool flg=0;\n\n\t\t\tfor(int i=0; i<3; ++i)\n\n\t\t\t\tif(ans[i]=='C')\n\n\t\t\t\t{\n\n\t\t\t\t\tif(ans[pos1[i]]=='A')\n\n\t\t\t\t\t{\n\n\t\t\t\t\t\tans[pos1[i]]='C';\n\n\t\t\t\t\t\tif(!query(ans.substr(pos2[i],3),pos2[i]))\n\n\t\t\t\t\t\t{\n\n\t\t\t\t\t\t\tans[pos1[i]]='H';\n\n\t\t\t\t\t\t\tif(!query(ans.substr(pos2[i],3),pos2[i]))\n\n\t\t\t\t\t\t\t\tans[pos1[i]]='O';\n\n\t\t\t\t\t\t}\n\n\t\t\t\t\t}\n\n\t\t\t\t\tif(ans[pos3[i]]=='A')\n\n\t\t\t\t\t{\n\n\t\t\t\t\t\tans[pos3[i]]='C';\n\n\t\t\t\t\t\tif(!query(ans,0))\n\n\t\t\t\t\t\t{\n\n\t\t\t\t\t\t\tans[pos3[i]]='H';\n\n\t\t\t\t\t\t\tif(!query(ans,0))\n\n\t\t\t\t\t\t\t\tans[pos3[i]]='O';\n\n\t\t\t\t\t\t}\n\n\t\t\t\t\t}\n\n\t\t\t\t\tflg=1;\n\n\t\t\t\t\tbreak;\n\n\t\t\t\t}\n\n\t\t\tif(!flg)\n\n\t\t\t{\n\n\t\t\t\tvector t=query(\"OH\");\n\n\t\t\t\tif(t.empty())\n\n\t\t\t\t{\n\n\t\t\t\t\tvector tt=query(\"HH\");\n\n\t\t\t\t\tans=\"HOOC\";\n\n\t\t\t\t\tfor(int i:tt) ans[i]=ans[i+1]='H';\n\n\t\t\t\t\tif(ans[1]=='H')\n\n\t\t\t\t\t{\n\n\t\t\t\t\t\tans[3]='C';\n\n\t\t\t\t\t\tif(!query(ans,0)) \n\n\t\t\t\t\t\t{\n\n\t\t\t\t\t\t\tans[3]='O';\n\n\t\t\t\t\t\t\tif(!query(ans,0)) ans[3]='H';\n\n\t\t\t\t\t\t}\n\n\t\t\t\t\t}\n\n\t\t\t\t\telse\n\n\t\t\t\t\t{\n\n\t\t\t\t\t\tassert(ans[2]=='O');\n\n\t\t\t\t\t\tvector ttt=query(\"OOO\");\n\n\t\t\t\t\t\tfor(int i:ttt)\n\n\t\t\t\t\t\t\tans[i*3]='O';\n\n\t\t\t\t\t}\n\n\t\t\t\t}\n\n\t\t\t\telse\n\n\t\t\t\t{\n\n\t\t\t\t\tans[t[0]]='O',ans[t[0]+1]='H',ans[pos1[t[0]]]='O';\n\n\t\t\t\t\tif(!query(ans.substr(pos2[t[0]],3),pos2[t[0]])) \n\n\t\t\t\t\t\tans[pos1[t[0]]]='H';\n\n\t\t\t\t\tans[pos3[t[0]]]='C';\n\n\t\t\t\t\tif(!query(ans,0))\n\n\t\t\t\t\t{\n\n\t\t\t\t\t\tans[pos3[t[0]]]='H';\n\n\t\t\t\t\t\tif(!query(ans,0)) ans[pos3[t[0]]]='O';\n\n\t\t\t\t\t}\n\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t}\n\n\t\tif(!check(ans)) return 0;\n\n\t}\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1320","problem_id":"C","statement":"C. World of Darkraft: Battle for Azathothtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputRoma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind.Roma has a choice to buy exactly one of nn different weapons and exactly one of mm different armor sets. Weapon ii has attack modifier aiai and is worth caicai coins, and armor set jj has defense modifier bjbj and is worth cbjcbj coins.After choosing his equipment Roma can proceed to defeat some monsters. There are pp monsters he can try to defeat. Monster kk has defense xkxk, attack ykyk and possesses zkzk coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster kk can be defeated with a weapon ii and an armor set jj if ai>xkai>xk and bj>ykbj>yk. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one.Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins.Help Roma find the maximum profit of the grind.InputThe first line contains three integers nn, mm, and pp (1\u2264n,m,p\u22642\u22c51051\u2264n,m,p\u22642\u22c5105)\u00a0\u2014 the number of available weapons, armor sets and monsters respectively.The following nn lines describe available weapons. The ii-th of these lines contains two integers aiai and caicai (1\u2264ai\u22641061\u2264ai\u2264106, 1\u2264cai\u22641091\u2264cai\u2264109)\u00a0\u2014 the attack modifier and the cost of the weapon ii.The following mm lines describe available armor sets. The jj-th of these lines contains two integers bjbj and cbjcbj (1\u2264bj\u22641061\u2264bj\u2264106, 1\u2264cbj\u22641091\u2264cbj\u2264109)\u00a0\u2014 the defense modifier and the cost of the armor set jj.The following pp lines describe monsters. The kk-th of these lines contains three integers xk,yk,zkxk,yk,zk (1\u2264xk,yk\u22641061\u2264xk,yk\u2264106, 1\u2264zk\u22641031\u2264zk\u2264103)\u00a0\u2014 defense, attack and the number of coins of the monster kk.OutputPrint a single integer\u00a0\u2014 the maximum profit of the grind.ExampleInputCopy2 3 3\n2 3\n4 7\n2 4\n3 2\n5 11\n1 2 4\n2 1 6\n3 4 6\nOutputCopy1\n","tags":["brute force","data structures","sortings"],"code":"#include \n\ntypedef long long ll;\n\ntypedef long double ld;\n\nusing namespace std;\n\n#include \n\nusing namespace __gnu_pbds;\n\ntypedef tree,rb_tree_tag,\n\ntree_order_statistics_node_update> indexed_set;\n\n#define endl '\\n'\n\n#define ilihg ios_base::sync_with_stdio(false);cin.tie(NULL)\n\n\n\nint c[1000001];\n\nvoid build(int i,int l,int r,int a[],vector &t){\n\n if(l==r){\n\n t[i]=a[l];\n\n }\n\n else{\n\n int mid=(l+r)\/2;\n\n build(2*i+1,l,mid,a,t);\n\n build(2*i+2,mid+1,r,a,t);\n\n t[i]=max(t[2*i+1],t[2*i+2]);\n\n }\n\n}\n\nvoid propagate(int i,int l,int r,vector &t,vector &p){\n\n t[i]+=p[i];\n\n if(l!=r){\n\n p[2*i+1]+=p[i];\n\n p[2*i+2]+=p[i];\n\n }\n\n p[i]=0;\n\n}\n\nvoid update(int i,int tl,int tr,int l,int r,ll val,vector &t,vector &p){\n\n propagate(i,tl,tr,t,p);\n\n if(tl>=l&&tr<=r){\n\n p[i]+=val;\n\n }\n\n else{\n\n int mid=(tl+tr)\/2;\n\n if(mid>=l){\n\n update(2*i+1,tl,mid,l,r,val,t,p);\n\n }\n\n if(mid+1<=r){\n\n update(2*i+2,mid+1,tr,l,r,val,t,p);\n\n }\n\n propagate(2*i+1,tl,mid,t,p);\n\n propagate(2*i+2,mid+1,tr,t,p);\n\n t[i]=max(t[2*i+1],t[2*i+2]);\n\n }\n\n}\n\nll query(int i,int tl,int tr,int l,int r,vector &t,vector &p){\n\n propagate(i,tl,tr,t,p);\n\n if(tl>=l&&tr<=r){\n\n return t[i];\n\n }\n\n else{\n\n int mid=(tl+tr)\/2;\n\n ll k=-1e9;\n\n if(mid>=l){\n\n k=max(k,query(2*i+1,tl,mid,l,r,t,p));\n\n }\n\n if(mid+1<=r){\n\n k=max(k,query(2*i+2,mid+1,tr,l,r,t,p));\n\n }\n\n return k;\n\n }\n\n}\n\nint main(){\n\n#ifndef ONLINE_JUDGE\n\n\tfreopen(\"input.txt\",\"r\",stdin);\n\n\tfreopen(\"output.txt\",\"w\",stdout);\n\n#endif\n\n\tclock_t time_req=clock();\n\n\tilihg;\n\n\tint t=1;\n\n \/\/ cin>>t;\n\n while(t--){\n\n \tint n,m,q;\n\n \tcin>>n>>m>>q;\n\n \tpair a[n],b[m];\n\n \tfor(int i=0;i>a[i].first>>a[i].second;\n\n \t}\n\n \tsort(a,a+n);\n\n \tfor(int i=0;i>b[i].first>>b[i].second;\n\n \t}\n\n \tsort(b,b+m);\n\n \tint k=-2e9;\n\n \tint j=m-1;\n\n \tfor(int i=1000000;i>=0;i--){\n\n \t\twhile(j>=0&&b[j].first>i){\n\n \t\t\tk=max(k,-b[j].second);\n\n \t\t\tj--;\n\n \t\t}\n\n \t\tc[i]=k;\n\n \t}\n\n \tvector t(4000001),p(4000001);\n\n \tbuild(0,0,1000000,c,t);\n\n \tvector,int>> v(q);\n\n \tfor(int i=0;i>v[i].first.first>>v[i].first.second>>v[i].second;\n\n \t}\n\n \tsort(v.begin(),v.end());\n\n \tj=0;\n\n \tll ans=-2e9;\n\n \tfor(int i=0;i\n\n#define mset(a,b) memset((a),(b),sizeof((a)))\n\n#define rep(i,l,r) for(int i=(l);i<=(r);i++)\n\n#define dec(i,l,r) for(int i=(r);i>=(l);i--)\n\n#define cmax(a,b) (((a)<(b))?(a=b):(a))\n\n#define cmin(a,b) (((a)>(b))?(a=b):(a))\n\n#define Next(k) for(int x=head[k];x;x=li[x].next)\n\n#define vc vector\n\n#define ar array\n\n#define pi pair\n\n#define fi first\n\n#define se second\n\n#define mp make_pair\n\n#define pb push_back\n\n#define N 21\n\n#define M 2000100\n\nusing namespace std;\n\n\n\ntypedef double dd;\n\ntypedef long double ld;\n\ntypedef long long ll;\n\ntypedef unsigned int uint;\n\ntypedef unsigned long long ull;\n\n#define int long long\n\ntypedef pair P;\n\ntypedef vector vi;\n\n\n\nconst int INF=0x3f3f3f3f;\n\nconst dd eps=1e-9;\n\n\n\ntemplate inline void read(T &x) {\n\n x=0; int f=1;\n\n char c=getchar();\n\n for(;!isdigit(c);c=getchar()) if(c == '-') f=-f;\n\n for(;isdigit(c);c=getchar()) x=x*10+c-'0';\n\n x*=f;\n\n}\n\n\n\nint n,a[N],f[M],lg[M],b[N],bt,sum[M];\n\n\n\ninline vi Get(int nl,int nr){\n\n vi v;v.clear();\n\n if(nl==nr){\n\n if(b[nl]>0){v.pb(-b[nl]);v.pb(b[nl]);}\n\n else{v.pb(b[nl]);v.pb(-b[nl]);}return v;\n\n }\n\n v=Get(nl+1,nr);\n\n vi v1,v2;v1.clear();v2.clear();\n\n rep(i,0,(int)v.size()-1) v1.pb(v[i]-b[nl]);\n\n rep(i,0,(int)v.size()-1) v2.pb(v[i]+b[nl]);\n\n vi now;now.clear();\n\n int l=0,r=0;\n\n while(l<(int)v1.size()&&r<(int)v2.size()){\n\n if(v1[l]>i)&1) b[++bt]=a[i+1];int mid=(1+bt)>>1;\n\n vi L=Get(1,mid),R=Get(mid+1,bt);\n\n \/\/ printf(\"mid=%d\\n\",mid);\n\n int l=R.size(),r=R.size()-1;\n\n \/\/ puts(\"L:\");for(int x:L) printf(\"%d \",x);puts(\"\");\n\n \/\/ puts(\"R:\");for(int x:R) printf(\"%d \",x);puts(\"\");\n\n int nd=1+(abs(sum[S])<=siz)*2;\n\n \/\/ printf(\"nd=%d\\n\",nd);\n\n for(int i=0;i=-siz) l--;\n\n while(r>=0&&L[i]+R[r]>siz) r--;\n\n \/\/ printf(\"l=%d r=%d\\n\",l,r);\n\n nd-=min(nd,r-l+1);\n\n \/\/ if(l<=r&&r!=R.size()) return 1;\n\n \/\/ if(r==-1) return 0;\n\n }\n\n return (nd==0);\n\n}\n\n\n\nsigned main(){\n\n \/\/ freopen(\"my.in\",\"r\",stdin);\n\n \/\/ freopen(\"my.out\",\"w\",stdout);\n\n read(n);rep(i,1,n) read(a[i]),lg[1<>i)&1) sum[j]+=sum[j^(1<\n\nusing namespace std;\n\n\n\nint dp[401][401][401], n, m;\n\nstring s, t;\n\n\n\nint solve(int i, int j, int k) {\n\n if (i == n) {\n\n if (k == m)\n\n return j;\n\n return -2;\n\n }\n\n\n\n int &res = dp[i][j][k];\n\n if (res != -1)\n\n return res;\n\n \n\n res = solve(i + 1, j, k);\n\n if (j < m && s[i] == t[j])\n\n res = max(res, solve(i + 1, j + 1, k));\n\n if (k < m && s[i] == t[k])\n\n res = max(res, solve(i + 1, j, k + 1));\n\n \n\n return res;\n\n}\n\nint main() {\n\n ios_base::sync_with_stdio(0);\n\n cin.tie(0);\n\n\n\n int tt;\n\n cin >> tt;\n\n while (tt--) {\n\n bool ok = false;\n\n cin >> s >> t;\n\n n = s.size();\n\n m = t.size();\n\n\n\n for (int i = 0; i <= n; i++)\n\n for (int j = 0; j <= m; j++)\n\n for (int k = 0; k <= m; k++)\n\n dp[i][j][k] = -1;\n\n \n\n for (int i = 0; i < m; i++)\n\n ok |= (solve(0, 0, i + 1) > i);\n\n \n\n cout << (ok ? \"YES\\n\" : \"NO\\n\");\n\n }\n\n}\n\n \t \t \t\t\t \t\t\t \t\t\t \t \t \t\t\t","language":"cpp"} -{"contest_id":"1325","problem_id":"D","statement":"D. Ehab the Xorcisttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven 2 integers uu and vv, find the shortest array such that bitwise-xor of its elements is uu, and the sum of its elements is vv.InputThe only line contains 2 integers uu and vv (0\u2264u,v\u22641018)(0\u2264u,v\u22641018).OutputIf there's no array that satisfies the condition, print \"-1\". Otherwise:The first line should contain one integer, nn, representing the length of the desired array. The next line should contain nn positive integers, the array itself. If there are multiple possible answers, print any.ExamplesInputCopy2 4\nOutputCopy2\n3 1InputCopy1 3\nOutputCopy3\n1 1 1InputCopy8 5\nOutputCopy-1InputCopy0 0\nOutputCopy0NoteIn the first sample; 3\u22951=23\u22951=2 and 3+1=43+1=4. There is no valid array of smaller length.Notice that in the fourth sample the array is empty.","tags":["bitmasks","constructive algorithms","greedy","number theory"],"code":"#include \nusing namespace std;\nint main(){\nlong long n,m,x;\ncin>>n>>m;\nif(n>m||(n&1)!=(m&1)) cout<<-1;\nelse if(n==m){\nif(n) cout<<\"1\\n\"<>1;\nif(x&n) cout<<\"3\\n\"<\n\nusing namespace std;\n\ntypedef long long ll;\n\n\n\nstruct cust{\n\n ll t,l,h;\n\n};\n\n\n\nvoid solve(){\n\n ll n,m,lo,hi;\n\n cin>>n>>m;\n\n lo=hi=m;\n\n vector a(n);\n\n for(auto &x:a)cin>>x.t>>x.l>>x.h;\n\n for(int i=n-1;i>0;i--)a[i].t-=a[i-1].t;\n\n for(int i=0;ihi+a[i].t){\n\n cout<<\"NO\\n\";\n\n return;\n\n }\n\n lo=max(lo-a[i].t,a[i].l);\n\n hi=min(hi+a[i].t,a[i].h);\n\n }\n\n cout<<\"YES\\n\";\n\n return;\n\n}\n\n\n\nint main() {\n\n ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\n int t=1;\n\n cin>>t;\n\n while(t--)solve();\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1290","problem_id":"F","statement":"F. Making Shapestime limit per test5 secondsmemory limit per test768 megabytesinputstandard inputoutputstandard outputYou are given nn pairwise non-collinear two-dimensional vectors. You can make shapes in the two-dimensional plane with these vectors in the following fashion: Start at the origin (0,0)(0,0). Choose a vector and add the segment of the vector to the current point. For example, if your current point is at (x,y)(x,y) and you choose the vector (u,v)(u,v), draw a segment from your current point to the point at (x+u,y+v)(x+u,y+v) and set your current point to (x+u,y+v)(x+u,y+v). Repeat step 2 until you reach the origin again.You can reuse a vector as many times as you want.Count the number of different, non-degenerate (with an area greater than 00) and convex shapes made from applying the steps, such that the shape can be contained within a m\u00d7mm\u00d7m square, and the vectors building the shape are in counter-clockwise fashion. Since this number can be too large, you should calculate it by modulo 998244353998244353.Two shapes are considered the same if there exists some parallel translation of the first shape to another.A shape can be contained within a m\u00d7mm\u00d7m square if there exists some parallel translation of this shape so that every point (u,v)(u,v) inside or on the border of the shape satisfies 0\u2264u,v\u2264m0\u2264u,v\u2264m.InputThe first line contains two integers nn and mm \u00a0\u2014 the number of vectors and the size of the square (1\u2264n\u226451\u2264n\u22645, 1\u2264m\u22641091\u2264m\u2264109).Each of the next nn lines contains two integers xixi and yiyi \u00a0\u2014 the xx-coordinate and yy-coordinate of the ii-th vector (|xi|,|yi|\u22644|xi|,|yi|\u22644, (xi,yi)\u2260(0,0)(xi,yi)\u2260(0,0)).It is guaranteed, that no two vectors are parallel, so for any two indices ii and jj such that 1\u2264i\n\n#define fi(s) freopen(s\".in\",\"r\",stdin)\n\n#define fo(s) freopen(s\".out\",\"w\",stdout)\n\n#define file(s) fi(s),fo(s)\n\nusing namespace std;\n\nnamespace tomorinao {char arrs;\n\ntypedef long long ll;\n\nconst int mod = 998244353;\n\nint MOD(int x) {return x>=mod?x-mod:x;}\n\nint f[31][25][25][25][25][2][2],w[31],n,m,X[5],Y[5];\n\nint fin(int x,int zx,int zy,int fx,int fy,int lzx,int lzy) {\n\n\tif(x==w[0]+1) return !zx&&!zy&&!fx&&!fy&&lzx&&lzy;\n\n\tint&r=f[x][zx][zy][fx][fy][lzx][lzy];if(~r) return r;r=0;\n\n\tfor(int S=0;S<1<>i&1) {\n\n\t\t\tif(X[i]>0) nzx+=X[i];else nfx-=X[i];\n\n\t\t\tif(Y[i]>0) nzy+=Y[i];else nfy-=Y[i];\n\n\t\t}\n\n\t\tif((nzx&1)^(nfx&1)||(nzy&1)^(nfy&1)) continue;\n\n\t\tr=MOD(r+fin(x+1,nzx>>1,nzy>>1,nfx>>1,nfy>>1,(nzx&1)>n>>m,memset(f,-1,sizeof(f));\n\n\tfor(int i=0;i>X[i]>>Y[i];\n\n\twhile(m) w[++w[0]]=m&1,m>>=1;\n\n\tcout<\nusing namespace std;\n\nint main()\n{\n long long t,n,m;\n cin >> t;\n while (t--) {\n cin >> n >> m;\n if (n % m == 0)\n cout << \"YES\"<\n\nusing namespace std;\n\nusing ll = long long;\n\nauto solve() {\n\n int n, x;\n\n cin >> n >> x;\n\n string s;\n\n cin >> s;\n\n int zero = 0, one = 0;\n\n map mp;\n\n int mi = 0, mx = 0;\n\n for (auto i : s) {\n\n if (i == '0')\n\n zero++;\n\n else\n\n one++;\n\n mp[zero - one]++;\n\n mi = min(mi, zero - one);\n\n mx = max(mx, zero - one);\n\n }\n\n \/\/ cout << mi << \" \" << mx << \" \";\n\n if (zero - one == 0) {\n\n if (mp[x] == 0 && x != 0) {\n\n cout << \"0\\n\";\n\n } else {\n\n cout << \"-1\\n\";\n\n }\n\n } else {\n\n int sum = 0;\n\n if (x == 0)\n\n sum++;\n\n if(x < mi && zero - one < 0){\n\n int dif = abs(mi - x);\n\n dif \/= abs(zero - one);\n\n x += dif * abs(zero - one);\n\n }\n\n while (x < mi && zero - one < 0) {\n\n x -= zero - one;\n\n }\n\n if (x > mx && zero - one > 0) {\n\n int dif = abs(x - mx);\n\n dif \/= abs(zero-one);\n\n x -= dif * abs(zero - one);\n\n }\n\n while (x > mx && zero - one > 0) {\n\n x -= zero - one;\n\n }\n\n while (x >= mi && x <= mx) {\n\n sum += mp[x];\n\n x -= zero - one;\n\n }\n\n cout << sum << \"\\n\";\n\n }\n\n}\n\nauto main() -> int {\n\n ios::sync_with_stdio(false);\n\n cin.tie(nullptr), cout.tie(nullptr);\n\n int _ = 1;\n\n cin >> _;\n\n while (_--) {\n\n solve();\n\n }\n\n}","language":"cpp"} -{"contest_id":"1322","problem_id":"D","statement":"D. Reality Showtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputA popular reality show is recruiting a new cast for the third season! nn candidates numbered from 11 to nn have been interviewed. The candidate ii has aggressiveness level lili, and recruiting this candidate will cost the show sisi roubles.The show host reviewes applications of all candidates from i=1i=1 to i=ni=n by increasing of their indices, and for each of them she decides whether to recruit this candidate or not. If aggressiveness level of the candidate ii is strictly higher than that of any already accepted candidates, then the candidate ii will definitely be rejected. Otherwise the host may accept or reject this candidate at her own discretion. The host wants to choose the cast so that to maximize the total profit.The show makes revenue as follows. For each aggressiveness level vv a corresponding profitability value cvcv is specified, which can be positive as well as negative. All recruited participants enter the stage one by one by increasing of their indices. When the participant ii enters the stage, events proceed as follows: The show makes clicli roubles, where lili is initial aggressiveness level of the participant ii. If there are two participants with the same aggressiveness level on stage, they immediately start a fight. The outcome of this is: the defeated participant is hospitalized and leaves the show. aggressiveness level of the victorious participant is increased by one, and the show makes ctct roubles, where tt is the new aggressiveness level. The fights continue until all participants on stage have distinct aggressiveness levels. It is allowed to select an empty set of participants (to choose neither of the candidates).The host wants to recruit the cast so that the total profit is maximized. The profit is calculated as the total revenue from the events on stage, less the total expenses to recruit all accepted participants (that is, their total sisi). Help the host to make the show as profitable as possible.InputThe first line contains two integers nn and mm (1\u2264n,m\u226420001\u2264n,m\u22642000) \u2014 the number of candidates and an upper bound for initial aggressiveness levels.The second line contains nn integers lili (1\u2264li\u2264m1\u2264li\u2264m) \u2014 initial aggressiveness levels of all candidates.The third line contains nn integers sisi (0\u2264si\u226450000\u2264si\u22645000) \u2014 the costs (in roubles) to recruit each of the candidates.The fourth line contains n+mn+m integers cici (|ci|\u22645000|ci|\u22645000) \u2014 profitability for each aggrressiveness level.It is guaranteed that aggressiveness level of any participant can never exceed n+mn+m under given conditions.OutputPrint a single integer\u00a0\u2014 the largest profit of the show.ExamplesInputCopy5 4\n4 3 1 2 1\n1 2 1 2 1\n1 2 3 4 5 6 7 8 9\nOutputCopy6\nInputCopy2 2\n1 2\n0 0\n2 1 -100 -100\nOutputCopy2\nInputCopy5 4\n4 3 2 1 1\n0 2 6 7 4\n12 12 12 6 -3 -5 3 10 -4\nOutputCopy62\nNoteIn the first sample case it is optimal to recruit candidates 1,2,3,51,2,3,5. Then the show will pay 1+2+1+1=51+2+1+1=5 roubles for recruitment. The events on stage will proceed as follows: a participant with aggressiveness level 44 enters the stage, the show makes 44 roubles; a participant with aggressiveness level 33 enters the stage, the show makes 33 roubles; a participant with aggressiveness level 11 enters the stage, the show makes 11 rouble; a participant with aggressiveness level 11 enters the stage, the show makes 11 roubles, a fight starts. One of the participants leaves, the other one increases his aggressiveness level to 22. The show will make extra 22 roubles for this. Total revenue of the show will be 4+3+1+1+2=114+3+1+1+2=11 roubles, and the profit is 11\u22125=611\u22125=6 roubles.In the second sample case it is impossible to recruit both candidates since the second one has higher aggressiveness, thus it is better to recruit the candidate 11.","tags":["bitmasks","dp"],"code":"\/\/ LUOGU_RID: 100118329\n#include \n\n#include \n\n#include \n\nusing std :: max;\n\nnamespace nagisa{\n\n\tconst int maxn = 2005;\n\n\tint n,m,l[maxn],c[maxn],p[maxn*2],dp[maxn*2][maxn];\n\n\tint main(){\n\n\t\tscanf(\"%d %d\",&n,&m);\n\n\t\tfor(int i=1;i<=n;++i)scanf(\"%d\",&l[i]);\t\t\n\n\t\tfor(int i=1;i<=n;++i)scanf(\"%d\",&c[i]);\t\t\n\n\t\tfor(int i=1;i<=n+m;++i)scanf(\"%d\",&p[i]);\n\n\t\tstd :: reverse(l+1,l+n+1),std :: reverse(c+1,c+n+1);\n\n\t\tmemset(dp,0xcf,sizeof(dp)),dp[0][0] = 0;\n\n\t\tfor(int i=0;i<=n;++i){\n\n\t\t\tfor(int j=n-1;~j;--j)dp[l[i]][j+1] = max(dp[l[i]][j+1],dp[l[i]][j]-c[i]+p[l[i]]);\n\n\t\t\tfor(int j=l[i],b=n;j\n\nusing namespace std;\n\nlong long count(int a,int b)\n\n{\n\n if(b<10)\n\n {\n\n return 0;\n\n }\n\n else\n\n {\n\n return a+count(a,b\/10);\n\n }\n\n}\n\n\n\nvoid solve()\n\n{\n\n int a,b;\n\n cin >> a >> b;\n\n int dub=b;\n\n int k=0;\n\n int d=0;\n\n while(dub)\n\n {\n\n d++;\n\n if(dub%10==9)\n\n k++;\n\n dub=dub\/10;\n\n \n\n }\n\n if(k==d)\n\n cout << count(a,b)+a;\n\n else\n\n cout << count(a,b);\n\n cout << endl;\n\n}\n\nint main()\n\n{\n\n int t;\n\n cin >> t;\n\n while(t--)\n\n solve();\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1284","problem_id":"B","statement":"B. New Year and Ascent Sequencetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputA sequence a=[a1,a2,\u2026,al]a=[a1,a2,\u2026,al] of length ll has an ascent if there exists a pair of indices (i,j)(i,j) such that 1\u2264i\nusing namespace std;\nint main(){\nios::sync_with_stdio(0);\ncin.tie(0);\nlong long t,sum;\nint n,a;\ncin>>t;\nsum=t*t;\nvector x;\nvector y;\nfor(int i=0;i>n;\nint ma=INT_MIN,mi=INT_MAX,b=INT_MAX;\nbool can=false;\nfor(int j=0;j>a;\nif(a>b){\ncan=true;\n}else\nb=a;\nma=max(ma,a);\nmi=min(mi,a);\n}\nif(!can){\nx.push_back(mi);\ny.push_back(ma);\n}\n}\nsort(y.begin(),y.end());\nfor(int i=0;i\n\n\n\nint n, k;\n\n\n\nstd::vector> dp[2][2];\n\n\n\nvoid checkMax(int &a, int b) { a = std::max(a, b); }\n\n\n\nint main() {\n\n std::ios::sync_with_stdio(false);\n\n std::cin.tie(nullptr);\n\n std::cin >> n >> k;\n\n for (int i = 0; i < 2; ++i) {\n\n for (int j = 0; j < 2; ++j) {\n\n dp[i][j].resize(n + 1);\n\n for (int k = 0; k <= n; ++k) {\n\n dp[i][j][k].assign(1 << (n - k), (i | j) ? -1 : 0);\n\n }\n\n }\n\n }\n\n for (int i = 0; i < k; ++i) {\n\n int a;\n\n std::cin >> a, --a;\n\n dp[1][0][0][a] = 0;\n\n }\n\n for (int i = 0; i < (1 << (n - 1)); ++i) {\n\n if (dp[1][0][0][2 * i] != -1 && dp[1][0][0][2 * i + 1] != -1) {\n\n dp[1][1][1][i] = 1;\n\n }\n\n if (dp[1][0][0][2 * i] != -1 || dp[1][0][0][2 * i + 1] != -1) {\n\n dp[1][0][1][i] = dp[0][1][1][i] = 1;\n\n }\n\n }\n\n for (int i = 2; i <= n; ++i) {\n\n for (int j = 0; j < (1 << (n - i)); ++j) {\n\n for (int flu = 0; flu < 2; ++flu) {\n\n for (int fll = 0; fll < 2; ++fll) {\n\n if (dp[flu][fll][i - 1][2 * j] != -1) {\n\n for (int fru = 0; fru < 2; ++fru) {\n\n for (int frl = 0; frl < 2; ++frl) {\n\n if (dp[fru][frl][i - 1][2 * j + 1] != -1) {\n\n for (int ru = 0; ru < 2; ++ru) {\n\n for (int rl1 = 0; rl1 < 2; ++rl1) {\n\n for (int rl2 = 0; rl2 < 2; ++rl2) {\n\n checkMax(\n\n dp[ru ? flu : fru]\n\n [rl2 ? (ru ? fru : flu)\n\n : rl1 ? fll\n\n : frl][i][j],\n\n dp[flu][fll][i - 1][2 * j] +\n\n dp[fru][frl][i - 1]\n\n [2 * j + 1] +\n\n (flu | fru) +\n\n (fll | frl) +\n\n ((rl1 ? fll : frl) |\n\n (ru ? fru : flu)));\n\n }\n\n }\n\n }\n\n }\n\n }\n\n }\n\n }\n\n }\n\n }\n\n }\n\n }\n\n int ans = 0;\n\n for (int fu = 0; fu < 2; ++fu) {\n\n for (int fl = 0; fl < 2; ++fl) {\n\n checkMax(ans, dp[fu][fl][n][0] + (fu | fl));\n\n }\n\n }\n\n std::cout << ans << \"\\n\";\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1294","problem_id":"B","statement":"B. Collecting Packagestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a robot in a warehouse and nn packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0)(0,0). The ii-th package is at the point (xi,yi)(xi,yi). It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point (0,0)(0,0) doesn't contain a package.The robot is semi-broken and only can move up ('U') and right ('R'). In other words, in one move the robot can go from the point (x,y)(x,y) to the point (x+1,yx+1,y) or to the point (x,y+1)(x,y+1).As we say above, the robot wants to collect all nn packages (in arbitrary order). He wants to do it with the minimum possible number of moves. If there are several possible traversals, the robot wants to choose the lexicographically smallest path.The string ss of length nn is lexicographically less than the string tt of length nn if there is some index 1\u2264j\u2264n1\u2264j\u2264n that for all ii from 11 to j\u22121j\u22121 si=tisi=ti and sj\n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n\n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\n#define ll long long\n\n#define ld long double\n\n#define el '\\n'\n\n#define pi 3.14159265358979323846\n\n#define NumOfDigit(w) (int)(log10(w) + 1)\n\n#define fr(p_) for(auto &p__ : p_) cout< fixed << setprecision\n\n#define orderedset template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n\n\n#define tree typedef tree, rb_tree_tag, tree_order_statistics_node_update> indexed_set;\n\n\/\/member functions :\n\n\/\/1. order_of_key(k) : number of elements strictly lesser than k\n\n\/\/2. find_by_order(k) : k-th element in the set\n\n\n\n\n\nbool sortbyPair(const pair &a, const pair &b) {\n\n if (a.second != b.second)\n\n return (a.second < b.second);\n\n else\n\n return (a.first < b.first);\n\n}\n\n\n\n\n\nll fast_power(ll base, ll exp) {\n\n if (!exp) return 1;\n\n\n\n ll ans = fast_power(base, exp \/ 2);\n\n ans = (ans % 1000000007 * ans % 1000000007) % 1000000007;\n\n if (exp & 1)\n\n ans = (ans % 1000000007 * base % 1000000007) % 1000000007;\n\n return ans;\n\n}\n\n\n\n\n\nll convert_binary_to_decimal(long long n) {\n\n int dec = 0, i = 0, rem;\n\n\n\n while (n != 0) {\n\n rem = n % 10;\n\n n \/= 10;\n\n dec += rem * pow(2, i);\n\n i++;\n\n }\n\n return dec;\n\n}\n\n\n\n\n\nbool is_prime(ll n) {\n\n if (n == 0 or n == 1)\n\n return 0;\n\n\n\n for (int i = 2; i < n; ++i) {\n\n if (n % i == 0)\n\n return 0;\n\n }\n\n return 1;\n\n}\n\n\n\n\n\nll gcd(ll a, ll b) {\n\n if (b == 0)\n\n return a;\n\n return gcd(b, a % b);\n\n}\n\n\n\n\n\nll lcm(ll a, ll b) {\n\n return ((((a \/ gcd(a, b)) % 1000000009) * (b % 1000000009)) % 1000000009);\n\n}\n\n\n\n\n\nll ncr2(ll n, ll r) {\n\n ll ans = 1, fact = 2;\n\n for (ll i = n - r + 1; i <= n; i++) {\n\n ans *= i;\n\n if (ans % fact == 0 and fact <= r)\n\n ans \/= fact++;\n\n }\n\n return ans;\n\n}\n\n\n\n\/\/ll factorial[1000006], inversefactorial[1000006];\n\n\/\/\n\n\/\/void calcFacAndInv(ll n) {\n\n\/\/ factorial[0] = inversefactorial[0] = 1;\n\n\/\/ for (ll i = 1; i <= n; i++) {\n\n\/\/ factorial[i] = (i * factorial[i - 1]) % 1000000007;\n\n\/\/ inversefactorial[i] = fast_power(factorial[i], 1000000007 - 2);\n\n\/\/ }\n\n\/\/}\n\n\/\/\n\n\/\/ll ncr1(ll n, ll r) {\n\n\/\/ return ((factorial[n] % 1000000007 *\n\n\/\/ ((inversefactorial[n - r] % 1000000007 * inversefactorial[r] % 1000000007) % 1000000007) % 1000000007) %\n\n\/\/ 1000000007);\n\n\/\/}\n\n\/\/\n\n\/\/\n\n\/\/ll npr(ll n, ll r) {\n\n\/\/ return ((factorial[n] % 1000000007 * inversefactorial[n - r] % 1000000007) % 1000000007);\n\n\/\/}\n\n\n\n\n\nvector sieve(ll n) {\n\n vector isPrime(n + 1, 1);\n\n isPrime[0] = isPrime[1] = 0;\n\n for (ll i = 2; i <= n; i++)\n\n if (isPrime[i])\n\n for (ll j = 2 * i; j <= n; j += i)\n\n isPrime[j] = 0;\n\n return isPrime;\n\n}\n\n\n\n\/\/vector> factorize(ll n) {\n\n\/\/ vector> fact;\n\n\/\/ for (ll i = 2; i * i <= n; i++) {\n\n\/\/ int cnt = 0;\n\n\/\/ while (n % i == 0) {\n\n\/\/ n \/= i;\n\n\/\/ cnt++;\n\n\/\/ }\n\n\/\/ if (cnt) fact.push_back({i, cnt});\n\n\/\/ }\n\n\/\/ if (n > 1) fact.push_back({n, 1});\n\n\/\/ return fact;\n\n\/\/}\n\n\n\nvector getdivisors(ll n) {\n\n vector divs;\n\n for (ll i = 1; i * i <= n; i++) {\n\n if (n % i == 0) {\n\n divs.push_back(i);\n\n if (i != n \/ i) divs.push_back(n \/ i);\n\n }\n\n }\n\n return divs;\n\n}\n\n\n\n\n\n\/\/ upper_bound -->(greater than only) -->return iterator\n\n\/\/ lower_bound -->(greater than or equal) -->return iterator\n\n\n\n\n\n\/\/vector factorize(ll n) {\n\n\/\/ vector fact;\n\n\/\/ for (ll i = 2; i * i <= n; i++) {\n\n\/\/ while (n % i == 0) {\n\n\/\/ n \/= i;\n\n\/\/ fact.push_back(i);\n\n\/\/ }\n\n\/\/ }\n\n\/\/ if (n > 1) fact.push_back(n);\n\n\/\/ return fact;\n\n\/\/}\n\n\n\n\n\npair arr[2005];\n\n\n\nint main() {\n\n fast\n\n int t, n, x, y;\n\n cin >> t;\n\n while (t--) {\n\n cin >> n;\n\n for (int i = 0; i < n; i++) {\n\n cin >> x >> y;\n\n arr[i] = make_pair(x, y);\n\n }\n\n sort(arr, arr + n);\n\n int ok = 1;\n\n int last2 = 0;\n\n int last = 0;\n\n string res;\n\n bool flag = 0;\n\n for (int i = 0; i < n; i++) {\n\n\n\n if (arr[i].second < last) {\n\n cout << \"NO\" << el;\n\n flag = 1;\n\n }\n\n if (flag)break;\n\n\n\n int need_x = 0;\n\n need_x = arr[i].first - last2;\n\n\n\n while (need_x > 0) {\n\n res += 'R';\n\n need_x--;\n\n }\n\n\n\n last2 = arr[i].first;\n\n need_x = arr[i].second - last;\n\n\n\n while (need_x > 0) {\n\n res += 'U';\n\n need_x--;\n\n }\n\n last = max(last, arr[i].second);\n\n }\n\n if (flag)continue;\n\n cout << \"YES\" << el;\n\n cout << res << el;\n\n }\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1295","problem_id":"A","statement":"A. Display The Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of 77 segments which can be turned on and off to compose different digits. The following picture describes how you can display all 1010 decimal digits:As you can see, different digits may require different number of segments to be turned on. For example, if you want to display 11, you have to turn on 22 segments of the screen, and if you want to display 88, all 77 segments of some place to display a digit should be turned on.You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than nn segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than nn segments.Your program should be able to process tt different test cases.InputThe first line contains one integer tt (1\u2264t\u22641001\u2264t\u2264100) \u2014 the number of test cases in the input.Then the test cases follow, each of them is represented by a separate line containing one integer nn (2\u2264n\u22641052\u2264n\u2264105) \u2014 the maximum number of segments that can be turned on in the corresponding testcase.It is guaranteed that the sum of nn over all test cases in the input does not exceed 105105.OutputFor each test case, print the greatest integer that can be displayed by turning on no more than nn segments of the screen. Note that the answer may not fit in the standard 3232-bit or 6464-bit integral data type.ExampleInputCopy2\n3\n4\nOutputCopy7\n11\n","tags":["greedy"],"code":"#include \n\nusing namespace std;\n\ntypedef long long int ll;\n\n#define fast \\\n\n ios_base::sync_with_stdio(0); \\\n\n cout.tie(nullptr); \\\n\n cin.tie(nullptr);\n\n#define precision cout << fixed << setprecision(12);\n\n#define done cout << \"Successful\\n\";\n\n#define input(gggg, n) \\\n\n for (int xd = 0; xd < n; xd++) \\\n\n cin >> gggg[xd];\n\n#define print(x) cout << #x << \" = \" << x << \"\\n\";\n\n#define nl \"\\n\"\n\n#define sp \" \"\n\n\n\nvoid solve()\n\n{\n\n ll n, c = 0;\n\n cin >> n;\n\n string a;\n\n if (n % 2 == 0)\n\n {\n\n ll d = n \/ 2;\n\n for (int i = 0; i < d; ++i)\n\n {\n\n cout << 1;\n\n }\n\n }\n\n else\n\n {\n\n cout << 7;\n\n ll d = (n - 3) \/ 2;\n\n for (int i = 0; i < d; ++i)\n\n {\n\n cout << 1;\n\n }\n\n }\n\n cout << nl;\n\n}\n\n\n\nint main()\n\n{\n\n\n\n fast;\n\n int t;\n\n cin >> t;\n\n while (t--)\n\n solve();\n\n \/\/ solve();\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1295","problem_id":"A","statement":"A. Display The Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of 77 segments which can be turned on and off to compose different digits. The following picture describes how you can display all 1010 decimal digits:As you can see, different digits may require different number of segments to be turned on. For example, if you want to display 11, you have to turn on 22 segments of the screen, and if you want to display 88, all 77 segments of some place to display a digit should be turned on.You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than nn segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than nn segments.Your program should be able to process tt different test cases.InputThe first line contains one integer tt (1\u2264t\u22641001\u2264t\u2264100) \u2014 the number of test cases in the input.Then the test cases follow, each of them is represented by a separate line containing one integer nn (2\u2264n\u22641052\u2264n\u2264105) \u2014 the maximum number of segments that can be turned on in the corresponding testcase.It is guaranteed that the sum of nn over all test cases in the input does not exceed 105105.OutputFor each test case, print the greatest integer that can be displayed by turning on no more than nn segments of the screen. Note that the answer may not fit in the standard 3232-bit or 6464-bit integral data type.ExampleInputCopy2\n3\n4\nOutputCopy7\n11\n","tags":["greedy"],"code":"\/*______________________________________________________________________\n\n||--------------------------------------------------------------------||\n\n|| ||\n\n|| \"I bear witness that there is no god but Allah and I bear witness ||\n\n|| that Muhammad (peace be upon him) is the Messenger of Allah\" ||\n\n|| ||\n\n|| *** In the name of Allah, the Most Gracious, the Most Merciful.*** ||\n\n|| ||\n\n========================================================================\n\n========================================================================\n\n|| ||\n\n|| --> Author : Abu Bakar Siddique Arman (#arman_bhaai) ||\n\n|| --> Email : arman.bhaai@gmail.com ||\n\n|| --> Portfolio : arman-bhaai.github.io ||\n\n|| --> LinkedIn : linkedin.com\/in\/abubakar-arman ||\n\n|| --> GitHub : github.com\/arman-bhaai ||\n\n|| --> FaceBook : fb.me\/arman.bhaai ||\n\n|| --> YouTube : youtube.com\/@arman-bhaai ||\n\n|| --> StopStalk : stopstalk.com\/user\/profile\/arman_bhaai ||\n\n|| --> Clist : clist.by\/coder\/arman_bhaai ||\n\n|| --> CodeForces : codeforces.com\/profile\/arman_bhaai ||\n\n|| --> CodeChef : codechef.com\/users\/arman_bhaai ||\n\n|| --> AtCoder : atcoder.jp\/users\/arman_bhaai ||\n\n|| --> HackerRank : hackerrank.com\/arman_bhaai ||\n\n|| --> LeetCode : leetcode.com\/arman_bhaai ||\n\n||____________________________________________________________________||\n\n----------------------------------------------------------------------*\/\n\n\n\n\/\/ I believe in OpenSource. So, any of my code snippets are Copyright-Free.\n\n\/\/ <3 Happy Coding <3\n\n\n\n\/\/ Contest ID :: 1201\n\n\/\/ Problem Name :: A. Important Exam\n\n\/\/ Problem URL :: https:\/\/codeforces.com\/contest\/1201\/problem\/A\n\n\/\/ Submission :: \n\n\n\n\/*******************************************************************************\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n*******************************************************************************\/\n\n\n\n\n\n#include \n\nusing namespace std;\n\n\n\n\/\/ typedefs\n\ntypedef long long ll;\n\ntypedef string str;\t\t\/\/ salute Python! proud to be a Pythonista too!!\n\ntypedef long double db;\n\ntypedef vector vi;\n\ntypedef vector vii;\n\ntypedef vector> vvi;\n\n\/\/ typedef map mi;\n\ntypedef pair pi;\n\n\/\/ shortcuts\n\n#define en '\\n'\n\n#define pb push_back\n\n#define ins insert\n\n#define ft front\n\n#define bk back\n\n#define mp make_pair\n\n#define F first\n\n#define S second\n\n#define LB lower_bound\n\n#define UB upper_bound\n\n#define getv(_) for(auto &__:_) cin>>__;\n\n#define all(_) (_).begin(),(_).end()\n\n#define rall(_) (_).rbegin(),(_).rend()\n\n#define forn(_,__) for(int _=0;_<(__);_++)\n\n#define fornr(_,__) for(int _=(__);_>0;_--)\n\n#define forab(_,__,___) for(int _=(__);_<(___);_++)\n\n#define forba(_,___,__) for(int _=(___);_>(__);_--)\n\n#define sz(_) (_).size()\n\n#define rsz(__) (__).resize()\n\n#define each(__, _) for (auto&__:_)\n\n\/\/ constants\n\nconst db PI = acos(-1.0);\n\nconst int MX = (int)2e5 + 5;\n\nconst ll INF = (ll)1e18; \/\/ not too close to LLONG_MAX\n\nconst int MOD = (int)1e9 + 7;\n\n\/\/ debugging\n\ntemplateostream&operator<<(ostream&os,const pair&p){return os<<\"(\"<ostream&operator<<(ostream&os,const vector&v){os<<\"[\";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<\", \";os<<*it;}return os<<\"]\";}\n\ntemplateostream&operator<<(ostream&os,const set&v){os<<\"{\";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<\", \";os<<*it;}return os<<\"}\";}\n\ntemplateostream&operator<<(ostream&os,const multiset&v) {os<<\"{\";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<\", \";os<<*it;}return os<<\"}\";}\n\ntemplateostream&operator<<(ostream&os,const map&v){os<<\"[\";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<\", \";os<<\"(\"<F<<\", \"<S<<\")\";}return os<<\"]\";}\n\n#define couts(___) cout<<(___)<<\" \";\n\n#define coutn(___) cout<<(___)<<'\\n';\n\n#define dbg(args...) do {cerr << #args << \" ==> \"; boss(args); } while(0);\n\nvoid boss(){cerr << endl;}\n\ntemplatevoid boss(T a[],int n){for(int i=0;ivoid boss(T arg,const k&...j){cerr<>n>>m;\n\n\n\n\/\/ \tif(n>m) cin>>a>>b;\n\n\/\/ \telse cin>>b>>a;\n\n\n\n\/\/ \tstr s;\n\n\/\/ \tforn(i,sz(a)){\n\n\/\/ \t\tif(!count(all(b),a[i]) || count(all(s),a[i])) s.pb(a[i]);\n\n\/\/ \t}\n\n\/\/ }\n\n\n\nvoid solve(){\n\n\tint n; cin>>n;\n\n\t\/\/ vi v={'','','1','7',};\n\n\tstr s=\"\";\n\n\tif(n&1){\n\n\t\ts=\"7\";\n\n\t\tforn(i,(n-3)\/2) s.pb('1');\n\n\t} else {\n\n\t\tforn(i,n\/2) s.pb('1');\n\n\t}\n\n\tcout<>__;\n\n\tfor(int _=___; _<=__; _++){\n\n\t\t\/\/ cout<<\"Case \"<<_<<\": \";\n\n\t\tsolve(); cout<\n\n#pragma GCC optimize(0)\n\n\n\nusing namespace std;\n\n\n\ntypedef long long ll;\n\ntypedef unsigned long long ull;\n\n\n\ninline ll read()\n\n{\n\n ll X = 0, w = 0; char ch = 0;\n\n while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }\n\n while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();\n\n return w ? -X : X;\n\n}\n\n\n\ninline void write(ll x)\n\n{\n\n if (x < 0) putchar('-'), x = -x;\n\n if (x > 9) write(x \/ 10);\n\n putchar(x % 10 + '0');\n\n}\n\n\n\n#define pb push_back\n\n#define mp make_pair\n\n\n\ntypedef pair pii;\n\n\n\nusing namespace std;\n\n\n\nvoid solve() {\n\n int n = read(), h = read(), l = read(), r = read();\n\n int dp[n + 1][h];\n\n memset(dp, -1, sizeof(dp));\n\n dp[0][0] = 0;\n\n int pres[n + 1];\n\n pres[0] = 0;\n\n for (int i = 1; i <= n; i++) {\n\n pres[i] = (pres[i - 1] + read()) % h;\n\n }\n\n for (int i = 0; i < n; i++) {\n\n for (int j = 0; j < h; j++) {\n\n if (dp[i][j] >= 0) {\n\n dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (l <= (pres[i + 1] - j + h) % h && (pres[i + 1] - j + h) % h <= r));\n\n dp[i + 1][(j + 1) % h] = max(dp[i + 1][(j + 1) % h], dp[i][j] + (l <= (pres[i + 1] - j - 1 + h + h) % h && (pres[i + 1] - j - 1 + h + h) % h <= r));\n\n }\n\n }\n\n }\n\n int ans = 0;\n\n for (int j = 0; j < h; j++) {\n\n if (dp[n][j] > ans) ans = dp[n][j];\n\n }\n\n write(ans); putchar('\\n');\n\n}\n\n\n\nint main() {\n\n int t = 1;\n\n while (t--) solve();\n\n}\n\n","language":"cpp"} -{"contest_id":"1315","problem_id":"A","statement":"A. Dead Pixeltime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputScreen resolution of Polycarp's monitor is a\u00d7ba\u00d7b pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates (x,y)(x,y) (0\u2264x2a+b>2 (e.g. a=b=1a=b=1 is impossible).OutputPrint tt integers\u00a0\u2014 the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn't contain the dead pixel.ExampleInputCopy6\n8 8 0 0\n1 10 0 3\n17 31 10 4\n2 1 0 0\n5 10 3 9\n10 10 4 8\nOutputCopy56\n6\n442\n1\n45\n80\nNoteIn the first test case; the screen resolution is 8\u00d788\u00d78, and the upper left pixel is a dead pixel. Here you can see one of two possible layouts of the maximal window. ","tags":["implementation"],"code":"#include\n#include\n#include\n#include\nusing namespace std;\nint main(){\n\tint n;\n\tscanf(\"%d\",&n);\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tint a,b,x,y;\n\t\tscanf(\"%d%d%d%d\",&a,&b,&x,&y);\n\t\tint p1=a-x-1,q1=b-y-1,p2=x-1+1,q2=y-1+1;\n\t\tint f1=p1*b,f2=q1*a,f3=p2*b,f4=q2*a;\n\t\tprintf(\"%d\\n\",max(f1,max(f2,max(f3,f4))));\n\t}\n\treturn 0;\n}\n\n\t \t\t\t \t \t \t \t \t\t\t\t \t\t\t\t\t \t","language":"cpp"} -{"contest_id":"1312","problem_id":"F","statement":"F. Attack on Red Kingdomtime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe Red Kingdom is attacked by the White King and the Black King!The Kingdom is guarded by nn castles, the ii-th castle is defended by aiai soldiers. To conquer the Red Kingdom, the Kings have to eliminate all the defenders. Each day the White King launches an attack on one of the castles. Then, at night, the forces of the Black King attack a castle (possibly the same one). Then the White King attacks a castle, then the Black King, and so on. The first attack is performed by the White King.Each attack must target a castle with at least one alive defender in it. There are three types of attacks: a mixed attack decreases the number of defenders in the targeted castle by xx (or sets it to 00 if there are already less than xx defenders); an infantry attack decreases the number of defenders in the targeted castle by yy (or sets it to 00 if there are already less than yy defenders); a cavalry attack decreases the number of defenders in the targeted castle by zz (or sets it to 00 if there are already less than zz defenders). The mixed attack can be launched at any valid target (at any castle with at least one soldier). However, the infantry attack cannot be launched if the previous attack on the targeted castle had the same type, no matter when and by whom it was launched. The same applies to the cavalry attack. A castle that was not attacked at all can be targeted by any type of attack.The King who launches the last attack will be glorified as the conqueror of the Red Kingdom, so both Kings want to launch the last attack (and they are wise enough to find a strategy that allows them to do it no matter what are the actions of their opponent, if such strategy exists). The White King is leading his first attack, and you are responsible for planning it. Can you calculate the number of possible options for the first attack that allow the White King to launch the last attack? Each option for the first attack is represented by the targeted castle and the type of attack, and two options are different if the targeted castles or the types of attack are different.InputThe first line contains one integer tt (1\u2264t\u226410001\u2264t\u22641000) \u2014 the number of test cases.Then, the test cases follow. Each test case is represented by two lines. The first line contains four integers nn, xx, yy and zz (1\u2264n\u22643\u22c51051\u2264n\u22643\u22c5105, 1\u2264x,y,z\u226451\u2264x,y,z\u22645). The second line contains nn integers a1a1, a2a2, ..., anan (1\u2264ai\u226410181\u2264ai\u22641018).It is guaranteed that the sum of values of nn over all test cases in the input does not exceed 3\u22c51053\u22c5105.OutputFor each test case, print the answer to it: the number of possible options for the first attack of the White King (or 00, if the Black King can launch the last attack no matter how the White King acts).ExamplesInputCopy3\n2 1 3 4\n7 6\n1 1 2 3\n1\n1 1 2 2\n3\nOutputCopy2\n3\n0\nInputCopy10\n6 5 4 5\n2 3 2 3 1 3\n1 5 2 3\n10\n4 4 2 3\n8 10 8 5\n2 2 1 4\n8 5\n3 5 3 5\n9 2 10\n4 5 5 5\n2 10 4 2\n2 3 1 4\n1 10\n3 1 5 3\n9 8 7\n2 5 4 5\n8 8\n3 5 1 4\n5 5 10\nOutputCopy0\n2\n1\n2\n5\n12\n5\n0\n0\n2\n","tags":["games","two pointers"],"code":"#line 1 \"\/home\/maspy\/compro\/library\/my_template.hpp\"\n\n#if defined(LOCAL)\n\n#include \n\n#else\n\n#pragma GCC optimize(\"Ofast\")\n\n#pragma GCC optimize(\"unroll-loops\")\n\n\n\n#include \n\n\n\nusing namespace std;\n\n\n\nusing ll = long long;\n\nusing pi = pair;\n\nusing vi = vector;\n\nusing u32 = unsigned int;\n\nusing u64 = unsigned long long;\n\nusing i128 = __int128;\n\n\n\ntemplate \n\nusing vc = vector;\n\ntemplate \n\nusing vvc = vector>;\n\ntemplate \n\nusing vvvc = vector>;\n\ntemplate \n\nusing vvvvc = vector>;\n\ntemplate \n\nusing vvvvvc = vector>;\n\ntemplate \n\nusing pq = priority_queue;\n\ntemplate \n\nusing pqg = priority_queue, greater>;\n\n\n\n#define vec(type, name, ...) vector name(__VA_ARGS__)\n\n#define vv(type, name, h, ...) \\\n\n vector> name(h, vector(__VA_ARGS__))\n\n#define vvv(type, name, h, w, ...) \\\n\n vector>> name( \\\n\n h, vector>(w, vector(__VA_ARGS__)))\n\n#define vvvv(type, name, a, b, c, ...) \\\n\n vector>>> name( \\\n\n a, vector>>( \\\n\n b, vector>(c, vector(__VA_ARGS__))))\n\n\n\n\/\/ https:\/\/trap.jp\/post\/1224\/\n\n#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)\n\n#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)\n\n#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)\n\n#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))\n\n#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)\n\n#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)\n\n#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)\n\n#define FOR4_R(i, a, b, c) for (ll i = (b)-1; i >= ll(a); i -= (c))\n\n#define overload4(a, b, c, d, e, ...) e\n\n#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)\n\n#define FOR_R(...) \\\n\n overload4(__VA_ARGS__, FOR4_R, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)\n\n\n\n#define FOR_subset(t, s) for (ll t = s; t >= 0; t = (t == 0 ? -1 : (t - 1) & s))\n\n#define all(x) x.begin(), x.end()\n\n#define len(x) ll(x.size())\n\n#define elif else if\n\n\n\n#define eb emplace_back\n\n#define mp make_pair\n\n#define mt make_tuple\n\n#define fi first\n\n#define se second\n\n\n\n#define stoi stoll\n\n\n\ntemplate \n\nT SUM(const vector &A) {\n\n T sum = 0;\n\n for (auto &&a: A) sum += a;\n\n return sum;\n\n}\n\n\n\n#define MIN(v) *min_element(all(v))\n\n#define MAX(v) *max_element(all(v))\n\n#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))\n\n#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))\n\n#define UNIQUE(x) \\\n\n sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()\n\n\n\nint popcnt(int x) { return __builtin_popcount(x); }\n\nint popcnt(u32 x) { return __builtin_popcount(x); }\n\nint popcnt(ll x) { return __builtin_popcountll(x); }\n\nint popcnt(u64 x) { return __builtin_popcountll(x); }\n\n\/\/ (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)\n\nint topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\n\nint topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }\n\nint topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n\nint topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }\n\n\/\/ (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)\n\nint lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\n\nint lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }\n\nint lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n\nint lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }\n\n\n\ntemplate \n\nT pick(deque &que) {\n\n T a = que.front();\n\n que.pop_front();\n\n return a;\n\n}\n\n\n\ntemplate \n\nT pick(pq &que) {\n\n T a = que.top();\n\n que.pop();\n\n return a;\n\n}\n\n\n\ntemplate \n\nT pick(pqg &que) {\n\n assert(que.size());\n\n T a = que.top();\n\n que.pop();\n\n return a;\n\n}\n\n\n\ntemplate \n\nT pick(vc &que) {\n\n assert(que.size());\n\n T a = que.back();\n\n que.pop_back();\n\n return a;\n\n}\n\n\n\ntemplate \n\nT ceil(T x, U y) {\n\n return (x > 0 ? (x + y - 1) \/ y : x \/ y);\n\n}\n\n\n\ntemplate \n\nT floor(T x, U y) {\n\n return (x > 0 ? x \/ y : (x - y + 1) \/ y);\n\n}\n\n\n\ntemplate \n\npair divmod(T x, U y) {\n\n T q = floor(x, y);\n\n return {q, x - q * y};\n\n}\n\n\n\ntemplate \n\nll binary_search(F check, ll ok, ll ng) {\n\n assert(check(ok));\n\n while (abs(ok - ng) > 1) {\n\n auto x = (ng + ok) \/ 2;\n\n tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));\n\n }\n\n return ok;\n\n}\n\n\n\ntemplate \n\ndouble binary_search_real(F check, double ok, double ng, int iter = 100) {\n\n FOR(iter) {\n\n double x = (ok + ng) \/ 2;\n\n tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));\n\n }\n\n return (ok + ng) \/ 2;\n\n}\n\n\n\ntemplate \n\ninline bool chmax(T &a, const S &b) {\n\n return (a < b ? a = b, 1 : 0);\n\n}\n\ntemplate \n\ninline bool chmin(T &a, const S &b) {\n\n return (a > b ? a = b, 1 : 0);\n\n}\n\n\n\nvc s_to_vi(const string &S, char first_char) {\n\n vc A(S.size());\n\n FOR(i, S.size()) { A[i] = S[i] - first_char; }\n\n return A;\n\n}\n\n\n\ntemplate \n\nvector cumsum(vector &A, int off = 1) {\n\n int N = A.size();\n\n vector B(N + 1);\n\n FOR(i, N) { B[i + 1] = B[i] + A[i]; }\n\n if (off == 0) B.erase(B.begin());\n\n return B;\n\n}\n\n\n\ntemplate \n\nvc bincount(const vc &A, int size) {\n\n vc C(size);\n\n for (auto &&x: A) { ++C[x]; }\n\n return C;\n\n}\n\n\n\n\/\/ stable\n\ntemplate \n\nvector argsort(const vector &A) {\n\n vector ids(A.size());\n\n iota(all(ids), 0);\n\n sort(all(ids),\n\n [&](int i, int j) { return A[i] < A[j] || (A[i] == A[j] && i < j); });\n\n return ids;\n\n}\n\n\n\n\/\/ A[I[0]], A[I[1]], ...\n\ntemplate \n\nvc rearrange(const vc &A, const vc &I) {\n\n int n = len(I);\n\n vc B(n);\n\n FOR(i, n) B[i] = A[I[i]];\n\n return B;\n\n}\n\n#endif\n\n#line 1 \"\/home\/maspy\/compro\/library\/other\/io.hpp\"\n\n\/\/ based on yosupo's fastio\n\n#include \n\n\n\nnamespace fastio {\n\n\/\/ \u30af\u30e9\u30b9\u304c read(), print() \u3092\u6301\u3063\u3066\u3044\u308b\u304b\u3092\u5224\u5b9a\u3059\u308b\u30e1\u30bf\u95a2\u6570\n\nstruct has_write_impl {\n\n template \n\n static auto check(T &&x) -> decltype(x.write(), std::true_type{});\n\n\n\n template \n\n static auto check(...) -> std::false_type;\n\n};\n\n\n\ntemplate \n\nclass has_write : public decltype(has_write_impl::check(std::declval())) {\n\n};\n\n\n\nstruct has_read_impl {\n\n template \n\n static auto check(T &&x) -> decltype(x.read(), std::true_type{});\n\n\n\n template \n\n static auto check(...) -> std::false_type;\n\n};\n\n\n\ntemplate \n\nclass has_read : public decltype(has_read_impl::check(std::declval())) {};\n\n\n\nstruct Scanner {\n\n FILE *fp;\n\n char line[(1 << 15) + 1];\n\n size_t st = 0, ed = 0;\n\n void reread() {\n\n memmove(line, line + st, ed - st);\n\n ed -= st;\n\n st = 0;\n\n ed += fread(line + ed, 1, (1 << 15) - ed, fp);\n\n line[ed] = '\\0';\n\n }\n\n bool succ() {\n\n while (true) {\n\n if (st == ed) {\n\n reread();\n\n if (st == ed) return false;\n\n }\n\n while (st != ed && isspace(line[st])) st++;\n\n if (st != ed) break;\n\n }\n\n if (ed - st <= 50) {\n\n bool sep = false;\n\n for (size_t i = st; i < ed; i++) {\n\n if (isspace(line[i])) {\n\n sep = true;\n\n break;\n\n }\n\n }\n\n if (!sep) reread();\n\n }\n\n return true;\n\n }\n\n template ::value, int> = 0>\n\n bool read_single(T &ref) {\n\n if (!succ()) return false;\n\n while (true) {\n\n size_t sz = 0;\n\n while (st + sz < ed && !isspace(line[st + sz])) sz++;\n\n ref.append(line + st, sz);\n\n st += sz;\n\n if (!sz || st != ed) break;\n\n reread();\n\n }\n\n return true;\n\n }\n\n template ::value, int> = 0>\n\n bool read_single(T &ref) {\n\n if (!succ()) return false;\n\n bool neg = false;\n\n if (line[st] == '-') {\n\n neg = true;\n\n st++;\n\n }\n\n ref = T(0);\n\n while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }\n\n if (neg) ref = -ref;\n\n return true;\n\n }\n\n template ::value>::type * = nullptr>\n\n inline bool read_single(T &x) {\n\n x.read();\n\n return true;\n\n }\n\n bool read_single(double &ref) {\n\n string s;\n\n if (!read_single(s)) return false;\n\n ref = std::stod(s);\n\n return true;\n\n }\n\n bool read_single(char &ref) {\n\n string s;\n\n if (!read_single(s) || s.size() != 1) return false;\n\n ref = s[0];\n\n return true;\n\n }\n\n template \n\n bool read_single(vector &ref) {\n\n for (auto &d: ref) {\n\n if (!read_single(d)) return false;\n\n }\n\n return true;\n\n }\n\n template \n\n bool read_single(pair &p) {\n\n return (read_single(p.first) && read_single(p.second));\n\n }\n\n template \n\n void read_single_tuple(T &t) {\n\n if constexpr (N < std::tuple_size::value) {\n\n auto &x = std::get(t);\n\n read_single(x);\n\n read_single_tuple(t);\n\n }\n\n }\n\n template \n\n bool read_single(tuple &tpl) {\n\n read_single_tuple(tpl);\n\n return true;\n\n }\n\n void read() {}\n\n template \n\n void read(H &h, T &... t) {\n\n bool f = read_single(h);\n\n assert(f);\n\n read(t...);\n\n }\n\n Scanner(FILE *fp) : fp(fp) {}\n\n};\n\n\n\nstruct Printer {\n\n Printer(FILE *_fp) : fp(_fp) {}\n\n ~Printer() { flush(); }\n\n\n\n static constexpr size_t SIZE = 1 << 15;\n\n FILE *fp;\n\n char line[SIZE], small[50];\n\n size_t pos = 0;\n\n void flush() {\n\n fwrite(line, 1, pos, fp);\n\n pos = 0;\n\n }\n\n void write(const char val) {\n\n if (pos == SIZE) flush();\n\n line[pos++] = val;\n\n }\n\n template ::value, int> = 0>\n\n void write(T val) {\n\n if (pos > (1 << 15) - 50) flush();\n\n if (val == 0) {\n\n write('0');\n\n return;\n\n }\n\n if (val < 0) {\n\n write('-');\n\n val = -val; \/\/ todo min\n\n }\n\n size_t len = 0;\n\n while (val) {\n\n small[len++] = char(0x30 | (val % 10));\n\n val \/= 10;\n\n }\n\n for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }\n\n pos += len;\n\n }\n\n void write(const string s) {\n\n for (char c: s) write(c);\n\n }\n\n void write(const char *s) {\n\n size_t len = strlen(s);\n\n for (size_t i = 0; i < len; i++) write(s[i]);\n\n }\n\n void write(const double x) {\n\n ostringstream oss;\n\n oss << fixed << setprecision(15) << x;\n\n string s = oss.str();\n\n write(s);\n\n }\n\n void write(const long double x) {\n\n ostringstream oss;\n\n oss << fixed << setprecision(15) << x;\n\n string s = oss.str();\n\n write(s);\n\n }\n\n template ::value>::type * = nullptr>\n\n inline void write(T x) {\n\n x.write();\n\n }\n\n template \n\n void write(const vector val) {\n\n auto n = val.size();\n\n for (size_t i = 0; i < n; i++) {\n\n if (i) write(' ');\n\n write(val[i]);\n\n }\n\n }\n\n template \n\n void write(const pair val) {\n\n write(val.first);\n\n write(' ');\n\n write(val.second);\n\n }\n\n template \n\n void write_tuple(const T t) {\n\n if constexpr (N < std::tuple_size::value) {\n\n if constexpr (N > 0) { write(' '); }\n\n const auto x = std::get(t);\n\n write(x);\n\n write_tuple(t);\n\n }\n\n }\n\n template \n\n bool write(tuple tpl) {\n\n write_tuple(tpl);\n\n return true;\n\n }\n\n template \n\n void write(const array val) {\n\n auto n = val.size();\n\n for (size_t i = 0; i < n; i++) {\n\n if (i) write(' ');\n\n write(val[i]);\n\n }\n\n }\n\n void write(i128 val) {\n\n string s;\n\n bool negative = 0;\n\n if (val < 0) {\n\n negative = 1;\n\n val = -val;\n\n }\n\n while (val) {\n\n s += '0' + int(val % 10);\n\n val \/= 10;\n\n }\n\n if (negative) s += \"-\";\n\n reverse(all(s));\n\n if (len(s) == 0) s = \"0\";\n\n write(s);\n\n }\n\n};\n\nScanner scanner = Scanner(stdin);\n\nPrinter printer = Printer(stdout);\n\nvoid flush() { printer.flush(); }\n\nvoid print() { printer.write('\\n'); }\n\ntemplate \n\nvoid print(Head &&head, Tail &&... tail) {\n\n printer.write(head);\n\n if (sizeof...(Tail)) printer.write(' ');\n\n print(forward(tail)...);\n\n}\n\n\n\nvoid read() {}\n\ntemplate \n\nvoid read(Head &head, Tail &... tail) {\n\n scanner.read(head);\n\n read(tail...);\n\n}\n\n} \/\/ namespace fastio\n\nusing fastio::print;\n\nusing fastio::flush;\n\nusing fastio::read;\n\n\n\n#define INT(...) \\\n\n int __VA_ARGS__; \\\n\n read(__VA_ARGS__)\n\n#define LL(...) \\\n\n ll __VA_ARGS__; \\\n\n read(__VA_ARGS__)\n\n#define STR(...) \\\n\n string __VA_ARGS__; \\\n\n read(__VA_ARGS__)\n\n#define CHAR(...) \\\n\n char __VA_ARGS__; \\\n\n read(__VA_ARGS__)\n\n#define DBL(...) \\\n\n double __VA_ARGS__; \\\n\n read(__VA_ARGS__)\n\n\n\n#define VEC(type, name, size) \\\n\n vector name(size); \\\n\n read(name)\n\n#define VV(type, name, h, w) \\\n\n vector> name(h, vector(w)); \\\n\n read(name)\n\n\n\nvoid YES(bool t = 1) { print(t ? \"YES\" : \"NO\"); }\n\nvoid NO(bool t = 1) { YES(!t); }\n\nvoid Yes(bool t = 1) { print(t ? \"Yes\" : \"No\"); }\n\nvoid No(bool t = 1) { Yes(!t); }\n\nvoid yes(bool t = 1) { print(t ? \"yes\" : \"no\"); }\n\nvoid no(bool t = 1) { yes(!t); }\n\n#line 1 \"\/home\/maspy\/compro\/library\/string\/zalgorithm.hpp\"\n\ntemplate \/\/ string, vector \u3069\u3061\u3089\u3067\u3082\n\nvector zalgorithm(const STRING& s) {\n\n int n = int(s.size());\n\n if (n == 0) return {};\n\n vector z(n);\n\n z[0] = 0;\n\n for (int i = 1, j = 0; i < n; i++) {\n\n int& k = z[i];\n\n k = (j + z[j] <= i) ? 0 : min(j + z[j] - i, z[i - j]);\n\n while (i + k < n && s[k] == s[i + k]) k++;\n\n if (j + z[j] < i + z[i]) j = i;\n\n }\n\n z[0] = n;\n\n return z;\n\n}\n\n#line 4 \"main.cpp\"\n\n\n\ntemplate \n\nstruct Interpolate_Periodic_Sequence_Query {\n\n vc dat;\n\n int p;\n\n\n\n Interpolate_Periodic_Sequence_Query(vc A) : dat(A) {\n\n reverse(all(A));\n\n auto Z = zalgorithm(A);\n\n Z[0] = 0;\n\n p = max_element(all(Z)) - Z.begin();\n\n FOR(i, p) assert(A[i] == A[p + i]);\n\n }\n\n\n\n T operator[](ll n) {\n\n if (n < len(dat)) return dat[n];\n\n ll k = ceil(n - (len(dat) - 1), p);\n\n n -= k * p;\n\n return dat[n];\n\n }\n\n};\n\n\n\nvoid solve() {\n\n LL(N, X, Y, Z);\n\n ll LIM = 1000;\n\n vv(int, dp, 3, LIM);\n\n FOR(n, 1, LIM) {\n\n FOR(s, 3) {\n\n set TO;\n\n TO.insert(dp[0][max(0, n - X)]);\n\n if (s != 1) TO.insert(dp[1][max(0, n - Y)]);\n\n if (s != 2) TO.insert(dp[2][max(0, n - Z)]);\n\n int mex = 0;\n\n while (TO.count(mex)) ++mex;\n\n dp[s][n] = mex;\n\n }\n\n }\n\n vc DP = dp[0];\n\n Interpolate_Periodic_Sequence_Query XXX(DP);\n\n ll g = 0;\n\n VEC(ll, A, N);\n\n for (auto&& x: A) {\n\n if (x < LIM) continue;\n\n ll k = ceil(x - (LIM - 1), XXX.p);\n\n x -= k * XXX.p;\n\n }\n\n\n\n for (auto&& x: A) { g ^= DP[x]; }\n\n ll ANS = 0;\n\n for (auto&& n: A) {\n\n vc TO;\n\n TO.eb(dp[0][max(0, n - X)]);\n\n TO.eb(dp[1][max(0, n - Y)]);\n\n TO.eb(dp[2][max(0, n - Z)]);\n\n int now = dp[0][n];\n\n for (auto&& t: TO)\n\n if ((g ^ now ^ t) == 0) ++ANS;\n\n }\n\n\n\n print(ANS);\n\n}\n\n\n\nsigned main() {\n\n INT(T);\n\n FOR(T) solve();\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1286","problem_id":"C1","statement":"C1. Madhouse (Easy version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is different with hard version only by constraints on total answers lengthIt is an interactive problemVenya joined a tour to the madhouse; in which orderlies play with patients the following game. Orderlies pick a string ss of length nn, consisting only of lowercase English letters. The player can ask two types of queries: ? l r \u2013 ask to list all substrings of s[l..r]s[l..r]. Substrings will be returned in random order, and in every substring, all characters will be randomly shuffled. ! s \u2013 guess the string picked by the orderlies. This query can be asked exactly once, after that the game will finish. If the string is guessed correctly, the player wins, otherwise he loses. The player can ask no more than 33 queries of the first type.To make it easier for the orderlies, there is an additional limitation: the total number of returned substrings in all queries of the first type must not exceed (n+1)2(n+1)2.Venya asked you to write a program, which will guess the string by interacting with the orderlies' program and acting by the game's rules.Your program should immediately terminate after guessing the string using a query of the second type. In case your program guessed the string incorrectly, or it violated the game rules, it will receive verdict Wrong answer.Note that in every test case the string is fixed beforehand and will not change during the game, which means that the interactor is not adaptive.InputFirst line contains number nn (1\u2264n\u22641001\u2264n\u2264100)\u00a0\u2014 the length of the picked string.InteractionYou start the interaction by reading the number nn.To ask a query about a substring from ll to rr inclusively (1\u2264l\u2264r\u2264n1\u2264l\u2264r\u2264n), you should output? l ron a separate line. After this, all substrings of s[l..r]s[l..r] will be returned in random order, each substring exactly once. In every returned substring all characters will be randomly shuffled.In the case, if you ask an incorrect query, ask more than 33 queries of the first type or there will be more than (n+1)2(n+1)2 substrings returned in total, you will receive verdict Wrong answer.To guess the string ss, you should output! son a separate line.After printing each query, do not forget to flush the output. Otherwise, you will get Idleness limit exceeded. To flush the output, you can use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. If you received - (dash) as an answer to any query, you need to terminate your program with exit code 0 (for example, by calling exit(0)). This means that there was an error in the interaction protocol. If you don't terminate with exit code 0, you can receive any unsuccessful verdict.Hack formatTo hack a solution, use the following format:The first line should contain one integer nn (1\u2264n\u22641001\u2264n\u2264100)\u00a0\u2014 the length of the string, and the following line should contain the string ss.ExampleInputCopy4\n\na\naa\na\n\ncb\nb\nc\n\ncOutputCopy? 1 2\n\n? 3 4\n\n? 4 4\n\n! aabc","tags":["brute force","constructive algorithms","interactive","math"],"code":"#include \n\nusing namespace std;\n\n#define int long long\n\n#define fi first\n\n#define se second\n\n\n\nint t, n, m, k, a[1000005], mod = 1e9 + 7;\n\nmt19937_64 rng;\n\nmap, int> mp;\n\nvector ans[100005];\n\nint mul(int x, int y){\n\n if(y == 0) return 1;\n\n int ans = mul(x, y \/ 2);\n\n if(y % 2 == 0) return (ans * ans) % mod;\n\n else return (((ans * ans) % mod) * x) % mod;\n\n}\n\n\n\nvoid solve(){\n\n if(n == 1){\n\n cout << \"? \" << 1 <<\" \"<< n << endl;\n\n string s; cin >> s;\n\n cout << \"! \" << s << endl; return;\n\n }\n\n cout << \"? \" << 1 <<\" \"<< n << endl;\n\n for(int i = 1; i <= n * (n + 1) \/ 2; i++){\n\n string s; cin >> s; \n\n int sz = s.size();\n\n vector v(26);\n\n for(int j = 0; j < sz; j++) v[s[j] - 97]++;\n\n mp[v]++;\n\n }\n\n cout << \"? \" << 2 <<\" \"<< n << endl;\n\n for(int i = 1; i <= n * (n - 1) \/ 2; i++){\n\n string s; cin >> s; \n\n int sz = s.size();\n\n vector v(26);\n\n for(int j = 0; j < sz; j++) v[s[j] - 97]++;\n\n mp[v]--;\n\n }\n\n for(auto i: mp){\n\n if(i.se){\n\n int sum = 0;\n\n for(auto j: i.fi) sum += j;\n\n ans[sum] = i.fi;\n\n }\n\n }\n\n cout << \"! \";\n\n ans[0].assign(26, 0);\n\n for(int i = 1; i <= n; i++){\n\n for(int j = 0; j < 26; j++){\n\n if(ans[i][j] > ans[i - 1][j]){\n\n cout << char(j + 97);\n\n }\n\n }\n\n }\n\n cout << endl;\n\n}\n\n\n\nsigned main(){\n\n ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);\n\n rng.seed((int)main ^ time(0));\n\n #ifdef Kawaii\n\n auto starttime = chrono::high_resolution_clock::now();\n\n #endif\n\n\n\n cin >> n;\n\n solve();\n\n\n\n #ifdef Kawaii\n\n auto endtime = chrono::high_resolution_clock::now();\n\n auto duration = chrono::duration_cast(endtime - starttime).count(); \n\n cout << \"\\n=====\" << \"\\nUsed: \" << duration << \" ms\\n\";\n\n #endif\n\n}","language":"cpp"} -{"contest_id":"1286","problem_id":"E","statement":"E. Fedya the Potter Strikes Backtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFedya has a string SS, initially empty, and an array WW, also initially empty.There are nn queries to process, one at a time. Query ii consists of a lowercase English letter cici and a nonnegative integer wiwi. First, cici must be appended to SS, and wiwi must be appended to WW. The answer to the query is the sum of suspiciousnesses for all subsegments of WW [L,\u00a0R][L,\u00a0R], (1\u2264L\u2264R\u2264i)(1\u2264L\u2264R\u2264i).We define the suspiciousness of a subsegment as follows: if the substring of SS corresponding to this subsegment (that is, a string of consecutive characters from LL-th to RR-th, inclusive) matches the prefix of SS of the same length (that is, a substring corresponding to the subsegment [1,\u00a0R\u2212L+1][1,\u00a0R\u2212L+1]), then its suspiciousness is equal to the minimum in the array WW on the [L,\u00a0R][L,\u00a0R] subsegment. Otherwise, in case the substring does not match the corresponding prefix, the suspiciousness is 00.Help Fedya answer all the queries before the orderlies come for him!InputThe first line contains an integer nn (1\u2264n\u2264600000)(1\u2264n\u2264600000)\u00a0\u2014 the number of queries.The ii-th of the following nn lines contains the query ii: a lowercase letter of the Latin alphabet cici and an integer wiwi (0\u2264wi\u2264230\u22121)(0\u2264wi\u2264230\u22121).All queries are given in an encrypted form. Let ansans be the answer to the previous query (for the first query we set this value equal to 00). Then, in order to get the real query, you need to do the following: perform a cyclic shift of cici in the alphabet forward by ansans, and set wiwi equal to wi\u2295(ans\u00a0&\u00a0MASK)wi\u2295(ans\u00a0&\u00a0MASK), where \u2295\u2295 is the bitwise exclusive \"or\", && is the bitwise \"and\", and MASK=230\u22121MASK=230\u22121.OutputPrint nn lines, ii-th line should contain a single integer \u2014 the answer to the ii-th query.ExamplesInputCopy7\na 1\na 0\ny 3\ny 5\nv 4\nu 6\nr 8\nOutputCopy1\n2\n4\n5\n7\n9\n12\nInputCopy4\na 2\ny 2\nz 0\ny 2\nOutputCopy2\n2\n2\n2\nInputCopy5\na 7\nu 5\nt 3\ns 10\ns 11\nOutputCopy7\n9\n11\n12\n13\nNoteFor convenience; we will call \"suspicious\" those subsegments for which the corresponding lines are prefixes of SS, that is, those whose suspiciousness may not be zero.As a result of decryption in the first example, after all requests, the string SS is equal to \"abacaba\", and all wi=1wi=1, that is, the suspiciousness of all suspicious sub-segments is simply equal to 11. Let's see how the answer is obtained after each request:1. SS = \"a\", the array WW has a single subsegment \u2014 [1,\u00a01][1,\u00a01], and the corresponding substring is \"a\", that is, the entire string SS, thus it is a prefix of SS, and the suspiciousness of the subsegment is 11.2. SS = \"ab\", suspicious subsegments: [1,\u00a01][1,\u00a01] and [1,\u00a02][1,\u00a02], total 22.3. SS = \"aba\", suspicious subsegments: [1,\u00a01][1,\u00a01], [1,\u00a02][1,\u00a02], [1,\u00a03][1,\u00a03] and [3,\u00a03][3,\u00a03], total 44.4. SS = \"abac\", suspicious subsegments: [1,\u00a01][1,\u00a01], [1,\u00a02][1,\u00a02], [1,\u00a03][1,\u00a03], [1,\u00a04][1,\u00a04] and [3,\u00a03][3,\u00a03], total 55.5. SS = \"abaca\", suspicious subsegments: [1,\u00a01][1,\u00a01], [1,\u00a02][1,\u00a02], [1,\u00a03][1,\u00a03], [1,\u00a04][1,\u00a04] , [1,\u00a05][1,\u00a05], [3,\u00a03][3,\u00a03] and [5,\u00a05][5,\u00a05], total 77.6. SS = \"abacab\", suspicious subsegments: [1,\u00a01][1,\u00a01], [1,\u00a02][1,\u00a02], [1,\u00a03][1,\u00a03], [1,\u00a04][1,\u00a04] , [1,\u00a05][1,\u00a05], [1,\u00a06][1,\u00a06], [3,\u00a03][3,\u00a03], [5,\u00a05][5,\u00a05] and [5,\u00a06][5,\u00a06], total 99.7. SS = \"abacaba\", suspicious subsegments: [1,\u00a01][1,\u00a01], [1,\u00a02][1,\u00a02], [1,\u00a03][1,\u00a03], [1,\u00a04][1,\u00a04] , [1,\u00a05][1,\u00a05], [1,\u00a06][1,\u00a06], [1,\u00a07][1,\u00a07], [3,\u00a03][3,\u00a03], [5,\u00a05][5,\u00a05], [5,\u00a06][5,\u00a06], [5,\u00a07][5,\u00a07] and [7,\u00a07][7,\u00a07], total 1212.In the second example, after all requests SS = \"aaba\", W=[2,0,2,0]W=[2,0,2,0].1. SS = \"a\", suspicious subsegments: [1,\u00a01][1,\u00a01] (suspiciousness 22), totaling 22.2. SS = \"aa\", suspicious subsegments: [1,\u00a01][1,\u00a01] (22), [1,\u00a02][1,\u00a02] (00), [2,\u00a02][2,\u00a02] ( 00), totaling 22.3. SS = \"aab\", suspicious subsegments: [1,\u00a01][1,\u00a01] (22), [1,\u00a02][1,\u00a02] (00), [1,\u00a03][1,\u00a03] ( 00), [2,\u00a02][2,\u00a02] (00), totaling 22.4. SS = \"aaba\", suspicious subsegments: [1,\u00a01][1,\u00a01] (22), [1,\u00a02][1,\u00a02] (00), [1,\u00a03][1,\u00a03] ( 00), [1,\u00a04][1,\u00a04] (00), [2,\u00a02][2,\u00a02] (00), [4,\u00a04][4,\u00a04] (00), totaling 22.In the third example, from the condition after all requests SS = \"abcde\", W=[7,2,10,1,7]W=[7,2,10,1,7].1. SS = \"a\", suspicious subsegments: [1,\u00a01][1,\u00a01] (77), totaling 77.2. SS = \"ab\", suspicious subsegments: [1,\u00a01][1,\u00a01] (77), [1,\u00a02][1,\u00a02] (22), totaling 99.3. SS = \"abc\", suspicious subsegments: [1,\u00a01][1,\u00a01] (77), [1,\u00a02][1,\u00a02] (22), [1,\u00a03][1,\u00a03] ( 22), totaling 1111.4. SS = \"abcd\", suspicious subsegments: [1,\u00a01][1,\u00a01] (77), [1,\u00a02][1,\u00a02] (22), [1,\u00a03][1,\u00a03] ( 22), [1,\u00a04][1,\u00a04] (11), totaling 1212.5. SS = \"abcde\", suspicious subsegments: [1,\u00a01][1,\u00a01] (77), [1,\u00a02][1,\u00a02] (22), [1,\u00a03][1,\u00a03] ( 22), [1,\u00a04][1,\u00a04] (11), [1,\u00a05][1,\u00a05] (11), totaling 1313.","tags":["data structures","strings"],"code":"\/\/ S2OJ Submission #67679 @ 1675132874448\n#include \n\n#define int long long\n\n\n\nconst int N = 1e6 + 10;\n\nint n;\n\nchar s[N];\n\nint w[N];\n\nstd::map cnt;\n\nint q[N], top;\n\nint nxt[N], anc[N];\n\nint ans1, ans2;\n\n__int128_t ans;\n\nint sum;\n\n\n\nvoid write(__int128_t x) {\n\n\tif (!x) return ;\n\n\twrite(x \/ 10);\n\n\tputchar(x % 10 + '0');\n\n}\n\n\n\nsigned main() {\n\n\tscanf(\"%lld\", &n);\n\n\tnxt[1] = 0;\n\n\tgetchar();\n\n\ts[1] = getchar();\n\n\tscanf(\"%lld\", &w[1]);\n\n\tans = w[1];\n\n\tans1 = w[1] % 26, ans2 = w[1];\n\n\tq[top = 1] = 1;\n\n\tif (ans == 0) {\n\n\t\tputs(\"0\");\n\n\t}\n\n\telse {\n\n\t\twrite(ans);\n\n\t\tputs(\"\");\n\n\t}\n\n\tfor (int i = 2, j = 0; i <= n; ++i) {\n\n\t\tgetchar();\n\n\t\ts[i] = getchar();\n\n\t\tscanf(\"%lld\", &w[i]);\n\n\t\ts[i] = (s[i] - 'a' + ans1) % 26 + 'a';\n\n\t\tw[i] ^= ans2;\n\n\t\twhile (j && s[j + 1] != s[i]) {\n\n\t\t\tj = nxt[j];\n\n\t\t}\n\n\t\tif (s[j + 1] == s[i]) ++j;\n\n\t\tnxt[i] = j;\n\n\t\tif (s[nxt[i - 1] + 1] == s[i]) {\n\n\t\t\tanc[i - 1] = anc[nxt[i - 1]];\n\n\t\t}\n\n\t\telse {\n\n\t\t\tanc[i - 1] = nxt[i - 1];\n\n\t\t}\n\n\t\tfor (int k = i - 1; k > 0;) {\n\n\t\t\tif (s[k + 1] == s[i]) {\n\n\t\t\t\tk = anc[k];\n\n\t\t\t}\n\n\t\t\telse {\n\n\t\t\t\tint v = w[*std::lower_bound(q + 1, q + top + 1, i - k)];\n\n\t\t\t\t--cnt[v];\n\n\t\t\t\tsum -= v;\n\n\t\t\t\tif (cnt[v] == 0) cnt.erase(v);\n\n\t\t\t\tk = nxt[k];\n\n\t\t\t}\n\n\t\t}\n\n\t\tif (s[1] == s[i]) {\n\n\t\t\t++cnt[w[i]];\n\n\t\t\tsum += w[i];\n\n\t\t}\n\n\t\twhile (top && w[i] <= w[q[top]]) {\n\n\t\t\t--top;\n\n\t\t}\n\n\t\tq[++top] = i;\n\n\t\tint num = 0;\n\n\t\tfor (auto it = cnt.upper_bound(w[i]); it != cnt.end();) {\n\n\t\t\tsum -= 1ll * (it->first) * (it->second);\n\n\t\t\tnum += it->second;\n\n\t\t\tauto jt = std::next(it);\n\n\t\t\tcnt.erase(it);\n\n\t\t\tit = jt;\n\n\t\t}\n\n\t\tcnt[w[i]] += num;\n\n\t\tsum += num * w[i];\n\n\t\tint res = w[q[1]] + sum;\n\n\t\tans1 = (ans1 + res) % 26;\n\n\t\tans2 = (ans2 + res) & ((1ll << 30) - 1);\n\n\t\tans += res;\n\n\t\tif (ans == 0) {\n\n\t\t\tputs(\"0\");\n\n\t\t}\n\n\t\telse {\n\n\t\t\twrite(ans);\n\n\t\t\tputs(\"\");\n\n\t\t}\n\n\t}\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1316","problem_id":"B","statement":"B. String Modificationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has a string ss of length nn. He decides to make the following modification to the string: Pick an integer kk, (1\u2264k\u2264n1\u2264k\u2264n). For ii from 11 to n\u2212k+1n\u2212k+1, reverse the substring s[i:i+k\u22121]s[i:i+k\u22121] of ss. For example, if string ss is qwer and k=2k=2, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length 22) weqr (after reversing the second substring of length 22) werq (after reversing the last substring of length 22) Hence, the resulting string after modifying ss with k=2k=2 is werq. Vasya wants to choose a kk such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of kk. Among all such kk, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.A string aa is lexicographically smaller than a string bb if and only if one of the following holds: aa is a prefix of bb, but a\u2260ba\u2260b; in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb. InputEach test contains multiple test cases. The first line contains the number of test cases tt (1\u2264t\u226450001\u2264t\u22645000). The description of the test cases follows.The first line of each test case contains a single integer nn (1\u2264n\u226450001\u2264n\u22645000)\u00a0\u2014 the length of the string ss.The second line of each test case contains the string ss of nn lowercase latin letters.It is guaranteed that the sum of nn over all test cases does not exceed 50005000.OutputFor each testcase output two lines:In the first line output the lexicographically smallest string s\u2032s\u2032 achievable after the above-mentioned modification. In the second line output the appropriate value of kk (1\u2264k\u2264n1\u2264k\u2264n) that you chose for performing the modification. If there are multiple values of kk that give the lexicographically smallest string, output the smallest value of kk among them.ExampleInputCopy6\n4\nabab\n6\nqwerty\n5\naaaaa\n6\nalaska\n9\nlfpbavjsm\n1\np\nOutputCopyabab\n1\nertyqw\n3\naaaaa\n1\naksala\n6\navjsmbpfl\n5\np\n1\nNoteIn the first testcase of the first sample; the string modification results for the sample abab are as follows : for k=1k=1 : abab for k=2k=2 : baba for k=3k=3 : abab for k=4k=4 : babaThe lexicographically smallest string achievable through modification is abab for k=1k=1 and 33. Smallest value of kk needed to achieve is hence 11. ","tags":["brute force","constructive algorithms","implementation","sortings","strings"],"code":"#include \n\nusing namespace std;\n\n\n\n#ifdef AISH_PC\n\n #include \n\n#else\n\n #define debug(x...)\n\n#endif\n\n\n\n#define fastIO {ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}\n\n#define int long long\n\n#define mod 1000000007\n\n#define PI 3.1415926535897932384626\n\n#define endl \"\\n\"\n\n#define all(v) (v).begin(),(v).end()\n\n#define YES cout << \"YES\" << \"\\n\"\n\n#define NO cout << \"NO\" << \"\\n\"\n\n#define ispowoftwo(n) (!(n & (n-1)))\n\n#define pb push_back\n\n#define ppb pop_back\n\n#define mine(a) (*min_element((a).begin(), (a).end()))\n\n#define maxe(a) (*max_element((a).begin(), (a).end()))\n\n#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())\n\n#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())\n\n#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())\n\n#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())\n\ntypedef pair pii;\n\ntypedef vector vi;\n\ntypedef vector vp;\n\ntypedef vector vvi;\n\nvi prime;\n\n\n\nint gcd(int a, int b)\n\n{\n\n if (a == 0)\n\n return b;\n\n return gcd(b % a, a);\n\n}\n\n\n\n\/\/Function for creating a vector of prime numbers\n\nvoid primes(int n)\n\n{\n\n prime.assign(n+1,1);\n\n for (int p=2;p*p<=n;p++) \n\n {\n\n if(prime[p]) \n\n {\n\n for (int i=p*p;i<=n;i+=p)\n\n {\n\n prime[i]=0;\n\n }\n\n }\n\n }\n\n}\n\n\n\n\/\/Conditional sorting in vector of pairs\n\nbool sortcon(const pair&a,const pair&b)\n\n{\n\n if(a.first==b.first)\n\n {\n\n return(a.second>b.second);\n\n }\n\n return a.first>n>>s;\n\n mapmp;\n\n int k=0;\n\n for(int i = 0; i < n; i++)\n\n {\n\n string s1=s.substr(0,k);\n\n string s2=s.substr(k);\n\n if((n-k)%2!=0)\n\n {\n\n reverse(all(s1));\n\n }\n\n if(mp[s2+s1]==0)\n\n {\n\n mp[s2+s1]=k+1;\n\n }\n\n else\n\n {\n\n mp[s2+s1]=min(mp[s2+s1],k+1);\n\n }\n\n debug(s1, s2);\n\n k++;\n\n }\n\n auto it=mp.begin();\n\n cout<first<second<<><><><><><><><><><><>><><>><><><><><><><>\n\n\n\nint32_t main()\n\n{ \n\n fastIO;\n\n auto start = std::chrono::high_resolution_clock::now(); \n\n int t;\n\n t=1;\n\n cin>>t;\n\n while(t--)\n\n {\n\n solve(); \n\n } \n\n \n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1291","problem_id":"F","statement":"F. Coffee Varieties (easy version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the easy version of the problem. You can find the hard version in the Div. 1 contest. Both versions only differ in the number of times you can ask your friend to taste coffee.This is an interactive problem.You're considering moving to another city; where one of your friends already lives. There are nn caf\u00e9s in this city, where nn is a power of two. The ii-th caf\u00e9 produces a single variety of coffee aiai. As you're a coffee-lover, before deciding to move or not, you want to know the number dd of distinct varieties of coffees produced in this city.You don't know the values a1,\u2026,ana1,\u2026,an. Fortunately, your friend has a memory of size kk, where kk is a power of two.Once per day, you can ask him to taste a cup of coffee produced by the caf\u00e9 cc, and he will tell you if he tasted a similar coffee during the last kk days.You can also ask him to take a medication that will reset his memory. He will forget all previous cups of coffee tasted. You can reset his memory at most 30\u00a000030\u00a0000 times.More formally, the memory of your friend is a queue SS. Doing a query on caf\u00e9 cc will: Tell you if acac is in SS; Add acac at the back of SS; If |S|>k|S|>k, pop the front element of SS. Doing a reset request will pop all elements out of SS.Your friend can taste at most 2n2k2n2k cups of coffee in total. Find the diversity dd (number of distinct values in the array aa).Note that asking your friend to reset his memory does not count towards the number of times you ask your friend to taste a cup of coffee.In some test cases the behavior of the interactor is adaptive. It means that the array aa may be not fixed before the start of the interaction and may depend on your queries. It is guaranteed that at any moment of the interaction, there is at least one array aa consistent with all the answers given so far.InputThe first line contains two integers nn and kk (1\u2264k\u2264n\u226410241\u2264k\u2264n\u22641024, kk and nn are powers of two).It is guaranteed that 2n2k\u226420\u00a00002n2k\u226420\u00a0000.InteractionYou begin the interaction by reading nn and kk. To ask your friend to taste a cup of coffee produced by the caf\u00e9 cc, in a separate line output? ccWhere cc must satisfy 1\u2264c\u2264n1\u2264c\u2264n. Don't forget to flush, to get the answer.In response, you will receive a single letter Y (yes) or N (no), telling you if variety acac is one of the last kk varieties of coffee in his memory. To reset the memory of your friend, in a separate line output the single letter R in upper case. You can do this operation at most 30\u00a000030\u00a0000 times. When you determine the number dd of different coffee varieties, output! ddIn case your query is invalid, you asked more than 2n2k2n2k queries of type ? or you asked more than 30\u00a000030\u00a0000 queries of type R, the program will print the letter E and will finish interaction. You will receive a Wrong Answer verdict. Make sure to exit immediately to avoid getting other verdicts.After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hack formatThe first line should contain the word fixedThe second line should contain two integers nn and kk, separated by space (1\u2264k\u2264n\u226410241\u2264k\u2264n\u22641024, kk and nn are powers of two).It must hold that 2n2k\u226420\u00a00002n2k\u226420\u00a0000.The third line should contain nn integers a1,a2,\u2026,ana1,a2,\u2026,an, separated by spaces (1\u2264ai\u2264n1\u2264ai\u2264n).ExamplesInputCopy4 2\nN\nN\nY\nN\nN\nN\nN\nOutputCopy? 1\n? 2\n? 3\n? 4\nR\n? 4\n? 1\n? 2\n! 3\nInputCopy8 8\nN\nN\nN\nN\nY\nY\nOutputCopy? 2\n? 6\n? 4\n? 5\n? 2\n? 5\n! 6\nNoteIn the first example; the array is a=[1,4,1,3]a=[1,4,1,3]. The city produces 33 different varieties of coffee (11, 33 and 44).The successive varieties of coffee tasted by your friend are 1,4,1,3,3,1,41,4,1,3,3,1,4 (bold answers correspond to Y answers). Note that between the two ? 4 asks, there is a reset memory request R, so the answer to the second ? 4 ask is N. Had there been no reset memory request, the answer to the second ? 4 ask is Y.In the second example, the array is a=[1,2,3,4,5,6,6,6]a=[1,2,3,4,5,6,6,6]. The city produces 66 different varieties of coffee.The successive varieties of coffee tasted by your friend are 2,6,4,5,2,52,6,4,5,2,5.","tags":["graphs","interactive"],"code":"\/\/ LUOGU_RID: 93829713\n#include\n\nusing namespace std;\n\n#define pi pair\n\n#define mp make_pair\n\n#define fi first\n\n#define se second\n\n#define pb push_back\n\n#define ls (rt<<1)\n\n#define rs (rt<<1|1)\n\n#define mid ((l+r)>>1)\n\n#define lowbit(x) (x&-x)\n\nconst int maxn=1e4+5,M=2e5+5,mod=998244353;\n\ninline int read(){\n\n\tchar ch=getchar();bool f=0;int x=0;\n\n\tfor(;!isdigit(ch);ch=getchar())if(ch=='-')f=1;\n\n\tfor(;isdigit(ch);ch=getchar())x=(x<<1)+(x<<3)+(ch^48);\n\n\tif(f==1){x=-x;}return x;\n\n}\n\nvoid print(int x){\n\n static int a[55];int top=0;\n\n if(x<0) putchar('-'),x=-x;\n\n do{a[top++]=x%10,x\/=10;}while(x);\n\n while(top) putchar(a[--top]+48);\n\n}\n\nint n,k,flag[maxn],x,ans=0;\n\nint g[1055][1055];\n\nchar a[5];\n\nvoid query(int x){\n\n\tcout<<\"? \"<\n\n#define int long long\n\n#define fa(i,a,n) for(int i=a;i\n\n#define Utaval ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\nusing namespace std;\n\n\n\nvoid solve(){\n\n int n,a,b,k;\n\n cin>>n>>a>>b>>k;\n\n vectorv(n),temp;\n\n fa(i,0,n)cin>>v[i];\n\n fa(i,0,n){\n\n int kar = v[i]%(a+b);\n\n if(!kar)temp.pb(b);\n\n else if(kar>a)temp.pb(kar-a);\n\n }\n\n int ct=0;\n\n sort(temp.begin(),temp.end());\n\n ct+=(n-temp.size());\n\n fa(i,0,temp.size()){\n\n int ans = temp[i]\/a;\n\n if(temp[i]%a)ans++;\n\n if(ans>k)break;\n\n k-=ans;\n\n ct++;\n\n }\n\n cout<>t;\n\n while(t--)\n\n solve();\n\n}","language":"cpp"} -{"contest_id":"1285","problem_id":"D","statement":"D. Dr. Evil Underscorestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; as a friendship gift, Bakry gave Badawy nn integers a1,a2,\u2026,ana1,a2,\u2026,an and challenged him to choose an integer XX such that the value max1\u2264i\u2264n(ai\u2295X)max1\u2264i\u2264n(ai\u2295X) is minimum possible, where \u2295\u2295 denotes the bitwise XOR operation.As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of max1\u2264i\u2264n(ai\u2295X)max1\u2264i\u2264n(ai\u2295X).InputThe first line contains integer nn (1\u2264n\u22641051\u2264n\u2264105).The second line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u2264230\u221210\u2264ai\u2264230\u22121).OutputPrint one integer \u2014 the minimum possible value of max1\u2264i\u2264n(ai\u2295X)max1\u2264i\u2264n(ai\u2295X).ExamplesInputCopy3\n1 2 3\nOutputCopy2\nInputCopy2\n1 5\nOutputCopy4\nNoteIn the first sample; we can choose X=3X=3.In the second sample, we can choose X=5X=5.","tags":["bitmasks","brute force","dfs and similar","divide and conquer","dp","greedy","strings","trees"],"code":"#include\n\ntypedef long long ll;\n\nconst int maxx=10010;\n\nconst int inf=0x3f3f3f3f;\n\nusing namespace std;\n\nvectora;\n\nint n;\n\nint solve(vectorv, int bit)\n\n{\n\n if(bit<0 || v.size()==0)\n\n return 0;\n\n vectorz,o;\n\n for(int i=0; i>bit)&1)\n\n z.push_back(v[i]);\n\n else\n\n o.push_back(v[i]);\n\n }\n\n if(o.size()==0)\n\n return solve(z,bit-1);\n\n if(z.size()==0)\n\n return solve(o,bit-1);\n\n return (min(solve(o,bit-1),solve(z, bit-1))|1<>n;\n\n for(int i=1,x; i++<=n;)\n\n {\n\n cin>>x;\n\n a.push_back(x);\n\n }\n\n cout<\n\n#include \n\nusing namespace std;\n\nconst int maxn = 100000 + 10;\n\nint n;\n\nint trie[maxn*30][2], cnt;\n\nvoid insert(int val)\n\n{\n\n int x = 0;\n\n for (int i=(1<<29);i;i>>=1)\n\n {\n\n int a = (bool)(val & i);\n\n if (!trie[x][a]) trie[x][a] = ++cnt;\n\n x = trie[x][a];\n\n }\n\n}\n\nint find(int x, int k)\n\n{\n\n if ((!trie[x][0] && !trie[x][1])) return 0;\n\n int ans = 0x3f3f3f3f;\n\n if (trie[x][0]) ans = min(ans, find(trie[x][0], k >> 1));\n\n if (trie[x][1]) ans = min(ans, find(trie[x][1], k >> 1));\n\n return trie[x][0] && trie[x][1] ? (k | ans) : ans;\n\n}\n\nint main()\n\n{\n\n scanf(\"%d\",&n);\n\n for (int i=1;i<=n;++i)\n\n {\n\n int a;\n\n scanf(\"%d\",&a);\n\n insert(a);\n\n }\n\n printf(\"%d\\n\",find(0, 1<<29));\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1316","problem_id":"A","statement":"A. Grade Allocationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputnn students are taking an exam. The highest possible score at this exam is mm. Let aiai be the score of the ii-th student. You have access to the school database which stores the results of all students.You can change each student's score as long as the following conditions are satisfied: All scores are integers 0\u2264ai\u2264m0\u2264ai\u2264m The average score of the class doesn't change. You are student 11 and you would like to maximize your own score.Find the highest possible score you can assign to yourself such that all conditions are satisfied.InputEach test contains multiple test cases. The first line contains the number of test cases tt (1\u2264t\u22642001\u2264t\u2264200). The description of the test cases follows.The first line of each test case contains two integers nn and mm (1\u2264n\u22641031\u2264n\u2264103, 1\u2264m\u22641051\u2264m\u2264105) \u00a0\u2014 the number of students and the highest possible score respectively.The second line of each testcase contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u2264m0\u2264ai\u2264m) \u00a0\u2014 scores of the students.OutputFor each testcase, output one integer \u00a0\u2014 the highest possible score you can assign to yourself such that both conditions are satisfied._ExampleInputCopy2\n4 10\n1 2 3 4\n4 5\n1 2 3 4\nOutputCopy10\n5\nNoteIn the first case; a=[1,2,3,4]a=[1,2,3,4], with average of 2.52.5. You can change array aa to [10,0,0,0][10,0,0,0]. Average remains 2.52.5, and all conditions are satisfied.In the second case, 0\u2264ai\u226450\u2264ai\u22645. You can change aa to [5,1,1,3][5,1,1,3]. You cannot increase a1a1 further as it will violate condition 0\u2264ai\u2264m0\u2264ai\u2264m.","tags":["implementation"],"code":"\/\/\/Moba8ta gya mn el 2alb\n\n#pragma GCC optimize(\"O3\")\n\n#pragma GCC optimize(\"unroll-loops\")\n\n#pragma GCC target(\"avx,avx2,fma\")\n\n#include \"bits\/stdc++.h\"\n\nusing namespace std;\n\n\n\n#define pb push_back\n\n#define F first\n\n#define S second\n\n#define f(i, a, b) for (int i = a; i < b; i++)\n\n#define all(a) a.begin(), a.end()\n\n#define rall(a) a.rbegin(), a.rend()\n\n#define sz(x) (int)(x).size()\n\n#define mp(x, y) make_pair(x, y)\n\n#define popCnt(x) (__builtin_popcountll(x))\n\n#define int ll\n\n\n\nusing ll = long long;\n\nusing ull = unsigned long long;\n\nusing uint = uint32_t;\n\nusing ii = pair;\n\n\n\nconst int N = 2e5 + 5, A = 12, LG = 18, MOD = 1e9 + 7;\n\nconst long double PI = acos(-1);\n\nconst long double EPS = 1e-9;\n\nconst int INF = 1e18;\n\nvoid doWork()\n\n{\n\n int n, m;\n\n cin >> n >> m;\n\n int sum = 0;\n\n f(i,0,n) {\n\n int x;\n\n cin >> x;\n\n sum += x;\n\n }\n\n cout << min(sum,m) << endl;\n\n\n\n}\n\nint32_t main()\n\n{\n\n#ifdef ONLINE_JUDGE\n\n ios_base::sync_with_stdio(0);\n\n cin.tie(0);\n\n#endif \/\/ ONLINE_JUDGE\n\n int t = 1;\n\n cin >> t;\n\n while (t--)\n\n {\n\n doWork();\n\n }\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1310","problem_id":"F","statement":"F. Bad Cryptographytime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputIn modern cryptography much is tied to the algorithmic complexity of solving several problems. One of such problems is a discrete logarithm problem. It is formulated as follows: Let's fix a finite field and two it's elements aa and bb. One need to fun such xx that ax=bax=b or detect there is no such x. It is most likely that modern mankind cannot solve the problem of discrete logarithm for a sufficiently large field size. For example, for a field of residues modulo prime number, primes of 1024 or 2048 bits are considered to be safe. However, calculations with such large numbers can place a significant load on servers that perform cryptographic operations. For this reason, instead of a simple module residue field, more complex fields are often used. For such field no fast algorithms that use a field structure are known, smaller fields can be used and operations can be properly optimized. Developer Nikolai does not trust the generally accepted methods, so he wants to invent his own. Recently, he read about a very strange field\u00a0\u2014 nimbers, and thinks it's a great fit for the purpose. The field of nimbers is defined on a set of integers from 0 to 22k\u2212122k\u22121 for some positive integer kk . Bitwise exclusive or (\u2295\u2295) operation is used as addition. One of ways to define multiplication operation (\u2299\u2299) is following properties: 0\u2299a=a\u22990=00\u2299a=a\u22990=0 1\u2299a=a\u22991=a1\u2299a=a\u22991=a a\u2299b=b\u2299aa\u2299b=b\u2299a a\u2299(b\u2299c)=(a\u2299b)\u2299ca\u2299(b\u2299c)=(a\u2299b)\u2299c a\u2299(b\u2295c)=(a\u2299b)\u2295(a\u2299c)a\u2299(b\u2295c)=(a\u2299b)\u2295(a\u2299c) If a=22na=22n for some integer n>0n>0, and b0n>0, then a\u2299a=32\u22c5aa\u2299a=32\u22c5a. For example: 4\u22994=64\u22994=6 8\u22998=4\u22992\u22994\u22992=4\u22994\u22992\u22992=6\u22993=(4\u22952)\u22993=(4\u22993)\u2295(2\u2299(2\u22951))=(4\u22993)\u2295(2\u22992)\u2295(2\u22991)=12\u22953\u22952=13.8\u22998=4\u22992\u22994\u22992=4\u22994\u22992\u22992=6\u22993=(4\u22952)\u22993=(4\u22993)\u2295(2\u2299(2\u22951))=(4\u22993)\u2295(2\u22992)\u2295(2\u22991)=12\u22953\u22952=13. 32\u229964=(16\u22992)\u2299(16\u22994)=(16\u229916)\u2299(2\u22994)=24\u22998=(16\u22958)\u22998=(16\u22998)\u2295(8\u22998)=128\u229513=14132\u229964=(16\u22992)\u2299(16\u22994)=(16\u229916)\u2299(2\u22994)=24\u22998=(16\u22958)\u22998=(16\u22998)\u2295(8\u22998)=128\u229513=141 5\u22996=(4\u22951)\u2299(4\u22952)=(4\u22994)\u2295(4\u22992)\u2295(4\u22991)\u2295(1\u22992)=6\u22958\u22954\u22952=85\u22996=(4\u22951)\u2299(4\u22952)=(4\u22994)\u2295(4\u22992)\u2295(4\u22991)\u2295(1\u22992)=6\u22958\u22954\u22952=8 Formally, this algorithm can be described by following pseudo-code. multiply(a, b) { ans = 0 for p1 in bits(a) \/\/ numbers of bits of a equal to one for p2 in bits(b) \/\/ numbers of bits of b equal to one ans = ans xor multiply_powers_of_2(1 << p1, 1 << p2) return ans;}multiply_powers_of_2(a, b) { if (a == 1 or b == 1) return a * b n = maximal value, such 2^{2^{n}} <= max(a, b) power = 2^{2^{n}}; if (a >= power and b >= power) { return multiply(power * 3 \/ 2, multiply_powers_of_2(a \/ power, b \/ power)) } else if (a >= power) { return multiply_powers_of_2(a \/ power, b) * power } else { return multiply_powers_of_2(a, b \/ power) * power }}It can be shown, that this operations really forms a field. Moreover, than can make sense as game theory operations, but that's not related to problem much. With the help of appropriate caching and grouping of operations, it is possible to calculate the product quickly enough, which is important to improve speed of the cryptoalgorithm. More formal definitions as well as additional properties can be clarified in the wikipedia article at link. The authors of the task hope that the properties listed in the statement should be enough for the solution. Powering for such muliplication is defined in same way, formally a\u2299k=a\u2299a\u2299\u22ef\u2299a\ue152\ue153\ue151\ue150\ue154\ue154\ue154\ue154\ue154\ue154\ue154\ue154\ue154\ue154\ue154\ue154\ue154\ue154k\u00a0timesa\u2299k=a\u2299a\u2299\u22ef\u2299a\u23dfk\u00a0times.You need to analyze the proposed scheme strength. For pairs of numbers aa and bb you need to find such xx, that a\u2299x=ba\u2299x=b, or determine that it doesn't exist. InputIn the first line of input there is single integer tt (1\u2264t\u22641001\u2264t\u2264100) \u2014 number of pairs, for which you need to find the discrete logarithm.In each of next tt line there is a pair of integers aa bb (1\u2264a,b<2641\u2264a,b<264). OutputFor each pair you should print one integer xx (0\u2264x<2640\u2264x<264), such that a\u2299x=ba\u2299x=b, or -1 if no such x exists. It can be shown, that if any such xx exists, there is one inside given bounds. If there are several good values, you can output any of them. ExampleInputCopy7\n2 2\n1 1\n2 3\n8 10\n8 2\n321321321321 2\n123214213213 4356903202345442785\nOutputCopy1\n1\n2\n4\n-1\n6148914691236517205\n68943624821423112\n","tags":["math","number theory"],"code":"#include \n\nusing namespace std;\ntypedef unsigned long long ull;\nconst ull l = 0xFFFFFFFFFFFFFFFF;\nconst int p[] = {3, 5, 17, 257, 641, 65537, 6700417}, m[] = {1, 2, 6, 16, 25, 256, 2588}, I[] = {2, 2, 1, 32, 590, 16384, 3883315};\null F[65][65], T[65][257][257], a, b, ans; int t, r[7];\null mul(ull, ull) ;\null mul2(ull a, ull b) {\nif (a == 1 || b == 1) return a * b;\nif (a < b) swap(a, b);\nint x = __builtin_ctzll(a), y = __builtin_ctzll(b), z = 1 << 63 - __builtin_clz(x);\nif (y & z) return mul((1ull << z >> 1) * 3, mul2(a >> z, b >> z));\nreturn mul2(a >> z, b) << z;\n}\null mul(ull a, ull b) {\null x, y, c = 0;\nfor (ull i=a; i; i^=x) {\nx = 1ull << __builtin_ctzll(i);\nfor (ull j=b; j; j^=y)\ny = 1ull << __builtin_ctzll(j), c ^= mul2(x, y);\n} return c;\n}\null prod(ull a, ull b) {\null c = 0;\nfor (int i=0; i<8; i++)\nfor (int j=0; j<8; j++)\nc ^= T[i<<3|j][a>>(i<<3)&255][b>>(j<<3)&255];\nreturn c;\n}\null qpow(ull x, ull y) {\null t = 1;\nfor (; y; y>>=1) {\nif (y & 1) t = prod(t, x);\nx = prod(x, x);\n} return t;\n}\nint main() {\nfor (int x=0; x<64; x++)\nfor (int y=0; y<64; y++)\nF[x][y] = mul2(1ull << x, 1ull << y);\nfor (int i=0; i<8; i++)\nfor (int j=0; j<8; j++) {\nfor (int x=0; x<8; x++)\nfor (int y=0; y<8; y++)\nT[i<<3|j][1<> t;\nwhile (t --) {\nscanf (\"%llu%llu\", &a, &b), ans = 0;\nfor (int i=0; i<7; i++) {\null A = qpow(a, l \/ p[i]), B = qpow(b, l \/ p[i]), Am = qpow(A, m[i]), P = 1; int res = -1;\nunordered_map id;\nfor (int j=0; j> _T; \\\n while (_T--)\n\ntypedef tree, rb_tree_tag,\n tree_order_statistics_node_update>\n ordered_set;\n\ntypedef tree, null_type,\n less>, rb_tree_tag,\n tree_order_statistics_node_update>\n ordered_set_pair;\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-----\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\nconst int INF = 1e18; \/\/ infinity\nconst int mod = 1e9 + 7; \/\/ mod\nconst int base1 = 972663749; \/\/ base1\nconst int base2 = 998244353; \/\/ base2\nconst int mod1 = 1000000007; \/\/ mod1\nconst int mod2 = 1000000009; \/\/ mod2\n\nconst double pi = 4 * atan(1);\n\nvector dx = {-1, +1, +0, +0, +1, -1, +1, -1};\nvector dy = {+0, +0, +1, -1, +1, -1, -1, +1};\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/----\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\nint modpower(int x, int n, int m)\n{\n if (n == 0)\n return 1 % m;\n int u = modpower(x, n \/ 2, m);\n u = (u * u) % m;\n if (n % 2 == 1)\n u = (u * x) % m;\n return u;\n}\n\nint modinverse(int i, int MOD)\n{\n if (i == 1)\n return 1;\n return (MOD - ((MOD \/ i) * modinverse(MOD % i, MOD)) % MOD + MOD) % MOD;\n}\n\nint lcm(int x1, int x2)\n{\n return ((x1 * x2) \/ __gcd(x1, x2));\n}\n\nint bitCount(unsigned int u)\n{\n unsigned int uCount;\n uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111);\n return ((uCount + (uCount >> 3)) & 030707070707) % 63;\n}\n\nvoid printVector(vector &array)\n{\n int sz = array.size();\n if (sz == 0)\n return;\n for (int i = 0; i < sz - 1; i++)\n {\n cout << array[i] << \" \";\n }\n cout << array[sz - 1] << endl;\n}\n\nvoid printArray(int array[], int sz)\n{\n for (int i = 0; i < sz - 1; i++)\n {\n cout << array[i] << \" \";\n }\n cout << array[sz - 1] << endl;\n}\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/----main-function----\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/====================================================================================================\n\/\/====================================================================================================\n\nint n, m;\nint a[200005];\n\nvoid solve_the_probelm(int test_case)\n{\n cin >> n >> m;\n for (int i = 1; i <= n; i++)\n {\n cin >> a[i];\n }\n\n if (n > m)\n {\n cout << 0 << endl;\n return;\n }\n int ans = 1;\n for (int i = 1; i <= n; i++)\n {\n for (int j = i + 1; j <= n; j++)\n {\n ans = (ans * abs(a[i] - a[j])) % m;\n }\n }\n cout << ans << endl;\n}\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/------------------------\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/====================================================================================================\n\/\/====================================================================================================\nsigned main()\n{\n int test_cases = 1;\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n#ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n\n \/\/ cin >> test_cases; \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/_test_cause___\n\n for (int test_case = 1; test_case <= test_cases; test_case++)\n solve_the_probelm(test_case);\n\n#ifndef ONLINE_JUDGE\n cout << \"\\nExecution Time : \" << 1.0 * clock() \/ CLOCKS_PER_SEC << \"s \";\n#endif\n\n return 0;\n}\n","language":"cpp"} -{"contest_id":"1322","problem_id":"B","statement":"B. Presenttime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputCatherine received an array of integers as a gift for March 8. Eventually she grew bored with it; and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one\u00a0\u2014 xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute(a1+a2)\u2295(a1+a3)\u2295\u2026\u2295(a1+an)\u2295(a2+a3)\u2295\u2026\u2295(a2+an)\u2026\u2295(an\u22121+an)(a1+a2)\u2295(a1+a3)\u2295\u2026\u2295(a1+an)\u2295(a2+a3)\u2295\u2026\u2295(a2+an)\u2026\u2295(an\u22121+an)Here x\u2295yx\u2295y is a bitwise XOR operation (i.e. xx ^ yy in many modern programming languages). You can read about it in Wikipedia: https:\/\/en.wikipedia.org\/wiki\/Exclusive_or#Bitwise_operation.InputThe first line contains a single integer nn (2\u2264n\u22644000002\u2264n\u2264400000)\u00a0\u2014 the number of integers in the array.The second line contains integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641071\u2264ai\u2264107).OutputPrint a single integer\u00a0\u2014 xor of all pairwise sums of integers in the given array.ExamplesInputCopy2\n1 2\nOutputCopy3InputCopy3\n1 2 3\nOutputCopy2NoteIn the first sample case there is only one sum 1+2=31+2=3.In the second sample case there are three sums: 1+2=31+2=3, 1+3=41+3=4, 2+3=52+3=5. In binary they are represented as 0112\u22951002\u22951012=01020112\u22951002\u22951012=0102, thus the answer is 2.\u2295\u2295 is the bitwise xor operation. To define x\u2295yx\u2295y, consider binary representations of integers xx and yy. We put the ii-th bit of the result to be 1 when exactly one of the ii-th bits of xx and yy is 1. Otherwise, the ii-th bit of the result is put to be 0. For example, 01012\u229500112=0110201012\u229500112=01102.","tags":["binary search","bitmasks","constructive algorithms","data structures","math","sortings"],"code":"#include \n\n\n\n#include \n\n#include \n\n#include \n\n#include \n\n\n\n#define TIME 1.0*clock()\/CLOCKS_PER_SEC\n\n#define all(x) x.begin(), x.end()\n\n#define ull unsigned long long\n\n#define pii pair < int , int >\n\n#pragma GCC optimize(\"Ofast\")\n\n#define ld long double\n\n#define pb push_back\n\n#define ll long long\n\n#define endl '\\n'\n\n#define S second\n\n#define F first\n\n#define sz size\n\n#define int ll\n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\ntemplate \n\nusing ordered_set = tree ,rb_tree_tag,tree_order_statistics_node_update>;\n\nmt19937 gen(chrono::system_clock::now().time_since_epoch().count());\n\nint b[500000];\n\nint n;\n\nint get (int x, int y) {\n\n if (x > y) return 0;\n\n int kl = 0;\n\n int l = 1, r = 1;\n\n for (int i = n; i; i--) {\n\n while (l <= n && b[i] + b[l] < x) l++;\n\n while (r <= n && b[i] + b[r] <= y) r++;\n\n kl += (r - l) - (l <= i && i < r);\n\n }\n\n return (kl \/ 2) ;\n\n}\n\nint32_t main() {\n\n #ifdef LOCAL\n\n freopen(\"input.txt\",\"r\",stdin);\n\n freopen(\"output.txt\",\"w\",stdout);\n\n #endif\n\n ios_base::sync_with_stdio(0);\n\n cin.tie(0);\n\n cout.tie(0);\n\n cin >> n;\n\n int a[n + 2];\n\n for (int i = 1; i <= n; i++) cin >> a[i];\n\n int ans = 0;\n\n for (int i = 0; i <= 25; i++){\n\n for (int j = 1; j <= n; j++){\n\n b[j] = (a[j] % (1 << (i + 1)));\n\n }\n\n sort(b + 1, b + 1 + n);\n\n int cnt = get((1 << i), (1 << (i + 1)) - 1) + get(((1 << (i + 1)) + (1 << i)), ((1 << (i + 1)) + (1 << (i + 1)) - 1));\n\n if (cnt % 2) ans += (1 << i);\n\n }\n\n cout << ans;\n\nreturn 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1305","problem_id":"E","statement":"E. Kuroni and the Score Distributiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni is the coordinator of the next Mathforces round written by the \"Proof by AC\" team. All the preparation has been done; and he is discussing with the team about the score distribution for the round.The round consists of nn problems, numbered from 11 to nn. The problems are ordered in increasing order of difficulty, no two problems have the same difficulty. A score distribution for the round can be denoted by an array a1,a2,\u2026,ana1,a2,\u2026,an, where aiai is the score of ii-th problem. Kuroni thinks that the score distribution should satisfy the following requirements: The score of each problem should be a positive integer not exceeding 109109. A harder problem should grant a strictly higher score than an easier problem. In other words, 1\u2264a1\n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\nusing namespace std;\n\n#define fi first\n\n#define sc second\n\n#define mkp make_pair\n\n#define pii pair\n\ntypedef long long ll;\n\nconst int N=1e5+5,oo=1e8,mod=1e9+9;\n\ninline int read() {\n\n int x=0,flag=0;char ch=getchar();\n\n while(ch<'0'||ch>'9') {flag|=(ch=='-');ch=getchar();}\n\n while('0'<=ch&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}\n\n return flag?-x:x;\n\n}\n\ninline int mx(int x,int y) {return x>y?x:y;}\n\ninline int mn(int x,int y) {return x0?x:-x;}\n\ninline int ok(int x) {return x>0?1:-1;}\n\n\n\nint n,m,a[N],t[N];\n\nint main() {\n\n#ifndef ONLINE_JUDGE\n\n freopen(\"1.in\",\"r\",stdin);\n\n freopen(\"1.out\",\"w\",stdout);\n\n#endif\n\n n=read();m=read();\n\n int lim=0;\n\n for(int i=1;i<=n;++i)\n\n lim+=(i-1)\/2;\n\n if(lim=0) {\n\n m-=(p-1)\/2;\n\n a[p]=p;\n\n } else break;\n\n for(int i=1;i=1;--i)\n\n if(t[i]==m) {\n\n a[p]=i;\n\n break;\n\n }\n\n for(int i=p+1;i<=n;++i)\n\n a[i]=oo-(n-i)*10000;\n\n for(int i=1;i<=n;++i) printf(\"%d \",a[i]);\n\n return 0;\n\n}\n\n\/\/yqmpo62327\n\n\/\/Ro5dPTkt","language":"cpp"} -{"contest_id":"1312","problem_id":"F","statement":"F. Attack on Red Kingdomtime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe Red Kingdom is attacked by the White King and the Black King!The Kingdom is guarded by nn castles, the ii-th castle is defended by aiai soldiers. To conquer the Red Kingdom, the Kings have to eliminate all the defenders. Each day the White King launches an attack on one of the castles. Then, at night, the forces of the Black King attack a castle (possibly the same one). Then the White King attacks a castle, then the Black King, and so on. The first attack is performed by the White King.Each attack must target a castle with at least one alive defender in it. There are three types of attacks: a mixed attack decreases the number of defenders in the targeted castle by xx (or sets it to 00 if there are already less than xx defenders); an infantry attack decreases the number of defenders in the targeted castle by yy (or sets it to 00 if there are already less than yy defenders); a cavalry attack decreases the number of defenders in the targeted castle by zz (or sets it to 00 if there are already less than zz defenders). The mixed attack can be launched at any valid target (at any castle with at least one soldier). However, the infantry attack cannot be launched if the previous attack on the targeted castle had the same type, no matter when and by whom it was launched. The same applies to the cavalry attack. A castle that was not attacked at all can be targeted by any type of attack.The King who launches the last attack will be glorified as the conqueror of the Red Kingdom, so both Kings want to launch the last attack (and they are wise enough to find a strategy that allows them to do it no matter what are the actions of their opponent, if such strategy exists). The White King is leading his first attack, and you are responsible for planning it. Can you calculate the number of possible options for the first attack that allow the White King to launch the last attack? Each option for the first attack is represented by the targeted castle and the type of attack, and two options are different if the targeted castles or the types of attack are different.InputThe first line contains one integer tt (1\u2264t\u226410001\u2264t\u22641000) \u2014 the number of test cases.Then, the test cases follow. Each test case is represented by two lines. The first line contains four integers nn, xx, yy and zz (1\u2264n\u22643\u22c51051\u2264n\u22643\u22c5105, 1\u2264x,y,z\u226451\u2264x,y,z\u22645). The second line contains nn integers a1a1, a2a2, ..., anan (1\u2264ai\u226410181\u2264ai\u22641018).It is guaranteed that the sum of values of nn over all test cases in the input does not exceed 3\u22c51053\u22c5105.OutputFor each test case, print the answer to it: the number of possible options for the first attack of the White King (or 00, if the Black King can launch the last attack no matter how the White King acts).ExamplesInputCopy3\n2 1 3 4\n7 6\n1 1 2 3\n1\n1 1 2 2\n3\nOutputCopy2\n3\n0\nInputCopy10\n6 5 4 5\n2 3 2 3 1 3\n1 5 2 3\n10\n4 4 2 3\n8 10 8 5\n2 2 1 4\n8 5\n3 5 3 5\n9 2 10\n4 5 5 5\n2 10 4 2\n2 3 1 4\n1 10\n3 1 5 3\n9 8 7\n2 5 4 5\n8 8\n3 5 1 4\n5 5 10\nOutputCopy0\n2\n1\n2\n5\n12\n5\n0\n0\n2\n","tags":["games","two pointers"],"code":"#include \n\n#define f first\n\n#define s second\n\n#define pb push_back\n\n#define all(x) (x).begin(), (x).end()\n\n#define sz(x) ((int) (x).size())\n\nusing namespace std;\n\n\n\nusing ll = long long;\n\nusing ld = long double;\n\nusing pii = pair;\n\nusing vi = vector;\n\nusing vp = vector ;\n\nusing vl = vector;\n\n\n\nconst int inf = INT_MAX;\n\nconst ll linf = LLONG_MAX;\n\n\n\nconst int mod = 1e9+7;\n\n\n\nint mex(vi v) {\n\n sort(all(v));\n\n v.resize(unique(all(v)) - v.begin());\n\n for(int i = 0; i < sz(v); i++)\n\n if(i != v[i]) return i;\n\n return sz(v);\n\n}\n\n\n\nusing BLOCK = array, 5>;\n\n\n\nvector step[6][6][6];\n\nvector period[6][6][6];\n\n\n\nvoid solve() {\n\n int n, x, y, z;\n\n scanf(\"%d %d %d %d\", &n, &x, &y, &z);\n\n vl a(n);\n\n auto sg = [&](ll v, int j) -> int {\n\n ll b = v \/ 5;\n\n int r = v % 5;\n\n if(b < sz(step[x][y][z])) {\n\n return step[x][y][z][b][r][j];\n\n }\n\n else {\n\n b -= sz(step[x][y][z]);\n\n b %= sz(period[x][y][z]);\n\n return period[x][y][z][b][r][j];\n\n }\n\n };\n\n ll tot = 0;\n\n for(auto &v : a) {\n\n scanf(\"%lld\", &v);\n\n tot ^= sg(v, 0);\n\n }\n\n int ans = 0;\n\n array w = {x, y, z};\n\n for(auto &v : a) {\n\n ll cur = tot ^ sg(v, 0);\n\n for(int j : {0, 1, 2}) {\n\n if(cur == sg(max(0ll, v - w[j]), j))\n\n ans++;\n\n }\n\n }\n\n printf(\"%d\\n\", ans);\n\n\n\n}\n\n\n\nint main() {\n\n for(int x = 1; x <= 5; x++) {\n\n for(int y = 1; y <= 5; y++) {\n\n for(int z = 1; z <= 5; z++) {\n\n set S;\n\n vector > sg;\n\n vector block_sg;\n\n array w = {x, y, z};\n\n for(int j = 0; ; j += 5) {\n\n BLOCK block;\n\n for(int k = j; k < j + 5; k++) {\n\n array nsg;\n\n if(k == 0) nsg = {0, 0, 0};\n\n else for(int i : {0, 1, 2}) {\n\n vi v;\n\n for(int j : {0, 1, 2}) {\n\n if(!i || i != j) {\n\n v.pb(sg[max(0, k - w[j])][j]);\n\n }\n\n }\n\n nsg[i] = mex(v);\n\n }\n\n block[k - j] = nsg;\n\n sg.pb(nsg);\n\n }\n\n if(S.count(block)) {\n\n bool f = false;\n\n for(int q = 0; q < sz(block_sg); q++) {\n\n if(!f && block_sg[q] == block)\n\n f = true;\n\n if(!f) {\n\n step[x][y][z].pb(block_sg[q]);\n\n }\n\n else {\n\n period[x][y][z].pb(block_sg[q]);\n\n }\n\n }\n\n break;\n\n }\n\n block_sg.pb(block);\n\n S.insert(block);\n\n }\n\n }\n\n }\n\n }\n\n int tc;\n\n scanf(\"%d\", &tc);\n\n while(tc--) {\n\n solve();\n\n }\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1292","problem_id":"E","statement":"E. Rin and The Unknown Flowertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMisoilePunch\u266a - \u5f69This is an interactive problem!On a normal day at the hidden office in A.R.C. Markland-N; Rin received an artifact, given to her by the exploration captain Sagar.After much analysis, she now realizes that this artifact contains data about a strange flower, which has existed way before the New Age. However, the information about its chemical structure has been encrypted heavily.The chemical structure of this flower can be represented as a string pp. From the unencrypted papers included, Rin already knows the length nn of that string, and she can also conclude that the string contains at most three distinct letters: \"C\" (as in Carbon), \"H\" (as in Hydrogen), and \"O\" (as in Oxygen).At each moment, Rin can input a string ss of an arbitrary length into the artifact's terminal, and it will return every starting position of ss as a substring of pp.However, the artifact has limited energy and cannot be recharged in any way, since the technology is way too ancient and is incompatible with any current A.R.C.'s devices. To be specific: The artifact only contains 7575 units of energy. For each time Rin inputs a string ss of length tt, the artifact consumes 1t21t2 units of energy. If the amount of energy reaches below zero, the task will be considered failed immediately, as the artifact will go black forever. Since the artifact is so precious yet fragile, Rin is very nervous to attempt to crack the final data. Can you give her a helping hand?InteractionThe interaction starts with a single integer tt (1\u2264t\u22645001\u2264t\u2264500), the number of test cases. The interaction for each testcase is described below:First, read an integer nn (4\u2264n\u2264504\u2264n\u226450), the length of the string pp.Then you can make queries of type \"? s\" (1\u2264|s|\u2264n1\u2264|s|\u2264n) to find the occurrences of ss as a substring of pp.After the query, you need to read its result as a series of integers in a line: The first integer kk denotes the number of occurrences of ss as a substring of pp (\u22121\u2264k\u2264n\u22121\u2264k\u2264n). If k=\u22121k=\u22121, it means you have exceeded the energy limit or printed an invalid query, and you need to terminate immediately, to guarantee a \"Wrong answer\" verdict, otherwise you might get an arbitrary verdict because your solution will continue to read from a closed stream. The following kk integers a1,a2,\u2026,aka1,a2,\u2026,ak (1\u2264a1\n\nusing namespace std;\n\ntemplate void read(T &t) {\n\n\tt=0; char ch=getchar(); int f=1;\n\n\twhile (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }\n\n\tdo { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;\n\n}\n\ntemplate void write(T t) {\n\n\tif (t<0) { putchar('-'); write(-t); return; }\n\n\tif (t>9) write(t\/10);\n\n\tputchar('0'+t%10);\n\n}\n\ntemplate void writeln(T t) { write(t); puts(\"\"); }\n\n#define MP make_pair\n\nconst int xyr=1;\n\nint T,n;\n\nstring ans;\n\nchar t[60]; double all;\n\nint query(string s) {\n\n\tint len=s.size(); all+=1.0\/len\/len;\n\n\tprintf(\"? \"); for (int i=0;i P[10];\n\nvoid dfs(int x,string &s) {\n\n\tif (tmp<0) return;\n\n\tif (x==n) {\n\n\t\ttmp--; if (tmp==0) { ans=s; return; }\n\n\t\tif (query(s)) ans=s,tmp=-1;\n\n\t\treturn;\n\n\t}\n\n\tif (s[x]>0) dfs(x+1,s);\n\n\telse {\n\n\t\tfor (char &a : P[x]) s[x]=a,dfs(x+1,s); s[x]=0;\n\n\t}\n\n}\n\nint main() {\n\n\/\/\tfreopen(\"1.txt\",\"r\",stdin);\n\n\tscanf(\"%d\",&T); while (T--) {\n\n\t\tall=0;\n\n\t\tscanf(\"%d\",&n); if (!xyr) scanf(\"%s\",t);\n\n\t\tans.resize(n);\n\n\t\tfor (int i=0;i4) {\n\n\t\t\tquery(\"CC\"),query(\"CH\"),query(\"CO\");\n\n\t\t\tquery(\"HO\"),query(\"OO\");\n\n\t\t\tfor (int i=1;i0;\n\n\t\t\t\tif (!flag) ans[0]='O',ans[n-1]='C',flag|=query(ans)>0;\n\n\t\t\t\tif (!flag) ans[0]='H',ans[n-1]='C',flag|=query(ans)>0;\n\n\t\t\t\tif (!flag) ans[0]='H',ans[n-1]='H';\n\n\t\t\t} else if (!ans[0]&&ans[n-1]) {\n\n\t\t\t\tans[0]='O'; if (!query(ans)) ans[0]='H';\n\n\t\t\t} else if (!ans[n-1]&&ans[0]) {\n\n\t\t\t\tans[n-1]='C'; if (!query(ans)) ans[n-1]='H';\n\n\t\t\t}\n\n\t\t} else {\n\n\t\t\tint cnt=query(\"CC\")+query(\"CH\")+query(\"CO\");\n\n\t\t\tif (cnt||query(\"HO\")) {\n\n\t\t\t\tfor (int i=0;i<=2;i++) P[i]={'O','H'};\n\n\t\t\t\tP[3]={'O','H','C'}; tmp=1;\n\n\t\t\t\tfor (int i=0;i0);\n\n\t\tprintf(\"! \"); for (int i=0;i\n\n#define ms(x, v) memset(x, v, sizeof(x))\n\n#define il __attribute__((always_inline))\n\n#define U(i,l,r) for(int i(l),END##i(r);i<=END##i;++i)\n\n#define D(i,r,l) for(int i(r),END##i(l);i>=END##i;--i)\n\nusing namespace std;\n\n\n\ntypedef unsigned long long ull;\n\ntypedef long long ll;\n\ntemplate using BS = basic_string;\n\n\n\n\/\/const int SZ(1 << 23);\n\n\/\/unsigned char buf[SZ], *S, *Q;\n\n\/\/#define getchar() ((S==Q)&&(Q=buf+fread(S=buf,1,SZ,stdin)),S==Q?EOF:*S++)\n\ntemplate \n\nvoid rd(T& s) {\n\n\tint c = getchar();\n\n\tT f = 1; s = 0;\n\n\twhile (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); }\n\n\twhile (isdigit(c)) { s = s * 10 + (c ^ 48); c = getchar(); }\n\n\ts *= f;\n\n}\n\ntemplate \n\nvoid rd(T& x, Y&... y) { rd(x), rd(y...); }\n\ntemplate \n\nvoid pr(T s, bool f = 1) {\n\n if (s < 0) { printf(\"-\"); s = -s; }\n\n if (!s) return void(f ? printf(\"0\") : 0);\n\n pr(s \/ 10, 0);\n\n printf(\"%d\", (signed)(s % 10));\n\n}\n\n#define meow(...) fprintf(stderr, __VA_ARGS__)\n\n\n\nconst int N = 2025;\n\nconst ll P = 998244353;\n\n\n\nll sub1(ll n) {\n\n\t\/\/ \u6c42 \\sum_1^n \u5206\u62c6\u6570\n\n\tll f[N] {1}; \/\/ \u5212\u5206\u6570\n\n\tU (i, 1, n)\n\n\t\tU (j, i, n)\n\n\t\t\t(f[j] += f[j - i]) %= P;\n\n\tll ans = 0;\n\n\tU (i, 1, n) (ans += f[i]) %= P;\n\n\treturn ans;\n\n}\n\n\n\nll sub2(ll n) {\n\n\t\/\/ \u6784\u9020\u96c6\u5408\u4f7f\u5f97 i*t_i <= n\uff0c\u8ba1\u6570\n\n \/\/ \u5dee\u5206\u5219\u4e0d\u9700\u8981\u8003\u8651 t_i \u51cf\u7684\u9650\u5236\n\n \/\/ \u8fd9\u6837\u4e00\u4e2a c_i=t_i - t_{i-1}=1 \u5bf9\u548c\u7684\u53f3\u79fb\u4e3a sum 1..i = i(i-1)\/2\n\n ll f[N] {1};\n\n for (int i = 1; i * (i + 1) >> 1 <= n; ++i)\n\n for (int j = i * (i + 1) >> 1; j <= n; ++j)\n\n (f[j] += f[j - i * (i + 1) \/ 2]) %= P;\n\n ll ans = 0;\n\n U (i, 1, n) (ans += f[i]) %= P;\n\n return ans;\n\n}\n\n\n\nint n, k;\n\nbool extend(int k, vector v) {\n\n vector g;\n\n for (int j = 1; j < k; ++j) {\n\n int sum = 0;\n\n sort(v.rbegin(), v.rend());\n\n U (i, 0, v.size() - 1) sum += (i + 1) * v[i];\n\n if (sum > n) return 0;\n\n if (j + 3 < k && sum > 23) return 0;\n\n U (i, 0, v.size() - 1)\n\n U (j, 1, v[i])\n\n g.push_back(i + 1);\n\n v.swap(g);\n\n vector().swap(g);\n\n }\n\n return 1;\n\n}\n\nvector v;\n\nll ans = 0;\n\nbool dfs(int lim) { \/\/ \u968f\u4fbf\u55ef\u5206\n\n if (!extend(k, v)) return 0;\n\n \/\/ clog << lim << ' ' << v.size() << endl;\n\n ++ans;\n\n \/\/ meow(\"x\");\n\n \/\/ for (int x : v) meow(\"%d \", x);\n\n \/\/ meow(\"\\n\");\n\n for (int i = lim, flag; ; ++i) {\n\n v.push_back(i);\n\n flag = dfs(i);\n\n v.pop_back();\n\n if (!flag) return 1;\n\n }\n\n return 1;\n\n}\n\nll sub3() {\n\n dfs(1);\n\n return ans - 1;\n\n}\n\n\n\nint main() {\n\n \/\/ Sol::main();\n\n \/\/ return 0;\n\n\trd(n, k);\n\n\tif (k == 1) exit(printf(\"%lld\", sub1(n)) & 0);\n\n if (k == 2) exit(printf(\"%lld\", sub2(n)) & 0);\n\n printf(\"%lld\", sub3());\n\n}","language":"cpp"} -{"contest_id":"1288","problem_id":"D","statement":"D. Minimax Problemtime limit per test5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given nn arrays a1a1, a2a2, ..., anan; each array consists of exactly mm integers. We denote the yy-th element of the xx-th array as ax,yax,y.You have to choose two arrays aiai and ajaj (1\u2264i,j\u2264n1\u2264i,j\u2264n, it is possible that i=ji=j). After that, you will obtain a new array bb consisting of mm integers, such that for every k\u2208[1,m]k\u2208[1,m] bk=max(ai,k,aj,k)bk=max(ai,k,aj,k).Your goal is to choose ii and jj so that the value of mink=1mbkmink=1mbk is maximum possible.InputThe first line contains two integers nn and mm (1\u2264n\u22643\u22c51051\u2264n\u22643\u22c5105, 1\u2264m\u226481\u2264m\u22648) \u2014 the number of arrays and the number of elements in each array, respectively.Then nn lines follow, the xx-th line contains the array axax represented by mm integers ax,1ax,1, ax,2ax,2, ..., ax,max,m (0\u2264ax,y\u22641090\u2264ax,y\u2264109).OutputPrint two integers ii and jj (1\u2264i,j\u2264n1\u2264i,j\u2264n, it is possible that i=ji=j) \u2014 the indices of the two arrays you have to choose so that the value of mink=1mbkmink=1mbk is maximum possible. If there are multiple answers, print any of them.ExampleInputCopy6 5\n5 0 3 1 2\n1 8 9 1 3\n1 2 3 4 5\n9 1 0 3 7\n2 3 0 6 3\n6 4 1 7 0\nOutputCopy1 5\n","tags":["binary search","bitmasks","dp"],"code":"#include\nusing namespace std;\n#define f(i,j,k) for(register int i=j;i<=k;++i)\n#define g(i,j,k) for(register int i=j;i>=k;--i)\nint n,m,s,l;\nint f[303];\nint a[303030][10];\nint doing(int x){\nint p=(1<=x);\nf[q]=i;\n}\nf(i,0,p)f(j,0,p)if(f[i] && f[j] && (i|j)==p){\ns=f[i];l=f[j];\nreturn 1;\n}\nreturn 0;\n}\nint main(){\ncin>>n>>m;\nf(i,1,n)f(j,1,m)scanf(\"%d\",&a[i][j]);\ns=l=1;\nint le=1,ri=1e9,mid;\nwhile(le<=ri){\nmid=(le+ri)>>1;\nif(doing(mid))le=mid+1;\nelse ri=mid-1;\n}\nprintf(\"%d %d\\n\",s,l);\nreturn 0;\n}\n","language":"cpp"} -{"contest_id":"1312","problem_id":"B","statement":"B. Bogosorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a1,a2,\u2026,ana1,a2,\u2026,an. Array is good if for each pair of indexes i\n\n#include \n\nusing namespace std;\n\nint a[110];\n\nbool cmp(int a,int b){\n\n return a>b;\n\n}\n\nint main()\n\n{\n\n int t;\n\n cin>>t;\n\n while(t--){\n\n int n;\n\n cin>>n;\n\n for(int i=1;i<=n;i++){\n\n cin>>a[i];\n\n }\n\n sort(a+1,a+1+n,cmp);\n\n for(int i=1;i<=n;i++){\n\n cout<\n\n\n\nusing namespace std;\n\nusing ll = long long;\n\nusing vi = vector;\n\n\n\nint main() {\n\n cin.tie(0), ios::sync_with_stdio(0);\n\n ll t, x, y, a, b;\n\n cin >> t;\n\n while (t--) {\n\n cin >> x >> y >> a >> b;\n\n if ((y-x) % (a+b)) cout << -1;\n\n else cout << (y-x)\/(a+b);\n\n cout << '\\n';\n\n }\n\n}","language":"cpp"} -{"contest_id":"1304","problem_id":"C","statement":"C. Air Conditionertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGildong owns a bulgogi restaurant. The restaurant has a lot of customers; so many of them like to make a reservation before visiting it.Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant.The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially.Each customer is characterized by three values: titi \u2014 the time (in minutes) when the ii-th customer visits the restaurant, lili \u2014 the lower bound of their preferred temperature range, and hihi \u2014 the upper bound of their preferred temperature range.A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the ii-th customer is satisfied if and only if the temperature is between lili and hihi (inclusive) in the titi-th minute.Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers.InputEach test contains one or more test cases. The first line contains the number of test cases qq (1\u2264q\u22645001\u2264q\u2264500). Description of the test cases follows.The first line of each test case contains two integers nn and mm (1\u2264n\u22641001\u2264n\u2264100, \u2212109\u2264m\u2264109\u2212109\u2264m\u2264109), where nn is the number of reserved customers and mm is the initial temperature of the restaurant.Next, nn lines follow. The ii-th line of them contains three integers titi, lili, and hihi (1\u2264ti\u22641091\u2264ti\u2264109, \u2212109\u2264li\u2264hi\u2264109\u2212109\u2264li\u2264hi\u2264109), where titi is the time when the ii-th customer visits, lili is the lower bound of their preferred temperature range, and hihi is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive.The customers are given in non-decreasing order of their visit time, and the current time is 00.OutputFor each test case, print \"YES\" if it is possible to satisfy all customers. Otherwise, print \"NO\".You can print each letter in any case (upper or lower).ExampleInputCopy4\n3 0\n5 1 2\n7 3 5\n10 -1 0\n2 12\n5 7 10\n10 16 20\n3 -100\n100 0 0\n100 -50 50\n200 100 100\n1 100\n99 -100 0\nOutputCopyYES\nNO\nYES\nNO\nNoteIn the first case; Gildong can control the air conditioner to satisfy all customers in the following way: At 00-th minute, change the state to heating (the temperature is 0). At 22-nd minute, change the state to off (the temperature is 2). At 55-th minute, change the state to heating (the temperature is 2, the 11-st customer is satisfied). At 66-th minute, change the state to off (the temperature is 3). At 77-th minute, change the state to cooling (the temperature is 3, the 22-nd customer is satisfied). At 1010-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at 00-th minute and leave it be. Then all customers will be satisfied. Note that the 11-st customer's visit time equals the 22-nd customer's visit time.In the second and the fourth case, Gildong has to make at least one customer unsatisfied.","tags":["dp","greedy","implementation","sortings","two pointers"],"code":"\/***********\n\n\n\n MK-1311\n\n\n\n***********\/\n\n#include \n\nusing namespace std;\n\n\n\n#define fi first\n\n#define se second\n\n#define pb push_back\n\n#define nl '\\n'\n\n\n\n#ifdef LOCAL\n\n#include \"debug.h\"\n\n#else\n\n#define debug(...)\n\n#endif\n\n\n\n\n\n\/*\n\n\n\n\n\n*\/\n\n\n\n#define int long long\n\n\n\n\n\nvoid solve() {\n\n int n, it;\n\n cin >> n >> it;\n\n vector t(n + 1, 0), l(n + 1, 0), h(n + 1, 0);\n\n for(int i = 1; i <= n; i++) {\n\n cin >> t[i] >> l[i] >> h[i];\n\n }\n\n int x = it, y = it;\n\n for(int i = 1; i <= n; i++) {\n\n int time = t[i] - t[i - 1];\n\n x = max(l[i], x - time);\n\n y = min(h[i], y + time);\n\n debug(x, y);\n\n if(l[i] > y || h[i] < x) {\n\n cout << \"NO\" << endl;\n\n return;\n\n }\n\n }\n\n cout << \"YES\" << endl;\n\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nint32_t main() {\n\n ios_base::sync_with_stdio(0); cin.tie(0);\n\n int tt = 1; \n\n cin >> tt;\n\n for(int i = 0; i < tt; i++) {\n\n solve();\n\n }\n\n cerr << \"time taken : \" << (float)clock() \/ CLOCKS_PER_SEC << \" secs\" << endl;\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1288","problem_id":"E","statement":"E. Messenger Simulatortime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp is a frequent user of the very popular messenger. He's chatting with his friends all the time. He has nn friends, numbered from 11 to nn.Recall that a permutation of size nn is an array of size nn such that each integer from 11 to nn occurs exactly once in this array.So his recent chat list can be represented with a permutation pp of size nn. p1p1 is the most recent friend Polycarp talked to, p2p2 is the second most recent and so on.Initially, Polycarp's recent chat list pp looks like 1,2,\u2026,n1,2,\u2026,n (in other words, it is an identity permutation).After that he receives mm messages, the jj-th message comes from the friend ajaj. And that causes friend ajaj to move to the first position in a permutation, shifting everyone between the first position and the current position of ajaj by 11. Note that if the friend ajaj is in the first position already then nothing happens.For example, let the recent chat list be p=[4,1,5,3,2]p=[4,1,5,3,2]: if he gets messaged by friend 33, then pp becomes [3,4,1,5,2][3,4,1,5,2]; if he gets messaged by friend 44, then pp doesn't change [4,1,5,3,2][4,1,5,3,2]; if he gets messaged by friend 22, then pp becomes [2,4,1,5,3][2,4,1,5,3]. For each friend consider all position he has been at in the beginning and after receiving each message. Polycarp wants to know what were the minimum and the maximum positions.InputThe first line contains two integers nn and mm (1\u2264n,m\u22643\u22c51051\u2264n,m\u22643\u22c5105) \u2014 the number of Polycarp's friends and the number of received messages, respectively.The second line contains mm integers a1,a2,\u2026,ama1,a2,\u2026,am (1\u2264ai\u2264n1\u2264ai\u2264n) \u2014 the descriptions of the received messages.OutputPrint nn pairs of integers. For each friend output the minimum and the maximum positions he has been in the beginning and after receiving each message.ExamplesInputCopy5 4\n3 5 1 4\nOutputCopy1 3\n2 5\n1 4\n1 5\n1 5\nInputCopy4 3\n1 2 4\nOutputCopy1 3\n1 2\n3 4\n1 4\nNoteIn the first example; Polycarp's recent chat list looks like this: [1,2,3,4,5][1,2,3,4,5] [3,1,2,4,5][3,1,2,4,5] [5,3,1,2,4][5,3,1,2,4] [1,5,3,2,4][1,5,3,2,4] [4,1,5,3,2][4,1,5,3,2] So, for example, the positions of the friend 22 are 2,3,4,4,52,3,4,4,5, respectively. Out of these 22 is the minimum one and 55 is the maximum one. Thus, the answer for the friend 22 is a pair (2,5)(2,5).In the second example, Polycarp's recent chat list looks like this: [1,2,3,4][1,2,3,4] [1,2,3,4][1,2,3,4] [2,1,3,4][2,1,3,4] [4,2,1,3][4,2,1,3] ","tags":["data structures"],"code":"\/\/ time-limit: 3000\n\/\/ problem-url: https:\/\/codeforces.com\/problemset\/problem\/1288\/E\n\/\/ clang-format off\n#include\"bits\/stdc++.h\"\n#include \n#define mod 1000000007\n#define inf 1e18\n#define pq priority_queue\n#define pqi priority_queue\n#define pqimn priority_queue>\n#define pb push_back\n#define sz(v) ((int)(v).size())\n#define all(v) (v).begin(),(v).end()\n#define ss second\n#define ff first\n \nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\ntemplate\nusing pair2 = pair;\nusing pii = pair;\nusing pli = pair;\nusing pll = pair;\ntemplate\nostream& operator <<(ostream& ostream, vector& v) {for(auto& element : v) {cout << element << \" \";}return ostream;}\ntemplate\nistream& operator >>(istream& istream, vector& v) { for(auto& element : v) {cin >> element;}return istream;}\ntemplate T abs(T x) { return x < 0? -x : x; }\ntemplate bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; }\ntemplate bool chmax(T &x, const T& y) { if (x < y) { x = y; return true; } return false; }\n \n \nvoid __print(int x) {cerr << x;}\nvoid __print(long x) {cerr << x;}\nvoid __print(long long x) {cerr << x;}\nvoid __print(unsigned x) {cerr << x;}\nvoid __print(unsigned long x) {cerr << x;}\nvoid __print(unsigned long long x) {cerr << x;}\nvoid __print(float x) {cerr << x;}\nvoid __print(double x) {cerr << x;}\nvoid __print(long double x) {cerr << x;}\nvoid __print(char x) {cerr << '\\'' << x << '\\'';}\nvoid __print(const char *x) {cerr << '\\\"' << x << '\\\"';}\nvoid __print(const string &x) {cerr << '\\\"' << x << '\\\"';}\nvoid __print(bool x) {cerr << (x ? \"true\" : \"false\");}\n \ntemplate\nvoid __print(const pair &x);\ntemplate\nvoid __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? \", \" : \"\"), __print(i); cerr << \"}\";}\ntemplate\nvoid __print(const pair &x) {cerr << '{'; __print(x.first); cerr << \", \"; __print(x.second); cerr << '}';}\nvoid _print() {cerr << \"]\\n\";}\ntemplate \nvoid _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << \", \"; _print(v...);}\n#ifdef DEBUG\n#define dbg(x...) cerr << \"\\e[91m\"<<__func__<<\":\"<<__LINE__<<\" [\" << #x << \"] = [\"; _print(x); cerr << \"\\e[39m\" << endl;\n#else\n#define dbg(x...)\n#endif\n#include\n#include\nusing namespace __gnu_pbds;\n\ntemplate using o_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\ntemplate using o_map = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n\/\/ find_by_order(0) -> k th element\n\/\/ order_of_key(k) -> number of elements less than k\n\n\/\/ clang-format on\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n int n, m;\n cin >> n >> m;\n std::vector a;\n std::vector pos(n, -1);\n o_set s;\n std::vector lans(n), rans(n);\n for (int i = 0; i < n; i++) {\n pos[i] = 0;\n rans[i] = i;\n lans[i] = i;\n }\n for (int i = n - 1; i >= 0; i--) {\n s.order_of_key(n - i - 1);\n pos[i] = n - i - 1;\n s.insert(n - i - 1);\n }\n for (int i = 0; i < m; i++) {\n int k;\n cin >> k;\n k--, a.pb(k);\n }\n for (int i = 0; i < m; i++) {\n chmin(lans[a[i]], 0);\n int z = n - s.order_of_key(pos[a[i]]) - 1;\n s.erase(pos[a[i]]);\n chmax(rans[a[i]], z);\n pos[a[i]] = n + i;\n s.insert(n + i);\n dbg(pos);\n }\n for (int i = 0; i < n; i++) {\n int z = n - s.order_of_key(pos[i]) - 1;\n chmax(rans[i], z);\n }\n for (int i = 0; i < n; i++) {\n cout << lans[i] + 1 << ' ' << rans[i] + 1 << '\\n';\n }\n return 0;\n}\n","language":"cpp"} -{"contest_id":"1307","problem_id":"F","statement":"F. Cow and Vacationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie is planning a vacation! In Cow-lifornia; there are nn cities, with n\u22121n\u22121 bidirectional roads connecting them. It is guaranteed that one can reach any city from any other city. Bessie is considering vv possible vacation plans, with the ii-th one consisting of a start city aiai and destination city bibi.It is known that only rr of the cities have rest stops. Bessie gets tired easily, and cannot travel across more than kk consecutive roads without resting. In fact, she is so desperate to rest that she may travel through the same city multiple times in order to do so.For each of the vacation plans, does there exist a way for Bessie to travel from the starting city to the destination city?InputThe first line contains three integers nn, kk, and rr (2\u2264n\u22642\u22c51052\u2264n\u22642\u22c5105, 1\u2264k,r\u2264n1\u2264k,r\u2264n) \u00a0\u2014 the number of cities, the maximum number of roads Bessie is willing to travel through in a row without resting, and the number of rest stops.Each of the following n\u22121n\u22121 lines contain two integers xixi and yiyi (1\u2264xi,yi\u2264n1\u2264xi,yi\u2264n, xi\u2260yixi\u2260yi), meaning city xixi and city yiyi are connected by a road. The next line contains rr integers separated by spaces \u00a0\u2014 the cities with rest stops. Each city will appear at most once.The next line contains vv (1\u2264v\u22642\u22c51051\u2264v\u22642\u22c5105) \u00a0\u2014 the number of vacation plans.Each of the following vv lines contain two integers aiai and bibi (1\u2264ai,bi\u2264n1\u2264ai,bi\u2264n, ai\u2260biai\u2260bi) \u00a0\u2014 the start and end city of the vacation plan. OutputIf Bessie can reach her destination without traveling across more than kk roads without resting for the ii-th vacation plan, print YES. Otherwise, print NO.ExamplesInputCopy6 2 1\n1 2\n2 3\n2 4\n4 5\n5 6\n2\n3\n1 3\n3 5\n3 6\nOutputCopyYES\nYES\nNO\nInputCopy8 3 3\n1 2\n2 3\n3 4\n4 5\n4 6\n6 7\n7 8\n2 5 8\n2\n7 1\n8 1\nOutputCopyYES\nNO\nNoteThe graph for the first example is shown below. The rest stop is denoted by red.For the first query; Bessie can visit these cities in order: 1,2,31,2,3.For the second query, Bessie can visit these cities in order: 3,2,4,53,2,4,5. For the third query, Bessie cannot travel to her destination. For example, if she attempts to travel this way: 3,2,4,5,63,2,4,5,6, she travels on more than 22 roads without resting. The graph for the second example is shown below. ","tags":["dfs and similar","dsu","trees"],"code":"\/\/ LUOGU_RID: 90825835\n#include\n\n#include\n\n#include\n\nconst int maxn=20;\n\nstruct dsu\n\n{\n\n\tint n,fa[400005];\n\n\tint find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}\n\n\tvoid pre(int n){this->n=n;for (int i=1;i<=n;i++) fa[i]=i;}\n\n\tvoid merge(int x,int y){\/*printf(\"Merge %d %d\\n\",x,y);*\/fa[find(x)]=find(y);}\n\n};\n\ndsu ss;\n\nint n,k,c,q,fa[400005][25],col[400005],dep[400005],d[400005],cnt;\n\nstd::vector a[400005],p;\n\nvoid add(int x,int y){a[x].push_back(y);a[y].push_back(x);}\n\nvoid dfs(int v,int x,int d)\n\n{\n\n\/\/\tprintf(\"dfs %d %d %d\\n\",v,x,d);\n\n\tdep[v]=d;\n\n\tfa[v][0]=x;\n\n\tfor (int i=1;i<=maxn;i++) fa[v][i]=fa[fa[v][i-1]][i-1];\n\n\tfor (int i=0;i<(int)a[v].size();i++)\n\n\t{\n\n\t\tint u=a[v][i];\n\n\/\/\t\tprintf(\"Edge %d %d\\n\",v,u);\n\n\t\tif (u==x) continue;\n\n\t\tdfs(u,v,d+1);\n\n\t}\n\n}\n\nvoid bfs(std::vector s)\n\n{\n\n\tstd::queue q;\n\n\tfor (int i=1;i<=n;i++) d[i]=0;\n\n\tfor (int i=0;i<(int)s.size();i++) col[s[i]]=i+1,q.push(s[i]);\n\n\twhile (!q.empty())\n\n\t{\n\n\t\tint v=q.front();q.pop();\n\n\t\tif (d[v]==k) continue;\n\n\/\/\t\tprintf(\"v = %d\\n\",v);\n\n\t\tfor (int i=0;i<(int)a[v].size();i++)\n\n\t\t{\n\n\t\t\tint u=a[v][i];\n\n\t\t\tif (col[u]) ss.merge(col[v],col[u]);\n\n\t\t\telse col[u]=col[v],d[u]=d[v]+1,q.push(u);\n\n\t\t}\n\n\t}\n\n}\n\nint getfa(int x,int k)\n\n{\n\n\tfor (int i=maxn;i>=0;i--) if (k&(1<=0;i--) if ((dep[x]-dep[y])&(1<=0;i--) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];\n\n\treturn fa[x][0];\n\n}\n\nint getdis(int x,int y){return dep[x]+dep[y]-2*dep[lca(x,y)];}\n\nvoid getstp(int &x,int y,int k)\n\n{\n\n\tint d=lca(x,y);\n\n\tif (dep[x]-dep[d]>=k) x=getfa(x,k);\n\n\telse x=getfa(y,getdis(x,y)-k);\n\n}\n\nint solve(int x,int y)\n\n{\n\n\tif (getdis(x,y)<=2*k||col[x]==col[y]&&col[x]&&col[y]) return 1;\n\n\/\/\tprintf(\"%d %d %d\\n\",x,y,getdis(x,y));\n\n\tgetstp(x,y,k),getstp(y,x,k);\n\n\/\/\tprintf(\"%d %d\\n\",x,y);\n\n\tint px=col[x],py=col[y];\n\n\treturn (px&&py&&px==py);\n\n}\n\nint main()\n\n{\n\n\tscanf(\"%d%d%d\",&n,&k,&c);cnt=n;\n\n\tfor (int i=1;i\n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\nusing namespace std;\n\n\n\n#define sz(v) ((int)(v).size())\n\n#define all(a) (a).begin(), (a).end()\n\n#define rall(a) a.rbegin(), a.rend()\n\n#define F first\n\n#define S second\n\n#define rep(i, n) for(int i = 0;i < n;i++)\n\n#define time clock() \/ (double) CLOCKS_PER_SEC\n\n\n\nusing pii = pair;\n\nusing ll = long long;\n\nusing ld = long double;\n\n\n\nconst ll infll = (ll)4e18 + 27;\n\nconst ll inf = (ll)1e9 + 27;\n\nconst ll mod = (ll)1e9 + 7;\n\n\n\n#define dbg(x) cout << #x << \" = \" << (x) << endl\n\n\n\ntemplate \n\nistream& operator>> (istream& in, pair& b) {\n\n in >> b.first >> b.second;\n\n return in;\n\n}\n\n\n\ntemplate \n\nostream& operator<< (ostream& out, const pair& b) {\n\n out << \"{\" << b.first << \", \" << b.second << \"}\";\n\n return out;\n\n}\n\n\n\ntemplate \n\nistream& operator>> (istream& in, vector& b) {\n\n for (auto& v : b)\n\n in >> v;\n\n return in;\n\n}\n\n\n\ntemplate \n\nostream& operator<< (ostream& out, vector& b) {\n\n for (auto v : b)\n\n out << v << ' ';\n\n return out;\n\n}\n\n\n\ntemplate \n\nvoid print(T x, string end = \"\\n\") {\n\n cout << x << end;\n\n}\n\n\n\ntemplate bool chkmin(T1& x, const T2& y) { return x > y && (x = y, true); }\n\ntemplate bool chkmax(T1& x, const T2& y) { return x < y && (x = y, true); }\n\n\n\nmt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());\n\n\n\nconst int N = 310;\n\nint a[N][N];\n\nconst int K = 2e6 + 10;\n\nvector del[K];\n\nvector add[K];\n\nint sub[K];\n\nstruct dsu {\n\n vector d, p;\n\n int n = 1;\n\n void init(int k){\n\n n = k;\n\n d.assign(n, 1);\n\n p.assign(n, 0);\n\n iota(all(p), 0ll);\n\n }\n\n int find(int u){\n\n if(u == p[u])return u;\n\n return p[u] = find(p[u]);\n\n }\n\n void unite(int u, int v){\n\n u = find(u);\n\n v = find(v);\n\n if(u == v)return;\n\n if(d[u] < d[v])swap(u, v);\n\n d[u] += d[v];\n\n p[v] = u;\n\n }\n\n};\n\ndsu d;\n\nint n, m;\n\nint dx[4] = {-1, 1, 0, 0};\n\nint dy[4] = {0, 0, 1, -1};\n\nvoid calculate(vector &event, int coefficient){\n\n for(auto [xy, index]: event){\n\n int current = 1;\n\n int x = xy \/ m, y = xy % m;\n\n a[x][y] = 1;\n\n for(int j = 0;j < 4;j++){\n\n int nx = x + dx[j], ny = y + dy[j];\n\n if(nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] == 1 && d.find(nx * m + ny) != d.find(x * m + y)){\n\n current--;\n\n d.unite(nx * m + ny, x * m + y);\n\n }\n\n }\n\n sub[index] += current * coefficient;\n\n }\n\n for(auto [xy, index]: event){\n\n d.d[xy] = 1;\n\n d.p[xy] = xy;\n\n a[xy \/ m][xy % m] = 0;\n\n }\n\n}\n\n\n\nvoid solve() {\n\n cin >> n >> m;\n\n d.init(K);\n\n int q;\n\n cin >> q;\n\n for(int i = 0;i < q;i++){\n\n int x, y, c;\n\n cin >> x >> y >> c;\n\n x--;y--;\n\n if(a[x][y] == c)continue;\n\n add[c].push_back({x * m + y, i});\n\n del[a[x][y]].push_back({x * m + y, i});\n\n a[x][y] = c;\n\n }\n\n for(int i = 0;i < n;i++)\n\n for(int j = 0;j < m;j++)\n\n del[a[i][j]].push_back({i * m + j, q});\n\n for(int i = 0;i < n;i++) for(int j = 0;j < m;j++) a[i][j] = 0;\n\n for(int i = 0;i < K;i++)\n\n reverse(all(del[i]));\n\n for(int i = 0;i < K;i++){\n\n calculate(add[i], 1);\n\n }\n\n for(int i = 0;i < K;i++){\n\n calculate(del[i], -1);\n\n }\n\n int ans = 1;\n\n for(int i = 0;i < q;i++){\n\n ans += sub[i];\n\n cout << ans << \"\\n\";\n\n }\n\n}\n\n\n\nsigned main() {\n\n ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << fixed << setprecision(15);\n\n int test = 1;\n\n \/\/cin >> test;\n\n while (test--)\n\n solve();\n\n#ifdef home\n\n cout << \"_________________________________\" << endl;\n\n cout << \"finished in \" << time << \" s\";\n\n#endif\n\n return 0;\n\n}\n\n\/*\n\n6 9 3\n\n1 9 1\n\n3 8 1\n\n6 9 1\n\n *\/","language":"cpp"} -{"contest_id":"1301","problem_id":"B","statement":"B. Motarack's Birthdaytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputDark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array aa of nn non-negative integers.Dark created that array 10001000 years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer kk (0\u2264k\u22641090\u2264k\u2264109) and replaces all missing elements in the array aa with kk.Let mm be the maximum absolute difference between all adjacent elements (i.e. the maximum value of |ai\u2212ai+1||ai\u2212ai+1| for all 1\u2264i\u2264n\u221211\u2264i\u2264n\u22121) in the array aa after Dark replaces all missing elements with kk.Dark should choose an integer kk so that mm is minimized. Can you help him?InputThe input consists of multiple test cases. The first line contains a single integer tt (1\u2264t\u22641041\u2264t\u2264104) \u00a0\u2014 the number of test cases. The description of the test cases follows.The first line of each test case contains one integer nn (2\u2264n\u22641052\u2264n\u2264105)\u00a0\u2014 the size of the array aa.The second line of each test case contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (\u22121\u2264ai\u2264109\u22121\u2264ai\u2264109). If ai=\u22121ai=\u22121, then the ii-th integer is missing. It is guaranteed that at least one integer is missing in every test case.It is guaranteed, that the sum of nn for all test cases does not exceed 4\u22c51054\u22c5105.OutputPrint the answers for each test case in the following format:You should print two integers, the minimum possible value of mm and an integer kk (0\u2264k\u22641090\u2264k\u2264109) that makes the maximum absolute difference between adjacent elements in the array aa equal to mm.Make sure that after replacing all the missing elements with kk, the maximum absolute difference between adjacent elements becomes mm.If there is more than one possible kk, you can print any of them.ExampleInputCopy7\n5\n-1 10 -1 12 -1\n5\n-1 40 35 -1 35\n6\n-1 -1 9 -1 3 -1\n2\n-1 -1\n2\n0 -1\n4\n1 -1 3 -1\n7\n1 -1 7 5 2 -1 5\nOutputCopy1 11\n5 35\n3 6\n0 42\n0 0\n1 2\n3 4\nNoteIn the first test case after replacing all missing elements with 1111 the array becomes [11,10,11,12,11][11,10,11,12,11]. The absolute difference between any adjacent elements is 11. It is impossible to choose a value of kk, such that the absolute difference between any adjacent element will be \u22640\u22640. So, the answer is 11.In the third test case after replacing all missing elements with 66 the array becomes [6,6,9,6,3,6][6,6,9,6,3,6]. |a1\u2212a2|=|6\u22126|=0|a1\u2212a2|=|6\u22126|=0; |a2\u2212a3|=|6\u22129|=3|a2\u2212a3|=|6\u22129|=3; |a3\u2212a4|=|9\u22126|=3|a3\u2212a4|=|9\u22126|=3; |a4\u2212a5|=|6\u22123|=3|a4\u2212a5|=|6\u22123|=3; |a5\u2212a6|=|3\u22126|=3|a5\u2212a6|=|3\u22126|=3. So, the maximum difference between any adjacent elements is 33.","tags":["binary search","greedy","ternary search"],"code":"\/\/ #pragma GCC optimize \"fast-math\"\n\/\/ #pragma GCC optimize(\"O3,unroll-loops\")\n\/\/ #pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n#pragma GCC diagnostic ignored \"-Wunused-variable\"\n\n#include \"bits\/stdc++.h\"\n#include \"ext\/pb_ds\/assoc_container.hpp\"\n\nusing namespace std;\nusing namespace __gnu_pbds;\n\n#define endl \"\\n\"\n#define int long long\n\/\/ #define ONLINE_JUDGE\n\n#define MULTI \\\n int _T; \\\n cin >> _T; \\\n while (_T--)\n\ntypedef tree, rb_tree_tag,\n tree_order_statistics_node_update>\n ordered_set;\n\ntypedef tree, null_type,\n less>, rb_tree_tag,\n tree_order_statistics_node_update>\n ordered_set_pair;\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-----\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\nconst int INF = 1e18; \/\/ infinity\nconst int mod = 1e9 + 7; \/\/ mod\nconst int base1 = 972663749; \/\/ base1\nconst int base2 = 998244353; \/\/ base2\nconst int mod1 = 1000000007; \/\/ mod1\nconst int mod2 = 1000000009; \/\/ mod2\n\nconst double pi = 4 * atan(1);\n\nvector dx = {-1, +1, +0, +0, +1, -1, +1, -1};\nvector dy = {+0, +0, +1, -1, +1, -1, -1, +1};\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/----\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\nint modpower(int x, int n, int m)\n{\n if (n == 0)\n return 1 % m;\n int u = modpower(x, n \/ 2, m);\n u = (u * u) % m;\n if (n % 2 == 1)\n u = (u * x) % m;\n return u;\n}\n\nint modinverse(int i, int MOD)\n{\n if (i == 1)\n return 1;\n return (MOD - ((MOD \/ i) * modinverse(MOD % i, MOD)) % MOD + MOD) % MOD;\n}\n\nint lcm(int x1, int x2)\n{\n return ((x1 * x2) \/ __gcd(x1, x2));\n}\n\nint bitCount(unsigned int u)\n{\n unsigned int uCount;\n uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111);\n return ((uCount + (uCount >> 3)) & 030707070707) % 63;\n}\n\nvoid printVector(vector &array)\n{\n int sz = array.size();\n if (sz == 0)\n return;\n for (int i = 0; i < sz - 1; i++)\n {\n cout << array[i] << \" \";\n }\n cout << array[sz - 1] << endl;\n}\n\nvoid printArray(int array[], int sz)\n{\n for (int i = 0; i < sz - 1; i++)\n {\n cout << array[i] << \" \";\n }\n cout << array[sz - 1] << endl;\n}\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/----main-function----\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/====================================================================================================\n\/\/====================================================================================================\n\nint n, k, mx = INTMAX_MIN, mn = INTMAX_MAX, a[100005], b[100005];\n\nint check(int curr)\n{\n for (int i = 1; i <= n; i++)\n {\n if (a[i] == -1)\n {\n b[i] = curr;\n }\n else\n {\n b[i] = a[i];\n }\n }\n\n int mxdiffernce = 0;\n\n for (int i = 1; i <= n - 1; i++)\n {\n\n mxdiffernce = max(mxdiffernce, abs(b[i] - b[i + 1]));\n }\n\n return mxdiffernce;\n}\n\nvoid solve_the_probelm(int test_case)\n{\n\n cin >> n;\n for (int i = 1; i <= n; i++)\n {\n\n cin >> a[i];\n if (a[i] == -1)\n continue;\n mx = max(mx, a[i]);\n mn = min(mn, a[i]);\n }\n\n int high = mx, low = mn, mid, ans = 0;\n if (low == INT_MAX && high == INT_MIN)\n {\n cout << \"0 0\" << endl;\n return;\n }\n while (low <= high)\n {\n mid = low + (high - low) \/ 2;\n if (check(mid - 1) >= check(mid) && check(mid) <= check(mid + 1))\n {\n ans = mid;\n break;\n }\n else if (check(mid - 1) > check(mid) && check(mid) > check(mid + 1))\n low = mid + 1;\n else if (check(mid - 1) < check(mid) && check(mid) < check(mid + 1))\n high = mid - 1;\n }\n\n cout << check(ans) << \" \" << ans << endl;\n}\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/------------------------\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/====================================================================================================\n\/\/====================================================================================================\nsigned main()\n{\n int test_cases = 1;\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n#ifndef ONLINE_JUDGE\n freopen(\"input.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n#endif\n\n cin >> test_cases; \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/_test_cause___\n\n for (int test_case = 1; test_case <= test_cases; test_case++)\n solve_the_probelm(test_case);\n\n#ifndef ONLINE_JUDGE\n cout << \"\\nExecution Time : \" << 1.0 * clock() \/ CLOCKS_PER_SEC << \"s \";\n#endif\n\n return 0;\n}\n","language":"cpp"} -{"contest_id":"1141","problem_id":"F1","statement":"F1. Same Sum Blocks (Easy)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is given in two editions; which differ exclusively in the constraints on the number nn.You are given an array of integers a[1],a[2],\u2026,a[n].a[1],a[2],\u2026,a[n]. A block is a sequence of contiguous (consecutive) elements a[l],a[l+1],\u2026,a[r]a[l],a[l+1],\u2026,a[r] (1\u2264l\u2264r\u2264n1\u2264l\u2264r\u2264n). Thus, a block is defined by a pair of indices (l,r)(l,r).Find a set of blocks (l1,r1),(l2,r2),\u2026,(lk,rk)(l1,r1),(l2,r2),\u2026,(lk,rk) such that: They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (li,ri)(li,ri) and (lj,rj(lj,rj) where i\u2260ji\u2260j either rikk\u2032>k. The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks.InputThe first line contains integer nn (1\u2264n\u2264501\u2264n\u226450) \u2014 the length of the given array. The second line contains the sequence of elements a[1],a[2],\u2026,a[n]a[1],a[2],\u2026,a[n] (\u2212105\u2264ai\u2264105\u2212105\u2264ai\u2264105).OutputIn the first line print the integer kk (1\u2264k\u2264n1\u2264k\u2264n). The following kk lines should contain blocks, one per line. In each line print a pair of indices li,rili,ri (1\u2264li\u2264ri\u2264n1\u2264li\u2264ri\u2264n) \u2014 the bounds of the ii-th block. You can print blocks in any order. If there are multiple answers, print any of them.ExamplesInputCopy7\n4 1 2 2 1 5 3\nOutputCopy3\n7 7\n2 3\n4 5\nInputCopy11\n-5 -4 -3 -2 -1 0 1 2 3 4 5\nOutputCopy2\n3 4\n1 1\nInputCopy4\n1 1 1 1\nOutputCopy4\n4 4\n1 1\n2 2\n3 3\n","tags":["greedy"],"code":"#include\n\n#include \n\n#include \n\nusing namespace std;\n\n\/\/using L = __int128;\n\n#include\n\n#include \n\nusing namespace __gnu_pbds;\n\nusing ll = long long;\n\nusing ull = unsigned long long;\n\nusing ld = long double;\n\n#define nd \"\\n\"\n\n#define all(x) (x).begin(), (x).end()\n\n#define lol cout <<\"i am here\"< void print(container v) { for (auto& it : v) cout << it << ' ' ;cout <\n\nll fp(ll a , ll p){ if(!p) return 1; ll v = fp(a , p\/2); v*=v;return p & 1 ? v*a : v; }\n\n\n\ntemplate using ordered_set = tree, rb_tree_tag,tree_order_statistics_node_update>;\n\n\n\nll mul (ll a, ll b){\n\n return ( ( a % mod ) * ( b % mod ) ) % mod;\n\n}\n\nll add (ll a , ll b) {\n\n return (a + b + mod) % mod;\n\n}\n\n\n\ntemplate< typename T > using min_heap = priority_queue , greater < T > > ;\n\n\n\nvoid hi(int tc) {\n\n map > > mp;\n\n ll n; cin >> n;\n\n vector a(n);\n\n for (ll &i : a) cin >> i;\n\n for (int i = 0; i < n; ++i) {\n\n ll c = 0;\n\n for (int j = i; j < n; ++j){\n\n c+=a[j];\n\n mp[c].emplace(i , j);\n\n }\n\n }\n\n vector > f;\n\n for (auto &i : mp){\/\/ sum , vec\n\n vector > ans;\n\n auto &st= i.second;\n\n auto it = st.end(); it = prev(it); ans.emplace_back(*it);\n\n it = prev(it);\n\n while (it != st.begin()){\n\n ll L = next(it)->first;\n\n if (it->second >= L) it = st.erase(it);\n\n else ans.emplace_back(*it);\n\n it = prev(it);\n\n }\n\n if (it->second < ans.back().first) ans.emplace_back(*it);\n\n if ((int)ans.size() > (int)f.size())\n\n f.swap(ans);\n\n }\n\n cout << (int) f.size() <> tt;\n\n while(tt--) hi(++tc);\n\n return 0;\n\n}\n\n\n\n","language":"cpp"} -{"contest_id":"1285","problem_id":"C","statement":"C. Fadi and LCMtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; Osama gave Fadi an integer XX, and Fadi was wondering about the minimum possible value of max(a,b)max(a,b) such that LCM(a,b)LCM(a,b) equals XX. Both aa and bb should be positive integers.LCM(a,b)LCM(a,b) is the smallest positive integer that is divisible by both aa and bb. For example, LCM(6,8)=24LCM(6,8)=24, LCM(4,12)=12LCM(4,12)=12, LCM(2,3)=6LCM(2,3)=6.Of course, Fadi immediately knew the answer. Can you be just like Fadi and find any such pair?InputThe first and only line contains an integer XX (1\u2264X\u226410121\u2264X\u22641012).OutputPrint two positive integers, aa and bb, such that the value of max(a,b)max(a,b) is minimum possible and LCM(a,b)LCM(a,b) equals XX. If there are several possible such pairs, you can print any.ExamplesInputCopy2\nOutputCopy1 2\nInputCopy6\nOutputCopy2 3\nInputCopy4\nOutputCopy1 4\nInputCopy1\nOutputCopy1 1\n","tags":["brute force","math","number theory"],"code":"#include \n\nusing namespace std;\n\n#define ll long long int\n\n#define mod 1000000007\n\n\n\nll binpow(ll a, ll n)\n\n{\n\n ll res = 1;\n\n while (n)\n\n {\n\n if (n % 2 == 1)\n\n {\n\n n--;\n\n res = res * a;\n\n }\n\n else\n\n {\n\n n \/= 2;\n\n a = a * a;\n\n }\n\n }\n\n return res;\n\n}\n\nvoid solve()\n\n{\n\n ll n;\n\n cin >> n;\n\n ll temp = n;\n\n map m;\n\n for (ll i = 2; i * i <= n; i++)\n\n {\n\n while (n % i == 0)\n\n {\n\n m[i]++;\n\n n \/= i;\n\n }\n\n }\n\n if (n >= 2)\n\n m[n]++;\n\n n = temp;\n\n ll mn = n;\n\n for (ll i = 2; i * i <= n; i++)\n\n {\n\n ll res = 1;\n\n if (n % i == 0)\n\n {\n\n for (auto it : m)\n\n {\n\n ll x = binpow(it.first, it.second);\n\n if (i % x == 0)\n\n {\n\n res = res * x;\n\n }\n\n }\n\n mn = min(mn, max(res, n \/ res));\n\n }\n\n }\n\n cout << mn << \" \" << n \/ mn << endl;\n\n}\n\nint main()\n\n{\n\n ios_base::sync_with_stdio(false);\n\n solve();\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1324","problem_id":"A","statement":"A. Yet Another Tetris Problemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given some Tetris field consisting of nn columns. The initial height of the ii-th column of the field is aiai blocks. On top of these columns you can place only figures of size 2\u00d712\u00d71 (i.e. the height of this figure is 22 blocks and the width of this figure is 11 block). Note that you cannot rotate these figures.Your task is to say if you can clear the whole field by placing such figures.More formally, the problem can be described like this:The following process occurs while at least one aiai is greater than 00: You place one figure 2\u00d712\u00d71 (choose some ii from 11 to nn and replace aiai with ai+2ai+2); then, while all aiai are greater than zero, replace each aiai with ai\u22121ai\u22121. And your task is to determine if it is possible to clear the whole field (i.e. finish the described process), choosing the places for new figures properly.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1\u2264t\u22641001\u2264t\u2264100) \u2014 the number of test cases.The next 2t2t lines describe test cases. The first line of the test case contains one integer nn (1\u2264n\u22641001\u2264n\u2264100) \u2014 the number of columns in the Tetris field. The second line of the test case contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641001\u2264ai\u2264100), where aiai is the initial height of the ii-th column of the Tetris field.OutputFor each test case, print the answer \u2014 \"YES\" (without quotes) if you can clear the whole Tetris field and \"NO\" otherwise.ExampleInputCopy4\n3\n1 1 3\n4\n1 1 2 1\n2\n11 11\n1\n100\nOutputCopyYES\nNO\nYES\nYES\nNoteThe first test case of the example field is shown below:Gray lines are bounds of the Tetris field. Note that the field has no upper bound.One of the correct answers is to first place the figure in the first column. Then after the second step of the process; the field becomes [2,0,2][2,0,2]. Then place the figure in the second column and after the second step of the process, the field becomes [0,0,0][0,0,0].And the second test case of the example field is shown below:It can be shown that you cannot do anything to end the process.In the third test case of the example, you first place the figure in the second column after the second step of the process, the field becomes [0,2][0,2]. Then place the figure in the first column and after the second step of the process, the field becomes [0,0][0,0].In the fourth test case of the example, place the figure in the first column, then the field becomes [102][102] after the first step of the process, and then the field becomes [0][0] after the second step of the process.","tags":["implementation","number theory"],"code":"\/* \u0628\u0633\u0645 \u0627\u0644\u0644\u0647 \u0627\u0644\u0631\u062d\u0645\u0646 \u0627\u0644\u0631\u062d\u064a\u0645 *\/\n#include \nusing namespace std;\n#define Ma3rof ios_base::sync_with_stdio(false);cin.tie(NULL);\nvoid solve() {\n int n;\n cin >> n;\n bool odd = false, even = false;\n while (n--) {\n int x;\n cin >> x;\n if(x&1)\n odd = true;\n else\n even = true;\n }\n if (even && odd)\n cout << \"NO\\n\";\n else\n cout << \"YES\\n\";\n}\nsigned main() {\n Ma3rof\n int t = 1;\n cin >> t;\n while (t--) {\n \/* \u0648\u0645\u0627 \u062a\u062f\u0631\u064a \u0646\u0641\u0633 \u0645\u0627\u0630\u0627 \u062a\u06a9\u0633\u0628 \u063a\u062f\u0627 \u0648\u0645\u0627 \u062a\u062f\u0631\u064a \u0646\u0641\u0633 \u0628\u0627\u064a \u0627\u0631\u0636 \u062a\u0645\u0648\u062a *\/\n solve();\n }\n return 0;\n}\n","language":"cpp"} -{"contest_id":"1284","problem_id":"A","statement":"A. New Year and Namingtime limit per test1 secondmemory limit per test1024 megabytesinputstandard inputoutputstandard outputHappy new year! The year 2020 is also known as Year Gyeongja (\uacbd\uc790\ub144; gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.There are two sequences of nn strings s1,s2,s3,\u2026,sns1,s2,s3,\u2026,sn and mm strings t1,t2,t3,\u2026,tmt1,t2,t3,\u2026,tm. These strings contain only lowercase letters. There might be duplicates among these strings.Let's call a concatenation of strings xx and yy as the string that is obtained by writing down strings xx and yy one right after another without changing the order. For example, the concatenation of the strings \"code\" and \"forces\" is the string \"codeforces\".The year 1 has a name which is the concatenation of the two strings s1s1 and t1t1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.For example, if n=3,m=4,s=n=3,m=4,s={\"a\", \"b\", \"c\"}, t=t= {\"d\", \"e\", \"f\", \"g\"}, the following table denotes the resulting year names. Note that the names of the years may repeat. You are given two sequences of strings of size nn and mm and also qq queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?InputThe first line contains two integers n,mn,m (1\u2264n,m\u2264201\u2264n,m\u226420).The next line contains nn strings s1,s2,\u2026,sns1,s2,\u2026,sn. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 11 and at most 1010.The next line contains mm strings t1,t2,\u2026,tmt1,t2,\u2026,tm. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 11 and at most 1010.Among the given n+mn+m strings may be duplicates (that is, they are not necessarily all different).The next line contains a single integer qq (1\u2264q\u226420201\u2264q\u22642020).In the next qq lines, an integer yy (1\u2264y\u22641091\u2264y\u2264109) is given, denoting the year we want to know the name for.OutputPrint qq lines. For each line, print the name of the year as per the rule described above.ExampleInputCopy10 12\nsin im gye gap eul byeong jeong mu gi gyeong\nyu sul hae ja chuk in myo jin sa o mi sin\n14\n1\n2\n3\n4\n10\n11\n12\n13\n73\n2016\n2017\n2018\n2019\n2020\nOutputCopysinyu\nimsul\ngyehae\ngapja\ngyeongo\nsinmi\nimsin\ngyeyu\ngyeyu\nbyeongsin\njeongyu\nmusul\ngihae\ngyeongja\nNoteThe first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal.","tags":["implementation","strings"],"code":"#include\n\nusing namespace std;\n\n\n\ntypedef long long int ll ;\n\ntypedef long double ld;\n\ntypedef vector vll;\n\ntypedef vector vs;\n\ntypedef vector > vpi;\n\ntypedef set s;\n\ntypedef map mll;\n\ntypedef pair pint;\n\ndouble pi = 3.14159265359;\n\n#define FOR(n,v) for(ll i=1;i<=n;i++) cin>>v[i];\n\n#define REP(i,n) for(int i=0;i() );\n\n#define py printf(\"YES\\n\");\n\n#define pn printf(\"NO\\n\");\n\n#define pmone printf(\"-1\\n\");\n\n\n\nvoid solve()\n\n{\n\n int n,m;\n\n cin>>n>>m;\n\n vector v(n+3),v1(m+3);\n\n\n\n FOR(n,v)\n\n FOR(m,v1)\n\n\n\n int q;\n\n cin>>q;\n\n for(int i = 0 ; i < q ; i++)\n\n {\n\n\n\n\n\n int a;\n\n cin>>a;\n\n int x,y;\n\n x = a%n;\n\n y = (a)%m;\n\n if(x == 0) x = n;\n\n if(y == 0) y = m;\n\n \/\/cout<<\"case : \"<>t;\n\n \/\/while(t--)\n\n solve();\n\n\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1322","problem_id":"C","statement":"C. Instant Noodlestime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWu got hungry after an intense training session; and came to a nearby store to buy his favourite instant noodles. After Wu paid for his purchase, the cashier gave him an interesting task.You are given a bipartite graph with positive integers in all vertices of the right half. For a subset SS of vertices of the left half we define N(S)N(S) as the set of all vertices of the right half adjacent to at least one vertex in SS, and f(S)f(S) as the sum of all numbers in vertices of N(S)N(S). Find the greatest common divisor of f(S)f(S) for all possible non-empty subsets SS (assume that GCD of empty set is 00).Wu is too tired after his training to solve this problem. Help him!InputThe first line contains a single integer tt (1\u2264t\u22645000001\u2264t\u2264500000)\u00a0\u2014 the number of test cases in the given test set. Test case descriptions follow.The first line of each case description contains two integers nn and mm (1\u00a0\u2264\u00a0n,\u00a0m\u00a0\u2264\u00a05000001\u00a0\u2264\u00a0n,\u00a0m\u00a0\u2264\u00a0500000)\u00a0\u2014 the number of vertices in either half of the graph, and the number of edges respectively.The second line contains nn integers cici (1\u2264ci\u226410121\u2264ci\u22641012). The ii-th number describes the integer in the vertex ii of the right half of the graph.Each of the following mm lines contains a pair of integers uiui and vivi (1\u2264ui,vi\u2264n1\u2264ui,vi\u2264n), describing an edge between the vertex uiui of the left half and the vertex vivi of the right half. It is guaranteed that the graph does not contain multiple edges.Test case descriptions are separated with empty lines. The total value of nn across all test cases does not exceed 500000500000, and the total value of mm across all test cases does not exceed 500000500000 as well.OutputFor each test case print a single integer\u00a0\u2014 the required greatest common divisor.ExampleInputCopy3\n2 4\n1 1\n1 1\n1 2\n2 1\n2 2\n\n3 4\n1 1 1\n1 1\n1 2\n2 2\n2 3\n\n4 7\n36 31 96 29\n1 2\n1 3\n1 4\n2 2\n2 4\n3 1\n4 3\nOutputCopy2\n1\n12\nNoteThe greatest common divisor of a set of integers is the largest integer gg such that all elements of the set are divisible by gg.In the first sample case vertices of the left half and vertices of the right half are pairwise connected, and f(S)f(S) for any non-empty subset is 22, thus the greatest common divisor of these values if also equal to 22.In the second sample case the subset {1}{1} in the left half is connected to vertices {1,2}{1,2} of the right half, with the sum of numbers equal to 22, and the subset {1,2}{1,2} in the left half is connected to vertices {1,2,3}{1,2,3} of the right half, with the sum of numbers equal to 33. Thus, f({1})=2f({1})=2, f({1,2})=3f({1,2})=3, which means that the greatest common divisor of all values of f(S)f(S) is 11.","tags":["graphs","hashing","math","number theory"],"code":"\/\/ LUOGU_RID: 102636546\n#include \n\n#define int long long\n\n#define mem(a,b) memset(a,b,sizeof(a))\n\n#define fre(z) freopen(z\".in\",\"r\",stdin),freopen(z\".out\",\"w\",stdout)\n\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef unsigned long long ull;\n\ntypedef pair Pair;\n\nconst double eps=1e-8;\n\nconst int inf=2139062143;\n\n#ifdef ONLINE_JUDGE\n\nstatic char buf[1000000],*p1=buf,*p2=buf,obuf[1000000],*p3=obuf;\n\n#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++\n\n#endif\n\ninline void qread(){}template\n\ninline void qread(T1 &a,T2&...b){\n\n register T1 x=0;register bool f=false;char ch=getchar();\n\n while(ch<'0') f|=(ch=='-'),ch=getchar();\n\n while(ch>='0') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();\n\n x=(f?-x:x);a=x;qread(b...);\n\n}inline void dread(){}template\n\ninline void dread(T1 &a,T2&...b){\n\n register double w=0;register ll x=0,base=1;\n\n register bool f=false;char ch=getchar();\n\n while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();\n\n while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();\n\n w=(f?-x:x);if(ch!='.') return a=w,dread(b...);x=0,ch=getchar();\n\n while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),base*=10,ch=getchar();\n\n register double tmp=(double)(x\/(double)base);w=w+(double)(f?-tmp:tmp);a=w;dread(b...);\n\n}templatevoid cmax(T &x,T y){if(xvoid cmin(T &x,T y){if(x>y) x=y;}\n\ntemplate T qmax(T x,T y){return x>y?x:y;}\n\ntemplate T qmax(T x,T y,Arg ...arg){return qmax(x>y?x:y,arg...);}\n\ntemplate T qmin(T x,T y){return x T qmin(T x,T y,Arg ...arg){return qmin(x T randint(T l,T r){static mt19937 eng(time(0));uniform_int_distributiondis(l,r);return dis(eng);}\n\nconst int MAXN=5e5+7;\n\nint T,n,a[MAXN],m;sets[MAXN];map,int>mp;\n\nsigned main(){\n\n qread(T);int i,j;\n\n while(T--){\n\n qread(n,m);for(i=1;i<=n;i++) qread(a[i]),s[i].clear();mp.clear();\n\n for(i=1;i<=m;i++){\n\n int u,v;qread(u,v);\n\n s[v].emplace(u);\n\n }for(i=1;i<=n;i++) if(!s[i].empty()) mp[s[i]]+=a[i];int ans=0;\n\n for(auto i:mp){\n\n if(!ans) ans=i.second;else ans=__gcd(ans,i.second);\n\n }printf(\"%lld\\n\",ans);\n\n }\n\n #ifndef ONLINE_JUDGE\n\n cerr<<\"Time: \"< null\");\n\n #endif\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1312","problem_id":"A","statement":"A. Two Regular Polygonstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and mm (m\n\nusing namespace std;\n\n#define sahadat() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\n#define ll long long\n\nvoid solve()\n\n{\n\n ll n,m;cin>>n>>m;\n\n if(n%m==0) cout<<\"YES\\n\";\n\n else cout<<\"NO\\n\";\n\n}\n\nint main()\n\n{\n\n sahadat();\n\n \n\n ll t;cin>>t;\n\n while(t--){\n\n solve();\n\n }\n\n \n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1311","problem_id":"D","statement":"D. Three Integerstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three integers a\u2264b\u2264ca\u2264b\u2264c.In one move, you can add +1+1 or \u22121\u22121 to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero) number of times, you can even perform this operation several times with one number. Note that you cannot make non-positive numbers using such operations.You have to perform the minimum number of such operations in order to obtain three integers A\u2264B\u2264CA\u2264B\u2264C such that BB is divisible by AA and CC is divisible by BB.You have to answer tt independent test cases. InputThe first line of the input contains one integer tt (1\u2264t\u22641001\u2264t\u2264100) \u2014 the number of test cases.The next tt lines describe test cases. Each test case is given on a separate line as three space-separated integers a,ba,b and cc (1\u2264a\u2264b\u2264c\u22641041\u2264a\u2264b\u2264c\u2264104).OutputFor each test case, print the answer. In the first line print resres \u2014 the minimum number of operations you have to perform to obtain three integers A\u2264B\u2264CA\u2264B\u2264C such that BB is divisible by AA and CC is divisible by BB. On the second line print any suitable triple A,BA,B and CC.ExampleInputCopy8\n1 2 3\n123 321 456\n5 10 15\n15 18 21\n100 100 101\n1 22 29\n3 19 38\n6 30 46\nOutputCopy1\n1 1 3\n102\n114 228 456\n4\n4 8 16\n6\n18 18 18\n1\n100 100 100\n7\n1 22 22\n2\n1 19 38\n8\n6 24 48\n","tags":["brute force","math"],"code":"#pragma GCC optimize(\"Ofast\")\n\n#pragma GCC optimize (\"unroll-loops\")\n\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx\")\n\n#include \n\n\n\nusing namespace std;\n\n\n\nvoid solve() {\n\n int N = 1e5 + 2;\n\n vector> v(N);\n\n for (int i = 1; i < N; i++) {\n\n for (int j = 1; j * j <= i; j++) {\n\n if (i % j == 0) {\n\n v[i].push_back(j);\n\n v[i].push_back(i \/ j);\n\n }\n\n }\n\n }\n\n cout << '\\n';\n\n int q;\n\n cin >> q;\n\n while (q--) {\n\n int a, b, c;\n\n cin >> a >> b >> c;\n\n int ans = 1e9;\n\n for (int i = 1; i <= 1e5; i++) {\n\n int d = abs(b - i);\n\n if (i > c) {\n\n d += i - c;\n\n } else {\n\n d += min(c % i, i - c % i);\n\n }\n\n if (i < a) {\n\n d += a - i;\n\n } else {\n\n int mn = 1e9;\n\n for (int j : v[i]) {\n\n mn = min(mn, abs(a - j));\n\n }\n\n d += mn;\n\n }\n\n ans = min(ans, d);\n\n }\n\n for (int i = 1; i <= 1e5; i++) {\n\n int d = abs(b - i);\n\n if (i > c) {\n\n d += i - c;\n\n } else {\n\n d += min(c % i, i - c % i);\n\n }\n\n if (i < a) {\n\n d += a - i;\n\n } else {\n\n int mn = 1e9;\n\n for (int j : v[i]) {\n\n mn = min(mn, abs(a - j));\n\n }\n\n d += mn;\n\n }\n\n if (ans == d) {\n\n cout << d << '\\n';\n\n if (i < a) {\n\n a = i;\n\n } else {\n\n int x = 1e9;\n\n for (int j : v[i]) {\n\n if (abs(a - x) > abs(a - j)) {\n\n x = j;\n\n }\n\n }\n\n a = x;\n\n }\n\n cout << a << \" \" << i << \" \";\n\n if (i > c) {\n\n c = i;\n\n } else {\n\n if (c % i < i - c % i) {\n\n c -= c % i;\n\n } else {\n\n c += i - c % i;\n\n }\n\n }\n\n cout << c;\n\n break;\n\n }\n\n }\n\n cout << '\\n';\n\n }\n\n\n\n}\n\n\n\nint main() {\n\n ios_base::sync_with_stdio( false ); cin.tie( nullptr ); cout.tie( nullptr );\n\n int q = 1;\n\n \/\/ cin >> q;\n\n while (q--) {\n\n solve();\n\n cout << '\\n';\n\n }\n\n}","language":"cpp"} -{"contest_id":"1294","problem_id":"F","statement":"F. Three Paths on a Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an unweighted tree with nn vertices. Recall that a tree is a connected undirected graph without cycles.Your task is to choose three distinct vertices a,b,ca,b,c on this tree such that the number of edges which belong to at least one of the simple paths between aa and bb, bb and cc, or aa and cc is the maximum possible. See the notes section for a better understanding.The simple path is the path that visits each vertex at most once.InputThe first line contains one integer number nn (3\u2264n\u22642\u22c51053\u2264n\u22642\u22c5105) \u2014 the number of vertices in the tree. Next n\u22121n\u22121 lines describe the edges of the tree in form ai,biai,bi (1\u2264ai1\u2264ai, bi\u2264nbi\u2264n, ai\u2260biai\u2260bi). It is guaranteed that given graph is a tree.OutputIn the first line print one integer resres \u2014 the maximum number of edges which belong to at least one of the simple paths between aa and bb, bb and cc, or aa and cc.In the second line print three integers a,b,ca,b,c such that 1\u2264a,b,c\u2264n1\u2264a,b,c\u2264n and a\u2260,b\u2260c,a\u2260ca\u2260,b\u2260c,a\u2260c.If there are several answers, you can print any.ExampleInputCopy8\n1 2\n2 3\n3 4\n4 5\n4 6\n3 7\n3 8\nOutputCopy5\n1 8 6\nNoteThe picture corresponding to the first example (and another one correct answer):If you choose vertices 1,5,61,5,6 then the path between 11 and 55 consists of edges (1,2),(2,3),(3,4),(4,5)(1,2),(2,3),(3,4),(4,5), the path between 11 and 66 consists of edges (1,2),(2,3),(3,4),(4,6)(1,2),(2,3),(3,4),(4,6) and the path between 55 and 66 consists of edges (4,5),(4,6)(4,5),(4,6). The union of these paths is (1,2),(2,3),(3,4),(4,5),(4,6)(1,2),(2,3),(3,4),(4,5),(4,6) so the answer is 55. It can be shown that there is no better answer.","tags":["dfs and similar","dp","greedy","trees"],"code":"\/\/ LUOGU_RID: 101696200\n\/\/ LUOGU_RID: 101682976\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#define fs(i,x,y) for(int i=(x),_=(y);i<=_;++i)\n\n#define fn(i,x,y) for(int i=(x),_=(y);i>=_;--i)\n\n#define tor(i,v,x) for(int i=head[x],v=to[i];i;i=nxt[i],v=to[i])\n\n#define ls(x) ch[x][0]\n\n#define rs(x) ch[x][1]\n\n#define mpi(x,y) make_pair(x,y)\n\n#define pi pair\n\n#define fi first\n\n#define se second\n\n\/\/#define int ll\n\n#define DEBUG printf(\"%s %d\\n\",__FUNCTION__,__LINE__)\n\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef double db;\n\ntypedef long double ldb;\n\ntemplate\n\nvoid read(T &x){\n\n\tx=0;char ch=getchar();bool f=0;\n\n\twhile(ch<'0'||ch>'9'){\n\n\t\tif(ch=='-')f=1;\n\n\t\tch=getchar();\n\n\t}\n\n\twhile(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();\n\n\tx=f?-x:x;\n\n}\n\ntemplate\n\nvoid read(T &x,Args &...args){\n\n\tread(x);read(args...);\n\n}\n\n\n\nconst int N=1e6+7;\n\nint n,m,ans;\n\nint dep[N];\n\nvectormx[N];\n\nvectorG[N];\n\nvectorwt(3,0);\n\n\n\nvoid upd(int x,int y,int z){\n\n\twt[0]=x,wt[1]=y,wt[2]=z;\n\n}\n\n\n\nvoid df(int u,int fa){\n\n\tdep[u]=dep[fa]+1;\n\n\tmx[u].push_back(mpi(0,u));\n\n\tmx[u].push_back(mpi(0,u));\n\n\tfor(int v:G[u]){\n\n\t\tif(v^fa){\n\n\t\t\tdf(v,u);\n\n\t\t\tmx[u].push_back(mpi(mx[v][0].fi+1,mx[v][0].se));\n\n\t\t}\n\n\t}\n\n\tsort(mx[u].begin(),mx[u].end(),[&](pi &lhs,pi &rhs){\n\n\t\treturn lhs.fi>rhs.fi;\n\n\t});\n\n}\n\n\n\nvoid dfs(int u,int fa){\n\n\tsort(mx[u].begin(),mx[u].end(),[&](pi &lhs,pi &rhs){\n\n\t\treturn lhs.fi>rhs.fi;\n\n\t});\n\n\tif(mx[u][0].fi+mx[u][1].fi+mx[u][2].fi>ans){\n\n\t\tans=mx[u][0].fi+mx[u][1].fi+mx[u][2].fi;\n\n\t\tupd(mx[u][0].se,mx[u][1].se,mx[u][2].se);\n\n\t}\n\n\/\/\tcout<<\"\/\/\/\/u:\"<\n#define int __int128\n#define N 600005\nusing namespace std;\nint read(){\n\tint w=0,h=1;char ch=getchar();\n\twhile(ch<'0'||ch>'9'){if(ch=='-')h=-h;ch=getchar();}\n\twhile(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}\n\treturn w*h;\n}\nvoid write(int x){if(x>9)write(x\/10);putchar(x%10+'0');}\nint n,w[N],nxt[N],fail[N],stk[N],top;\nmaps;vectordel;\nchar S[N],ch[2];\nsigned main(){\n\tn=read();\n\tint ans=0,sum=0;\n\tfor(int i=1,j=0;i<=n;i++){\n\t\tscanf(\"%s\",ch+1);w[i]=read();\n\t\tS[i]=(ch[1]-'a'+ans)%26+'a';\n\t\tw[i]^=(ans&((1<<30)-1));\n\t\twhile(j&&S[i]!=S[j+1])j=nxt[j];\n\t\tif(i>1&&S[i]==S[j+1])j++;nxt[i]=j;\n\t\tfail[i-1]=(S[i]==S[nxt[i-1]+1])?fail[nxt[i-1]]:nxt[i-1];\n\t\tfor(int k=i-1,val;k;)\n\t\t\tif(S[i]==S[k+1])k=fail[k];\n\t\t\telse{\n\t\t\t\tsum-=(val=w[*lower_bound(stk+1,stk+top+1,i-k)]);\n\t\t\t\tif(--s[val]==0)s.erase(val);k=nxt[k];\n\t\t\t}\n\t\twhile(top&&w[stk[top]]>=w[i])top--;stk[++top]=i;\n\t\tint cnt=0;del.clear();\n\t\tfor(auto it=s.upper_bound(w[i]);it!=s.end();it++)\n\t\t\tcnt+=it->second,sum+=(w[i]-it->first)*it->second,del.push_back(it->first);\n\t\tfor(auto u:del)s.erase(u);s[w[i]]+=cnt;\n\t\tif(i>1&&S[i]==S[1])sum+=w[i],s[w[i]]++;\n\t\tans+=sum+w[stk[1]];\n\t\twrite(ans);putchar('\\n');\n\t}\n\treturn 0;\n}\n","language":"cpp"} -{"contest_id":"1287","problem_id":"A","statement":"A. Angry Studentstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIt's a walking tour day in SIS.Winter; so tt groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.Initially, some students are angry. Let's describe a group of students by a string of capital letters \"A\" and \"P\": \"A\" corresponds to an angry student \"P\" corresponds to a patient student Such string describes the row from the last to the first student.Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index ii in the string describing a group then they will throw a snowball at the student that corresponds to the character with index i+1i+1 (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them.Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry.Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group.InputThe first line contains a single integer tt\u00a0\u2014 the number of groups of students (1\u2264t\u22641001\u2264t\u2264100). The following 2t2t lines contain descriptions of groups of students.The description of the group starts with an integer kiki (1\u2264ki\u22641001\u2264ki\u2264100)\u00a0\u2014 the number of students in the group, followed by a string sisi, consisting of kiki letters \"A\" and \"P\", which describes the ii-th group of students.OutputFor every group output single integer\u00a0\u2014 the last moment a student becomes angry.ExamplesInputCopy1\n4\nPPAP\nOutputCopy1\nInputCopy3\n12\nAPPAPPPAPPPP\n3\nAAP\n3\nPPA\nOutputCopy4\n1\n0\nNoteIn the first test; after 11 minute the state of students becomes PPAA. After that, no new angry students will appear.In the second tets, state of students in the first group is: after 11 minute\u00a0\u2014 AAPAAPPAAPPP after 22 minutes\u00a0\u2014 AAAAAAPAAAPP after 33 minutes\u00a0\u2014 AAAAAAAAAAAP after 44 minutes all 1212 students are angry In the second group after 11 minute, all students are angry.","tags":["greedy","implementation"],"code":"#include \nusing namespace std;\nint main() {\nint n;cin>>n;\nwhile(n--){\n int indx=0,sum=0, s=0,t = 0;\n cin>>t;\n string srt ;\n cin>>srt;\n for(int i =0;i<10000;i++){\n s=srt.find_last_of( \"A\") ;\n if(sindx)\n indx=sum;\n }\n cout<\n\n#define MOD 1000000007\n\n#define eps 1e-9\n\n#define emailam ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\n#define endl '\\n'\n\n#define ll long long\n\n#define ull unsigned long long\n\n#define MAX 200010\n\n#define pb push_back\n\n#define all(a) a.begin(),a.end()\n\n#define pf push_front\n\n#define fi first\n\n#define se second\n\n#define pii pair\n\nconst int INF = INT_MAX;\n\nusing namespace std;\n\nconst int N=1e6+10;\n\n#define int long long\n\n\/*----------------------------------------------------------------*\/\n\nint dx[] = {+0, +0, -1, +1, +1, +1, -1, -1};\n\nint dy[] = {-1, +1, +0, +0, +1, -1, +1, -1};\n\n\/*----------------------------------------------------------------*\/\n\nvoid READ(){\n\n#ifndef ONLINE_JUDGE\n\n freopen(\"input.txt\", \"r\", stdin), freopen(\"output.txt\", \"w\", stdout);\n\n#endif\n\n}\n\nint n,m;\n\nint dp[20][1001];\n\nconst int mod=1e9+7;\n\nint calc(int i,int j){\n\n if(i==m){\n\n return 1;\n\n }\n\n int &ret=dp[i][j];\n\n if(~ret){\n\n return ret;\n\n }\n\n ret=0;\n\n for(int k=j;k<=n;k++){\n\n ret=(ret+calc(i+1,k))%mod;\n\n }\n\n return ret;\n\n}\n\nvoid solve(){\n\n cin>>n>>m;\n\n m*=2;\n\n memset(dp,-1,sizeof dp);\n\n cout<>t;\n\n while(t--){\n\n solve();\n\n }\n\n}","language":"cpp"} -{"contest_id":"1292","problem_id":"D","statement":"D. Chaotic V.time limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output\u00c6sir - CHAOS \u00c6sir - V.\"Everything has been planned out. No more hidden concerns. The condition of Cytus is also perfect.The time right now...... 00:01:12......It's time.\"The emotion samples are now sufficient. After almost 3 years; it's time for Ivy to awake her bonded sister, Vanessa.The system inside A.R.C.'s Library core can be considered as an undirected graph with infinite number of processing nodes, numbered with all positive integers (1,2,3,\u20261,2,3,\u2026). The node with a number xx (x>1x>1), is directly connected with a node with number xf(x)xf(x), with f(x)f(x) being the lowest prime divisor of xx.Vanessa's mind is divided into nn fragments. Due to more than 500 years of coma, the fragments have been scattered: the ii-th fragment is now located at the node with a number ki!ki! (a factorial of kiki).To maximize the chance of successful awakening, Ivy decides to place the samples in a node PP, so that the total length of paths from each fragment to PP is smallest possible. If there are multiple fragments located at the same node, the path from that node to PP needs to be counted multiple times.In the world of zeros and ones, such a requirement is very simple for Ivy. Not longer than a second later, she has already figured out such a node.But for a mere human like you, is this still possible?For simplicity, please answer the minimal sum of paths' lengths from every fragment to the emotion samples' assembly node PP.InputThe first line contains an integer nn (1\u2264n\u22641061\u2264n\u2264106)\u00a0\u2014 number of fragments of Vanessa's mind.The second line contains nn integers: k1,k2,\u2026,knk1,k2,\u2026,kn (0\u2264ki\u226450000\u2264ki\u22645000), denoting the nodes where fragments of Vanessa's mind are located: the ii-th fragment is at the node with a number ki!ki!.OutputPrint a single integer, denoting the minimal sum of path from every fragment to the node with the emotion samples (a.k.a. node PP).As a reminder, if there are multiple fragments at the same node, the distance from that node to PP needs to be counted multiple times as well.ExamplesInputCopy3\n2 1 4\nOutputCopy5\nInputCopy4\n3 1 4 4\nOutputCopy6\nInputCopy4\n3 1 4 1\nOutputCopy6\nInputCopy5\n3 1 4 1 5\nOutputCopy11\nNoteConsidering the first 2424 nodes of the system; the node network will look as follows (the nodes 1!1!, 2!2!, 3!3!, 4!4! are drawn bold):For the first example, Ivy will place the emotion samples at the node 11. From here: The distance from Vanessa's first fragment to the node 11 is 11. The distance from Vanessa's second fragment to the node 11 is 00. The distance from Vanessa's third fragment to the node 11 is 44. The total length is 55.For the second example, the assembly node will be 66. From here: The distance from Vanessa's first fragment to the node 66 is 00. The distance from Vanessa's second fragment to the node 66 is 22. The distance from Vanessa's third fragment to the node 66 is 22. The distance from Vanessa's fourth fragment to the node 66 is again 22. The total path length is 66.","tags":["dp","graphs","greedy","math","number theory","trees"],"code":"#include\n#define int long long\nusing namespace std;\nconst int N=5e3+1;\nint n;\nint f[N][N],s[N],p[N],c[N];\nsigned main()\n{\nscanf(\"%lld\",&n);\nfor(int i=1;i<=n;i++)\n{\nint x;\nscanf(\"%lld\",&x);\nc[x]++;\n}\np[0]=1;\nfor(int i=1;i<=5000;i++)\n{\np[i]=p[i-1];\nfor(int j=2,x=i;j<=i;j++)\n{\nf[i][j]=f[i-1][j];\nif(x%j)continue;\nwhile(x%j==0)f[i][j]++,x\/=j;\np[i]=max(p[i],j);\n}\n}\nint ans=0;\nfor(int i=1;i<=5000;i++)\nfor(int j=1;j<=i;j++)ans+=f[i][j]*c[i];\nwhile(1)\n{\nmemset(s,0,sizeof(s));\nfor(int i=1;i<=5000;i++)s[p[i]]+=c[i];\nint mx=1;\nfor(int i=1;i<=5000;i++)if(s[i]>s[mx])mx=i;\nif(mx==1||s[mx]*21&&!f[i][p[i]])p[i]--;\n}\n}\nprintf(\"%lld\\n\",ans);\nreturn 0;\n}","language":"cpp"} -{"contest_id":"1294","problem_id":"F","statement":"F. Three Paths on a Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an unweighted tree with nn vertices. Recall that a tree is a connected undirected graph without cycles.Your task is to choose three distinct vertices a,b,ca,b,c on this tree such that the number of edges which belong to at least one of the simple paths between aa and bb, bb and cc, or aa and cc is the maximum possible. See the notes section for a better understanding.The simple path is the path that visits each vertex at most once.InputThe first line contains one integer number nn (3\u2264n\u22642\u22c51053\u2264n\u22642\u22c5105) \u2014 the number of vertices in the tree. Next n\u22121n\u22121 lines describe the edges of the tree in form ai,biai,bi (1\u2264ai1\u2264ai, bi\u2264nbi\u2264n, ai\u2260biai\u2260bi). It is guaranteed that given graph is a tree.OutputIn the first line print one integer resres \u2014 the maximum number of edges which belong to at least one of the simple paths between aa and bb, bb and cc, or aa and cc.In the second line print three integers a,b,ca,b,c such that 1\u2264a,b,c\u2264n1\u2264a,b,c\u2264n and a\u2260,b\u2260c,a\u2260ca\u2260,b\u2260c,a\u2260c.If there are several answers, you can print any.ExampleInputCopy8\n1 2\n2 3\n3 4\n4 5\n4 6\n3 7\n3 8\nOutputCopy5\n1 8 6\nNoteThe picture corresponding to the first example (and another one correct answer):If you choose vertices 1,5,61,5,6 then the path between 11 and 55 consists of edges (1,2),(2,3),(3,4),(4,5)(1,2),(2,3),(3,4),(4,5), the path between 11 and 66 consists of edges (1,2),(2,3),(3,4),(4,6)(1,2),(2,3),(3,4),(4,6) and the path between 55 and 66 consists of edges (4,5),(4,6)(4,5),(4,6). The union of these paths is (1,2),(2,3),(3,4),(4,5),(4,6)(1,2),(2,3),(3,4),(4,5),(4,6) so the answer is 55. It can be shown that there is no better answer.","tags":["dfs and similar","dp","greedy","trees"],"code":"\/\/ LUOGU_RID: 101682976\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#define fs(i,x,y) for(int i=(x),_=(y);i<=_;++i)\n\n#define fn(i,x,y) for(int i=(x),_=(y);i>=_;--i)\n\n#define tor(i,v,x) for(int i=head[x],v=to[i];i;i=nxt[i],v=to[i])\n\n#define ls(x) ch[x][0]\n\n#define rs(x) ch[x][1]\n\n#define mpi(x,y) make_pair(x,y)\n\n#define pi pair\n\n#define fi first\n\n#define se second\n\n\/\/#define int ll\n\n#define DEBUG printf(\"%s %d\\n\",__FUNCTION__,__LINE__)\n\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef double db;\n\ntypedef long double ldb;\n\ntemplate\n\nvoid read(T &x){\n\n\tx=0;char ch=getchar();bool f=0;\n\n\twhile(ch<'0'||ch>'9'){\n\n\t\tif(ch=='-')f=1;\n\n\t\tch=getchar();\n\n\t}\n\n\twhile(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();\n\n\tx=f?-x:x;\n\n}\n\ntemplate\n\nvoid read(T &x,Args &...args){\n\n\tread(x);read(args...);\n\n}\n\n\n\nconst int N=1e6+7;\n\nint n,m,ans;\n\nint dep[N];\n\nvectormx[N];\n\nvectorG[N];\n\nvectorwt(3,0);\n\n\n\nvoid upd(int x,int y,int z){\n\n\twt[0]=x,wt[1]=y,wt[2]=z;\n\n}\n\n\n\nvoid df(int u,int fa){\n\n\tdep[u]=dep[fa]+1;\n\n\tmx[u].push_back(mpi(0,u));\n\n\tmx[u].push_back(mpi(0,u));\n\n\tfor(int v:G[u]){\n\n\t\tif(v^fa){\n\n\t\t\tdf(v,u);\n\n\t\t\tmx[u].push_back(mpi(mx[v][0].fi+1,mx[v][0].se));\n\n\t\t}\n\n\t}\n\n\tsort(mx[u].begin(),mx[u].end(),[&](pi &lhs,pi &rhs){\n\n\t\treturn lhs.fi>rhs.fi;\n\n\t});\n\n}\n\n\n\nvoid dfs(int u,int fa){\n\n\tsort(mx[u].begin(),mx[u].end(),[&](pi &lhs,pi &rhs){\n\n\t\treturn lhs.fi>rhs.fi;\n\n\t});\n\n\tif(mx[u][0].fi+mx[u][1].fi+mx[u][2].fi>ans){\n\n\t\tans=mx[u][0].fi+mx[u][1].fi+mx[u][2].fi;\n\n\t\tupd(mx[u][0].se,mx[u][1].se,mx[u][2].se);\n\n\t}\n\n\/\/\tcout<<\"\/\/\/\/u:\"<\n\n#define ll long long\n\nusing namespace std;\n\nconst ll mod = 1e9+7;\n\nint main()\n\n{\n\n ios::sync_with_stdio(false);\n\n cin.tie(NULL);\n\n cout.tie(NULL);\n\n ll H,n,najm=1e18,idx=-1;\n\n cin >> H >> n;\n\n vectora(n),p(n),x(n,1e18);\n\n for(ll i=0;i> a[i];\n\n if(i) p[i]=p[i-1],x[i]=x[i-1];\n\n p[i]+=a[i];\n\n x[i]=min(x[i],p[i]);\n\n }\n\n if(p[n-1]>=0)\n\n {\n\n if(x[n-1]>-H)\n\n {\n\n cout << -1;\n\n return 0;\n\n }\n\n for(ll i=0;i=-H&&p[i]<0)\n\n {\n\n cout << 1+i;\n\n return 0;\n\n }\n\n }\n\n }\n\n ll l=0,r=LLONG_MAX\/abs(p[n-1]),o=-1;\n\n while(l<=r)\n\n {\n\n ll s=(l+r)\/2;\n\n if(s*p[n-1]+x[n-1]<=-H)\n\n {\n\n o=s;\n\n r=s-1;\n\n }\n\n else l=s+1;\n\n }\n\n \/\/ cout << o << endl;\n\n ll R=o*p[n-1],ans=n*o;\n\n l=0,r=n-1,o=-1;\n\n while(l<=r)\n\n {\n\n ll s=(l+r)\/2;\n\n if(x[s]+R<=-H)\n\n {\n\n o=s;\n\n r=s-1;\n\n }\n\n else l=s+1;\n\n }\n\n \/\/ cout << ans << ' ' << o << endl;\n\n cout << ans+o+1;\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1322","problem_id":"B","statement":"B. Presenttime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputCatherine received an array of integers as a gift for March 8. Eventually she grew bored with it; and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one\u00a0\u2014 xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute(a1+a2)\u2295(a1+a3)\u2295\u2026\u2295(a1+an)\u2295(a2+a3)\u2295\u2026\u2295(a2+an)\u2026\u2295(an\u22121+an)(a1+a2)\u2295(a1+a3)\u2295\u2026\u2295(a1+an)\u2295(a2+a3)\u2295\u2026\u2295(a2+an)\u2026\u2295(an\u22121+an)Here x\u2295yx\u2295y is a bitwise XOR operation (i.e. xx ^ yy in many modern programming languages). You can read about it in Wikipedia: https:\/\/en.wikipedia.org\/wiki\/Exclusive_or#Bitwise_operation.InputThe first line contains a single integer nn (2\u2264n\u22644000002\u2264n\u2264400000)\u00a0\u2014 the number of integers in the array.The second line contains integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641071\u2264ai\u2264107).OutputPrint a single integer\u00a0\u2014 xor of all pairwise sums of integers in the given array.ExamplesInputCopy2\n1 2\nOutputCopy3InputCopy3\n1 2 3\nOutputCopy2NoteIn the first sample case there is only one sum 1+2=31+2=3.In the second sample case there are three sums: 1+2=31+2=3, 1+3=41+3=4, 2+3=52+3=5. In binary they are represented as 0112\u22951002\u22951012=01020112\u22951002\u22951012=0102, thus the answer is 2.\u2295\u2295 is the bitwise xor operation. To define x\u2295yx\u2295y, consider binary representations of integers xx and yy. We put the ii-th bit of the result to be 1 when exactly one of the ii-th bits of xx and yy is 1. Otherwise, the ii-th bit of the result is put to be 0. For example, 01012\u229500112=0110201012\u229500112=01102.","tags":["binary search","bitmasks","constructive algorithms","data structures","math","sortings"],"code":"\/\/ #pragma GCC optimize(\"O3,unroll-loops\")\n\n\/\/ #pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\n#include \n\nusing namespace std;\n\nusing namespace __gnu_cxx;\n\nusing namespace __gnu_pbds;\n\ntemplate using Tree = tree, rb_tree_tag,tree_order_statistics_node_update>;\n\nusing Trie = trie, pat_trie_tag, trie_prefix_search_node_update>;\n\n\/\/ template using heapq = __gnu_pbds::priority_queue, pairing_heap_tag>;\n\ntemplate using heapq = std::priority_queue, greater>;\n\n#define ll long long\n\n#define i128 __int128\n\n#define ld long double\n\n#define ui unsigned int\n\n#define ull unsigned long long\n\n#define pii pair\n\n#define pll pair\n\n#define pdd pair\n\n#define vi vector\n\n#define vvi vector>\n\n#define vll vector\n\n#define vvll vector>\n\n#define vpii vector\n\n#define vpll vector\n\n#define lb lower_bound\n\n#define ub upper_bound\n\n#define pb push_back\n\n#define pf push_front\n\n#define eb emplace_back\n\n#define fi first\n\n#define se second\n\n#define rep(i, a, b) for(int i = a; i < b; ++i)\n\n#define per(i, a, b) for(int i = a; i > b; --i)\n\n#define each(x, v) for(auto& x: v)\n\n#define len(x) (int)x.size()\n\n#define elif else if\n\n#define all(x) (x).begin(), (x).end()\n\n#define rall(x) (x).rbegin(), (x).rend()\n\n#define mst(x, a) memset(x, a, sizeof(x))\n\n#define lowbit(x) (x & (-x))\n\n#define bitcnt(x) (__builtin_popcountll(x))\n\n#define endl \"\\n\"\n\nmt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );\n\n#define Ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)\n\nstruct custom_hash {\n\n static uint64_t splitmix64(uint64_t x) {\n\n \/\/ http:\/\/xorshift.di.unimi.it\/splitmix64.c\n\n x += 0x9e3779b97f4a7c15;\n\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n\n return x ^ (x >> 31);\n\n }\n\n\n\n size_t operator()(uint64_t x) const {\n\n static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\n\n return splitmix64(x + FIXED_RANDOM);\n\n }\n\n\n\n size_t operator()(pair x) const {\n\n static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();\n\n return splitmix64(x.first + FIXED_RANDOM) ^ (splitmix64(x.second + FIXED_RANDOM) >> 1);\n\n }\n\n};\n\nconst i128 ONE = 1;\n\nistream &operator>>(istream &in, i128 &x) {\n\n string s;\n\n in >> s;\n\n bool minus = false;\n\n if (s[0] == '-') {\n\n minus = true;\n\n s.erase(s.begin());\n\n }\n\n x = 0;\n\n for (auto i : s) {\n\n x *= 10;\n\n x += i - '0';\n\n }\n\n if (minus) x = -x;\n\n return in;\n\n}\n\nostream &operator<<(ostream &out, i128 x) {\n\n string s;\n\n bool minus = false;\n\n if (x < 0) {\n\n minus = true;\n\n x = -x;\n\n }\n\n while (x) {\n\n s.push_back(x % 10 + '0');\n\n x \/= 10;\n\n }\n\n if (s.empty()) s = \"0\";\n\n if (minus) out << '-';\n\n reverse(s.begin(), s.end());\n\n out << s;\n\n return out;\n\n}\n\ntemplate ostream &operator<<(ostream &os, const vector &v) {\n\n for(auto it = begin(v); it != end(v); ++it) {\n\n if(it == begin(v)) os << *it;\n\n else os << \" \" << *it;\n\n }\n\n return os;\n\n}\n\ntemplate ostream &operator<<(ostream &os, const set &v) {\n\n for(auto it = begin(v); it != end(v); ++it) {\n\n if(it == begin(v)) os << *it;\n\n else os << \" \" << *it;\n\n }\n\n return os;\n\n}\n\ntemplate ostream &operator<<(ostream &os, const multiset &v) {\n\n for(auto it = begin(v); it != end(v); ++it) {\n\n if(it == begin(v)) os << *it;\n\n else os << \" \" << *it;\n\n }\n\n return os;\n\n}\n\ntemplate ostream &operator<<(ostream &os, const Tree &v) {\n\n for(auto it = begin(v); it != end(v); ++it) {\n\n if(it == begin(v)) os << *it;\n\n else os << \" \" << *it;\n\n }\n\n return os;\n\n}\n\ntemplate ostream &operator<<(ostream &os, const pair &p) {\n\n os << p.first << \" \" << p.second;\n\n return os;\n\n}\n\nll gcd(ll x,ll y) {\n\n if(!x) return y;\n\n if(!y) return x;\n\n int t = __builtin_ctzll(x | y);\n\n x >>= __builtin_ctzll(x);\n\n do {\n\n y >>= __builtin_ctzll(y);\n\n if(x > y) swap(x, y);\n\n y -= x;\n\n } while(y);\n\n return x< y ? x : y; }\n\nll min(ll x, ll y) { return x < y ? x : y; }\n\nll Mod(ll x, int mod) { return (x % mod + mod) % mod; }\n\nll pow(ll x, ll y, ll mod){\n\n ll res = 1, cur = x;\n\n while (y) {\n\n if (y & 1) res = res * cur % mod;\n\n cur = ONE * cur * cur % mod;\n\n y >>= 1;\n\n }\n\n return res % mod;\n\n}\n\nll probabilityMod(ll x, ll y, ll mod) {\n\n return x * pow(y, mod-2, mod) % mod;\n\n}\n\nvvi getGraph(int n, int m, bool directed = false) {\n\n vvi res(n);\n\n rep(_, 0, m) {\n\n int u, v;\n\n cin >> u >> v;\n\n u--, v--;\n\n res[u].emplace_back(v);\n\n if(!directed) res[v].emplace_back(u);\n\n }\n\n return res;\n\n}\n\nvector getWeightedGraph(int n, int m, bool directed = false) {\n\n vector res(n);\n\n rep(_, 0, m) {\n\n int u, v, w;\n\n cin >> u >> v >> w;\n\n u--, v--;\n\n res[u].emplace_back(v, w);\n\n if(!directed) res[v].emplace_back(u, w);\n\n }\n\n return res;\n\n}\n\nconst ll LINF = 0x1fffffffffffffff;\n\nconst ll MINF = 0x7fffffffffff;\n\nconst int INF = 0x3fffffff;\n\nconst int MOD = 1000000007;\n\nconst int MODD = 998244353;\n\nconst int N = 1e6 + 10;\n\n\n\nvoid solve() {\n\n int n;\n\n cin >> n;\n\n vi a(n);\n\n each(i, a) cin >> i;\n\n auto calc = [&] (int bit) -> int {\n\n vi h;\n\n each(i, a) h.pb(i % (1 << bit + 1));\n\n sort(all(h));\n\n int res = 0;\n\n each(i, h) {\n\n res += ub(all(h), (1 << bit + 1) - 1 - i) - lb(all(h), (1 << bit) - i);\n\n if ((1 << bit) - i <= i && i <= (1 << bit + 1) - 1 - i)\n\n res -= 1;\n\n res += ub(all(h), (1 << bit + 2) - 2 - i) - lb(all(h), (0b11 << bit) - i);\n\n if ((0b11 << bit) - i <= i && i <= (1 << bit + 2) - 2 - i)\n\n res -= 1;\n\n }\n\n res \/= 2;\n\n return res & 1;\n\n };\n\n int ans = 0;\n\n rep(bit, 0, 30) ans |= calc(bit) << bit;\n\n cout << ans << endl;\n\n}\n\n\n\nsigned main() {\n\n ios::sync_with_stdio(0);\n\n cin.tie(0);\n\n cout.tie(0);\n\n int t = 1;\n\n \/\/ cin >> t;\n\n while (t--) {\n\n solve();\n\n }\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1321","problem_id":"C","statement":"C. Remove Adjacenttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string ss consisting of lowercase Latin letters. Let the length of ss be |s||s|. You may perform several operations on this string.In one operation, you can choose some index ii and remove the ii-th character of ss (sisi) if at least one of its adjacent characters is the previous letter in the Latin alphabet for sisi. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index ii should satisfy the condition 1\u2264i\u2264|s|1\u2264i\u2264|s| during each operation.For the character sisi adjacent characters are si\u22121si\u22121 and si+1si+1. The first and the last characters of ss both have only one adjacent character (unless |s|=1|s|=1).Consider the following example. Let s=s= bacabcab. During the first move, you can remove the first character s1=s1= b because s2=s2= a. Then the string becomes s=s= acabcab. During the second move, you can remove the fifth character s5=s5= c because s4=s4= b. Then the string becomes s=s= acabab. During the third move, you can remove the sixth character s6=s6='b' because s5=s5= a. Then the string becomes s=s= acaba. During the fourth move, the only character you can remove is s4=s4= b, because s3=s3= a (or s5=s5= a). The string becomes s=s= acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.InputThe first line of the input contains one integer |s||s| (1\u2264|s|\u22641001\u2264|s|\u2264100) \u2014 the length of ss.The second line of the input contains one string ss consisting of |s||s| lowercase Latin letters.OutputPrint one integer \u2014 the maximum possible number of characters you can remove if you choose the sequence of moves optimally.ExamplesInputCopy8\nbacabcab\nOutputCopy4\nInputCopy4\nbcda\nOutputCopy3\nInputCopy6\nabbbbb\nOutputCopy5\nNoteThe first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only; but it can be shown that the maximum possible answer to this test is 44.In the second example, you can remove all but one character of ss. The only possible answer follows. During the first move, remove the third character s3=s3= d, ss becomes bca. During the second move, remove the second character s2=s2= c, ss becomes ba. And during the third move, remove the first character s1=s1= b, ss becomes a. ","tags":["brute force","constructive algorithms","greedy","strings"],"code":"\/\/ @author manish926\n\n\/\/NEVER GIVE UP,struggle... ,struggle...,struggle.... success.\n\n#include\n\nusing namespace std;\n\n#define ll long long int\n\n#define ld long double\n\n#define f1(i,n) for(int i=0;i\n\n#define pi pair\n\n#define vvi vector> \n\n#define vll vector\n\n#define mi map\n\n#define umi unordered_map\n\n#define si set\n\n#define usi unordered_set\n\n#define pb push_back\n\n#define pp pop_back\n\n#define sortv(v) sort(v.begin(),v.end())\n\n#define e '\\n'\n\n#define all(x) x.begin(),x.end()\n\n#define mp make_pair\n\n\n\n#ifndef ONLINE_JUDGE\n\n#define debug(x) cerr << #x <<\" \"; _print(x); cerr << endl;\n\n#else\n\n#define debug(x)\n\n#endif\n\nvoid _print(int t) {cerr << t;}\n\nvoid _print(ld t) {cerr << t;}\n\nvoid _print(string t) {cerr << t;}\n\nvoid _print(char t) {cerr << t;}\n\nvoid _print(double t) {cerr << t;}\n\ntemplate void _print(pair p) {cerr << \"{\"; _print(p.ff); cerr << \",\"; _print(p.ss); cerr << \"}\";}\n\ntemplate void _print(vector v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\ntemplate void _print(set v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\ntemplate void _print(multiset v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\ntemplate void _print(map v) {cerr << \"[ \"; for (auto i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\n\n\nsigned main(){\n\nios_base::sync_with_stdio(false);\n\ncin.tie(NULL); \n\n\n\nint n;cin>>n;\n\nstring s;cin>>s;\n\nint ans =0;\n\nfor(char ch = 'z' ; ch>='b';){\n\n int t = 0;\n\n for(int i=0;i 0 && ((ch-'0')-1 == (s[i-1]-'0'))){\n\n s.erase(s.begin()+i);\n\n ans++;\n\n t++;\n\n break;\n\n }else if(i\n\n#include \n\nusing namespace std;\n\ntemplate void print(container v) { for (auto& it : v) cout << it << ' ' ;cout <> n >> m;\n\n vector > a(n , vector (m));\n\n for (auto &i : a) for (auto &j : i) cin >> j , --j;\n\n ll ans = 0;\n\n for (int j = 0; j < m; ++j){\n\n vector cnt(n);\/\/ the number of cycles needed to put the element in its position\n\n for (int i = 0;i < n; ++i){\n\n if (a[i][j] % m != j || a[i][j] \/m >= n) continue;\n\n cnt[(i - a[i][j]\/m +n) % n]++;\n\n }\n\n int cur = n - cnt[0];\n\n for (int i = 1; i < n; ++i) cur = min(cur , n-cnt[i]+i);\n\n ans+=cur;\n\n }\n\n cout << ans <> tt;\n\n while(tt--) hi();\n\n return 0;\n\n}\n\n\n\n","language":"cpp"} -{"contest_id":"1304","problem_id":"E","statement":"E. 1-Trees and Queriestime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputGildong was hiking a mountain; walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree?Then he found that such tree-like graphs are called 1-trees. Since Gildong was bored of solving too many tree problems, he wanted to see if similar techniques in trees can be used in 1-trees as well. Instead of solving it by himself, he's going to test you by providing queries on 1-trees.First, he'll provide you a tree (not 1-tree) with nn vertices, then he will ask you qq queries. Each query contains 55 integers: xx, yy, aa, bb, and kk. This means you're asked to determine if there exists a path from vertex aa to bb that contains exactly kk edges after adding a bidirectional edge between vertices xx and yy. A path can contain the same vertices and same edges multiple times. All queries are independent of each other; i.e. the added edge in a query is removed in the next query.InputThe first line contains an integer nn (3\u2264n\u22641053\u2264n\u2264105), the number of vertices of the tree.Next n\u22121n\u22121 lines contain two integers uu and vv (1\u2264u,v\u2264n1\u2264u,v\u2264n, u\u2260vu\u2260v) each, which means there is an edge between vertex uu and vv. All edges are bidirectional and distinct.Next line contains an integer qq (1\u2264q\u22641051\u2264q\u2264105), the number of queries Gildong wants to ask.Next qq lines contain five integers xx, yy, aa, bb, and kk each (1\u2264x,y,a,b\u2264n1\u2264x,y,a,b\u2264n, x\u2260yx\u2260y, 1\u2264k\u22641091\u2264k\u2264109) \u2013 the integers explained in the description. It is guaranteed that the edge between xx and yy does not exist in the original tree.OutputFor each query, print \"YES\" if there exists a path that contains exactly kk edges from vertex aa to bb after adding an edge between vertices xx and yy. Otherwise, print \"NO\".You can print each letter in any case (upper or lower).ExampleInputCopy5\n1 2\n2 3\n3 4\n4 5\n5\n1 3 1 2 2\n1 4 1 3 2\n1 4 1 3 3\n4 2 3 3 9\n5 2 3 3 9\nOutputCopyYES\nYES\nNO\nYES\nNO\nNoteThe image below describes the tree (circles and solid lines) and the added edges for each query (dotted lines). Possible paths for the queries with \"YES\" answers are: 11-st query: 11 \u2013 33 \u2013 22 22-nd query: 11 \u2013 22 \u2013 33 44-th query: 33 \u2013 44 \u2013 22 \u2013 33 \u2013 44 \u2013 22 \u2013 33 \u2013 44 \u2013 22 \u2013 33 ","tags":["data structures","dfs and similar","shortest paths","trees"],"code":"#include\n\nusing namespace std;\n\n\n\nconst int N = 1e5 + 1, M = 20;\n\n\n\nvector adj[N];\n\nint dep[N], Par[N][M], path[N];\n\n \n\nvoid dfs(int cur, int par) \n\n{\n\n dep[cur] = dep[par] + 1;\n\n Par[cur][0] = par;\n\n for (int j = 1; j < M; j++) Par[cur][j] = Par[Par[cur][j - 1]][j - 1];\n\n for (auto x : adj[cur]) if (x != par) dfs(x, cur);\n\n}\n\n \n\nint LCA(int u, int v) \n\n{\n\n if (u == v) return u;\n\n if (dep[u] < dep[v]) swap(u, v);\n\n int diff = dep[u] - dep[v];\n\n for (int j = M - 1; j >= 0; j--) \n\n {\n\n if ((diff >> j) & 1) u = Par[u][j];\n\n }\n\n \n\n for (int j = M - 1; j >= 0; j--) \n\n {\n\n if (Par[u][j] != Par[v][j]) {\n\n u = Par[u][j];\n\n v = Par[v][j];\n\n }\n\n }\n\n \n\n return (u != v ? Par[u][0] : u);\n\n}\n\n \n\nint distance(int a, int b)\n\n{\n\n int c = LCA(a, b);\n\n return dep[a] + dep[b] - 2*dep[c];\n\n}\n\n\n\nint main()\n\n{\n\n #ifndef ONLINE_JUDGE\n\n freopen(\"input.txt\", \"r\", stdin);\n\n freopen(\"output.txt\", \"w\", stdout);\n\n #endif\n\n\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(NULL);\n\n\n\n int n; cin >> n;\n\n\n\n for(int i = 1; i < n; i++)\n\n {\n\n int x, y; cin >> x >> y;\n\n adj[x].push_back(y);\n\n adj[y].push_back(x);\n\n }\n\n\n\n dfs(1, 0);\n\n\n\n int q; cin >> q;\n\n\n\n for(int i = 1; i <= q; i++)\n\n {\n\n int x, y, a, b, k; cin >> x >> y >> a >> b >> k;\n\n int path1 = distance(a, b);\n\n int path2 = distance(a, x) + distance(y, b) + 1;\n\n int path3 = distance(a, y) + distance(x, b) + 1;\n\n if(path1 <= k && path1%2 == k%2) cout << \"YES\\n\";\n\n else if(path2 <= k && path2%2 == k%2) cout << \"YES\\n\";\n\n else if(path3 <= k && path3%2 == k%2) cout << \"YES\\n\";\n\n else cout << \"NO\\n\";\n\n }\n\n}","language":"cpp"} -{"contest_id":"1286","problem_id":"B","statement":"B. Numbers on Treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEvlampiy was gifted a rooted tree. The vertices of the tree are numbered from 11 to nn. Each of its vertices also has an integer aiai written on it. For each vertex ii, Evlampiy calculated cici\u00a0\u2014 the number of vertices jj in the subtree of vertex ii, such that aj> n;\n\n\n\n vector p(n + 1, 0), c(n + 1, 0);\n\n for (int i = 1; i <= n; ++i)\n\n cin >> p[i] >> c[i];\n\n \n\n vector> adj(n + 1);\n\n for (int i = 1; i <= n; ++i)\n\n adj[p[i]].push_back(i);\n\n \n\n int rt = -1;\n\n for (int i = 1; i <= n; ++i)\n\n if (p[i] == 0)\n\n rt = i;\n\n \n\n vector sz(n + 1);\n\n\n\n auto dfs = [&] (auto &f, int cur) -> int {\n\n int s = 1;\n\n for (auto &e : adj[cur]) {\n\n s += f(f, e);\n\n }\n\n return sz[cur] = s;\n\n };\n\n dfs(dfs, rt);\n\n\n\n for (int i = 1; i <= n; ++i) {\n\n if (c[i] >= sz[i]) {\n\n cout << \"NO\\n\";\n\n return;\n\n }\n\n }\n\n\n\n vector ans(n + 1, -1);\n\n auto dfs2 = [&] (auto &f, int cur, vector &av) -> void {\n\n ans[cur] = av[c[cur]];\n\n int i = 0;\n\n for (auto &e : adj[cur]) {\n\n vector cv;\n\n for (int j = 0; j < sz[e]; ++j) {\n\n if (i == c[cur])\n\n i++;\n\n cv.push_back(av[i]);\n\n i++;\n\n }\n\n f(f, e, cv);\n\n }\n\n };\n\n vector av(n);\n\n iota(begin(av), end(av), 1);\n\n dfs2(dfs2, rt, av);\n\n\n\n cout << \"YES\\n\";\n\n for (int i = 1; i <= n; ++i)\n\n cout << ans[i] << ' ';\n\n cout << '\\n';\n\n};\n\n\n\nvoid init() {\n\n \n\n}\n\n\n\nint main()\n\n{\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(NULL);\n\n cin.exceptions(cin.failbit);\n\n \n\n #ifdef FLOAT_PRECISION\n\n cout << fixed;\n\n cout.precision(FLOAT_PRECISION);\n\n #endif\n\n \n\n init();\n\n \n\n int numTests = 1;\n\n #ifdef MULTI_TESTS\n\n cin >> numTests;\n\n #endif\n\n for (int testNo = 1; testNo <= numTests; testNo++) {\n\n solve();\n\n }\n\n \n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1141","problem_id":"F2","statement":"F2. Same Sum Blocks (Hard)time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is given in two editions; which differ exclusively in the constraints on the number nn.You are given an array of integers a[1],a[2],\u2026,a[n].a[1],a[2],\u2026,a[n]. A block is a sequence of contiguous (consecutive) elements a[l],a[l+1],\u2026,a[r]a[l],a[l+1],\u2026,a[r] (1\u2264l\u2264r\u2264n1\u2264l\u2264r\u2264n). Thus, a block is defined by a pair of indices (l,r)(l,r).Find a set of blocks (l1,r1),(l2,r2),\u2026,(lk,rk)(l1,r1),(l2,r2),\u2026,(lk,rk) such that: They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (li,ri)(li,ri) and (lj,rj(lj,rj) where i\u2260ji\u2260j either rikk\u2032>k. The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks.InputThe first line contains integer nn (1\u2264n\u226415001\u2264n\u22641500) \u2014 the length of the given array. The second line contains the sequence of elements a[1],a[2],\u2026,a[n]a[1],a[2],\u2026,a[n] (\u2212105\u2264ai\u2264105\u2212105\u2264ai\u2264105).OutputIn the first line print the integer kk (1\u2264k\u2264n1\u2264k\u2264n). The following kk lines should contain blocks, one per line. In each line print a pair of indices li,rili,ri (1\u2264li\u2264ri\u2264n1\u2264li\u2264ri\u2264n) \u2014 the bounds of the ii-th block. You can print blocks in any order. If there are multiple answers, print any of them.ExamplesInputCopy7\n4 1 2 2 1 5 3\nOutputCopy3\n7 7\n2 3\n4 5\nInputCopy11\n-5 -4 -3 -2 -1 0 1 2 3 4 5\nOutputCopy2\n3 4\n1 1\nInputCopy4\n1 1 1 1\nOutputCopy4\n4 4\n1 1\n2 2\n3 3\n","tags":["data structures","greedy"],"code":"#include \n\n#define x first\n\n#define y second\n\n#define TLE ios::sync_with_stdio(0),cin.tie(0)\n\nusing namespace std;\n\ntypedef long long LL;\n\ntypedef pair PII;\n\ntypedef pair PDD;\n\ntypedef unsigned long long ULL;\n\nconst int N = 3e5 + 10, M = 2 * N, mod = 1e9 + 7;\n\nconst double eps = 1e-8;\n\nint dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};\n\nint h[N], ne[M], e[M], w[M];\n\nLL T, n, m, k;\n\nint a[N], s[N];\n\nint main()\n\n{\n\n TLE;\n\n cin >> n;\n\n for (int i = 1; i <= n; i ++ ) cin >> a[i], s[i] = s[i - 1] + a[i];\n\n map> hash;\n\n for (int i = 1; i <= n; i ++ )\n\n for (int j = 1; j <= i; j ++ )\n\n hash[s[i] - s[j - 1]].push_back({j, i});\n\n \/\/for (auto& c : hash[3]) cout << c.x << \" \" << c.y << endl;\n\n int st = -2e9, ed = -2e9;\n\n vector ans;\n\n for (auto& v : hash){\n\n st = -2e9, ed = -2e9;\n\n vector tmp;\n\n for (auto& c : v.y){\n\n if (c.x > ed){\n\n if (ed != -2e9) tmp.push_back({st, ed});\n\n st = c.x, ed = c.y;\n\n }\n\n }\n\n tmp.push_back({st, ed});\n\n if (tmp.size() > ans.size()) ans = tmp;\n\n }\n\n cout << ans.size() << \"\\n\";\n\n for (auto& c : ans) cout << c.x << \" \" << c.y << \"\\n\";\n\n}\n\n","language":"cpp"} -{"contest_id":"1313","problem_id":"B","statement":"B. Different Rulestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNikolay has only recently started in competitive programming; but already qualified to the finals of one prestigious olympiad. There going to be nn participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took xx-th place and in the second round\u00a0\u2014 yy-th place. Then the total score of the participant A is sum x+yx+y. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every ii from 11 to nn exactly one participant took ii-th place in first round and exactly one participant took ii-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got xx-th place in first round and yy-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question.InputThe first line contains an integer tt (1\u2264t\u22641001\u2264t\u2264100)\u00a0\u2014 the number of test cases to solve.Each of the following tt lines contains integers nn, xx, yy (1\u2264n\u22641091\u2264n\u2264109, 1\u2264x,y\u2264n1\u2264x,y\u2264n)\u00a0\u2014 the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round.OutputPrint two integers\u00a0\u2014 the minimum and maximum possible overall place Nikolay could take.ExamplesInputCopy15 1 3OutputCopy1 3InputCopy16 3 4OutputCopy2 6NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second\u00a0\u2014 third place.","tags":["constructive algorithms","greedy","implementation","math"],"code":"#include \n#include \nusing namespace std;\nvoid solve(){\nint n,x,y;\ncin>>n>>x>>y;\ncout<>t;\nwhile(t--)solve();\nreturn 0;\n}\n","language":"cpp"} -{"contest_id":"1313","problem_id":"C2","statement":"C2. Skyscrapers (hard version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThis is a harder version of the problem. In this version n\u2264500000n\u2264500000The outskirts of the capital are being actively built up in Berland. The company \"Kernel Panic\" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought nn plots along the highway and is preparing to build nn skyscrapers, one skyscraper per plot.Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it.Formally, let's number the plots from 11 to nn. Then if the skyscraper on the ii-th plot has aiai floors, it must hold that aiai is at most mimi (1\u2264ai\u2264mi1\u2264ai\u2264mi). Also there mustn't be integers jj and kk such that jaiai\n#include\n#include\n#include\nusing namespace std;\ntypedef long long ll;\nvoid solve() {\n\tint n;\n\tcin >> n;\n\tvector a(n + 1);\n\tvector presum(n + 1), sufsum(n + 1);\n\tfor (int i = 1; i <= n; ++i) {\n\t\tcin >> a[i];\n\t}\n\tstack st;\/\/\u6808\u91cc\u9762\u5b58\u653e\u7684\u662f\u4e0b\u6807\n\tvector l(n + 2), r(n + 2);\n\tfor (int i = 1; i <= n; ++i) {\n\t\twhile (!st.empty() && a[st.top()] >=\n\t\t a[i]) {\n\t\t\t\/\/ \u4e0d\u540c\u9898\u76ee\u7684\u7b26\u53f7\u4e0d\u540c\uff0c\u6ce8\u610f\u5206\u8fa8\n\t\t\tst.pop();\n\t\t}\n\t\tl[i] = (st.size() == 0 ? 0 :\n\t\t st.top());\n\t\tst.push(i);\/\/ \u6ce8\u610f\u6808\u91cc\u9762\u653e\u8fdb\u53bb\u7684\u662f\u4e0b\u6807\n\t}\n\twhile (!st.empty())st.pop();\n\tfor (int i = n; i >= 1; --i) {\n\t\twhile (!st.empty() && a[st.top()] >=\n\t\t a[i]) {\n\t\t\t\/\/ \u4e0d\u540c\u9898\u76ee\u7684\u7b26\u53f7\u4e0d\u540c\uff0c\u6ce8\u610f\u5206\u8fa8\n\t\t\tst.pop();\n\t\t}\n\t\tr[i] = (st.size() == 0 ? (n + 1) :\n\t\t st.top());\n\t\tst.push(i);\/\/ \u6ce8\u610f\u6808\u91cc\u9762\u653e\u8fdb\u53bb\u7684\u662f\u4e0b\u6807\n\t}\n\n\tfor (int i = 1; i <= n; i++) {\n\t\tpresum[i] = presum[l[i]] + a[i] * (i - l[i]);\n\t}\n\tfor (int i = n; i >= 1; --i) {\n\t\tsufsum[i] = sufsum[r[i]] + a[i] * (r[i] - i);\n\t}\n\n\n\tvector res(n + 1);\n\tint idx;\n\tll ans = -1;\n\tfor (int j = 1; j <= n; j++) {\n\t\tif (ans < presum[j] + sufsum[j] - a[j]) {\n\t\t\tans = presum[j] + sufsum[j] - a[j];\n\t\t\tidx = j;\n\t\t}\n\t}\n\tll pre = a[idx];\n\tfor (int i = idx - 1; i >= 1; --i) {\n\t\tres[i] = min(pre, a[i]);\n\t\tpre = min(pre, a[i]);\n\t}\n\tres[idx] = a[idx];\n\tpre = a[idx];\n\tfor (int i = idx + 1; i <= n; i++) {\n\t\tres[i] = min(pre, a[i]);\n\t\tpre = min(pre, a[i]);\n\t}\n\tfor (int i = 1; i <= n; i++) {\n\t\tcout << res[i] << \" \";\n\t}\n}\nint main() {\n\tios::sync_with_stdio(false), cin.tie(0);\n\tsolve();\n}\n\t \t\t\t \t \t \t\t\t \t \t \t \t\t \t \t\t\t","language":"cpp"} -{"contest_id":"1303","problem_id":"E","statement":"E. Erase Subsequencestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string ss. You can build new string pp from ss using the following operation no more than two times: choose any subsequence si1,si2,\u2026,siksi1,si2,\u2026,sik where 1\u2264i1\n\n\n\nusing namespace std;\n\n#define int long long\n\nconst int maxn=405;\n\nbitset dp[maxn][maxn];\n\nint32_t main()\n\n{\n\n ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);\n\n int t;cin>>t;\n\n while(t--)\n\n {\n\n string s1,s2;cin>>s1>>s2;\n\n int m=s2.size();s2.push_back('$');\n\n bool ok=false;\n\n {\n\n for(int i=0;i<=m;++i) for(int j=0;j<=m;++j) dp[i][j]=0;\n\n for(int i=0;i<=m;++i) dp[0][i][i]=1;\n\n for(char c:s1)\n\n {\n\n for(int i=m;i>=0;--i)\n\n {\n\n for(int j=m;j>=i;--j)\n\n {\n\n if(c==s2[i]) dp[i+1][j]|=dp[i][j];\n\n if(c==s2[j]) dp[i][j+1]|=dp[i][j];\n\n }\n\n }\n\n }\n\n for(int i=0;i\n\n#include \n\nusing namespace std;\n\n\n\nint main()\n\n{\n\n map m;\n\n m[1] = 2;\n\n m[2] = 5;\n\n m[3] = 5;\n\n m[4] = 4;\n\n m[5] = 5;\n\n m[6] = 6;\n\n m[7] = 3;\n\n m[8] = 7;\n\n m[9] = 6;\n\n m[0] = 6;\n\n int n,f;\n\n string s;\n\n cin>>n;\n\n for(int i = 0; i < n; i++)\n\n {\n\n s = \"\";\n\n f = 0;\n\n int tmp;\n\n cin>>tmp;\n\n int res = tmp\/2;\n\n if(tmp%2 == 1)\n\n {\n\n res -=1;\n\n f = 1;\n\n }\n\n for(int j = 0; j < res; j++)\n\n {\n\n s += \"1\";\n\n }\n\n if(f)\n\n s = \"7\" + s;\n\n cout<\n\n\/\/#define int long long\n\nusing namespace std;\n\nconst int mod = 998244353;\n\n\n\nnamespace FastIO {\n\n\ttemplateinline T read(){T x=0,w=1;char c=0;while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+(c-'0');c=getchar();}return x*w;}\n\n\ttemplateinline void write(T x){if(x<0){x=-x;putchar('-');}if(x>9)write(x\/10);putchar(x%10+'0');}\n\n\tinline int getInt(){return read();}\n\n\tinline int putInt(int x,char c){write(x),putchar(c);return 0;}\n\n} namespace Number_Theory {\n\n\tint pw(int a,int b,int p=mod){int res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}\n\n\tint gcd(int x,int y){return!y?x:gcd(y,x%y);}\n\n\tvoid exgcd(int a,int b,int&x,int&y){if(!b){x=1,y=0;return;}exgcd(b,a%b,y,x);y-=(a\/b*x);}\n\n\tint inv(int x,int p=mod){exgcd(x,p,x,*new int);return(x%p+p)%p;}\n\n\tint bsgs(int a,int b,int p){mapval;int A=1,t=sqrt(p)+1;for(int i=0;i\n#include \n\nusing namespace std;\n\nint main()\n{\n int n;\n vector r;\n vector b;\n int rw=0;\n int bw=0;\n int ans;\n cin>>n;\n for(int i = 0; i>r_i;\n r.push_back(r_i);\n }\n for(int i = 0; i>b_i;\n b.push_back(b_i);\n }\n for(int i = 0; ib[i]){\n rw+=1;\n \n }\n if(b[i]>r[i]){\n bw+=1;\n \n }\n }\n \n bw+=1;\n if(rw==0){\n ans=-1;\n }\n else{\n if(bw%rw==0){\n ans=bw\/rw;\n }\n else{\n ans=bw\/rw+1;\n }\n }\n cout<\n\nusing namespace std;\n\nconst int N=2010;\n\n\n\nint l[N],s[N],c[N<<1],f[N<<1][N];\n\n\n\nint main()\n\n{\n\n\tint n,m;\n\n\tscanf(\"%d%d\",&n,&m);\n\n\tm=n+m;\n\n\tfor (int i=n;i;i--)\n\n\t\tscanf(\"%d\",&l[i]);\n\n\tfor (int i=n;i;i--)\n\n\t\tscanf(\"%d\",&s[i]);\n\n\tfor (int i=1;i<=m;i++)\n\n\t\tscanf(\"%d\",&c[i]);\n\n\tmemset(f,0xaf,sizeof(f));\n\n\tfor (int i=0;i<=m;i++)\n\n\t\tf[i][0]=0;\n\n\tfor (int i=1;i<=n;i++)\n\n\t{\n\n\t\tfor (int j=n;j;j--)\n\n\t\t\tf[l[i]][j]=max(f[l[i]][j],f[l[i]][j-1]+c[l[i]]-s[i]);\n\n\t\tfor (int j=l[i];j<=m;j++)\n\n\t\t\tfor (int k=0;k<=n>>j-l[i];k++)\n\n\t\t\t\tf[j+1][k\/2]=max(f[j+1][k\/2],f[j][k]+k\/2*c[j+1]);\n\n\t}\n\n\tprintf(\"%d\",f[m][0]);\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1294","problem_id":"E","statement":"E. Obtain a Permutationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a rectangular matrix of size n\u00d7mn\u00d7m consisting of integers from 11 to 2\u22c51052\u22c5105.In one move, you can: choose any element of the matrix and change its value to any integer between 11 and n\u22c5mn\u22c5m, inclusive; take any column and shift it one cell up cyclically (see the example of such cyclic shift below). A cyclic shift is an operation such that you choose some jj (1\u2264j\u2264m1\u2264j\u2264m) and set a1,j:=a2,j,a2,j:=a3,j,\u2026,an,j:=a1,ja1,j:=a2,j,a2,j:=a3,j,\u2026,an,j:=a1,j simultaneously. Example of cyclic shift of the first column You want to perform the minimum number of moves to make this matrix look like this: In other words, the goal is to obtain the matrix, where a1,1=1,a1,2=2,\u2026,a1,m=m,a2,1=m+1,a2,2=m+2,\u2026,an,m=n\u22c5ma1,1=1,a1,2=2,\u2026,a1,m=m,a2,1=m+1,a2,2=m+2,\u2026,an,m=n\u22c5m (i.e. ai,j=(i\u22121)\u22c5m+jai,j=(i\u22121)\u22c5m+j) with the minimum number of moves performed.InputThe first line of the input contains two integers nn and mm (1\u2264n,m\u22642\u22c5105,n\u22c5m\u22642\u22c51051\u2264n,m\u22642\u22c5105,n\u22c5m\u22642\u22c5105) \u2014 the size of the matrix.The next nn lines contain mm integers each. The number at the line ii and position jj is ai,jai,j (1\u2264ai,j\u22642\u22c51051\u2264ai,j\u22642\u22c5105).OutputPrint one integer \u2014 the minimum number of moves required to obtain the matrix, where a1,1=1,a1,2=2,\u2026,a1,m=m,a2,1=m+1,a2,2=m+2,\u2026,an,m=n\u22c5ma1,1=1,a1,2=2,\u2026,a1,m=m,a2,1=m+1,a2,2=m+2,\u2026,an,m=n\u22c5m (ai,j=(i\u22121)m+jai,j=(i\u22121)m+j).ExamplesInputCopy3 3\n3 2 1\n1 2 3\n4 5 6\nOutputCopy6\nInputCopy4 3\n1 2 3\n4 5 6\n7 8 9\n10 11 12\nOutputCopy0\nInputCopy3 4\n1 6 3 4\n5 10 7 8\n9 2 11 12\nOutputCopy2\nNoteIn the first example; you can set a1,1:=7,a1,2:=8a1,1:=7,a1,2:=8 and a1,3:=9a1,3:=9 then shift the first, the second and the third columns cyclically, so the answer is 66. It can be shown that you cannot achieve a better answer.In the second example, the matrix is already good so the answer is 00.In the third example, it is enough to shift the second column cyclically twice to obtain a good matrix, so the answer is 22.","tags":["greedy","implementation","math"],"code":"#include \n\nusing namespace std;\n\n\n\n#define YES cout << \"YES\" << endl\n\n#define NO cout << \"NO\" << endl\n\n#define all(_) _.begin(), _.end()\n\n#define rall(_) _.rbegin(), _.rend()\n\n#define sz(_) (int)_.size()\n\n#define fs first\n\n#define se second\n\n#define Odd(_x) ((_x) & 1)\n\nusing i64 = long long;\n\nusing ll = long long;\n\n\/\/mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nconst int inf = 1e9 + 10;\n\nconst ll mod = 1e9 + 7;\n\n\n\nvoid solve() {\n\n int n, m;\n\n cin >> n >> m;\n\n vector a(n, vector(m ));\n\n for (int i = 0; i < n; i++) {\n\n for (int j = 0; j < m; j++) {\n\n cin >> a[i][j];\n\n a[i][j]--;\n\n }\n\n }\n\n int ans = 0;\n\n for (int i = 0; i < m; i++) {\n\n vector cnt(n);\n\n for (int j = 0; j < n; j++) {\n\n if (a[j][i] % m == i && a[j][i] < n * m) {\n\n cnt[(j - a[j][i] \/ m + n) % n] += 1;\n\n }\n\n }\n\n int res = n;\n\n for (int i = 0; i < n; i++) {\n\n res = min(res, n - cnt[i] + i);\n\n }\n\n ans += res;\n\n }\n\n cout << ans << \"\\n\";\n\n}\n\n\/\/#define MULTI_INPUT\n\n\n\nint main() {\n\n#ifndef ONLINE_JUDGE\n\n freopen(R\"(D:\\source files\\source file2\\input.txt)\", \"r\", stdin);\n\n freopen(R\"(D:\\source files\\source file2\\output.txt)\", \"w\", stdout);\n\n#endif\n\n std::ios::sync_with_stdio(false);\n\n cin.tie(nullptr);\n\n cout << fixed << setprecision(20);\n\n#ifdef MULTI_INPUT\n\n int T;\n\n cin >> T;\n\n while (T--) {\n\n solve();\n\n }\n\n#else\n\n solve();\n\n#endif\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1325","problem_id":"E","statement":"E. Ehab's REAL Number Theory Problemtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.A sequence aa is a subsequence of an array bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements.InputThe first line contains an integer nn (1\u2264n\u22641051\u2264n\u2264105)\u00a0\u2014 the length of aa.The second line contains nn integers a1a1, a2a2, \u2026\u2026, anan (1\u2264ai\u22641061\u2264ai\u2264106)\u00a0\u2014 the elements of the array aa.OutputOutput the length of the shortest non-empty subsequence of aa product of whose elements is a perfect square. If there are several shortest subsequences, you can find any of them. If there's no such subsequence, print \"-1\".ExamplesInputCopy3\n1 4 6\nOutputCopy1InputCopy4\n2 3 6 6\nOutputCopy2InputCopy3\n6 15 10\nOutputCopy3InputCopy4\n2 3 5 7\nOutputCopy-1NoteIn the first sample; you can choose a subsequence [1][1].In the second sample, you can choose a subsequence [6,6][6,6].In the third sample, you can choose a subsequence [6,15,10][6,15,10].In the fourth sample, there is no such subsequence.","tags":["brute force","dfs and similar","graphs","number theory","shortest paths"],"code":"\/\/ Judges with GCC >= 12 only needs Ofast\n\n\/\/ #pragma GCC optimize(\"O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize\")\n\n\/\/ MLE optimization\n\n\/\/ #pragma GCC optimize(\"conserve-stack\")\n\n\/\/ Old judges\n\n\/\/ #pragma GCC target(\"sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2\")\n\n\/\/ New judges. Test with assert(__builtin_cpu_supports(\"avx2\"));\n\n\/\/ #pragma GCC target(\"avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native\")\n\n\/\/ Atcoder\n\n\/\/ #pragma GCC target(\"avx2,popcnt,lzcnt,abm,bmi,bmi2,fma\")\n\n#include\n\nusing namespace std;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nuniform_real_distribution<> pp(0.0,1.0);\n\n#define ld long double\n\n#define pii pair\n\n#define piii pair\n\n#define fi first\n\n#define se second\n\nconst int inf=1e9;\n\nconst int mod=998244353;\n\nconst int mod2=1e9+7;\n\nconst int maxn=100005;\n\nconst int maxm=200005;\n\nconst int maxq=500005;\n\nconst int maxl=20;\n\nconst int maxa=1000000;\n\nint power(int a,int n){\n\n int res=1;\n\n while(n){\n\n if(n&1) res=res*a%mod;\n\n a=a*a%mod;n>>=1;\n\n }\n\n return res;\n\n}\n\nint n,num,a[maxn],prime[maxa+5],pos[maxa+5],cn[maxn],from[maxn],dist[maxn];\n\nvector edge[maxn];\n\nint query(int s){\n\n int res=inf;\n\n for(int i=1;i<=num;i++){dist[i]=-1;from[i]=-1;}\n\n queue q;q.push(s);dist[s]=0;from[s]=0;\n\n while(!q.empty()){\n\n int u=q.front();q.pop();\n\n for(pii v:edge[u]){\n\n if(dist[v.fi]!=-1){\n\n if(from[u]!=v.se) res=min(res,dist[u]+dist[v.fi]+1);\n\n continue;\n\n }\n\n dist[v.fi]=dist[u]+1;from[v.fi]=v.se;\n\n q.push(v.fi);\n\n }\n\n }\n\n return res;\n\n}\n\nvoid solve(){\n\n for(int i=2;i<=maxa;i++){\n\n if(prime[i]==0){\n\n prime[i]=i;pos[i]=++num;\n\n for(int j=min(i,maxa\/i+1)*i;j<=maxa;j+=i) prime[j]=i;\n\n }\n\n }\n\n cin >> n;\n\n for(int i=1;i<=n;i++){\n\n cin >> a[i];\n\n int d=a[i],p=1,q=1;\n\n while(d>1){\n\n int j=prime[d],cnt=0;\n\n while(d%j==0){d\/=j;cnt^=1;}\n\n if(cnt){\n\n if(p==1) p=j;\n\n else q=j;\n\n }\n\n }\n\n if(p==1){\n\n cout << 1 << '\\n';\n\n return;\n\n }\n\n else if(q==1) cn[pos[p]]++;\n\n else{\n\n edge[pos[p]].push_back({pos[q],i});\n\n edge[pos[q]].push_back({pos[p],i});\n\n }\n\n }\n\n queue q;\n\n for(int i=1;i<=num;i++){\n\n dist[i]=-1;\n\n if(cn[i]){\n\n q.push(i);\n\n dist[i]=1;from[i]=i;\n\n }\n\n if(cn[i]>=2){\n\n cout << 2 << '\\n';\n\n return;\n\n }\n\n }\n\n while(!q.empty()){\n\n int u=q.front();q.pop();\n\n for(pii v:edge[u]){\n\n if(dist[v.fi]!=-1) continue;\n\n dist[v.fi]=dist[u]+1;from[v.fi]=from[u];\n\n q.push(v.fi);\n\n }\n\n }\n\n int res=inf;\n\n for(int i=1;i<=num;i++){\n\n for(pii v:edge[i]){\n\n if(dist[i]!=-1 && dist[v.fi]!=-1 && from[i]!=from[v.fi]) res=min(res,dist[i]+dist[v.fi]+1);\n\n }\n\n }\n\n for(int i=1;i*i<=maxa;i++){\n\n if(pos[i]==0) continue;\n\n res=min(res,query(pos[i]));\n\n }\n\n cout << (res==inf?-1:res) << '\\n';\n\n}\n\nsigned main(){\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(NULL);cout.tie(NULL);\n\n int test=1;\/\/cin >> test;\n\n while(test--) solve();\n\n}\n\n","language":"cpp"} -{"contest_id":"1321","problem_id":"C","statement":"C. Remove Adjacenttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string ss consisting of lowercase Latin letters. Let the length of ss be |s||s|. You may perform several operations on this string.In one operation, you can choose some index ii and remove the ii-th character of ss (sisi) if at least one of its adjacent characters is the previous letter in the Latin alphabet for sisi. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index ii should satisfy the condition 1\u2264i\u2264|s|1\u2264i\u2264|s| during each operation.For the character sisi adjacent characters are si\u22121si\u22121 and si+1si+1. The first and the last characters of ss both have only one adjacent character (unless |s|=1|s|=1).Consider the following example. Let s=s= bacabcab. During the first move, you can remove the first character s1=s1= b because s2=s2= a. Then the string becomes s=s= acabcab. During the second move, you can remove the fifth character s5=s5= c because s4=s4= b. Then the string becomes s=s= acabab. During the third move, you can remove the sixth character s6=s6='b' because s5=s5= a. Then the string becomes s=s= acaba. During the fourth move, the only character you can remove is s4=s4= b, because s3=s3= a (or s5=s5= a). The string becomes s=s= acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.InputThe first line of the input contains one integer |s||s| (1\u2264|s|\u22641001\u2264|s|\u2264100) \u2014 the length of ss.The second line of the input contains one string ss consisting of |s||s| lowercase Latin letters.OutputPrint one integer \u2014 the maximum possible number of characters you can remove if you choose the sequence of moves optimally.ExamplesInputCopy8\nbacabcab\nOutputCopy4\nInputCopy4\nbcda\nOutputCopy3\nInputCopy6\nabbbbb\nOutputCopy5\nNoteThe first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only; but it can be shown that the maximum possible answer to this test is 44.In the second example, you can remove all but one character of ss. The only possible answer follows. During the first move, remove the third character s3=s3= d, ss becomes bca. During the second move, remove the second character s2=s2= c, ss becomes ba. And during the third move, remove the first character s1=s1= b, ss becomes a. ","tags":["brute force","constructive algorithms","greedy","strings"],"code":"#include\n\nusing namespace std;\nint main()\n{\nint n; string s;\ncin>>n>>s;\nfor(int i='z';i>'a';i--)\nfor(int k=0;k\n\nusing namespace std;\n\nint main()\n{\n int t;\n cin>>t;\n while(t--)\n {\n int n;\n cin>>n;\n int a[n];\n int x=0,y,z=0,c[2];\n for(int i=0;i>a[i];\n if(a[i]%2==0)\n {\n x=1;\n y=i;\n }\n else\n {\n if(z==0)\n {\n c[0]=i;\n z++;\n }\n else if(z==1)\n {\n c[1]=i;\n z++;\n }\n }\n }\n if(x==1)\n {\n cout<<1<<\"\\n\"<=2)\n {\n cout<<2<<\"\\n\"<\n\nusing namespace std;\n\n#define oo 1000000010\n\n#define mod 1000000007\n\nconst int N = 300010;\n\nint n , arr[N] ; \n\n \n\nvoid solve(){\n\n int mn = oo , mx = -oo;\n\n\tscanf(\"%d\",&n);\n\n\tfor(int i=0;i 0 && arr[i] == -1 && arr[i - 1] != -1)\n\n\t\t\tmn = min(mn , arr[i - 1]) , mx = max(mx , arr[i - 1]);\n\n\t\tif(i < n - 1 && arr[i] == - 1 && arr[i + 1] != -1)\n\n\t\t\tmn = min(mn , arr[i + 1]) , mx = max(mx , arr[i + 1]);\n\n\t}\n\n\tint res = (mx + mn) \/ 2;\n\n\tint ans = 0;\n\n\tfor(int i=0;i> t;\n\n\twhile(t--){\n\n\t solve();\n\n\t}\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1288","problem_id":"D","statement":"D. Minimax Problemtime limit per test5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given nn arrays a1a1, a2a2, ..., anan; each array consists of exactly mm integers. We denote the yy-th element of the xx-th array as ax,yax,y.You have to choose two arrays aiai and ajaj (1\u2264i,j\u2264n1\u2264i,j\u2264n, it is possible that i=ji=j). After that, you will obtain a new array bb consisting of mm integers, such that for every k\u2208[1,m]k\u2208[1,m] bk=max(ai,k,aj,k)bk=max(ai,k,aj,k).Your goal is to choose ii and jj so that the value of mink=1mbkmink=1mbk is maximum possible.InputThe first line contains two integers nn and mm (1\u2264n\u22643\u22c51051\u2264n\u22643\u22c5105, 1\u2264m\u226481\u2264m\u22648) \u2014 the number of arrays and the number of elements in each array, respectively.Then nn lines follow, the xx-th line contains the array axax represented by mm integers ax,1ax,1, ax,2ax,2, ..., ax,max,m (0\u2264ax,y\u22641090\u2264ax,y\u2264109).OutputPrint two integers ii and jj (1\u2264i,j\u2264n1\u2264i,j\u2264n, it is possible that i=ji=j) \u2014 the indices of the two arrays you have to choose so that the value of mink=1mbkmink=1mbk is maximum possible. If there are multiple answers, print any of them.ExampleInputCopy6 5\n5 0 3 1 2\n1 8 9 1 3\n1 2 3 4 5\n9 1 0 3 7\n2 3 0 6 3\n6 4 1 7 0\nOutputCopy1 5\n","tags":["binary search","bitmasks","dp"],"code":"#include\n\ntypedef long long ll;\n\ntypedef unsigned long long ull;\n\nusing namespace std;\n\nconst ll mod = 1e9+7;\n\nconst int mm = 3e5 + 10;\n\nll a[mm][10];\n\nll n,m;\n\nmap>anss;\n\nbool check(ll x){\n\n\tmapmp;\n\n\tfor(int i=1;i<=n;i++){\n\n\t\tll bj=0;\n\n\t\tint jj=1;\n\n\t\tfor(int j=1;j<=m;j++){\n\n\t\t\tif(a[i][j]>=x)bj+=(1ll<<(j-1));\n\n\t\t\telse jj=0;\n\n\t\t}\n\n\t\tmp[bj]=i;\n\n\t\tif(jj){\n\n\t\t\tanss[x].first=anss[x].second=i;\n\n\t\t\treturn 1;\n\n\t\t}\n\n\t}\n\n\tll xx=(1ll<>n>>m;\n\n\tll ma=0;\n\n\tfor(int i=1;i<=n;i++){\n\n\t\tfor(int j=1;j<=m;j++){\n\n\t\t\tcin>>a[i][j];\n\n\t\t\tma=max(ma,a[i][j]);\n\n\t\t}\n\n\t}\n\n\tll l=0,r=ma;\n\n\tll ans=0;\n\n\twhile(l<=r){\n\n\t\tint m=(l+r)>>1;\n\n\t\tif(check(m))l=m+1,ans=m;\n\n\t\telse r=m-1;\n\n\t}cout<\n\n \n\nusing namespace std;\n\n \n\nconst int maxn = 2e5 +5;\n\n \n\nint n , dis[maxn] , a , b , c , dadi[maxn];\n\nvector masir;\n\nbool mark[maxn];\n\nvector adj[maxn];\n\nqueue q;\n\n \n\nvoid dfs(int u , int dad){\n\n mark[u] = true;\n\n dis[u] = dis[dad]+1;\n\n for (int i = 0; i < adj[u].size(); ++i) {\n\n int v = adj[u][i];\n\n if (!mark[v]){\n\n dadi[v] = u;\n\n dfs(v , u);\n\n }\n\n }\n\n}\n\n \n\nvoid bfs(){\n\n while(q.size()){\n\n int u = q.front();\n\n q.pop();\n\n for (auto v : adj[u]) {\n\n if (!mark[v]){\n\n q.push(v);\n\n mark[v] = true;\n\n \n\n dis[v] = dis[u] + 1;\n\n }\n\n }\n\n }\n\n}\n\n \n\n \n\nint main() {\n\n ios_base::sync_with_stdio(false);\n\n cin >> n;\n\n for (int i = 0; i < n-1; ++i) {\n\n int u , v;\n\n cin >> u >> v;\n\n adj[v].push_back(u);\n\n adj[u].push_back(v);\n\n }\n\n dfs(1 , 0);\n\n \n\n for (int i = 1; i <= n; ++i) {\n\n dis[i]--;\n\n }\n\n int maxi = 0;\n\n int f = 0;\n\n for (int i = 1; i <=n ; ++i) {\n\n if (dis[i] > maxi){\n\n maxi = dis[i];\n\n f = i;\n\n }\n\n }\n\n a = f;\n\n for (int i = 1; i <= n; ++i) {\n\n dis[i] = 0;\n\n mark[i] = false;\n\n dadi[i] = 0;\n\n }\n\n \n\n dfs(a , 0);\n\n \n\n maxi = 0;\n\n f = 0;\n\n \n\n for (int i = 1 ; i <= n ; i++){\n\n if (dis[i] > maxi){\n\n maxi = dis[i];\n\n f = i;\n\n }\n\n }\n\n b = f;\n\n int B = b;\n\n masir.push_back(B);\n\n while(B != a){\n\n masir.push_back(dadi[B]);\n\n B = dadi[B];\n\n }\n\n \n\n for (int i = 0; i <= n; ++i) {\n\n mark[i] = false;\n\n dis[i] = 0;\n\n }\n\n \n\n for (int i = 0; i < masir.size(); ++i) {\n\n mark[masir[i]] = true;\n\n q.push(masir[i]);\n\n dis[masir[i]] = 0;\n\n \n\n }\n\n \n\n \n\n bfs();\n\n \n\n \n\n maxi = -1;\n\n f = 0;\n\n for (int i = 1; i <= n; ++i) {\n\n if (maxi < dis[i] and i!= a and i!= b){\n\n maxi = dis[i];\n\n f = i;\n\n }\n\n }\n\n c = f;\n\n cout << masir.size() + maxi-1 << endl;\n\n cout << a << \" \" << b << \" \" << c;\n\n \n\n \n\n \n\n}","language":"cpp"} -{"contest_id":"13","problem_id":"E","statement":"E. Holestime limit per test1 secondmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes to play a lot. Most of all he likes to play a game \u00abHoles\u00bb. This is a game for one person with following rules:There are N holes located in a single row and numbered from left to right with numbers from 1 to N. Each hole has it's own power (hole number i has the power ai). If you throw a ball into hole i it will immediately jump to hole i\u2009+\u2009ai; then it will jump out of it and so on. If there is no hole with such number, the ball will just jump out of the row. On each of the M moves the player can perform one of two actions: Set the power of the hole a to value b. Throw a ball into the hole a and count the number of jumps of a ball before it jump out of the row and also write down the number of the hole from which it jumped out just before leaving the row. Petya is not good at math, so, as you have already guessed, you are to perform all computations.InputThe first line contains two integers N and M (1\u2009\u2264\u2009N\u2009\u2264\u2009105, 1\u2009\u2264\u2009M\u2009\u2264\u2009105) \u2014 the number of holes in a row and the number of moves. The second line contains N positive integers not exceeding N \u2014 initial values of holes power. The following M lines describe moves made by Petya. Each of these line can be one of the two types: 0 a b 1 a Type 0 means that it is required to set the power of hole a to b, and type 1 means that it is required to throw a ball into the a-th hole. Numbers a and b are positive integers do not exceeding N.OutputFor each move of the type 1 output two space-separated numbers on a separate line \u2014 the number of the last hole the ball visited before leaving the row and the number of jumps it made.ExamplesInputCopy8 51 1 1 1 1 2 8 21 10 1 31 10 3 41 2OutputCopy8 78 57 3","tags":["data structures","dsu"],"code":"\/\/#pragma GCC optomize (\"Ofast\")\n\n\/\/#pragma GCC optomize (\"unroll-loops\")\n\n\/\/#pragma GCC target (\"avx,avx2,fma\")\n\n#include \n\n \n\n#define F first\n\n#define S second \n\n#define ll long long\n\n#define all(x) (x.begin(), x.end());\n\n#define uint unsigned int\n\n#define pb push_back\n\n#define\tios\tios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n\n\/\/#define int ll\n\n \n\nusing namespace std;\n\n \n\nconst int N = 1e5+9;\n\n\/\/const ll INF = 1e18 , inf = 1e9 , mod = 1e9+7;\n\n\n\nint n,m;\n\nint a[N];\n\nint cnt[N];\n\nint last[N];\n\n\n\nint BLOCK=300;\n\n\n\nvoid upd(int x){\n\n\tint l=x*BLOCK,r=min(l+BLOCK,n)-1;\n\n\tfor(int i=r;i>=l;i--){\n\n\t\tif(i+a[i]>r){\n\n\t\t\tcnt[i]=1;\n\n\t\t\tlast[i]=i;\n\n\t\t}\n\n\t\telse{\n\n\t\t\tcnt[i]=cnt[i+a[i]]+1;\n\n\t\t\tlast[i]=last[i+a[i]];\n\n\t\t}\n\n\t}\n\n}\n\n\n\nsigned main(){\n\n\/\/ \tfreopen(\".in\", \"r\", stdin);\n\n\/\/ \tfreopen(\".out\", \"w\", stdout);\n\n ios;\n\n\tint tt=1;\n\n\/\/\tcin>>tt;\n\n\twhile(tt--){\n\n\t\tcin>>n>>m;\n\n\t\tfor(int i=0;i>a[i];\n\n\t\tfor(int i=0;i*BLOCK>q;\n\n\t\t\tif(q==0){\n\n\t\t\t\tint ind,val;\n\n\t\t\t\tcin>>ind>>val;\n\n\t\t\t\tind--;\n\n\t\t\t\ta[ind]=val;\n\n\t\t\t\tupd(ind\/BLOCK);\n\n\t\t\t}\n\n\t\t\telse{\n\n\t\t\t\tint ind;\n\n\t\t\t\tcin>>ind;\n\n\t\t\t\tint ans=0;\n\n\t\t\t\tind--;\n\n\t\t\t\twhile(ind\n\n#define int long long\n\nusing namespace std;\n\nconst int maxn=150005,inf=1e18;\n\nint n,a[maxn],pos[maxn];\n\nstruct vl \n\n{int sum,mx,sc,cnt;\n\nvl(){sum=0;mx=sc=-inf;cnt=0;}\n\n};\n\ninline vl merge(vl a,vl b)\n\n{\n\n vl ret;\n\n ret.sum=a.sum+b.sum;\n\n ret.mx=max(a.mx,b.mx);\n\n ret.sc=-inf;\n\n ret.cnt=0;\n\n if(a.mx!=ret.mx)ret.sc=max(ret.sc,a.mx);\n\n else ret.cnt+=a.cnt;\n\n if(b.mx!=ret.mx)ret.sc=max(ret.sc,b.mx);\n\n else ret.cnt+=b.cnt;\n\n if(a.sc!=ret.mx)ret.sc=max(ret.sc,a.sc);\n\n if(b.sc!=ret.mx)ret.sc=max(ret.sc,b.sc);\n\n return ret;\n\n}\n\nvl val[maxn<<2];\n\nint tag[maxn<<2],add[maxn<<2],len[maxn<<2];\n\ninline void pushup(int i)\n\n{\n\n val[i]=merge(val[i<<1],val[i<<1|1]);\n\n len[i]=len[i<<1]+len[i<<1|1];\n\n}\n\ninline void pushtgmin(int i,int v)\n\n{\n\n if(v>=val[i].mx)return;\n\n val[i].sum-=(val[i].mx-v)*val[i].cnt;\n\n val[i].mx=v;\n\n tag[i]=min(tag[i],v);\n\n return;\n\n}\n\ninline void pushtgadd(int i,int v)\n\n{\n\n add[i]+=v;\n\n val[i].sum+=len[i]*v;\n\n if(tag[i]!=inf)tag[i]+=v;\n\n if(val[i].mx!=-inf)val[i].mx+=v;\n\n if(val[i].sc!=-inf)val[i].sc+=v; \n\n}\n\ninline void pushdown(int i)\n\n{\n\n pushtgadd(i<<1,add[i]);\n\n pushtgadd(i<<1|1,add[i]);\n\n add[i]=0;\n\n pushtgmin(i<<1,tag[i]);\n\n pushtgmin(i<<1|1,tag[i]);\n\n tag[i]=inf;\n\n}\n\nvoid update(int i,int l,int r,int L,int R,int v)\n\n{\n\n if(L>R)return;\n\n if(L<=l&&r<=R){pushtgadd(i,v);return;}\n\n int mid=l+r>>1;pushdown(i);\n\n if(L<=mid)update(i<<1,l,mid,L,R,v);\n\n if(R>mid)update(i<<1|1,mid+1,r,L,R,v);\n\n pushup(i);\n\n}\n\nvoid calcmin(int i,int l,int r,int L,int R,int v)\n\n{\n\n \/\/if(i==1)printf(\"#%d %d %d\\n\",L,R,v);\n\n if(rR||L>R)return;\n\n if(L<=L&&r<=R&&val[i].sc>1;pushdown(i);\n\n calcmin(i<<1,l,mid,L,R,v);\n\n calcmin(i<<1|1,mid+1,r,L,R,v);\n\n pushup(i);\n\n}\n\nvoid build(int i,int l,int r)\n\n{\n\n len[i]=0;\n\n val[i].cnt=val[i].sum=0;\n\n val[i].mx=val[i].sc=-inf;\n\n tag[i]=inf;add[i]=0;\n\n if(l==r)return;\n\n int mid=l+r>>1;\n\n build(i<<1,l,mid);\n\n build(i<<1|1,mid+1,r);\n\n}\n\nint query(int i,int l,int r,int p)\n\n{\n\n if(l==r)return val[i].sum;\n\n int mid=l+r>>1;\n\n pushdown(i);\n\n if(p<=mid)return query(i<<1,l,mid,p);\n\n else return query(i<<1|1,mid+1,r,p);\n\n}\n\nvoid modify(int i,int l,int r,int p,int v)\n\n{\n\n if(l==r)\n\n {\n\n val[i].cnt=1;\n\n val[i].mx=v;\n\n val[i].sc=-inf;\n\n val[i].sum=v;\n\n len[i]=1;\n\n return;\n\n }\n\n int mid=l+r>>1;\n\n pushdown(i);\n\n if(p<=mid)modify(i<<1,l,mid,p,v);\n\n else modify(i<<1|1,mid+1,r,p,v);\n\n pushup(i);\n\n}\n\nint c[maxn];\n\nint num[maxn];\n\nvoid uadd(int p,int v){while(p<=n){c[p]+=v;p+=p&-p;}}\n\nint ask(int p){int ret=0;while(p){ret+=c[p];p-=p&-p;}return ret;}\n\nint sum[maxn];\n\nsigned main()\n\n{\n\n scanf(\"%lld\",&n);\n\n for(int i=1;i<=n;i++)\n\n scanf(\"%lld\",&a[i]);\n\n for(int i=1;i<=n;i++)\n\n pos[a[i]]=i,num[a[i]]=ask(a[i]),uadd(a[i],1);\n\n build(1,1,n);\n\n for(int i=1;i<=n;i++)\n\n { \n\n update(1,1,n,pos[i]+1,n,1);\n\n calcmin(1,1,n,1,pos[i]-1,num[i]);\n\n modify(1,1,n,pos[i],i);\n\n \n\n int S=0;\n\n \/\/for(int j=1,x;j<=n;j++)\n\n \/\/ if(a[j]<=i)\n\n \/\/ printf(\"%d \",(x=query(1,1,n,j))),S+=x;\n\n \/\/ putchar('\\n');\n\n \/\/printf(\"%d\\n\",val[1].sum);\n\n sum[i]+=val[1].sum;\n\n }\n\n reverse(a+1,a+1+n);\n\n memset(c,0,sizeof c);\n\n for(int i=1;i<=n;i++)\n\n pos[a[i]]=i,num[a[i]]=ask(a[i]),uadd(a[i],1);\n\n build(1,1,n);\n\n for(int i=1;i<=n;i++)\n\n {\n\n update(1,1,n,pos[i]+1,n,1);\n\n calcmin(1,1,n,1,pos[i]-1,num[i]);\n\n modify(1,1,n,pos[i],i);\n\n \n\n \/\/ int S=0;\n\n \/\/ for(int j=n,x;j>=1;j--)\n\n \/\/ if(a[j]<=i)\n\n \/\/ printf(\"%d \",i-(x=query(1,1,n,j))+1),S+=x;\n\n \/\/ putchar('\\n');\n\n \/\/printf(\"%d\\n\",val[1].sum);\n\n sum[i]-=i*i-val[1].sum;\n\n }\n\n for(int i=1;i<=n;i++)\n\n printf(\"%lld\\n\",sum[i]);\n\n \/\/ putchar('\\n');\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1293","problem_id":"A","statement":"A. ConneR and the A.R.C. Markland-Ntime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSakuzyo - ImprintingA.R.C. Markland-N is a tall building with nn floors numbered from 11 to nn. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin \"ConneR\" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor ss of the building. On each floor (including floor ss, of course), there is a restaurant offering meals. However, due to renovations being in progress, kk of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!InputThe first line contains one integer tt (1\u2264t\u226410001\u2264t\u22641000)\u00a0\u2014 the number of test cases in the test. Then the descriptions of tt test cases follow.The first line of a test case contains three integers nn, ss and kk (2\u2264n\u22641092\u2264n\u2264109, 1\u2264s\u2264n1\u2264s\u2264n, 1\u2264k\u2264min(n\u22121,1000)1\u2264k\u2264min(n\u22121,1000))\u00a0\u2014 respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants.The second line of a test case contains kk distinct integers a1,a2,\u2026,aka1,a2,\u2026,ak (1\u2264ai\u2264n1\u2264ai\u2264n)\u00a0\u2014 the floor numbers of the currently closed restaurants.It is guaranteed that the sum of kk over all test cases does not exceed 10001000.OutputFor each test case print a single integer\u00a0\u2014 the minimum number of staircases required for ConneR to walk from the floor ss to a floor with an open restaurant.ExampleInputCopy5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77\nOutputCopy2\n0\n4\n0\n2\nNoteIn the first example test case; the nearest floor with an open restaurant would be the floor 44.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the 66-th floor.","tags":["binary search","brute force","implementation"],"code":"#include \n\n#include \n\n#include \n\n\n\n#define ar array\n\n#define vt vector\n\n#define pq priority_queue\n\n#define pu push\n\n#define pub push_back\n\n#define em emplace\n\n#define emb emplace_back\n\n\n\n#define all(x) x.begin(), x.end()\n\n#define allr(x) x.rbegin(), x.rend()\n\n#define allp(x, l, r) x.begin() + l, x.begin() + r\n\n#define len(x) (int)x.size()\n\n\n\nusing namespace std;\n\nusing namespace __gnu_pbds;\n\n\n\nusing ll = long long;\n\nusing ld = long double;\n\nusing ull = unsigned long long;\n\n\n\ntemplate \n\nvoid re(array & x);\n\ntemplate \n\nvoid re(vt & x);\n\n\n\ntemplate \n\nvoid re(T& x) {\n\n cin >> x;\n\n}\n\n\n\ntemplate \n\nvoid re(T& x, M&... args) {\n\n re(x); re(args...);\n\n}\n\n\n\ntemplate \n\nvoid re(vt & x) {\n\n for(auto& it : x)\n\n re(it);\n\n}\n\n\n\ntemplate \n\nvoid re(array & x) {\n\n for(auto& it : x)\n\n re(it);\n\n}\n\n\n\ntemplate \n\nvoid wr(array x);\n\ntemplate \n\nvoid wr(vt x);\n\n\n\ntemplate \n\nvoid wr(T x) {\n\n cout << x;\n\n}\n\n\n\ntemplate void wr(T x, M... args) {\n\n wr(x), wr(args...);\n\n}\n\n\n\ntemplate \n\nvoid wr(vt x) {\n\n for(auto it : x)\n\n wr(it, ' ');\n\n}\n\n\n\ntemplate \n\nvoid wr(array x) {\n\n for(auto it : x)\n\n wr(it, ' ');\n\n}\n\n\n\n\n\ninline void Open(const string Name) {\n\n #ifndef ONLINE_JUDGE\n\n (void)!freopen((Name + \".in\").c_str(), \"r\", stdin);\n\n (void)!freopen((Name + \".out\").c_str(), \"w\", stdout);\n\n #endif\n\n}\n\n\n\nvoid solve() {\n\n int n, s, k; re(n, s, k);\n\n vt v(k); re(v);\n\n\n\n int res = (int)(1e9);\n\n for (int i = s; i <= n; ++i) {\n\n if (find(all(v), i) == v.end()) {\n\n res = min(res, i - s);\n\n break;\n\n }\n\n }\n\n for (int i = s; i > 0; --i) {\n\n if (find(all(v), i) == v.end()) {\n\n res = min(res, s - i);\n\n break;\n\n }\n\n }\n\n wr(res, '\\n');\n\n}\n\n\n\nint main() {\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(nullptr);\n\n\n\n \/\/Open(\"\");\n\n\n\n int t; re(t);\n\n for(;t;t--) {\n\n solve();\n\n }\n\n \n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1311","problem_id":"A","statement":"A. Add Odd or Subtract Eventime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two positive integers aa and bb.In one move, you can change aa in the following way: Choose any positive odd integer xx (x>0x>0) and replace aa with a+xa+x; choose any positive even integer yy (y>0y>0) and replace aa with a\u2212ya\u2212y. You can perform as many such operations as you want. You can choose the same numbers xx and yy in different moves.Your task is to find the minimum number of moves required to obtain bb from aa. It is guaranteed that you can always obtain bb from aa.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1\u2264t\u22641041\u2264t\u2264104) \u2014 the number of test cases.Then tt test cases follow. Each test case is given as two space-separated integers aa and bb (1\u2264a,b\u22641091\u2264a,b\u2264109).OutputFor each test case, print the answer \u2014 the minimum number of moves required to obtain bb from aa if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain bb from aa.ExampleInputCopy5\n2 3\n10 10\n2 4\n7 4\n9 3\nOutputCopy1\n0\n2\n2\n1\nNoteIn the first test case; you can just add 11.In the second test case, you don't need to do anything.In the third test case, you can add 11 two times.In the fourth test case, you can subtract 44 and add 11.In the fifth test case, you can just subtract 66.","tags":["greedy","implementation","math"],"code":"#include\n\nusing namespace std;\n\nint main()\n\n{\n\n int test;\n\n cin >> test;\n\n\n\n while(test--){\n\n int a, b;\n\n cin >> a >> b;\n\n\n\n if(a == b) cout << 0 << endl;\n\n else if(a\n\n#include \n\nusing std::cin, std::cout;\n\n \n\nint main(){\n\n int t,n,n1,m,a,b,x,y,c;\n\n cin >> t;\n\n while(t--){\n\n cin>> n;\n\n n1 = n;\n\n b=0,a=0,x=0,y=0,m=3;\n\n if(n%2 == 0){\n\n a = 2;\n\n while(n%2 == 0){\n\n n \/= 2;\n\n x++;\n\n }\n\n }\n\n else{\n\n while(!a){\n\n while(n%m == 0){\n\n n \/= m;\n\n x++;\n\n }\n\n if(x != 0){a = m;}\n\n else if(m*m < n1){m += 2;}\n\n else{break;}\n\n }\n\n }\n\n if((n == 1 && x < 6) || m*m > n1){cout<<\"NO\"<<'\\n';}\n\n else if(n == 1){\n\n cout<<\"YES\"<<'\\n';\n\n b = a*a;\n\n c = pow(a,x-3);\n\n cout<< a <<' '<< b <<' '<< c <<'\\n';}\n\n else if(x > 2){\n\n cout<<\"YES\"<<'\\n';\n\n b = pow(a,x-1);\n\n c = n1\/pow(a,x);\n\n cout<< a <<' '<< b <<' '<< c <<'\\n';}\n\n else{\n\n while(!b){\n\n while(n%m == 0){\n\n n \/= m;\n\n y++;\n\n }\n\n if(y != 0){b = m;}\n\n else if(m*m < n1){m += 2;}\n\n else{break;}\n\n }\n\n if((n == 1 && x+y < 4) || m*m > n1){cout<<\"NO\"<<'\\n';}\n\n else if(n == 1 && x==1){\n\n cout<<\"YES\"<<'\\n';\n\n c = pow(b,y-1);\n\n cout<< a <<' '<< b <<' '<< c <<'\\n';}\n\n else if(n == 1 && y==1){\n\n cout<<\"YES\"<<'\\n';\n\n c = pow(a,x-1);\n\n cout<< a <<' '<< b <<' '<< c <<'\\n';}\n\n else if(n == 1){\n\n cout<<\"YES\"<<'\\n';\n\n c = a*b;\n\n a = pow(a,x-1);\n\n b = pow(b,y-1);\n\n cout<< a <<' '<< b <<' '<< c <<'\\n';}\n\n else{\n\n cout<<\"YES\"<<'\\n';\n\n a = pow(a,x);\n\n b = pow(b,y);\n\n cout<< a <<' '<< b <<' '<< n <<'\\n';\n\n }\n\n } \n\n }\n\n}","language":"cpp"} -{"contest_id":"1290","problem_id":"D","statement":"D. Coffee Varieties (hard version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. You can find the easy version in the Div. 2 contest. Both versions only differ in the number of times you can ask your friend to taste coffee.This is an interactive problem.You're considering moving to another city; where one of your friends already lives. There are nn caf\u00e9s in this city, where nn is a power of two. The ii-th caf\u00e9 produces a single variety of coffee aiai. As you're a coffee-lover, before deciding to move or not, you want to know the number dd of distinct varieties of coffees produced in this city.You don't know the values a1,\u2026,ana1,\u2026,an. Fortunately, your friend has a memory of size kk, where kk is a power of two.Once per day, you can ask him to taste a cup of coffee produced by the caf\u00e9 cc, and he will tell you if he tasted a similar coffee during the last kk days.You can also ask him to take a medication that will reset his memory. He will forget all previous cups of coffee tasted. You can reset his memory at most 30\u00a000030\u00a0000 times.More formally, the memory of your friend is a queue SS. Doing a query on caf\u00e9 cc will: Tell you if acac is in SS; Add acac at the back of SS; If |S|>k|S|>k, pop the front element of SS. Doing a reset request will pop all elements out of SS.Your friend can taste at most 3n22k3n22k cups of coffee in total. Find the diversity dd (number of distinct values in the array aa).Note that asking your friend to reset his memory does not count towards the number of times you ask your friend to taste a cup of coffee.In some test cases the behavior of the interactor is adaptive. It means that the array aa may be not fixed before the start of the interaction and may depend on your queries. It is guaranteed that at any moment of the interaction, there is at least one array aa consistent with all the answers given so far.InputThe first line contains two integers nn and kk (1\u2264k\u2264n\u226410241\u2264k\u2264n\u22641024, kk and nn are powers of two).It is guaranteed that 3n22k\u226415\u00a00003n22k\u226415\u00a0000.InteractionYou begin the interaction by reading nn and kk. To ask your friend to taste a cup of coffee produced by the caf\u00e9 cc, in a separate line output? ccWhere cc must satisfy 1\u2264c\u2264n1\u2264c\u2264n. Don't forget to flush, to get the answer.In response, you will receive a single letter Y (yes) or N (no), telling you if variety acac is one of the last kk varieties of coffee in his memory. To reset the memory of your friend, in a separate line output the single letter R in upper case. You can do this operation at most 30\u00a000030\u00a0000 times. When you determine the number dd of different coffee varieties, output! ddIn case your query is invalid, you asked more than 3n22k3n22k queries of type ? or you asked more than 30\u00a000030\u00a0000 queries of type R, the program will print the letter E and will finish interaction. You will receive a Wrong Answer verdict. Make sure to exit immediately to avoid getting other verdicts.After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hack formatThe first line should contain the word fixedThe second line should contain two integers nn and kk, separated by space (1\u2264k\u2264n\u226410241\u2264k\u2264n\u22641024, kk and nn are powers of two).It must hold that 3n22k\u226415\u00a00003n22k\u226415\u00a0000.The third line should contain nn integers a1,a2,\u2026,ana1,a2,\u2026,an, separated by spaces (1\u2264ai\u2264n1\u2264ai\u2264n).ExamplesInputCopy4 2\nN\nN\nY\nN\nN\nN\nN\nOutputCopy? 1\n? 2\n? 3\n? 4\nR\n? 4\n? 1\n? 2\n! 3\nInputCopy8 8\nN\nN\nN\nN\nY\nY\nOutputCopy? 2\n? 6\n? 4\n? 5\n? 2\n? 5\n! 6\nNoteIn the first example; the array is a=[1,4,1,3]a=[1,4,1,3]. The city produces 33 different varieties of coffee (11, 33 and 44).The successive varieties of coffee tasted by your friend are 1,4,1,3,3,1,41,4,1,3,3,1,4 (bold answers correspond to Y answers). Note that between the two ? 4 asks, there is a reset memory request R, so the answer to the second ? 4 ask is N. Had there been no reset memory request, the answer to the second ? 4 ask is Y.In the second example, the array is a=[1,2,3,4,5,6,6,6]a=[1,2,3,4,5,6,6,6]. The city produces 66 different varieties of coffee.The successive varieties of coffee tasted by your friend are 2,6,4,5,2,52,6,4,5,2,5.","tags":["constructive algorithms","graphs","interactive"],"code":"#include\n\n#define all(x) begin(x), end(x)\n\nusing namespace std;\n\nusing ll = long long;\n\nint n, k;\n\ndeque cur;\n\nvoid reset() {\n\n\tcout << \"R\" << endl;\n\n\twhile(!cur.empty()) cur.pop_back();\n\n}\n\nbool add(int x) {\n\n\tcur.push_back(x);\n\n\tif(cur.size() > k) cur.pop_front();\n\n\tcout << \"? \" << x << endl;\n\n\tchar c;\n\n\tcin >> c;\n\n\treturn c == 'N';\n\n}\n\nint main() {\n\n\tcin.tie(0)->sync_with_stdio(0);\n\n\tcin >> n >> k;\n\n\tvector> lol;\n\n\tint Bs = k>1?k\/2:1;\n\n\tfor(int i = 1; i <= n; i++) {\n\n\t\tif(lol.empty() || lol.back().size() == Bs) lol.push_back({});\n\n\t\tlol.back().push_back(i);\n\n\t}\n\n\tauto update = [&](int i) {\n\n\t\tvector ni;\n\n\t\tfor(auto x : lol[i]) if(add(x))\n\n\t\t\tni.push_back(x);\n\n\t\tlol[i] = ni;\n\n\t};\n\n\tint m = lol.size();\n\n\tfor(int i = 1; i <= m\/2; i++) {\n\n\t\tint g = __gcd(m, i);\n\n\t\tfor(int j = 0; j < g; j++) {\n\n\t\t\tvector v { j };\n\n\t\t\tdo {\n\n\t\t\t\tv.push_back((v.back() + i)%m);\n\n\t\t\t} while(v.back() != j);\n\n\t\t\tv.pop_back();\n\n\t\t\tfor(auto id : v) {\n\n\t\t\t\tupdate(id);\n\n\t\t\t}\n\n\t\t\treset();\n\n\t\t\tif(v.size() > 2) {\n\n\t\t\t\tupdate(v.back());\n\n\t\t\t\tupdate(v[0]);\n\n\t\t\t\treset();\n\n\t\t\t}\n\n\t\t}\n\n\t}\n\n\t\n\n\tint ans = 0;\n\n\tfor(auto i : lol) ans += i.size();\n\n\tcout << \"! \" << ans << endl;\n\n\treturn 0;\n\n} ","language":"cpp"} -{"contest_id":"1296","problem_id":"A","statement":"A. Array with Odd Sumtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa consisting of nn integers.In one move, you can choose two indices 1\u2264i,j\u2264n1\u2264i,j\u2264n such that i\u2260ji\u2260j and set ai:=ajai:=aj. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose ii and jj and replace aiai with ajaj).Your task is to say if it is possible to obtain an array with an odd (not divisible by 22) sum of elements.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1\u2264t\u226420001\u2264t\u22642000) \u2014 the number of test cases.The next 2t2t lines describe test cases. The first line of the test case contains one integer nn (1\u2264n\u226420001\u2264n\u22642000) \u2014 the number of elements in aa. The second line of the test case contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u226420001\u2264ai\u22642000), where aiai is the ii-th element of aa.It is guaranteed that the sum of nn over all test cases does not exceed 20002000 (\u2211n\u22642000\u2211n\u22642000).OutputFor each test case, print the answer on it \u2014 \"YES\" (without quotes) if it is possible to obtain the array with an odd sum of elements, and \"NO\" otherwise.ExampleInputCopy5\n2\n2 3\n4\n2 2 8 8\n3\n3 3 3\n4\n5 5 5 5\n4\n1 1 1 1\nOutputCopyYES\nNO\nYES\nNO\nNO\n","tags":["math"],"code":"#include \n\n\n\nusing namespace std;\n\n\n\nint main() {\n\n int t;\n\n cin >> t;\n\n\n\n while (t--) {\n\n int n;\n\n cin >> n;\n\n\n\n int sum = 0;\n\n bool odd = false;\n\n bool even = false;\n\n\n\n for (size_t i = 0; i < n; i++) {\n\n int x;\n\n cin >> x;\n\n\n\n sum += x;\n\n odd |= x % 2 != 0;\n\n even |= x % 2 == 0;\n\n }\n\n \n\n if (sum % 2 != 0 || (odd && even))\n\n cout << \"YES\" << endl;\n\n else\n\n cout << \"NO\" << endl;\n\n }\n\n\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1288","problem_id":"E","statement":"E. Messenger Simulatortime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp is a frequent user of the very popular messenger. He's chatting with his friends all the time. He has nn friends, numbered from 11 to nn.Recall that a permutation of size nn is an array of size nn such that each integer from 11 to nn occurs exactly once in this array.So his recent chat list can be represented with a permutation pp of size nn. p1p1 is the most recent friend Polycarp talked to, p2p2 is the second most recent and so on.Initially, Polycarp's recent chat list pp looks like 1,2,\u2026,n1,2,\u2026,n (in other words, it is an identity permutation).After that he receives mm messages, the jj-th message comes from the friend ajaj. And that causes friend ajaj to move to the first position in a permutation, shifting everyone between the first position and the current position of ajaj by 11. Note that if the friend ajaj is in the first position already then nothing happens.For example, let the recent chat list be p=[4,1,5,3,2]p=[4,1,5,3,2]: if he gets messaged by friend 33, then pp becomes [3,4,1,5,2][3,4,1,5,2]; if he gets messaged by friend 44, then pp doesn't change [4,1,5,3,2][4,1,5,3,2]; if he gets messaged by friend 22, then pp becomes [2,4,1,5,3][2,4,1,5,3]. For each friend consider all position he has been at in the beginning and after receiving each message. Polycarp wants to know what were the minimum and the maximum positions.InputThe first line contains two integers nn and mm (1\u2264n,m\u22643\u22c51051\u2264n,m\u22643\u22c5105) \u2014 the number of Polycarp's friends and the number of received messages, respectively.The second line contains mm integers a1,a2,\u2026,ama1,a2,\u2026,am (1\u2264ai\u2264n1\u2264ai\u2264n) \u2014 the descriptions of the received messages.OutputPrint nn pairs of integers. For each friend output the minimum and the maximum positions he has been in the beginning and after receiving each message.ExamplesInputCopy5 4\n3 5 1 4\nOutputCopy1 3\n2 5\n1 4\n1 5\n1 5\nInputCopy4 3\n1 2 4\nOutputCopy1 3\n1 2\n3 4\n1 4\nNoteIn the first example; Polycarp's recent chat list looks like this: [1,2,3,4,5][1,2,3,4,5] [3,1,2,4,5][3,1,2,4,5] [5,3,1,2,4][5,3,1,2,4] [1,5,3,2,4][1,5,3,2,4] [4,1,5,3,2][4,1,5,3,2] So, for example, the positions of the friend 22 are 2,3,4,4,52,3,4,4,5, respectively. Out of these 22 is the minimum one and 55 is the maximum one. Thus, the answer for the friend 22 is a pair (2,5)(2,5).In the second example, Polycarp's recent chat list looks like this: [1,2,3,4][1,2,3,4] [1,2,3,4][1,2,3,4] [2,1,3,4][2,1,3,4] [4,2,1,3][4,2,1,3] ","tags":["data structures"],"code":"#include \n\nusing namespace std;\n\n\n\ntypedef long long ll;\n\ntypedef pair pii;\n\ntypedef pair pll;\n\n\n\n#define sp \" \"\n\n#define nl \"\\n\"\n\n#define F first\n\n#define S second\n\n#define PB push_back\n\n#define all(arr) arr.begin(), arr.end()\n\n\n\nconst int N = 3e5 + 5;\n\nconst int MOD = 1e9 + 7;\n\nconst ll INF = 1e18;\n\n\n\nvoid freop(){\n\n if(fopen(\"FILE.INP\", \"r\")){\n\n freopen(\"FILE.INP\", \"r\", stdin);\n\n freopen(\"FILE.OUT\", \"w\", stdout);\n\n }\n\n}\n\n\n\nint n, m;\n\nint a[N], pre[N];\n\nvector seg[4 * N];\n\n\n\nvoid build(int id, int l, int r){\n\n if(l == r){\n\n seg[id].PB(pre[l]);\n\n return;\n\n }\n\n int mid = (l + r) \/ 2;\n\n build(id*2, l, mid);\n\n build(id*2+1, mid+1, r);\n\n merge(all(seg[id*2]), all(seg[id*2+1]), back_inserter(seg[id]));\n\n}\n\n\n\nint get(int id, int l, int r, int u, int v, int val){\n\n if(r < u || l > v) return 0;\n\n if(u <= l && r <= v) {\n\n int pos = lower_bound(all(seg[id]), val) - seg[id].begin();\n\n return pos;\n\n }\n\n int mid = (l + r) \/ 2;\n\n return get(id*2, l, mid, u, v, val) + get(id*2+1, mid+1, r, u, v, val);\n\n}\n\nint st[4*N];\n\nvoid add(int node, int val){\n\n node += n; st[node] = val;\n\n for(node \/= 2; node > 0; node \/= 2)\n\n st[node] = st[node*2] + st[node*2+1];\n\n}\n\nint query(int lo, int hi){\n\n lo += n, hi += n;\n\n int ans = 0;\n\n while(lo <= hi){\n\n if(lo % 2 == 1) ans += st[lo++];\n\n if(hi % 2 == 0) ans += st[hi--];\n\n lo \/= 2, hi \/= 2;\n\n }\n\n return ans;\n\n}\n\npii ans[N];\n\nint main(){\n\n ios_base::sync_with_stdio(0);\n\n cin.tie(0);\n\n \/\/freop();\n\n \/\/srand(time(NULL));\n\n cin >> n >> m;\n\n for(int i = 1; i <= m; ++i) cin >> a[i];\n\n map pos;\n\n for(int i = 1; i <= m; ++i){\n\n pre[i] = pos[a[i]];\n\n pos[a[i]] = i;\n\n }\n\n build(1, 1, m);\n\n pos.clear();\n\n for(int i = 1; i <= n; ++i) ans[i].F = ans[i].S = i;\n\n for(int i = 1; i <= m; ++i){\n\n int val = a[i];\n\n if(pos[val] == 0){\n\n ans[val].S = max(ans[val].S, val + query(val + 1, n));\n\n }\n\n else{\n\n int pre = pos[val];\n\n ans[val].S = max(ans[val].S, 1 + get(1, 1, m, pre + 1, i - 1, pre + 1));\n\n }\n\n pos[val] = i;\n\n ans[val].F = 1;\n\n add(val, 1);\n\n }\n\n for(int i = 1; i <= n; ++i){\n\n if(pos[i] == 0){\n\n ans[i].S = max(ans[i].S, i + query(i + 1, n));\n\n }\n\n else{\n\n int pre = pos[i];\n\n ans[i].S = max(ans[i].S, 1 + get(1, 1, m, pre + 1, m, pre + 1));\n\n }\n\n }\n\n for(int i = 1; i <= n; ++i) {\n\n cout << ans[i].F << sp << ans[i].S << nl;\n\n }\n\n return 0;\n\n}\n\n\n\n","language":"cpp"} -{"contest_id":"1293","problem_id":"B","statement":"B. JOE is on TV!time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output3R2 - Standby for ActionOur dear Cafe's owner; JOE Miller, will soon take part in a new game TV-show \"1 vs. nn\"!The game goes in rounds, where in each round the host asks JOE and his opponents a common question. All participants failing to answer are eliminated. The show ends when only JOE remains (we assume that JOE never answers a question wrong!).For each question JOE answers, if there are ss (s>0s>0) opponents remaining and tt (0\u2264t\u2264s0\u2264t\u2264s) of them make a mistake on it, JOE receives tsts dollars, and consequently there will be s\u2212ts\u2212t opponents left for the next question.JOE wonders what is the maximum possible reward he can receive in the best possible scenario. Yet he has little time before show starts, so can you help him answering it instead?InputThe first and single line contains a single integer nn (1\u2264n\u22641051\u2264n\u2264105), denoting the number of JOE's opponents in the show.OutputPrint a number denoting the maximum prize (in dollars) JOE could have.Your answer will be considered correct if it's absolute or relative error won't exceed 10\u2212410\u22124. In other words, if your answer is aa and the jury answer is bb, then it must hold that |a\u2212b|max(1,b)\u226410\u22124|a\u2212b|max(1,b)\u226410\u22124.ExamplesInputCopy1\nOutputCopy1.000000000000\nInputCopy2\nOutputCopy1.500000000000\nNoteIn the second example; the best scenario would be: one contestant fails at the first question; the other fails at the next one. The total reward will be 12+11=1.512+11=1.5 dollars.","tags":["combinatorics","greedy","math"],"code":"#include \n\n#include \n\n#define ll long long\n\n#include \n\nusing namespace std;\n\nint main() {\n\n int t=1;\n\n while(t--){\n\n int n; cin>>n;\n\n float a=0;\n\n for(float i=n;i>0;i--){\n\n a=a+(1\/i);\n\n }\n\n cout<\nusing namespace std;\n\ntypedef long long i64;\ntypedef vector vi;\ntypedef pair pii;\n\ntemplate\ninline T read(){\n T x=0,f=0;char ch=getchar();\n while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();\n while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();\n return f?-x:x;\n}\n\n#define rdi read\n#define rdi64 read\n#define fi first\n#define se second\n#define pb push_back\n#define mp make_pair\n\nconst int N=65,MOD=1e9+7;\n\nint n,a[N];\nvi e[N],_e[N];\n\ni64 C[N][N];\nvoid init(int n){\n for(int i=0;i<=n;i++){\n C[i][0]=1;\n for(int j=1;j<=i;j++) \n C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD;\n }\n}\n\nint id[N];\ni64 f[(1<<(N\/4))+1][N];\npair solve(const vi &nodes){\n if(nodes.size()==1) {return {0,1};}\n \n for(int i=1;i<=n;i++) id[i]=0;\n \n vi S,T;\n int cnt=0;\n for(auto x:nodes) \n if(_e[x].empty()) S.pb(x),id[x]=++cnt;\n else T.pb(x);\n\n static int s[N];\n for(auto x:T){\n s[x]=0;\n for(auto y:_e[x]) \n if(id[y]) s[x]|=(1<<(id[y]-1));\n }\n\n f[0][0]=1;\n for(int i=0;i<(1< ret={(int)(nodes.size()-S.size()-1),f[(1<\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define ll long long int\n#define mod 1000000007\n\nusing namespace std;\n\n\/\/ ==================== FUNCTIONS FOR INPUT AND OUTPUT OF VECTORS =======================================================\n\nvoid input(vector < ll > &arr)\n{\n for(int i = 0;i < arr.size();i++)\n cin >> arr[i];\n}\n\nvoid output(vector < ll > &arr)\n{\n for(int i = 0;i < arr.size();i++)\n cout << arr[i] << \" \";\n\n cout << \"\\n\";\n}\n\n\/\/ ============================== FOR MATHEMATICAL FUNCTIONS =============================================================\n\nll gcd(ll a,ll b)\n{\n if(b==0)\n return a;\n\n return gcd(b,a%b);\n}\n\nll power(ll a,ll b)\n{\n if(b==0)\n return 1;\n\n if(b==1)\n return a;\n\n ll smallans=power(a,b\/2);\n ll myans=(smallans*smallans)%mod;\n\n if((b&1))\n myans=(myans*a)%mod;\n\n return myans;\n}\n\nll multiply(ll a,ll b)\n{\n ll ans=((a%mod)*(b%mod))%mod;\n\n return ans;\n}\n\nll divide(ll a,ll b)\n{\n return multiply(a,power(b,mod-2));\n}\n\n\/\/ ============================ SEGMENT TREE FOR DEFAULT MINIMUM QUERY ==================================================\n\nvoid manageLazy(vector &tree,vector &lazy,ll idx)\n{\n tree[2*idx + 1] += lazy[idx];\n lazy[2*idx + 1] += lazy[idx];\n\n tree[2*idx + 2] += lazy[idx]; \n lazy[2*idx + 2] += lazy[idx];\n\n lazy[idx] = 0;\n}\n\nvoid build(vector &tree,vector &arr,ll left,ll right,ll idx)\n{\n if(left == right)\n {\n tree[idx] = arr[left];\n return;\n }\n\n ll mid = (left + right) \/ 2;\n build(tree , arr , left , mid , 2*idx + 1);\n build(tree , arr , mid + 1 , right , 2*idx + 2);\n\n tree[idx] = min(tree[2*idx + 1],tree[2*idx + 2]);\n}\n\nvoid update(vector &tree,vector &lazy,ll tl,ll tr,ll l,ll r,ll idx,ll val)\n{\n if(l > r)\n return;\n\n if(l == tl && tr == r)\n {\n tree[idx] += val;\n lazy[idx] += val;\n return;\n }\n\n manageLazy(tree,lazy,idx);\n ll mid = (tl + tr) \/ 2;\n\n update(tree,lazy,tl,mid,l,min(r,mid),2*idx + 1,val);\n update(tree,lazy,mid + 1,tr,max(l,mid + 1),r,2*idx + 2,val);\n\n tree[idx] = min(tree[2*idx + 1],tree[2*idx + 2]);\n\n}\n\nll query(vector &tree,vector & lazy,ll tl,ll tr,ll l,ll r,ll idx)\n{\n if(l > r)\n return 1e18;\n\n if(l <= tl && tr <= r)\n return tree[idx];\n\n manageLazy(tree,lazy,idx);\n ll mid = (tl + tr) \/ 2;\n\n ll a = query(tree,lazy,tl,mid,l,min(r,mid),2*idx + 1);\n ll b = query(tree,lazy,mid + 1,tr,max(l,mid + 1),r,2*idx + 2);\n\n return min(a,b);\n}\n\n\/\/ ================================== SPARSE TABLE FOR DEFAULT MINIMUM QUERY ==================================================\n\nvoid precompute_min(vector < vector < ll > > &sparsetable , vector < ll > &a)\n{\n int n = sparsetable.size();\n int p = sparsetable[0].size();\n\n for(int i = 0;i < n;i++) \n sparsetable[i][0] = a[i];\n\n for(int j = 1;j < p;j++)\n {\n for(int i = 0;i + (1 << j) <= n;i++)\n sparsetable[i][j] = min(sparsetable[i][j - 1] , sparsetable[i + (1 << (j - 1))][j - 1]);\n }\n \n}\n\nll minquery(vector < vector < ll > > &sparsetable , vector < ll > &log , ll l , ll r)\n{\n ll range = r - l + 1;\n if(range == 0) return sparsetable[l][0];\n ll j = log[range];\n return min(sparsetable[l][j] , sparsetable[r - (1 << j) + 1][j]);\n}\n\n\/\/========================== BINARY INDEX TREE =========================================================================\n\nvoid update(vector &tree,ll index,ll val)\n{\n index++;\n while(index < tree.size())\n {\n tree[index]=(tree[index] + val) %mod;\n \n index+=index&(-index);\n }\n}\n \nll calculate(vector &tree,ll index)\n{\n ll sum=0;\n index++;\n while(index > 0)\n {\n sum=(sum + tree[index])%mod;\n \n index-=index&(-index);\n }\n \n return sum;\n}\n\n\/\/=========================== FOR DISJOINT SET UNION ====================================================================\n\nll findpar(ll p,vector &parent)\n{\n if(parent[p]==p)\n return p;\n\n parent[p]=findpar(parent[p],parent);\n\n return parent[p];\n}\n\nvoid merge(ll a, ll b,vector &parent,vector &size_) \n{\n a = findpar(a,parent);\n b = findpar(b,parent);\n \n if (a != b) \n {\n if (size_[a] < size_[b])\n swap(a, b);\n \n parent[b] = a;\n size_[a] += size_[b];\n }\n}\n\n\/\/ ====================================== FOR STORING AND COUNTING THE PRIMES USING SIEVE ============================\n\nvoid sieve(vector &primes,vector &count)\n{\n for(int i=2;i> n >> m;\n\n vector < ll > arr(n, 0);\n input(arr);\n\n ll poss = 0;\n for(int i = 1;i < arr.size();i++) poss += arr[i];\n poss = min(poss, m - arr[0]);\n\n cout << (arr[0] + poss) << \"\\n\";\n\n}\n\nint main()\n{\n ios_base::sync_with_stdio(false); \n cin.tie(NULL);\n cout.tie(NULL);\n #ifndef ONLINE_JUDGE\n freopen(\"input2.txt\", \"r\", stdin);\n freopen(\"output.txt\", \"w\", stdout);\n #endif\n\n int t;\n cin >> t;\n\n while(t--)\n {\n\n solve();\n \n }\n\n return 0;\n}","language":"cpp"} -{"contest_id":"1296","problem_id":"D","statement":"D. Fight with Monsterstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn monsters standing in a row numbered from 11 to nn. The ii-th monster has hihi health points (hp). You have your attack power equal to aa hp and your opponent has his attack power equal to bb hp.You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to 00.The fight with a monster happens in turns. You hit the monster by aa hp. If it is dead after your hit, you gain one point and you both proceed to the next monster. Your opponent hits the monster by bb hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster. You have some secret technique to force your opponent to skip his turn. You can use this technique at most kk times in total (for example, if there are two monsters and k=4k=4, then you can use the technique 22 times on the first monster and 11 time on the second monster, but not 22 times on the first monster and 33 times on the second monster).Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.InputThe first line of the input contains four integers n,a,bn,a,b and kk (1\u2264n\u22642\u22c5105,1\u2264a,b,k\u22641091\u2264n\u22642\u22c5105,1\u2264a,b,k\u2264109) \u2014 the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique.The second line of the input contains nn integers h1,h2,\u2026,hnh1,h2,\u2026,hn (1\u2264hi\u22641091\u2264hi\u2264109), where hihi is the health points of the ii-th monster.OutputPrint one integer \u2014 the maximum number of points you can gain if you use the secret technique optimally.ExamplesInputCopy6 2 3 3\n7 10 50 12 1 8\nOutputCopy5\nInputCopy1 1 100 99\n100\nOutputCopy1\nInputCopy7 4 2 1\n1 3 5 4 2 7 6\nOutputCopy6\n","tags":["greedy","sortings"],"code":"#include\n\nusing namespace std;\n\ntypedef long long ll;\n\nint h[200001];\n\nint main()\n\n{\n\n\tios::sync_with_stdio(0);\n\n\tcin.tie(0);\n\n\tcout.tie(0);\n\n\tint n, a, b, k, ans;\n\n\tcin>>n>>a>>b>>k;\n\n\tfor(int i=1;i<=n;++i)\n\n\t{\n\n\t\tcin>>h[i];\n\n\t\th[i] %= (a+b);\n\n\t\tif(h[i]>=1&&h[i]<=a)\n\n\t\t{\n\n\t\t\th[i] = 0;\n\n\t\t}\n\n\t\telse if(h[i]==0)\n\n\t\t{\n\n\t\t\th[i] = a+b;\n\n\t\t\th[i] = h[i]%a==0?(h[i]\/a-1):(h[i]\/a);\n\n\t\t}\n\n\t\telse\n\n\t\t{\n\n\t\t\th[i] = h[i]%a==0?(h[i]\/a-1):(h[i]\/a);\n\n\t\t}\n\n\t}\n\n\tans = 0;\n\n\tsort(h+1, h+n+1);\n\n\tfor(int i=1;i<=n;++i)\n\n\t{\n\n\t\tif(h[i]==0)\n\n\t\t{\n\n\t\t\t++ans;\n\n\t\t}\n\n\t\telse \n\n\t\t{\n\n\t\t\tif(k>=h[i]) \n\n\t\t\t{\n\n\t\t\t\tk -= h[i];\n\n\t\t\t\t++ans;\n\n\t\t\t}\n\n\t\t\telse break;\n\n\t\t}\n\n\t}\n\n\tcout<\n\n\n\nusing namespace std;\n\n\n\nusing ll = long long;\n\nusing pii = pair;\n\n\n\n#define out(x) cout << #x << '=' << x << endl;\n\n#define lowbit(x) (x & -x);\n\n\n\n\n\nint main(void) {\n\n ios::sync_with_stdio(false);\n\n cin.tie(0); cout.tie(0);\n\n int t = 1;\n\n \/\/cin >> t;\n\n \n\n while (t--) {\n\n \tll h;\n\n \tint n;\n\n \tcin >> h >> n;\n\n \tvector a(n);\n\n \tll sum = 0;\n\n \tll hh = h;\n\n \tint ch = 0;\n\n \tfor (int i = 0; i < n; i++) {\n\n \t\tcin >> a[i];\n\n \t\tsum += a[i];\n\n \t\thh += a[i];\n\n \t\tif (hh <= 0) {\n\n \t\t\tcout << i + 1 << endl;\n\n \t\t\tch = 1;\n\n \t\t\tbreak;\n\n\t\t\t}\n\n\t\t}\n\n\t\tif (ch) continue;\n\n\t\tif (sum >= 0) {\n\n\t\t\tcout << -1 << endl;\n\n\t\t} else {\n\n\t\t\tll left = 0;\n\n\t\t\tll right = -(h - sum - 1) \/ sum; \n\n\t\t\t\n\n\t\t\tauto check = [&](ll rounds) -> ll {\n\n\t\t\t\tif (h + (__int128) rounds * sum <= 0) return rounds * n;\n\n\t\t\t\tll health = h + rounds * sum;\n\n\t\t\t\tfor (int i = 1; i <= n; i++) {\n\n\t\t\t\t\thealth += a[i - 1];\n\n\t\t\t\t\t\/\/out()\n\n\t\t\t\t\t\/\/out(health);\n\n\t\t\t\t\t\n\n\t\t\t\t\tif (health <= 0) {\n\n\t\t\t\t\t\treturn rounds * n + i;\n\n\t\t\t\t\t}\n\n\t\t\t\t}\n\n\t\t\t\treturn -1;\n\n\t\t\t};\n\n\t\t\tll ans = -1;\n\n\t\t\twhile (left <= right) {\n\n\t\t\t\tll mid = left + (right - left) \/ 2;\n\n\t\t\t\tll days = check(mid);\n\n\t\t\t\tif (days == -1) {\n\n\t\t\t\t\tleft = mid + 1;\n\n\t\t\t\t} else {\n\n\t\t\t\t\tright = mid - 1;\n\n\t\t\t\t\tans = days;\n\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t\t\n\n\t\t\tcout << ans << endl;\n\n\t\t}\n\n\t}\n\n}\n\n\n\n","language":"cpp"} -{"contest_id":"1301","problem_id":"A","statement":"A. Three Stringstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three strings aa, bb and cc of the same length nn. The strings consist of lowercase English letters only. The ii-th letter of aa is aiai, the ii-th letter of bb is bibi, the ii-th letter of cc is cici.For every ii (1\u2264i\u2264n1\u2264i\u2264n) you must swap (i.e. exchange) cici with either aiai or bibi. So in total you'll perform exactly nn swap operations, each of them either ci\u2194aici\u2194ai or ci\u2194bici\u2194bi (ii iterates over all integers between 11 and nn, inclusive).For example, if aa is \"code\", bb is \"true\", and cc is \"help\", you can make cc equal to \"crue\" taking the 11-st and the 44-th letters from aa and the others from bb. In this way aa becomes \"hodp\" and bb becomes \"tele\".Is it possible that after these swaps the string aa becomes exactly the same as the string bb?InputThe input consists of multiple test cases. The first line contains a single integer tt (1\u2264t\u22641001\u2264t\u2264100) \u00a0\u2014 the number of test cases. The description of the test cases follows.The first line of each test case contains a string of lowercase English letters aa.The second line of each test case contains a string of lowercase English letters bb.The third line of each test case contains a string of lowercase English letters cc.It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100100.OutputPrint tt lines with answers for all test cases. For each test case:If it is possible to make string aa equal to string bb print \"YES\" (without quotes), otherwise print \"NO\" (without quotes).You can print either lowercase or uppercase letters in the answers.ExampleInputCopy4\naaa\nbbb\nccc\nabc\nbca\nbca\naabb\nbbaa\nbaba\nimi\nmii\niim\nOutputCopyNO\nYES\nYES\nNO\nNoteIn the first test case; it is impossible to do the swaps so that string aa becomes exactly the same as string bb.In the second test case, you should swap cici with aiai for all possible ii. After the swaps aa becomes \"bca\", bb becomes \"bca\" and cc becomes \"abc\". Here the strings aa and bb are equal.In the third test case, you should swap c1c1 with a1a1, c2c2 with b2b2, c3c3 with b3b3 and c4c4 with a4a4. Then string aa becomes \"baba\", string bb becomes \"baba\" and string cc becomes \"abab\". Here the strings aa and bb are equal.In the fourth test case, it is impossible to do the swaps so that string aa becomes exactly the same as string bb.","tags":["implementation","strings"],"code":"#include\n\nusing namespace std;\n\n\n\n#define int long long\n\n#define PII pair\n\n#define x first\n\n#define y second\n\nconst int N=1e5+10;\n\n\n\nstring a,b,c;\n\n\n\nvoid solve()\n\n{\n\n\tcin>>a>>b>>c;\n\n\tbool f=0;\n\n\tfor(int i=0;i>T;\n\n\twhile(T--)\n\n\t\tsolve();\n\n\t\n\n\treturn 0;\n\n}","language":"cpp"} -{"contest_id":"1324","problem_id":"F","statement":"F. Maximum White Subtreetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree consisting of nn vertices. A tree is a connected undirected graph with n\u22121n\u22121 edges. Each vertex vv of this tree has a color assigned to it (av=1av=1 if the vertex vv is white and 00 if the vertex vv is black).You have to solve the following problem for each vertex vv: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex vv? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains cntwcntw white vertices and cntbcntb black vertices, you have to maximize cntw\u2212cntbcntw\u2212cntb.InputThe first line of the input contains one integer nn (2\u2264n\u22642\u22c51052\u2264n\u22642\u22c5105) \u2014 the number of vertices in the tree.The second line of the input contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u226410\u2264ai\u22641), where aiai is the color of the ii-th vertex.Each of the next n\u22121n\u22121 lines describes an edge of the tree. Edge ii is denoted by two integers uiui and vivi, the labels of vertices it connects (1\u2264ui,vi\u2264n,ui\u2260vi(1\u2264ui,vi\u2264n,ui\u2260vi).It is guaranteed that the given edges form a tree.OutputPrint nn integers res1,res2,\u2026,resnres1,res2,\u2026,resn, where resiresi is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex ii.ExamplesInputCopy9\n0 1 1 1 0 0 0 0 1\n1 2\n1 3\n3 4\n3 5\n2 6\n4 7\n6 8\n5 9\nOutputCopy2 2 2 2 2 1 1 0 2 \nInputCopy4\n0 0 1 0\n1 2\n1 3\n1 4\nOutputCopy0 -1 1 -1 \nNoteThe first example is shown below:The black vertices have bold borders.In the second example; the best subtree for vertices 2,32,3 and 44 are vertices 2,32,3 and 44 correspondingly. And the best subtree for the vertex 11 is the subtree consisting of vertices 11 and 33.","tags":["dfs and similar","dp","graphs","trees"],"code":"\/*\n\n \u0950 \u0924\u094d\u0930\u094d\u092f\u092e\u094d\u092c\u0915\u0902 \u092f\u091c\u093e\u092e\u0939\u0947 \u0938\u0941\u0917\u0928\u094d\u0927\u093f\u0902 \u092a\u0941\u0937\u094d\u091f\u093f\u0935\u0930\u094d\u0927\u0928\u092e\u094d |\n\n \u0909\u0930\u094d\u0935\u093e\u0930\u0941\u0915\u092e\u093f\u0935 \u092c\u0928\u094d\u0927\u0928\u093e\u0928\u094d\u092e\u0943\u0924\u094d\u092f\u094b\u0930\u094d\u092e\u0941\u0915\u094d\u0937\u0940\u092f \u092e\u093e\u093d\u092e\u0943\u0924\u093e\u0924\u094d ||\n\n*\/\n\n#include \n\nusing namespace std;\n\n#define int long long int\n\n#define endl \"\\n\"\n\nconst int N = 2e5 + 5;\n\nconst int inf = 1e18;\n\nint mod = 1e9 + 7;\n\nint power(int a, int b)\n\n{\n\n int ans = 1;\n\n while (b > 0)\n\n {\n\n if (b & 1)\n\n {\n\n ans = (ans * a) % mod;\n\n }\n\n a = (a * a) % mod;\n\n b >>= 1;\n\n }\n\n return ans;\n\n}\n\n\n\n\/\/ vector c(N), dp(N), ans(N), g[N];\n\nint n;\n\nvector a;\n\nvector> gr;\n\n\n\nvoid dfs1(int u, int p)\n\n{\n\n for (int v : gr[u])\n\n {\n\n if (v != p)\n\n {\n\n dfs1(v, u);\n\n a[u] += max(0ll, a[v]);\n\n }\n\n }\n\n}\n\nvoid dfs2(int u, int p)\n\n{\n\n for (int v : gr[u])\n\n {\n\n if (v != p)\n\n {\n\n a[v] += max(0ll, a[u] - max(0ll, a[v]));\n\n dfs2(v, u);\n\n }\n\n }\n\n}\n\n\n\nvoid solve()\n\n{\n\n cin >> n;\n\n a.resize(n);\n\n gr.resize(n);\n\n for (int i = 0; i < n; i++)\n\n {\n\n cin >> a[i];\n\n if (a[i] == 0)\n\n a[i] = -1;\n\n }\n\n for (int i = 0; i < n - 1; i++)\n\n {\n\n int u, v;\n\n cin >> u >> v;\n\n u--, v--;\n\n gr[u].push_back(v);\n\n gr[v].push_back(u);\n\n }\n\n dfs1(0, -1);\n\n dfs2(0, -1);\n\n for (int i = 0; i < n; i++)\n\n cout << a[i] << \" \\n\"[i == n - 1];\n\n}\n\n\n\nint32_t main()\n\n{\n\n ios_base::sync_with_stdio(false);\n\n cin.tie(NULL);\n\n cout.tie(NULL);\n\n \/\/ #ifndef ONLINE_JUDGE\n\n \/\/ freopen(\"input.txt\",\"r\",stdin);\n\n \/\/ freopen(\"output.txt\",\"w\",stdout);\n\n \/\/ #endif\n\n int t = 1;\n\n \/\/ cin >> t;\n\n while (t--)\n\n solve();\n\n\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1311","problem_id":"B","statement":"B. WeirdSorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn.You are also given a set of distinct positions p1,p2,\u2026,pmp1,p2,\u2026,pm, where 1\u2264pi\nusing namespace std;\nconst int N = 100;\nint main(){\n\tint t;\n\tcin >> t;\n\twhile (t--) {\n\t\tint n, p;\n\t\tint a[N], b[N];\n\t\tcin >> n >> p;\n\t\tfor (int i = 0; i < n; i++) cin >> a[i];\n\n\t\tfor (int i = 0; i < n; i++) b[i] = 0;\n\t\tfor (int i = 0; i < p; i++) {\n\t\t\tint x;\n\t\t\tcin >> x;\n\t\t\tb[x-1] = 1;\n\t\t}\n\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tfor (int j = 0; j < n - i - 1; j++) {\n\t\t\t\tif (b[j] == 1 && a[j] > a[j + 1]) {\n\t\t\t\t\tswap(a[j], a[j + 1]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tbool x = true;\n\t\tfor (int i = 0; i < n - 1; i++) {\n\t\t\tif (a[i] > a[i + 1]) {\n\t\t\t\tx = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (x == true) cout << \"YES\" << endl;\n\t\telse cout << \"NO\" << endl;\n\n\t}\n\treturn 0;\n}\n\t\t \t\t \t\t\t\t\t\t\t\t \t \t \t \t \t\t\t","language":"cpp"} -{"contest_id":"1311","problem_id":"E","statement":"E. Construct the Binary Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and dd. You need to construct a rooted binary tree consisting of nn vertices with a root at the vertex 11 and the sum of depths of all vertices equals to dd.A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex vv is the last different from vv vertex on the path from the root to the vertex vv. The depth of the vertex vv is the length of the path from the root to the vertex vv. Children of vertex vv are all vertices for which vv is the parent. The binary tree is such a tree that no vertex has more than 22 children.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1\u2264t\u226410001\u2264t\u22641000) \u2014 the number of test cases.The only line of each test case contains two integers nn and dd (2\u2264n,d\u226450002\u2264n,d\u22645000) \u2014 the number of vertices in the tree and the required sum of depths of all vertices.It is guaranteed that the sum of nn and the sum of dd both does not exceed 50005000 (\u2211n\u22645000,\u2211d\u22645000\u2211n\u22645000,\u2211d\u22645000).OutputFor each test case, print the answer.If it is impossible to construct such a tree, print \"NO\" (without quotes) in the first line. Otherwise, print \"{YES}\" in the first line. Then print n\u22121n\u22121 integers p2,p3,\u2026,pnp2,p3,\u2026,pn in the second line, where pipi is the parent of the vertex ii. Note that the sequence of parents you print should describe some binary tree.ExampleInputCopy3\n5 7\n10 19\n10 18\nOutputCopyYES\n1 2 1 3 \nYES\n1 2 3 3 9 9 2 1 6 \nNO\nNotePictures corresponding to the first and the second test cases of the example:","tags":["brute force","constructive algorithms","trees"],"code":"#include\n\n#define ll long long \n\n#define pii pair\n\n#define fi first\n\n#define se second\n\n#define pb push_back\n\n#define lowbit(x) (x&(-x))\n\n#define lb long double\n\n#define db double\n\n#define pll pair\n\n#define debug(a) cout<<\"debug: \"<<(#a)<<\" = \"<\n\nusing namespace std;\n\nconst int maxn=2e5+100;\n\nconst int mode=1e9+7;\n\nconst ll inf=9223372036854775807;\n\nconst db eps=1e-6;\n\nll n,m,a[maxn];\n\nll sum,ans[maxn],d;\n\nstring str;\n\nvectorvt;\n\nint vis[maxn];\n\nint par[maxn],ch[maxn];\n\nint now,c,mx;\n\nint get()\n\n{\n\n\tint res=now;\n\n\tc++;\n\n\tif(c==mx)\n\n\t{\n\n\t\tnow++;\n\n\t\tmx*=2;\n\n\t\tc=1;\n\n\t}\n\n\treturn res;\n\n}\n\nvoid solve()\n\n{\n\n cin>>n>>d;\n\n ll cnt=0;\n\n for(int i=1;i<=n-1;i++)\n\n {\n\n \tans[i]=i;\n\n \tch[i]=0;\n\n \tcnt+=i;\n\n\t}\n\n\tch[0]=0;\n\n\tif(cntd)\n\n\t{\n\n\t\tint deg=get();\n\n\t\tll res=cnt-d;\n\n\t\tif(deg>=pos)\n\n\t\tbreak;\n\n\t\tif(res>=(pos-deg))\n\n\t\t{\n\n\t\t\tans[pos]=deg;\n\n\t\t\tcnt-=(pos-deg);\n\n\t\t}else{\n\n\t\t\tans[pos]-=res;\n\n\t\t\tcnt=d;\n\n\t\t\tbreak;\n\n\t\t}\n\n\t\tpos--;\n\n\t}\n\n\tif(cnt>d)\n\n\t{\n\n\t\tcout<<\"NO\"<<'\\n';\n\n\t\treturn;\n\n\t}\n\n\tcout<<\"YES\"<<'\\n';\n\n\tans[0]=0;\n\n\tfor(int i=1;i<=n-1;i++)\n\n\t{\n\n\t\tfor(int j=0;j<=n-1;j++)\n\n\t\t{\n\n\t\t\tif(ans[j]==ans[i]-1&&ch[j]<2)\n\n\t\t\t{\n\n\t\t\t\tpar[i]=j;\n\n\t\t\t\tch[j]++;\n\n\t\t\t\tbreak;\n\n\t\t\t}\n\n\t\t}\n\n\t}\n\n for(int i=1;i<=n-1;i++)\n\n {\n\n \tcout<>t;\n\n\twhile(t--) \n\n solve();\n\n\treturn 0;\n\n}\n\n\n\n\n\n\n\n","language":"cpp"} -{"contest_id":"1286","problem_id":"B","statement":"B. Numbers on Treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEvlampiy was gifted a rooted tree. The vertices of the tree are numbered from 11 to nn. Each of its vertices also has an integer aiai written on it. For each vertex ii, Evlampiy calculated cici\u00a0\u2014 the number of vertices jj in the subtree of vertex ii, such that aj\nusing namespace std;\n#define int long long\nconst int maxm=2e3+5;\nvectorg[maxm];\nint c[maxm];\nint res[maxm];\nint n;\nvector dfs(int now){\n vectorres;\/\/\u5b58\u5b50\u8282\u70b9\n for(int x:g[now]){\n vectorch=dfs(x);\n for(int v:ch){\n res.push_back(v);\n }\n }\n if(c[now]>res.size()){\/\/\u5982\u679c\u6ca1\u6cd5\u63d2\u5165\u8bf4\u660e\u4e0d\u53ef\u80fd\n cout<<\"NO\"<>n;\n for(int i=1;i<=n;i++){\n int fa;\n cin>>fa;\n cin>>c[i];\n g[fa].push_back(i);\n }\n vectorans=dfs(0);\/\/0\u662f\u865a\u6839\n for(int i=0;i<(int)ans.size();i++){\/\/\u8282\u70b9\u8d4b\u503c\n res[ans[i]]=i;\n }\n cout<<\"YES\"<\n\n#define ll long long\n\n#define endl \"\\n\"\n\n#define rep(X,Y) for(X=0;X>T; for(g=0;g\n\nusing namespace std;\n\nint main()\n\n{\n\n#ifndef ONLINE_JUDGE\n\n freopen(\"input.txt\", \"r\", stdin);\n\n freopen(\"outp1.txt\", \"w\", stdout);\n\n#endif\n\n ll i, j;\n\n ml {\n\n ll n , k , x = 0;\n\n cin >> n >> k;\n\n mapmp;\n\n for(i= 0 ; i<=100;i++)\n\n {\n\n mp[i] = 0;\n\n }\n\n rep(i , n)\n\n {\n\n ll a;\n\n cin >> a;\n\n ll cnt = 0;\n\n while (a)\n\n {\n\n ll b = a % k;\n\n mp[cnt] += b;\n\n cnt++;\n\n a \/= k;\n\n\n\n }\n\n }\n\n for (auto it : mp)\n\n {\n\n if (it.second > 1)\n\n {\n\n x = 1;\n\n break;\n\n }\n\n }\n\n if (x)cout << \"NO\" << endl;\n\n else cout << \"YES\" << endl;\n\n }\n\n\n\n return 0;\n\n}","language":"cpp"} -{"contest_id":"1296","problem_id":"A","statement":"A. Array with Odd Sumtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa consisting of nn integers.In one move, you can choose two indices 1\u2264i,j\u2264n1\u2264i,j\u2264n such that i\u2260ji\u2260j and set ai:=ajai:=aj. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose ii and jj and replace aiai with ajaj).Your task is to say if it is possible to obtain an array with an odd (not divisible by 22) sum of elements.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1\u2264t\u226420001\u2264t\u22642000) \u2014 the number of test cases.The next 2t2t lines describe test cases. The first line of the test case contains one integer nn (1\u2264n\u226420001\u2264n\u22642000) \u2014 the number of elements in aa. The second line of the test case contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u226420001\u2264ai\u22642000), where aiai is the ii-th element of aa.It is guaranteed that the sum of nn over all test cases does not exceed 20002000 (\u2211n\u22642000\u2211n\u22642000).OutputFor each test case, print the answer on it \u2014 \"YES\" (without quotes) if it is possible to obtain the array with an odd sum of elements, and \"NO\" otherwise.ExampleInputCopy5\n2\n2 3\n4\n2 2 8 8\n3\n3 3 3\n4\n5 5 5 5\n4\n1 1 1 1\nOutputCopyYES\nNO\nYES\nNO\nNO\n","tags":["math"],"code":"#include \n\n\n\nusing namespace std;\n\n\n\nusing ll = long long;\n\nusing pii = pair;\n\nusing pll = pair;\n\nusing vi = vector;\n\nusing vb = vector;\n\nusing vll = vector;\n\nusing vs = vector;\n\n#define ALL(v) v.begin(), v.end()\n\n#define SORT(v) sort(ALL(v))\n\n#define SZ(v) int(v.size())\n\n\n\n#ifdef DLOCAL\n\n#include \n\n#else\n\n#define deb(...);\n\n#endif\n\n\n\nvoid solve() {\n\n int n; cin >> n;\n\n vi vec(2);\n\n for (int i = 0; i < n; ++i) {\n\n int v; cin >> v;\n\n vec[v % 2]++;\n\n }\n\n\n\n if (vec[1] % 2 || (vec[1] && vec[0])) {\n\n cout << \"YES\\n\";\n\n }\n\n else {\n\n cout << \"NO\\n\";\n\n }\n\n}\n\n\n\nint main() {\n\n ios::sync_with_stdio(0); cin.tie(0);\n\n\n\n \/\/ifstream cin (\"puzzle_name.in\");\n\n \/\/ofstream cout (\"puzzle_name.out\");\n\n\n\n int t; cin >> t;\n\n while (t--)\n\n {\n\n solve();\n\n }\n\n\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1293","problem_id":"A","statement":"A. ConneR and the A.R.C. Markland-Ntime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSakuzyo - ImprintingA.R.C. Markland-N is a tall building with nn floors numbered from 11 to nn. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin \"ConneR\" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor ss of the building. On each floor (including floor ss, of course), there is a restaurant offering meals. However, due to renovations being in progress, kk of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!InputThe first line contains one integer tt (1\u2264t\u226410001\u2264t\u22641000)\u00a0\u2014 the number of test cases in the test. Then the descriptions of tt test cases follow.The first line of a test case contains three integers nn, ss and kk (2\u2264n\u22641092\u2264n\u2264109, 1\u2264s\u2264n1\u2264s\u2264n, 1\u2264k\u2264min(n\u22121,1000)1\u2264k\u2264min(n\u22121,1000))\u00a0\u2014 respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants.The second line of a test case contains kk distinct integers a1,a2,\u2026,aka1,a2,\u2026,ak (1\u2264ai\u2264n1\u2264ai\u2264n)\u00a0\u2014 the floor numbers of the currently closed restaurants.It is guaranteed that the sum of kk over all test cases does not exceed 10001000.OutputFor each test case print a single integer\u00a0\u2014 the minimum number of staircases required for ConneR to walk from the floor ss to a floor with an open restaurant.ExampleInputCopy5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77\nOutputCopy2\n0\n4\n0\n2\nNoteIn the first example test case; the nearest floor with an open restaurant would be the floor 44.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the 66-th floor.","tags":["binary search","brute force","implementation"],"code":"\/\/ Problem: A. ConneR and the A.R.C. Markland-N\n\n\/\/ Contest: Codeforces - Codeforces Round #614 (Div. 2)\n\n\/\/ URL: https:\/\/codeforces.com\/problemset\/problem\/1293\/A\n\n\/\/ Memory Limit: 256 MB\n\n\/\/ Time Limit: 1000 ms\n\n\/\/ \n\n\/\/ Powered by CP Editor (https:\/\/cpeditor.org)\n\n\n\n#include \n\nusing namespace std;\n\n#define pb push_back\n\n#define endl \"\\n\"\n\nvoid solve(){\n\nint n,s,k;\n\ncin>>n>>s>>k;\n\nint forw=0;\n\nint back=0;\n\nvectorv(k);\n\nfor(int i=0;i>v[i];\n\n}\n\nsetst;\n\nfor(int i=0;i0;i--){\n\n\tif(st.find(i)==st.end()){\n\n\t\tflag2 =true;\n\n\t\tbreak;\n\n\t}\n\n\tback++;\n\n}\n\nif(flag1 && flag2){\n\nint ans = min(forw,back);\n\ncout<>t;\n\n while(t--){\n\n solve();\n\n } \n\n }","language":"cpp"} -{"contest_id":"1316","problem_id":"D","statement":"D. Nash Matrixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNash designed an interesting yet simple board game where a player is simply required to follow instructions written on the cell where the player currently stands. This board game is played on the n\u00d7nn\u00d7n board. Rows and columns of this board are numbered from 11 to nn. The cell on the intersection of the rr-th row and cc-th column is denoted by (r,c)(r,c).Some cells on the board are called blocked zones. On each cell of the board, there is written one of the following 55 characters \u00a0\u2014 UU, DD, LL, RR or XX \u00a0\u2014 instructions for the player. Suppose that the current cell is (r,c)(r,c). If the character is RR, the player should move to the right cell (r,c+1)(r,c+1), for LL the player should move to the left cell (r,c\u22121)(r,c\u22121), for UU the player should move to the top cell (r\u22121,c)(r\u22121,c), for DD the player should move to the bottom cell (r+1,c)(r+1,c). Finally, if the character in the cell is XX, then this cell is the blocked zone. The player should remain in this cell (the game for him isn't very interesting from now on).It is guaranteed that the characters are written in a way that the player will never have to step outside of the board, no matter at which cell he starts.As a player starts from a cell, he moves according to the character in the current cell. The player keeps moving until he lands in a blocked zone. It is also possible that the player will keep moving infinitely long.For every of the n2n2 cells of the board Alice, your friend, wants to know, how will the game go, if the player starts in this cell. For each starting cell of the board, she writes down the cell that the player stops at, or that the player never stops at all. She gives you the information she has written: for each cell (r,c)(r,c) she wrote: a pair (xx,yy), meaning if a player had started at (r,c)(r,c), he would end up at cell (xx,yy). or a pair (\u22121\u22121,\u22121\u22121), meaning if a player had started at (r,c)(r,c), he would keep moving infinitely long and would never enter the blocked zone. It might be possible that Alice is trying to fool you and there's no possible grid that satisfies all the constraints Alice gave you. For the given information Alice provided you, you are required to decipher a possible board, or to determine that such a board doesn't exist. If there exist several different boards that satisfy the provided information, you can find any of them.InputThe first line of the input contains a single integer nn (1\u2264n\u22641031\u2264n\u2264103) \u00a0\u2014 the side of the board.The ii-th of the next nn lines of the input contains 2n2n integers x1,y1,x2,y2,\u2026,xn,ynx1,y1,x2,y2,\u2026,xn,yn, where (xj,yj)(xj,yj) (1\u2264xj\u2264n,1\u2264yj\u2264n1\u2264xj\u2264n,1\u2264yj\u2264n, or (xj,yj)=(\u22121,\u22121)(xj,yj)=(\u22121,\u22121)) is the pair written by Alice for the cell (i,j)(i,j). OutputIf there doesn't exist a board satisfying the information that Alice gave you, print a single line containing INVALID. Otherwise, in the first line print VALID. In the ii-th of the next nn lines, print the string of nn characters, corresponding to the characters in the ii-th row of the suitable board you found. Each character of a string can either be UU, DD, LL, RR or XX. If there exist several different boards that satisfy the provided information, you can find any of them.ExamplesInputCopy2\n1 1 1 1\n2 2 2 2\nOutputCopyVALID\nXL\nRX\nInputCopy3\n-1 -1 -1 -1 -1 -1\n-1 -1 2 2 -1 -1\n-1 -1 -1 -1 -1 -1\nOutputCopyVALID\nRRD\nUXD\nULLNoteFor the sample test 1 :The given grid in output is a valid one. If the player starts at (1,1)(1,1), he doesn't move any further following XX and stops there. If the player starts at (1,2)(1,2), he moves to left following LL and stops at (1,1)(1,1). If the player starts at (2,1)(2,1), he moves to right following RR and stops at (2,2)(2,2). If the player starts at (2,2)(2,2), he doesn't move any further following XX and stops there. The simulation can be seen below : For the sample test 2 : The given grid in output is a valid one, as a player starting at any cell other than the one at center (2,2)(2,2), keeps moving in an infinitely long cycle and never stops. Had he started at (2,2)(2,2), he wouldn't have moved further following instruction XX .The simulation can be seen below : ","tags":["constructive algorithms","dfs and similar","graphs","implementation"],"code":"\/\/ https:\/\/codeforces.com\/problemset\/problem\/1316\/D\n\n#include \n#include \n#include \n#include \n\nusing Point = std::pair;\nconstexpr Point Stop(-1, -1);\n\nstruct Test {\n struct Move {\n int dr, dc;\n char cmd, inv_cmd;\n\n Point apply(int r, int c) const {\n return {r + dr, c + dc};\n }\n };\n\n static constexpr Move movies[] = {\n {-1, 0, 'U', 'D'},\n {1, 0, 'D', 'U'},\n {0, -1, 'L', 'R'},\n {0, 1, 'R', 'L'},\n };\n\n int size;\n std::vector> grid;\n std::vector result;\n\n bool in_range(int r, int c) {\n return 0 <= r && r < size && 0 <= c && c < size;\n }\n\n void dfs(int r, int c, const Point& mark) {\n for (const auto& m: movies) {\n const auto [nr, nc] = m.apply(r, c);\n if (!in_range(nr, nc) || grid[nr][nc] != mark || result[nr][nc] != ' ') {\n continue;\n }\n result[nr][nc] = m.inv_cmd;\n dfs(nr, nc, mark);\n }\n }\n\npublic:\n Test(int size):\n size(size),\n grid(size, std::vector(size)),\n result(size, std::string(size, ' '))\n {}\n\n bool solve() {\n for (int r = 0; r < size; ++r) {\n for (int c = 0; c < size; c++) {\n if (grid[r][c] == Point(r, c)) {\n result[r][c] = 'X';\n dfs(r, c, grid[r][c]);\n }\n }\n } \n for (int r = 0; r < size; ++r) {\n for (int c = 0; c < size; c++) {\n if (result[r][c] != ' ' || grid[r][c] != Stop) {\n continue;\n }\n for (const auto& m: movies) {\n const auto [nr, nc] = m.apply(r, c);\n if (!in_range(nr, nc)) {\n continue;\n }\n if (grid[nr][nc] == Stop) {\n result[r][c] = m.cmd;\n if (result[nr][nc] == ' ') {\n result[nr][nc] = m.inv_cmd;\n }\n }\n }\n }\n }\n for (const auto& row: result) {\n if (row.find(' ') != std::string::npos) {\n return false;\n }\n }\n return true;\n }\n};\n\nint main() {\n std::ios::sync_with_stdio(false);\n\n int size;\n std::cin >> size;\n Test test(size);\n for (auto& row: test.grid) {\n for (auto& p: row) {\n std::cin >> p.first >> p.second;\n if (p != Stop) {\n --p.first;\n --p.second;\n }\n }\n }\n\n if (test.solve()) {\n std::cout << \"VALID\\n\";\n for (const auto& row: test.result) {\n std::cout << row << '\\n';\n }\n } else {\n std::cout << \"INVALID\\n\";\n }\n\n return 0;\n}","language":"cpp"} -{"contest_id":"1303","problem_id":"C","statement":"C. Perfect Keyboardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him \u2014 his keyboard will consist of only one row; where all 2626 lowercase Latin letters will be arranged in some order.Polycarp uses the same password ss on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in ss, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in ss, so, for example, the password cannot be password (two characters s are adjacent).Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?InputThe first line contains one integer TT (1\u2264T\u226410001\u2264T\u22641000) \u2014 the number of test cases.Then TT lines follow, each containing one string ss (1\u2264|s|\u22642001\u2264|s|\u2264200) representing the test case. ss consists of lowercase Latin letters only. There are no two adjacent equal characters in ss.OutputFor each test case, do the following: if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem); otherwise, print YES (in upper case), and then a string consisting of 2626 lowercase Latin letters \u2014 the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them. ExampleInputCopy5\nababa\ncodedoca\nabcda\nzxzytyz\nabcdefghijklmnopqrstuvwxyza\nOutputCopyYES\nbacdefghijklmnopqrstuvwxyz\nYES\nedocabfghijklmnpqrstuvwxyz\nNO\nYES\nxzytabcdefghijklmnopqrsuvw\nNO\n","tags":["dfs and similar","greedy","implementation"],"code":"#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#include\n\n#define forin(i,a,n) for(int i=a;i<=n;i++)\n\n#define forni(i,n,a) for(int i=n;i>=a;i--)\n\n#define fi first\n\n#define se second\n\nusing namespace std;\n\ntypedef long long ll;\n\ntypedef double db;\n\ntypedef pair PII;\n\nconst double eps=1e-7;\n\nconst int N=3e5+7 ,M=2*N , INF=0x3f3f3f3f,mod=1e9+7;\n\ninline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}\n\nwhile(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}\n\nvoid stin() {freopen(\"in_put.txt\",\"r\",stdin);freopen(\"my_out_put.txt\",\"w\",stdout);}\n\n\n\ntemplate T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}\n\ntemplate T lcm(T a,T b) {return a*b\/gcd(a,b);}\n\n\n\nint T;\n\nint n,m,k;\n\nchar str[220];\n\npair w[30];\n\nbool sign;\n\nchar ans[30];\n\nbool st[30];\n\n\n\nbool path(char ch,char t) {\n\n int p=ch-'a';\n\n if(w[p].fi==t||w[p].se==t) return true;\n\n if(w[p].fi==0) {\n\n w[p].fi=t;\n\n return true;\n\n }else if(w[p].se==0) {\n\n w[p].se=t;\n\n return true;\n\n }else return false;\n\n}\n\n\n\nvoid dfs(int u) {\n\n if(u==3) {\n\n int res=0;\n\n if(w[ans[1]-'a'].fi!=0) res++;\n\n if(w[ans[1]-'a'].se!=0) res++;\n\n if(res==2) return ;\n\n if(res==1&&w[ans[1]-'a'].fi!=ans[2]) return ;\n\n }\n\n \n\n if(u>3&&u<=27) {\n\n char a=w[ans[u-2]-'a'].fi,b=w[ans[u-2]-'a'].se;\n\n if(a!=0&&b==0) {\n\n if(!(a==ans[u-3]||a==ans[u-1])) {\n\n return ;\n\n }\n\n }else if(a!=0&&b!=0) {\n\n if(!((a==ans[u-3]&&b==ans[u-1])||(a==ans[u-1]&&b==ans[u-3]))) {\n\n return ;\n\n }\n\n }\n\n }\n\n \n\n if(u>26) {\n\n if(w[ans[u-1]-'a'].se!=0) return ;\n\n if(w[ans[u-1]-'a'].fi!=0&&ans[u-2]!=w[ans[u-1]-'a'].fi) return ;\n\n sign=true;\n\n return ;\n\n }\n\n \n\n for(int i='a';i<='z';i++) {\n\n if(!st[i-'a']) {\n\n ans[u]=i;\n\n st[i-'a']=true;\n\n dfs(u+1);\n\n if(sign) return ;\n\n st[i-'a']=false;\n\n }\n\n }\n\n}\n\n\n\nvoid solve() { \n\n scanf(\"%s\",str+1);\n\n \n\n n=strlen(str+1);\n\n \n\n if(n==1) {\n\n printf(\"YES\\n\");\n\n for(int i='a';i<='z';i++) printf(\"%c\",i);\n\n printf(\"\\n\");\n\n return ;\n\n }\n\n \n\n memset(w,0,sizeof w);\n\n memset(st,false,sizeof st);\n\n memset(ans,0,sizeof ans);\n\n bool flag=true;\n\n sign=false;\n\n for(int i=2;i<=n;i++) {\n\n char a=str[i-1],b=str[i];\n\n if(!path(a,b)) {\n\n flag=false;\n\n break;\n\n }\n\n if(!path(b,a)) {\n\n flag=false;\n\n break;\n\n }\n\n }\n\n \n\n int x=0,y=0;\n\n for(int i=0;i<26;i++) {\n\n if(w[i].se!=0) x++;\n\n if(w[i].fi!=0) y++;\n\n }\n\n \n\n if(y-x<2) flag=false;\n\n \n\n if(!flag) printf(\"NO\\n\");\n\n else {\n\n dfs(1);\n\n if(!sign) printf(\"NO\\n\");\n\n else {\n\n printf(\"YES\\n\");\n\n for(int i=1;i<=26;i++) printf(\"%c\",ans[i]);\n\n printf(\"\\n\");\n\n }\n\n }\n\n}\n\n\n\nint main() {\n\n \/\/ init();\n\n \/\/ stin();\n\n \n\n scanf(\"%d\",&T);\n\n \/\/ T=1; \n\n while(T--) solve();\n\n \n\n return 0; \n\n} ","language":"cpp"} -{"contest_id":"1290","problem_id":"A","statement":"A. Mind Controltime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou and your n\u22121n\u22121 friends have found an array of integers a1,a2,\u2026,ana1,a2,\u2026,an. You have decided to share it in the following way: All nn of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.You are standing in the mm-th position in the line. Before the process starts, you may choose up to kk different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.Suppose that you're doing your choices optimally. What is the greatest integer xx such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to xx?Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.InputThe input consists of multiple test cases. The first line contains a single integer tt (1\u2264t\u226410001\u2264t\u22641000) \u00a0\u2014 the number of test cases. The description of the test cases follows.The first line of each test case contains three space-separated integers nn, mm and kk (1\u2264m\u2264n\u226435001\u2264m\u2264n\u22643500, 0\u2264k\u2264n\u221210\u2264k\u2264n\u22121) \u00a0\u2014 the number of elements in the array, your position in line and the number of people whose choices you can fix.The second line of each test case contains nn positive integers a1,a2,\u2026,ana1,a2,\u2026,an (1\u2264ai\u22641091\u2264ai\u2264109) \u00a0\u2014 elements of the array.It is guaranteed that the sum of nn over all test cases does not exceed 35003500.OutputFor each test case, print the largest integer xx such that you can guarantee to obtain at least xx.ExampleInputCopy4\n6 4 2\n2 9 2 3 8 5\n4 4 1\n2 13 60 4\n4 1 3\n1 2 2 1\n2 2 0\n1 2\nOutputCopy8\n4\n1\n1\nNoteIn the first test case; an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element (55) because he or she was forced by you to take the last element. After this turn the remaining array will be [2,9,2,3,8][2,9,2,3,8]; the second person will take the first element (22) because he or she was forced by you to take the first element. After this turn the remaining array will be [9,2,3,8][9,2,3,8]; if the third person will choose to take the first element (99), at your turn the remaining array will be [2,3,8][2,3,8] and you will take 88 (the last element); if the third person will choose to take the last element (88), at your turn the remaining array will be [9,2,3][9,2,3] and you will take 99 (the first element). Thus, this strategy guarantees to end up with at least 88. We can prove that there is no strategy that guarantees to end up with at least 99. Hence, the answer is 88.In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 44.","tags":["brute force","data structures","implementation"],"code":"#include \"bits\/stdc++.h\"\n\nusing namespace std;\n\n\n\n\/\/ #define ORD_SET\n\n\/\/ #define ROPE\n\n#ifdef ORD_SET\n\n #include \n\n #include \n\n using namespace __gnu_pbds;\n\n template\n\n using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>;\n\n#endif\n\n#ifdef ROPE\n\n #include \n\n using namespace __gnu_cxx;\n\n#endif \n\n\n\n\/\/ #pragma comment(linker, \"\/stack:200000000\")\n\n\/\/ #pragma GCC optimize(\"Ofast\")\n\n\/\/ #pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native\")\n\n\n\nusing ll = long long;\n\nusing ld = long double;\n\n#define pb push_back\n\n#define ff first\n\n#define ss second\n\n#define sz(x) (ll)(x).size()\n\n#define all(x) (x).begin(), (x).end()\n\n#define rall(x) (x).rbegin(), (x).rend()\n\n\/\/ #define ll int\n\nvoid freopen(string s) { freopen((s + \".in\").c_str(), \"r\", stdin); freopen((s + \".out\").c_str(), \"w\", stdout); }\n\nvoid NotInteractive() { ios_base::sync_with_stdio(false); cin.tie(NULL); }\n\nvoid pre(ll a) { cout<>= 1; } return pow; }\n\ntemplate T gcd(T a, T b) { if(b==0) return a; return gcd(b, a%b); }\n\ntemplate T lcm(T a, T b) { return a\/gcd(a, b)*b; }\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\n\n\nstruct sn { ll l, r, x; }; \/\/ a.swap(b)\n\nll B = 316;\n\nconst ll mod = (ll)1e9+7;\n\nconst ll inf = (ll)1e18+7;\n\nconst ll MX = LLONG_MAX;\n\nconst ll MN = LLONG_MIN;\n\nconst ld P = acos(-1.0);\n\nconst ll N = (ll)3e5+5;\n\nll a[N];\n\n\n\nvoid kigash() {\n\n ll n, m, k;\n\n cin>>n>>m>>k;\n\n for(ll i=1; i<=n; i++) cin>>a[i];\n\n k = min(k, m-1);\n\n ll d = m-1-k, ans = MN;\n\n for(ll j=0; j<=k; j++) {\n\n ll mn = MX;\n\n for(ll i=0; i<=d; i++) mn = min(mn, max(a[j+i+1], a[j+i+1+n-m]));\n\n ans = max(ans, mn);\n\n }\n\n cout<>tt;\n\n for(ll i=1; i<=tt; i++) kigash();\n\n \n\n exit(0);\n\n}","language":"cpp"} -{"contest_id":"1300","problem_id":"A","statement":"A. Non-zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGuy-Manuel and Thomas have an array aa of nn integers [a1,a2,\u2026,ana1,a2,\u2026,an]. In one step they can add 11 to any element of the array. Formally, in one step they can choose any integer index ii (1\u2264i\u2264n1\u2264i\u2264n) and do ai:=ai+1ai:=ai+1.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make a1+a2+a1+a2+ \u2026\u2026 +an\u22600+an\u22600 and a1\u22c5a2\u22c5a1\u22c5a2\u22c5 \u2026\u2026 \u22c5an\u22600\u22c5an\u22600.InputEach test contains multiple test cases. The first line contains the number of test cases tt (1\u2264t\u22641031\u2264t\u2264103). The description of the test cases follows.The first line of each test case contains an integer nn (1\u2264n\u22641001\u2264n\u2264100)\u00a0\u2014 the size of the array.The second line of each test case contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (\u2212100\u2264ai\u2264100\u2212100\u2264ai\u2264100)\u00a0\u2014 elements of the array .OutputFor each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.ExampleInputCopy4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1\nOutputCopy1\n2\n0\n2\nNoteIn the first test case; the sum is 00. If we add 11 to the first element, the array will be [3,\u22121,\u22121][3,\u22121,\u22121], the sum will be equal to 11 and the product will be equal to 33.In the second test case, both product and sum are 00. If we add 11 to the second and the third element, the array will be [\u22121,1,1,1][\u22121,1,1,1], the sum will be equal to 22 and the product will be equal to \u22121\u22121. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding 11 twice to the first element the array will be [2,\u22122,1][2,\u22122,1], the sum will be 11 and the product will be \u22124\u22124.","tags":["implementation","math"],"code":"\/\/ LUOGU_RID: 100979414\n#include\n\nusing namespace std;\n\nint main()\n\n{\n\n\tint t;\n\n\tcin>>t;\n\n\twhile(t--)\n\n\t{\n\n\t\tint n;\n\n\t\tcin>>n;\n\n\t\tint sum=0,ans=0;\n\n\t\tfor(int i=1;i<=n;i++)\n\n\t\t{\n\n\t\t\tint x;\n\n\t\t\tcin>>x;\n\n\t\t\tsum+=x;\n\n\t\t\tif(x==0)\n\n\t\t\t{\n\n\t\t\t\tsum++,ans++;\n\n\t\t\t}\n\n\t\t}\t\n\n\t\tif(sum==0)\n\n\t\t{\n\n\t\t\tans++;\n\n\t\t}\n\n\t\tcout<\nusing namespace std;\n#define int long long \n#define MODY ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\nsigned main(){\n int t;\n cin >> t;\n while(t--){\n int n;\n cin >> n;\n int a[n];\n for (int i = 0; i> a[i];\n bool found = false;\n for (int i =0; i\n\n#define int long long\n\n#define repp(n) for(int i=0;i>n;\n\n int a[n];repp(n)cin>>a[i];\n\n int sum=0,cnt=0;\n\n repp(n){\n\n sum+=a[i];\n\n if(a[i]==0)\n\n cnt++;\n\n }\n\n\n\n if(sum+cnt==0){\n\n cout<>t;while(t--)\n\n solve();\n\n return 0;\n\n}\n\n","language":"cpp"} -{"contest_id":"1285","problem_id":"D","statement":"D. Dr. Evil Underscorestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; as a friendship gift, Bakry gave Badawy nn integers a1,a2,\u2026,ana1,a2,\u2026,an and challenged him to choose an integer XX such that the value max1\u2264i\u2264n(ai\u2295X)max1\u2264i\u2264n(ai\u2295X) is minimum possible, where \u2295\u2295 denotes the bitwise XOR operation.As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of max1\u2264i\u2264n(ai\u2295X)max1\u2264i\u2264n(ai\u2295X).InputThe first line contains integer nn (1\u2264n\u22641051\u2264n\u2264105).The second line contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (0\u2264ai\u2264230\u221210\u2264ai\u2264230\u22121).OutputPrint one integer \u2014 the minimum possible value of max1\u2264i\u2264n(ai\u2295X)max1\u2264i\u2264n(ai\u2295X).ExamplesInputCopy3\n1 2 3\nOutputCopy2\nInputCopy2\n1 5\nOutputCopy4\nNoteIn the first sample; we can choose X=3X=3.In the second sample, we can choose X=5X=5.","tags":["bitmasks","brute force","dfs and similar","divide and conquer","dp","greedy","strings","trees"],"code":"#include\n\n#include\n\n#include\n\n#include\n\nusing namespace std;\n\n\/\/ #define debug(x) cout<<\"[debug]\"#x<<\"=\"< pii;\n\nconst double eps=1e-8;\n\nconst int INF=0x3f3f3f3f,mod=998244353;\n\n\n\n#ifndef ONLINE_JUDGE\n\n#define debug(...)\n\n#include\n\n#else\n\n#define debug(...)\n\n#endif\n\n\n\nconst int N=100005;\n\nint n;\n\nint dcq(vector ver,int w)\n\n{\n\n vector a;\n\n vector b;\n\n for(auto x:ver)\n\n {\n\n if(x>>w&1) a.push_back(x);\n\n else b.push_back(x^(1< ver;\n\n for(int i=1;i<=n;i++)\n\n {\n\n int x;\n\n scanf(\"%d\",&x);\n\n ver.push_back(x);\n\n }\n\n\n\n int res=dcq(ver,30);\n\n printf(\"%d\\n\",res);\n\n}","language":"cpp"} -{"contest_id":"13","problem_id":"C","statement":"C. Sequencetime limit per test1 secondmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes to play very much. And most of all he likes to play the following game:He is given a sequence of N integer numbers. At each step it is allowed to increase the value of any number by 1 or to decrease it by 1. The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math; so he asks for your help.The sequence a is called non-decreasing if a1\u2009\u2264\u2009a2\u2009\u2264\u2009...\u2009\u2264\u2009aN holds, where N is the length of the sequence.InputThe first line of the input contains single integer N (1\u2009\u2264\u2009N\u2009\u2264\u20095000) \u2014 the length of the initial sequence. The following N lines contain one integer each \u2014 elements of the sequence. These numbers do not exceed 109 by absolute value.OutputOutput one integer \u2014 minimum number of steps required to achieve the goal.ExamplesInputCopy53 2 -1 2 11OutputCopy4InputCopy52 1 1 1 1OutputCopy1","tags":["dp","sortings"],"code":"#include \nusing namespace std;\n\ntypedef long long int ll;\n\nint main()\n{\n\tll n; cin >> n;\n\n\tpriority_queue pq;\n\n\tll ans = 0;\n\n\tfor (ll i = 0; i < n; i++)\n\t{\n\t\tll x; cin >> x;\n\n\t\tif (pq.empty())\n\t\t{\n\t\t\tpq.push(x);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (pq.top() > x)\n\t\t\t{\n\t\t\t\tans += pq.top() - x;\n\n\t\t\t\tpq.pop();\n\n\t\t\t\tpq.push(x);\n\t\t\t}\n\n\t\t\tpq.push(x);\n\t\t}\n\t}\n\tcout << ans << endl;\n}\n \t\t\t \t\t\t \t \t \t\t\t \t \t","language":"cpp"} -{"contest_id":"1300","problem_id":"A","statement":"A. Non-zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGuy-Manuel and Thomas have an array aa of nn integers [a1,a2,\u2026,ana1,a2,\u2026,an]. In one step they can add 11 to any element of the array. Formally, in one step they can choose any integer index ii (1\u2264i\u2264n1\u2264i\u2264n) and do ai:=ai+1ai:=ai+1.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make a1+a2+a1+a2+ \u2026\u2026 +an\u22600+an\u22600 and a1\u22c5a2\u22c5a1\u22c5a2\u22c5 \u2026\u2026 \u22c5an\u22600\u22c5an\u22600.InputEach test contains multiple test cases. The first line contains the number of test cases tt (1\u2264t\u22641031\u2264t\u2264103). The description of the test cases follows.The first line of each test case contains an integer nn (1\u2264n\u22641001\u2264n\u2264100)\u00a0\u2014 the size of the array.The second line of each test case contains nn integers a1,a2,\u2026,ana1,a2,\u2026,an (\u2212100\u2264ai\u2264100\u2212100\u2264ai\u2264100)\u00a0\u2014 elements of the array .OutputFor each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.ExampleInputCopy4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1\nOutputCopy1\n2\n0\n2\nNoteIn the first test case; the sum is 00. If we add 11 to the first element, the array will be [3,\u22121,\u22121][3,\u22121,\u22121], the sum will be equal to 11 and the product will be equal to 33.In the second test case, both product and sum are 00. If we add 11 to the second and the third element, the array will be [\u22121,1,1,1][\u22121,1,1,1], the sum will be equal to 22 and the product will be equal to \u22121\u22121. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding 11 twice to the first element the array will be [2,\u22122,1][2,\u22122,1], the sum will be 11 and the product will be \u22124\u22124.","tags":["implementation","math"],"code":"\/\/ CF template, version 3.0\n\n\n\n#include \n\n\n\nusing namespace std;\n\n\n\n#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)\n\n#define getTest int t; cin >> t\n\n#define eachTest for (int _var=0;_var> (name)\n\n#define out(o) cout << (o)\n\n#define getList(cnt, name) vector (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }\n\n#define sortl(name) sort((name).begin(), (name).end())\n\n#define rev(name) reverse((name).begin(), (name).end())\n\n#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)\n\n#define decision(b) if (b){out(\"YES\");}else{out(\"NO\");}\n\n\n\n#define int long long int\n\n\n\nint gcd(int a, int b) {\n\n if (a == b) return a;\n\n return gcd(max(a, b), max(a, b) % min(a, b));\n\n}\n\n\n\n\/*\n\nSegtree:\n\nvoid build(vector &tree, vector &array, int i, int l, int r) {\n\n if (l == r) {\n\n tree[i] = array[l];\n\n } else {\n\n int middle = (l + r) \/ 2;\n\n build(tree, array, i * 2, l, middle);\n\n build(tree, array, i * 2 + 1, middle + 1, r);\n\n\n\n tree[i] = max(tree[i * 2], tree[i * 2 + 1]);\n\n }\n\n}\n\n\n\nvoid set_(vector &tree, int v, int l, int r, int i, int x) {\n\n if (l == r) {\n\n tree[v] = x;\n\n } else {\n\n int middle = (l + r) \/ 2;\n\n if (i <= middle) {\n\n set_(tree, v * 2, l, middle, i, x);\n\n } else {\n\n set_(tree, v * 2 + 1, middle + 1, r, i, x);\n\n }\n\n tree[v] = max(tree[v * 2], tree[v * 2 + 1]);\n\n }\n\n}\n\n\n\nint query(vector &tree, int v, int cl, int cr, int l, int r) {\n\n if (l == cl && r == cr) {\n\n return tree[v];\n\n }\n\n if (l > r) return 0;\n\n int middle = (cl + cr) \/ 2;\n\n return max(\n\n query(tree, v * 2, cl, middle, l, min(r, middle)),\n\n query(tree, v * 2 + 1, middle + 1, cr, max(l, middle + 1), r)\n\n );\n\n}\n\n\n\nvector tree(4 * n);\n\nbuild(tree, initial, 1, 0, n-1); \/\/ replace initial with name of array\n\nset_(tree, 1, 0, n-1, a, b);\n\nquery(tree, 1, 0, n-1, a, b); \/\/ this returns an int\n\ndata structure can be changed\n\n*\/\n\n\n\nsigned main() {\n\n improvePerformance;\n\n getTest;\n\n \n\n eachTest {\n\n get(n);\n\n getList(n, nums);\n\n\n\n int sum = 0;\n\n int zeroes = 0;\n\n for (int num: nums) {\n\n sum += num;\n\n if (!num) zeroes++;\n\n }\n\n\n\n int steps = zeroes;\n\n if (sum + zeroes == 0) steps++;\n\n\n\n out(steps);\n\n\n\n out(\"\\n\");\n\n }\n\n}","language":"cpp"} -{"contest_id":"1303","problem_id":"A","statement":"A. Erasing Zeroestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string ss. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase?InputThe first line contains one integer tt (1\u2264t\u22641001\u2264t\u2264100) \u2014 the number of test cases.Then tt lines follow, each representing a test case. Each line contains one string ss (1\u2264|s|\u22641001\u2264|s|\u2264100); each character of ss is either 0 or 1.OutputPrint tt integers, where the ii-th integer is the answer to the ii-th testcase (the minimum number of 0's that you have to erase from ss).ExampleInputCopy3\n010011\n0\n1111000\nOutputCopy2\n0\n0\nNoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111).","tags":["implementation","strings"],"code":"#include \n\n#include \n\nusing namespace std;\n\n\n\n\n\n#define si size\n\n#define f first\n\n#define s second\n\n#define vec vector\n\n#define pb push_back\n\n\n\ntypedef vec vi;\n\n\n\nvoid solve(){\n\n vi seq;\n\n pair res;\n\n string s;\n\n int n, cnt = 0;\n\n cin >> s;\n\n n = s.length();\n\n\n\n for(int i = 0; n > i; i++){\n\n seq.pb(int(s[i]) - 48);\n\n }\n\n\n\n for(int i = 0, j = 0; n > i; i++){\n\n if(i == 0){\n\n res.f.pb(seq[0]);\n\n res.s.pb(1);\n\n }\n\n else if(seq[i] != res.f[j]){\n\n res.f.pb(seq[i]);\n\n res.s.pb(1);\n\n j++;\n\n }\n\n else res.s[j] += 1;\n\n }\n\n\n\n if(res.f.si() == 1 && res.f[0] == 1) \n\n cout << \"0\\n\";\n\n else if(res.f.si() == 2 && res.f[0] == 0 && res.f[1] == 1)\n\n cout << \"0\\n\";\n\n else if(res.f.si() == 2 && res.f[0] == 1 && res.f[1] == 0)\n\n cout << \"0\\n\";\n\n else if(res.f.si() == 3 && res.f[0] == 0 && res.f[1] == 1 && res.f[2] == 0)\n\n cout << \"0\\n\";\n\n else{\n\n for(int i = 1; res.f.si() - 1 > i; i++){\n\n if(res.f[i] == 0) cnt += res.s[i];\n\n if(res.s[i] == 0) break;\n\n }\n\n cout << cnt << endl;\n\n }\n\n}\n\n\n\nint main(){\n\n int t; cin >> t;\n\n while(t--) solve();\n\n}\n\n\n\n\/\/ :D","language":"cpp"} -{"contest_id":"1322","problem_id":"A","statement":"A. Unusual Competitionstime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputA bracketed sequence is called correct (regular) if by inserting \"+\" and \"1\" you can get a well-formed mathematical expression from it. For example; sequences \"(())()\", \"()\" and \"(()(()))\" are correct, while \")(\", \"(()\" and \"(()))(\" are not.The teacher gave Dmitry's class a very strange task\u00a0\u2014 she asked every student to come up with a sequence of arbitrary length, consisting only of opening and closing brackets. After that all the students took turns naming the sequences they had invented. When Dima's turn came, he suddenly realized that all his classmates got the correct bracketed sequence, and whether he got the correct bracketed sequence, he did not know.Dima suspects now that he simply missed the word \"correct\" in the task statement, so now he wants to save the situation by modifying his sequence slightly. More precisely, he can the arbitrary number of times (possibly zero) perform the reorder operation.The reorder operation consists of choosing an arbitrary consecutive subsegment (substring) of the sequence and then reordering all the characters in it in an arbitrary way. Such operation takes ll nanoseconds, where ll is the length of the subsegment being reordered. It's easy to see that reorder operation doesn't change the number of opening and closing brackets. For example for \"))((\" he can choose the substring \")(\" and do reorder \")()(\" (this operation will take 22 nanoseconds).Since Dima will soon have to answer, he wants to make his sequence correct as fast as possible. Help him to do this, or determine that it's impossible.InputThe first line contains a single integer nn (1\u2264n\u22641061\u2264n\u2264106)\u00a0\u2014 the length of Dima's sequence.The second line contains string of length nn, consisting of characters \"(\" and \")\" only.OutputPrint a single integer\u00a0\u2014 the minimum number of nanoseconds to make the sequence correct or \"-1\" if it is impossible to do so.ExamplesInputCopy8\n))((())(\nOutputCopy6\nInputCopy3\n(()\nOutputCopy-1\nNoteIn the first example we can firstly reorder the segment from first to the fourth character; replacing it with \"()()\"; the whole sequence will be \"()()())(\". And then reorder the segment from the seventh to eighth character; replacing it with \"()\". In the end the sequence will be \"()()()()\"; while the total time spent is 4+2=64+2=6 nanoseconds.","tags":["greedy"],"code":"#include\n\nusing namespace std;\n\n#define int long long\n\nconst int N=1e6+10;\n\nsigned main()\n\n{\n\n int n;cin>>n;\n\n string s;cin>>s;\n\n int a=0,b=0;\n\n int c=0;\n\n int res=0;\n\n for(int i=0;i0) c--;\n\n }\n\n if(a==b) \n\n {\n\n if(c)\n\n res+=2*a;\n\n a=b=c=0;\n\n }\n\n }\n\n if(a!=b) puts(\"-1\");\n\n else cout<