Общий форумWA6: #include <iostream> #pragma comment(linker, "/STACK:46777216") #include <vector> using namespace std; #define ll int64_t const int MAXN = 50005; ll t[4*MAXN], add[4*MAXN], sz[MAXN]; ll n, q, x, cnt; ll y, z, value[MAXN]; char c[15]; vector <ll> g[MAXN]; ll p[MAXN], pos[MAXN]; void init(ll v, ll tl, ll tr) { if(tl == tr) add[v]=(ll)0, t[v]=value[tl]; else { ll m=(tl+tr)/2; init(2*v, tl, m); init(2*v+1, m+1,tr); add[v]=(ll)0, t[v]=t[2*v]+t[2*v+1]; } } ll sum(ll v, ll tl, ll tr, ll l, ll r) { if(tl == l && tr == r) return t[v]; else { ll m = (tl + tr) /2 ; ll a = add[v] * (ll)(r-l+1); if(r<=m) return a+sum(2*v, tl, m, l, r); if(l>m) return a+sum(2*v + 1, m+1, tr, l, r); return a+sum(2*v, tl, m, l, m) + sum(2*v + 1, m+1, tr, m+1, r); } } void update(ll v, ll tl, ll tr, ll l, ll r, ll val) { if(tl == l && tr == r) add[v]+=val, t[v]+=val*(ll)(tr-tl+1); else { ll m = (tl + tr) /2; if(r<=m) update(2*v , tl, m, l, r, val); else { if(l>m) update(2*v + 1, m+1, tr, l, r, val); else update(2*v , tl, m, l, m, val), update(2*v + 1, m+1, tr, m+1, r, val); } t[v]=t[2*v]+t[2*v +1]; } }; void dfs(ll v, ll par = -1) { p[v] = par, pos[v] = ++cnt, sz[v] = (ll)1; for (ll i = 0; i < (ll) g[v].size(); i++) { ll to = g[v][i]; if (to == par) continue; dfs(to, v); sz[v] += sz[to]; } } int main() { scanf("%lld %lld %lld", &n, &q, &value[1]); for (ll i = 2; i <= n; i++) { scanf("%lld %lld\n", &x, &value[i]); x++; g[x].push_back(i); g[i].push_back(x); } cnt = 0; dfs(1);
init(1, 1, n);
for (ll i = 1; i <= q; i++) { scanf("%s %lld %lld %lld\n",&c, &x, &y, &z); x++; if (c[0] == 'e') { ll salary=sum(1,1,n,pos[x],pos[x]); if(salary<y) update(1,1,n,pos[x],pos[x],z); } else { ll departament = sum(1,1,n,pos[x],pos[x]+sz[x]-1); if(departament<y*sz[x]) update(1,1,n,pos[x],pos[x]+sz[x]-1,z); } }
for(ll i=1;i<=n;i++) { ll salary=sum(1,1,n,pos[i],pos[i]); printf("%lld", salary); if(i<n) printf("\n"); } return 0; } WA8: #include <iostream> #pragma comment(linker, "/STACK:46777216") #include <vector> using namespace std; #define ll unsigned long long const int MAXN = 50005; ll sum[MAXN], add[MAXN], sz[MAXN], L[MAXN], R[MAXN]; int n, q, x, cnt, c; ll y, z, value[MAXN], len; char str[15]; vector <int> g[MAXN]; int p[MAXN], pos[MAXN], ind[MAXN]; void Create() { len=(ll)1; while(len*len<n) len++; if(len*len>n) len--; c= n / len; for(int i=1;i<=c;i++) L[i]=(ll)(1+len*(i-1)), R[i]=(ll)(len*i); if(c*len<n) c++, L[c]=R[c-1]+1, R[c]=n;
for(int i=1;i<=c;i++) { sum[i]=(ll)0, add[i]=(ll)0; for(int j=L[i];j<=R[i];j++) sum[i]+=value[j], ind[j]=i; } } void Update(int l, int r, ll val) { int k1=ind[l]; int k2=ind[r];
for(int i=k1+1;i<=k2-1;i++) sum[i]+=val*(ll)(R[i]-L[i]+1), add[i]+=val;
if(k1==k2) { for(int i=l;i<=r;i++) value[i]+=val, sum[k1]+=val; } else { if(l==L[k1]) sum[k1]+=val*(ll)(R[k1]-L[k1]+1), add[k1]+=val; else { for(int i=l;i<=R[k1];i++) value[i]+=val, sum[k1]+=val; }
if(r=R[k2]) sum[k2]+=val*(ll)(R[k2]-L[k2]+1), add[k2]+=val; else { for(int i=L[k2];i<=r;i++) value[i]+=val, sum[k2]+=val; } } } ll Sum(int l, int r) { int k1=ind[l]; int k2=ind[r]; ll res=(ll)0;
for(int i=k1+1;i<=k2-1;i++) res+=sum[i];
if(k1==k2) { for(int i=l;i<=r;i++) res+=value[i]; res+=add[k1]*(ll)(r-l+1); } else { for(int i=l;i<=R[k1];i++) res+=value[i]; res+=add[k1]*(ll)(R[k1]-l+1);
for(int i=L[k2];i<=r;i++) res+=value[i]; res+=add[k2]*(ll)(r-L[k2]+1); } return res; } void Dfs(int v, int par = -1) { p[v] = par, pos[v] = ++cnt, sz[v] = (ll)1; for (int i = 0; i < (int) g[v].size(); i++) { int to = g[v][i]; if (to == par) continue; Dfs(to, v); sz[v] += sz[to]; } } int main() { scanf("%d%d%lld\n", &n, &q, &value[1]); for (int i = 2; i <= n; i++) { scanf("%d%lld\n", &x, &value[i]); x++; g[x].push_back(i); g[i].push_back(x); }
Dfs(1); Create();
scanf("\n"); for (int i = 1; i <= q; i++) { scanf("%s%d%lld%lld\n",&str, &x, &y, &z); x++; if (str[0] == 'e') { ll salary=Sum(pos[x],pos[x]); if(salary<y) Update(pos[x],pos[x],z); } else { ll departament = Sum(pos[x],pos[x]+sz[x]-1); if(departament<y*sz[x]) Update(pos[x],pos[x]+sz[x]-1,z); } }
for(int i=1;i<=n;i++) { ll salary=Sum(pos[i],pos[i]); printf("%lld", salary); if(i<n) printf("\n"); } return 0; } Edited by author 10.03.2018 23:40 #include <iostream> #include <string> using namespace std; int main() { int n,m=1; string k; cin>>n; cin>>k; int j=k.length(); for(int i=n;i>=(n%j);i-=j){ m=m*i; } cout<<m<<k; return 0; } #include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; pair<int ,int > a[150000]; for(int i=0;i<n;i++) { cin >> a[i].first >> a[i].second; } for(int i=100;i>=0;--i) { for(int j=0;j<n;j++) { if(i==a[j].second) cout << a[j].first << " " << a[j].second << endl; } } } WA #2 a = input().split() s = input().split() n, k = int(a[0]), int(a[1]) out = ["" for x in range(n//k+1)] for i in range(k): if i != k - 1: for j in range(n//k+1): if len(s[0]) == 1: out[j] += " " + s[0] s.pop(0) elif len(s[0]) == 2: out[j] += " " + s[0] s.pop(0) else: out[j] += " " + s[0] s.pop(0) else: for j in range(n%k): if len(s[0]) == 1: out[j] += " " + s[0] s.pop(0) elif len(s[0]) == 2: out[j] += " " + s[0] s.pop(0) else: out[j] += " " + s[0] s.pop(0) for i in out: print(i) Edited by author 09.03.2018 19:59 Yes, use interval trees. wall 0 1 1 0 shot 0 1 Infinity or 1? that will cause a precision error I think get Accepted using divide and conquer I think raytraycer based on kd-tree (SAH) with ropes is most suitable for this task. Time (just sequential number) can be saved in primitives and min of times in nodes to speedup calculations. It seems that the main problem is precision. If it is so, how could we compute the answer more accurately? e.g., set what Epsilon threshold for determining a value being 0 ? There are some methods to solve problem. 1. Gauss. It really has troubles with precision. 2. Method of iterations. It works good, but your implementation should be fast enough and do about 2^50 iterations :) Thanks for your reply. :) I used Gaussian Elimination. Seems it's very difficult to handle the precision... We tried both methods but failed... It seems that using iterations could lead to precision problem too..we calc something like mat[64][64],and do mat^(2^60),but it turns out that the value in mat overflows... Sorry for my poor english-_- Just add something like that after each multiplication. void normalize(double a[][64]) { for(int i = 0; i < 64; i++) { double sum = 0; for(int j = 0; j < 64; j++) sum += a[i][j]; for(int j = 0; j < 64; j++) a[i][j] /= sum; } } Thanks a lot, Nikita! This normailze function really helps!
My program needs only 2^26 iterations using it, and without this function i received WA #1 all the time. Edited by author 17.06.2010 14:28 I used Gauss. But with BigDecimal. Can you explain why iterations more precize than gauss? Why you think that it is necessary about 2^50 iterations? I think that there exists a good alternative to Gaussian elimination - QR - decomposition of the matrix. It's precision, I think, would be good enough because it doesn't change the condition number of the matrix. I tried to solve it as you said. TL #3 ... QR - decomposition is only a bit slower than Gaussian elimination, and it's also O(n^3) Could you expalin what is the QR-decomposition? Maybe, I didn't understand you very well. Thanks a lot! But I don't understand how could it be useful to avoid big precision troubles with Gauss algorithm. I don't understand too. :-[ These methods have less calculation errors than Gauss method. But there is modification of Gauss method which more precise than QR-decomposition methods. In this modification we choose main element from all remaining elements in matrix. You can read about methods for solving systems of linear algebraic equations in the book: А.А.Амосов "Вычислительные методы для инженеров". Of course, there are a lot of other sources. Just want to mention that I kept getting WA and after changing double to long double immediately got AC. Maybe it helps output limit exceeded! what's the case 4 like? 3Q #include<stdio.h> int main() { int n , m , sum=0 ; double ava; scanf("%d",&n); int mark[11]; for(m=0;m<n;m++){ scanf("%d",&mark[m]); } for(m=0;m<n;m++){ sum=sum+mark[m]; } ava=(double)sum/n; if(ava<=3)printf("None\n"); else if (ava>=5)printf("Named\n"); else if(ava>=4.5)printf("High\n"); else printf("Common\n"); } My solution is crushed on 11 test. please help me... Any hints? be careful don't compute repeated floyd[i][j]=true only once Hello all. I am doing task #1701, but ,actually, my question is also related to other equal tasks: how can you not exceed the time limit when the task supposes large array as the answer? For instance, in mentioned task the time is limited to 2 seconds. In case when N == 50 000, if possible set of emplyee's salaries exists, you need write 50 000 strings to console. But, as i have found out by measurig, only outputting array to the console takes over 3-5 seconds. Test it using C# and C++ with array of int values set to "0". Edited by author 05.03.2018 09:04 If you use set<A*> or std::sort/std::stable_sort vector/array of pointers, your comparator won't work, because you need special comparator, like this struct APtrCmp { bool operator()(const A* lhs, const A* rhs) const { /* implement logic here */ } }; set<A*, APtrCmp> yourSet; Good luck! ;) #include <stdio.h> #include <math.h> int plus(int number){ return number*(number+1)/2; } int minus(int number){ return (-(number*(number+1)/2))+number; } int main(){ int N; scanf("%d",&N); if(abs(N)>=1 && abs(N)<=10000){ if(N<0) printf("%d",minus(N)); else printf("%d",plus(N)); } } Why do you think, that 0 is prohibited input? What the reason to check input at all? "if(abs(N)>=1 && abs(N)<=10000)" is superfluous. The statement of the problem can't lie. Also minus() is wrong. Thanks you so much. I'm new to olympiad programming and I sometimes make silly mistakes thanks to pointing to them Edited by author 03.03.2018 09:54 Edited by author 03.03.2018 09:53 this task is the same as 1075, but with a different number of dimensions(it doesn't really matter) 3 different solutions gets WA24. Is this test correct or I don't understand the statement? ASK C++17 [3] 28 фев 2018 14:35 If we run GCC 7.1, we can as well use -std=gnu++17 I have no G++ 7.1 to check. But G++ 7.2 said you are correct. Before sending your solution try this test on your pc and make sure that pc can calculate it faster than 10 hours :)) 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 1 |
|