Advertisement
dpuydv

Untitled

Aug 30th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2. #include<istream>
  3. #include<fstream>
  4. #include<ext/pb_ds/assoc_container.hpp>
  5. #include<ext/pb_ds/tree_policy.hpp>
  6.  
  7. using namespace std;
  8. using namespace chrono;
  9. using namespace __gnu_pbds;
  10.  
  11. // Macros definition
  12. #define ll long long
  13. #define lld long double
  14. #define inf 1e18
  15.  
  16. template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;
  17. template<class key, class value, class cmp = std::less<key>> using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
  18. // x.find_by_order(1) -> return pointer to element present at index 1
  19. // x.order_of_key(2) -> return index of element 2
  20.  
  21. #ifndef ONLINE_JUDGE
  22. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  23. #else
  24. #define debug(x)
  25. #endif
  26.  
  27. template<class T> void _print(T &x) {cerr << x;}
  28.  
  29. template <class T, class V> void _print(pair <T, V> p);
  30. template <class T> void _print(vector <T> v);
  31. template <class T> void _print(set <T> v);
  32. template <class T, class V> void _print(map <T, V> v);
  33. template <class T> void _print(multiset <T> v);
  34. template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
  35. template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
  36. template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
  37. template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
  38. template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
  39.  
  40. //To make unordered_map unhackable
  41. // use it as unordered_map<int,int,custom_hash> mapp;
  42. struct custom_hash {
  43.     static uint64_t splitmix64(uint64_t x) {
  44.         /* http://xorshift.di.unimi.it/splitmix64.c */
  45.         x += 0x9e3779b97f4a7c15;
  46.         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
  47.         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
  48.         return x ^ (x >> 31);
  49.     }
  50.  
  51.     size_t operator()(uint64_t x) const {
  52.         static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
  53.         return splitmix64(x + FIXED_RANDOM);
  54.     }
  55. };
  56.  
  57. // Predefined values
  58. const ll mod = 1e9+7;
  59. const int N = 4010;
  60. int s[4010][4010];
  61. int v[N*N];
  62.  
  63. void solve(){
  64.     int n,q;
  65.     cin>>n>>q;
  66.     // scanf("%d%d",&n,&q);
  67.     for(int i=1; i<=n*n; i++){
  68.         cin>>v[i];
  69.         // scanf("%d",&v[i]);
  70.         s[i%(2*n)][(i+2*n)/(2*n)] = s[i%(2*n)][(i+2*n)/(2*n)-1] + v[i];
  71.     }
  72.  
  73.     int t,x;
  74.     int p1, p2;
  75.     int ans=0;
  76.     for(int i=0; i<q; i++){
  77.         // int t,x;
  78.         cin>>t>>x;
  79.         // scanf("%d%d",&t,&x);
  80.         p1 = t-x+1, p2 = t-2*n+x;
  81.         ans=0;
  82.         if(p1>0) ans += s[p1%(2*n)][(p1+2*n)/(2*n)];
  83.         if(p2>0) ans += s[p2%(2*n)][(p2+2*n)/(2*n)];
  84.         // cout<<ans<<endl;
  85.         printf("%d\n",ans);
  86.     }
  87. }
  88.  
  89. int main(){
  90.    
  91.     // Deepu Yadav
  92.     // ios_base :: sync_with_stdio(false);
  93.     // cin.tie(NULL);
  94.     // cout.tie(NULL);
  95.  
  96.     #ifndef ONLINE_JUDGE
  97.     // freopen("Error.txt", "w", stderr);
  98.     #endif
  99.  
  100.     int t=1;
  101.     // cin>>t;
  102.     while(t--){
  103.         solve();
  104.     }
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement