Advertisement
dpuydv

Untitled

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